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"
29 #include "media-svc-noti.h"
32 char thumbnail_path[MEDIA_SVC_PATHNAME_SIZE];
33 }media_svc_thumbnailpath_s;
35 static __thread GList *g_media_svc_item_validity_query_list = NULL;
36 static __thread GList *g_media_svc_insert_item_query_list = NULL;
37 __thread GList *g_media_svc_move_item_query_list = NULL;
39 static int __media_svc_count_invalid_records_with_thumbnail(sqlite3 *handle, media_svc_storage_type_e storage_type, int *count);
40 static int __media_svc_get_invalid_records_with_thumbnail(sqlite3 *handle, media_svc_storage_type_e storage_type,
41 int count, media_svc_thumbnailpath_s * thumb_path);
42 static int __media_svc_count_invalid_folder_records_with_thumbnail(sqlite3 *handle, const char *folder_path, int *count);
43 static int __media_svc_get_invalid_folder_records_with_thumbnail(sqlite3 *handle, const char *folder_path,
44 int count, media_svc_thumbnailpath_s * thumb_path);
46 static int __media_svc_count_invalid_records_with_thumbnail(sqlite3 *handle, media_svc_storage_type_e storage_type, int *count)
48 int ret = MEDIA_INFO_ERROR_NONE;
49 sqlite3_stmt *sql_stmt = NULL;
50 char *sql = sqlite3_mprintf("SELECT count(*) FROM %s WHERE validity=0 AND storage_type=%d AND thumbnail_path IS NOT NULL",
51 MEDIA_SVC_DB_TABLE_MEDIA, storage_type);
53 ret = _media_svc_sql_prepare_to_step(handle, sql, &sql_stmt);
55 if (ret != MEDIA_INFO_ERROR_NONE) {
56 media_svc_error("error when __media_svc_count_invalid_records_with_thumbnail. err = [%d]", ret);
60 *count = sqlite3_column_int(sql_stmt, 0);
62 SQLITE3_FINALIZE(sql_stmt);
64 return MEDIA_INFO_ERROR_NONE;
68 static int __media_svc_get_invalid_records_with_thumbnail(sqlite3 *handle, media_svc_storage_type_e storage_type,
69 int count, media_svc_thumbnailpath_s * thumb_path)
73 sqlite3_stmt *sql_stmt = NULL;
75 char *sql = sqlite3_mprintf("select thumbnail_path from %s WHERE validity=0 AND storage_type=%d AND thumbnail_path IS NOT NULL",
76 MEDIA_SVC_DB_TABLE_MEDIA, storage_type);
78 media_svc_debug("[SQL query] : %s", sql);
80 err = sqlite3_prepare_v2(handle, sql, -1, &sql_stmt, NULL);
82 if (err != SQLITE_OK) {
83 media_svc_error("prepare error [%s]", sqlite3_errmsg(handle));
84 return MEDIA_INFO_ERROR_DATABASE_INTERNAL;
87 while (sqlite3_step(sql_stmt) == SQLITE_ROW) {
88 _strncpy_safe(thumb_path[idx].thumbnail_path, (const char *)sqlite3_column_text(sql_stmt, 0), sizeof(thumb_path[idx]));
89 //media_svc_debug("thumb_path[%d]=[%s]", idx, thumb_path[idx].thumbnail_path);
93 SQLITE3_FINALIZE(sql_stmt);
95 return MEDIA_INFO_ERROR_NONE;
98 static int __media_svc_count_invalid_folder_records_with_thumbnail(sqlite3 *handle, const char *folder_path, int *count)
100 int ret = MEDIA_INFO_ERROR_NONE;
101 sqlite3_stmt *sql_stmt = NULL;
102 char *sql = sqlite3_mprintf("SELECT count(*) FROM %s WHERE validity=0 AND path LIKE '%q/%%' AND thumbnail_path IS NOT NULL",
103 MEDIA_SVC_DB_TABLE_MEDIA, folder_path);
105 ret = _media_svc_sql_prepare_to_step(handle, sql, &sql_stmt);
107 if (ret != MEDIA_INFO_ERROR_NONE) {
108 media_svc_error("error when __media_svc_count_invalid_folder_records_with_thumbnail. err = [%d]", ret);
112 *count = sqlite3_column_int(sql_stmt, 0);
114 SQLITE3_FINALIZE(sql_stmt);
116 return MEDIA_INFO_ERROR_NONE;
120 static int __media_svc_get_invalid_folder_records_with_thumbnail(sqlite3 *handle, const char *folder_path,
121 int count, media_svc_thumbnailpath_s * thumb_path)
125 sqlite3_stmt *sql_stmt = NULL;
127 char *sql = sqlite3_mprintf("select thumbnail_path from %s WHERE validity=0 AND path LIKE '%q/%%' AND thumbnail_path IS NOT NULL",
128 MEDIA_SVC_DB_TABLE_MEDIA, folder_path);
130 media_svc_debug("[SQL query] : %s", sql);
132 err = sqlite3_prepare_v2(handle, sql, -1, &sql_stmt, NULL);
134 if (err != SQLITE_OK) {
135 media_svc_error("prepare error [%s]", sqlite3_errmsg(handle));
136 return MEDIA_INFO_ERROR_DATABASE_INTERNAL;
139 while (sqlite3_step(sql_stmt) == SQLITE_ROW) {
140 _strncpy_safe(thumb_path[idx].thumbnail_path, (const char *)sqlite3_column_text(sql_stmt, 0), sizeof(thumb_path[idx]));
144 SQLITE3_FINALIZE(sql_stmt);
146 return MEDIA_INFO_ERROR_NONE;
149 int _media_svc_count_record_with_path(sqlite3 *handle, const char *path, int *count)
151 int ret = MEDIA_INFO_ERROR_NONE;
152 sqlite3_stmt *sql_stmt = NULL;
154 char *sql = sqlite3_mprintf("SELECT count(*) FROM %s WHERE path='%q'", MEDIA_SVC_DB_TABLE_MEDIA, path);
156 ret = _media_svc_sql_prepare_to_step(handle, sql, &sql_stmt);
158 media_svc_retv_if(ret != MEDIA_INFO_ERROR_NONE, ret);
160 *count = sqlite3_column_int(sql_stmt, 0);
162 SQLITE3_FINALIZE(sql_stmt);
164 return MEDIA_INFO_ERROR_NONE;
167 int _media_svc_insert_item_with_data(sqlite3 *handle, media_svc_content_info_s *content_info, int is_burst, bool stack_query)
171 char *burst_id = NULL;
173 char * db_fields = "media_uuid, path, file_name, media_type, mime_type, size, added_time, modified_time, folder_uuid, \
174 thumbnail_path, title, album_id, album, artist, genre, composer, year, recorded_date, copyright, track_num, description,\
175 bitrate, samplerate, channel, duration, longitude, latitude, altitude, width, height, datetaken, orientation,\
176 rating, is_drm, storage_type, burst_id";
178 /* This sql is due to sqlite3_mprintf's wrong operation when using floating point in the text format */
179 /* This code will be removed when sqlite3_mprintf works clearly */
180 char *test_sql = sqlite3_mprintf("%f, %f, %f", content_info->media_meta.longitude, content_info->media_meta.latitude, content_info->media_meta.altitude);
181 sqlite3_free(test_sql);
184 int burst_id_int = 0;
185 err = _media_svc_get_burst_id(handle, &burst_id_int);
190 if (burst_id_int > 0) {
191 media_svc_debug("Burst id : %d", burst_id_int);
192 burst_id = sqlite3_mprintf("%d", burst_id_int);
196 char *sql = sqlite3_mprintf("INSERT INTO %s (%s) VALUES (%Q, %Q, %Q, %d, %Q, %lld, %d, %d, %Q, \
197 %Q, %Q, %d, %Q, %Q, %Q, %Q, %Q, %Q, %Q, %Q, %Q, \
198 %d, %d, %d, %d, %.2f, %.2f, %.2f, %d, %d, %Q, %d, \
200 MEDIA_SVC_DB_TABLE_MEDIA, db_fields,
201 content_info->media_uuid,
203 content_info->file_name,
204 content_info->media_type,
205 content_info->mime_type,
207 content_info->added_time,
208 content_info->modified_time,
209 content_info->folder_uuid,
210 content_info->thumbnail_path, //
211 content_info->media_meta.title,
212 content_info->album_id,
213 content_info->media_meta.album,
214 content_info->media_meta.artist,
215 content_info->media_meta.genre,
216 content_info->media_meta.composer,
217 content_info->media_meta.year,
218 content_info->media_meta.recorded_date,
219 content_info->media_meta.copyright,
220 content_info->media_meta.track_num,
221 content_info->media_meta.description, //
222 content_info->media_meta.bitrate,
223 content_info->media_meta.samplerate,
224 content_info->media_meta.channel,
225 content_info->media_meta.duration,
226 content_info->media_meta.longitude,
227 content_info->media_meta.latitude,
228 content_info->media_meta.altitude,
229 content_info->media_meta.width,
230 content_info->media_meta.height,
231 content_info->media_meta.datetaken,
232 content_info->media_meta.orientation,
233 content_info->media_meta.rating,
234 content_info->is_drm,
235 content_info->storage_type,
238 if (burst_id) sqlite3_free(burst_id);
242 err = _media_svc_sql_query(handle, sql);
244 if (err != SQLITE_OK) {
245 media_svc_error("failed to insert item");
247 return MEDIA_INFO_ERROR_DATABASE_INTERNAL;
250 media_svc_debug("query : %s", sql);
251 _media_svc_sql_query_add(&g_media_svc_insert_item_query_list, &sql);
254 return MEDIA_INFO_ERROR_NONE;
257 int _media_svc_update_item_with_data(sqlite3 *handle, media_svc_content_info_s *content_info)
261 /* This sql is due to sqlite3_mprintf's wrong operation when using floating point in the text format */
262 /* This code will be removed when sqlite3_mprintf works clearly */
263 char *test_sql = sqlite3_mprintf("%f, %f, %f", content_info->media_meta.longitude, content_info->media_meta.latitude, content_info->media_meta.altitude);
264 sqlite3_free(test_sql);
266 char *sql = sqlite3_mprintf("UPDATE %s SET \
267 size=%lld, modified_time=%d, thumbnail_path=%Q, title=%Q, album_id=%d, album=%Q, artist=%Q, genre=%Q, \
268 composer=%Q, year=%Q, recorded_date=%Q, copyright=%Q, track_num=%Q, description=%Q, \
269 bitrate=%d, samplerate=%d, channel=%d, duration=%d, longitude=%f, latitude=%f, altitude=%f, width=%d, height=%d, datetaken=%Q, \
270 orientation=%d WHERE path=%Q",
271 MEDIA_SVC_DB_TABLE_MEDIA,
273 content_info->modified_time,
274 content_info->thumbnail_path,
275 content_info->media_meta.title,
276 content_info->album_id,
277 content_info->media_meta.album,
278 content_info->media_meta.artist,
279 content_info->media_meta.genre,
280 content_info->media_meta.composer,
281 content_info->media_meta.year,
282 content_info->media_meta.recorded_date,
283 content_info->media_meta.copyright,
284 content_info->media_meta.track_num,
285 content_info->media_meta.description,
286 content_info->media_meta.bitrate,
287 content_info->media_meta.samplerate,
288 content_info->media_meta.channel,
289 content_info->media_meta.duration,
290 content_info->media_meta.longitude,
291 content_info->media_meta.latitude,
292 content_info->media_meta.altitude,
293 content_info->media_meta.width,
294 content_info->media_meta.height,
295 content_info->media_meta.datetaken,
296 content_info->media_meta.orientation,
300 err = _media_svc_sql_query(handle, sql);
302 if (err != SQLITE_OK) {
303 media_svc_error("failed to update item");
305 return MEDIA_INFO_ERROR_DATABASE_INTERNAL;
308 return MEDIA_INFO_ERROR_NONE;
310 int _media_svc_get_thumbnail_path_by_path(sqlite3 *handle, const char *path, char *thumbnail_path)
312 int ret = MEDIA_INFO_ERROR_NONE;
313 sqlite3_stmt *sql_stmt = NULL;
315 char *sql = sqlite3_mprintf("SELECT thumbnail_path FROM %s WHERE path='%q'", MEDIA_SVC_DB_TABLE_MEDIA, path);
317 ret = _media_svc_sql_prepare_to_step(handle, sql, &sql_stmt);
319 if (ret != MEDIA_INFO_ERROR_NONE) {
320 if(ret == MEDIA_INFO_ERROR_DATABASE_NO_RECORD) {
321 media_svc_debug("there is no thumbnail.");
324 media_svc_error("error when _media_svc_get_thumbnail_path_by_path. err = [%d]", ret);
329 _strncpy_safe(thumbnail_path, (const char *)sqlite3_column_text(sql_stmt, 0), MEDIA_SVC_PATHNAME_SIZE);
331 SQLITE3_FINALIZE(sql_stmt);
333 return MEDIA_INFO_ERROR_NONE;
336 int _media_svc_get_media_type_by_path(sqlite3 *handle, const char *path, int *media_type)
338 int ret = MEDIA_INFO_ERROR_NONE;
339 sqlite3_stmt *sql_stmt = NULL;
341 char *sql = sqlite3_mprintf("SELECT media_type FROM %s WHERE path='%q'", MEDIA_SVC_DB_TABLE_MEDIA, path);
343 ret = _media_svc_sql_prepare_to_step(handle, sql, &sql_stmt);
345 if (ret != MEDIA_INFO_ERROR_NONE) {
346 media_svc_error("error when _media_svc_get_media_type_by_path. err = [%d]", ret);
350 *media_type = sqlite3_column_int(sql_stmt, 0);
352 SQLITE3_FINALIZE(sql_stmt);
354 return MEDIA_INFO_ERROR_NONE;
357 int _media_svc_delete_item_by_path(sqlite3 *handle, const char *path)
360 char *sql = sqlite3_mprintf("DELETE FROM %s WHERE validity=1 AND path='%q'", MEDIA_SVC_DB_TABLE_MEDIA, path);
362 err = _media_svc_sql_query(handle, sql);
364 if (err != SQLITE_OK) {
365 media_svc_error("It failed to delete item (%d)", err);
366 return MEDIA_INFO_ERROR_DATABASE_INTERNAL;
369 return MEDIA_INFO_ERROR_NONE;
372 int _media_svc_truncate_table(sqlite3 *handle, media_svc_storage_type_e storage_type)
375 char *sql = sqlite3_mprintf("DELETE FROM %s WHERE storage_type=%d", MEDIA_SVC_DB_TABLE_MEDIA, storage_type);
377 err = _media_svc_sql_query(handle, sql);
379 if (err != SQLITE_OK) {
380 media_svc_error("It failed to truncate table (%d)", err);
381 return MEDIA_INFO_ERROR_DATABASE_INTERNAL;
384 return MEDIA_INFO_ERROR_NONE;
388 int _media_svc_delete_invalid_items(sqlite3 *handle, media_svc_storage_type_e storage_type)
391 media_svc_thumbnailpath_s *thumbpath_record = NULL;
393 int invalid_count = 0;
394 int ret = MEDIA_INFO_ERROR_NONE;
396 ret = __media_svc_count_invalid_records_with_thumbnail(handle, storage_type, &invalid_count);
397 media_svc_retv_if(ret != MEDIA_INFO_ERROR_NONE, ret);
399 media_svc_debug("invalid count: %d\n", invalid_count);
401 if (invalid_count > 0) {
402 thumbpath_record = (media_svc_thumbnailpath_s *)calloc( invalid_count, sizeof(media_svc_thumbnailpath_s));
403 if (thumbpath_record == NULL) {
404 media_svc_error("fail to memory allocation");
405 return MEDIA_INFO_ERROR_OUT_OF_MEMORY;
408 ret = __media_svc_get_invalid_records_with_thumbnail(handle, storage_type, invalid_count, thumbpath_record);
409 if (ret != MEDIA_INFO_ERROR_NONE) {
410 media_svc_error("error when get thumbnail record");
411 SAFE_FREE(thumbpath_record);
415 media_svc_debug("There is no item with thumbnail");
418 char *sql = sqlite3_mprintf("DELETE FROM %s WHERE validity = 0 AND storage_type=%d", MEDIA_SVC_DB_TABLE_MEDIA, storage_type);
419 err = _media_svc_sql_query(handle, sql);
421 if (err != SQLITE_OK) {
422 media_svc_error("To delete invalid items is failed(%d)", err);
423 SAFE_FREE(thumbpath_record);
424 return MEDIA_INFO_ERROR_DATABASE_INTERNAL;
427 /*Delete thumbnails*/
428 for (idx = 0; idx < invalid_count; idx++) {
429 if (strlen(thumbpath_record[idx].thumbnail_path) > 0) {
430 if (_media_svc_remove_file(thumbpath_record[idx].thumbnail_path) == FALSE) {
431 media_svc_error("fail to remove thumbnail file.");
432 //SAFE_FREE(thumbpath_record);
433 //return MEDIA_INFO_ERROR_INTERNAL;
438 SAFE_FREE(thumbpath_record);
440 return MEDIA_INFO_ERROR_NONE;
443 int _media_svc_delete_invalid_folder_items(sqlite3 *handle, const char *folder_path)
446 media_svc_thumbnailpath_s *thumbpath_record = NULL;
448 int invalid_count = 0;
449 int ret = MEDIA_INFO_ERROR_NONE;
451 ret = __media_svc_count_invalid_folder_records_with_thumbnail(handle, folder_path, &invalid_count);
452 media_svc_retv_if(ret != MEDIA_INFO_ERROR_NONE, ret);
454 media_svc_debug("invalid count: %d\n", invalid_count);
456 if (invalid_count > 0) {
457 thumbpath_record = (media_svc_thumbnailpath_s *)calloc( invalid_count, sizeof(media_svc_thumbnailpath_s));
458 if (thumbpath_record == NULL) {
459 media_svc_error("fail to memory allocation");
460 return MEDIA_INFO_ERROR_OUT_OF_MEMORY;
463 ret = __media_svc_get_invalid_folder_records_with_thumbnail(handle, folder_path, invalid_count, thumbpath_record);
464 if (ret != MEDIA_INFO_ERROR_NONE) {
465 media_svc_error("error when get thumbnail record");
466 SAFE_FREE(thumbpath_record);
470 media_svc_debug("There is no item with thumbnail");
473 char *sql = sqlite3_mprintf("DELETE FROM %s WHERE validity = 0 AND path LIKE '%q/%%'", MEDIA_SVC_DB_TABLE_MEDIA, folder_path);
474 err = _media_svc_sql_query(handle, sql);
476 if (err != SQLITE_OK) {
477 media_svc_error("To delete invalid items is failed(%d)", err);
478 SAFE_FREE(thumbpath_record);
479 return MEDIA_INFO_ERROR_DATABASE_INTERNAL;
482 /*Delete thumbnails*/
483 for (idx = 0; idx < invalid_count; idx++) {
484 if (strlen(thumbpath_record[idx].thumbnail_path) > 0) {
485 if (_media_svc_remove_file(thumbpath_record[idx].thumbnail_path) == FALSE) {
486 media_svc_error("fail to remove thumbnail file [%s].", thumbpath_record[idx].thumbnail_path);
487 //SAFE_FREE(thumbpath_record);
488 //return MEDIA_INFO_ERROR_INTERNAL;
493 SAFE_FREE(thumbpath_record);
495 return MEDIA_INFO_ERROR_NONE;
499 int _media_svc_update_item_validity(sqlite3 *handle, const char *path, int validity, bool stack_query)
503 char *sql = sqlite3_mprintf("UPDATE %s SET validity=%d WHERE path= '%q'", MEDIA_SVC_DB_TABLE_MEDIA, validity, path);
506 err = _media_svc_sql_query(handle, sql);
508 if (err != SQLITE_OK) {
509 media_svc_error("To update item as valid is failed(%d)", err);
510 return MEDIA_INFO_ERROR_DATABASE_INTERNAL;
513 _media_svc_sql_query_add(&g_media_svc_item_validity_query_list, &sql);
516 return MEDIA_INFO_ERROR_NONE;
519 int _media_svc_update_thumbnail_path(sqlite3 *handle, const char *path, const char *thumb_path)
523 char *sql = sqlite3_mprintf("UPDATE %s SET thumbnail_path=%Q WHERE path= %Q", MEDIA_SVC_DB_TABLE_MEDIA, thumb_path, path);
525 err = _media_svc_sql_query(handle, sql);
527 if (err != SQLITE_OK) {
528 media_svc_error("To update thumb path failed(%d)", err);
529 return MEDIA_INFO_ERROR_DATABASE_INTERNAL;
532 return MEDIA_INFO_ERROR_NONE;
535 int _media_svc_update_storage_item_validity(sqlite3 *handle, media_svc_storage_type_e storage_type, int validity)
538 char *sql = sqlite3_mprintf("UPDATE %s SET validity=%d WHERE storage_type=%d", MEDIA_SVC_DB_TABLE_MEDIA, validity, storage_type);
539 err = _media_svc_sql_query(handle, sql);
541 if (err != SQLITE_OK) {
542 media_svc_error("To update item as valid is failed(%d)", err);
543 return MEDIA_INFO_ERROR_DATABASE_INTERNAL;
546 return MEDIA_INFO_ERROR_NONE;
549 int _media_svc_update_folder_item_validity(sqlite3 *handle, const char *folder_path, int validity)
552 int ret = MEDIA_INFO_ERROR_NONE;
554 char folder_uuid[MEDIA_SVC_UUID_SIZE + 1] = {0, };
555 sqlite3_stmt *sql_stmt = NULL;
558 sql = sqlite3_mprintf("SELECT folder_uuid FROM %s WHERE path='%q'", MEDIA_SVC_DB_TABLE_FOLDER, folder_path);
559 ret = _media_svc_sql_prepare_to_step(handle, sql, &sql_stmt);
560 if (ret != MEDIA_INFO_ERROR_NONE) {
561 media_svc_error("error when get folder_id. err = [%d]", ret);
565 _strncpy_safe(folder_uuid, (const char *)sqlite3_column_text(sql_stmt, 0), MEDIA_SVC_UUID_SIZE+1);
566 SQLITE3_FINALIZE(sql_stmt);
568 /*Update folder item validity*/
569 sql = sqlite3_mprintf("UPDATE %s SET validity=%d WHERE folder_uuid='%q'", MEDIA_SVC_DB_TABLE_MEDIA, validity, folder_uuid);
570 err = _media_svc_sql_query(handle, sql);
572 if (err != SQLITE_OK) {
573 media_svc_error("To update folder item as valid is failed(%d)", err);
574 return MEDIA_INFO_ERROR_DATABASE_INTERNAL;
577 return MEDIA_INFO_ERROR_NONE;
580 int _media_svc_update_recursive_folder_item_validity(sqlite3 *handle, const char *folder_path, int validity)
584 /*Update folder item validity*/
585 char *sql = sqlite3_mprintf("UPDATE %s SET validity=%d WHERE path LIKE '%q/%%'", MEDIA_SVC_DB_TABLE_MEDIA, validity, folder_path);
586 err = _media_svc_sql_query(handle, sql);
588 if (err != SQLITE_OK) {
589 media_svc_error("To update recursive folder item validity is failed(%d)", err);
590 return MEDIA_INFO_ERROR_DATABASE_INTERNAL;
593 return MEDIA_INFO_ERROR_NONE;
596 int _media_svc_update_item_by_path(sqlite3 *handle, const char *src_path, media_svc_storage_type_e dest_storage, const char *dest_path,
597 const char *file_name, int modified_time, const char *folder_uuid, const char *thumb_path, bool stack_query)
599 /* update path, filename, modified_time, folder_uuid, thumbnail_path, */
600 /* played_count, last_played_time, last_played_position, favourite, storaget_type*/
605 if(thumb_path != NULL) {
606 sql = sqlite3_mprintf("UPDATE %s SET \
607 path=%Q, file_name=%Q, modified_time=%d, folder_uuid=%Q, thumbnail_path=%Q, storage_type=%d, \
608 played_count=0, last_played_time=0, last_played_position=0 \
610 MEDIA_SVC_DB_TABLE_MEDIA, dest_path, file_name, modified_time, folder_uuid, thumb_path, dest_storage, src_path);
612 sql = sqlite3_mprintf("UPDATE %s SET \
613 path=%Q, file_name=%Q, modified_time=%d, folder_uuid=%Q, storage_type=%d, \
614 played_count=0, last_played_time=0, last_played_position=0 \
616 MEDIA_SVC_DB_TABLE_MEDIA, dest_path, file_name, modified_time, folder_uuid, dest_storage, src_path);
620 err = _media_svc_sql_query(handle, sql);
622 if (err != SQLITE_OK) {
623 media_svc_error("It failed to update metadata (%d)", err);
624 return MEDIA_INFO_ERROR_DATABASE_INTERNAL;
627 _media_svc_sql_query_add(&g_media_svc_move_item_query_list, &sql);
630 return MEDIA_INFO_ERROR_NONE;
633 int _media_svc_list_query_do(sqlite3 *handle, media_svc_query_type_e query_type)
635 int ret = MEDIA_INFO_ERROR_NONE;
637 ret = _media_svc_sql_begin_trans(handle);
638 media_svc_retv_if(ret != MEDIA_INFO_ERROR_NONE, ret);
640 if (query_type == MEDIA_SVC_QUERY_SET_ITEM_VALIDITY)
641 ret = _media_svc_sql_query_list(handle, &g_media_svc_item_validity_query_list);
642 else if (query_type == MEDIA_SVC_QUERY_MOVE_ITEM)
643 ret = _media_svc_sql_query_list(handle, &g_media_svc_move_item_query_list);
644 else if (query_type == MEDIA_SVC_QUERY_INSERT_ITEM)
645 ret = _media_svc_sql_query_list(handle, &g_media_svc_insert_item_query_list);
647 ret = MEDIA_INFO_ERROR_INVALID_PARAMETER;
649 if (ret != MEDIA_INFO_ERROR_NONE) {
650 media_svc_error("_media_svc_list_query_do failed. start rollback");
651 _media_svc_sql_rollback_trans(handle);
655 ret = _media_svc_sql_end_trans(handle);
656 if (ret != MEDIA_INFO_ERROR_NONE) {
657 media_svc_error("mb_svc_sqlite3_commit_trans failed.. Now start to rollback\n");
658 _media_svc_sql_rollback_trans(handle);
662 return MEDIA_INFO_ERROR_NONE;
665 int _media_svc_get_media_id_by_path(sqlite3 *handle, const char *path, char *media_uuid, int max_length)
667 int ret = MEDIA_INFO_ERROR_NONE;
668 sqlite3_stmt *sql_stmt = NULL;
669 char *sql = sqlite3_mprintf("SELECT media_uuid FROM %s WHERE validity=1 AND path='%q'",
670 MEDIA_SVC_DB_TABLE_MEDIA, path);
672 ret = _media_svc_sql_prepare_to_step(handle, sql, &sql_stmt);
674 if (ret != MEDIA_INFO_ERROR_NONE) {
675 media_svc_error("error when __media_svc_count_invalid_records_with_thumbnail. err = [%d]", ret);
679 strncpy(media_uuid, (const char*)sqlite3_column_text(sql_stmt, 0), max_length);
680 media_uuid[max_length - 1] = '\0';
682 SQLITE3_FINALIZE(sql_stmt);
684 return MEDIA_INFO_ERROR_NONE;
687 int _media_svc_get_burst_id(sqlite3 *handle, int *id)
689 int ret = MEDIA_INFO_ERROR_NONE;
691 sqlite3_stmt *sql_stmt = NULL;
692 char *sql = sqlite3_mprintf("SELECT max(CAST(burst_id AS INTEGER)) FROM %s", MEDIA_SVC_DB_TABLE_MEDIA);
694 ret = _media_svc_sql_prepare_to_step(handle, sql, &sql_stmt);
696 if (ret != MEDIA_INFO_ERROR_NONE) {
697 media_svc_error("error when _media_svc_get_burst_id. err = [%d]", ret);
701 cur_id = sqlite3_column_int(sql_stmt, 0);
703 SQLITE3_FINALIZE(sql_stmt);
705 return MEDIA_INFO_ERROR_NONE;
708 int _media_svc_get_noti_info(sqlite3 *handle, const char *path, int update_item, media_svc_noti_item **item)
710 int ret = MEDIA_INFO_ERROR_NONE;
711 sqlite3_stmt *sql_stmt = NULL;
715 media_svc_error("_media_svc_get_noti_info failed");
716 return MEDIA_INFO_ERROR_INVALID_PARAMETER;
719 if (update_item == MS_MEDIA_ITEM_FILE) {
720 sql = sqlite3_mprintf("SELECT media_uuid, media_type, mime_type FROM %s", MEDIA_SVC_DB_TABLE_MEDIA);
721 } else if (update_item == MS_MEDIA_ITEM_DIRECTORY) {
722 sql = sqlite3_mprintf("SELECT folder_uuid FROM %s", MEDIA_SVC_DB_TABLE_FOLDER);
724 media_svc_error("_media_svc_get_noti_info failed : update item");
725 return MEDIA_INFO_ERROR_INVALID_PARAMETER;
728 ret = _media_svc_sql_prepare_to_step(handle, sql, &sql_stmt);
730 if (ret != MEDIA_INFO_ERROR_NONE) {
731 media_svc_error("error when _media_svc_get_noti_info. err = [%d]", ret);
735 *item = calloc(1, sizeof(media_svc_noti_item));
737 media_svc_error("_media_svc_get_noti_info failed : calloc");
738 return MEDIA_INFO_ERROR_OUT_OF_MEMORY;
741 if (update_item == MS_MEDIA_ITEM_FILE) {
742 if (sqlite3_column_text(sql_stmt, 0))
743 (*item)->media_uuid = strdup((const char *)sqlite3_column_text(sql_stmt, 0));
745 (*item)->media_type = sqlite3_column_int(sql_stmt, 1);
747 if (sqlite3_column_text(sql_stmt, 2))
748 (*item)->mime_type = strdup((const char *)sqlite3_column_text(sql_stmt, 2));
749 } else if (update_item == MS_MEDIA_ITEM_DIRECTORY) {
750 if (sqlite3_column_text(sql_stmt, 0))
751 (*item)->media_uuid = strdup((const char *)sqlite3_column_text(sql_stmt, 0));
754 SQLITE3_FINALIZE(sql_stmt);
756 return MEDIA_INFO_ERROR_NONE;