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 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 modified_time FROM '%q' WHERE 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, 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_uuid, path, file_name, media_type, mime_type, size, added_time, modified_time, folder_id, thumbnail_path, \
85 title, album_id, album, artist, album_artist, genre, composer, year, recorded_date, copyright, track_num, description, \
86 favourite, bitrate, bitpersample, samplerate, channel, duration, \
87 longitude, latitude, altitude, exposure_time, fnumber, iso, model, width, height, datetaken, orientation, rating, is_drm, storage_type, timeline, sync_status, \
88 file_name_pinyin, title_pinyin, album_pinyin, artist_pinyin, album_artist_pinyin, genre_pinyin, composer_pinyin, copyright_pinyin, description_pinyin, storage_uuid, is_360";
90 const char *db_fields_basic = "media_uuid, path, file_name, media_type, mime_type, size, added_time, modified_time, folder_id, thumbnail_path, \
91 album_id, favourite, is_drm, storage_type, timeline, sync_status, file_name_pinyin, storage_uuid, is_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)) {
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 /* 1st line */
212 ret = _media_svc_sql_query(sql, uid);
213 SQLITE3_SAFE_FREE(sql);
214 if (ret != MS_MEDIA_ERR_NONE) {
215 media_svc_error("failed to insert item");
219 media_svc_debug("query : %s", sql);
220 _media_svc_sql_query_add(&g_media_svc_insert_item_query_list, &sql);
223 return MS_MEDIA_ERR_NONE;
226 int _media_svc_update_meta_with_data(media_svc_content_info_s *content_info)
228 int ret = MS_MEDIA_ERR_NONE;
230 /* This sql is due to sqlite3_mprintf's wrong operation when using floating point in the text format */
231 /* This code will be removed when sqlite3_mprintf works clearly */
232 char *test_sql = sqlite3_mprintf("%f, %f, %f", content_info->media_meta.longitude, content_info->media_meta.latitude, content_info->media_meta.altitude);
233 SQLITE3_SAFE_FREE(test_sql);
235 /*Update Pinyin If Support Pinyin*/
236 if (_media_svc_check_pinyin_support()) {
237 if (STRING_VALID(content_info->file_name))
238 _media_svc_get_pinyin_str(content_info->file_name, &content_info->file_name_pinyin);
239 if (STRING_VALID(content_info->media_meta.title))
240 _media_svc_get_pinyin_str(content_info->media_meta.title, &content_info->media_meta.title_pinyin);
241 if (STRING_VALID(content_info->media_meta.album))
242 _media_svc_get_pinyin_str(content_info->media_meta.album, &content_info->media_meta.album_pinyin);
243 if (STRING_VALID(content_info->media_meta.artist))
244 _media_svc_get_pinyin_str(content_info->media_meta.artist, &content_info->media_meta.artist_pinyin);
245 if (STRING_VALID(content_info->media_meta.album_artist))
246 _media_svc_get_pinyin_str(content_info->media_meta.album_artist, &content_info->media_meta.album_artist_pinyin);
247 if (STRING_VALID(content_info->media_meta.genre))
248 _media_svc_get_pinyin_str(content_info->media_meta.genre, &content_info->media_meta.genre_pinyin);
249 if (STRING_VALID(content_info->media_meta.composer))
250 _media_svc_get_pinyin_str(content_info->media_meta.composer, &content_info->media_meta.composer_pinyin);
251 if (STRING_VALID(content_info->media_meta.copyright))
252 _media_svc_get_pinyin_str(content_info->media_meta.copyright, &content_info->media_meta.copyright_pinyin);
253 if (STRING_VALID(content_info->media_meta.description))
254 _media_svc_get_pinyin_str(content_info->media_meta.description, &content_info->media_meta.description_pinyin);
257 char *sql = sqlite3_mprintf("UPDATE '%q' SET title=%Q, album=%Q, artist=%Q, album_artist=%Q, genre=%Q, composer=%Q, copyright=%Q, description=%Q, \
258 file_name_pinyin=%Q, title_pinyin=%Q, album_pinyin=%Q, artist_pinyin=%Q, album_artist_pinyin=%Q, genre_pinyin=%Q, composer_pinyin=%Q, copyright_pinyin=%Q, description_pinyin=%Q \
260 content_info->storage_uuid,
261 content_info->media_meta.title,
262 content_info->media_meta.album,
263 content_info->media_meta.artist,
264 content_info->media_meta.album_artist,
265 content_info->media_meta.genre,
266 content_info->media_meta.composer,
267 content_info->media_meta.copyright,
268 content_info->media_meta.description,
269 content_info->file_name_pinyin,
270 content_info->media_meta.title_pinyin,
271 content_info->media_meta.album_pinyin,
272 content_info->media_meta.artist_pinyin,
273 content_info->media_meta.album_artist_pinyin,
274 content_info->media_meta.genre_pinyin,
275 content_info->media_meta.composer_pinyin,
276 content_info->media_meta.copyright_pinyin,
277 content_info->media_meta.description_pinyin,
282 media_svc_debug("query : %s", sql);
283 _media_svc_sql_query_add(&g_media_svc_update_item_query_list, &sql);
285 media_svc_error("sqlite3_mprintf failed");
286 ret = MS_MEDIA_ERR_OUT_OF_MEMORY;
292 int _media_svc_update_item_with_data(const char *storage_id, media_svc_content_info_s *content_info, uid_t uid)
294 int ret = MS_MEDIA_ERR_NONE;
296 /* This sql is due to sqlite3_mprintf's wrong operation when using floating point in the text format */
297 /* This code will be removed when sqlite3_mprintf works clearly */
298 char *test_sql = sqlite3_mprintf("%f, %f, %f", content_info->media_meta.longitude, content_info->media_meta.latitude, content_info->media_meta.altitude);
299 SQLITE3_SAFE_FREE(test_sql);
301 /*Update Pinyin If Support Pinyin*/
302 if (_media_svc_check_pinyin_support()) {
303 if (STRING_VALID(content_info->file_name))
304 _media_svc_get_pinyin_str(content_info->file_name, &content_info->file_name_pinyin);
305 if (STRING_VALID(content_info->media_meta.title))
306 _media_svc_get_pinyin_str(content_info->media_meta.title, &content_info->media_meta.title_pinyin);
307 if (STRING_VALID(content_info->media_meta.album))
308 _media_svc_get_pinyin_str(content_info->media_meta.album, &content_info->media_meta.album_pinyin);
309 if (STRING_VALID(content_info->media_meta.artist))
310 _media_svc_get_pinyin_str(content_info->media_meta.artist, &content_info->media_meta.artist_pinyin);
311 if (STRING_VALID(content_info->media_meta.album_artist))
312 _media_svc_get_pinyin_str(content_info->media_meta.album_artist, &content_info->media_meta.album_artist_pinyin);
313 if (STRING_VALID(content_info->media_meta.genre))
314 _media_svc_get_pinyin_str(content_info->media_meta.genre, &content_info->media_meta.genre_pinyin);
315 if (STRING_VALID(content_info->media_meta.composer))
316 _media_svc_get_pinyin_str(content_info->media_meta.composer, &content_info->media_meta.composer_pinyin);
317 if (STRING_VALID(content_info->media_meta.copyright))
318 _media_svc_get_pinyin_str(content_info->media_meta.copyright, &content_info->media_meta.copyright_pinyin);
319 if (STRING_VALID(content_info->media_meta.description))
320 _media_svc_get_pinyin_str(content_info->media_meta.description, &content_info->media_meta.description_pinyin);
323 char *sql = sqlite3_mprintf("UPDATE '%q' SET size=%lld, modified_time=%d, thumbnail_path=%Q, title=%Q, album_id=%d, album=%Q, artist=%Q, album_artist=%Q, genre=%Q, \
324 composer=%Q, year=%Q, recorded_date=%Q, copyright=%Q, track_num=%Q, description=%Q, bitrate=%d, bitpersample=%d, samplerate=%d, channel=%d, duration=%d, \
325 longitude=%f, latitude=%f, altitude=%f, exposure_time=%Q, fnumber=%f, iso=%d, model=%Q, width=%d, height=%d, datetaken=%Q, orientation=%d WHERE path=%Q;",
328 content_info->modified_time,
329 content_info->thumbnail_path,
330 content_info->media_meta.title,
331 content_info->album_id,
332 content_info->media_meta.album,
333 content_info->media_meta.artist,
334 content_info->media_meta.album_artist,
335 content_info->media_meta.genre,
336 content_info->media_meta.composer,
337 content_info->media_meta.year,
338 content_info->media_meta.recorded_date,
339 content_info->media_meta.copyright,
340 content_info->media_meta.track_num,
341 content_info->media_meta.description,
342 content_info->media_meta.bitrate,
343 content_info->media_meta.bitpersample,
344 content_info->media_meta.samplerate,
345 content_info->media_meta.channel,
346 content_info->media_meta.duration,
347 content_info->media_meta.longitude,
348 content_info->media_meta.latitude,
349 content_info->media_meta.altitude,
350 content_info->media_meta.exposure_time,
351 content_info->media_meta.fnumber,
352 content_info->media_meta.iso,
353 content_info->media_meta.model,
354 content_info->media_meta.width,
355 content_info->media_meta.height,
356 content_info->media_meta.datetaken,
357 content_info->media_meta.orientation,
361 ret = _media_svc_sql_query(sql, uid);
362 SQLITE3_SAFE_FREE(sql);
366 int _media_svc_get_thumbnail_path_by_path(sqlite3 *handle, const char *path, char *thumbnail_path)
368 int ret = MS_MEDIA_ERR_NONE;
369 sqlite3_stmt *sql_stmt = NULL;
372 media_svc_retvm_if(!STRING_VALID(path), MS_MEDIA_ERR_INVALID_PARAMETER, "path is NULL");
374 sql = sqlite3_mprintf("SELECT thumbnail_path FROM '%q' WHERE path='%q'", MEDIA_SVC_DB_VIEW_MEDIA, path);
376 ret = _media_svc_sql_prepare_to_step(handle, sql, &sql_stmt);
378 if (ret != MS_MEDIA_ERR_NONE) {
379 if (ret == MS_MEDIA_ERR_DB_NO_RECORD)
380 media_svc_debug("there is no thumbnail.");
382 media_svc_error("error when _media_svc_get_thumbnail_path_by_path. err = [%d]", ret);
387 SAFE_STRLCPY(thumbnail_path, (const char *)sqlite3_column_text(sql_stmt, 0), MEDIA_SVC_PATHNAME_SIZE);
389 SQLITE3_FINALIZE(sql_stmt);
391 return MS_MEDIA_ERR_NONE;
394 int _media_svc_get_media_type_by_path(sqlite3 *handle, const char *storage_id, const char *path, int *media_type)
396 int ret = MS_MEDIA_ERR_NONE;
397 sqlite3_stmt *sql_stmt = NULL;
400 media_svc_retvm_if(!STRING_VALID(storage_id), MS_MEDIA_ERR_INVALID_PARAMETER, "storage_id is NULL");
401 media_svc_retvm_if(!STRING_VALID(path), MS_MEDIA_ERR_INVALID_PARAMETER, "path is NULL");
403 sql = sqlite3_mprintf("SELECT media_type FROM '%q' WHERE path='%q'", storage_id, path);
405 ret = _media_svc_sql_prepare_to_step(handle, sql, &sql_stmt);
407 if (ret != MS_MEDIA_ERR_NONE) {
408 media_svc_error("error when _media_svc_get_media_type_by_path. err = [%d]", ret);
412 *media_type = sqlite3_column_int(sql_stmt, 0);
414 SQLITE3_FINALIZE(sql_stmt);
416 return MS_MEDIA_ERR_NONE;
419 int _media_svc_delete_item_by_path(const char *storage_id, const char *path, bool stack_query, uid_t uid)
421 int ret = MS_MEDIA_ERR_NONE;
424 media_svc_retvm_if(!STRING_VALID(storage_id), MS_MEDIA_ERR_INVALID_PARAMETER, "storage_id is NULL");
425 media_svc_retvm_if(!STRING_VALID(path), MS_MEDIA_ERR_INVALID_PARAMETER, "path is NULL");
427 sql = sqlite3_mprintf("DELETE FROM '%q' WHERE path='%q';", storage_id, path);
430 ret = _media_svc_sql_query(sql, uid);
431 SQLITE3_SAFE_FREE(sql);
432 if (ret != MS_MEDIA_ERR_NONE) {
433 media_svc_error("failed to delete item");
437 media_svc_debug("query : %s", sql);
438 _media_svc_sql_query_add(&g_media_svc_insert_item_query_list, &sql);
444 int _media_svc_update_item_validity(const char *storage_id, const char *path, int validity, bool stack_query, uid_t uid)
446 int ret = MS_MEDIA_ERR_NONE;
449 media_svc_retvm_if(!STRING_VALID(storage_id), MS_MEDIA_ERR_INVALID_PARAMETER, "storage_id is NULL");
450 media_svc_retvm_if(!STRING_VALID(path), MS_MEDIA_ERR_INVALID_PARAMETER, "path is NULL");
452 media_svc_debug("path=[%s], validity=[%d]", path, validity);
454 sql = sqlite3_mprintf("UPDATE '%q' SET validity=%d WHERE path= '%q';", storage_id, validity, path);
457 ret = _media_svc_sql_query(sql, uid);
458 SQLITE3_SAFE_FREE(sql);
460 _media_svc_sql_query_add(&g_media_svc_item_validity_query_list, &sql);
466 int _media_svc_update_thumbnail_path(const char *storage_id, const char *path, const char *thumb_path, uid_t uid)
468 int ret = MS_MEDIA_ERR_NONE;
470 char *sql = sqlite3_mprintf("UPDATE '%q' SET thumbnail_path=%Q WHERE path=%Q;", storage_id, thumb_path, path);
472 ret = _media_svc_sql_query(sql, uid);
473 SQLITE3_SAFE_FREE(sql);
478 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)
480 /* update path, filename, modified_time, folder_uuid, thumbnail_path, */
482 /* NOTICE (This operation is abnormal. Modifications should be cautious.)
483 1. Update source to destination info
484 2. Insert destination table
485 3. Remove source info
487 int ret = MS_MEDIA_ERR_NONE;
488 char *update = sqlite3_mprintf("UPDATE '%q' SET path=%Q, file_name=%Q, modified_time=%d, folder_id=%Q, storage_type=%d, storage_uuid='%q' WHERE path=%Q;",
489 org_storage_id, dest_path, file_name, modified_time, folder_uuid, dest_storage, dst_storage_id, src_path);
490 char *insert = sqlite3_mprintf("INSERT OR REPLACE INTO '%q' SELECT * FROM '%q' WHERE path = '%q';", dst_storage_id, org_storage_id, dest_path);
491 char *remove = sqlite3_mprintf("DELETE FROM '%q' WHERE path = '%q';", org_storage_id, dest_path);
494 if (strcmp(org_storage_id, dst_storage_id) != 0)
495 query = sqlite3_mprintf("%s %s %s", update, insert, remove);
497 query = sqlite3_mprintf("%s %s", update, insert);
499 SQLITE3_SAFE_FREE(update);
500 SQLITE3_SAFE_FREE(insert);
501 SQLITE3_SAFE_FREE(remove);
503 ret = _media_svc_sql_query(query, uid);
504 SQLITE3_SAFE_FREE(query);
509 int _media_svc_list_query_do(media_svc_query_type_e query_type, uid_t uid)
511 int ret = MS_MEDIA_ERR_NONE;
513 /* For multiples of 200, empty requests are possible */
514 if (query_type == MEDIA_SVC_QUERY_UPDATE_COMMON) {
515 if (g_media_svc_update_list == NULL || g_list_length(g_media_svc_update_list) == 0)
516 return MS_MEDIA_ERR_NONE;
519 ret = _media_svc_sql_begin_trans(uid);
520 media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
522 if (query_type == MEDIA_SVC_QUERY_SET_ITEM_VALIDITY)
523 ret = _media_svc_sql_query_list(&g_media_svc_item_validity_query_list, uid);
524 else if (query_type == MEDIA_SVC_QUERY_INSERT_ITEM)
525 ret = _media_svc_sql_query_list(&g_media_svc_insert_item_query_list, uid);
526 else if (query_type == MEDIA_SVC_QUERY_UPDATE_ITEM)
527 ret = _media_svc_sql_query_list(&g_media_svc_update_item_query_list, uid);
528 else if (query_type == MEDIA_SVC_QUERY_INSERT_FOLDER)
529 ret = _media_svc_sql_query_list(_media_svc_get_folder_list_ptr(), uid);
530 else if (query_type == MEDIA_SVC_QUERY_UPDATE_COMMON)
531 ret = _media_svc_sql_query_list(&g_media_svc_update_list, uid);
533 ret = MS_MEDIA_ERR_INVALID_PARAMETER;
535 media_svc_retvm_if(ret != MS_MEDIA_ERR_NONE, ret, "Failed to request queries");
537 ret = _media_svc_sql_end_trans(uid);
538 media_svc_retvm_if(ret != MS_MEDIA_ERR_NONE, ret, "Failed to batch insert");
540 return MS_MEDIA_ERR_NONE;
543 int _media_svc_append_query_list(const char *query, uid_t uid)
545 int ret = MS_MEDIA_ERR_NONE;
547 media_svc_retvm_if(query == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "query is NULL");
549 g_media_svc_update_list = g_list_append(g_media_svc_update_list, (gpointer)query);
551 if (g_list_length(g_media_svc_update_list) >= MEDIA_SVC_MAX_COMMIT_SIZE)
552 ret = _media_svc_list_query_do(MEDIA_SVC_QUERY_UPDATE_COMMON, uid);
557 int _media_svc_get_noti_info(sqlite3 *handle, const char *storage_id, const char *path, int update_item, media_svc_noti_item **item)
559 int ret = MS_MEDIA_ERR_NONE;
560 sqlite3_stmt *sql_stmt = NULL;
562 int is_root_dir = FALSE;
564 media_svc_retvm_if(item == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "item is NULL");
565 media_svc_retvm_if(!STRING_VALID(storage_id), MS_MEDIA_ERR_INVALID_PARAMETER, "storage_id is NULL");
566 media_svc_retvm_if(!STRING_VALID(path), MS_MEDIA_ERR_INVALID_PARAMETER, "path is NULL");
568 if (update_item == MS_MEDIA_ITEM_FILE)
569 sql = sqlite3_mprintf("SELECT media_uuid, media_type, mime_type FROM '%q' WHERE path=%Q", storage_id, path);
570 else if (update_item == MS_MEDIA_ITEM_DIRECTORY)
571 sql = sqlite3_mprintf("SELECT folder_id FROM '%q' WHERE folder_path=%Q AND storage_uuid='%q'", MEDIA_SVC_DB_TABLE_FOLDER, path, storage_id);
573 media_svc_error("_media_svc_get_noti_info failed : update item");
574 return MS_MEDIA_ERR_INVALID_PARAMETER;
577 ret = _media_svc_sql_prepare_to_step(handle, sql, &sql_stmt);
579 if (ret != MS_MEDIA_ERR_NONE) {
580 if (ret == MS_MEDIA_ERR_DB_NO_RECORD && update_item == MS_MEDIA_ITEM_DIRECTORY) {
581 media_svc_debug("This is root directory of media");
585 media_svc_error("error when _media_svc_get_noti_info. err = [%d]", ret);
590 *item = calloc(1, sizeof(media_svc_noti_item));
592 media_svc_error("_media_svc_get_noti_info failed : calloc");
593 return MS_MEDIA_ERR_OUT_OF_MEMORY;
596 if (update_item == MS_MEDIA_ITEM_FILE) {
597 (*item)->media_uuid = g_strdup((const char *)sqlite3_column_text(sql_stmt, 0));
598 (*item)->media_type = sqlite3_column_int(sql_stmt, 1);
599 (*item)->mime_type = g_strdup((const char *)sqlite3_column_text(sql_stmt, 2));
600 } else if (update_item == MS_MEDIA_ITEM_DIRECTORY) {
602 (*item)->media_uuid = NULL;
604 (*item)->media_uuid = g_strdup((const char *)sqlite3_column_text(sql_stmt, 0));
607 SQLITE3_FINALIZE(sql_stmt);
609 return MS_MEDIA_ERR_NONE;