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-svc-media.h"
24 #include "media-svc-media-folder.h"
25 #include "media-svc-error.h"
26 #include "media-svc-debug.h"
27 #include "media-svc-util.h"
28 #include "media-svc-db-utils.h"
31 char thumbnail_path[MEDIA_SVC_PATHNAME_SIZE];
32 }media_svc_thumbnailpath_s;
34 static __thread GList *g_media_svc_item_validity_query_list = NULL;
35 static __thread GList *g_media_svc_insert_item_query_list = NULL;
36 __thread GList *g_media_svc_move_item_query_list = NULL;
38 static int __media_svc_count_invalid_records_with_thumbnail(sqlite3 *handle, media_svc_storage_type_e storage_type, int *count);
39 static int __media_svc_get_invalid_records_with_thumbnail(sqlite3 *handle, media_svc_storage_type_e storage_type,
40 int count, media_svc_thumbnailpath_s * thumb_path);
41 static int __media_svc_count_invalid_folder_records_with_thumbnail(sqlite3 *handle, const char *folder_path, int *count);
42 static int __media_svc_get_invalid_folder_records_with_thumbnail(sqlite3 *handle, const char *folder_path,
43 int count, media_svc_thumbnailpath_s * thumb_path);
45 static int __media_svc_count_invalid_records_with_thumbnail(sqlite3 *handle, media_svc_storage_type_e storage_type, int *count)
47 int ret = MEDIA_INFO_ERROR_NONE;
48 sqlite3_stmt *sql_stmt = NULL;
49 char *sql = sqlite3_mprintf("SELECT count(*) FROM %s WHERE validity=0 AND storage_type=%d AND thumbnail_path IS NOT NULL",
50 MEDIA_SVC_DB_TABLE_MEDIA, storage_type);
52 ret = _media_svc_sql_prepare_to_step(handle, sql, &sql_stmt);
54 if (ret != MEDIA_INFO_ERROR_NONE) {
55 media_svc_error("error when __media_svc_count_invalid_records_with_thumbnail. err = [%d]", ret);
59 *count = sqlite3_column_int(sql_stmt, 0);
61 SQLITE3_FINALIZE(sql_stmt);
63 return MEDIA_INFO_ERROR_NONE;
67 static int __media_svc_get_invalid_records_with_thumbnail(sqlite3 *handle, media_svc_storage_type_e storage_type,
68 int count, media_svc_thumbnailpath_s * thumb_path)
72 sqlite3_stmt *sql_stmt = NULL;
74 char *sql = sqlite3_mprintf("select thumbnail_path from %s WHERE validity=0 AND storage_type=%d AND thumbnail_path IS NOT NULL",
75 MEDIA_SVC_DB_TABLE_MEDIA, storage_type);
77 media_svc_debug("[SQL query] : %s", sql);
79 err = sqlite3_prepare_v2(handle, sql, -1, &sql_stmt, NULL);
81 if (err != SQLITE_OK) {
82 media_svc_error("prepare error [%s]", sqlite3_errmsg(handle));
83 return MEDIA_INFO_ERROR_DATABASE_INTERNAL;
86 while (sqlite3_step(sql_stmt) == SQLITE_ROW) {
87 _strncpy_safe(thumb_path[idx].thumbnail_path, (const char *)sqlite3_column_text(sql_stmt, 0), sizeof(thumb_path[idx]));
88 //media_svc_debug("thumb_path[%d]=[%s]", idx, thumb_path[idx].thumbnail_path);
92 SQLITE3_FINALIZE(sql_stmt);
94 return MEDIA_INFO_ERROR_NONE;
97 static int __media_svc_count_invalid_folder_records_with_thumbnail(sqlite3 *handle, const char *folder_path, int *count)
99 int ret = MEDIA_INFO_ERROR_NONE;
100 sqlite3_stmt *sql_stmt = NULL;
101 char *sql = sqlite3_mprintf("SELECT count(*) FROM %s WHERE validity=0 AND path LIKE '%q/%%' AND thumbnail_path IS NOT NULL",
102 MEDIA_SVC_DB_TABLE_MEDIA, folder_path);
104 ret = _media_svc_sql_prepare_to_step(handle, sql, &sql_stmt);
106 if (ret != MEDIA_INFO_ERROR_NONE) {
107 media_svc_error("error when __media_svc_count_invalid_folder_records_with_thumbnail. err = [%d]", ret);
111 *count = sqlite3_column_int(sql_stmt, 0);
113 SQLITE3_FINALIZE(sql_stmt);
115 return MEDIA_INFO_ERROR_NONE;
119 static int __media_svc_get_invalid_folder_records_with_thumbnail(sqlite3 *handle, const char *folder_path,
120 int count, media_svc_thumbnailpath_s * thumb_path)
124 sqlite3_stmt *sql_stmt = NULL;
126 char *sql = sqlite3_mprintf("select thumbnail_path from %s WHERE validity=0 AND path LIKE '%q/%%' AND thumbnail_path IS NOT NULL",
127 MEDIA_SVC_DB_TABLE_MEDIA, folder_path);
129 media_svc_debug("[SQL query] : %s", sql);
131 err = sqlite3_prepare_v2(handle, sql, -1, &sql_stmt, NULL);
133 if (err != SQLITE_OK) {
134 media_svc_error("prepare error [%s]", sqlite3_errmsg(handle));
135 return MEDIA_INFO_ERROR_DATABASE_INTERNAL;
138 while (sqlite3_step(sql_stmt) == SQLITE_ROW) {
139 _strncpy_safe(thumb_path[idx].thumbnail_path, (const char *)sqlite3_column_text(sql_stmt, 0), sizeof(thumb_path[idx]));
143 SQLITE3_FINALIZE(sql_stmt);
145 return MEDIA_INFO_ERROR_NONE;
148 int _media_svc_count_record_with_path(sqlite3 *handle, const char *path, int *count)
150 int ret = MEDIA_INFO_ERROR_NONE;
151 sqlite3_stmt *sql_stmt = NULL;
153 char *sql = sqlite3_mprintf("SELECT count(*) FROM %s WHERE path='%q'", MEDIA_SVC_DB_TABLE_MEDIA, path);
155 ret = _media_svc_sql_prepare_to_step(handle, sql, &sql_stmt);
157 media_svc_retv_if(ret != MEDIA_INFO_ERROR_NONE, ret);
159 *count = sqlite3_column_int(sql_stmt, 0);
161 SQLITE3_FINALIZE(sql_stmt);
163 return MEDIA_INFO_ERROR_NONE;
166 int _media_svc_insert_item_with_data(sqlite3 *handle, media_svc_content_info_s *content_info, bool stack_query)
171 char * db_fields = "media_uuid, path, file_name, media_type, mime_type, size, added_time, modified_time, folder_uuid, \
172 thumbnail_path, title, album_id, album, artist, genre, composer, year, recorded_date, copyright, track_num, description,\
173 bitrate, samplerate, channel, duration, longitude, latitude, altitude, width, height, datetaken, orientation,\
174 rating, is_drm, storage_type";
176 /* This sql is due to sqlite3_mprintf's wrong operation when using floating point in the text format */
177 /* This code will be removed when sqlite3_mprintf works clearly */
178 char *test_sql = sqlite3_mprintf("%f, %f, %f", content_info->media_meta.longitude, content_info->media_meta.latitude, content_info->media_meta.altitude);
179 sqlite3_free(test_sql);
181 char *sql = sqlite3_mprintf("INSERT INTO %s (%s) VALUES (%Q, %Q, %Q, %d, %Q, %lld, %d, %d, %Q, \
182 %Q, %Q, %d, %Q, %Q, %Q, %Q, %Q, %Q, %Q, %Q, %Q, \
183 %d, %d, %d, %d, %.2f, %.2f, %.2f, %d, %d, %Q, %d, \
185 MEDIA_SVC_DB_TABLE_MEDIA, db_fields,
186 content_info->media_uuid,
188 content_info->file_name,
189 content_info->media_type,
190 content_info->mime_type,
192 content_info->added_time,
193 content_info->modified_time,
194 content_info->folder_uuid,
195 content_info->thumbnail_path, //
196 content_info->media_meta.title,
197 content_info->album_id,
198 content_info->media_meta.album,
199 content_info->media_meta.artist,
200 content_info->media_meta.genre,
201 content_info->media_meta.composer,
202 content_info->media_meta.year,
203 content_info->media_meta.recorded_date,
204 content_info->media_meta.copyright,
205 content_info->media_meta.track_num,
206 content_info->media_meta.description, //
207 content_info->media_meta.bitrate,
208 content_info->media_meta.samplerate,
209 content_info->media_meta.channel,
210 content_info->media_meta.duration,
211 content_info->media_meta.longitude,
212 content_info->media_meta.latitude,
213 content_info->media_meta.altitude,
214 content_info->media_meta.width,
215 content_info->media_meta.height,
216 content_info->media_meta.datetaken,
217 content_info->media_meta.orientation,
218 content_info->media_meta.rating,
219 content_info->is_drm,
220 content_info->storage_type);
223 err = _media_svc_sql_query(handle, sql);
225 if (err != SQLITE_OK) {
226 media_svc_error("failed to insert item");
228 return MEDIA_INFO_ERROR_DATABASE_INTERNAL;
231 media_svc_debug("query : %s", sql);
232 _media_svc_sql_query_add(&g_media_svc_insert_item_query_list, &sql);
235 return MEDIA_INFO_ERROR_NONE;
238 int _media_svc_update_item_with_data(sqlite3 *handle, media_svc_content_info_s *content_info)
242 /* This sql is due to sqlite3_mprintf's wrong operation when using floating point in the text format */
243 /* This code will be removed when sqlite3_mprintf works clearly */
244 char *test_sql = sqlite3_mprintf("%f, %f, %f", content_info->media_meta.longitude, content_info->media_meta.latitude, content_info->media_meta.altitude);
245 sqlite3_free(test_sql);
247 char *sql = sqlite3_mprintf("UPDATE %s SET \
248 size=%lld, modified_time=%d, thumbnail_path=%Q, title=%Q, album_id=%d, album=%Q, artist=%Q, genre=%Q, \
249 composer=%Q, year=%Q, recorded_date=%Q, copyright=%Q, track_num=%Q, description=%Q, \
250 bitrate=%d, samplerate=%d, channel=%d, duration=%d, longitude=%f, latitude=%f, altitude=%f, width=%d, height=%d, datetaken=%Q, \
251 orientation=%d WHERE path=%Q",
252 MEDIA_SVC_DB_TABLE_MEDIA,
254 content_info->modified_time,
255 content_info->thumbnail_path,
256 content_info->media_meta.title,
257 content_info->album_id,
258 content_info->media_meta.album,
259 content_info->media_meta.artist,
260 content_info->media_meta.genre,
261 content_info->media_meta.composer,
262 content_info->media_meta.year,
263 content_info->media_meta.recorded_date,
264 content_info->media_meta.copyright,
265 content_info->media_meta.track_num,
266 content_info->media_meta.description,
267 content_info->media_meta.bitrate,
268 content_info->media_meta.samplerate,
269 content_info->media_meta.channel,
270 content_info->media_meta.duration,
271 content_info->media_meta.longitude,
272 content_info->media_meta.latitude,
273 content_info->media_meta.altitude,
274 content_info->media_meta.width,
275 content_info->media_meta.height,
276 content_info->media_meta.datetaken,
277 content_info->media_meta.orientation,
281 err = _media_svc_sql_query(handle, sql);
283 if (err != SQLITE_OK) {
284 media_svc_error("failed to update item");
286 return MEDIA_INFO_ERROR_DATABASE_INTERNAL;
289 return MEDIA_INFO_ERROR_NONE;
291 int _media_svc_get_thumbnail_path_by_path(sqlite3 *handle, const char *path, char *thumbnail_path)
293 int ret = MEDIA_INFO_ERROR_NONE;
294 sqlite3_stmt *sql_stmt = NULL;
296 char *sql = sqlite3_mprintf("SELECT thumbnail_path FROM %s WHERE path='%q'", MEDIA_SVC_DB_TABLE_MEDIA, path);
298 ret = _media_svc_sql_prepare_to_step(handle, sql, &sql_stmt);
300 if (ret != MEDIA_INFO_ERROR_NONE) {
301 if(ret == MEDIA_INFO_ERROR_DATABASE_NO_RECORD) {
302 media_svc_debug("there is no thumbnail.");
305 media_svc_error("error when _media_svc_get_thumbnail_path_by_path. err = [%d]", ret);
310 _strncpy_safe(thumbnail_path, (const char *)sqlite3_column_text(sql_stmt, 0), MEDIA_SVC_PATHNAME_SIZE);
312 SQLITE3_FINALIZE(sql_stmt);
314 return MEDIA_INFO_ERROR_NONE;
317 int _media_svc_get_media_type_by_path(sqlite3 *handle, const char *path, int *media_type)
319 int ret = MEDIA_INFO_ERROR_NONE;
320 sqlite3_stmt *sql_stmt = NULL;
322 char *sql = sqlite3_mprintf("SELECT media_type FROM %s WHERE path='%q'", MEDIA_SVC_DB_TABLE_MEDIA, path);
324 ret = _media_svc_sql_prepare_to_step(handle, sql, &sql_stmt);
326 if (ret != MEDIA_INFO_ERROR_NONE) {
327 media_svc_error("error when _media_svc_get_media_type_by_path. err = [%d]", ret);
331 *media_type = sqlite3_column_int(sql_stmt, 0);
333 SQLITE3_FINALIZE(sql_stmt);
335 return MEDIA_INFO_ERROR_NONE;
338 int _media_svc_delete_item_by_path(sqlite3 *handle, const char *path)
341 char *sql = sqlite3_mprintf("DELETE FROM %s WHERE validity=1 AND path='%q'", MEDIA_SVC_DB_TABLE_MEDIA, path);
343 err = _media_svc_sql_query(handle, sql);
345 if (err != SQLITE_OK) {
346 media_svc_error("It failed to delete item (%d)", err);
347 return MEDIA_INFO_ERROR_DATABASE_INTERNAL;
350 return MEDIA_INFO_ERROR_NONE;
353 int _media_svc_truncate_table(sqlite3 *handle, media_svc_storage_type_e storage_type)
356 char *sql = sqlite3_mprintf("DELETE FROM %s WHERE storage_type=%d", MEDIA_SVC_DB_TABLE_MEDIA, storage_type);
358 err = _media_svc_sql_query(handle, sql);
360 if (err != SQLITE_OK) {
361 media_svc_error("It failed to truncate table (%d)", err);
362 return MEDIA_INFO_ERROR_DATABASE_INTERNAL;
365 return MEDIA_INFO_ERROR_NONE;
369 int _media_svc_delete_invalid_items(sqlite3 *handle, media_svc_storage_type_e storage_type)
372 media_svc_thumbnailpath_s *thumbpath_record = NULL;
374 int invalid_count = 0;
375 int ret = MEDIA_INFO_ERROR_NONE;
377 ret = __media_svc_count_invalid_records_with_thumbnail(handle, storage_type, &invalid_count);
378 media_svc_retv_if(ret != MEDIA_INFO_ERROR_NONE, ret);
380 media_svc_debug("invalid count: %d\n", invalid_count);
382 if (invalid_count > 0) {
383 thumbpath_record = (media_svc_thumbnailpath_s *)calloc( invalid_count, sizeof(media_svc_thumbnailpath_s));
384 if (thumbpath_record == NULL) {
385 media_svc_error("fail to memory allocation");
386 return MEDIA_INFO_ERROR_OUT_OF_MEMORY;
389 ret = __media_svc_get_invalid_records_with_thumbnail(handle, storage_type, invalid_count, thumbpath_record);
390 if (ret != MEDIA_INFO_ERROR_NONE) {
391 media_svc_error("error when get thumbnail record");
392 SAFE_FREE(thumbpath_record);
396 media_svc_debug("There is no item with thumbnail");
399 char *sql = sqlite3_mprintf("DELETE FROM %s WHERE validity = 0 AND storage_type=%d", MEDIA_SVC_DB_TABLE_MEDIA, storage_type);
400 err = _media_svc_sql_query(handle, sql);
402 if (err != SQLITE_OK) {
403 media_svc_error("To delete invalid items is failed(%d)", err);
404 SAFE_FREE(thumbpath_record);
405 return MEDIA_INFO_ERROR_DATABASE_INTERNAL;
408 /*Delete thumbnails*/
409 for (idx = 0; idx < invalid_count; idx++) {
410 if (strlen(thumbpath_record[idx].thumbnail_path) > 0) {
411 if (_media_svc_remove_file(thumbpath_record[idx].thumbnail_path) == FALSE) {
412 media_svc_error("fail to remove thumbnail file.");
413 //SAFE_FREE(thumbpath_record);
414 //return MEDIA_INFO_ERROR_INTERNAL;
419 SAFE_FREE(thumbpath_record);
421 return MEDIA_INFO_ERROR_NONE;
424 int _media_svc_delete_invalid_folder_items(sqlite3 *handle, const char *folder_path)
427 media_svc_thumbnailpath_s *thumbpath_record = NULL;
429 int invalid_count = 0;
430 int ret = MEDIA_INFO_ERROR_NONE;
432 ret = __media_svc_count_invalid_folder_records_with_thumbnail(handle, folder_path, &invalid_count);
433 media_svc_retv_if(ret != MEDIA_INFO_ERROR_NONE, ret);
435 media_svc_debug("invalid count: %d\n", invalid_count);
437 if (invalid_count > 0) {
438 thumbpath_record = (media_svc_thumbnailpath_s *)calloc( invalid_count, sizeof(media_svc_thumbnailpath_s));
439 if (thumbpath_record == NULL) {
440 media_svc_error("fail to memory allocation");
441 return MEDIA_INFO_ERROR_OUT_OF_MEMORY;
444 ret = __media_svc_get_invalid_folder_records_with_thumbnail(handle, folder_path, invalid_count, thumbpath_record);
445 if (ret != MEDIA_INFO_ERROR_NONE) {
446 media_svc_error("error when get thumbnail record");
447 SAFE_FREE(thumbpath_record);
451 media_svc_debug("There is no item with thumbnail");
454 char *sql = sqlite3_mprintf("DELETE FROM %s WHERE validity = 0 AND path LIKE '%q/%%'", MEDIA_SVC_DB_TABLE_MEDIA, folder_path);
455 err = _media_svc_sql_query(handle, sql);
457 if (err != SQLITE_OK) {
458 media_svc_error("To delete invalid items is failed(%d)", err);
459 SAFE_FREE(thumbpath_record);
460 return MEDIA_INFO_ERROR_DATABASE_INTERNAL;
463 /*Delete thumbnails*/
464 for (idx = 0; idx < invalid_count; idx++) {
465 if (strlen(thumbpath_record[idx].thumbnail_path) > 0) {
466 if (_media_svc_remove_file(thumbpath_record[idx].thumbnail_path) == FALSE) {
467 media_svc_error("fail to remove thumbnail file [%s].", thumbpath_record[idx].thumbnail_path);
468 //SAFE_FREE(thumbpath_record);
469 //return MEDIA_INFO_ERROR_INTERNAL;
474 SAFE_FREE(thumbpath_record);
476 return MEDIA_INFO_ERROR_NONE;
480 int _media_svc_update_item_validity(sqlite3 *handle, const char *path, int validity, bool stack_query)
484 char *sql = sqlite3_mprintf("UPDATE %s SET validity=%d WHERE path= '%q'", MEDIA_SVC_DB_TABLE_MEDIA, validity, path);
487 err = _media_svc_sql_query(handle, sql);
489 if (err != SQLITE_OK) {
490 media_svc_error("To update item as valid is failed(%d)", err);
491 return MEDIA_INFO_ERROR_DATABASE_INTERNAL;
494 _media_svc_sql_query_add(&g_media_svc_item_validity_query_list, &sql);
497 return MEDIA_INFO_ERROR_NONE;
500 int _media_svc_update_thumbnail_path(sqlite3 *handle, const char *path, const char *thumb_path)
504 char *sql = sqlite3_mprintf("UPDATE %s SET thumbnail_path=%Q WHERE path= %Q", MEDIA_SVC_DB_TABLE_MEDIA, thumb_path, path);
506 err = _media_svc_sql_query(handle, sql);
508 if (err != SQLITE_OK) {
509 media_svc_error("To update thumb path failed(%d)", err);
510 return MEDIA_INFO_ERROR_DATABASE_INTERNAL;
513 return MEDIA_INFO_ERROR_NONE;
516 int _media_svc_update_storage_item_validity(sqlite3 *handle, media_svc_storage_type_e storage_type, int validity)
519 char *sql = sqlite3_mprintf("UPDATE %s SET validity=%d WHERE storage_type=%d", MEDIA_SVC_DB_TABLE_MEDIA, validity, storage_type);
520 err = _media_svc_sql_query(handle, sql);
522 if (err != SQLITE_OK) {
523 media_svc_error("To update item as valid is failed(%d)", err);
524 return MEDIA_INFO_ERROR_DATABASE_INTERNAL;
527 return MEDIA_INFO_ERROR_NONE;
530 int _media_svc_update_folder_item_validity(sqlite3 *handle, const char *folder_path, int validity)
533 int ret = MEDIA_INFO_ERROR_NONE;
535 char folder_uuid[MEDIA_SVC_UUID_SIZE + 1] = {0, };
536 sqlite3_stmt *sql_stmt = NULL;
539 sql = sqlite3_mprintf("SELECT folder_uuid FROM %s WHERE path='%q'", MEDIA_SVC_DB_TABLE_FOLDER, folder_path);
540 ret = _media_svc_sql_prepare_to_step(handle, sql, &sql_stmt);
541 if (ret != MEDIA_INFO_ERROR_NONE) {
542 media_svc_error("error when get folder_id. err = [%d]", ret);
546 _strncpy_safe(folder_uuid, (const char *)sqlite3_column_text(sql_stmt, 0), MEDIA_SVC_UUID_SIZE+1);
547 SQLITE3_FINALIZE(sql_stmt);
549 /*Update folder item validity*/
550 sql = sqlite3_mprintf("UPDATE %s SET validity=%d WHERE folder_uuid='%q'", MEDIA_SVC_DB_TABLE_MEDIA, validity, folder_uuid);
551 err = _media_svc_sql_query(handle, sql);
553 if (err != SQLITE_OK) {
554 media_svc_error("To update folder item as valid is failed(%d)", err);
555 return MEDIA_INFO_ERROR_DATABASE_INTERNAL;
558 return MEDIA_INFO_ERROR_NONE;
561 int _media_svc_update_recursive_folder_item_validity(sqlite3 *handle, const char *folder_path, int validity)
565 /*Update folder item validity*/
566 char *sql = sqlite3_mprintf("UPDATE %s SET validity=%d WHERE path LIKE '%q/%%'", MEDIA_SVC_DB_TABLE_MEDIA, validity, folder_path);
567 err = _media_svc_sql_query(handle, sql);
569 if (err != SQLITE_OK) {
570 media_svc_error("To update recursive folder item validity is failed(%d)", err);
571 return MEDIA_INFO_ERROR_DATABASE_INTERNAL;
574 return MEDIA_INFO_ERROR_NONE;
577 int _media_svc_update_item_by_path(sqlite3 *handle, const char *src_path, media_svc_storage_type_e dest_storage, const char *dest_path,
578 const char *file_name, int modified_time, const char *folder_uuid, const char *thumb_path, bool stack_query)
580 /* update path, filename, modified_time, folder_uuid, thumbnail_path, */
581 /* played_count, last_played_time, last_played_position, favourite, storaget_type*/
586 if(thumb_path != NULL) {
587 sql = sqlite3_mprintf("UPDATE %s SET \
588 path=%Q, file_name=%Q, modified_time=%d, folder_uuid=%Q, thumbnail_path=%Q, storage_type=%d, \
589 played_count=0, last_played_time=0, last_played_position=0 \
591 MEDIA_SVC_DB_TABLE_MEDIA, dest_path, file_name, modified_time, folder_uuid, thumb_path, dest_storage, src_path);
593 sql = sqlite3_mprintf("UPDATE %s SET \
594 path=%Q, file_name=%Q, modified_time=%d, folder_uuid=%Q, storage_type=%d, \
595 played_count=0, last_played_time=0, last_played_position=0 \
597 MEDIA_SVC_DB_TABLE_MEDIA, dest_path, file_name, modified_time, folder_uuid, dest_storage, src_path);
601 err = _media_svc_sql_query(handle, sql);
603 if (err != SQLITE_OK) {
604 media_svc_error("It failed to update metadata (%d)", err);
605 return MEDIA_INFO_ERROR_DATABASE_INTERNAL;
608 _media_svc_sql_query_add(&g_media_svc_move_item_query_list, &sql);
611 return MEDIA_INFO_ERROR_NONE;
614 int _media_svc_list_query_do(sqlite3 *handle, media_svc_query_type_e query_type)
616 int ret = MEDIA_INFO_ERROR_NONE;
618 ret = _media_svc_sql_begin_trans(handle);
619 media_svc_retv_if(ret != MEDIA_INFO_ERROR_NONE, ret);
621 if (query_type == MEDIA_SVC_QUERY_SET_ITEM_VALIDITY)
622 ret = _media_svc_sql_query_list(handle, &g_media_svc_item_validity_query_list);
623 else if (query_type == MEDIA_SVC_QUERY_MOVE_ITEM)
624 ret = _media_svc_sql_query_list(handle, &g_media_svc_move_item_query_list);
625 else if (query_type == MEDIA_SVC_QUERY_INSERT_ITEM)
626 ret = _media_svc_sql_query_list(handle, &g_media_svc_insert_item_query_list);
628 ret = MEDIA_INFO_ERROR_INVALID_PARAMETER;
630 if (ret != MEDIA_INFO_ERROR_NONE) {
631 media_svc_error("_media_svc_list_query_do failed. start rollback");
632 _media_svc_sql_rollback_trans(handle);
636 ret = _media_svc_sql_end_trans(handle);
637 if (ret != MEDIA_INFO_ERROR_NONE) {
638 media_svc_error("mb_svc_sqlite3_commit_trans failed.. Now start to rollback\n");
639 _media_svc_sql_rollback_trans(handle);
643 return MEDIA_INFO_ERROR_NONE;
646 int _media_svc_get_media_id_by_path(sqlite3 *handle, const char *path, char *media_uuid, int max_length)
648 int ret = MEDIA_INFO_ERROR_NONE;
649 sqlite3_stmt *sql_stmt = NULL;
650 char *sql = sqlite3_mprintf("SELECT media_uuid FROM %s WHERE validity=1 AND path='%q'",
651 MEDIA_SVC_DB_TABLE_MEDIA, path);
653 ret = _media_svc_sql_prepare_to_step(handle, sql, &sql_stmt);
655 if (ret != MEDIA_INFO_ERROR_NONE) {
656 media_svc_error("error when __media_svc_count_invalid_records_with_thumbnail. err = [%d]", ret);
660 strncpy(media_uuid, (const char*)sqlite3_column_text(sql_stmt, 0), max_length);
661 media_uuid[max_length - 1] = '\0';
663 SQLITE3_FINALIZE(sql_stmt);
665 return MEDIA_INFO_ERROR_NONE;