4 * Copyright (c) 2000 - 2011 Samsung Electronics Co., Ltd. All rights reserved.
6 * Contact: Hyunjun Ko <zzoon.ko@samsung.com>, Haejeong Kim <backto.kim@samsung.com>
8 * Licensed under the Apache License, Version 2.0 (the "License");
9 * you may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
12 * http://www.apache.org/licenses/LICENSE-2.0
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS,
16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
23 #include <media-util.h>
25 #include "media-svc-env.h"
26 #include "media-svc-debug.h"
27 #include "media-svc-util.h"
28 #include "media-svc-db-utils.h"
29 #include "media-util-err.h"
30 #include "media-util-db.h"
31 #include "media-svc-media.h"
33 static int __media_svc_db_upgrade(sqlite3 *db_handle, int cur_version, uid_t uid);
34 static int __media_svc_rebuild_view_query(sqlite3 *db_handle, uid_t uid);
35 static int __media_svc_query_direct(sqlite3 *handle, const char *query, uid_t uid);
38 static GHashTable *table;
39 static GSList *column_list[MEDIA_SVC_DB_LIST_MAX];
41 int __media_svc_add_table_info(const char *name, const char *trigger_name, const char *event_table, const char *action_table, const char *view_name)
43 table_info_s *tbl = NULL;
45 media_svc_retvm_if(!STRING_VALID(name), MS_MEDIA_ERR_INVALID_PARAMETER, "name is NULL");
47 if (STRING_VALID(trigger_name)) {
48 media_svc_retvm_if(!STRING_VALID(event_table), MS_MEDIA_ERR_INVALID_PARAMETER, "event_table is NULL");
49 media_svc_retvm_if(!STRING_VALID(action_table), MS_MEDIA_ERR_INVALID_PARAMETER, "action_table is NULL");
52 tbl = malloc(sizeof(table_info_s));
54 media_svc_error("MS_MEDIA_ERR_OUT_OF_MEMORY");
55 return MS_MEDIA_ERR_OUT_OF_MEMORY;
58 memset(tbl, 0x00, sizeof(table_info_s));
60 if (STRING_VALID(trigger_name)) {
61 tbl->trigger_name = malloc(MEDIA_SVC_PATHNAME_SIZE);
62 if (tbl->trigger_name == NULL) {
63 media_svc_error("MS_MEDIA_ERR_OUT_OF_MEMORY");
65 return MS_MEDIA_ERR_OUT_OF_MEMORY;
68 memset(tbl->trigger_name, 0x00, MEDIA_SVC_PATHNAME_SIZE);
69 snprintf(tbl->trigger_name, MEDIA_SVC_PATHNAME_SIZE, "%s_%s", trigger_name, event_table);
71 tbl->event_table = strndup(event_table, strlen(event_table));
72 tbl->action_table = strndup(action_table, strlen(action_table));
75 if (STRING_VALID(view_name))
76 tbl->view_name = strndup(view_name, strlen(view_name));
78 g_hash_table_insert(table, (gpointer)name, (gpointer)tbl);
80 return MS_MEDIA_ERR_NONE;
83 int __media_svc_add_column_info(GSList **slist, const char *name, const char *type, const char *option, int version, const char *index_name, bool is_unique, bool is_trigger, bool is_view)
85 column_info_s *col = NULL;
86 col = malloc(sizeof(column_info_s));
88 media_svc_error("MS_MEDIA_ERR_OUT_OF_MEMORY");
89 return MS_MEDIA_ERR_OUT_OF_MEMORY;
91 memset(col, 0, sizeof(column_info_s));
93 col->name = strndup(name, strlen(name));
94 col->type = strndup(type, strlen(type));
96 col->has_option = true;
97 col->option = strndup(option, strlen(option));
99 col->has_option = false;
101 col->version = version;
102 if (index_name != NULL) {
103 col->is_index = true;
104 col->index_name = strndup(index_name, strlen(index_name));
106 col->is_index = false;
108 col->is_unique = is_unique;
109 col->is_trigger = is_trigger;
110 col->is_view = is_view;
111 *slist = g_slist_append(*slist, col);
113 return MS_MEDIA_ERR_NONE;
116 static int __media_svc_rebuild_view_query(sqlite3 *db_handle, uid_t uid)
118 int ret = MS_MEDIA_ERR_NONE;
119 column_info_s *col_ptr = NULL;
121 char table_query[MEDIA_SVC_QUERY_LEN_NORMAL] = {0, };
122 char temp[1024] = {0, };
126 _media_svc_update_media_view(db_handle, uid);
128 /*drop playlist_view, tag_view */
129 sql = sqlite3_mprintf(MEDIA_SVC_DB_QUERY_DROP_VIEW, MEDIA_SVC_DB_VIEW_PLAYLIST);
130 ret = _media_svc_sql_query(sql, uid);
131 SQLITE3_SAFE_FREE(sql);
132 media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
134 sql = sqlite3_mprintf(MEDIA_SVC_DB_QUERY_DROP_VIEW, MEDIA_SVC_DB_VIEW_TAG);
135 ret = _media_svc_sql_query(sql, uid);
136 SQLITE3_SAFE_FREE(sql);
137 media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
139 /*create playlist_view */
140 len = g_slist_length(column_list[MEDIA_SVC_DB_LIST_PLAYLIST]);
141 for (i = 1; i < len; i++) {
142 col_ptr = g_slist_nth_data(column_list[MEDIA_SVC_DB_LIST_PLAYLIST], i);
143 if (col_ptr->is_view) {
145 if (strncmp(col_ptr->name, MEDIA_SVC_DB_COLUMN_THUMBNAIL, strlen(MEDIA_SVC_DB_COLUMN_THUMBNAIL)) == 0)
146 snprintf(temp, sizeof(temp), ", playlist.%s AS p_thumbnail_path", col_ptr->name);
148 snprintf(temp, sizeof(temp), ", playlist.%s", col_ptr->name);
150 snprintf(temp, sizeof(temp), "playlist.%s", col_ptr->name);
153 SAFE_STRLCAT(table_query, temp, sizeof(table_query));
155 memset(temp, 0, sizeof(temp));
157 len = g_slist_length(column_list[MEDIA_SVC_DB_LIST_PLAYLIST_MAP]);
158 for (i = 1; i < len; i++) {
159 col_ptr = g_slist_nth_data(column_list[MEDIA_SVC_DB_LIST_PLAYLIST_MAP], i);
160 if (col_ptr->is_view) {
161 if (strncmp(col_ptr->name, MEDIA_SVC_DB_COLUMN_MAP_ID, strlen(MEDIA_SVC_DB_COLUMN_MAP_ID)) == 0)
162 snprintf(temp, sizeof(temp), ", playlist_media_count IS NOT NULL AS playlist_media_count, playlist_map.%s AS pm_id", col_ptr->name);
164 snprintf(temp, sizeof(temp), ", playlist_map.%s", col_ptr->name);
165 SAFE_STRLCAT(table_query, temp, sizeof(table_query));
167 memset(temp, 0, sizeof(temp));
170 len = g_slist_length(column_list[MEDIA_SVC_DB_LIST_MEDIA]);
171 for (i = 1; i < len; i++) {
172 col_ptr = g_slist_nth_data(column_list[MEDIA_SVC_DB_LIST_MEDIA], i);
173 if (col_ptr->is_view) {
174 snprintf(temp, sizeof(temp), ", media_view.%s", col_ptr->name);
175 SAFE_STRLCAT(table_query, temp, sizeof(table_query));
177 memset(temp, 0, sizeof(temp));
179 sql = sqlite3_mprintf(MEDIA_SVC_DB_QUERY_VIEW_PLAYLIST, MEDIA_SVC_DB_VIEW_PLAYLIST, table_query);
180 ret = _media_svc_sql_query(sql, uid);
181 SQLITE3_SAFE_FREE(sql);
182 media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
186 memset(table_query, 0, sizeof(table_query));
188 len = g_slist_length(column_list[MEDIA_SVC_DB_LIST_TAG]);
189 for (i = 1; i < len; i++) {
190 col_ptr = g_slist_nth_data(column_list[MEDIA_SVC_DB_LIST_TAG], i);
191 if (col_ptr->is_view) {
193 snprintf(temp, sizeof(temp), ", tag.%s", col_ptr->name);
195 snprintf(temp, sizeof(temp), "tag.%s", col_ptr->name);
198 SAFE_STRLCAT(table_query, temp, sizeof(table_query));
200 memset(temp, 0, sizeof(temp));
202 len = g_slist_length(column_list[MEDIA_SVC_DB_LIST_TAG_MAP]);
203 for (i = 1; i < len; i++) {
204 col_ptr = g_slist_nth_data(column_list[MEDIA_SVC_DB_LIST_TAG_MAP], i);
205 if (col_ptr->is_view) {
206 if (strncmp(col_ptr->name, MEDIA_SVC_DB_COLUMN_MAP_ID, strlen(MEDIA_SVC_DB_COLUMN_MAP_ID)) == 0)
207 snprintf(temp, sizeof(temp), ", tag_media_count IS NOT NULL AS tag_media_count, tag_map.%s AS tm_id", col_ptr->name);
209 snprintf(temp, sizeof(temp), ", tag_map.%s", col_ptr->name);
210 SAFE_STRLCAT(table_query, temp, sizeof(table_query));
212 memset(temp, 0, sizeof(temp));
215 len = g_slist_length(column_list[MEDIA_SVC_DB_LIST_MEDIA]);
216 for (i = 1; i < len; i++) {
217 col_ptr = g_slist_nth_data(column_list[MEDIA_SVC_DB_LIST_MEDIA], i);
218 if (col_ptr->is_view) {
219 snprintf(temp, sizeof(temp), ", media_view.%s", col_ptr->name);
220 SAFE_STRLCAT(table_query, temp, sizeof(table_query));
222 memset(temp, 0, sizeof(temp));
224 sql = sqlite3_mprintf(MEDIA_SVC_DB_QUERY_VIEW_TAG, MEDIA_SVC_DB_VIEW_TAG, table_query);
225 ret = _media_svc_sql_query(sql, uid);
226 SQLITE3_SAFE_FREE(sql);
227 media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
229 return MS_MEDIA_ERR_NONE;
232 int _media_svc_make_table_query(const char *table_name, media_svc_table_slist_e list, uid_t uid)
234 int ret = MS_MEDIA_ERR_NONE;
235 table_info_s *tb = NULL;
236 column_info_s *col_ptr = NULL;
238 char table_query[MEDIA_SVC_QUERY_LEN_NORMAL] = {0, };
239 char index_query[MEDIA_SVC_QUERY_LEN_NORMAL] = {0, };
240 char trigger_query[MEDIA_SVC_QUERY_LEN_NORMAL] = {0, };
241 char table_query_sub[1024] = {0, };
242 char temp[1024] = {0 ,};
244 bool sub_flag = false;
248 tb = g_hash_table_lookup(table, table_name);
250 media_svc_debug("lookup fail.. table name [%s] ", table_name);
251 tb = g_hash_table_lookup(table, MEDIA_SVC_DB_TABLE_MEDIA);
254 len = g_slist_length(column_list[list]);
257 media_svc_error("Invalid column");
258 return MS_MEDIA_ERR_INTERNAL;
261 for (i = 1; i < len; i++) {
262 col_ptr = g_slist_nth_data(column_list[list], i);
264 if (col_ptr->has_option) {
266 snprintf(temp, sizeof(temp), ", %s %s %s", col_ptr->name, col_ptr->type, col_ptr->option);
268 snprintf(temp, sizeof(temp), "%s %s %s", col_ptr->name, col_ptr->type, col_ptr->option);
271 SAFE_STRLCAT(table_query, temp, sizeof(table_query));
274 snprintf(temp, sizeof(temp), ", %s %s", col_ptr->name, col_ptr->type);
276 snprintf(temp, sizeof(temp), "%s %s", col_ptr->name, col_ptr->type);
279 SAFE_STRLCAT(table_query, temp, sizeof(table_query));
281 memset(temp, 0, sizeof(temp));
284 if (col_ptr->is_unique) {
285 if (sub_flag == true) {
286 snprintf(temp, sizeof(temp), ", %s", col_ptr->name);
288 snprintf(temp, sizeof(temp), "%s", col_ptr->name);
291 SAFE_STRLCAT(table_query_sub, temp, sizeof(table_query_sub));
293 memset(temp, 0, sizeof(temp));
296 if (col_ptr->is_index) {
297 snprintf(temp, sizeof(temp), MEDIA_SVC_DB_QUERY_INDEX, col_ptr->index_name, table_name, col_ptr->name);
298 SAFE_STRLCAT(index_query, temp, sizeof(index_query));
300 memset(temp, 0, sizeof(temp));
303 if (col_ptr->is_trigger) {
304 if (STRING_VALID(tb->trigger_name)) {
305 if (strncmp(table_name, MEDIA_SVC_DB_TABLE_ALBUM, strlen(MEDIA_SVC_DB_TABLE_ALBUM)) == 0)
306 snprintf(temp, sizeof(temp), MEDIA_SVC_DB_QUERY_TRIGGER_WITH_COUNT, tb->trigger_name, tb->event_table, tb->action_table, tb->event_table, col_ptr->name, col_ptr->name, col_ptr->name, col_ptr->name);
308 snprintf(temp, sizeof(temp), MEDIA_SVC_DB_QUERY_TRIGGER, tb->trigger_name, tb->event_table, tb->action_table, col_ptr->name, col_ptr->name);
310 SAFE_STRLCAT(trigger_query, temp, sizeof(trigger_query));
312 media_svc_error("invalid trigger name");
315 memset(temp, 0, sizeof(temp));
319 if (sub_flag == true) {
320 sql = sqlite3_mprintf(MEDIA_SVC_DB_QUERY_TABLE_WITH_UNIQUE, table_name, table_query, table_query_sub);
321 ret = _media_svc_sql_query(sql, uid);
322 SQLITE3_SAFE_FREE(sql);
323 memset(table_query, 0, sizeof(table_query));
324 memset(table_query_sub, 0, sizeof(table_query_sub));
325 media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
327 sql = sqlite3_mprintf(MEDIA_SVC_DB_QUERY_TABLE, table_name, table_query);
328 ret = _media_svc_sql_query(sql, uid);
329 SQLITE3_SAFE_FREE(sql);
330 memset(table_query, 0, sizeof(table_query));
331 media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
334 if (STRING_VALID(index_query)) {
335 ret = _media_svc_sql_query(index_query, uid);
336 memset(index_query, 0, sizeof(index_query));
337 media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
340 if (STRING_VALID(trigger_query)) {
341 ret = _media_svc_sql_query(trigger_query, uid);
342 memset(trigger_query, 0, sizeof(trigger_query));
343 media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
348 if (tb != NULL && tb->view_name != NULL) {
349 if (strncmp(table_name, MEDIA_SVC_DB_TABLE_MEDIA, strlen(MEDIA_SVC_DB_TABLE_MEDIA)) == 0) {
350 sql = sqlite3_mprintf(MEDIA_SVC_DB_QUERY_VIEW_MEDIA, tb->view_name, table_name);
351 ret = _media_svc_sql_query(sql, uid);
352 SQLITE3_SAFE_FREE(sql);
353 media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
355 } else if (strncmp(table_name, MEDIA_SVC_DB_TABLE_PLAYLIST, strlen(MEDIA_SVC_DB_TABLE_PLAYLIST)) == 0) {
356 len = g_slist_length(column_list[MEDIA_SVC_DB_LIST_PLAYLIST]);
357 for (i = 1; i < len; i++) {
358 col_ptr = g_slist_nth_data(column_list[MEDIA_SVC_DB_LIST_PLAYLIST], i);
359 if (col_ptr->is_view) {
361 if (strncmp(col_ptr->name, MEDIA_SVC_DB_COLUMN_THUMBNAIL, strlen(MEDIA_SVC_DB_COLUMN_THUMBNAIL)) == 0)
362 snprintf(temp, sizeof(temp), ", playlist.%s AS p_thumbnail_path", col_ptr->name);
364 snprintf(temp, sizeof(temp), ", playlist.%s", col_ptr->name);
366 snprintf(temp, sizeof(temp), "playlist.%s", col_ptr->name);
369 SAFE_STRLCAT(table_query, temp, sizeof(table_query));
371 memset(temp, 0, sizeof(temp));
373 len = g_slist_length(column_list[MEDIA_SVC_DB_LIST_PLAYLIST_MAP]);
374 for (i = 1; i < len; i++) {
375 col_ptr = g_slist_nth_data(column_list[MEDIA_SVC_DB_LIST_PLAYLIST_MAP], i);
376 if (col_ptr->is_view) {
377 if (strncmp(col_ptr->name, MEDIA_SVC_DB_COLUMN_MAP_ID, strlen(MEDIA_SVC_DB_COLUMN_MAP_ID)) == 0)
378 snprintf(temp, sizeof(temp), ", playlist_media_count IS NOT NULL AS playlist_media_count, playlist_map.%s AS pm_id", col_ptr->name);
380 snprintf(temp, sizeof(temp), ", playlist_map.%s", col_ptr->name);
381 SAFE_STRLCAT(table_query, temp, sizeof(table_query));
383 memset(temp, 0, sizeof(temp));
386 len = g_slist_length(column_list[MEDIA_SVC_DB_LIST_MEDIA]);
387 for (i = 1; i < len; i++) {
388 col_ptr = g_slist_nth_data(column_list[MEDIA_SVC_DB_LIST_MEDIA], i);
389 if (col_ptr->is_view) {
390 snprintf(temp, sizeof(temp), ", media_view.%s", col_ptr->name);
391 SAFE_STRLCAT(table_query, temp, sizeof(table_query));
393 memset(temp, 0, sizeof(temp));
395 sql = sqlite3_mprintf(MEDIA_SVC_DB_QUERY_VIEW_PLAYLIST, tb->view_name, table_query);
396 ret = _media_svc_sql_query(sql, uid);
397 SQLITE3_SAFE_FREE(sql);
398 media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
401 len = g_slist_length(column_list[MEDIA_SVC_DB_LIST_TAG]);
402 for (i = 1; i < len; i++) {
403 col_ptr = g_slist_nth_data(column_list[MEDIA_SVC_DB_LIST_TAG], i);
404 if (col_ptr->is_view) {
406 snprintf(temp, sizeof(temp), ", tag.%s", col_ptr->name);
408 snprintf(temp, sizeof(temp), "tag.%s", col_ptr->name);
411 SAFE_STRLCAT(table_query, temp, sizeof(table_query));
413 memset(temp, 0, sizeof(temp));
415 len = g_slist_length(column_list[MEDIA_SVC_DB_LIST_TAG_MAP]);
416 for (i = 1; i < len; i++) {
417 col_ptr = g_slist_nth_data(column_list[MEDIA_SVC_DB_LIST_TAG_MAP], i);
418 if (col_ptr->is_view) {
419 if (strncmp(col_ptr->name, MEDIA_SVC_DB_COLUMN_MAP_ID, strlen(MEDIA_SVC_DB_COLUMN_MAP_ID)) == 0)
420 snprintf(temp, sizeof(temp), ", tag_media_count IS NOT NULL AS tag_media_count, tag_map.%s AS tm_id", col_ptr->name);
422 snprintf(temp, sizeof(temp), ", tag_map.%s", col_ptr->name);
423 SAFE_STRLCAT(table_query, temp, sizeof(table_query));
425 memset(temp, 0, sizeof(temp));
428 len = g_slist_length(column_list[MEDIA_SVC_DB_LIST_MEDIA]);
429 for (i = 1; i < len; i++) {
430 col_ptr = g_slist_nth_data(column_list[MEDIA_SVC_DB_LIST_MEDIA], i);
431 if (col_ptr->is_view) {
432 snprintf(temp, sizeof(temp), ", media_view.%s", col_ptr->name);
433 SAFE_STRLCAT(table_query, temp, sizeof(table_query));
435 memset(temp, 0, sizeof(temp));
437 sql = sqlite3_mprintf(MEDIA_SVC_DB_QUERY_VIEW_TAG, tb->view_name, table_query);
438 ret = _media_svc_sql_query(sql, uid);
439 SQLITE3_SAFE_FREE(sql);
440 media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
444 return MS_MEDIA_ERR_NONE;
447 static int __media_svc_upgrade_table_query(sqlite3 *db_handle, const char *table_name, media_svc_table_slist_e list, uid_t uid)
449 int ret = MS_MEDIA_ERR_NONE;
450 column_info_s *col_ptr = NULL;
452 char temp[1024] = {0, };
455 sqlite3_stmt *sql_stmt = NULL;
457 len = g_slist_length(column_list[list]);
459 sql = sqlite3_mprintf("PRAGMA user_version");
460 ret = _media_svc_sql_prepare_to_step(db_handle, sql, &sql_stmt);
462 if (ret != MS_MEDIA_ERR_NONE) {
463 media_svc_error("error when get user_version. err = [%d]", ret);
466 cur_version = sqlite3_column_int(sql_stmt, 0);
467 SQLITE3_FINALIZE(sql_stmt);
469 len = g_slist_length(column_list[list]);
470 for (i = 1; i < len; i++) {
471 col_ptr = g_slist_nth_data(column_list[list], i);
472 if (col_ptr->version > cur_version) {
474 if (col_ptr->has_option)
475 snprintf(temp, sizeof(temp), "%s %s %s", col_ptr->name, col_ptr->type, col_ptr->option);
477 snprintf(temp, sizeof(temp), "%s %s", col_ptr->name, col_ptr->type);
478 sql = sqlite3_mprintf(MEDIA_SVC_DB_QUERY_ALTER_TABLE, table_name, temp);
479 ret = _media_svc_sql_query(sql, uid);
480 SQLITE3_SAFE_FREE(sql);
481 media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
483 if (col_ptr->is_index) {
484 memset(temp, 0, sizeof(temp));
485 snprintf(temp, sizeof(temp), MEDIA_SVC_DB_QUERY_INDEX, col_ptr->index_name, table_name, col_ptr->name);
486 ret = _media_svc_sql_query(temp, uid);
487 media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
490 memset(temp, 0, sizeof(temp));
493 return MS_MEDIA_ERR_NONE;
496 int _media_svc_init_table_query(const char *event_table_name)
498 int ret = MS_MEDIA_ERR_NONE;
501 /*variable initialize.. */
502 table = g_hash_table_new(g_str_hash, g_str_equal);
503 for (i = 0; i < MEDIA_SVC_DB_LIST_MAX; i++)
504 column_list[i] = g_slist_alloc();
506 /*table specification.. (table_name, index, unique set, trigger, view, trigger name, event table, action table, view name) */
507 ret = __media_svc_add_table_info(MEDIA_SVC_DB_TABLE_MEDIA, NULL, NULL, NULL, MEDIA_SVC_DB_VIEW_MEDIA);
508 media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
509 ret = __media_svc_add_table_info(MEDIA_SVC_DB_TABLE_FOLDER, MEDIA_SVC_DB_TRIGGER_FOLDER, event_table_name, MEDIA_SVC_DB_TABLE_FOLDER, NULL);
510 media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
511 ret = __media_svc_add_table_info(MEDIA_SVC_DB_TABLE_PLAYLIST_MAP, MEDIA_SVC_DB_TRIGGER_PLAYLIST_MAP, event_table_name, MEDIA_SVC_DB_TABLE_PLAYLIST_MAP, NULL);
512 media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
513 ret = __media_svc_add_table_info(MEDIA_SVC_DB_TABLE_PLAYLIST, MEDIA_SVC_DB_TRIGGER_PLAYLIST_MAP1, MEDIA_SVC_DB_TABLE_PLAYLIST, MEDIA_SVC_DB_TABLE_PLAYLIST_MAP, MEDIA_SVC_DB_VIEW_PLAYLIST);
514 media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
515 ret = __media_svc_add_table_info(MEDIA_SVC_DB_TABLE_ALBUM, MEDIA_SVC_DB_TRIGGER_ALBUM, event_table_name, MEDIA_SVC_DB_TABLE_ALBUM, NULL);
516 media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
517 ret = __media_svc_add_table_info(MEDIA_SVC_DB_TABLE_TAG_MAP, MEDIA_SVC_DB_TRIGGER_TAG_MAP, event_table_name, MEDIA_SVC_DB_TABLE_TAG_MAP, NULL);
518 media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
519 ret = __media_svc_add_table_info(MEDIA_SVC_DB_TABLE_TAG, MEDIA_SVC_DB_TRIGGER_TAG_MAP1, MEDIA_SVC_DB_TABLE_TAG, MEDIA_SVC_DB_TABLE_TAG_MAP, MEDIA_SVC_DB_VIEW_TAG);
520 media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
521 ret = __media_svc_add_table_info(MEDIA_SVC_DB_TABLE_BOOKMARK, MEDIA_SVC_DB_TRIGGER_BOOKMARK, event_table_name, MEDIA_SVC_DB_TABLE_BOOKMARK, NULL);
522 media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
523 ret = __media_svc_add_table_info(MEDIA_SVC_DB_TABLE_STORAGE, NULL, NULL, NULL, NULL);
524 media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
525 ret = __media_svc_add_table_info(MEDIA_SVC_DB_TABLE_FACE_SCAN_LIST, MEDIA_SVC_DB_TRIGGER_FACE_SCAN_LIST, MEDIA_SVC_DB_TABLE_MEDIA, MEDIA_SVC_DB_TABLE_FACE_SCAN_LIST, NULL);
526 media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
527 ret = __media_svc_add_table_info(MEDIA_SVC_DB_TABLE_FACE, MEDIA_SVC_DB_TRIGGER_FACE, MEDIA_SVC_DB_TABLE_FACE_SCAN_LIST, MEDIA_SVC_DB_TABLE_FACE, NULL);
528 media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
530 /*insert column info.. */
532 ret = __media_svc_add_column_info(&column_list[MEDIA_SVC_DB_LIST_MEDIA], "media_id", MEDIA_SVC_DB_TYPE_TEXT, "PRIMARY KEY", USER_V2, NULL, false, false, true);
533 media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
534 ret = __media_svc_add_column_info(&column_list[MEDIA_SVC_DB_LIST_MEDIA], "media_path", MEDIA_SVC_DB_TYPE_TEXT, "NOT NULL UNIQUE", USER_V2, NULL, false, false, true);
535 media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
536 ret = __media_svc_add_column_info(&column_list[MEDIA_SVC_DB_LIST_MEDIA], "media_display_name", MEDIA_SVC_DB_TYPE_TEXT, "NOT NULL", USER_V2, "media_file_name_idx", false, false, true);
537 media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
538 ret = __media_svc_add_column_info(&column_list[MEDIA_SVC_DB_LIST_MEDIA], "media_type", MEDIA_SVC_DB_TYPE_INT, NULL, USER_V2, NULL, false, false, true);
539 media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
540 ret = __media_svc_add_column_info(&column_list[MEDIA_SVC_DB_LIST_MEDIA], "media_mime_type", MEDIA_SVC_DB_TYPE_TEXT, NULL, USER_V2, NULL, false, false, true);
541 media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
542 ret = __media_svc_add_column_info(&column_list[MEDIA_SVC_DB_LIST_MEDIA], "media_size", MEDIA_SVC_DB_TYPE_INT, "DEFAULT 0", USER_V2, NULL, false, false, true);
543 media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
544 ret = __media_svc_add_column_info(&column_list[MEDIA_SVC_DB_LIST_MEDIA], "media_added_time", MEDIA_SVC_DB_TYPE_INT, "DEFAULT 0", USER_V2, NULL, false, false, true);
545 media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
546 ret = __media_svc_add_column_info(&column_list[MEDIA_SVC_DB_LIST_MEDIA], "media_modified_time", MEDIA_SVC_DB_TYPE_INT, "DEFAULT 0", USER_V2, "media_modified_time_idx", false, false, true);
547 media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
548 ret = __media_svc_add_column_info(&column_list[MEDIA_SVC_DB_LIST_MEDIA], "folder_id", MEDIA_SVC_DB_TYPE_TEXT, "NOT NULL", USER_V2, "folder_uuid_idx", false, false, false);
549 media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
550 ret = __media_svc_add_column_info(&column_list[MEDIA_SVC_DB_LIST_MEDIA], "media_thumbnail_path", MEDIA_SVC_DB_TYPE_TEXT, NULL, USER_V2, NULL, false, false, true);
551 media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
552 ret = __media_svc_add_column_info(&column_list[MEDIA_SVC_DB_LIST_MEDIA], "media_title", MEDIA_SVC_DB_TYPE_TEXT, NULL, USER_V2, NULL, false, false, true);
553 media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
554 ret = __media_svc_add_column_info(&column_list[MEDIA_SVC_DB_LIST_MEDIA], "album_id", MEDIA_SVC_DB_TYPE_INT, "DEFAULT 0", USER_V2, NULL, false, false, false);
555 media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
556 ret = __media_svc_add_column_info(&column_list[MEDIA_SVC_DB_LIST_MEDIA], "media_album", MEDIA_SVC_DB_TYPE_TEXT, NULL, USER_V2, NULL, false, false, true);
557 media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
558 ret = __media_svc_add_column_info(&column_list[MEDIA_SVC_DB_LIST_MEDIA], "media_artist", MEDIA_SVC_DB_TYPE_TEXT, NULL, USER_V2, NULL, false, false, true);
559 media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
560 ret = __media_svc_add_column_info(&column_list[MEDIA_SVC_DB_LIST_MEDIA], "media_album_artist", MEDIA_SVC_DB_TYPE_TEXT, NULL, USER_V2, NULL, false, false, true);
561 media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
562 ret = __media_svc_add_column_info(&column_list[MEDIA_SVC_DB_LIST_MEDIA], "media_genre", MEDIA_SVC_DB_TYPE_TEXT, NULL, USER_V2, NULL, false, false, true);
563 media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
564 ret = __media_svc_add_column_info(&column_list[MEDIA_SVC_DB_LIST_MEDIA], "media_composer", MEDIA_SVC_DB_TYPE_TEXT, NULL, USER_V2, NULL, false, false, true);
565 media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
566 ret = __media_svc_add_column_info(&column_list[MEDIA_SVC_DB_LIST_MEDIA], "media_year", MEDIA_SVC_DB_TYPE_TEXT, NULL, USER_V2, NULL, false, false, true);
567 media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
568 ret = __media_svc_add_column_info(&column_list[MEDIA_SVC_DB_LIST_MEDIA], "media_recorded_date", MEDIA_SVC_DB_TYPE_TEXT, NULL, USER_V2, NULL, false, false, true);
569 media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
570 ret = __media_svc_add_column_info(&column_list[MEDIA_SVC_DB_LIST_MEDIA], "media_copyright", MEDIA_SVC_DB_TYPE_TEXT, NULL, USER_V2, NULL, false, false, true);
571 media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
572 ret = __media_svc_add_column_info(&column_list[MEDIA_SVC_DB_LIST_MEDIA], "media_track_num", MEDIA_SVC_DB_TYPE_TEXT, NULL, USER_V2, NULL, false, false, true);
573 media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
574 ret = __media_svc_add_column_info(&column_list[MEDIA_SVC_DB_LIST_MEDIA], "media_description", MEDIA_SVC_DB_TYPE_TEXT, NULL, USER_V2, NULL, false, false, true);
575 media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
576 ret = __media_svc_add_column_info(&column_list[MEDIA_SVC_DB_LIST_MEDIA], "media_bitrate", MEDIA_SVC_DB_TYPE_INT, "DEFAULT -1", USER_V2, NULL, false, false, true);
577 media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
578 ret = __media_svc_add_column_info(&column_list[MEDIA_SVC_DB_LIST_MEDIA], "media_bitpersample", MEDIA_SVC_DB_TYPE_INT, "DEFAULT 0", USER_V3, NULL, false, false, true);
579 media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
580 ret = __media_svc_add_column_info(&column_list[MEDIA_SVC_DB_LIST_MEDIA], "media_samplerate", MEDIA_SVC_DB_TYPE_INT, "DEFAULT -1", USER_V2, NULL, false, false, true);
581 media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
582 ret = __media_svc_add_column_info(&column_list[MEDIA_SVC_DB_LIST_MEDIA], "media_channel", MEDIA_SVC_DB_TYPE_INT, "DEFAULT -1", USER_V2, NULL, false, false, true);
583 media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
584 ret = __media_svc_add_column_info(&column_list[MEDIA_SVC_DB_LIST_MEDIA], "media_duration", MEDIA_SVC_DB_TYPE_INT, "DEFAULT -1", USER_V2, NULL, false, false, true);
585 media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
586 ret = __media_svc_add_column_info(&column_list[MEDIA_SVC_DB_LIST_MEDIA], "media_longitude", MEDIA_SVC_DB_TYPE_DOUBLE, "DEFAULT 0", USER_V2, NULL, false, false, true);
587 media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
588 ret = __media_svc_add_column_info(&column_list[MEDIA_SVC_DB_LIST_MEDIA], "media_latitude", MEDIA_SVC_DB_TYPE_DOUBLE, "DEFAULT 0", USER_V2, NULL, false, false, true);
589 media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
590 ret = __media_svc_add_column_info(&column_list[MEDIA_SVC_DB_LIST_MEDIA], "media_altitude", MEDIA_SVC_DB_TYPE_DOUBLE, "DEFAULT 0", USER_V2, NULL, false, false, true);
591 media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
592 ret = __media_svc_add_column_info(&column_list[MEDIA_SVC_DB_LIST_MEDIA], "exposure_time", MEDIA_SVC_DB_TYPE_TEXT, NULL, USER_V4, NULL, false, false, true);
593 media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
594 ret = __media_svc_add_column_info(&column_list[MEDIA_SVC_DB_LIST_MEDIA], "fnumber", MEDIA_SVC_DB_TYPE_DOUBLE, "DEFAULT 0", USER_V4, NULL, false, false, true);
595 media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
596 ret = __media_svc_add_column_info(&column_list[MEDIA_SVC_DB_LIST_MEDIA], "iso", MEDIA_SVC_DB_TYPE_INT, "DEFAULT -1", USER_V4, NULL, false, false, true);
597 media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
598 ret = __media_svc_add_column_info(&column_list[MEDIA_SVC_DB_LIST_MEDIA], "model", MEDIA_SVC_DB_TYPE_TEXT, NULL, USER_V4, NULL, false, false, true);
599 media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
600 ret = __media_svc_add_column_info(&column_list[MEDIA_SVC_DB_LIST_MEDIA], "media_width", MEDIA_SVC_DB_TYPE_INT, "DEFAULT -1", USER_V2, NULL, false, false, true);
601 media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
602 ret = __media_svc_add_column_info(&column_list[MEDIA_SVC_DB_LIST_MEDIA], "media_height", MEDIA_SVC_DB_TYPE_INT, "DEFAULT -1", USER_V2, NULL, false, false, true);
603 media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
604 ret = __media_svc_add_column_info(&column_list[MEDIA_SVC_DB_LIST_MEDIA], "media_datetaken", MEDIA_SVC_DB_TYPE_TEXT, NULL, USER_V2, NULL, false, false, true);
605 media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
606 ret = __media_svc_add_column_info(&column_list[MEDIA_SVC_DB_LIST_MEDIA], "media_orientation", MEDIA_SVC_DB_TYPE_INT, "DEFAULT -1", USER_V2, NULL, false, false, true);
607 media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
608 ret = __media_svc_add_column_info(&column_list[MEDIA_SVC_DB_LIST_MEDIA], "media_rating", MEDIA_SVC_DB_TYPE_INT, "DEFAULT 0", USER_V2, NULL, false, false, true);
609 media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
610 ret = __media_svc_add_column_info(&column_list[MEDIA_SVC_DB_LIST_MEDIA], "media_favourite", MEDIA_SVC_DB_TYPE_INT, "DEFAULT 0", USER_V2, NULL, false, false, true);
611 media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
612 ret = __media_svc_add_column_info(&column_list[MEDIA_SVC_DB_LIST_MEDIA], "media_is_drm", MEDIA_SVC_DB_TYPE_INT, "DEFAULT 0", USER_V2, NULL, false, false, true);
613 media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
614 ret = __media_svc_add_column_info(&column_list[MEDIA_SVC_DB_LIST_MEDIA], "media_storage_type", MEDIA_SVC_DB_TYPE_INT, NULL, USER_V2, NULL, false, false, true);
615 media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
616 ret = __media_svc_add_column_info(&column_list[MEDIA_SVC_DB_LIST_MEDIA], "media_timeline", MEDIA_SVC_DB_TYPE_INT, "DEFAULT 0", USER_V2, "media_timeline_idx", false, false, true);
617 media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
618 ret = __media_svc_add_column_info(&column_list[MEDIA_SVC_DB_LIST_MEDIA], "sync_status", MEDIA_SVC_DB_TYPE_INT, "DEFAULT 0", USER_V2, NULL, false, false, true);
619 media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
620 ret = __media_svc_add_column_info(&column_list[MEDIA_SVC_DB_LIST_MEDIA], "media_file_name_pinyin", MEDIA_SVC_DB_TYPE_TEXT, NULL, USER_V2, NULL, false, false, false);
621 media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
622 ret = __media_svc_add_column_info(&column_list[MEDIA_SVC_DB_LIST_MEDIA], "media_title_pinyin", MEDIA_SVC_DB_TYPE_TEXT, NULL, USER_V2, NULL, false, false, false);
623 media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
624 ret = __media_svc_add_column_info(&column_list[MEDIA_SVC_DB_LIST_MEDIA], "media_album_pinyin", MEDIA_SVC_DB_TYPE_TEXT, NULL, USER_V2, NULL, false, false, false);
625 media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
626 ret = __media_svc_add_column_info(&column_list[MEDIA_SVC_DB_LIST_MEDIA], "media_artist_pinyin", MEDIA_SVC_DB_TYPE_TEXT, NULL, USER_V2, NULL, false, false, false);
627 media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
628 ret = __media_svc_add_column_info(&column_list[MEDIA_SVC_DB_LIST_MEDIA], "media_album_artist_pinyin", MEDIA_SVC_DB_TYPE_TEXT, NULL, USER_V2, NULL, false, false, false);
629 media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
630 ret = __media_svc_add_column_info(&column_list[MEDIA_SVC_DB_LIST_MEDIA], "media_genre_pinyin", MEDIA_SVC_DB_TYPE_TEXT, NULL, USER_V2, NULL, false, false, false);
631 media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
632 ret = __media_svc_add_column_info(&column_list[MEDIA_SVC_DB_LIST_MEDIA], "media_composer_pinyin", MEDIA_SVC_DB_TYPE_TEXT, NULL, USER_V2, NULL, false, false, false);
633 media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
634 ret = __media_svc_add_column_info(&column_list[MEDIA_SVC_DB_LIST_MEDIA], "media_copyright_pinyin", MEDIA_SVC_DB_TYPE_TEXT, NULL, USER_V2, NULL, false, false, false);
635 media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
636 ret = __media_svc_add_column_info(&column_list[MEDIA_SVC_DB_LIST_MEDIA], "media_description_pinyin", MEDIA_SVC_DB_TYPE_TEXT, NULL, USER_V2, NULL, false, false, false);
637 media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
638 ret = __media_svc_add_column_info(&column_list[MEDIA_SVC_DB_LIST_MEDIA], "age_rating_pinyin", MEDIA_SVC_DB_TYPE_TEXT, NULL, USER_V2, NULL, false, false, false);
639 media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
640 ret = __media_svc_add_column_info(&column_list[MEDIA_SVC_DB_LIST_MEDIA], "keyword_pinyin", MEDIA_SVC_DB_TYPE_TEXT, NULL, USER_V2, NULL, false, false, false);
641 media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
642 /* storage_uuid column is added in DB v4. When doing DB upgrade to v4, if storage_uuid is NOT NULL, alter table failed. */
643 ret = __media_svc_add_column_info(&column_list[MEDIA_SVC_DB_LIST_MEDIA], "storage_uuid", MEDIA_SVC_DB_TYPE_TEXT, NULL, USER_V4, NULL, false, false, true);
644 media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
645 ret = __media_svc_add_column_info(&column_list[MEDIA_SVC_DB_LIST_MEDIA], "validity", MEDIA_SVC_DB_TYPE_INT, "DEFAULT 1", USER_V2, NULL, false, false, false);
646 media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
647 /* color column is added with dcm. (DB v5) */
648 ret = __media_svc_add_column_info(&column_list[MEDIA_SVC_DB_LIST_MEDIA], "media_360", MEDIA_SVC_DB_TYPE_INT, "DEFAULT 0", USER_V5, NULL, false, false, true);
649 media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
652 ret = __media_svc_add_column_info(&column_list[MEDIA_SVC_DB_LIST_FOLDER], "folder_id", MEDIA_SVC_DB_TYPE_TEXT, "PRIMARY KEY", USER_V2, NULL, false, false, false);
653 media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
654 ret = __media_svc_add_column_info(&column_list[MEDIA_SVC_DB_LIST_FOLDER], "folder_path", MEDIA_SVC_DB_TYPE_TEXT, "NOT NULL", USER_V2, NULL, true, false, false);
655 media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
656 ret = __media_svc_add_column_info(&column_list[MEDIA_SVC_DB_LIST_FOLDER], "folder_name", MEDIA_SVC_DB_TYPE_TEXT, "NOT NULL", USER_V2, NULL, false, false, false);
657 media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
658 ret = __media_svc_add_column_info(&column_list[MEDIA_SVC_DB_LIST_FOLDER], "folder_modified_time", MEDIA_SVC_DB_TYPE_INT, "DEFAULT 0", USER_V2, NULL, false, false, false);
659 media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
660 ret = __media_svc_add_column_info(&column_list[MEDIA_SVC_DB_LIST_FOLDER], "folder_name_pinyin", MEDIA_SVC_DB_TYPE_TEXT, NULL, USER_V2, NULL, false, false, false);
661 media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
662 ret = __media_svc_add_column_info(&column_list[MEDIA_SVC_DB_LIST_FOLDER], "folder_storage_type", MEDIA_SVC_DB_TYPE_INT, NULL, USER_V2, NULL, false, false, false);
663 media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
664 /* storage_uuid column is added in DB v4. When doing DB upgrade to v4, if storage_uuid is NOT NULL, alter table failed. */
665 ret = __media_svc_add_column_info(&column_list[MEDIA_SVC_DB_LIST_FOLDER], "storage_uuid", MEDIA_SVC_DB_TYPE_TEXT, NULL, USER_V4, NULL, true, false, false);
666 media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
667 ret = __media_svc_add_column_info(&column_list[MEDIA_SVC_DB_LIST_FOLDER], "validity", MEDIA_SVC_DB_TYPE_INT, "DEFAULT 1", USER_V4, NULL, false, false, false);
668 media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
671 ret = __media_svc_add_column_info(&column_list[MEDIA_SVC_DB_LIST_PLAYLIST_MAP], "_id", MEDIA_SVC_DB_TYPE_INT, "PRIMARY KEY AUTOINCREMENT", USER_V2, NULL, false, false, true);
672 media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
673 ret = __media_svc_add_column_info(&column_list[MEDIA_SVC_DB_LIST_PLAYLIST_MAP], "playlist_id", MEDIA_SVC_DB_TYPE_INT, "NOT NULL", USER_V2, NULL, false, false, false);
674 media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
675 ret = __media_svc_add_column_info(&column_list[MEDIA_SVC_DB_LIST_PLAYLIST_MAP], "media_id", MEDIA_SVC_DB_TYPE_TEXT, "NOT NULL", USER_V2, NULL, false, true, false);
676 media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
677 ret = __media_svc_add_column_info(&column_list[MEDIA_SVC_DB_LIST_PLAYLIST_MAP], "playlist_member_order", MEDIA_SVC_DB_TYPE_INT, "NOT NULL", USER_V2, NULL, false, false, true);
678 media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
681 ret = __media_svc_add_column_info(&column_list[MEDIA_SVC_DB_LIST_PLAYLIST], "playlist_id", MEDIA_SVC_DB_TYPE_INT, "PRIMARY KEY AUTOINCREMENT", USER_V2, NULL, false, true, true);
682 media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
683 ret = __media_svc_add_column_info(&column_list[MEDIA_SVC_DB_LIST_PLAYLIST], "playlist_name", MEDIA_SVC_DB_TYPE_TEXT, "NOT NULL UNIQUE", USER_V2, NULL, false, false, true);
684 media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
685 ret = __media_svc_add_column_info(&column_list[MEDIA_SVC_DB_LIST_PLAYLIST], "thumbnail_path", MEDIA_SVC_DB_TYPE_TEXT, NULL, USER_V2, NULL, false, false, true);
686 media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
689 ret = __media_svc_add_column_info(&column_list[MEDIA_SVC_DB_LIST_ALBUM], "album_id", MEDIA_SVC_DB_TYPE_INT, "PRIMARY KEY AUTOINCREMENT", USER_V2, NULL, false, true, false);
690 media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
691 ret = __media_svc_add_column_info(&column_list[MEDIA_SVC_DB_LIST_ALBUM], "name", MEDIA_SVC_DB_TYPE_TEXT, "NOT NULL", USER_V2, NULL, false, false, false);
692 media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
693 ret = __media_svc_add_column_info(&column_list[MEDIA_SVC_DB_LIST_ALBUM], "artist", MEDIA_SVC_DB_TYPE_TEXT, NULL, USER_V2, NULL, false, false, false);
694 media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
695 ret = __media_svc_add_column_info(&column_list[MEDIA_SVC_DB_LIST_ALBUM], "album_art", MEDIA_SVC_DB_TYPE_TEXT, NULL, USER_V2, NULL, false, false, false);
696 media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
699 ret = __media_svc_add_column_info(&column_list[MEDIA_SVC_DB_LIST_TAG_MAP], "_id", MEDIA_SVC_DB_TYPE_INT, "PRIMARY KEY AUTOINCREMENT", USER_V2, NULL, false, false, true);
700 media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
701 ret = __media_svc_add_column_info(&column_list[MEDIA_SVC_DB_LIST_TAG_MAP], "tag_id", MEDIA_SVC_DB_TYPE_INT, "NOT NULL", USER_V2, NULL, true, false, false);
702 media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
703 ret = __media_svc_add_column_info(&column_list[MEDIA_SVC_DB_LIST_TAG_MAP], "media_id", MEDIA_SVC_DB_TYPE_TEXT, "NOT NULL", USER_V2, NULL, true, true, false);
704 media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
707 ret = __media_svc_add_column_info(&column_list[MEDIA_SVC_DB_LIST_TAG], "tag_id ", MEDIA_SVC_DB_TYPE_INT, "PRIMARY KEY AUTOINCREMENT", USER_V2, NULL, false, true, true);
708 media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
709 ret = __media_svc_add_column_info(&column_list[MEDIA_SVC_DB_LIST_TAG], "tag_name", MEDIA_SVC_DB_TYPE_TEXT, "NOT NULL UNIQUE", USER_V2, NULL, false, false, true);
710 media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
713 ret = __media_svc_add_column_info(&column_list[MEDIA_SVC_DB_LIST_BOOKMARK], "bookmark_id", MEDIA_SVC_DB_TYPE_INT, "PRIMARY KEY AUTOINCREMENT", USER_V2, NULL, false, false, false);
714 media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
715 ret = __media_svc_add_column_info(&column_list[MEDIA_SVC_DB_LIST_BOOKMARK], "media_id", MEDIA_SVC_DB_TYPE_TEXT, "NOT NULL", USER_V2, NULL, true, true, false);
716 media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
717 ret = __media_svc_add_column_info(&column_list[MEDIA_SVC_DB_LIST_BOOKMARK], "bookmark_marked_time", MEDIA_SVC_DB_TYPE_INT, "DEFAULT 0", USER_V2, NULL, true, false, false);
718 media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
719 ret = __media_svc_add_column_info(&column_list[MEDIA_SVC_DB_LIST_BOOKMARK], "bookmark_thumbnail_path", MEDIA_SVC_DB_TYPE_TEXT, NULL, USER_V2, NULL, false, false, false);
720 media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
721 ret = __media_svc_add_column_info(&column_list[MEDIA_SVC_DB_LIST_BOOKMARK], "bookmark_name", MEDIA_SVC_DB_TYPE_TEXT, NULL, USER_V6, NULL, false, false, false);
722 media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
725 ret = __media_svc_add_column_info(&column_list[MEDIA_SVC_DB_LIST_STORAGE], "storage_id", MEDIA_SVC_DB_TYPE_TEXT, "PRIMARY KEY", USER_V3, NULL, false, false, false);
726 media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
727 ret = __media_svc_add_column_info(&column_list[MEDIA_SVC_DB_LIST_STORAGE], "storage_path", MEDIA_SVC_DB_TYPE_TEXT, "NOT NULL", USER_V3, NULL, false, false, false);
728 media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
729 ret = __media_svc_add_column_info(&column_list[MEDIA_SVC_DB_LIST_STORAGE], "storage_type", MEDIA_SVC_DB_TYPE_INT, "DEFAULT 0", USER_V3, NULL, false, false, false);
730 media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
731 ret = __media_svc_add_column_info(&column_list[MEDIA_SVC_DB_LIST_STORAGE], "validity", MEDIA_SVC_DB_TYPE_INT, "DEFAULT 1", USER_V3, NULL, false, false, false);
732 media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
735 ret = __media_svc_add_column_info(&column_list[MEDIA_SVC_DB_LIST_FACE_SCAN_LIST], "media_id", MEDIA_SVC_DB_TYPE_TEXT, "NOT NULL UNIQUE", USER_V4, NULL, false, true, false);
736 media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
737 ret = __media_svc_add_column_info(&column_list[MEDIA_SVC_DB_LIST_FACE_SCAN_LIST], "modified_time", MEDIA_SVC_DB_TYPE_INT, "DEFAULT 0", USER_V6, NULL, false, false, false);
738 media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
741 ret = __media_svc_add_column_info(&column_list[MEDIA_SVC_DB_LIST_FACE], "media_face_id", MEDIA_SVC_DB_TYPE_INT, "PRIMARY KEY AUTOINCREMENT", USER_V4, NULL, false, false, false);
742 media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
743 ret = __media_svc_add_column_info(&column_list[MEDIA_SVC_DB_LIST_FACE], "media_id", MEDIA_SVC_DB_TYPE_TEXT, "NOT NULL", USER_V4, NULL, true, true, false);
744 media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
745 ret = __media_svc_add_column_info(&column_list[MEDIA_SVC_DB_LIST_FACE], "face_rect_x", MEDIA_SVC_DB_TYPE_INT, "DEFAULT 0", USER_V4, NULL, true, false, false);
746 media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
747 ret = __media_svc_add_column_info(&column_list[MEDIA_SVC_DB_LIST_FACE], "face_rect_y", MEDIA_SVC_DB_TYPE_INT, "DEFAULT 0", USER_V4, NULL, true, false, false);
748 media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
749 ret = __media_svc_add_column_info(&column_list[MEDIA_SVC_DB_LIST_FACE], "face_rect_w", MEDIA_SVC_DB_TYPE_INT, "DEFAULT 0", USER_V4, NULL, true, false, false);
750 media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
751 ret = __media_svc_add_column_info(&column_list[MEDIA_SVC_DB_LIST_FACE], "face_rect_h", MEDIA_SVC_DB_TYPE_INT, "DEFAULT 0", USER_V4, NULL, true, false, false);
752 media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
753 ret = __media_svc_add_column_info(&column_list[MEDIA_SVC_DB_LIST_FACE], "face_orientation", MEDIA_SVC_DB_TYPE_INT, "DEFAULT 0", USER_V4, NULL, false, false, false);
754 media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
755 ret = __media_svc_add_column_info(&column_list[MEDIA_SVC_DB_LIST_FACE], "media_face_tag", MEDIA_SVC_DB_TYPE_TEXT, NULL, USER_V4, NULL, false, false, false);
756 media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
760 void __media_svc_table_free(table_info_s *tb)
762 SAFE_FREE(tb->trigger_name);
763 SAFE_FREE(tb->view_name);
764 SAFE_FREE(tb->event_table);
765 SAFE_FREE(tb->action_table);
769 void __media_svc_column_free(column_info_s *col)
771 SAFE_FREE(col->name);
772 SAFE_FREE(col->type);
773 SAFE_FREE(col->option);
774 SAFE_FREE(col->index_name);
778 void _media_svc_destroy_table_query()
781 table_info_s *tb = NULL;
782 column_info_s *col_ptr = NULL;
786 tb = g_hash_table_lookup(table, MEDIA_SVC_DB_TABLE_MEDIA);
787 __media_svc_table_free(tb);
788 g_hash_table_remove(table, MEDIA_SVC_DB_TABLE_MEDIA);
790 tb = g_hash_table_lookup(table, MEDIA_SVC_DB_TABLE_FOLDER);
791 __media_svc_table_free(tb);
792 g_hash_table_remove(table, MEDIA_SVC_DB_TABLE_FOLDER);
794 tb = g_hash_table_lookup(table, MEDIA_SVC_DB_TABLE_PLAYLIST);
795 __media_svc_table_free(tb);
796 g_hash_table_remove(table, MEDIA_SVC_DB_TABLE_PLAYLIST);
798 tb = g_hash_table_lookup(table, MEDIA_SVC_DB_TABLE_PLAYLIST_MAP);
799 __media_svc_table_free(tb);
800 g_hash_table_remove(table, MEDIA_SVC_DB_TABLE_PLAYLIST_MAP);
802 tb = g_hash_table_lookup(table, MEDIA_SVC_DB_TABLE_ALBUM);
803 __media_svc_table_free(tb);
804 g_hash_table_remove(table, MEDIA_SVC_DB_TABLE_ALBUM);
806 tb = g_hash_table_lookup(table, MEDIA_SVC_DB_TABLE_TAG);
807 __media_svc_table_free(tb);
808 g_hash_table_remove(table, MEDIA_SVC_DB_TABLE_TAG);
810 tb = g_hash_table_lookup(table, MEDIA_SVC_DB_TABLE_TAG_MAP);
811 __media_svc_table_free(tb);
812 g_hash_table_remove(table, MEDIA_SVC_DB_TABLE_TAG_MAP);
814 tb = g_hash_table_lookup(table, MEDIA_SVC_DB_TABLE_BOOKMARK);
815 __media_svc_table_free(tb);
816 g_hash_table_remove(table, MEDIA_SVC_DB_TABLE_BOOKMARK);
818 tb = g_hash_table_lookup(table, MEDIA_SVC_DB_TABLE_STORAGE);
819 __media_svc_table_free(tb);
820 g_hash_table_remove(table, MEDIA_SVC_DB_TABLE_STORAGE);
822 g_hash_table_destroy(table);
825 len = g_slist_length(column_list[MEDIA_SVC_DB_LIST_MEDIA]);
827 for (i = 1; i < len; i++) {
828 col_ptr = g_slist_nth_data(column_list[MEDIA_SVC_DB_LIST_MEDIA], i);
829 __media_svc_column_free(col_ptr);
832 len = g_slist_length(column_list[MEDIA_SVC_DB_LIST_FOLDER]);
834 for (i = 1; i < len; i++) {
835 col_ptr = g_slist_nth_data(column_list[MEDIA_SVC_DB_LIST_FOLDER], i);
836 __media_svc_column_free(col_ptr);
839 len = g_slist_length(column_list[MEDIA_SVC_DB_LIST_PLAYLIST_MAP]);
841 for (i = 1; i < len; i++) {
842 col_ptr = g_slist_nth_data(column_list[MEDIA_SVC_DB_LIST_PLAYLIST_MAP], i);
843 __media_svc_column_free(col_ptr);
846 len = g_slist_length(column_list[MEDIA_SVC_DB_LIST_PLAYLIST]);
848 for (i = 1; i < len; i++) {
849 col_ptr = g_slist_nth_data(column_list[MEDIA_SVC_DB_LIST_PLAYLIST], i);
850 __media_svc_column_free(col_ptr);
853 len = g_slist_length(column_list[MEDIA_SVC_DB_LIST_ALBUM]);
855 for (i = 1; i < len; i++) {
856 col_ptr = g_slist_nth_data(column_list[MEDIA_SVC_DB_LIST_ALBUM], i);
857 __media_svc_column_free(col_ptr);
860 len = g_slist_length(column_list[MEDIA_SVC_DB_LIST_TAG_MAP]);
862 for (i = 1; i < len; i++) {
863 col_ptr = g_slist_nth_data(column_list[MEDIA_SVC_DB_LIST_TAG_MAP], i);
864 __media_svc_column_free(col_ptr);
867 len = g_slist_length(column_list[MEDIA_SVC_DB_LIST_TAG]);
869 for (i = 1; i < len; i++) {
870 col_ptr = g_slist_nth_data(column_list[MEDIA_SVC_DB_LIST_TAG], i);
871 __media_svc_column_free(col_ptr);
874 len = g_slist_length(column_list[MEDIA_SVC_DB_LIST_BOOKMARK]);
876 for (i = 1; i < len; i++) {
877 col_ptr = g_slist_nth_data(column_list[MEDIA_SVC_DB_LIST_BOOKMARK], i);
878 __media_svc_column_free(col_ptr);
881 len = g_slist_length(column_list[MEDIA_SVC_DB_LIST_STORAGE]);
883 for (i = 1; i < len; i++) {
884 col_ptr = g_slist_nth_data(column_list[MEDIA_SVC_DB_LIST_STORAGE], i);
885 __media_svc_column_free(col_ptr);
888 for (i = 0; i < MEDIA_SVC_DB_LIST_MAX; i++)
889 g_slist_free(column_list[i]);
893 static int __alter_table(sqlite3 *db_handle, uid_t uid)
895 int ret = MS_MEDIA_ERR_NONE;
897 ret = __media_svc_upgrade_table_query(db_handle, MEDIA_SVC_DB_TABLE_MEDIA, MEDIA_SVC_DB_LIST_MEDIA, uid);
898 media_svc_retvm_if(ret != MS_MEDIA_ERR_NONE, ret, "fail to __media_svc_upgrade_table_query [%d]", ret);
900 ret = __media_svc_upgrade_table_query(db_handle, MEDIA_SVC_DB_TABLE_PLAYLIST_MAP, MEDIA_SVC_DB_LIST_PLAYLIST_MAP, uid);
901 media_svc_retvm_if(ret != MS_MEDIA_ERR_NONE, ret, "fail to __media_svc_upgrade_table_query [%d]", ret);
903 ret = __media_svc_upgrade_table_query(db_handle, MEDIA_SVC_DB_TABLE_PLAYLIST, MEDIA_SVC_DB_LIST_PLAYLIST, uid);
904 media_svc_retvm_if(ret != MS_MEDIA_ERR_NONE, ret, "fail to __media_svc_upgrade_table_query [%d]", ret);
906 ret = __media_svc_upgrade_table_query(db_handle, MEDIA_SVC_DB_TABLE_ALBUM, MEDIA_SVC_DB_LIST_ALBUM, uid);
907 media_svc_retvm_if(ret != MS_MEDIA_ERR_NONE, ret, "fail to __media_svc_upgrade_table_query [%d]", ret);
909 ret = __media_svc_upgrade_table_query(db_handle, MEDIA_SVC_DB_TABLE_TAG_MAP, MEDIA_SVC_DB_LIST_TAG_MAP, uid);
910 media_svc_retvm_if(ret != MS_MEDIA_ERR_NONE, ret, "fail to __media_svc_upgrade_table_query [%d]", ret);
912 ret = __media_svc_upgrade_table_query(db_handle, MEDIA_SVC_DB_TABLE_TAG, MEDIA_SVC_DB_LIST_TAG, uid);
913 media_svc_retvm_if(ret != MS_MEDIA_ERR_NONE, ret, "fail to __media_svc_upgrade_table_query [%d]", ret);
915 ret = __media_svc_upgrade_table_query(db_handle, MEDIA_SVC_DB_TABLE_BOOKMARK, MEDIA_SVC_DB_LIST_BOOKMARK, uid);
916 media_svc_retvm_if(ret != MS_MEDIA_ERR_NONE, ret, "fail to __media_svc_upgrade_table_query [%d]", ret);
918 ret = __media_svc_upgrade_table_query(db_handle, MEDIA_SVC_DB_TABLE_STORAGE, MEDIA_SVC_DB_LIST_STORAGE, uid);
919 media_svc_retvm_if(ret != MS_MEDIA_ERR_NONE, ret, "fail to __media_svc_upgrade_table_query [%d]", ret);
924 static int __update_db_from_V6_to_V8(uid_t uid) //tizen 4.0 to 5.5
926 int ret = MS_MEDIA_ERR_NONE;
930 sql = sqlite3_mprintf("DROP VIEW IF EXISTS %q;DROP VIEW IF EXISTS %q;DROP VIEW IF EXISTS %q",
931 MEDIA_SVC_DB_VIEW_PLAYLIST, MEDIA_SVC_DB_VIEW_TAG, MEDIA_SVC_DB_VIEW_MEDIA);
933 ret = _media_svc_sql_query(sql, uid);
934 SQLITE3_SAFE_FREE(sql);
935 media_svc_retvm_if(ret != MS_MEDIA_ERR_NONE, ret, "fail to _media_svc_sql_query [%d]", ret);
937 /******Upgrade Media Table******/
938 /* remove user data columns */
939 sql = sqlite3_mprintf("ALTER TABLE %q RENAME TO %q;", MEDIA_SVC_DB_TABLE_MEDIA, MEDIA_SVC_DB_TABLE_TMP_TABLE);
941 ret = _media_svc_sql_query(sql, uid);
942 SQLITE3_SAFE_FREE(sql);
943 media_svc_retvm_if(ret != MS_MEDIA_ERR_NONE, ret, "fail to _media_svc_sql_query [%d]", ret);
945 ret = _media_svc_make_table_query(MEDIA_SVC_DB_TABLE_MEDIA, MEDIA_SVC_DB_LIST_MEDIA, uid);
946 media_svc_retvm_if(ret != MS_MEDIA_ERR_NONE, ret, "fail to _media_svc_make_table_query [%d]", ret);
948 sql = sqlite3_mprintf("INSERT INTO %q (media_id, media_path, media_display_name, media_type, media_mime_type, media_size, media_added_time, media_modified_time, folder_id, media_thumbnail_path, media_title, album_id, media_album, media_artist, media_album_artist, media_genre, media_composer, media_year, media_recorded_date, media_copyright, media_track_num, media_description, media_bitrate, media_bitpersample, media_samplerate, media_channel, media_duration, media_longitude, media_latitude, media_altitude, exposure_time, fnumber, iso, model, media_width, media_height, media_datetaken, media_orientation, media_rating, media_favourite, media_is_drm, media_storage_type, media_timeline, sync_status, media_file_name_pinyin, media_title_pinyin, media_album_pinyin, media_artist_pinyin, media_album_artist_pinyin, media_genre_pinyin, media_composer_pinyin, media_copyright_pinyin, media_description_pinyin, age_rating_pinyin, keyword_pinyin, storage_uuid, validity, media_360) \
949 SELECT media_uuid, path, file_name, media_type, mime_type, size, added_time, modified_time, folder_uuid, thumbnail_path, title, album_id, album, artist, album_artist, genre, composer, year, recorded_date, copyright, track_num, description, bitrate, bitpersample, samplerate, channel, duration, longitude, latitude, altitude, exposure_time, fnumber, iso, model, width, height, datetaken, orientation, rating, favourite, is_drm, storage_type, timeline, sync_status, file_name_pinyin, title_pinyin, album_pinyin, artist_pinyin, album_artist_pinyin, genre_pinyin, composer_pinyin, copyright_pinyin, description_pinyin, age_rating_pinyin, keyword_pinyin, storage_uuid, validity, is_360 FROM %q;",
950 MEDIA_SVC_DB_TABLE_MEDIA, MEDIA_SVC_DB_TABLE_TMP_TABLE);
952 ret = _media_svc_sql_query(sql, uid);
953 SQLITE3_SAFE_FREE(sql);
954 media_svc_retvm_if(ret != MS_MEDIA_ERR_NONE, ret, "fail to _media_svc_sql_query [%d]", ret);
956 sql = sqlite3_mprintf("DROP TABLE %q;", MEDIA_SVC_DB_TABLE_TMP_TABLE);
958 ret = _media_svc_sql_query(sql, uid);
959 SQLITE3_SAFE_FREE(sql);
960 media_svc_retvm_if(ret != MS_MEDIA_ERR_NONE, ret, "fail to _media_svc_sql_query [%d]", ret);
962 /******Upgrade Folder Table******/
963 /* remove "scan_status" column */
964 sql = sqlite3_mprintf("ALTER TABLE %q RENAME TO %q;", MEDIA_SVC_DB_TABLE_FOLDER, MEDIA_SVC_DB_TABLE_TMP_TABLE);
966 ret = _media_svc_sql_query(sql, uid);
967 SQLITE3_SAFE_FREE(sql);
968 media_svc_retvm_if(ret != MS_MEDIA_ERR_NONE, ret, "fail to _media_svc_sql_query [%d]", ret);
970 ret = _media_svc_make_table_query(MEDIA_SVC_DB_TABLE_FOLDER, MEDIA_SVC_DB_LIST_FOLDER, uid);
971 media_svc_retvm_if(ret != MS_MEDIA_ERR_NONE, ret, "fail to _media_svc_make_table_query [%d]", ret);
973 sql = sqlite3_mprintf("INSERT INTO %q (folder_id, folder_path, folder_name, folder_modified_time, folder_name_pinyin, folder_storage_type, storage_uuid, validity) SELECT folder_uuid, path, name, modified_time, name_pinyin, storage_type, storage_uuid, validity FROM %q;", MEDIA_SVC_DB_TABLE_FOLDER, MEDIA_SVC_DB_TABLE_TMP_TABLE);
975 ret = _media_svc_sql_query(sql, uid);
976 SQLITE3_SAFE_FREE(sql);
977 media_svc_retvm_if(ret != MS_MEDIA_ERR_NONE, ret, "fail to _media_svc_sql_query [%d]", ret);
979 sql = sqlite3_mprintf("DROP TABLE %q;", MEDIA_SVC_DB_TABLE_TMP_TABLE);
981 ret = _media_svc_sql_query(sql, uid);
982 SQLITE3_SAFE_FREE(sql);
983 media_svc_retvm_if(ret != MS_MEDIA_ERR_NONE, ret, "fail to _media_svc_sql_query [%d]", ret);
985 /******Upgrade Storage Table******/
986 /* remove "scan_status", "storage_name" column */
987 sql = sqlite3_mprintf("ALTER TABLE %q RENAME TO %q;", MEDIA_SVC_DB_TABLE_STORAGE, MEDIA_SVC_DB_TABLE_TMP_TABLE);
989 ret = _media_svc_sql_query(sql, uid);
990 SQLITE3_SAFE_FREE(sql);
991 media_svc_retvm_if(ret != MS_MEDIA_ERR_NONE, ret, "fail to _media_svc_sql_query [%d]", ret);
993 ret = _media_svc_make_table_query(MEDIA_SVC_DB_TABLE_STORAGE, MEDIA_SVC_DB_LIST_STORAGE, uid);
994 media_svc_retvm_if(ret != MS_MEDIA_ERR_NONE, ret, "fail to _media_svc_make_table_query [%d]", ret);
996 sql = sqlite3_mprintf("INSERT INTO %q (storage_id, storage_path, storage_type, validity) SELECT storage_uuid, storage_path, storage_type, validity FROM %q;", MEDIA_SVC_DB_TABLE_STORAGE, MEDIA_SVC_DB_TABLE_TMP_TABLE);
998 ret = _media_svc_sql_query(sql, uid);
999 SQLITE3_SAFE_FREE(sql);
1000 media_svc_retvm_if(ret != MS_MEDIA_ERR_NONE, ret, "fail to _media_svc_sql_query [%d]", ret);
1002 sql = sqlite3_mprintf("DROP TABLE %q;", MEDIA_SVC_DB_TABLE_TMP_TABLE);
1004 ret = _media_svc_sql_query(sql, uid);
1005 SQLITE3_SAFE_FREE(sql);
1006 media_svc_retvm_if(ret != MS_MEDIA_ERR_NONE, ret, "fail to _media_svc_sql_query [%d]", ret);
1008 /******Upgrade face Table******/
1009 /* Change "face_uuid" to "face_id" (face_uuid is TEXT, face_id is INTEGER) */
1010 sql = sqlite3_mprintf("ALTER TABLE %q RENAME TO %q;", MEDIA_SVC_DB_TABLE_FACE, MEDIA_SVC_DB_TABLE_TMP_TABLE);
1012 ret = _media_svc_sql_query(sql, uid);
1013 SQLITE3_SAFE_FREE(sql);
1014 media_svc_retvm_if(ret != MS_MEDIA_ERR_NONE, ret, "fail to _media_svc_sql_query [%d]", ret);
1016 ret = _media_svc_make_table_query(MEDIA_SVC_DB_TABLE_FACE, MEDIA_SVC_DB_LIST_FACE, uid);
1017 media_svc_retvm_if(ret != MS_MEDIA_ERR_NONE, ret, "fail to _media_svc_make_table_query [%d]", ret);
1019 sql = sqlite3_mprintf("INSERT INTO %q (media_id, face_rect_x, face_rect_y, face_rect_w, face_rect_h, face_orientation, media_face_tag) SELECT media_uuid, face_rect_x, face_rect_y, face_rect_w, face_rect_h, orientation, face_tag FROM %q;", MEDIA_SVC_DB_TABLE_FACE, MEDIA_SVC_DB_TABLE_TMP_TABLE);
1021 ret = _media_svc_sql_query(sql, uid);
1022 SQLITE3_SAFE_FREE(sql);
1023 media_svc_retvm_if(ret != MS_MEDIA_ERR_NONE, ret, "fail to _media_svc_sql_query [%d]", ret);
1025 sql = sqlite3_mprintf("DROP TABLE %q;", MEDIA_SVC_DB_TABLE_TMP_TABLE);
1027 ret = _media_svc_sql_query(sql, uid);
1028 SQLITE3_SAFE_FREE(sql);
1029 media_svc_retvm_if(ret != MS_MEDIA_ERR_NONE, ret, "fail to _media_svc_sql_query [%d]", ret);
1031 /******Upgrade face_scan_list Table******/
1032 /* Drop trigger first */
1033 sql = sqlite3_mprintf("DROP TRIGGER IF EXISTS %q_%q;DROP TRIGGER IF EXISTS %q_%q;",
1034 MEDIA_SVC_DB_TRIGGER_FACE, MEDIA_SVC_DB_TABLE_FACE_SCAN_LIST, MEDIA_SVC_DB_TRIGGER_FACE_SCAN_LIST, MEDIA_SVC_DB_TABLE_MEDIA);
1036 ret = _media_svc_sql_query(sql, uid);
1037 SQLITE3_SAFE_FREE(sql);
1038 media_svc_retvm_if(ret != MS_MEDIA_ERR_NONE, ret, "fail to _media_svc_sql_query [%d]", ret);
1040 /* remove "storage_uuid" column */
1041 sql = sqlite3_mprintf("ALTER TABLE %q RENAME TO %q;", MEDIA_SVC_DB_TABLE_FACE_SCAN_LIST, MEDIA_SVC_DB_TABLE_TMP_TABLE);
1043 ret = _media_svc_sql_query(sql, uid);
1044 SQLITE3_SAFE_FREE(sql);
1045 media_svc_retvm_if(ret != MS_MEDIA_ERR_NONE, ret, "fail to _media_svc_sql_query [%d]", ret);
1047 ret = _media_svc_make_table_query(MEDIA_SVC_DB_TABLE_FACE_SCAN_LIST, MEDIA_SVC_DB_LIST_FACE_SCAN_LIST, uid);
1048 media_svc_retvm_if(ret != MS_MEDIA_ERR_NONE, ret, "fail to _media_svc_make_table_query [%d]", ret);
1050 sql = sqlite3_mprintf("INSERT INTO %q (media_id, modified_time) SELECT media_uuid, modified_time FROM %q;", MEDIA_SVC_DB_TABLE_FACE_SCAN_LIST, MEDIA_SVC_DB_TABLE_TMP_TABLE);
1052 ret = _media_svc_sql_query(sql, uid);
1053 SQLITE3_SAFE_FREE(sql);
1054 media_svc_retvm_if(ret != MS_MEDIA_ERR_NONE, ret, "fail to _media_svc_sql_query [%d]", ret);
1056 sql = sqlite3_mprintf("DROP TABLE %q;", MEDIA_SVC_DB_TABLE_TMP_TABLE);
1058 ret = _media_svc_sql_query(sql, uid);
1059 SQLITE3_SAFE_FREE(sql);
1060 media_svc_retvm_if(ret != MS_MEDIA_ERR_NONE, ret, "fail to _media_svc_sql_query [%d]", ret);
1065 static int __update_db_to_latest(int cur_version, uid_t uid) //tizen 5.0(and before) to 5.5
1067 int ret = MS_MEDIA_ERR_NONE;
1071 sql = sqlite3_mprintf("DROP VIEW IF EXISTS %q;DROP VIEW IF EXISTS %q;DROP VIEW IF EXISTS %q",
1072 MEDIA_SVC_DB_VIEW_PLAYLIST, MEDIA_SVC_DB_VIEW_TAG, MEDIA_SVC_DB_VIEW_MEDIA);
1074 ret = _media_svc_sql_query(sql, uid);
1075 SQLITE3_SAFE_FREE(sql);
1076 media_svc_retvm_if(ret != MS_MEDIA_ERR_NONE, ret, "fail to _media_svc_sql_query [%d]", ret);
1078 if (cur_version == USER_V6) {
1079 /* Drop trigger first */
1080 sql = sqlite3_mprintf("DROP TRIGGER IF EXISTS %q_%q;DROP TRIGGER IF EXISTS %q_%q;",
1081 MEDIA_SVC_DB_TRIGGER_FACE, MEDIA_SVC_DB_TABLE_FACE_SCAN_LIST, MEDIA_SVC_DB_TRIGGER_FACE_SCAN_LIST, MEDIA_SVC_DB_TABLE_MEDIA);
1083 ret = _media_svc_sql_query(sql, uid);
1084 SQLITE3_SAFE_FREE(sql);
1085 media_svc_retvm_if(ret != MS_MEDIA_ERR_NONE, ret, "fail to _media_svc_sql_query [%d]", ret);
1089 sql = sqlite3_mprintf("ALTER TABLE %q RENAME COLUMN play_order TO playlist_member_order;\
1090 ALTER TABLE %q RENAME COLUMN name TO playlist_name;\
1091 ALTER TABLE %q RENAME COLUMN media_uuid TO media_id;",
1092 MEDIA_SVC_DB_TABLE_PLAYLIST_MAP, MEDIA_SVC_DB_TABLE_PLAYLIST, MEDIA_SVC_DB_TABLE_PLAYLIST_MAP);
1094 ret = _media_svc_sql_query(sql, uid);
1095 SQLITE3_SAFE_FREE(sql);
1096 media_svc_retvm_if(ret != MS_MEDIA_ERR_NONE, ret, "fail to _media_svc_sql_query [%d]", ret);
1099 sql = sqlite3_mprintf("ALTER TABLE %q RENAME COLUMN name TO tag_name;\
1100 ALTER TABLE %q RENAME COLUMN media_uuid TO media_id;",
1101 MEDIA_SVC_DB_TABLE_TAG, MEDIA_SVC_DB_TABLE_TAG_MAP);
1103 ret = _media_svc_sql_query(sql, uid);
1104 SQLITE3_SAFE_FREE(sql);
1105 media_svc_retvm_if(ret != MS_MEDIA_ERR_NONE, ret, "fail to _media_svc_sql_query [%d]", ret);
1108 sql = sqlite3_mprintf("ALTER TABLE %q RENAME COLUMN marked_time TO bookmark_marked_time;\
1109 ALTER TABLE %q RENAME COLUMN name TO bookmark_name;\
1110 ALTER TABLE %q RENAME COLUMN thumbnail_path TO bookmark_thumbnail_path;\
1111 ALTER TABLE %q RENAME COLUMN media_uuid TO media_id;",
1112 MEDIA_SVC_DB_TABLE_BOOKMARK, MEDIA_SVC_DB_TABLE_BOOKMARK,
1113 MEDIA_SVC_DB_TABLE_BOOKMARK, MEDIA_SVC_DB_TABLE_BOOKMARK);
1115 ret = _media_svc_sql_query(sql, uid);
1116 SQLITE3_SAFE_FREE(sql);
1117 media_svc_retvm_if(ret != MS_MEDIA_ERR_NONE, ret, "fail to _media_svc_sql_query [%d]", ret);
1119 if (cur_version == USER_V7) {
1121 sql = sqlite3_mprintf("ALTER TABLE %q RENAME COLUMN face_id TO media_face_id;\
1122 ALTER TABLE %q RENAME COLUMN orientation TO face_orientation;\
1123 ALTER TABLE %q RENAME COLUMN face_tag TO media_face_tag;\
1124 ALTER TABLE %q RENAME COLUMN media_uuid TO media_id;\
1125 ALTER TABLE %q RENAME COLUMN media_uuid TO media_id;",
1126 MEDIA_SVC_DB_TABLE_FACE, MEDIA_SVC_DB_TABLE_FACE, MEDIA_SVC_DB_TABLE_FACE,
1127 MEDIA_SVC_DB_TABLE_FACE_SCAN_LIST, MEDIA_SVC_DB_TABLE_FACE);
1129 ret = _media_svc_sql_query(sql, uid);
1130 SQLITE3_SAFE_FREE(sql);
1131 media_svc_retvm_if(ret != MS_MEDIA_ERR_NONE, ret, "fail to _media_svc_sql_query [%d]", ret);
1134 sql = sqlite3_mprintf("ALTER TABLE %q RENAME COLUMN modified_time TO folder_modified_time;\
1135 ALTER TABLE %q RENAME COLUMN path TO folder_path;\
1136 ALTER TABLE %q RENAME COLUMN name TO folder_name;\
1137 ALTER TABLE %q RENAME COLUMN name_pinyin TO folder_name_pinyin;\
1138 ALTER TABLE %q RENAME COLUMN storage_type TO folder_storage_type;\
1139 ALTER TABLE %q RENAME COLUMN folder_uuid TO folder_id;",
1140 MEDIA_SVC_DB_TABLE_FOLDER, MEDIA_SVC_DB_TABLE_FOLDER, MEDIA_SVC_DB_TABLE_FOLDER,
1141 MEDIA_SVC_DB_TABLE_FOLDER, MEDIA_SVC_DB_TABLE_FOLDER, MEDIA_SVC_DB_TABLE_FOLDER);
1143 ret = _media_svc_sql_query(sql, uid);
1144 SQLITE3_SAFE_FREE(sql);
1145 media_svc_retvm_if(ret != MS_MEDIA_ERR_NONE, ret, "fail to _media_svc_sql_query [%d]", ret);
1148 sql = sqlite3_mprintf("ALTER TABLE %q RENAME COLUMN storage_uuid TO storage_id;",
1149 MEDIA_SVC_DB_TABLE_STORAGE);
1151 ret = _media_svc_sql_query(sql, uid);
1152 SQLITE3_SAFE_FREE(sql);
1153 media_svc_retvm_if(ret != MS_MEDIA_ERR_NONE, ret, "fail to _media_svc_sql_query [%d]", ret);
1156 sql = sqlite3_mprintf("ALTER TABLE %q RENAME COLUMN file_name_pinyin TO media_file_name_pinyin;\
1157 ALTER TABLE %q RENAME COLUMN title_pinyin TO media_title_pinyin;\
1158 ALTER TABLE %q RENAME COLUMN album_pinyin TO media_album_pinyin;\
1159 ALTER TABLE %q RENAME COLUMN artist_pinyin TO media_artist_pinyin;\
1160 ALTER TABLE %q RENAME COLUMN album_artist_pinyin TO media_album_artist_pinyin;\
1161 ALTER TABLE %q RENAME COLUMN genre_pinyin TO media_genre_pinyin;\
1162 ALTER TABLE %q RENAME COLUMN composer_pinyin TO media_composer_pinyin;\
1163 ALTER TABLE %q RENAME COLUMN copyright_pinyin TO media_copyright_pinyin;\
1164 ALTER TABLE %q RENAME COLUMN description_pinyin TO media_description_pinyin;\
1165 ALTER TABLE %q RENAME COLUMN folder_uuid TO folder_id;\
1166 ALTER TABLE %q RENAME COLUMN media_uuid TO media_id;\
1167 ALTER TABLE %q RENAME COLUMN path TO media_path;\
1168 ALTER TABLE %q RENAME COLUMN file_name TO media_display_name;\
1169 ALTER TABLE %q RENAME COLUMN mime_type TO media_mime_type;\
1170 ALTER TABLE %q RENAME COLUMN size TO media_size;\
1171 ALTER TABLE %q RENAME COLUMN added_time TO media_added_time;\
1172 ALTER TABLE %q RENAME COLUMN modified_time TO media_modified_time;\
1173 ALTER TABLE %q RENAME COLUMN thumbnail_path TO media_thumbnail_path;\
1174 ALTER TABLE %q RENAME COLUMN title TO media_title;\
1175 ALTER TABLE %q RENAME COLUMN album TO media_album;\
1176 ALTER TABLE %q RENAME COLUMN artist TO media_artist;\
1177 ALTER TABLE %q RENAME COLUMN album_artist TO media_album_artist;\
1178 ALTER TABLE %q RENAME COLUMN genre TO media_genre;\
1179 ALTER TABLE %q RENAME COLUMN composer TO media_composer;\
1180 ALTER TABLE %q RENAME COLUMN year TO media_year;\
1181 ALTER TABLE %q RENAME COLUMN recorded_date TO media_recorded_date;\
1182 ALTER TABLE %q RENAME COLUMN copyright TO media_copyright;\
1183 ALTER TABLE %q RENAME COLUMN track_num TO media_track_num;\
1184 ALTER TABLE %q RENAME COLUMN description TO media_description;\
1185 ALTER TABLE %q RENAME COLUMN bitrate TO media_bitrate;\
1186 ALTER TABLE %q RENAME COLUMN bitpersample TO media_bitpersample;\
1187 ALTER TABLE %q RENAME COLUMN samplerate TO media_samplerate;\
1188 ALTER TABLE %q RENAME COLUMN channel TO media_channel;\
1189 ALTER TABLE %q RENAME COLUMN duration TO media_duration;\
1190 ALTER TABLE %q RENAME COLUMN longitude TO media_longitude;\
1191 ALTER TABLE %q RENAME COLUMN latitude TO media_latitude;\
1192 ALTER TABLE %q RENAME COLUMN altitude TO media_altitude;\
1193 ALTER TABLE %q RENAME COLUMN width TO media_width;\
1194 ALTER TABLE %q RENAME COLUMN height TO media_height;\
1195 ALTER TABLE %q RENAME COLUMN datetaken TO media_datetaken;\
1196 ALTER TABLE %q RENAME COLUMN orientation TO media_orientation;\
1197 ALTER TABLE %q RENAME COLUMN rating TO media_rating;\
1198 ALTER TABLE %q RENAME COLUMN favourite TO media_favourite;\
1199 ALTER TABLE %q RENAME COLUMN is_drm TO media_is_drm;\
1200 ALTER TABLE %q RENAME COLUMN storage_type TO media_storage_type;\
1201 ALTER TABLE %q RENAME COLUMN timeline TO media_timeline;\
1202 ALTER TABLE %q RENAME COLUMN is_360 TO media_360;",
1203 MEDIA_SVC_DB_TABLE_MEDIA,
1204 MEDIA_SVC_DB_TABLE_MEDIA,
1205 MEDIA_SVC_DB_TABLE_MEDIA,
1206 MEDIA_SVC_DB_TABLE_MEDIA,
1207 MEDIA_SVC_DB_TABLE_MEDIA,
1208 MEDIA_SVC_DB_TABLE_MEDIA,
1209 MEDIA_SVC_DB_TABLE_MEDIA,
1210 MEDIA_SVC_DB_TABLE_MEDIA,
1211 MEDIA_SVC_DB_TABLE_MEDIA,
1212 MEDIA_SVC_DB_TABLE_MEDIA,
1213 MEDIA_SVC_DB_TABLE_MEDIA,
1214 MEDIA_SVC_DB_TABLE_MEDIA,
1215 MEDIA_SVC_DB_TABLE_MEDIA,
1216 MEDIA_SVC_DB_TABLE_MEDIA,
1217 MEDIA_SVC_DB_TABLE_MEDIA,
1218 MEDIA_SVC_DB_TABLE_MEDIA,
1219 MEDIA_SVC_DB_TABLE_MEDIA,
1220 MEDIA_SVC_DB_TABLE_MEDIA,
1221 MEDIA_SVC_DB_TABLE_MEDIA,
1222 MEDIA_SVC_DB_TABLE_MEDIA,
1223 MEDIA_SVC_DB_TABLE_MEDIA,
1224 MEDIA_SVC_DB_TABLE_MEDIA,
1225 MEDIA_SVC_DB_TABLE_MEDIA,
1226 MEDIA_SVC_DB_TABLE_MEDIA,
1227 MEDIA_SVC_DB_TABLE_MEDIA,
1228 MEDIA_SVC_DB_TABLE_MEDIA,
1229 MEDIA_SVC_DB_TABLE_MEDIA,
1230 MEDIA_SVC_DB_TABLE_MEDIA,
1231 MEDIA_SVC_DB_TABLE_MEDIA,
1232 MEDIA_SVC_DB_TABLE_MEDIA,
1233 MEDIA_SVC_DB_TABLE_MEDIA,
1234 MEDIA_SVC_DB_TABLE_MEDIA,
1235 MEDIA_SVC_DB_TABLE_MEDIA,
1236 MEDIA_SVC_DB_TABLE_MEDIA,
1237 MEDIA_SVC_DB_TABLE_MEDIA,
1238 MEDIA_SVC_DB_TABLE_MEDIA,
1239 MEDIA_SVC_DB_TABLE_MEDIA,
1240 MEDIA_SVC_DB_TABLE_MEDIA,
1241 MEDIA_SVC_DB_TABLE_MEDIA,
1242 MEDIA_SVC_DB_TABLE_MEDIA,
1243 MEDIA_SVC_DB_TABLE_MEDIA,
1244 MEDIA_SVC_DB_TABLE_MEDIA,
1245 MEDIA_SVC_DB_TABLE_MEDIA,
1246 MEDIA_SVC_DB_TABLE_MEDIA,
1247 MEDIA_SVC_DB_TABLE_MEDIA,
1248 MEDIA_SVC_DB_TABLE_MEDIA,
1249 MEDIA_SVC_DB_TABLE_MEDIA);
1251 ret = _media_svc_sql_query(sql, uid);
1252 SQLITE3_SAFE_FREE(sql);
1253 media_svc_retvm_if(ret != MS_MEDIA_ERR_NONE, ret, "fail to _media_svc_sql_query [%d]", ret);
1259 static int __media_svc_db_upgrade(sqlite3 *db_handle, int cur_version, uid_t uid)
1261 int ret = MS_MEDIA_ERR_NONE;
1264 media_svc_debug_fenter();
1266 ret = _media_svc_init_table_query(MEDIA_SVC_DB_TABLE_MEDIA);
1267 if (ret != MS_MEDIA_ERR_NONE) {
1268 media_svc_error("Query initialization failed [%d]", ret);
1272 ret = __alter_table(db_handle, uid);
1273 if (ret != MS_MEDIA_ERR_NONE) {
1274 media_svc_error("fail to __alter_table [%d]", ret);
1278 ret = __update_db_from_V6_to_V8(uid);
1280 /* Upgrade issue tizen 4.0 to 5.5 */
1281 if (cur_version == USER_V6) {
1282 ret = __update_db_from_V6_to_V8(uid);
1283 if (ret != MS_MEDIA_ERR_NONE) {
1284 media_svc_error("fail to __update_db_from_V6_to_V8 [%d]", ret);
1289 /* Upgrade issue tizen 5.0 to 5.5 */
1290 if (cur_version < USER_V8) {
1291 ret = __update_db_to_latest(cur_version, uid);
1292 if (ret != MS_MEDIA_ERR_NONE) {
1293 media_svc_error("fail to __update_db_from_V6_to_V8 [%d]", ret);
1298 if (cur_version == USER_V6) {
1299 /* Create the removed trigger */
1300 sql = sqlite3_mprintf(MEDIA_SVC_DB_QUERY_TRIGGER, "face_cleanup_face_scan_list", MEDIA_SVC_DB_TABLE_FACE_SCAN_LIST, MEDIA_SVC_DB_TABLE_FACE, "media_id", "media_id");
1302 ret = _media_svc_sql_query(sql, uid);
1303 SQLITE3_SAFE_FREE(sql);
1304 if (ret != MS_MEDIA_ERR_NONE) {
1305 media_svc_error("_media_svc_sql_query failed");
1309 sql = sqlite3_mprintf(MEDIA_SVC_DB_QUERY_TRIGGER, "face_scan_list_cleanup_media", MEDIA_SVC_DB_TABLE_MEDIA, MEDIA_SVC_DB_TABLE_FACE_SCAN_LIST, "media_id", "media_id");
1311 ret = _media_svc_sql_query(sql, uid);
1312 SQLITE3_SAFE_FREE(sql);
1313 if (ret != MS_MEDIA_ERR_NONE) {
1314 media_svc_error("_media_svc_sql_query failed");
1319 /* Rebuilding view */
1320 ret = __media_svc_rebuild_view_query(db_handle, uid);
1322 /* Update user version */
1323 sql = sqlite3_mprintf("PRAGMA user_version=%d;", LATEST_VERSION_NUMBER);
1325 ret = _media_svc_sql_query(sql, uid);
1326 SQLITE3_SAFE_FREE(sql);
1328 _media_svc_destroy_table_query();
1330 media_svc_debug_fleave();
1335 _media_svc_destroy_table_query();
1340 int _media_svc_sql_query(const char *sql_str, uid_t uid)
1342 return media_db_request_update_db(sql_str, uid);
1345 int _media_svc_sql_query_direct(const char *sql_str, uid_t uid)
1347 int ret = MS_MEDIA_ERR_NONE;
1348 sqlite3 *handle = NULL;
1350 ret = media_db_connect(&handle, uid, true);
1351 media_svc_retvm_if(ret != MS_MEDIA_ERR_NONE, ret, "DB connection failed");
1352 ret = __media_svc_query_direct(handle, sql_str, uid);
1353 media_db_disconnect(handle);
1358 int _media_svc_get_user_version(sqlite3 *db_handle, int *user_version)
1360 int ret = MS_MEDIA_ERR_NONE;
1361 sqlite3_stmt *sql_stmt = NULL;
1362 char *sql = sqlite3_mprintf("PRAGMA user_version;");
1364 ret = _media_svc_sql_prepare_to_step(db_handle, sql, &sql_stmt);
1365 if (ret != MS_MEDIA_ERR_NONE) {
1366 media_svc_error("error when get user_version.");
1370 *user_version = sqlite3_column_int(sql_stmt, 0);
1371 SQLITE3_FINALIZE(sql_stmt);
1373 return MS_MEDIA_ERR_NONE;
1376 int _media_svc_sql_prepare_to_step(sqlite3 *handle, const char *sql_str, sqlite3_stmt **stmt)
1380 media_svc_retvm_if(sql_str == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "invalid query");
1382 if (handle == NULL) {
1383 media_svc_error("handle is NULL");
1384 sqlite3_free((char *)sql_str);
1385 return MS_MEDIA_ERR_INVALID_PARAMETER;
1388 media_svc_sec_debug("Query[%s]", sql_str);
1390 err = sqlite3_prepare_v2(handle, sql_str, -1, stmt, NULL);
1391 sqlite3_free((char *)sql_str);
1393 if (err != SQLITE_OK) {
1394 media_svc_error("prepare error %d[%s]", err, sqlite3_errmsg(handle));
1395 if (err == SQLITE_CORRUPT)
1396 return MS_MEDIA_ERR_DB_CORRUPT;
1397 else if (err == SQLITE_PERM)
1398 return MS_MEDIA_ERR_DB_PERMISSION;
1400 return MS_MEDIA_ERR_DB_INTERNAL;
1403 err = sqlite3_step(*stmt);
1404 if (err != SQLITE_ROW) {
1405 media_svc_debug("No record");
1406 SQLITE3_FINALIZE(*stmt);
1407 return MS_MEDIA_ERR_DB_NO_RECORD;
1410 return MS_MEDIA_ERR_NONE;
1413 int _media_svc_sql_prepare_to_step_simple(sqlite3 *handle, const char *sql_str, sqlite3_stmt **stmt)
1417 media_svc_retvm_if(handle == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "handle is NULL");
1419 media_svc_sec_debug("Query[%s]", sql_str);
1421 if (!STRING_VALID(sql_str)) {
1422 media_svc_error("invalid query");
1423 return MS_MEDIA_ERR_INVALID_PARAMETER;
1426 err = sqlite3_prepare_v2(handle, sql_str, -1, stmt, NULL);
1427 sqlite3_free((char *)sql_str);
1429 if (err != SQLITE_OK) {
1430 media_svc_error("prepare error %d[%s]", err, sqlite3_errmsg(handle));
1431 if (err == SQLITE_CORRUPT)
1432 return MS_MEDIA_ERR_DB_CORRUPT;
1433 else if (err == SQLITE_PERM)
1434 return MS_MEDIA_ERR_DB_PERMISSION;
1436 return MS_MEDIA_ERR_DB_INTERNAL;
1439 return MS_MEDIA_ERR_NONE;
1442 int _media_svc_sql_query_list(GList **query_list, uid_t uid)
1444 int ret = MS_MEDIA_ERR_NONE;
1446 int length = g_list_length(*query_list);
1448 char query_bundle[MEDIA_SVC_QUERY_LEN_MAX] = {0, };
1452 media_svc_debug("query list length : [%d]", length);
1454 memset(query_bundle, 0, sizeof(query_bundle));
1456 for (idx = 0; idx < length; idx++) {
1457 sql = (char *)g_list_nth_data(*query_list, idx);
1458 if (STRING_VALID(sql)) {
1459 query_len = strlen(sql);
1461 if (query_len >= (sizeof(query_bundle) - 1)) {
1462 media_svc_error("NEED TO CHECK FILE : A single query size exceeds 8k [%d]", query_len);
1463 SQLITE3_SAFE_FREE(sql);
1467 if ((total_len + query_len) >= (sizeof(query_bundle) - 1)) {
1468 ret = media_db_request_update_db(query_bundle, uid);
1469 if (ret != MS_MEDIA_ERR_NONE)
1470 media_svc_error("media_db_request_update_db failed : %d", ret);
1472 memset(query_bundle, 0, sizeof(query_bundle));
1474 /* NEED TO CHECK : If a single query size is over 8K, drop it. */
1475 /* Consider inserting the basic information without metadata */
1476 /* Refer : SQLITE3 spec In Tizen 3.0 */
1477 /* Single query limit size = 1 billion bytes(1GiB) */
1478 /* DB limit size = 1 billion bytes(1GiB) */
1479 /* column limit = 2000 */
1482 SAFE_STRLCAT(query_bundle, sql, sizeof(query_bundle));
1483 total_len += query_len;
1485 SQLITE3_SAFE_FREE(sql);
1489 if (total_len > 0) {
1490 ret = media_db_request_update_db(query_bundle, uid);
1491 if (ret != MS_MEDIA_ERR_NONE)
1492 media_svc_error("media_db_request_update_db failed : %d", ret);
1494 memset(query_bundle, 0, sizeof(query_bundle));
1498 _media_svc_sql_query_release(query_list);
1500 return MS_MEDIA_ERR_NONE;
1505 #define SLEEP_TIME 1000 * 1000
1506 static int __media_svc_query_direct(sqlite3 *handle, const char *query, uid_t uid)
1508 int ret = MS_MEDIA_ERR_NONE;
1509 char *zErrMsg = NULL;
1510 int retry_count = 0;
1513 ret = sqlite3_exec(handle, query, NULL, NULL, &zErrMsg);
1514 if (SQLITE_OK != ret) {
1515 media_svc_sec_error("Error[%s],Query[%s]", zErrMsg, query);
1516 SQLITE3_SAFE_FREE(zErrMsg);
1517 if (ret == SQLITE_BUSY) {
1518 ret = MS_MEDIA_ERR_DB_BUSY_FAIL;
1519 } else if (ret == SQLITE_CONSTRAINT) {
1520 ret = MS_MEDIA_ERR_DB_CONSTRAINT_FAIL;
1521 } else if (ret == SQLITE_FULL) {
1522 ret = MS_MEDIA_ERR_DB_FULL_FAIL;
1523 } else if (ret == SQLITE_LOCKED) {
1524 if (retry_count < MAX_RETRY) {
1525 media_svc_error("Locked retry[%d]", retry_count);
1530 ret = MS_MEDIA_ERR_DB_UPDATE_FAIL;
1532 ret = MS_MEDIA_ERR_DB_UPDATE_FAIL;
1539 int _media_svc_sql_query_list_direct(GList **query_list, uid_t uid)
1541 int ret = MS_MEDIA_ERR_NONE;
1543 int length = g_list_length(*query_list);
1545 sqlite3 *handle = NULL;
1547 media_svc_debug("query list length[%d]", length);
1551 ret = media_db_connect(&handle, uid, true);
1552 media_svc_retvm_if(ret != MS_MEDIA_ERR_NONE, ret, "DB connection failed");
1554 for (idx = 0; idx < length; idx++) {
1555 sql = (char *)g_list_nth_data(*query_list, idx);
1556 if (STRING_VALID(sql)) {
1557 ret = __media_svc_query_direct(handle, sql, uid);
1558 if (ret != MS_MEDIA_ERR_NONE)
1559 media_svc_debug("_media_svc_query_direct failed[%s]", sql);
1561 SQLITE3_SAFE_FREE(sql);
1565 media_db_disconnect(handle);
1568 _media_svc_sql_query_release(query_list);
1570 return MS_MEDIA_ERR_NONE;
1573 void _media_svc_sql_query_add(GList **query_list, char **query)
1575 *query_list = g_list_append(*query_list, *query);
1578 void _media_svc_sql_query_release(GList **query_list)
1581 g_list_free(*query_list);
1586 int _media_svc_check_db_upgrade(sqlite3 *db_handle, int user_version, uid_t uid)
1588 if (user_version < LATEST_VERSION_NUMBER) {
1589 media_svc_error("Current DB is out of date(%d).. So start to upgrade DB(%d)", user_version, LATEST_VERSION_NUMBER);
1590 return __media_svc_db_upgrade(db_handle, user_version, uid);
1592 return MS_MEDIA_ERR_NONE;
1596 int _media_svc_create_media_table_with_id(const char *table_id, uid_t uid)
1598 int ret = MS_MEDIA_ERR_NONE;
1600 ret = _media_svc_init_table_query(table_id);
1601 if (ret != MS_MEDIA_ERR_NONE) {
1602 media_svc_error("_media_svc_init_table_query failed");
1606 ret = _media_svc_make_table_query(table_id, MEDIA_SVC_DB_LIST_MEDIA, uid);
1607 if (ret != MS_MEDIA_ERR_NONE) {
1608 media_svc_error("_media_svc_make_table_query failed");
1612 /* Add for trigger */
1613 ret = _media_svc_make_table_query(MEDIA_SVC_DB_TABLE_FOLDER, MEDIA_SVC_DB_LIST_FOLDER, uid);
1614 if (ret != MS_MEDIA_ERR_NONE) {
1615 media_svc_error("_media_svc_make_table_query failed");
1619 ret = _media_svc_make_table_query(MEDIA_SVC_DB_TABLE_PLAYLIST_MAP, MEDIA_SVC_DB_LIST_PLAYLIST_MAP, uid);
1620 if (ret != MS_MEDIA_ERR_NONE) {
1621 media_svc_error("_media_svc_make_table_query failed");
1625 ret = _media_svc_make_table_query(MEDIA_SVC_DB_TABLE_ALBUM, MEDIA_SVC_DB_LIST_ALBUM, uid);
1626 if (ret != MS_MEDIA_ERR_NONE) {
1627 media_svc_error("_media_svc_make_table_query failed");
1631 ret = _media_svc_make_table_query(MEDIA_SVC_DB_TABLE_TAG_MAP, MEDIA_SVC_DB_LIST_TAG_MAP, uid);
1632 if (ret != MS_MEDIA_ERR_NONE) {
1633 media_svc_error("_media_svc_make_table_query failed");
1637 ret = _media_svc_make_table_query(MEDIA_SVC_DB_TABLE_BOOKMARK, MEDIA_SVC_DB_LIST_BOOKMARK, uid);
1638 if (ret != MS_MEDIA_ERR_NONE) {
1639 media_svc_error("_media_svc_make_table_query failed");
1643 _media_svc_destroy_table_query();
1648 _media_svc_destroy_table_query();
1653 int _media_svc_update_media_view(sqlite3 *db_handle, uid_t uid)
1655 int ret = MS_MEDIA_ERR_NONE;
1657 sqlite3_stmt *sql_stmt = NULL;
1660 GList *storage_list = NULL;
1661 char view_query[MEDIA_SVC_QUERY_LEN_NORMAL] = {0, };
1662 memset(view_query, 0x00, sizeof(view_query));
1664 snprintf(view_query, sizeof(view_query), "DROP VIEW IF EXISTS %s; CREATE VIEW IF NOT EXISTS %s AS SELECT * from %s ", MEDIA_SVC_DB_VIEW_MEDIA, MEDIA_SVC_DB_VIEW_MEDIA, MEDIA_SVC_DB_TABLE_MEDIA);
1666 /*Select list of storage*/
1667 sql = sqlite3_mprintf("SELECT storage_id FROM '%s' WHERE validity=1", MEDIA_SVC_DB_TABLE_STORAGE);
1668 ret = _media_svc_sql_prepare_to_step_simple(db_handle, sql, &sql_stmt);
1669 media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
1671 while (sqlite3_step(sql_stmt) == SQLITE_ROW) {
1672 if (STRING_VALID((const char *)sqlite3_column_text(sql_stmt, 0)))
1673 storage_list = g_list_append(storage_list, strdup((char *)sqlite3_column_text(sql_stmt, 0)));
1675 SQLITE3_FINALIZE(sql_stmt);
1677 if ((storage_list != NULL) && (g_list_length(storage_list) > 0)) {
1678 item_cnt = g_list_length(storage_list);
1680 for (idx = 0; idx < item_cnt; idx++) {
1682 char *storage_id = NULL;
1683 storage_id = g_list_nth_data(storage_list, idx);
1685 if (STRING_VALID(storage_id)) {
1686 /*Select list of storage*/
1687 sql = sqlite3_mprintf("SELECT COUNT(*) FROM SQLITE_MASTER WHERE type='table' and name='%q'", storage_id);
1688 ret = _media_svc_sql_prepare_to_step(db_handle, sql, &sql_stmt);
1689 if (ret != MS_MEDIA_ERR_NONE) {
1690 SAFE_FREE(storage_id);
1694 table_cnt = sqlite3_column_int(sql_stmt, 0);
1695 SQLITE3_FINALIZE(sql_stmt);
1697 if (table_cnt > 0) {
1698 char append_query[128] = {0, };
1699 memset(append_query, 0x00, sizeof(append_query));
1700 snprintf(append_query, sizeof(append_query), " UNION SELECT * from '%s'", storage_id);
1701 SAFE_STRLCAT(view_query, append_query, sizeof(view_query));
1703 media_svc_error("media table not exist for storage [%s]", storage_id);
1706 SAFE_FREE(storage_id);
1709 g_list_free(storage_list);
1712 ret = _media_svc_sql_query(view_query, uid);
1713 media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);