Update the query used to delete invalid items in storage
[platform/core/multimedia/libmedia-service.git] / src / common / media-svc-media.c
1 /*
2  * libmedia-service
3  *
4  * Copyright (c) 2000 - 2011 Samsung Electronics Co., Ltd. All rights reserved.
5  *
6  * Contact: Hyunjun Ko <zzoon.ko@samsung.com>, Haejeong Kim <backto.kim@samsung.com>
7  *
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
11  *
12  * http://www.apache.org/licenses/LICENSE-2.0
13  *
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.
19  *
20  */
21
22 #include <string.h>
23 #include <unistd.h>
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"
32
33 #define MEDIA_SVC_MAX_COMMIT_SIZE 200
34
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;
38 static __thread GList *g_media_svc_update_item_query_list = NULL;
39 static __thread GList *g_media_svc_update_list = NULL;
40 static __thread int g_media_svc_update_list_count = 0;
41
42 static int __media_svc_get_invalid_records_with_thumbnail(sqlite3 *handle, media_svc_storage_type_e storage_type, GArray **thumb_path)
43 {
44         int ret = MS_MEDIA_ERR_NONE;
45         sqlite3_stmt *sql_stmt = NULL;
46         char *sql = sqlite3_mprintf("SELECT thumbnail_path FROM '%s' WHERE validity=0 AND storage_type=%d AND thumbnail_path IS NOT NULL", MEDIA_SVC_DB_TABLE_MEDIA, storage_type);
47
48         media_svc_debug("[SQL query] : %s", sql);
49
50         ret = _media_svc_sql_prepare_to_step_simple(handle, sql, &sql_stmt);
51         media_svc_retvm_if(ret != MS_MEDIA_ERR_NONE, ret, "Query failed. err[%d]", ret);
52
53         while (sqlite3_step(sql_stmt) == SQLITE_ROW) {
54                 char *path = NULL;
55                 path = g_strdup((const char *)sqlite3_column_text(sql_stmt, 0));
56                 g_array_append_val(*thumb_path, path);
57         }
58
59         SQLITE3_FINALIZE(sql_stmt);
60
61         return MS_MEDIA_ERR_NONE;
62 }
63
64 static int __media_svc_get_invalid_folder_records_with_thumbnail(sqlite3 *handle, const char *folder_path, const char *folder_uuid, bool is_recursive, GArray **thumb_path)
65 {
66         int ret = MS_MEDIA_ERR_NONE;
67         sqlite3_stmt *sql_stmt = NULL;
68         char *sql = NULL;
69
70         if (is_recursive)
71                 sql = sqlite3_mprintf("SELECT thumbnail_path FROM '%s' WHERE validity=0 AND path LIKE '%q/%%' AND thumbnail_path IS NOT NULL", MEDIA_SVC_DB_TABLE_MEDIA, folder_path);
72         else
73                 sql = sqlite3_mprintf("SELECT thumbnail_path FROM '%s' WHERE validity=0 AND folder_uuid = '%q' AND thumbnail_path IS NOT NULL", MEDIA_SVC_DB_TABLE_MEDIA, folder_uuid);
74
75         media_svc_debug("[SQL query] : %s", sql);
76
77         ret = _media_svc_sql_prepare_to_step_simple(handle, sql, &sql_stmt);
78         media_svc_retvm_if(ret != MS_MEDIA_ERR_NONE, ret, "Query failed. err[%d]", ret);
79
80         while (sqlite3_step(sql_stmt) == SQLITE_ROW) {
81                 char *path = NULL;
82                 path = g_strdup((const char *)sqlite3_column_text(sql_stmt, 0));
83                 g_array_append_val(*thumb_path, path);
84         }
85
86         SQLITE3_FINALIZE(sql_stmt);
87
88         return MS_MEDIA_ERR_NONE;
89 }
90
91 int _media_svc_count_record_with_path(sqlite3 *handle, const char *storage_id, const char *path, int *count)
92 {
93         int ret = MS_MEDIA_ERR_NONE;
94         sqlite3_stmt *sql_stmt = NULL;
95
96         char *sql = sqlite3_mprintf("SELECT count(*) FROM '%s' WHERE path='%q'", storage_id, path);
97
98         ret = _media_svc_sql_prepare_to_step(handle, sql, &sql_stmt);
99
100         media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
101
102         *count = sqlite3_column_int(sql_stmt, 0);
103
104         SQLITE3_FINALIZE(sql_stmt);
105
106         return MS_MEDIA_ERR_NONE;
107 }
108
109 int _media_svc_insert_item_with_data(sqlite3 *handle, const char *storage_id, media_svc_content_info_s *content_info, int is_burst, bool stack_query, uid_t uid)
110 {
111         int ret = MS_MEDIA_ERR_NONE;
112         char *burst_id = NULL;
113
114         const char *db_fields = "media_uuid, path, file_name, media_type, mime_type, size, added_time, modified_time, folder_uuid, thumbnail_path, \
115                 title, album_id, album, artist, album_artist, genre, composer, year, recorded_date, copyright, track_num, description, category, keyword, location_tag, \
116                 content_name, age_rating, author, provider, last_played_time, played_count, favourite, bitrate, bitpersample, samplerate, channel, duration, \
117                 longitude, latitude, altitude, exposure_time, fnumber, iso, model, width, height, datetaken, orientation, rating, is_drm, storage_type, burst_id, timeline, weather, sync_status, \
118                 file_name_pinyin, title_pinyin, album_pinyin, artist_pinyin, album_artist_pinyin, genre_pinyin, composer_pinyin, copyright_pinyin, description_pinyin, storage_uuid, is_360";
119
120         const char *db_fields_basic = "media_uuid, path, file_name, media_type, mime_type, size, added_time, modified_time, folder_uuid, thumbnail_path, \
121                 album_id, last_played_time, played_count, favourite, is_drm, storage_type, burst_id, timeline, sync_status, file_name_pinyin, storage_uuid, is_360";
122
123         /* This sql is due to sqlite3_mprintf's wrong operation when using floating point in the text format */
124         /* This code will be removed when sqlite3_mprintf works clearly */
125         char *test_sql = sqlite3_mprintf("%f, %f, %f", content_info->media_meta.longitude, content_info->media_meta.latitude, content_info->media_meta.altitude);
126         SQLITE3_SAFE_FREE(test_sql);
127
128         if (is_burst) {
129                 int burst_id_int = 0;
130                 ret = _media_svc_get_burst_id(handle, storage_id, &burst_id_int);
131                 if (ret != MS_MEDIA_ERR_NONE)
132                         burst_id = NULL;
133
134                 if (burst_id_int > 0) {
135                         media_svc_debug("Burst id : %d", burst_id_int);
136                         burst_id = sqlite3_mprintf("%d", burst_id_int);
137                 }
138
139                 /* Get thumbnail for burst shot */
140                 char thumb_path[MEDIA_SVC_PATHNAME_SIZE + 1] = {0, };
141
142                 ret = _media_svc_request_thumbnail(content_info->path, thumb_path, sizeof(thumb_path), uid);
143                 if (ret == MS_MEDIA_ERR_NONE)
144                         content_info->thumbnail_path = g_strdup(thumb_path);
145         }
146
147         /*Update Pinyin If Support Pinyin*/
148         if (_media_svc_check_pinyin_support()) {
149                 if (STRING_VALID(content_info->file_name))
150                         _media_svc_get_pinyin_str(content_info->file_name, &content_info->file_name_pinyin);
151                 if (STRING_VALID(content_info->media_meta.title))
152                         _media_svc_get_pinyin_str(content_info->media_meta.title, &content_info->media_meta.title_pinyin);
153                 if (STRING_VALID(content_info->media_meta.album))
154                         _media_svc_get_pinyin_str(content_info->media_meta.album, &content_info->media_meta.album_pinyin);
155                 if (STRING_VALID(content_info->media_meta.artist))
156                         _media_svc_get_pinyin_str(content_info->media_meta.artist, &content_info->media_meta.artist_pinyin);
157                 if (STRING_VALID(content_info->media_meta.album_artist))
158                         _media_svc_get_pinyin_str(content_info->media_meta.album_artist, &content_info->media_meta.album_artist_pinyin);
159                 if (STRING_VALID(content_info->media_meta.genre))
160                         _media_svc_get_pinyin_str(content_info->media_meta.genre, &content_info->media_meta.genre_pinyin);
161                 if (STRING_VALID(content_info->media_meta.composer))
162                         _media_svc_get_pinyin_str(content_info->media_meta.composer, &content_info->media_meta.composer_pinyin);
163                 if (STRING_VALID(content_info->media_meta.copyright))
164                         _media_svc_get_pinyin_str(content_info->media_meta.copyright, &content_info->media_meta.copyright_pinyin);
165                 if (STRING_VALID(content_info->media_meta.description))
166                         _media_svc_get_pinyin_str(content_info->media_meta.description, &content_info->media_meta.description_pinyin);
167         }
168
169         char *sql = sqlite3_mprintf("INSERT INTO '%s' (%s) VALUES (%Q, %Q, %Q, %d, %Q, %lld, %d, %d, %Q, \
170                 %Q, %Q, %d, %Q, %Q, %Q, %Q, %Q, %Q, %Q, %Q, %Q, %Q, %Q, %Q, %Q, %Q, %Q, %Q, %Q, %d, %d, %d, \
171                 %d, %d, %d, %d, %d, %.6f, %.6f, %.6f, %Q, %.6f, %d, %Q, %d, %d, %Q, %d, \
172                 %d, %d, %d, %Q, %d, %Q, %d, %Q, %Q, %Q, %Q, %Q, %Q, %Q, %Q, %Q, %Q, %d);",
173                                 content_info->storage_uuid, db_fields,
174                                 content_info->media_uuid,
175                                 content_info->path,
176                                 content_info->file_name,
177                                 content_info->media_type,
178                                 content_info->mime_type,
179                                 content_info->size,
180                                 content_info->added_time,
181                                 content_info->modified_time,
182                                 content_info->folder_uuid,                      /* 1st line */
183                                 content_info->thumbnail_path,
184                                 content_info->media_meta.title,
185                                 content_info->album_id,
186                                 content_info->media_meta.album,
187                                 content_info->media_meta.artist,
188                                 content_info->media_meta.album_artist,
189                                 content_info->media_meta.genre,
190                                 content_info->media_meta.composer,
191                                 content_info->media_meta.year,
192                                 content_info->media_meta.recorded_date,
193                                 content_info->media_meta.copyright,
194                                 content_info->media_meta.track_num,
195                                 content_info->media_meta.description,
196                                 content_info->media_meta.category,
197                                 content_info->media_meta.keyword,
198                                 content_info->media_meta.location_tag,
199                                 content_info->media_meta.content_name,
200                                 content_info->media_meta.age_rating,
201                                 content_info->media_meta.author,
202                                 content_info->media_meta.provider,
203                                 content_info->last_played_time,
204                                 content_info->played_count,
205                                 content_info->favourate,                /* 2nd line */
206                                 content_info->media_meta.bitrate,
207                                 content_info->media_meta.bitpersample,
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.exposure_time,
215                                 content_info->media_meta.fnumber,
216                                 content_info->media_meta.iso,
217                                 content_info->media_meta.model,
218                                 content_info->media_meta.width,
219                                 content_info->media_meta.height,
220                                 content_info->media_meta.datetaken,
221                                 content_info->media_meta.orientation,           /* 3rd line */
222                                 content_info->media_meta.rating,
223                                 content_info->is_drm,
224                                 content_info->storage_type,
225                                 burst_id,
226                                 content_info->timeline,
227                                 content_info->media_meta.weather,
228                                 content_info->sync_status,
229                                 content_info->file_name_pinyin,
230                                 content_info->media_meta.title_pinyin,
231                                 content_info->media_meta.album_pinyin,
232                                 content_info->media_meta.artist_pinyin,
233                                 content_info->media_meta.album_artist_pinyin,
234                                 content_info->media_meta.genre_pinyin,
235                                 content_info->media_meta.composer_pinyin,
236                                 content_info->media_meta.copyright_pinyin,
237                                 content_info->media_meta.description_pinyin,
238                                 content_info->storage_uuid,
239                                 content_info->media_meta.is_360         /* 4th line */
240                         );
241
242         /* Check query length */
243         if (sql != NULL && strlen(sql) >= (MEDIA_SVC_QUERY_LEN_MAX - 1)) {
244                 media_svc_debug("Query length is over %d byte.. So, remake query using only basic information..", MEDIA_SVC_QUERY_LEN_MAX - 1);
245                 SQLITE3_SAFE_FREE(sql);
246
247                 sql = sqlite3_mprintf("INSERT INTO '%s' (%s) VALUES (%Q, %Q, %Q, %d, %Q, %lld, %d, %d, %Q, %Q, %d, %d, %d, %d, %d, %d, %Q, %d, %d, %Q, %Q, %d);",
248                                                 content_info->storage_uuid, db_fields_basic,
249                                                 content_info->media_uuid,
250                                                 content_info->path,
251                                                 content_info->file_name,
252                                                 content_info->media_type,
253                                                 content_info->mime_type,
254                                                 content_info->size,
255                                                 content_info->added_time,
256                                                 content_info->modified_time,
257                                                 content_info->folder_uuid,
258                                                 content_info->thumbnail_path,
259                                                 content_info->album_id,
260                                                 content_info->last_played_time,
261                                                 content_info->played_count,
262                                                 content_info->favourate,
263                                                 content_info->is_drm,
264                                                 content_info->storage_type,
265                                                 burst_id,
266                                                 content_info->timeline,
267                                                 content_info->sync_status,
268                                                 content_info->file_name_pinyin,
269                                                 content_info->storage_uuid,
270                                                 content_info->media_meta.is_360         /* 1st line */
271                                                 );
272         }
273
274         SQLITE3_SAFE_FREE(burst_id);
275
276         if (!stack_query) {
277                 ret = _media_svc_sql_query(sql, uid);
278                 SQLITE3_SAFE_FREE(sql);
279                 if (ret != MS_MEDIA_ERR_NONE) {
280                         media_svc_error("failed to insert item");
281                         return ret;
282                 }
283         } else {
284                 media_svc_debug("query : %s", sql);
285                 _media_svc_sql_query_add(&g_media_svc_insert_item_query_list, &sql);
286         }
287
288         return MS_MEDIA_ERR_NONE;
289 }
290
291 int _media_svc_update_meta_with_data(media_svc_content_info_s *content_info)
292 {
293         int ret = MS_MEDIA_ERR_NONE;
294
295         /* This sql is due to sqlite3_mprintf's wrong operation when using floating point in the text format */
296         /* This code will be removed when sqlite3_mprintf works clearly */
297         char *test_sql = sqlite3_mprintf("%f, %f, %f", content_info->media_meta.longitude, content_info->media_meta.latitude, content_info->media_meta.altitude);
298         SQLITE3_SAFE_FREE(test_sql);
299
300         /*Update Pinyin If Support Pinyin*/
301         if (_media_svc_check_pinyin_support()) {
302                 if (STRING_VALID(content_info->file_name))
303                         _media_svc_get_pinyin_str(content_info->file_name, &content_info->file_name_pinyin);
304                 if (STRING_VALID(content_info->media_meta.title))
305                         _media_svc_get_pinyin_str(content_info->media_meta.title, &content_info->media_meta.title_pinyin);
306                 if (STRING_VALID(content_info->media_meta.album))
307                         _media_svc_get_pinyin_str(content_info->media_meta.album, &content_info->media_meta.album_pinyin);
308                 if (STRING_VALID(content_info->media_meta.artist))
309                         _media_svc_get_pinyin_str(content_info->media_meta.artist, &content_info->media_meta.artist_pinyin);
310                 if (STRING_VALID(content_info->media_meta.album_artist))
311                         _media_svc_get_pinyin_str(content_info->media_meta.album_artist, &content_info->media_meta.album_artist_pinyin);
312                 if (STRING_VALID(content_info->media_meta.genre))
313                         _media_svc_get_pinyin_str(content_info->media_meta.genre, &content_info->media_meta.genre_pinyin);
314                 if (STRING_VALID(content_info->media_meta.composer))
315                         _media_svc_get_pinyin_str(content_info->media_meta.composer, &content_info->media_meta.composer_pinyin);
316                 if (STRING_VALID(content_info->media_meta.copyright))
317                         _media_svc_get_pinyin_str(content_info->media_meta.copyright, &content_info->media_meta.copyright_pinyin);
318                 if (STRING_VALID(content_info->media_meta.description))
319                         _media_svc_get_pinyin_str(content_info->media_meta.description, &content_info->media_meta.description_pinyin);
320         }
321
322         char *sql = sqlite3_mprintf("UPDATE %s SET title=%Q, album=%Q, artist=%Q, album_artist=%Q, genre=%Q, composer=%Q, copyright=%Q, description=%Q, \
323                 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 \
324                 WHERE path=%Q;",
325                                 MEDIA_SVC_DB_TABLE_MEDIA,
326                                 content_info->media_meta.title,
327                                 content_info->media_meta.album,
328                                 content_info->media_meta.artist,
329                                 content_info->media_meta.album_artist,
330                                 content_info->media_meta.genre,
331                                 content_info->media_meta.composer,
332                                 content_info->media_meta.copyright,
333                                 content_info->media_meta.description,
334                                 content_info->file_name_pinyin,
335                                 content_info->media_meta.title_pinyin,
336                                 content_info->media_meta.album_pinyin,
337                                 content_info->media_meta.artist_pinyin,
338                                 content_info->media_meta.album_artist_pinyin,
339                                 content_info->media_meta.genre_pinyin,
340                                 content_info->media_meta.composer_pinyin,
341                                 content_info->media_meta.copyright_pinyin,
342                                 content_info->media_meta.description_pinyin,
343                                 content_info->path
344                                 );
345
346         if (sql != NULL) {
347                 media_svc_debug("query : %s", sql);
348                 _media_svc_sql_query_add(&g_media_svc_update_item_query_list, &sql);
349         } else {
350                 media_svc_error("sqlite3_mprintf failed");
351                 ret = MS_MEDIA_ERR_OUT_OF_MEMORY;
352         }
353
354         return ret;
355 }
356
357 int _media_svc_update_item_with_data(const char *storage_id, media_svc_content_info_s *content_info, uid_t uid)
358 {
359         int ret = MS_MEDIA_ERR_NONE;
360
361         /* This sql is due to sqlite3_mprintf's wrong operation when using floating point in the text format */
362         /* This code will be removed when sqlite3_mprintf works clearly */
363         char *test_sql = sqlite3_mprintf("%f, %f, %f", content_info->media_meta.longitude, content_info->media_meta.latitude, content_info->media_meta.altitude);
364         SQLITE3_SAFE_FREE(test_sql);
365
366         /*Update Pinyin If Support Pinyin*/
367         if (_media_svc_check_pinyin_support()) {
368                 if (STRING_VALID(content_info->file_name))
369                         _media_svc_get_pinyin_str(content_info->file_name, &content_info->file_name_pinyin);
370                 if (STRING_VALID(content_info->media_meta.title))
371                         _media_svc_get_pinyin_str(content_info->media_meta.title, &content_info->media_meta.title_pinyin);
372                 if (STRING_VALID(content_info->media_meta.album))
373                         _media_svc_get_pinyin_str(content_info->media_meta.album, &content_info->media_meta.album_pinyin);
374                 if (STRING_VALID(content_info->media_meta.artist))
375                         _media_svc_get_pinyin_str(content_info->media_meta.artist, &content_info->media_meta.artist_pinyin);
376                 if (STRING_VALID(content_info->media_meta.album_artist))
377                         _media_svc_get_pinyin_str(content_info->media_meta.album_artist, &content_info->media_meta.album_artist_pinyin);
378                 if (STRING_VALID(content_info->media_meta.genre))
379                         _media_svc_get_pinyin_str(content_info->media_meta.genre, &content_info->media_meta.genre_pinyin);
380                 if (STRING_VALID(content_info->media_meta.composer))
381                         _media_svc_get_pinyin_str(content_info->media_meta.composer, &content_info->media_meta.composer_pinyin);
382                 if (STRING_VALID(content_info->media_meta.copyright))
383                         _media_svc_get_pinyin_str(content_info->media_meta.copyright, &content_info->media_meta.copyright_pinyin);
384                 if (STRING_VALID(content_info->media_meta.description))
385                         _media_svc_get_pinyin_str(content_info->media_meta.description, &content_info->media_meta.description_pinyin);
386         }
387
388         char *sql = sqlite3_mprintf("UPDATE '%s' SET size=%lld, modified_time=%d, thumbnail_path=%Q, title=%Q, album_id=%d, album=%Q, artist=%Q, album_artist=%Q, genre=%Q, \
389                 composer=%Q, year=%Q, recorded_date=%Q, copyright=%Q, track_num=%Q, description=%Q, bitrate=%d, bitpersample=%d, samplerate=%d, channel=%d, duration=%d, \
390                 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;",
391                                 storage_id,
392                                 content_info->size,
393                                 content_info->modified_time,
394                                 content_info->thumbnail_path,
395                                 content_info->media_meta.title,
396                                 content_info->album_id,
397                                 content_info->media_meta.album,
398                                 content_info->media_meta.artist,
399                                 content_info->media_meta.album_artist,
400                                 content_info->media_meta.genre,
401                                 content_info->media_meta.composer,
402                                 content_info->media_meta.year,
403                                 content_info->media_meta.recorded_date,
404                                 content_info->media_meta.copyright,
405                                 content_info->media_meta.track_num,
406                                 content_info->media_meta.description,
407                                 content_info->media_meta.bitrate,
408                                 content_info->media_meta.bitpersample,
409                                 content_info->media_meta.samplerate,
410                                 content_info->media_meta.channel,
411                                 content_info->media_meta.duration,
412                                 content_info->media_meta.longitude,
413                                 content_info->media_meta.latitude,
414                                 content_info->media_meta.altitude,
415                                 content_info->media_meta.exposure_time,
416                                 content_info->media_meta.fnumber,
417                                 content_info->media_meta.iso,
418                                 content_info->media_meta.model,
419                                 content_info->media_meta.width,
420                                 content_info->media_meta.height,
421                                 content_info->media_meta.datetaken,
422                                 content_info->media_meta.orientation,
423                                 content_info->path
424                                 );
425
426         ret = _media_svc_sql_query(sql, uid);
427         SQLITE3_SAFE_FREE(sql);
428
429         return ret;
430 }
431 int _media_svc_get_thumbnail_path_by_path(sqlite3 *handle, const char *path, char *thumbnail_path)
432 {
433         int ret = MS_MEDIA_ERR_NONE;
434         sqlite3_stmt *sql_stmt = NULL;
435
436         char *sql = sqlite3_mprintf("SELECT thumbnail_path FROM '%s' WHERE path='%q'", MEDIA_SVC_DB_TABLE_MEDIA, path);
437
438         ret = _media_svc_sql_prepare_to_step(handle, sql, &sql_stmt);
439
440         if (ret != MS_MEDIA_ERR_NONE) {
441                 if (ret == MS_MEDIA_ERR_DB_NO_RECORD)
442                         media_svc_debug("there is no thumbnail.");
443                 else
444                         media_svc_error("error when _media_svc_get_thumbnail_path_by_path. err = [%d]", ret);
445
446                 return ret;
447         }
448
449         SAFE_STRLCPY(thumbnail_path, (const char *)sqlite3_column_text(sql_stmt, 0), MEDIA_SVC_PATHNAME_SIZE);
450
451         SQLITE3_FINALIZE(sql_stmt);
452
453         return MS_MEDIA_ERR_NONE;
454 }
455
456 int _media_svc_get_media_type_by_path(sqlite3 *handle, const char *storage_id, const char *path, int *media_type)
457 {
458         int ret = MS_MEDIA_ERR_NONE;
459         sqlite3_stmt *sql_stmt = NULL;
460
461         char *sql = sqlite3_mprintf("SELECT media_type FROM '%s' WHERE path='%q'", storage_id, path);
462
463         ret = _media_svc_sql_prepare_to_step(handle, sql, &sql_stmt);
464
465         if (ret != MS_MEDIA_ERR_NONE) {
466                 media_svc_error("error when _media_svc_get_media_type_by_path. err = [%d]", ret);
467                 return ret;
468         }
469
470         *media_type = sqlite3_column_int(sql_stmt, 0);
471
472         SQLITE3_FINALIZE(sql_stmt);
473
474         return MS_MEDIA_ERR_NONE;
475 }
476
477 int _media_svc_delete_item_by_path(const char *storage_id, const char *path, bool stack_query, uid_t uid)
478 {
479         int ret = MS_MEDIA_ERR_NONE;
480         char *sql = sqlite3_mprintf("DELETE FROM '%s' WHERE path='%q';", storage_id, path);
481
482         if (!stack_query) {
483                 ret = _media_svc_sql_query(sql, uid);
484                 SQLITE3_SAFE_FREE(sql);
485                 if (ret != MS_MEDIA_ERR_NONE) {
486                         media_svc_error("failed to delete item");
487                         return ret;
488                 }
489         } else {
490                 media_svc_debug("query : %s", sql);
491                 _media_svc_sql_query_add(&g_media_svc_insert_item_query_list, &sql);
492         }
493
494         return ret;
495 }
496
497 int _media_svc_truncate_table(const char *storage_id, media_svc_storage_type_e storage_type, uid_t uid)
498 {
499         int ret = MS_MEDIA_ERR_NONE;
500
501         char *sql = sqlite3_mprintf("DELETE FROM '%s' where storage_type=%d;", storage_id, storage_type);
502
503         ret = _media_svc_sql_query(sql, uid);
504         SQLITE3_SAFE_FREE(sql);
505
506         return ret;
507 }
508
509 int _media_svc_delete_invalid_items(sqlite3 *handle, const char *storage_id, media_svc_storage_type_e storage_type, uid_t uid)
510 {
511         int ret = MS_MEDIA_ERR_NONE;
512         GArray *thumb_list = NULL;
513
514         thumb_list = g_array_new(FALSE, FALSE, sizeof(char*));
515
516         /*If external USB, no need this process */
517         if (storage_type != MEDIA_SVC_STORAGE_EXTERNAL_USB) {
518                 ret = __media_svc_get_invalid_records_with_thumbnail(handle, storage_type, &thumb_list);
519                 if (ret != MS_MEDIA_ERR_NONE) {
520                         media_svc_error("error when get thumbnail record");
521                         g_array_free(thumb_list, false);
522                         thumb_list = NULL;
523                         return ret;
524                 }
525         }
526
527         char *sql = sqlite3_mprintf("DELETE FROM '%s' WHERE validity = 0 AND storage_type = %d;", storage_id, storage_type);
528
529         ret = _media_svc_sql_query(sql, uid);
530         SQLITE3_SAFE_FREE(sql);
531         if (ret != MS_MEDIA_ERR_NONE) {
532                 while (thumb_list->len != 0) {
533                         char *thumb_path = NULL;
534                         thumb_path = g_array_index(thumb_list , char*, 0);
535                         g_array_remove_index(thumb_list, 0);
536                         SAFE_FREE(thumb_path);
537                 }
538                 g_array_free(thumb_list, false);
539                 thumb_list = NULL;
540                 return ret;
541         }
542
543         /*Delete thumbnails*/
544         while (thumb_list->len != 0) {
545                 char *thumb_path = NULL;
546                 thumb_path = g_array_index(thumb_list , char*, 0);
547                 g_array_remove_index(thumb_list, 0);
548
549                 ret = _media_svc_remove_file(thumb_path);
550                 if (ret != MS_MEDIA_ERR_NONE)
551                         media_svc_error("fail to remove thumbnail file.");
552
553                 SAFE_FREE(thumb_path);
554         }
555
556         g_array_free(thumb_list, false);
557         thumb_list = NULL;
558
559         return MS_MEDIA_ERR_NONE;
560 }
561
562 int _media_svc_delete_invalid_folder_items(sqlite3 *handle, const char *storage_id, const char *folder_path, bool is_recursive, uid_t uid)
563 {
564         int ret = MS_MEDIA_ERR_NONE;
565         char folder_uuid[MEDIA_SVC_UUID_SIZE+1] = {0,};
566         GArray *thumb_list = NULL;
567         char *sql = NULL;
568
569         /*get folder uuid from DB*/
570         ret = _media_svc_get_folder_id_by_foldername(handle, storage_id, folder_path, folder_uuid, uid);
571         media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
572
573         thumb_list = g_array_new(FALSE, FALSE, sizeof(char*));
574
575         ret = __media_svc_get_invalid_folder_records_with_thumbnail(handle, folder_path, folder_uuid, is_recursive, &thumb_list);
576         if (ret != MS_MEDIA_ERR_NONE) {
577                 media_svc_error("error when get thumbnail record");
578                 g_array_free(thumb_list, false);
579                 thumb_list = NULL;
580                 return ret;
581         }
582
583         if (is_recursive)
584                 sql = sqlite3_mprintf("DELETE FROM '%s' WHERE validity = 0 AND path LIKE '%q/%%';", storage_id, folder_path);
585         else
586                 sql = sqlite3_mprintf("DELETE FROM '%s' WHERE validity = 0 AND folder_uuid='%q';", storage_id, folder_uuid);
587
588         ret = _media_svc_sql_query(sql, uid);
589         SQLITE3_SAFE_FREE(sql);
590         if (ret != MS_MEDIA_ERR_NONE) {
591                 while (thumb_list->len != 0) {
592                         char *thumb_path = NULL;
593                         thumb_path = g_array_index(thumb_list , char*, 0);
594                         g_array_remove_index(thumb_list, 0);
595                         SAFE_FREE(thumb_path);
596                 }
597                 g_array_free(thumb_list, false);
598                 thumb_list = NULL;
599
600                 return ret;
601         }
602
603         /*Delete thumbnails*/
604         while (thumb_list->len != 0) {
605                 char *thumb_path = NULL;
606                 thumb_path = g_array_index(thumb_list , char*, 0);
607                 g_array_remove_index(thumb_list, 0);
608
609                 ret = _media_svc_remove_file(thumb_path);
610                 if (ret != MS_MEDIA_ERR_NONE)
611                         media_svc_error("fail to remove thumbnail file.");
612
613                 SAFE_FREE(thumb_path);
614         }
615
616         g_array_free(thumb_list, false);
617         thumb_list = NULL;
618
619         return MS_MEDIA_ERR_NONE;
620 }
621
622 int _media_svc_update_item_validity(const char *storage_id, const char *path, int validity, bool stack_query, uid_t uid)
623 {
624         int ret = MS_MEDIA_ERR_NONE;
625
626         char *sql = sqlite3_mprintf("UPDATE '%s' SET validity=%d WHERE path= '%q';", storage_id, validity, path);
627
628         if (!stack_query) {
629                 ret = _media_svc_sql_query(sql, uid);
630                 SQLITE3_SAFE_FREE(sql);
631         } else {
632                 _media_svc_sql_query_add(&g_media_svc_item_validity_query_list, &sql);
633         }
634
635         return ret;
636 }
637
638 int _media_svc_update_thumbnail_path(const char *path, const char *thumb_path, uid_t uid)
639 {
640         int ret = MS_MEDIA_ERR_NONE;
641
642         char *sql = sqlite3_mprintf("UPDATE '%s' SET thumbnail_path=%Q WHERE path= %Q;", MEDIA_SVC_DB_TABLE_MEDIA, thumb_path, path);
643
644         ret = _media_svc_sql_query(sql, uid);
645         SQLITE3_SAFE_FREE(sql);
646
647         return ret;
648 }
649
650 int _media_svc_update_storage_item_validity(const char *storage_id, media_svc_storage_type_e storage_type, int validity, uid_t uid)
651 {
652         int ret = MS_MEDIA_ERR_NONE;
653
654         char *sql = sqlite3_mprintf("UPDATE '%s' SET validity=%d WHERE storage_type=%d;", storage_id, validity, storage_type);
655
656         ret = _media_svc_sql_query(sql, uid);
657         SQLITE3_SAFE_FREE(sql);
658
659         return ret;
660 }
661
662 int _media_svc_update_folder_item_validity(sqlite3 *handle, const char *storage_id, const char *folder_path, int validity, uid_t uid)
663 {
664         int ret = MS_MEDIA_ERR_NONE;
665         char *sql = NULL;
666         char folder_uuid[MEDIA_SVC_UUID_SIZE + 1] = {0, };
667         sqlite3_stmt *sql_stmt = NULL;
668
669         /*Get folder ID*/
670         sql = sqlite3_mprintf("SELECT folder_uuid FROM '%s' WHERE path='%q' AND storage_uuid='%q'", MEDIA_SVC_DB_TABLE_FOLDER, folder_path, storage_id);
671
672         ret = _media_svc_sql_prepare_to_step(handle, sql, &sql_stmt);
673         if (ret != MS_MEDIA_ERR_NONE) {
674                 if (ret == MS_MEDIA_ERR_DB_NO_RECORD)
675                         media_svc_debug("folder not exist");
676                 else
677                         media_svc_error("error when get folder_id. err = [%d]", ret);
678
679                 return ret;
680         }
681
682         SAFE_STRLCPY(folder_uuid, (const char *)sqlite3_column_text(sql_stmt, 0), MEDIA_SVC_UUID_SIZE + 1);
683         SQLITE3_FINALIZE(sql_stmt);
684
685         /*Update folder item validity*/
686         sql = sqlite3_mprintf("UPDATE '%s' SET validity=%d WHERE folder_uuid='%q';", storage_id, validity, folder_uuid);
687
688         ret = _media_svc_sql_query(sql, uid);
689         SQLITE3_SAFE_FREE(sql);
690
691         return ret;
692 }
693
694 int _media_svc_update_recursive_folder_item_validity(const char *storage_id, const char *folder_path, int validity, uid_t uid)
695 {
696         int ret = MS_MEDIA_ERR_NONE;
697
698         /*Update folder item validity*/
699         char *sql = sqlite3_mprintf("UPDATE '%s' SET validity=%d WHERE (storage_type = 0 OR storage_type = 1) AND path LIKE '%q/%%';", storage_id, validity, folder_path);
700
701         ret = _media_svc_sql_query(sql, uid);
702         SQLITE3_SAFE_FREE(sql);
703
704         return ret;
705 }
706
707 int _media_svc_update_item_by_path(const char *src_path, media_svc_storage_type_e dest_storage, const char *dest_path,
708                                 const char *file_name, int modified_time, const char *folder_uuid, const char *thumb_path, bool stack_query, uid_t uid)
709 {
710         /* update path, filename, modified_time, folder_uuid, thumbnail_path, */
711         /* played_count, last_played_time, last_played_position, favourite, storaget_type*/
712
713         int ret = MS_MEDIA_ERR_NONE;
714         char *sql = NULL;
715
716         if (thumb_path != NULL) {
717                 sql = sqlite3_mprintf("UPDATE '%s' SET path=%Q, file_name=%Q, modified_time=%d, folder_uuid=%Q, thumbnail_path=%Q, storage_type=%d, \
718                                         played_count=0, last_played_time=0, last_played_position=0 WHERE path=%Q;",
719                                         MEDIA_SVC_DB_TABLE_MEDIA, dest_path, file_name, modified_time, folder_uuid, thumb_path, dest_storage, src_path);
720         } else {
721                 sql = sqlite3_mprintf("UPDATE '%s' SET path=%Q, file_name=%Q, modified_time=%d, folder_uuid=%Q, storage_type=%d, \
722                                         played_count=0, last_played_time=0, last_played_position=0 WHERE path=%Q;",
723                                         MEDIA_SVC_DB_TABLE_MEDIA, dest_path, file_name, modified_time, folder_uuid, dest_storage, src_path);
724         }
725
726         if (!stack_query) {
727                 ret = _media_svc_sql_query(sql, uid);
728                 SQLITE3_SAFE_FREE(sql);
729         } else {
730                 _media_svc_sql_query_add(&g_media_svc_move_item_query_list, &sql);
731         }
732
733         return ret;
734 }
735
736 int _media_svc_list_query_do(media_svc_query_type_e query_type, uid_t uid)
737 {
738         int ret = MS_MEDIA_ERR_NONE;
739
740         ret = _media_svc_sql_begin_trans(uid);
741         media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
742
743         if (query_type == MEDIA_SVC_QUERY_SET_ITEM_VALIDITY)
744                 ret = _media_svc_sql_query_list(&g_media_svc_item_validity_query_list, uid);
745         else if (query_type == MEDIA_SVC_QUERY_MOVE_ITEM)
746                 ret = _media_svc_sql_query_list(&g_media_svc_move_item_query_list, uid);
747         else if (query_type == MEDIA_SVC_QUERY_INSERT_ITEM)
748                 ret = _media_svc_sql_query_list(&g_media_svc_insert_item_query_list, uid);
749         else if (query_type == MEDIA_SVC_QUERY_UPDATE_ITEM)
750                 ret = _media_svc_sql_query_list(&g_media_svc_update_item_query_list, uid);
751         else if (query_type == MEDIA_SVC_QUERY_INSERT_FOLDER)
752                 ret = _media_svc_sql_query_list(_media_svc_get_folder_list_ptr(), uid);
753         else if (query_type == MEDIA_SVC_QUERY_UPDATE_COMMON)
754                 ret = _media_svc_sql_query_list(&g_media_svc_update_list, uid);
755         else
756                 ret = MS_MEDIA_ERR_INVALID_PARAMETER;
757
758         if (ret != MS_MEDIA_ERR_NONE) {
759                 media_svc_error("_media_svc_list_query_do failed. start rollback");
760                 _media_svc_sql_rollback_trans(uid);
761                 return ret;
762         }
763
764         ret = _media_svc_sql_end_trans(uid);
765         if (ret != MS_MEDIA_ERR_NONE) {
766                 media_svc_error("mb_svc_sqlite3_commit_trans failed.. Now start to rollback");
767                 _media_svc_sql_rollback_trans(uid);
768                 return ret;
769         }
770
771         return MS_MEDIA_ERR_NONE;
772 }
773
774 int _media_svc_append_query_list(const char *query, uid_t uid)
775 {
776         int ret = MS_MEDIA_ERR_NONE;
777
778         g_media_svc_update_list = g_list_append(g_media_svc_update_list, (gpointer)query);
779
780         g_media_svc_update_list_count++;
781
782         if (g_media_svc_update_list_count >= MEDIA_SVC_MAX_COMMIT_SIZE) {
783                 ret = _media_svc_list_query_do(MEDIA_SVC_QUERY_UPDATE_COMMON, uid);
784                 g_media_svc_update_list_count = 0;
785         }
786
787         return ret;
788 }
789
790 int _media_svc_get_burst_id(sqlite3 *handle, const char *storage_id, int *id)
791 {
792         int ret = MS_MEDIA_ERR_NONE;
793         int cur_id = -1;
794         sqlite3_stmt *sql_stmt = NULL;
795         char *sql = sqlite3_mprintf("SELECT max(CAST(burst_id AS INTEGER)) FROM '%s'", storage_id);
796
797         ret = _media_svc_sql_prepare_to_step(handle, sql, &sql_stmt);
798
799         if (ret != MS_MEDIA_ERR_NONE) {
800                 media_svc_error("error when _media_svc_get_burst_id. err = [%d]", ret);
801                 return ret;
802         }
803
804         cur_id = sqlite3_column_int(sql_stmt, 0);
805         *id = ++cur_id;
806         SQLITE3_FINALIZE(sql_stmt);
807
808         return MS_MEDIA_ERR_NONE;
809 }
810
811 int _media_svc_get_noti_info(sqlite3 *handle, const char *storage_id, const char *path, int update_item, media_svc_noti_item **item)
812 {
813         int ret = MS_MEDIA_ERR_NONE;
814         sqlite3_stmt *sql_stmt = NULL;
815         char *sql = NULL;
816         int is_root_dir = FALSE;
817
818         if (item == NULL) {
819                 media_svc_error("invalid parameter");
820                 return MS_MEDIA_ERR_INVALID_PARAMETER;
821         }
822
823         if (update_item == MS_MEDIA_ITEM_FILE)
824                 sql = sqlite3_mprintf("SELECT media_uuid, media_type, mime_type FROM '%s' WHERE path=%Q", storage_id, path);
825         else if (update_item == MS_MEDIA_ITEM_DIRECTORY)
826                 sql = sqlite3_mprintf("SELECT folder_uuid FROM '%s' WHERE path=%Q AND storage_uuid='%s'", MEDIA_SVC_DB_TABLE_FOLDER, path, storage_id);
827         else {
828                 media_svc_error("_media_svc_get_noti_info failed : update item");
829                 return MS_MEDIA_ERR_INVALID_PARAMETER;
830         }
831
832         ret = _media_svc_sql_prepare_to_step(handle, sql, &sql_stmt);
833
834         if (ret != MS_MEDIA_ERR_NONE) {
835                 if (ret == MS_MEDIA_ERR_DB_NO_RECORD && update_item == MS_MEDIA_ITEM_DIRECTORY) {
836                         media_svc_debug("This is root directory of media");
837                         sql_stmt = NULL;
838                         is_root_dir = TRUE;
839                 } else {
840                         media_svc_error("error when _media_svc_get_noti_info. err = [%d]", ret);
841                         return ret;
842                 }
843         }
844
845         *item = calloc(1, sizeof(media_svc_noti_item));
846         if (*item == NULL) {
847                 media_svc_error("_media_svc_get_noti_info failed : calloc");
848                 return MS_MEDIA_ERR_OUT_OF_MEMORY;
849         }
850
851         if (update_item == MS_MEDIA_ITEM_FILE) {
852                 (*item)->media_uuid = g_strdup((const char *)sqlite3_column_text(sql_stmt, 0));
853                 (*item)->media_type = sqlite3_column_int(sql_stmt, 1);
854                 (*item)->mime_type = g_strdup((const char *)sqlite3_column_text(sql_stmt, 2));
855         } else if (update_item == MS_MEDIA_ITEM_DIRECTORY) {
856                 if (is_root_dir)
857                         (*item)->media_uuid = NULL;
858                 else
859                         (*item)->media_uuid = g_strdup((const char *)sqlite3_column_text(sql_stmt, 0));
860         }
861
862         SQLITE3_FINALIZE(sql_stmt);
863
864         return MS_MEDIA_ERR_NONE;
865 }
866
867 int _media_svc_count_invalid_folder_items(sqlite3 *handle, const char *storage_id, const char *folder_path, int *count)
868 {
869         int ret = MS_MEDIA_ERR_NONE;
870         sqlite3_stmt *sql_stmt = NULL;
871
872         char *sql = sqlite3_mprintf("SELECT count(*) FROM '%s' WHERE validity=0 AND path LIKE '%q/%%'", storage_id, folder_path);
873
874         ret = _media_svc_sql_prepare_to_step(handle, sql, &sql_stmt);
875
876         if (ret != MS_MEDIA_ERR_NONE) {
877                 media_svc_error("error when _media_svc_count_invalid_folder_items. err = [%d]", ret);
878                 return ret;
879         }
880
881         *count = sqlite3_column_int(sql_stmt, 0);
882
883         SQLITE3_FINALIZE(sql_stmt);
884
885         return MS_MEDIA_ERR_NONE;
886 }
887
888 int _media_svc_get_fileinfo_by_path(sqlite3 *handle, const char *storage_id, const char *path, time_t *modified_time, unsigned long long *size)
889 {
890         int ret = MS_MEDIA_ERR_NONE;
891         sqlite3_stmt *sql_stmt = NULL;
892         char *sql = sqlite3_mprintf("SELECT modified_time, size FROM '%s' WHERE path='%q'", storage_id, path);
893
894         ret = _media_svc_sql_prepare_to_step(handle, sql, &sql_stmt);
895
896         if (ret != MS_MEDIA_ERR_NONE) {
897                 media_svc_error("error when _media_svc_get_fileinfo_by_path. err = [%d]", ret);
898                 return ret;
899         }
900
901         *modified_time = (int)sqlite3_column_int(sql_stmt, 0);
902         *size = (unsigned long long)sqlite3_column_int64(sql_stmt, 1);
903
904         SQLITE3_FINALIZE(sql_stmt);
905
906         return MS_MEDIA_ERR_NONE;
907 }
908
909 int _media_svc_change_validity_item_batch(const char *storage_id, const char *path, int des_validity, int src_validity, uid_t uid)
910 {
911         int ret = MS_MEDIA_ERR_NONE;
912
913         char *sql = sqlite3_mprintf("UPDATE '%s' SET validity=%d WHERE validity=%d AND path LIKE '%q%%'", storage_id, des_validity, src_validity, path);
914
915         ret = _media_svc_sql_query(sql, uid);
916         SQLITE3_SAFE_FREE(sql);
917
918         return ret;
919 }
920
921 int _media_svc_insert_item_pass1(sqlite3 *handle, const char *storage_id, media_svc_content_info_s *content_info, int is_burst, bool stack_query, uid_t uid)
922 {
923         int ret = MS_MEDIA_ERR_NONE;
924         char *burst_id = NULL;
925
926         char * db_fields = (char *)"media_uuid, folder_uuid, path, file_name, media_type, mime_type, size, added_time, modified_time, is_drm, storage_type, timeline, burst_id, storage_uuid";
927
928         char *sql = sqlite3_mprintf("INSERT INTO '%s' (%s) VALUES (%Q, %Q, %Q, %Q, %d, %Q, %lld, %d, %d, %d, %d, %d, %Q, %Q);",
929                 storage_id, db_fields,
930                 content_info->media_uuid,
931                 content_info->folder_uuid,
932                 content_info->path,
933                 content_info->file_name,
934                 content_info->media_type,
935                 content_info->mime_type,
936                 content_info->size,
937                 content_info->added_time,
938                 content_info->modified_time,
939                 content_info->is_drm,
940                 content_info->storage_type,
941                 content_info->timeline,
942                 burst_id,
943                 storage_id
944                 );
945 #if 0
946                 SQLITE3_SAFE_FREE(burst_id);
947 #endif
948         media_svc_debug("MAKE PASS 1 QUERY END");
949
950         if (!stack_query) {
951                 ret = _media_svc_sql_query(sql, uid);
952                 SQLITE3_SAFE_FREE(sql);
953                 if (ret != MS_MEDIA_ERR_NONE) {
954                         media_svc_error("failed to insert item");
955                         return ret;
956                 }
957         } else {
958                 media_svc_debug("query : %s", sql);
959                 _media_svc_sql_query_add(&g_media_svc_insert_item_query_list, &sql);
960         }
961         media_svc_debug("END");
962         return MS_MEDIA_ERR_NONE;
963 }
964
965 int _media_svc_insert_item_pass2(const char *storage_id, media_svc_content_info_s *content_info, int is_burst, bool stack_query, uid_t uid)
966 {
967         int ret = MS_MEDIA_ERR_NONE;
968
969         media_svc_debug_fenter();
970
971         /*Update Pinyin If Support Pinyin*/
972         if (_media_svc_check_pinyin_support()) {
973                 if (STRING_VALID(content_info->file_name))
974                         _media_svc_get_pinyin_str(content_info->file_name, &content_info->file_name_pinyin);
975                 if (STRING_VALID(content_info->media_meta.title))
976                         _media_svc_get_pinyin_str(content_info->media_meta.title, &content_info->media_meta.title_pinyin);
977                 if (STRING_VALID(content_info->media_meta.album))
978                         _media_svc_get_pinyin_str(content_info->media_meta.album, &content_info->media_meta.album_pinyin);
979                 if (STRING_VALID(content_info->media_meta.artist))
980                         _media_svc_get_pinyin_str(content_info->media_meta.artist, &content_info->media_meta.artist_pinyin);
981                 if (STRING_VALID(content_info->media_meta.album_artist))
982                         _media_svc_get_pinyin_str(content_info->media_meta.album_artist, &content_info->media_meta.album_artist_pinyin);
983                 if (STRING_VALID(content_info->media_meta.genre))
984                         _media_svc_get_pinyin_str(content_info->media_meta.genre, &content_info->media_meta.genre_pinyin);
985                 if (STRING_VALID(content_info->media_meta.composer))
986                         _media_svc_get_pinyin_str(content_info->media_meta.composer, &content_info->media_meta.composer_pinyin);
987                 if (STRING_VALID(content_info->media_meta.copyright))
988                         _media_svc_get_pinyin_str(content_info->media_meta.copyright, &content_info->media_meta.copyright_pinyin);
989                 if (STRING_VALID(content_info->media_meta.description))
990                         _media_svc_get_pinyin_str(content_info->media_meta.description, &content_info->media_meta.description_pinyin);
991         }
992
993         char *sql = sqlite3_mprintf("UPDATE '%s' SET \
994                 thumbnail_path=%Q, title=%Q, album_id=%d, album=%Q, artist=%Q, album_artist=%Q, genre=%Q, composer=%Q, year=%Q, \
995                 recorded_date=%Q, copyright=%Q, track_num=%Q, description=%Q, bitrate=%d, bitpersample=%d, samplerate=%d, channel=%d, \
996                 duration=%d, longitude=%.6f, latitude=%.6f, altitude=%.6f, width=%d, height=%d, datetaken=%Q, orientation=%d, exposure_time=%Q,\
997                 fnumber=%.6f, iso=%d, model=%Q, rating=%d, weather=%Q, file_name_pinyin=%Q, title_pinyin=%Q, album_pinyin=%Q, \
998                 artist_pinyin=%Q, album_artist_pinyin=%Q, genre_pinyin=%Q, composer_pinyin=%Q, copyright_pinyin=%Q, description_pinyin=%Q, is_360=%d WHERE path=%Q;",
999                 storage_id,
1000                 /* content_info->folder_uuid, */
1001                 content_info->thumbnail_path,
1002                 content_info->media_meta.title,
1003                 content_info->album_id,
1004                 content_info->media_meta.album,
1005                 content_info->media_meta.artist,
1006                 content_info->media_meta.album_artist,
1007                 content_info->media_meta.genre,
1008                 content_info->media_meta.composer,
1009                 content_info->media_meta.year,
1010                 content_info->media_meta.recorded_date,
1011                 content_info->media_meta.copyright,
1012                 content_info->media_meta.track_num,
1013                 content_info->media_meta.description,
1014                 content_info->media_meta.bitrate,
1015                 content_info->media_meta.bitpersample,
1016                 content_info->media_meta.samplerate,
1017                 content_info->media_meta.channel,
1018                 content_info->media_meta.duration,
1019                 content_info->media_meta.longitude,
1020                 content_info->media_meta.latitude,
1021                 content_info->media_meta.altitude,
1022                 content_info->media_meta.width,
1023                 content_info->media_meta.height,
1024                 content_info->media_meta.datetaken,
1025                 content_info->media_meta.orientation,
1026                 content_info->media_meta.exposure_time,
1027                 content_info->media_meta.fnumber,
1028                 content_info->media_meta.iso,
1029                 content_info->media_meta.model,
1030                 content_info->media_meta.rating,
1031                 content_info->media_meta.weather,
1032                 content_info->file_name_pinyin,
1033                 content_info->media_meta.title_pinyin,
1034                 content_info->media_meta.album_pinyin,
1035                 content_info->media_meta.artist_pinyin,
1036                 content_info->media_meta.album_artist_pinyin,
1037                 content_info->media_meta.genre_pinyin,
1038                 content_info->media_meta.composer_pinyin,
1039                 content_info->media_meta.copyright_pinyin,
1040                 content_info->media_meta.description_pinyin,
1041                 content_info->media_meta.is_360,
1042                 content_info->path
1043                 );
1044
1045         if (!stack_query) {
1046                 ret = _media_svc_sql_query(sql, uid);
1047                 SQLITE3_SAFE_FREE(sql);
1048                 if (ret != MS_MEDIA_ERR_NONE) {
1049
1050                         media_svc_error("failed to insert item");
1051                         return ret;
1052                 }
1053         } else {
1054                 /*media_svc_debug("query : %s", sql);*/
1055                 _media_svc_sql_query_add(&g_media_svc_update_item_query_list, &sql);
1056         }
1057
1058         media_svc_debug_fleave();
1059
1060         return MS_MEDIA_ERR_NONE;
1061 }