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.
24 #include <sys/types.h>
25 #include <media-util-err.h>
26 #include "media-svc-media.h"
27 #include "media-svc-media-folder.h"
28 #include "media-svc-debug.h"
29 #include "media-svc-util.h"
30 #include "media-svc-db-utils.h"
31 #include "media-svc-noti.h"
33 #define MEDIA_SVC_MAX_COMMIT_SIZE 200
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 static __thread GList *g_media_svc_update_item_query_list = NULL;
38 static __thread GList *g_media_svc_update_list = NULL;
40 int _media_svc_count_record_with_path(sqlite3 *handle, const char *storage_id, const char *path, int *count)
42 int ret = MS_MEDIA_ERR_NONE;
43 sqlite3_stmt *sql_stmt = NULL;
46 media_svc_retvm_if(!STRING_VALID(storage_id), MS_MEDIA_ERR_INVALID_PARAMETER, "storage_id is NULL");
47 media_svc_retvm_if(!STRING_VALID(path), MS_MEDIA_ERR_INVALID_PARAMETER, "Path is NULL");
49 sql = sqlite3_mprintf("SELECT count(*) FROM '%q' WHERE MEDIA_path='%q'", storage_id, path);
50 ret = _media_svc_sql_prepare_to_step(handle, sql, &sql_stmt);
51 media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
53 *count = sqlite3_column_int(sql_stmt, 0);
55 SQLITE3_FINALIZE(sql_stmt);
57 return MS_MEDIA_ERR_NONE;
60 int _media_svc_get_modified_time(sqlite3 *handle, const char *storage_id, const char *path, int *modified_time)
62 int ret = MS_MEDIA_ERR_NONE;
63 sqlite3_stmt *sql_stmt = NULL;
66 media_svc_retvm_if(!STRING_VALID(storage_id), MS_MEDIA_ERR_INVALID_PARAMETER, "storage_id is NULL");
67 media_svc_retvm_if(!STRING_VALID(path), MS_MEDIA_ERR_INVALID_PARAMETER, "Path is NULL");
69 sql = sqlite3_mprintf("SELECT media_modified_time FROM '%q' WHERE MEDIA_path='%q'", storage_id, path);
70 ret = _media_svc_sql_prepare_to_step(handle, sql, &sql_stmt);
71 media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
73 *modified_time = sqlite3_column_int(sql_stmt, 0);
75 SQLITE3_FINALIZE(sql_stmt);
77 return MS_MEDIA_ERR_NONE;
80 int _media_svc_insert_item_with_data(sqlite3 *handle, bool is_direct, const char *storage_id, media_svc_content_info_s *content_info, bool stack_query, uid_t uid)
82 int ret = MS_MEDIA_ERR_NONE;
84 const char *db_fields = "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, \
85 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, \
86 media_favourite, media_bitrate, media_bitpersample, media_samplerate, media_channel, media_duration, \
87 media_longitude, media_latitude, media_altitude, exposure_time, fnumber, iso, model, media_width, media_height, media_datetaken, media_orientation, media_rating, media_is_drm, media_storage_type, media_timeline, sync_status, \
88 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, storage_uuid, media_360";
90 const char *db_fields_basic = "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, \
91 album_id, media_favourite, media_is_drm, media_storage_type, media_timeline, sync_status, media_file_name_pinyin, storage_uuid, media_360";
93 /* This sql is due to sqlite3_mprintf's wrong operation when using floating point in the text format */
94 /* This code will be removed when sqlite3_mprintf works clearly */
95 char *test_sql = sqlite3_mprintf("%f, %f, %f", content_info->media_meta.longitude, content_info->media_meta.latitude, content_info->media_meta.altitude);
96 SQLITE3_SAFE_FREE(test_sql);
98 /*Update Pinyin If Support Pinyin*/
99 if (_media_svc_check_pinyin_support()) {
100 if (STRING_VALID(content_info->file_name))
101 _media_svc_get_pinyin_str(content_info->file_name, &content_info->file_name_pinyin);
102 if (STRING_VALID(content_info->media_meta.title))
103 _media_svc_get_pinyin_str(content_info->media_meta.title, &content_info->media_meta.title_pinyin);
104 if (STRING_VALID(content_info->media_meta.album))
105 _media_svc_get_pinyin_str(content_info->media_meta.album, &content_info->media_meta.album_pinyin);
106 if (STRING_VALID(content_info->media_meta.artist))
107 _media_svc_get_pinyin_str(content_info->media_meta.artist, &content_info->media_meta.artist_pinyin);
108 if (STRING_VALID(content_info->media_meta.album_artist))
109 _media_svc_get_pinyin_str(content_info->media_meta.album_artist, &content_info->media_meta.album_artist_pinyin);
110 if (STRING_VALID(content_info->media_meta.genre))
111 _media_svc_get_pinyin_str(content_info->media_meta.genre, &content_info->media_meta.genre_pinyin);
112 if (STRING_VALID(content_info->media_meta.composer))
113 _media_svc_get_pinyin_str(content_info->media_meta.composer, &content_info->media_meta.composer_pinyin);
114 if (STRING_VALID(content_info->media_meta.copyright))
115 _media_svc_get_pinyin_str(content_info->media_meta.copyright, &content_info->media_meta.copyright_pinyin);
116 if (STRING_VALID(content_info->media_meta.description))
117 _media_svc_get_pinyin_str(content_info->media_meta.description, &content_info->media_meta.description_pinyin);
120 char *sql = sqlite3_mprintf("INSERT INTO '%q' (%q) VALUES (%Q, %Q, %Q, %d, %Q, %lld, %d, %d, %Q, \
121 %Q, %Q, %d, %Q, %Q, %Q, %Q, %Q, %Q, %Q, %Q, %Q, %Q, %d, \
122 %d, %d, %d, %d, %d, %.6f, %.6f, %.6f, %Q, %.6f, %d, %Q, %d, %d, %Q, %d, \
123 %d, %d, %d, %d, %d, %Q, %Q, %Q, %Q, %Q, %Q, %Q, %Q, %Q, %Q, %d);",
124 content_info->storage_uuid, db_fields,
125 content_info->media_uuid,
127 content_info->file_name,
128 content_info->media_type,
129 content_info->mime_type,
131 content_info->added_time,
132 content_info->modified_time,
133 content_info->folder_uuid, /* 1st line */
134 content_info->thumbnail_path,
135 content_info->media_meta.title,
136 content_info->album_id,
137 content_info->media_meta.album,
138 content_info->media_meta.artist,
139 content_info->media_meta.album_artist,
140 content_info->media_meta.genre,
141 content_info->media_meta.composer,
142 content_info->media_meta.year,
143 content_info->media_meta.recorded_date,
144 content_info->media_meta.copyright,
145 content_info->media_meta.track_num,
146 content_info->media_meta.description,
147 content_info->favourate, /* 2nd line */
148 content_info->media_meta.bitrate,
149 content_info->media_meta.bitpersample,
150 content_info->media_meta.samplerate,
151 content_info->media_meta.channel,
152 content_info->media_meta.duration,
153 content_info->media_meta.longitude,
154 content_info->media_meta.latitude,
155 content_info->media_meta.altitude,
156 content_info->media_meta.exposure_time,
157 content_info->media_meta.fnumber,
158 content_info->media_meta.iso,
159 content_info->media_meta.model,
160 content_info->media_meta.width,
161 content_info->media_meta.height,
162 content_info->media_meta.datetaken,
163 content_info->media_meta.orientation, /* 3rd line */
164 content_info->media_meta.rating,
165 content_info->is_drm,
166 content_info->storage_type,
167 content_info->timeline,
168 content_info->sync_status,
169 content_info->file_name_pinyin,
170 content_info->media_meta.title_pinyin,
171 content_info->media_meta.album_pinyin,
172 content_info->media_meta.artist_pinyin,
173 content_info->media_meta.album_artist_pinyin,
174 content_info->media_meta.genre_pinyin,
175 content_info->media_meta.composer_pinyin,
176 content_info->media_meta.copyright_pinyin,
177 content_info->media_meta.description_pinyin,
178 content_info->storage_uuid,
179 content_info->media_meta.is_360 /* 4th line */
182 /* Check query length */
183 if (sql != NULL && strlen(sql) >= (MEDIA_SVC_QUERY_LEN_MAX - 1) && is_direct == false) {
184 media_svc_debug("Query length is over %d byte.. So, remake query using only basic information..", MEDIA_SVC_QUERY_LEN_MAX - 1);
185 SQLITE3_SAFE_FREE(sql);
187 sql = sqlite3_mprintf("INSERT INTO '%q' (%q) VALUES (%Q, %Q, %Q, %d, %Q, %lld, %d, %d, %Q, %Q, %d, %d, %d, %d, %d, %d, %d, %d, %Q, %Q, %d);",
188 content_info->storage_uuid, db_fields_basic,
189 content_info->media_uuid,
191 content_info->file_name,
192 content_info->media_type,
193 content_info->mime_type,
195 content_info->added_time,
196 content_info->modified_time,
197 content_info->folder_uuid,
198 content_info->thumbnail_path,
199 content_info->album_id,
200 content_info->favourate,
201 content_info->is_drm,
202 content_info->storage_type,
203 content_info->timeline,
204 content_info->sync_status,
205 content_info->file_name_pinyin,
206 content_info->storage_uuid,
207 content_info->media_meta.is_360
213 ret = _media_svc_sql_query_direct(sql, uid);
215 ret = _media_svc_sql_query(sql, uid);
217 SQLITE3_SAFE_FREE(sql);
218 if (ret != MS_MEDIA_ERR_NONE) {
219 media_svc_error("failed to insert item");
223 media_svc_sec_debug("Query[%s]", sql);
224 _media_svc_sql_query_add(&g_media_svc_insert_item_query_list, &sql);
227 return MS_MEDIA_ERR_NONE;
230 int _media_svc_update_meta_with_data(media_svc_content_info_s *content_info)
232 int ret = MS_MEDIA_ERR_NONE;
234 /* This sql is due to sqlite3_mprintf's wrong operation when using floating point in the text format */
235 /* This code will be removed when sqlite3_mprintf works clearly */
236 char *test_sql = sqlite3_mprintf("%f, %f, %f", content_info->media_meta.longitude, content_info->media_meta.latitude, content_info->media_meta.altitude);
237 SQLITE3_SAFE_FREE(test_sql);
239 /*Update Pinyin If Support Pinyin*/
240 if (_media_svc_check_pinyin_support()) {
241 if (STRING_VALID(content_info->file_name))
242 _media_svc_get_pinyin_str(content_info->file_name, &content_info->file_name_pinyin);
243 if (STRING_VALID(content_info->media_meta.title))
244 _media_svc_get_pinyin_str(content_info->media_meta.title, &content_info->media_meta.title_pinyin);
245 if (STRING_VALID(content_info->media_meta.album))
246 _media_svc_get_pinyin_str(content_info->media_meta.album, &content_info->media_meta.album_pinyin);
247 if (STRING_VALID(content_info->media_meta.artist))
248 _media_svc_get_pinyin_str(content_info->media_meta.artist, &content_info->media_meta.artist_pinyin);
249 if (STRING_VALID(content_info->media_meta.album_artist))
250 _media_svc_get_pinyin_str(content_info->media_meta.album_artist, &content_info->media_meta.album_artist_pinyin);
251 if (STRING_VALID(content_info->media_meta.genre))
252 _media_svc_get_pinyin_str(content_info->media_meta.genre, &content_info->media_meta.genre_pinyin);
253 if (STRING_VALID(content_info->media_meta.composer))
254 _media_svc_get_pinyin_str(content_info->media_meta.composer, &content_info->media_meta.composer_pinyin);
255 if (STRING_VALID(content_info->media_meta.copyright))
256 _media_svc_get_pinyin_str(content_info->media_meta.copyright, &content_info->media_meta.copyright_pinyin);
257 if (STRING_VALID(content_info->media_meta.description))
258 _media_svc_get_pinyin_str(content_info->media_meta.description, &content_info->media_meta.description_pinyin);
261 char *sql = sqlite3_mprintf("UPDATE '%q' SET media_title=%Q, media_album=%Q, media_artist=%Q, media_album_artist=%Q, media_genre=%Q, media_composer=%Q, media_copyright=%Q, media_description=%Q, \
262 media_file_name_pinyin=%Q, media_title_pinyin=%Q, media_album_pinyin=%Q, media_artist_pinyin=%Q, media_album_artist_pinyin=%Q, media_genre_pinyin=%Q, media_composer_pinyin=%Q, media_copyright_pinyin=%Q, media_description_pinyin=%Q \
264 content_info->storage_uuid,
265 content_info->media_meta.title,
266 content_info->media_meta.album,
267 content_info->media_meta.artist,
268 content_info->media_meta.album_artist,
269 content_info->media_meta.genre,
270 content_info->media_meta.composer,
271 content_info->media_meta.copyright,
272 content_info->media_meta.description,
273 content_info->file_name_pinyin,
274 content_info->media_meta.title_pinyin,
275 content_info->media_meta.album_pinyin,
276 content_info->media_meta.artist_pinyin,
277 content_info->media_meta.album_artist_pinyin,
278 content_info->media_meta.genre_pinyin,
279 content_info->media_meta.composer_pinyin,
280 content_info->media_meta.copyright_pinyin,
281 content_info->media_meta.description_pinyin,
286 media_svc_debug("query[%s]", sql);
287 _media_svc_sql_query_add(&g_media_svc_update_item_query_list, &sql);
289 media_svc_error("sqlite3_mprintf failed");
290 ret = MS_MEDIA_ERR_OUT_OF_MEMORY;
296 int _media_svc_update_item_with_data(const char *storage_id, media_svc_content_info_s *content_info, uid_t uid)
298 int ret = MS_MEDIA_ERR_NONE;
300 /* This sql is due to sqlite3_mprintf's wrong operation when using floating point in the text format */
301 /* This code will be removed when sqlite3_mprintf works clearly */
302 char *test_sql = sqlite3_mprintf("%f, %f, %f", content_info->media_meta.longitude, content_info->media_meta.latitude, content_info->media_meta.altitude);
303 SQLITE3_SAFE_FREE(test_sql);
305 /*Update Pinyin If Support Pinyin*/
306 if (_media_svc_check_pinyin_support()) {
307 if (STRING_VALID(content_info->file_name))
308 _media_svc_get_pinyin_str(content_info->file_name, &content_info->file_name_pinyin);
309 if (STRING_VALID(content_info->media_meta.title))
310 _media_svc_get_pinyin_str(content_info->media_meta.title, &content_info->media_meta.title_pinyin);
311 if (STRING_VALID(content_info->media_meta.album))
312 _media_svc_get_pinyin_str(content_info->media_meta.album, &content_info->media_meta.album_pinyin);
313 if (STRING_VALID(content_info->media_meta.artist))
314 _media_svc_get_pinyin_str(content_info->media_meta.artist, &content_info->media_meta.artist_pinyin);
315 if (STRING_VALID(content_info->media_meta.album_artist))
316 _media_svc_get_pinyin_str(content_info->media_meta.album_artist, &content_info->media_meta.album_artist_pinyin);
317 if (STRING_VALID(content_info->media_meta.genre))
318 _media_svc_get_pinyin_str(content_info->media_meta.genre, &content_info->media_meta.genre_pinyin);
319 if (STRING_VALID(content_info->media_meta.composer))
320 _media_svc_get_pinyin_str(content_info->media_meta.composer, &content_info->media_meta.composer_pinyin);
321 if (STRING_VALID(content_info->media_meta.copyright))
322 _media_svc_get_pinyin_str(content_info->media_meta.copyright, &content_info->media_meta.copyright_pinyin);
323 if (STRING_VALID(content_info->media_meta.description))
324 _media_svc_get_pinyin_str(content_info->media_meta.description, &content_info->media_meta.description_pinyin);
327 char *sql = sqlite3_mprintf("UPDATE '%q' SET media_size=%lld, media_modified_time=%d, media_thumbnail_path=%Q, media_title=%Q, album_id=%d, media_album=%Q, media_artist=%Q, media_album_artist=%Q, media_genre=%Q, \
328 media_composer=%Q, media_year=%Q, media_recorded_date=%Q, media_copyright=%Q, media_track_num=%Q, media_description=%Q, media_bitrate=%d, media_bitpersample=%d, media_samplerate=%d, media_channel=%d, media_duration=%d, \
329 media_longitude=%f, media_latitude=%f, media_altitude=%f, exposure_time=%Q, fnumber=%f, iso=%d, model=%Q, media_width=%d, media_height=%d, media_datetaken=%Q, media_orientation=%d WHERE media_path=%Q;",
332 content_info->modified_time,
333 content_info->thumbnail_path,
334 content_info->media_meta.title,
335 content_info->album_id,
336 content_info->media_meta.album,
337 content_info->media_meta.artist,
338 content_info->media_meta.album_artist,
339 content_info->media_meta.genre,
340 content_info->media_meta.composer,
341 content_info->media_meta.year,
342 content_info->media_meta.recorded_date,
343 content_info->media_meta.copyright,
344 content_info->media_meta.track_num,
345 content_info->media_meta.description,
346 content_info->media_meta.bitrate,
347 content_info->media_meta.bitpersample,
348 content_info->media_meta.samplerate,
349 content_info->media_meta.channel,
350 content_info->media_meta.duration,
351 content_info->media_meta.longitude,
352 content_info->media_meta.latitude,
353 content_info->media_meta.altitude,
354 content_info->media_meta.exposure_time,
355 content_info->media_meta.fnumber,
356 content_info->media_meta.iso,
357 content_info->media_meta.model,
358 content_info->media_meta.width,
359 content_info->media_meta.height,
360 content_info->media_meta.datetaken,
361 content_info->media_meta.orientation,
365 ret = _media_svc_sql_query(sql, uid);
366 SQLITE3_SAFE_FREE(sql);
370 int _media_svc_get_thumbnail_path_by_path(sqlite3 *handle, const char *path, char *thumbnail_path)
372 int ret = MS_MEDIA_ERR_NONE;
373 sqlite3_stmt *sql_stmt = NULL;
376 media_svc_retvm_if(!STRING_VALID(path), MS_MEDIA_ERR_INVALID_PARAMETER, "path is NULL");
378 sql = sqlite3_mprintf("SELECT media_thumbnail_path FROM '%q' WHERE media_path='%q'", MEDIA_SVC_DB_VIEW_MEDIA, path);
380 ret = _media_svc_sql_prepare_to_step(handle, sql, &sql_stmt);
382 if (ret != MS_MEDIA_ERR_NONE) {
383 if (ret == MS_MEDIA_ERR_DB_NO_RECORD)
384 media_svc_debug("there is no thumbnail.");
386 media_svc_error("error when _media_svc_get_thumbnail_path_by_path. err = [%d]", ret);
391 SAFE_STRLCPY(thumbnail_path, (const char *)sqlite3_column_text(sql_stmt, 0), MEDIA_SVC_PATHNAME_SIZE);
393 SQLITE3_FINALIZE(sql_stmt);
395 return MS_MEDIA_ERR_NONE;
398 int _media_svc_get_media_type_by_path(sqlite3 *handle, const char *storage_id, const char *path, int *media_type)
400 int ret = MS_MEDIA_ERR_NONE;
401 sqlite3_stmt *sql_stmt = NULL;
404 media_svc_retvm_if(!STRING_VALID(storage_id), MS_MEDIA_ERR_INVALID_PARAMETER, "storage_id is NULL");
405 media_svc_retvm_if(!STRING_VALID(path), MS_MEDIA_ERR_INVALID_PARAMETER, "path is NULL");
407 sql = sqlite3_mprintf("SELECT media_type FROM '%q' WHERE media_path='%q'", storage_id, path);
409 ret = _media_svc_sql_prepare_to_step(handle, sql, &sql_stmt);
411 if (ret != MS_MEDIA_ERR_NONE) {
412 media_svc_error("error when _media_svc_get_media_type_by_path. err = [%d]", ret);
416 *media_type = sqlite3_column_int(sql_stmt, 0);
418 SQLITE3_FINALIZE(sql_stmt);
420 return MS_MEDIA_ERR_NONE;
423 int _media_svc_delete_item_by_path(bool is_direct, const char *storage_id, const char *path, bool stack_query, uid_t uid)
425 int ret = MS_MEDIA_ERR_NONE;
428 media_svc_retvm_if(!STRING_VALID(storage_id), MS_MEDIA_ERR_INVALID_PARAMETER, "storage_id is NULL");
429 media_svc_retvm_if(!STRING_VALID(path), MS_MEDIA_ERR_INVALID_PARAMETER, "path is NULL");
431 sql = sqlite3_mprintf("DELETE FROM '%q' WHERE media_path='%q';", storage_id, path);
435 ret = _media_svc_sql_query_direct(sql, uid);
437 ret = _media_svc_sql_query(sql, uid);
438 SQLITE3_SAFE_FREE(sql);
439 if (ret != MS_MEDIA_ERR_NONE) {
440 media_svc_error("failed to delete item");
444 media_svc_debug("query : %s", sql);
445 _media_svc_sql_query_add(&g_media_svc_insert_item_query_list, &sql);
451 int _media_svc_update_item_validity(const char *storage_id, const char *path, int validity, bool stack_query, uid_t uid)
453 int ret = MS_MEDIA_ERR_NONE;
456 media_svc_retvm_if(!STRING_VALID(storage_id), MS_MEDIA_ERR_INVALID_PARAMETER, "storage_id is NULL");
457 media_svc_retvm_if(!STRING_VALID(path), MS_MEDIA_ERR_INVALID_PARAMETER, "path is NULL");
459 media_svc_debug("path=[%s], validity=[%d]", path, validity);
461 sql = sqlite3_mprintf("UPDATE '%q' SET validity=%d WHERE media_path='%q';", storage_id, validity, path);
464 ret = _media_svc_sql_query_direct(sql, uid);
465 SQLITE3_SAFE_FREE(sql);
467 _media_svc_sql_query_add(&g_media_svc_item_validity_query_list, &sql);
473 int _media_svc_update_thumbnail_path(const char *storage_id, const char *path, const char *thumb_path, uid_t uid)
475 int ret = MS_MEDIA_ERR_NONE;
477 char *sql = sqlite3_mprintf("UPDATE '%q' SET media_thumbnail_path=%Q WHERE media_path=%Q;", storage_id, thumb_path, path);
479 ret = _media_svc_sql_query(sql, uid);
480 SQLITE3_SAFE_FREE(sql);
485 int _media_svc_update_item_by_path(const char *org_storage_id, const char *src_path, const char *dst_storage_id, ms_user_storage_type_e dest_storage, const char *dest_path, const char *file_name, int modified_time, const char *folder_uuid, uid_t uid)
487 /* update path, filename, modified_time, folder_uuid, thumbnail_path, */
489 /* NOTICE (This operation is abnormal. Modifications should be cautious.)
490 1. Update source to destination info
491 2. Insert destination table
492 3. Remove source info
494 int ret = MS_MEDIA_ERR_NONE;
495 char *update = sqlite3_mprintf("UPDATE '%q' SET media_path=%Q, media_display_name=%Q, media_modified_time=%d, folder_id=%Q, media_storage_type=%d, storage_uuid='%q' WHERE media_path=%Q;",
496 org_storage_id, dest_path, file_name, modified_time, folder_uuid, dest_storage, dst_storage_id, src_path);
497 char *insert = sqlite3_mprintf("INSERT OR REPLACE INTO '%q' SELECT * FROM '%q' WHERE media_path = '%q';", dst_storage_id, org_storage_id, dest_path);
498 char *remove = sqlite3_mprintf("DELETE FROM '%q' WHERE media_path = '%q';", org_storage_id, dest_path);
501 if (strcmp(org_storage_id, dst_storage_id) != 0)
502 query = sqlite3_mprintf("%s %s %s", update, insert, remove);
504 query = sqlite3_mprintf("%s %s", update, insert);
506 SQLITE3_SAFE_FREE(update);
507 SQLITE3_SAFE_FREE(insert);
508 SQLITE3_SAFE_FREE(remove);
510 ret = _media_svc_sql_query(query, uid);
511 SQLITE3_SAFE_FREE(query);
516 int _media_svc_list_query_do(media_svc_query_type_e query_type, uid_t uid)
518 int ret = MS_MEDIA_ERR_NONE;
520 /* For multiples of 200, empty requests are possible */
521 switch (query_type) {
522 case MEDIA_SVC_QUERY_UPDATE_COMMON:
523 if (g_media_svc_update_list == NULL || g_list_length(g_media_svc_update_list) == 0)
524 return MS_MEDIA_ERR_NONE;
526 ret = _media_svc_sql_begin_trans(uid);
527 media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
529 ret = _media_svc_sql_query_list(&g_media_svc_update_list, uid);
530 media_svc_retvm_if(ret != MS_MEDIA_ERR_NONE, ret, "Failed to request queries");
532 ret = _media_svc_sql_end_trans(uid);
534 case MEDIA_SVC_QUERY_SET_ITEM_VALIDITY:
535 ret = _media_svc_sql_query_list_direct(&g_media_svc_item_validity_query_list, uid);
537 case MEDIA_SVC_QUERY_INSERT_ITEM:
538 ret = _media_svc_sql_query_list_direct(&g_media_svc_insert_item_query_list, uid);
540 case MEDIA_SVC_QUERY_UPDATE_ITEM:
541 ret = _media_svc_sql_query_list_direct(&g_media_svc_update_item_query_list, uid);
543 case MEDIA_SVC_QUERY_INSERT_FOLDER:
544 ret = _media_svc_sql_query_list_direct(_media_svc_get_folder_list_ptr(), uid);
547 media_svc_error("Wrong type[%d]", query_type);
548 return MS_MEDIA_ERR_INVALID_PARAMETER;
551 media_svc_retvm_if(ret != MS_MEDIA_ERR_NONE, ret, "Failed to request queries");
553 return MS_MEDIA_ERR_NONE;
556 int _media_svc_append_query_list(const char *query, uid_t uid)
558 int ret = MS_MEDIA_ERR_NONE;
560 media_svc_retvm_if(query == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "query is NULL");
562 g_media_svc_update_list = g_list_append(g_media_svc_update_list, (gpointer)query);
564 if (g_list_length(g_media_svc_update_list) >= MEDIA_SVC_MAX_COMMIT_SIZE)
565 ret = _media_svc_list_query_do(MEDIA_SVC_QUERY_UPDATE_COMMON, uid);
570 int _media_svc_get_noti_info(sqlite3 *handle, const char *storage_id, const char *path, int update_item, media_svc_noti_item **item)
572 int ret = MS_MEDIA_ERR_NONE;
573 sqlite3_stmt *sql_stmt = NULL;
575 int is_root_dir = FALSE;
577 media_svc_retvm_if(item == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "item is NULL");
578 media_svc_retvm_if(!STRING_VALID(storage_id), MS_MEDIA_ERR_INVALID_PARAMETER, "storage_id is NULL");
579 media_svc_retvm_if(!STRING_VALID(path), MS_MEDIA_ERR_INVALID_PARAMETER, "path is NULL");
581 if (update_item == MS_MEDIA_ITEM_FILE)
582 sql = sqlite3_mprintf("SELECT media_id, media_type, media_mime_type FROM '%q' WHERE media_path=%Q", storage_id, path);
583 else if (update_item == MS_MEDIA_ITEM_DIRECTORY)
584 sql = sqlite3_mprintf("SELECT folder_id FROM '%q' WHERE folder_path=%Q AND storage_uuid='%q'", MEDIA_SVC_DB_TABLE_FOLDER, path, storage_id);
586 media_svc_error("_media_svc_get_noti_info failed : update item");
587 return MS_MEDIA_ERR_INVALID_PARAMETER;
590 ret = _media_svc_sql_prepare_to_step(handle, sql, &sql_stmt);
592 if (ret != MS_MEDIA_ERR_NONE) {
593 if (ret == MS_MEDIA_ERR_DB_NO_RECORD && update_item == MS_MEDIA_ITEM_DIRECTORY) {
594 media_svc_debug("This is root directory of media");
598 media_svc_error("error when _media_svc_get_noti_info. err = [%d]", ret);
603 *item = calloc(1, sizeof(media_svc_noti_item));
605 media_svc_error("_media_svc_get_noti_info failed : calloc");
606 return MS_MEDIA_ERR_OUT_OF_MEMORY;
609 if (update_item == MS_MEDIA_ITEM_FILE) {
610 (*item)->media_uuid = g_strdup((const char *)sqlite3_column_text(sql_stmt, 0));
611 (*item)->media_type = sqlite3_column_int(sql_stmt, 1);
612 (*item)->mime_type = g_strdup((const char *)sqlite3_column_text(sql_stmt, 2));
613 } else if (update_item == MS_MEDIA_ITEM_DIRECTORY) {
615 (*item)->media_uuid = NULL;
617 (*item)->media_uuid = g_strdup((const char *)sqlite3_column_text(sql_stmt, 0));
620 SQLITE3_FINALIZE(sql_stmt);
622 return MS_MEDIA_ERR_NONE;