#include "media-util.h"
#define BUFSIZE 4096
-static __thread char **sql_list = NULL;
-static __thread int g_list_idx = 0;
+#define BATCH_START "BEGIN"
+#define BATCH_END "COMMIT"
+
+static __thread GArray *sql_list = NULL;
static int __media_db_busy_handler(void *pData, int count);
static int __media_db_connect_db_with_handle(sqlite3 **db_handle, uid_t uid, bool need_write);
static void __media_db_destroy_sql_list()
{
- int i = 0;
+ if (sql_list != NULL) {
+ while (sql_list->len != 0) {
+ char *sql = g_array_index(sql_list, char *, 0);
+ g_array_remove_index(sql_list, 0);
- for (i = 0; i < g_list_idx; i++)
- MS_SAFE_FREE(sql_list[i]);
+ MS_SAFE_FREE(sql);
+ }
- MS_SAFE_FREE(sql_list);
- g_list_idx = 0;
+ g_array_unref(sql_list);
+ sql_list = NULL;
+ }
}
static int __media_db_busy_handler(void *pData, int count)
int media_db_update_db_batch_start(const char *query_str)
{
int ret = MS_MEDIA_ERR_NONE;
+ char *query = NULL;
MSAPI_RETVM_IF(!MS_STRING_VALID(query_str), MS_MEDIA_ERR_INVALID_PARAMETER, "Invalid Query");
- if (g_list_idx != 0) {
- MSAPI_DBG_ERR("Current idx is not 0");
+ if (sql_list != NULL) {
+ MSAPI_DBG_ERR("sql_list is already allocated");
ret = MS_MEDIA_ERR_DB_SERVER_BUSY_FAIL;
} else {
- sql_list = (char**)malloc(sizeof(char*));
+ sql_list = g_array_new(FALSE, FALSE, sizeof(char*));
MSAPI_RETVM_IF(sql_list == NULL, MS_MEDIA_ERR_OUT_OF_MEMORY, "Out of memory");
- sql_list[g_list_idx++] = strdup(query_str);
- MSAPI_RETVM_IF(sql_list[g_list_idx - 1] == NULL, MS_MEDIA_ERR_OUT_OF_MEMORY, "Out of memory");
+
+ query = g_strdup(query_str);
+ if (MS_STRING_VALID(query)) {
+ g_array_append_val(sql_list, query);
+ } else {
+ MSAPI_DBG_ERR("query is null");
+ __media_db_destroy_sql_list();
+ ret = MS_MEDIA_ERR_OUT_OF_MEMORY;
+ }
}
return ret;
int media_db_update_db_batch(const char *query_str)
{
int ret = MS_MEDIA_ERR_NONE;
+ char *query = NULL;
MSAPI_RETVM_IF(!MS_STRING_VALID(query_str), MS_MEDIA_ERR_INVALID_PARAMETER, "Invalid Query");
- sql_list = (char**)realloc(sql_list, (g_list_idx + 1) * sizeof(char*));
- MSAPI_RETVM_IF(sql_list == NULL, MS_MEDIA_ERR_OUT_OF_MEMORY, "Out of memory");
-
- sql_list[g_list_idx++] = strdup(query_str);
- MSAPI_RETVM_IF(sql_list[g_list_idx - 1] == NULL, MS_MEDIA_ERR_OUT_OF_MEMORY, "Out of memory");
+ if (sql_list) {
+ query = g_strdup(query_str);
+ if (MS_STRING_VALID(query)) {
+ g_array_append_val(sql_list, query);
+ } else {
+ MSAPI_DBG_ERR("query is null");
+ ret = MS_MEDIA_ERR_OUT_OF_MEMORY;
+ }
+ }
return ret;
}
{
sqlite3 * db_handle = (sqlite3 *)handle;
int ret = MS_MEDIA_ERR_NONE;
+ char *query = NULL;
if (db_handle == NULL || (!MS_STRING_VALID(query_str))) {
__media_db_destroy_sql_list();
return MS_MEDIA_ERR_INVALID_PARAMETER;
}
- sql_list = (char**)realloc(sql_list, (g_list_idx + 1) * sizeof(char*));
- if (sql_list == NULL) {
- __media_db_destroy_sql_list();
- MSAPI_DBG_ERR("Out of memory");
- return MS_MEDIA_ERR_OUT_OF_MEMORY;
- }
-
- sql_list[g_list_idx++] = strdup(query_str);
- if (sql_list[g_list_idx - 1] == NULL) {
- __media_db_destroy_sql_list();
- MSAPI_DBG_ERR("Out of memory");
- return MS_MEDIA_ERR_OUT_OF_MEMORY;
- }
+ if (sql_list) {
+ query = g_strdup(query_str);
+ if (MS_STRING_VALID(query)) {
+ g_array_append_val(sql_list, query);
+ } else {
+ __media_db_destroy_sql_list();
+ MSAPI_DBG_ERR("query is null");
+ return MS_MEDIA_ERR_OUT_OF_MEMORY;
+ }
- int i = 0;
- char *current_sql = NULL;
- for (i = 0; i < g_list_idx; i++) {
- current_sql = sql_list[i];
- ret = _media_db_update_directly(db_handle, current_sql);
- if (ret < 0) {
- if (i == 0) {
- /* This is fail of "BEGIN" */
- MSAPI_DBG_ERR("Query failed : %s", current_sql);
- break;
- } else if (i == g_list_idx - 1) {
- /* This is fail of "COMMIT" */
- MSAPI_DBG_ERR("Query failed : %s", current_sql);
- break;
- } else {
- MSAPI_DBG_ERR("Query failed : %s, but keep going to run remaining queries", current_sql);
+ while (sql_list->len != 0) {
+ char *current_sql = NULL;
+
+ current_sql = g_array_index(sql_list, char *, 0);
+ g_array_remove_index(sql_list, 0);
+
+ if (MS_STRING_VALID(current_sql)) {
+ ret = _media_db_update_directly(db_handle, current_sql);
+ if (ret < 0) {
+ if (strncmp(current_sql, BATCH_START, strlen(BATCH_START)) == 0 || strncmp(current_sql, BATCH_END, strlen(BATCH_END)) == 0) {
+ MSAPI_DBG_ERR("Query failed : %s", current_sql);
+ MS_SAFE_FREE(current_sql);
+ break;
+ } else {
+ MSAPI_DBG_ERR("Query failed : %s, but keep going to run remaining queries", current_sql);
+ }
+ }
}
+
+ MS_SAFE_FREE(current_sql);
}
}