4 * Copyright (c) 2000 - 2011 Samsung Electronics Co., Ltd. All rights reserved.
6 * Contact: Hyunjun Ko <zzoon.ko@samsung.com>, Haejeong Kim <backto.kim@samsung.com>
8 * Licensed under the Apache License, Version 2.0 (the "License");
9 * you may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
12 * http://www.apache.org/licenses/LICENSE-2.0
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS,
16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
23 * This file defines structure and functions related to database for managing music item.
25 * @file audio-svc-music-table.c
31 #include "media-svc-util.h"
32 #include "audio-svc-error.h"
33 #include "audio-svc-music-table.h"
34 #include "audio-svc-playlist-table.h"
35 #include "audio-svc-debug.h"
36 #include "audio-svc-types-priv.h"
37 #include "audio-svc-utils.h"
38 #include <drm-service.h>
39 #include "audio-svc-db-utils.h"
43 AUDIO_SVC_AUDIO_INFO_AUDIO_ID,
44 AUDIO_SVC_AUDIO_INFO_PATH,
45 AUDIO_SVC_AUDIO_INFO_FILE_NAME,
46 AUDIO_SVC_AUDIO_INFO_THUMBNAIL_PATH,
47 AUDIO_SVC_AUDIO_INFO_TITLE,
48 AUDIO_SVC_AUDIO_INFO_ALBUM,
49 AUDIO_SVC_AUDIO_INFO_ARTIST,
50 AUDIO_SVC_AUDIO_INFO_GENRE,
51 AUDIO_SVC_AUDIO_INFO_AUTHOR,
52 AUDIO_SVC_AUDIO_INFO_YEAR,
53 AUDIO_SVC_AUDIO_INFO_COPYRIGHT,
54 AUDIO_SVC_AUDIO_INFO_DESCRIPTION,
55 AUDIO_SVC_AUDIO_INFO_FORMAT,
56 AUDIO_SVC_AUDIO_INFO_BITRATE,
57 AUDIO_SVC_AUDIO_INFO_TRACK,
58 AUDIO_SVC_AUDIO_INFO_DURATION,
59 AUDIO_SVC_AUDIO_INFO_RATING,
60 AUDIO_SVC_AUDIO_INFO_PLAYCOUNT,
61 AUDIO_SVC_AUDIO_INFO_PLAYTIME,
62 AUDIO_SVC_AUDIO_INFO_ADDTIME,
63 AUDIO_SVC_AUDIO_INFO_RATEDTIME,
64 AUDIO_SVC_AUDIO_INFO_ALBUM_RATING,
65 AUDIO_SVC_AUDIO_INFO_DATE_MODIFIED,
66 AUDIO_SVC_AUDIO_INFO_SIZE,
67 AUDIO_SVC_AUDIO_INFO_CATEGORY,
68 AUDIO_SVC_AUDIO_INFO_VALID,
69 AUDIO_SVC_AUDIO_INFO_FOLDER_ID,
70 AUDIO_SVC_AUDIO_INFO_STORAGE,
71 AUDIO_SVC_AUDIO_INFO_FAVOURATE,
72 AUDIO_SVC_AUDIO_INFO_CONTENT_TYPE,
73 } audio_svc_audio_info_e;
75 #define AUDIO_SVC_ORDER_BY_TITLE "ORDER BY title COLLATE NOCASE"
76 #define AUDIO_SVC_ORDER_BY_ALBUM "ORDER BY album COLLATE NOCASE"
77 #define AUDIO_SVC_ORDER_BY_GENRE "ORDER BY genre COLLATE NOCASE"
78 #define AUDIO_SVC_ORDER_BY_AUTHOR "ORDER BY author COLLATE NOCASE"
79 #define AUDIO_SVC_ORDER_BY_ARTIST "ORDER BY artist COLLATE NOCASE"
80 #define AUDIO_SVC_COLLATE_NOCASE "COLLATE NOCASE"
82 static __thread GList *g_audio_svc_item_valid_query_list = NULL;
83 static __thread GList *g_audio_svc_move_item_query_list = NULL;
84 static __thread GList *g_audio_svc_insert_item_query_list = NULL;
87 static int __audio_svc_create_music_db_table(sqlite3 *handle);
88 static void __audio_svc_get_next_record(audio_svc_audio_item_s *item, sqlite3_stmt *stmt);
89 static int __audio_svc_count_invalid_records_with_thumbnail(sqlite3 *handle, audio_svc_storage_type_e storage_type);
90 static int __audio_svc_count_records_with_thumbnail(sqlite3 *handle, audio_svc_storage_type_e storage_type);
91 static int __audio_svc_get_invalid_records_with_thumbnail(sqlite3 *handle, audio_svc_storage_type_e storage_type, int count, mp_thumbnailpath_record_t * thumb_path);
92 static int __audio_svc_get_records_with_thumbnail(sqlite3 *handle, audio_svc_storage_type_e storage_type, int count, mp_thumbnailpath_record_t * thumb_path);
95 static int __audio_svc_create_music_db_table(sqlite3 *handle)
99 char *sql = sqlite3_mprintf("create table if not exists %s (\
100 audio_uuid text primary key, \
102 thumbnail_path text, \
108 year integer default -1, \
112 bitrate integer default -1, \
113 track_num integer default -1, \
114 duration integer default -1, \
115 rating integer default 0, \
116 played_count integer default 0, \
117 last_played_time integer default -1, \
120 album_rating integer default 0,\
121 modified_date integer default 0, \
122 size integer default 0, \
123 category INTEGER default 0, \
124 valid integer default 0, \
125 folder_uuid TEXT NOT NULL, \
126 storage_type integer, \
127 favourite integer default 0, \
128 content_type integer default %d);",
129 AUDIO_SVC_DB_TABLE_AUDIO,
130 AUDIO_SVC_CONTENT_TYPE);
132 err = _audio_svc_sql_query(handle, sql);
134 if (err != SQLITE_OK) {
135 audio_svc_error("It failed to create db table (%d)", err);
136 if (err == AUDIO_SVC_ERROR_DB_CONNECT) {
139 return AUDIO_SVC_ERROR_DB_CREATE_TABLE;
142 return AUDIO_SVC_ERROR_NONE;
145 static void __audio_svc_get_next_record(audio_svc_audio_item_s *item,
148 _strncpy_safe(item->audio_uuid,
149 (const char *)sqlite3_column_text(stmt, AUDIO_SVC_AUDIO_INFO_AUDIO_ID), sizeof(item->audio_uuid));
150 _strncpy_safe(item->pathname,
151 (const char *)sqlite3_column_text(stmt, AUDIO_SVC_AUDIO_INFO_PATH), sizeof(item->pathname));
152 _strncpy_safe(item->thumbname,
153 (const char *)sqlite3_column_text(stmt, AUDIO_SVC_AUDIO_INFO_THUMBNAIL_PATH), sizeof(item->thumbname));
154 _strncpy_safe(item->audio.title,
155 (const char *)sqlite3_column_text(stmt, AUDIO_SVC_AUDIO_INFO_TITLE), sizeof(item->audio.title));
156 _strncpy_safe(item->audio.album,
157 (const char *)sqlite3_column_text(stmt, AUDIO_SVC_AUDIO_INFO_ALBUM), sizeof(item->audio.album));
158 _strncpy_safe(item->audio.artist,
159 (const char *)sqlite3_column_text(stmt, AUDIO_SVC_AUDIO_INFO_ARTIST), sizeof(item->audio.artist));
160 _strncpy_safe(item->audio.genre,
161 (const char *)sqlite3_column_text(stmt, AUDIO_SVC_AUDIO_INFO_GENRE), sizeof(item->audio.genre));
162 _strncpy_safe(item->audio.author,
163 (const char *)sqlite3_column_text(stmt, AUDIO_SVC_AUDIO_INFO_AUTHOR), sizeof(item->audio.author));
164 _strncpy_safe(item->audio.year,
165 _year_2_str(sqlite3_column_int(stmt, AUDIO_SVC_AUDIO_INFO_YEAR)), sizeof(item->audio.year));
166 _strncpy_safe(item->audio.copyright,
167 (const char *)sqlite3_column_text(stmt, AUDIO_SVC_AUDIO_INFO_COPYRIGHT), sizeof(item->audio.copyright));
168 _strncpy_safe(item->audio.description,
169 (const char *)sqlite3_column_text(stmt, AUDIO_SVC_AUDIO_INFO_DESCRIPTION), sizeof(item->audio.description));
170 _strncpy_safe(item->audio.format,
171 (const char *)sqlite3_column_text(stmt, AUDIO_SVC_AUDIO_INFO_FORMAT), sizeof(item->audio.format));
173 item->audio.bitrate = sqlite3_column_int(stmt, AUDIO_SVC_AUDIO_INFO_BITRATE);
174 item->audio.track = sqlite3_column_int(stmt, AUDIO_SVC_AUDIO_INFO_TRACK);
175 item->audio.duration = sqlite3_column_int(stmt, AUDIO_SVC_AUDIO_INFO_DURATION);
176 item->rating = sqlite3_column_int(stmt, AUDIO_SVC_AUDIO_INFO_RATING);
177 item->played_count = sqlite3_column_int(stmt, AUDIO_SVC_AUDIO_INFO_PLAYCOUNT);
178 item->time_played = sqlite3_column_int(stmt, AUDIO_SVC_AUDIO_INFO_PLAYTIME);
179 item->time_added = sqlite3_column_int(stmt, AUDIO_SVC_AUDIO_INFO_ADDTIME);
180 item->audio.album_rating = sqlite3_column_int(stmt, AUDIO_SVC_AUDIO_INFO_ALBUM_RATING);
181 item->category = sqlite3_column_int(stmt, AUDIO_SVC_AUDIO_INFO_CATEGORY);
182 item->storage_type = sqlite3_column_int(stmt, AUDIO_SVC_AUDIO_INFO_STORAGE);
183 item->favourate = sqlite3_column_int(stmt, AUDIO_SVC_AUDIO_INFO_FAVOURATE);
188 __audio_svc_count_invalid_records_with_thumbnail(sqlite3 *handle, audio_svc_storage_type_e
191 int ret = AUDIO_SVC_ERROR_NONE;
193 sqlite3_stmt *sql_stmt = NULL;
196 ("select count(*) from %s where valid=0 and storage_type=%d and thumbnail_path is not null",
197 AUDIO_SVC_DB_TABLE_AUDIO, storage_type);
199 ret = _audio_svc_sql_prepare_to_step(handle, sql, &sql_stmt);
201 if (ret != AUDIO_SVC_ERROR_NONE) {
203 ("error when __audio_svc_count_invalid_records_with_thumbnail. err = [%d]",
208 count = sqlite3_column_int(sql_stmt, 0);
210 SQLITE3_FINALIZE(sql_stmt);
216 static int __audio_svc_count_records_with_thumbnail(sqlite3 *handle, audio_svc_storage_type_e
219 int ret = AUDIO_SVC_ERROR_NONE;
221 sqlite3_stmt *sql_stmt = NULL;
224 ("select count(*) from %s where storage_type=%d and thumbnail_path is not null",
225 AUDIO_SVC_DB_TABLE_AUDIO, storage_type);
227 ret = _audio_svc_sql_prepare_to_step(handle, sql, &sql_stmt);
229 if (ret != AUDIO_SVC_ERROR_NONE) {
231 ("error when __audio_svc_count_records_with_thumbnail. err = [%d]",
236 count = sqlite3_column_int(sql_stmt, 0);
238 SQLITE3_FINALIZE(sql_stmt);
245 __audio_svc_get_invalid_records_with_thumbnail(sqlite3 *handle, audio_svc_storage_type_e
246 storage_type, int count,
247 mp_thumbnailpath_record_t *
252 sqlite3_stmt *sql_stmt = NULL;
256 ("select thumbnail_path from %s where valid=0 and storage_type=%d and thumbnail_path is not null",
257 AUDIO_SVC_DB_TABLE_AUDIO, storage_type);
259 audio_svc_debug("[SQL query] : %s", sql);
261 err = sqlite3_prepare_v2(handle, sql, -1, &sql_stmt, NULL);
263 if (err != SQLITE_OK) {
264 audio_svc_error("prepare error [%s]", sqlite3_errmsg(handle));
265 return AUDIO_SVC_ERROR_DB_INTERNAL;
268 while (sqlite3_step(sql_stmt) == SQLITE_ROW) {
269 _strncpy_safe(thumb_path[idx].thumbnail_path,
270 (const char *)sqlite3_column_text(sql_stmt, 0),
271 sizeof(thumb_path[idx]));
273 audio_svc_debug("thumb_path[%d]=[%s]", idx,
274 thumb_path[idx].thumbnail_path);
277 SQLITE3_FINALIZE(sql_stmt);
279 return AUDIO_SVC_ERROR_NONE;
282 static int __audio_svc_get_records_with_thumbnail(sqlite3 *handle, audio_svc_storage_type_e
283 storage_type, int count,
284 mp_thumbnailpath_record_t *
289 sqlite3_stmt *sql_stmt = NULL;
293 ("select thumbnail_path from %s where storage_type=%d and thumbnail_path is not null",
294 AUDIO_SVC_DB_TABLE_AUDIO, storage_type);
296 audio_svc_debug("[SQL query] : %s", sql);
298 err = sqlite3_prepare_v2(handle, sql, -1, &sql_stmt, NULL);
300 if (err != SQLITE_OK) {
301 audio_svc_error("prepare error [%s]", sqlite3_errmsg(handle));
302 return AUDIO_SVC_ERROR_DB_INTERNAL;
305 while (sqlite3_step(sql_stmt) == SQLITE_ROW) {
306 _strncpy_safe(thumb_path[idx].thumbnail_path,
307 (const char *)sqlite3_column_text(sql_stmt, 0),
308 sizeof(thumb_path[idx]));
310 audio_svc_debug("thumb_path[%d]=[%s]", idx,
311 thumb_path[idx].thumbnail_path);
314 SQLITE3_FINALIZE(sql_stmt);
316 return AUDIO_SVC_ERROR_NONE;
319 int _audio_svc_create_music_table(sqlite3 *handle)
321 int ret = AUDIO_SVC_ERROR_NONE;
323 ret = __audio_svc_create_music_db_table(handle);
324 audio_svc_retv_if(ret != AUDIO_SVC_ERROR_NONE, ret);
326 return AUDIO_SVC_ERROR_NONE;
329 int _audio_svc_truncate_music_table(sqlite3 *handle, audio_svc_storage_type_e storage_type)
332 mp_thumbnailpath_record_t *thumbpath_record = NULL;
334 int invalid_count = 0;
335 int ret = AUDIO_SVC_ERROR_NONE;
337 invalid_count = __audio_svc_count_records_with_thumbnail(handle, storage_type);
338 audio_svc_debug("invalid count: %d\n", invalid_count);
340 if (invalid_count > 0) {
342 (mp_thumbnailpath_record_t *)
343 malloc(sizeof(mp_thumbnailpath_record_t) * invalid_count);
344 if (thumbpath_record == NULL) {
345 audio_svc_debug("fail to memory allocation");
346 return AUDIO_SVC_ERROR_OUT_OF_MEMORY;
348 memset(thumbpath_record, 0,
349 sizeof(mp_thumbnailpath_record_t) * invalid_count);
351 ret = __audio_svc_get_records_with_thumbnail(handle, storage_type, invalid_count, thumbpath_record);
352 if (ret != AUDIO_SVC_ERROR_NONE) {
353 audio_svc_error("error when get thumbnail record");
354 SAFE_FREE(thumbpath_record);
358 audio_svc_debug("There is no item with thumbnail");
362 sqlite3_mprintf("delete from %s where storage_type=%d",
363 AUDIO_SVC_DB_TABLE_AUDIO, storage_type);
364 err = _audio_svc_sql_query(handle, sql);
366 if (err != SQLITE_OK) {
367 audio_svc_error("It failed to truncate table (%d)", err);
368 SAFE_FREE(thumbpath_record);
370 if (err == AUDIO_SVC_ERROR_DB_CONNECT) {
373 return AUDIO_SVC_ERROR_DB_INTERNAL;
376 for (idx = 0; idx < invalid_count; idx++) {
377 if (strlen(thumbpath_record[idx].thumbnail_path) > 0) {
378 ret = _audio_svc_check_and_remove_thumbnail(handle, thumbpath_record[idx].thumbnail_path);
379 if (ret != AUDIO_SVC_ERROR_NONE) {
381 ("error _audio_svc_check_and_remove_thumbnail");
382 SAFE_FREE(thumbpath_record);
388 SAFE_FREE(thumbpath_record);
389 return AUDIO_SVC_ERROR_NONE;
393 int _audio_svc_create_folder_table(sqlite3 *handle)
396 char *sql = sqlite3_mprintf("create table if not exists %s (\
397 folder_uuid text primary key, \
400 storage_type integer,\
401 modified_date integer default 0);", AUDIO_SVC_DB_TABLE_AUDIO_FOLDER);
403 err = _audio_svc_sql_query(handle, sql);
405 if (err != SQLITE_OK) {
406 audio_svc_error("error while create folder table");
407 if (err == AUDIO_SVC_ERROR_DB_CONNECT) {
410 return AUDIO_SVC_ERROR_DB_CREATE_TABLE;
413 return AUDIO_SVC_ERROR_NONE;
416 int _audio_svc_delete_folder(sqlite3 *handle, audio_svc_storage_type_e storage_type, const char *folder_id)
420 if ((storage_type != AUDIO_SVC_STORAGE_PHONE)
421 && (storage_type != AUDIO_SVC_STORAGE_MMC)) {
422 audio_svc_error("Invalid storage type");
423 return AUDIO_SVC_ERROR_INVALID_PARAMETER;
426 if ((storage_type == AUDIO_SVC_STORAGE_MMC) && (folder_id == NULL)) { /* when mmc card removed. */
427 sql = sqlite3_mprintf("delete from %s where storage_type=%d",
428 AUDIO_SVC_DB_TABLE_AUDIO_FOLDER, storage_type);
429 err = _audio_svc_sql_query(handle, sql);
431 if (err != SQLITE_OK) {
432 audio_svc_debug("It failed to delete item (%d)", err);
433 if (err == AUDIO_SVC_ERROR_DB_CONNECT) {
436 return AUDIO_SVC_ERROR_DB_INTERNAL;
440 sqlite3_mprintf("delete from %s where folder_uuid='%q'",
441 AUDIO_SVC_DB_TABLE_AUDIO_FOLDER, folder_id);
442 err = _audio_svc_sql_query(handle, sql);
444 if (err != SQLITE_OK) {
445 audio_svc_debug("It failed to delete item (%d)", err);
446 if (err == AUDIO_SVC_ERROR_DB_CONNECT) {
449 return AUDIO_SVC_ERROR_DB_INTERNAL;
453 return AUDIO_SVC_ERROR_NONE;
456 int _audio_svc_insert_item_with_data(sqlite3 *handle, audio_svc_audio_item_s *item, bool stack_query)
459 int ret = AUDIO_SVC_ERROR_NONE;
461 int modified_date = -1;
463 char folder_id[AUDIO_SVC_UUID_SIZE+1] = {0,};
465 char *audio_id = NULL;
466 char *g_audio_svc_music_fields = "audio_uuid, path, file_name, thumbnail_path, title, album, artist, genre, author, year,\
467 copyright, description, format, bitrate,track_num,duration, rating, played_count, last_played_time, added_time, modified_date, size, category, valid, folder_uuid, storage_type";
468 char * file_name = NULL;
471 audio_svc_error("Invalid handle");
472 return AUDIO_SVC_ERROR_INVALID_PARAMETER;
475 audio_id = _media_info_generate_uuid();
476 if(audio_id == NULL ) {
477 audio_svc_error("Invalid UUID");
478 return AUDIO_SVC_ERROR_INTERNAL;
481 /* set creation date */
482 memset(&st, 0, sizeof(struct stat));
483 if (stat(item->pathname, &st) == 0) {
484 modified_date = st.st_mtime;
488 year = str_2_year(item->audio.year);
491 audio_svc_debug("year = %d", year);
492 _strncpy_safe(item->audio.year, AUDIO_SVC_TAG_UNKNOWN,
493 sizeof(item->audio.year));
496 ret = _audio_svc_get_and_append_folder_id_by_path(handle, item->pathname, item->storage_type, folder_id);
497 audio_svc_retv_if(ret != AUDIO_SVC_ERROR_NONE, ret);
499 file_name = g_path_get_basename( item->pathname);
503 ("insert into %s (%s) values ('%q', '%q', '%q', '%q', '%q', '%q','%q', '%q','%q',%d,'%q','%q','%q',%d,%d,%d,%d,%d,%d,%d,%d, %d, %d, %d, '%q', %d);",
504 AUDIO_SVC_DB_TABLE_AUDIO, g_audio_svc_music_fields,
514 str_2_year(item->audio.year),
515 item->audio.copyright,
516 item->audio.description,
520 item->audio.duration,
532 audio_svc_debug("query : %s", sql);
534 SAFE_FREE(file_name);
537 err = _audio_svc_sql_query(handle, sql);
539 if (err != SQLITE_OK) {
540 audio_svc_error("failed to insert music record");
542 if (err == AUDIO_SVC_ERROR_DB_CONNECT) {
545 return AUDIO_SVC_ERROR_DB_INTERNAL;
548 _audio_svc_sql_query_add(&g_audio_svc_insert_item_query_list, &sql);
551 //item->audio_uuid = sqlite3_last_insert_rowid(handle);
552 return AUDIO_SVC_ERROR_NONE;
555 int _audio_svc_select_music_record_by_audio_id(sqlite3 *handle, const char *audio_id,
556 audio_svc_audio_item_s *item)
558 int ret = AUDIO_SVC_ERROR_NONE;
559 sqlite3_stmt *sql_stmt = NULL;
563 ("select * from %s where audio_uuid='%q' and valid=1 and category=%d",
564 AUDIO_SVC_DB_TABLE_AUDIO, audio_id, AUDIO_SVC_CATEGORY_MUSIC);
566 ret = _audio_svc_sql_prepare_to_step(handle, sql, &sql_stmt);
568 if (ret != AUDIO_SVC_ERROR_NONE) {
569 audio_svc_error("error when _audio_svc_select_music_record_by_audio_id. ret = [%d]", ret);
573 __audio_svc_get_next_record(item, sql_stmt);
575 SQLITE3_FINALIZE(sql_stmt);
577 return AUDIO_SVC_ERROR_NONE;
580 int _audio_svc_select_music_record_by_path(sqlite3 *handle, const char *path,
581 audio_svc_audio_item_s *item)
583 int ret = AUDIO_SVC_ERROR_NONE;
584 sqlite3_stmt *sql_stmt = NULL;
587 sqlite3_mprintf("select * from %s where valid=1 and path='%q' ",
588 AUDIO_SVC_DB_TABLE_AUDIO, path);
590 ret = _audio_svc_sql_prepare_to_step(handle, sql, &sql_stmt);
592 if (ret != AUDIO_SVC_ERROR_NONE) {
594 ("error when _audio_svc_select_music_record_by_path. ret=[%d]",
599 __audio_svc_get_next_record(item, sql_stmt);
601 SQLITE3_FINALIZE(sql_stmt);
603 return AUDIO_SVC_ERROR_NONE;
606 int _audio_svc_delete_music_record_by_audio_id(sqlite3 *handle, const char *audio_id)
610 sqlite3_mprintf("delete from %s where valid=1 and audio_uuid='%q'",
611 AUDIO_SVC_DB_TABLE_AUDIO, audio_id);
613 err = _audio_svc_sql_query(handle, sql);
615 if (err != SQLITE_OK) {
616 audio_svc_error("It failed to delete item (%d)", err);
617 if (err == AUDIO_SVC_ERROR_DB_CONNECT) {
620 return AUDIO_SVC_ERROR_DB_INTERNAL;
622 return AUDIO_SVC_ERROR_NONE;
625 int _audio_svc_update_metadata_in_music_record(sqlite3 *handle, const char *audio_id,
626 audio_svc_audio_item_s *item)
631 ("update %s set thumbnail_path='%q',title='%q',album='%q',artist='%q',arist_seq='%q' ,genre='%q',author='%q',\
632 year=%d,copyright='%q',description='%q',format='%q',track_num=%d,duration=%d,bitrate=%d where valid=1 and audio_uuid='%q'",
633 AUDIO_SVC_DB_TABLE_AUDIO,
641 str_2_year(item->audio.year),
643 item->audio.copyright,
644 item->audio.description,
647 item->audio.track, item->audio.duration, item->audio.bitrate,
650 err = _audio_svc_sql_query(handle, sql);
652 if (err != SQLITE_OK) {
653 audio_svc_error("It failed to update metadata (%d)", err);
654 if (err == AUDIO_SVC_ERROR_DB_CONNECT) {
657 return AUDIO_SVC_ERROR_DB_INTERNAL;
660 return AUDIO_SVC_ERROR_NONE;
663 int _audio_svc_update_path_in_music_record(sqlite3 *handle, const char *src_path,
664 const char *path, const char *title)
669 if (STRING_VALID(title)) {
672 ("update %s set path='%q', title='%q', where valid=1 and path='%q'",
673 AUDIO_SVC_DB_TABLE_AUDIO, path, title, src_path);
677 ("update %s set path='%q', where valid=1 and path='%q'",
678 AUDIO_SVC_DB_TABLE_AUDIO, path, src_path);
681 err = _audio_svc_sql_query(handle, sql);
683 if (err != SQLITE_OK) {
684 audio_svc_error("It failed to update metadata (%d)", err);
685 if (err == AUDIO_SVC_ERROR_DB_CONNECT) {
688 return AUDIO_SVC_ERROR_DB_INTERNAL;
691 return AUDIO_SVC_ERROR_NONE;
694 int _audio_svc_update_path_and_storage_in_music_record(sqlite3 *handle, const char *src_path, const char *path, audio_svc_storage_type_e storage_type)
697 char *sql = sqlite3_mprintf("update %s set path='%q', storage_type=%d where valid=1 and path='%q'",
698 AUDIO_SVC_DB_TABLE_AUDIO, path, storage_type, src_path);
700 err = _audio_svc_sql_query(handle, sql);
702 if (err != SQLITE_OK) {
703 audio_svc_error("It failed to update metadata (%d)", err);
704 if (err == AUDIO_SVC_ERROR_DB_CONNECT) {
707 return AUDIO_SVC_ERROR_DB_INTERNAL;
710 return AUDIO_SVC_ERROR_NONE;
713 int _audio_svc_update_folder_id_in_music_record(sqlite3 *handle, const char *path, const char *folder_id)
716 char *sql = sqlite3_mprintf("update %s set folder_uuid='%q' where path='%q'",
717 AUDIO_SVC_DB_TABLE_AUDIO, folder_id, path);
719 err = _audio_svc_sql_query(handle, sql);
721 if (err != SQLITE_OK) {
722 audio_svc_error("It failed to update metadata (%d)", err);
723 if (err == AUDIO_SVC_ERROR_DB_CONNECT) {
726 return AUDIO_SVC_ERROR_DB_INTERNAL;
729 return AUDIO_SVC_ERROR_NONE;
732 int _audio_svc_update_thumb_path_in_music_record(sqlite3 *handle, const char *file_path,
739 ("update %s set thumbnail_path='%q' where valid=1 and path='%q'",
740 AUDIO_SVC_DB_TABLE_AUDIO, path, file_path);
742 err = _audio_svc_sql_query(handle, sql);
744 if (err != SQLITE_OK) {
745 audio_svc_error("It failed to update thumb path (%d)", err);
746 if (err == AUDIO_SVC_ERROR_DB_CONNECT) {
749 return AUDIO_SVC_ERROR_DB_INTERNAL;
751 return AUDIO_SVC_ERROR_NONE;
754 int _audio_svc_update_rating_in_music_record(sqlite3 *handle, const char *audio_id, int changed_value)
758 rated_time = time(NULL);
762 ("update %s set rating=%d, rated_time=%d where valid=1 and audio_uuid='%q'",
763 AUDIO_SVC_DB_TABLE_AUDIO, changed_value, rated_time, audio_id);
765 err = _audio_svc_sql_query(handle, sql);
767 if (err != SQLITE_OK) {
768 audio_svc_debug("To update rating is failed(%d)", err);
769 if (err == AUDIO_SVC_ERROR_DB_CONNECT) {
772 return AUDIO_SVC_ERROR_DB_INTERNAL;
775 return AUDIO_SVC_ERROR_NONE;
778 int _audio_svc_update_playtime_in_music_record(sqlite3 *handle, const char *audio_id, int changed_value)
784 ("update %s set last_played_time=%d where valid=1 and audio_uuid='%q'",
785 AUDIO_SVC_DB_TABLE_AUDIO, changed_value, audio_id);
787 err = _audio_svc_sql_query(handle, sql);
789 if (err != SQLITE_OK) {
790 audio_svc_error("To update last_played_time is failed(%d)",
792 if (err == AUDIO_SVC_ERROR_DB_CONNECT) {
795 return AUDIO_SVC_ERROR_DB_INTERNAL;
798 return AUDIO_SVC_ERROR_NONE;
801 int _audio_svc_update_playcount_in_music_record(sqlite3 *handle, const char *audio_id, int changed_value)
807 ("update %s set played_count=%d where valid=1 and audio_uuid='%q'",
808 AUDIO_SVC_DB_TABLE_AUDIO, changed_value, audio_id);
810 err = _audio_svc_sql_query(handle, sql);
812 if (err != SQLITE_OK) {
813 audio_svc_error("To update played count is failed(%d)", err);
814 if (err == AUDIO_SVC_ERROR_DB_CONNECT) {
817 return AUDIO_SVC_ERROR_DB_INTERNAL;
820 return AUDIO_SVC_ERROR_NONE;
823 int _audio_svc_update_addtime_in_music_record(sqlite3 *handle, const char *audio_id, int changed_value)
828 ("update %s set added_time=%d where valid=1 and audio_uuid='%q'",
829 AUDIO_SVC_DB_TABLE_AUDIO, changed_value, audio_id);
830 err = _audio_svc_sql_query(handle, sql);
832 if (err != SQLITE_OK) {
833 audio_svc_error("To update added_time is failed(%d)", err);
834 if (err == AUDIO_SVC_ERROR_DB_CONNECT) {
837 return AUDIO_SVC_ERROR_DB_INTERNAL;
840 return AUDIO_SVC_ERROR_NONE;
843 int _audio_svc_update_track_num_in_music_record(sqlite3 *handle, const char *audio_id, int changed_value)
848 ("update %s set track_num=%d where valid=1 and audio_uuid='%q'",
849 AUDIO_SVC_DB_TABLE_AUDIO, changed_value, audio_id);
850 err = _audio_svc_sql_query(handle, sql);
852 if (err != SQLITE_OK) {
853 audio_svc_error("update track num is failed(%d)", err);
854 if (err == AUDIO_SVC_ERROR_DB_CONNECT) {
857 return AUDIO_SVC_ERROR_DB_INTERNAL;
860 return AUDIO_SVC_ERROR_NONE;
863 int _audio_svc_update_album_rating_in_music_record(sqlite3 *handle, const char *audio_id,
869 ("update %s set album_rating=%d where valid=1 and audio_uuid='%q'",
870 AUDIO_SVC_DB_TABLE_AUDIO, changed_value, audio_id);
871 err = _audio_svc_sql_query(handle, sql);
873 if (err != SQLITE_OK) {
874 audio_svc_error("update album_rating is failed(%d)", err);
875 if (err == AUDIO_SVC_ERROR_DB_CONNECT) {
878 return AUDIO_SVC_ERROR_DB_INTERNAL;
881 return AUDIO_SVC_ERROR_NONE;
884 int _audio_svc_update_year_in_music_record(sqlite3 *handle, const char *audio_id, int changed_value)
889 ("update %s set year=%d where valid=1 and audio_uuid='%q'",
890 AUDIO_SVC_DB_TABLE_AUDIO, changed_value, audio_id);
891 err = _audio_svc_sql_query(handle, sql);
893 if (err != SQLITE_OK) {
894 audio_svc_error("update year is failed(%d)", err);
895 if (err == AUDIO_SVC_ERROR_DB_CONNECT) {
898 return AUDIO_SVC_ERROR_DB_INTERNAL;
901 return AUDIO_SVC_ERROR_NONE;
904 int _audio_svc_update_title_in_music_record(sqlite3 *handle, const char *audio_id,
905 const char *changed_value)
910 ("update %s set title='%q' where valid=1 and audio_uuid='%q'",
911 AUDIO_SVC_DB_TABLE_AUDIO, changed_value, audio_id);
912 err = _audio_svc_sql_query(handle, sql);
914 if (err != SQLITE_OK) {
915 audio_svc_error("To update title is failed(%d)", err);
916 if (err == AUDIO_SVC_ERROR_DB_CONNECT) {
919 return AUDIO_SVC_ERROR_DB_INTERNAL;
922 return AUDIO_SVC_ERROR_NONE;
925 int _audio_svc_update_artist_in_music_record(sqlite3 *handle, const char *audio_id,
926 const char *changed_value)
931 ("update %s set artist='%q' where valid=1 and audio_uuid='%q'",
932 AUDIO_SVC_DB_TABLE_AUDIO, changed_value, audio_id);
933 err = _audio_svc_sql_query(handle, sql);
935 if (err != SQLITE_OK) {
936 audio_svc_error("To update artist is failed(%d)", err);
937 if (err == AUDIO_SVC_ERROR_DB_CONNECT) {
940 return AUDIO_SVC_ERROR_DB_INTERNAL;
943 return AUDIO_SVC_ERROR_NONE;
946 int _audio_svc_update_album_in_music_record(sqlite3 *handle, const char *audio_id,
947 const char *changed_value)
952 ("update %s set album='%q' where valid=1 and audio_uuid='%q'",
953 AUDIO_SVC_DB_TABLE_AUDIO, changed_value, audio_id);
954 err = _audio_svc_sql_query(handle, sql);
956 if (err != SQLITE_OK) {
957 audio_svc_error("To update album is failed(%d)", err);
958 if (err == AUDIO_SVC_ERROR_DB_CONNECT) {
961 return AUDIO_SVC_ERROR_DB_INTERNAL;
964 return AUDIO_SVC_ERROR_NONE;
967 int _audio_svc_update_genre_in_music_record(sqlite3 *handle, const char *audio_id,
968 const char *changed_value)
973 ("update %s set genre='%q' where valid=1 and audio_uuid='%q'",
974 AUDIO_SVC_DB_TABLE_AUDIO, changed_value, audio_id);
975 err = _audio_svc_sql_query(handle, sql);
977 if (err != SQLITE_OK) {
978 audio_svc_error("To update genre is failed(%d)", err);
979 if (err == AUDIO_SVC_ERROR_DB_CONNECT) {
982 return AUDIO_SVC_ERROR_DB_INTERNAL;
985 return AUDIO_SVC_ERROR_NONE;
988 int _audio_svc_update_author_in_music_record(sqlite3 *handle, const char *audio_id,
989 const char *changed_value)
994 ("update %s set author='%q' where valid=1 and audio_uuid='%q'",
995 AUDIO_SVC_DB_TABLE_AUDIO, changed_value, audio_id);
996 err = _audio_svc_sql_query(handle, sql);
998 if (err != SQLITE_OK) {
999 audio_svc_error("To update author is failed(%d)", err);
1000 if (err == AUDIO_SVC_ERROR_DB_CONNECT) {
1003 return AUDIO_SVC_ERROR_DB_INTERNAL;
1006 return AUDIO_SVC_ERROR_NONE;
1009 int _audio_svc_update_description_in_music_record(sqlite3 *handle, const char *audio_id,
1010 const char *changed_value)
1015 ("update %s set description='%q' where valid=1 and audio_uuid='%q'",
1016 AUDIO_SVC_DB_TABLE_AUDIO, changed_value, audio_id);
1017 err = _audio_svc_sql_query(handle, sql);
1019 if (err != SQLITE_OK) {
1020 audio_svc_error("To update description is failed(%d)", err);
1021 if (err == AUDIO_SVC_ERROR_DB_CONNECT) {
1024 return AUDIO_SVC_ERROR_DB_INTERNAL;
1027 return AUDIO_SVC_ERROR_NONE;
1030 int _audio_svc_update_favourite_in_music_record(sqlite3 *handle, const char *audio_id, int changed_value)
1033 char *sql = sqlite3_mprintf("update %s set favourite=%d where valid=1 and audio_uuid='%q'",
1034 AUDIO_SVC_DB_TABLE_AUDIO, changed_value, audio_id);
1035 err = _audio_svc_sql_query(handle, sql);
1037 if (err != SQLITE_OK) {
1038 audio_svc_error("update album_rating is failed(%d)", err);
1039 if (err == AUDIO_SVC_ERROR_DB_CONNECT) {
1042 return AUDIO_SVC_ERROR_DB_INTERNAL;
1045 return AUDIO_SVC_ERROR_NONE;
1048 int _audio_svc_count_music_group_records(sqlite3 *handle, audio_svc_group_type_e group_type,
1049 const char *limit_string1,
1050 const char *limit_string2,
1051 const char *filter_string,
1052 const char *filter_string2, int *count)
1054 char query[AUDIO_SVC_QUERY_SIZE] = { 0 };
1055 char filter_query[AUDIO_SVC_METADATA_LEN_MAX + 5] = { 0 };
1056 char filter_query2[AUDIO_SVC_METADATA_LEN_MAX + 5] = { 0 };
1057 bool filter_mode = FALSE;
1058 bool filter_mode2 = FALSE;
1062 sqlite3_stmt *sql_stmt = NULL;
1064 if (filter_string) {
1065 if (strlen(filter_string) > 0) {
1066 snprintf(filter_query, sizeof(filter_query), "%%%s%%",
1071 if (filter_string2) {
1072 if (strlen(filter_string2) > 0) {
1073 snprintf(filter_query2, sizeof(filter_query2), "%%%s%%",
1075 filter_mode2 = TRUE;
1079 switch (group_type) {
1080 case AUDIO_SVC_GROUP_BY_ALBUM:
1082 snprintf(query, sizeof(query),
1083 "select count(distinct album) from %s where valid=1 and category=%d",
1084 AUDIO_SVC_DB_TABLE_AUDIO,
1085 AUDIO_SVC_CATEGORY_MUSIC);
1087 g_strlcat(query, " and album like ?",
1091 g_strlcat(query, " and album like ?",
1098 case AUDIO_SVC_GROUP_BY_ARTIST:
1100 snprintf(query, sizeof(query),
1101 "select count(distinct artist) from %s where valid=1 and category=%d",
1102 AUDIO_SVC_DB_TABLE_AUDIO,
1103 AUDIO_SVC_CATEGORY_MUSIC);
1105 g_strlcat(query, " and artist like ?",
1109 g_strlcat(query, " and artist like ?",
1116 case AUDIO_SVC_GROUP_BY_GENRE:
1118 snprintf(query, sizeof(query),
1119 "select count(distinct genre) from %s where valid=1 and category=%d",
1120 AUDIO_SVC_DB_TABLE_AUDIO,
1121 AUDIO_SVC_CATEGORY_MUSIC);
1123 g_strlcat(query, " and genre like ?",
1127 g_strlcat(query, " and genre like ?",
1134 case AUDIO_SVC_GROUP_BY_FOLDER:
1136 snprintf(query, sizeof(query),
1137 "select count(distinct folder_uuid) from %s where valid=1 and category=%d",
1138 AUDIO_SVC_DB_TABLE_AUDIO,
1139 AUDIO_SVC_CATEGORY_MUSIC);
1140 /*FIX ME. if filter_mode exist. */
1142 snprintf(query, sizeof(query),
1143 "select count(distinct a.folder_uuid) from %s a inner join %s b on a.folder_uuid=b.folder_uuid \
1144 where a.valid=1 and a.category=%d and b.path like ?",
1145 AUDIO_SVC_DB_TABLE_AUDIO, AUDIO_SVC_DB_TABLE_AUDIO_FOLDER, AUDIO_SVC_CATEGORY_MUSIC);
1148 g_strlcat(query, " and b.path like ?",
1154 case AUDIO_SVC_GROUP_BY_YEAR:
1156 snprintf(query, sizeof(query),
1157 "select count(distinct year) from %s where valid=1 and category=%d",
1158 AUDIO_SVC_DB_TABLE_AUDIO,
1159 AUDIO_SVC_CATEGORY_MUSIC);
1161 g_strlcat(query, " and year like ?",
1165 g_strlcat(query, " and year like ?",
1172 case AUDIO_SVC_GROUP_BY_COMPOSER:
1174 snprintf(query, sizeof(query),
1175 "select count(distinct author) from %s where valid=1 and category=%d",
1176 AUDIO_SVC_DB_TABLE_AUDIO,
1177 AUDIO_SVC_CATEGORY_MUSIC);
1179 g_strlcat(query, " and author like ?",
1183 g_strlcat(query, " and author like ?",
1190 case AUDIO_SVC_GROUP_BY_ARTIST_ALBUM:
1192 snprintf(query, sizeof(query),
1193 "select count(distinct album) from %s where valid=1 and category=%d",
1194 AUDIO_SVC_DB_TABLE_AUDIO,
1195 AUDIO_SVC_CATEGORY_MUSIC);
1196 if (limit_string1 && strlen(limit_string1) > 0) {
1197 g_strlcat(query, " and artist=?",
1200 g_strlcat(query, " and artist is null",
1204 g_strlcat(query, " and album like ?",
1208 g_strlcat(query, " and album like ?",
1215 case AUDIO_SVC_GROUP_BY_GENRE_ARTIST:
1217 snprintf(query, sizeof(query),
1218 "select count(distinct artist) from %s where valid=1 and category=%d",
1219 AUDIO_SVC_DB_TABLE_AUDIO,
1220 AUDIO_SVC_CATEGORY_MUSIC);
1221 if (limit_string1 && strlen(limit_string1) > 0) {
1222 g_strlcat(query, " and genre=?", sizeof(query));
1224 g_strlcat(query, " and genre is null",
1228 g_strlcat(query, " and artist like ?",
1232 g_strlcat(query, " and artist like ?",
1239 case AUDIO_SVC_GROUP_BY_GENRE_ALBUM:
1241 snprintf(query, sizeof(query),
1242 "select count(distinct album) from %s where valid=1 and category=%d",
1243 AUDIO_SVC_DB_TABLE_AUDIO,
1244 AUDIO_SVC_CATEGORY_MUSIC);
1245 if (limit_string1 && strlen(limit_string1) > 0) {
1246 g_strlcat(query, " and genre=?", sizeof(query));
1248 g_strlcat(query, " and genre is null",
1252 g_strlcat(query, " and album like ?",
1256 g_strlcat(query, " and album like ?",
1263 case AUDIO_SVC_GROUP_BY_GENRE_ARTIST_ALBUM:
1265 snprintf(query, sizeof(query),
1266 "select count(distinct album) from %s where valid=1 and category=%d",
1267 AUDIO_SVC_DB_TABLE_AUDIO,
1268 AUDIO_SVC_CATEGORY_MUSIC);
1269 if (limit_string1 && strlen(limit_string1) > 0) {
1270 if (limit_string2 && strlen(limit_string2) > 0) {
1272 " and genre=? and artist=?",
1276 " and genre=? and artist is null",
1280 if (limit_string2 && strlen(limit_string2) > 0) {
1282 " and genre is null and artist=?",
1286 " and genre is null and artist is null",
1292 g_strlcat(query, " and album like ?",
1296 g_strlcat(query, " and album like ?",
1305 audio_svc_error("Wrong type [%d]", group_type);
1306 return AUDIO_SVC_ERROR_INVALID_PARAMETER;
1310 audio_svc_debug("[SQL query] : %s", query);
1312 err = sqlite3_prepare_v2(handle, query, -1, &sql_stmt, NULL);
1313 if (err != SQLITE_OK) {
1314 audio_svc_error("prepare error [%s]", sqlite3_errmsg(handle));
1315 return AUDIO_SVC_ERROR_DB_INTERNAL;
1318 if (group_type != AUDIO_SVC_GROUP_BY_ALBUM
1319 && group_type != AUDIO_SVC_GROUP_BY_ARTIST
1320 && group_type != AUDIO_SVC_GROUP_BY_GENRE
1321 && group_type != AUDIO_SVC_GROUP_BY_FOLDER) {
1322 if (limit_string1 && strlen(limit_string1) > 0) {
1325 sqlite3_bind_text(sql_stmt, text_bind,
1326 (char *)limit_string1,
1327 strlen(limit_string1), NULL))) {
1329 ("limit_string(%s) binding is failed (%d)",
1330 limit_string1, err);
1331 SQLITE3_FINALIZE(sql_stmt);
1332 return AUDIO_SVC_ERROR_DB_INTERNAL;
1337 if (limit_string2 && strlen(limit_string2) > 0
1338 && group_type == AUDIO_SVC_GROUP_BY_GENRE_ARTIST_ALBUM) {
1341 sqlite3_bind_text(sql_stmt, text_bind,
1342 (char *)limit_string2,
1343 strlen(limit_string2), NULL))) {
1345 ("limit_string2(%s) binding is failed (%d)",
1346 limit_string2, err);
1347 SQLITE3_FINALIZE(sql_stmt);
1348 return AUDIO_SVC_ERROR_DB_INTERNAL;
1357 sqlite3_bind_text(sql_stmt, text_bind,
1358 (char *)filter_query,
1359 strlen(filter_query), NULL))) {
1361 ("filter_query(%s) binding is failed (%d)",
1363 SQLITE3_FINALIZE(sql_stmt);
1364 return AUDIO_SVC_ERROR_DB_INTERNAL;
1371 sqlite3_bind_text(sql_stmt, text_bind,
1372 (char *)filter_query2,
1373 strlen(filter_query2), NULL))) {
1375 ("filter_query(%s) binding is failed (%d)",
1376 filter_query2, err);
1377 SQLITE3_FINALIZE(sql_stmt);
1378 return AUDIO_SVC_ERROR_DB_INTERNAL;
1382 err = sqlite3_step(sql_stmt);
1383 if (err != SQLITE_ROW) {
1384 audio_svc_error("end of row [%s]", sqlite3_errmsg(handle));
1385 SQLITE3_FINALIZE(sql_stmt);
1386 return AUDIO_SVC_ERROR_DB_INTERNAL;
1389 *count = sqlite3_column_int(sql_stmt, 0);
1391 SQLITE3_FINALIZE(sql_stmt);
1393 return AUDIO_SVC_ERROR_NONE;
1397 int _audio_svc_get_music_group_records(sqlite3 *handle, audio_svc_group_type_e group_type,
1398 const char *limit_string1,
1399 const char *limit_string2,
1400 const char *filter_string,
1401 const char *filter_string2, int offset,
1402 int rows, audio_svc_group_item_s *group)
1404 char query[AUDIO_SVC_QUERY_SIZE] = { 0 };
1405 char tail_query[100] = { 0 };
1406 char filter_query[AUDIO_SVC_METADATA_LEN_MAX + 5] = { 0 };
1407 char filter_query2[AUDIO_SVC_METADATA_LEN_MAX + 5] = { 0 };
1408 bool filter_mode = FALSE;
1409 bool filter_mode2 = FALSE;
1414 sqlite3_stmt *sql_stmt = NULL;
1416 if (filter_string) {
1417 if (strlen(filter_string) > 0) {
1418 snprintf(filter_query, sizeof(filter_query), "%%%s%%",
1423 if (filter_string2) {
1424 if (strlen(filter_string2) > 0) {
1425 snprintf(filter_query2, sizeof(filter_query2), "%%%s%%",
1427 filter_mode2 = TRUE;
1431 switch (group_type) {
1432 case AUDIO_SVC_GROUP_BY_ALBUM:
1434 snprintf(query, sizeof(query),
1435 "select album, min(audio_uuid), artist, thumbnail_path, album_rating from %s where valid=1 and category=%d",
1436 AUDIO_SVC_DB_TABLE_AUDIO,
1437 AUDIO_SVC_CATEGORY_MUSIC);
1440 g_strlcat(query, " and album like ?",
1444 g_strlcat(query, " and album like ?",
1448 snprintf(tail_query, sizeof(tail_query), " group by album %s limit %d,%d", AUDIO_SVC_ORDER_BY_ALBUM, offset, rows);
1449 g_strlcat(query, tail_query, sizeof(query));
1453 case AUDIO_SVC_GROUP_BY_ARTIST:
1455 snprintf(query, sizeof(query),
1456 "select artist, min(audio_uuid), album, thumbnail_path, album_rating from %s where valid=1 and category=%d",
1457 AUDIO_SVC_DB_TABLE_AUDIO,
1458 AUDIO_SVC_CATEGORY_MUSIC);
1461 g_strlcat(query, " and artist like ? ",
1465 g_strlcat(query, " and artist like ? ",
1469 snprintf(tail_query, sizeof(tail_query), " group by artist %s limit %d,%d", AUDIO_SVC_ORDER_BY_ARTIST, offset, rows);
1470 g_strlcat(query, tail_query, sizeof(query));
1474 case AUDIO_SVC_GROUP_BY_ARTIST_ALBUM:
1476 if (limit_string1 && strlen(limit_string1) > 0) {
1477 snprintf(query, sizeof(query),
1478 "select album, min(audio_uuid), artist, thumbnail_path, album_rating \
1479 from %s where valid=1 and category=%d and artist=?",
1480 AUDIO_SVC_DB_TABLE_AUDIO, AUDIO_SVC_CATEGORY_MUSIC);
1482 snprintf(query, sizeof(query),
1483 "select album, min(audio_uuid), artist, thumbnail_path, album_rating \
1484 from %s where valid=1 and category=%d and artist is null",
1485 AUDIO_SVC_DB_TABLE_AUDIO, AUDIO_SVC_CATEGORY_MUSIC);
1488 g_strlcat(query, " and album like ?",
1492 g_strlcat(query, " and album like ?",
1496 snprintf(tail_query, sizeof(tail_query), " group by album %s limit %d,%d", AUDIO_SVC_ORDER_BY_ALBUM, offset, rows);
1497 g_strlcat(query, tail_query, sizeof(query));
1501 case AUDIO_SVC_GROUP_BY_GENRE:
1503 snprintf(query, sizeof(query),
1504 "select genre, min(audio_uuid), album, thumbnail_path, album_rating from %s where valid=1 and category=%d",
1505 AUDIO_SVC_DB_TABLE_AUDIO,
1506 AUDIO_SVC_CATEGORY_MUSIC);
1509 g_strlcat(query, " and genre like ?",
1513 g_strlcat(query, " and genre like ?",
1517 snprintf(tail_query, sizeof(tail_query), " group by genre %s limit %d,%d", AUDIO_SVC_ORDER_BY_GENRE, offset, rows);
1518 g_strlcat(query, tail_query, sizeof(query));
1522 case AUDIO_SVC_GROUP_BY_FOLDER:
1524 snprintf(query, sizeof(query),
1525 "select b.folder_name, min(a.audio_uuid), (b.path), (a.thumbnail_path), (a.album_rating) \
1526 from %s as a inner join %s b on a.folder_uuid=b.folder_uuid where a.valid=1 and a.category=%d",
1527 AUDIO_SVC_DB_TABLE_AUDIO, AUDIO_SVC_DB_TABLE_AUDIO_FOLDER, AUDIO_SVC_CATEGORY_MUSIC);
1529 snprintf(query, sizeof(query),
1530 "select b.folder_name, min(a.audio_uuid), (b.path), (a.thumbnail_path), (a.album_rating) \
1531 from %s as a inner join %s b on a.folder_uuid=b.folder_uuid where a.valid=1 and a.category=%d and b.path like ?",
1532 AUDIO_SVC_DB_TABLE_AUDIO, AUDIO_SVC_DB_TABLE_AUDIO_FOLDER, AUDIO_SVC_CATEGORY_MUSIC);
1535 snprintf(query, sizeof(query),
1536 "select b.folder_name, min(a.audio_uuid), (b.path), (a.thumbnail_path), (a.album_rating) \
1537 from %s as a inner join %s b on a.folder_uuid=b.folder_uuid where a.valid=1 and a.category=%d and b.path like ? and b.path like ?",
1538 AUDIO_SVC_DB_TABLE_AUDIO, AUDIO_SVC_DB_TABLE_AUDIO_FOLDER, AUDIO_SVC_CATEGORY_MUSIC);
1541 snprintf(tail_query, sizeof(tail_query),
1542 " group by a.folder_uuid order by b.folder_name %s, b.path %s limit %d,%d",
1543 AUDIO_SVC_COLLATE_NOCASE, AUDIO_SVC_COLLATE_NOCASE, offset, rows);
1544 g_strlcat(query, tail_query, sizeof(query));
1548 case AUDIO_SVC_GROUP_BY_YEAR:
1550 snprintf(query, sizeof(query),
1551 "select year, min(audio_uuid), album, thumbnail_path, album_rating from %s where valid=1 and category=%d",
1552 AUDIO_SVC_DB_TABLE_AUDIO,
1553 AUDIO_SVC_CATEGORY_MUSIC);
1556 g_strlcat(query, " and year like ?",
1560 g_strlcat(query, " and year like ?",
1564 snprintf(tail_query, sizeof(tail_query),
1565 " group by year order by year desc limit %d,%d",
1567 g_strlcat(query, tail_query, sizeof(query));
1571 case AUDIO_SVC_GROUP_BY_COMPOSER:
1573 snprintf(query, sizeof(query),
1574 "select author, min(audio_uuid), album, thumbnail_path, album_rating from %s where valid=1 and category=%d",
1575 AUDIO_SVC_DB_TABLE_AUDIO,
1576 AUDIO_SVC_CATEGORY_MUSIC);
1579 g_strlcat(query, " and author like ?",
1583 g_strlcat(query, " and author like ?",
1587 snprintf(tail_query, sizeof(tail_query), " group by author %s limit %d,%d", AUDIO_SVC_ORDER_BY_AUTHOR, offset, rows);
1588 g_strlcat(query, tail_query, sizeof(query));
1592 case AUDIO_SVC_GROUP_BY_GENRE_ARTIST:
1594 if (limit_string1 && strlen(limit_string1) > 0) {
1595 snprintf(query, sizeof(query),
1596 "select artist, min(audio_uuid), album, thumbnail_path, album_rating \
1597 from %s where valid=1 and category=%d and genre=?",
1598 AUDIO_SVC_DB_TABLE_AUDIO, AUDIO_SVC_CATEGORY_MUSIC);
1600 snprintf(query, sizeof(query),
1601 "select artist, min(audio_uuid), album, thumbnail_path, album_rating \
1602 from %s where valid=1 and category=%d and genre is null",
1603 AUDIO_SVC_DB_TABLE_AUDIO, AUDIO_SVC_CATEGORY_MUSIC);
1606 g_strlcat(query, " and artist like ?",
1610 g_strlcat(query, " and artist like ?",
1614 snprintf(tail_query, sizeof(tail_query), " group by artist %s limit %d,%d", AUDIO_SVC_ORDER_BY_ARTIST, offset, rows);
1615 g_strlcat(query, tail_query, sizeof(query));
1619 case AUDIO_SVC_GROUP_BY_GENRE_ALBUM:
1621 if (limit_string1 && strlen(limit_string1) > 0) {
1622 snprintf(query, sizeof(query),
1623 "select distinct album, min(audio_uuid), artist, thumbnail_path, album_rating \
1624 from %s where valid=1 and category=%d and genre=?",
1625 AUDIO_SVC_DB_TABLE_AUDIO, AUDIO_SVC_CATEGORY_MUSIC);
1627 snprintf(query, sizeof(query),
1628 "select distinct album, min(audio_uuid), artist, thumbnail_path, album_rating \
1629 from %s where valid=1 and category=%d and genre is null",
1630 AUDIO_SVC_DB_TABLE_AUDIO, AUDIO_SVC_CATEGORY_MUSIC);
1633 g_strlcat(query, " and album like ?",
1637 g_strlcat(query, " and album like ?",
1641 snprintf(tail_query, sizeof(tail_query), " group by album %s limit %d,%d", AUDIO_SVC_ORDER_BY_ALBUM, offset, rows);
1642 g_strlcat(query, tail_query, sizeof(query));
1646 case AUDIO_SVC_GROUP_BY_GENRE_ARTIST_ALBUM:
1648 if (limit_string1 && strlen(limit_string1) > 0) {
1649 if (limit_string2 && strlen(limit_string2) > 0) {
1650 snprintf(query, sizeof(query),
1651 "select album, min(audio_uuid), artist, thumbnail_path, album_rating \
1652 from %s where valid=1 and category=%d and genre=? and artist=?",
1653 AUDIO_SVC_DB_TABLE_AUDIO, AUDIO_SVC_CATEGORY_MUSIC);
1655 snprintf(query, sizeof(query),
1656 "select album, min(audio_uuid), artist, thumbnail_path, album_rating \
1657 from %s where valid=1 and category=%d and genre=? and artist is null",
1658 AUDIO_SVC_DB_TABLE_AUDIO, AUDIO_SVC_CATEGORY_MUSIC);
1661 if (limit_string2 && strlen(limit_string2) > 0) {
1662 snprintf(query, sizeof(query),
1663 "select album, min(audio_uuid), artist, thumbnail_path, album_rating \
1664 from %s where valid=1 and category=%d and genre is null and artist=?",
1665 AUDIO_SVC_DB_TABLE_AUDIO, AUDIO_SVC_CATEGORY_MUSIC);
1667 snprintf(query, sizeof(query),
1668 "select album, min(audio_uuid), artist, thumbnail_path, album_rating \
1669 from %s where valid=1 and category=%d and genre is null and artist is null",
1670 AUDIO_SVC_DB_TABLE_AUDIO, AUDIO_SVC_CATEGORY_MUSIC);
1675 g_strlcat(query, " and album like ?",
1679 g_strlcat(query, " and album like ?",
1683 snprintf(tail_query, sizeof(tail_query), " group by album %s limit %d,%d", AUDIO_SVC_ORDER_BY_ALBUM, offset, rows);
1684 g_strlcat(query, tail_query, sizeof(query));
1690 audio_svc_debug("[SQL query] : %s", query);
1692 err = sqlite3_prepare_v2(handle, query, -1, &sql_stmt, NULL);
1693 if (err != SQLITE_OK) {
1694 audio_svc_error("prepare error [%s]", sqlite3_errmsg(handle));
1695 return AUDIO_SVC_ERROR_DB_INTERNAL;
1698 if ((limit_string1) && strlen(limit_string1)
1699 && (group_type != AUDIO_SVC_GROUP_BY_ALBUM)
1700 && (group_type != AUDIO_SVC_GROUP_BY_ARTIST)) {
1703 sqlite3_bind_text(sql_stmt, text_bind,
1704 (char *)limit_string1,
1705 strlen(limit_string1), NULL))) {
1707 ("limit_string1(%s) binding is failed (%d)",
1708 limit_string1, err);
1709 SQLITE3_FINALIZE(sql_stmt);
1710 return AUDIO_SVC_ERROR_DB_INTERNAL;
1715 if ((limit_string2) && strlen(limit_string2)
1716 && (group_type == AUDIO_SVC_GROUP_BY_GENRE_ARTIST_ALBUM)) {
1719 sqlite3_bind_text(sql_stmt, text_bind,
1720 (char *)limit_string2,
1721 strlen(limit_string2), NULL))) {
1723 ("limit_string2(%s) binding is failed (%d)",
1724 limit_string2, err);
1725 SQLITE3_FINALIZE(sql_stmt);
1726 return AUDIO_SVC_ERROR_DB_INTERNAL;
1734 sqlite3_bind_text(sql_stmt, text_bind,
1735 (char *)filter_query,
1736 strlen(filter_query), NULL))) {
1738 ("filter_query(%s) binding is failed (%d)",
1740 SQLITE3_FINALIZE(sql_stmt);
1741 return AUDIO_SVC_ERROR_DB_INTERNAL;
1748 sqlite3_bind_text(sql_stmt, text_bind,
1749 (char *)filter_query2,
1750 strlen(filter_query2), NULL))) {
1752 ("filter_query(%s) binding is failed (%d)",
1753 filter_query2, err);
1754 SQLITE3_FINALIZE(sql_stmt);
1755 return AUDIO_SVC_ERROR_DB_INTERNAL;
1760 while (sqlite3_step(sql_stmt) == SQLITE_ROW) {
1761 if (group_type == AUDIO_SVC_GROUP_BY_YEAR) {
1762 int year = sqlite3_column_int(sql_stmt, 0);
1763 if (year == -1 || year == 0) {
1764 _strncpy_safe(group[idx].maininfo,
1765 AUDIO_SVC_TAG_UNKNOWN,
1766 sizeof(group[idx].maininfo));
1768 _strncpy_safe(group[idx].maininfo,
1770 sqlite3_column_text(sql_stmt, 0),
1771 sizeof(group[idx].maininfo));
1774 _strncpy_safe(group[idx].maininfo,
1776 sqlite3_column_text(sql_stmt, 0),
1777 sizeof(group[idx].maininfo));
1780 _strncpy_safe(group[idx].subinfo,
1781 (const char *)sqlite3_column_text(sql_stmt, 2),
1782 sizeof(group[idx].subinfo));
1784 _strncpy_safe(group[idx].thumbnail_path,
1785 (const char *)sqlite3_column_text(sql_stmt, 3),
1786 sizeof(group[idx].thumbnail_path));
1788 group[idx].album_rating = sqlite3_column_int(sql_stmt, 4);
1793 SQLITE3_FINALIZE(sql_stmt);
1795 return AUDIO_SVC_ERROR_NONE;
1799 int _audio_svc_count_music_track_records(sqlite3 *handle, audio_svc_track_type_e track_type,
1800 const char *type_string,
1801 const char *type_string2,
1802 const char *filter_string,
1803 const char *filter_string2, int *count)
1805 char query[AUDIO_SVC_QUERY_SIZE] = { 0 };
1806 char filter_query[AUDIO_SVC_METADATA_LEN_MAX + 5] = { 0 };
1807 char filter_query2[AUDIO_SVC_METADATA_LEN_MAX + 5] = { 0 };
1808 bool filter_mode = FALSE;
1809 bool filter_mode2 = FALSE;
1813 sqlite3_stmt *sql_stmt = NULL;
1815 if (filter_string) {
1816 if (strlen(filter_string) > 0) {
1817 snprintf(filter_query, sizeof(filter_query), "%%%s%%",
1822 if (filter_string2) {
1823 if (strlen(filter_string2) > 0) {
1824 snprintf(filter_query2, sizeof(filter_query2), "%%%s%%",
1826 filter_mode2 = TRUE;
1830 switch (track_type) {
1831 case AUDIO_SVC_TRACK_ALL:
1832 snprintf(query, sizeof(query),
1833 "select count(*) from %s where valid=1 and category=%d",
1834 AUDIO_SVC_DB_TABLE_AUDIO, AUDIO_SVC_CATEGORY_MUSIC);
1837 case AUDIO_SVC_TRACK_BY_ALBUM:
1838 if (type_string && strlen(type_string) > 0) {
1839 snprintf(query, sizeof(query),
1840 "select count(*) from %s where valid=1 and category=%d and album=?",
1841 AUDIO_SVC_DB_TABLE_AUDIO,
1842 AUDIO_SVC_CATEGORY_MUSIC);
1844 snprintf(query, sizeof(query),
1845 "select count(*) from %s where valid=1 and category=%d and album is null",
1846 AUDIO_SVC_DB_TABLE_AUDIO,
1847 AUDIO_SVC_CATEGORY_MUSIC);
1851 case AUDIO_SVC_TRACK_BY_ARTIST_ALBUM:
1852 if (type_string && strlen(type_string) > 0) {
1853 if (type_string2 && strlen(type_string2) > 0) {
1854 snprintf(query, sizeof(query),
1855 "select count(*) from %s where valid=1 and category=%d and album=? and artist=?",
1856 AUDIO_SVC_DB_TABLE_AUDIO,
1857 AUDIO_SVC_CATEGORY_MUSIC);
1859 snprintf(query, sizeof(query),
1860 "select count(*) from %s where valid=1 and category=%d and album=? and artist is null",
1861 AUDIO_SVC_DB_TABLE_AUDIO,
1862 AUDIO_SVC_CATEGORY_MUSIC);
1865 if (type_string2 && strlen(type_string2) > 0) {
1866 snprintf(query, sizeof(query),
1867 "select count(*) from %s where valid=1 and category=%d and album is null and artist=?",
1868 AUDIO_SVC_DB_TABLE_AUDIO,
1869 AUDIO_SVC_CATEGORY_MUSIC);
1871 snprintf(query, sizeof(query),
1872 "select count(*) from %s where valid=1 and category=%d and album is null and artist is null",
1873 AUDIO_SVC_DB_TABLE_AUDIO,
1874 AUDIO_SVC_CATEGORY_MUSIC);
1880 case AUDIO_SVC_TRACK_BY_ARTIST:
1881 if (type_string && strlen(type_string) > 0) {
1882 snprintf(query, sizeof(query),
1883 "select count(*) from %s where valid=1 and category=%d and artist=?",
1884 AUDIO_SVC_DB_TABLE_AUDIO,
1885 AUDIO_SVC_CATEGORY_MUSIC);
1887 snprintf(query, sizeof(query),
1888 "select count(*) from %s where valid=1 and category=%d and artist is null",
1889 AUDIO_SVC_DB_TABLE_AUDIO,
1890 AUDIO_SVC_CATEGORY_MUSIC);
1894 case AUDIO_SVC_TRACK_BY_ARTIST_GENRE:
1895 if (type_string && strlen(type_string) > 0) {
1896 if (type_string2 && strlen(type_string2) > 0) {
1897 snprintf(query, sizeof(query),
1898 "select count(*) from %s where valid=1 and category=%d and genre=? and artist=?",
1899 AUDIO_SVC_DB_TABLE_AUDIO,
1900 AUDIO_SVC_CATEGORY_MUSIC);
1902 snprintf(query, sizeof(query),
1903 "select count(*) from %s where valid=1 and category=%d and genre=? and artist is null",
1904 AUDIO_SVC_DB_TABLE_AUDIO,
1905 AUDIO_SVC_CATEGORY_MUSIC);
1908 if (type_string2 && strlen(type_string2) > 0) {
1909 snprintf(query, sizeof(query),
1910 "select count(*) from %s where valid=1 and category=%d and genre is null and artist=?",
1911 AUDIO_SVC_DB_TABLE_AUDIO,
1912 AUDIO_SVC_CATEGORY_MUSIC);
1914 snprintf(query, sizeof(query),
1915 "select count(*) from %s where valid=1 and category=%d and genre is null and artist is null",
1916 AUDIO_SVC_DB_TABLE_AUDIO,
1917 AUDIO_SVC_CATEGORY_MUSIC);
1924 case AUDIO_SVC_TRACK_BY_GENRE:
1925 if (type_string && strlen(type_string) > 0) {
1926 snprintf(query, sizeof(query),
1927 "select count(*) from %s where valid=1 and category=%d and genre=?",
1928 AUDIO_SVC_DB_TABLE_AUDIO,
1929 AUDIO_SVC_CATEGORY_MUSIC);
1931 snprintf(query, sizeof(query),
1932 "select count(*) from %s where valid=1 and category=%d and genre is null",
1933 AUDIO_SVC_DB_TABLE_AUDIO,
1934 AUDIO_SVC_CATEGORY_MUSIC);
1938 case AUDIO_SVC_TRACK_BY_FOLDER:
1939 if (type_string && strlen(type_string) > 0) {
1940 snprintf(query, sizeof(query),
1941 "select count(*) from %s where valid=1 and category=%d and folder_uuid=(select folder_uuid from %s where path = ?)",
1942 AUDIO_SVC_DB_TABLE_AUDIO,
1943 AUDIO_SVC_CATEGORY_MUSIC,
1944 AUDIO_SVC_DB_TABLE_AUDIO_FOLDER);
1946 snprintf(query, sizeof(query),
1947 "select count(*) from %s where valid=1 and category=%d and folder_uuid is null",
1948 AUDIO_SVC_DB_TABLE_AUDIO,
1949 AUDIO_SVC_CATEGORY_MUSIC);
1953 case AUDIO_SVC_TRACK_BY_YEAR:
1954 if (type_string && strlen(type_string) > 0) {
1955 snprintf(query, sizeof(query),
1956 "select count(*) from %s where valid=1 and category=%d and year=?",
1957 AUDIO_SVC_DB_TABLE_AUDIO,
1958 AUDIO_SVC_CATEGORY_MUSIC);
1960 snprintf(query, sizeof(query),
1961 "select count(*) from %s where valid=1 and category=%d and year is null",
1962 AUDIO_SVC_DB_TABLE_AUDIO,
1963 AUDIO_SVC_CATEGORY_MUSIC);
1967 case AUDIO_SVC_TRACK_BY_COMPOSER:
1968 if (type_string && strlen(type_string) > 0) {
1969 snprintf(query, sizeof(query),
1970 "select count(*) from %s where valid=1 and category=%d and author=?",
1971 AUDIO_SVC_DB_TABLE_AUDIO,
1972 AUDIO_SVC_CATEGORY_MUSIC);
1974 snprintf(query, sizeof(query),
1975 "select count(*) from %s where valid=1 and category=%d and author is null",
1976 AUDIO_SVC_DB_TABLE_AUDIO,
1977 AUDIO_SVC_CATEGORY_MUSIC);
1981 case AUDIO_SVC_TRACK_BY_TOPRATING:
1982 snprintf(query, sizeof(query),
1983 "select count(*) from %s where valid=1 and category=%d and rating > %d",
1984 AUDIO_SVC_DB_TABLE_AUDIO, AUDIO_SVC_CATEGORY_MUSIC,
1985 AUDIO_SVC_RATING_3);
1988 case AUDIO_SVC_TRACK_BY_PLAYED_TIME:
1989 snprintf(query, sizeof(query),
1990 "select count(*) from %s where valid=1 and category=%d and last_played_time > 0",
1991 AUDIO_SVC_DB_TABLE_AUDIO, AUDIO_SVC_CATEGORY_MUSIC);
1994 case AUDIO_SVC_TRACK_BY_ADDED_TIME:
1995 snprintf(query, sizeof(query),
1996 "select count(*) from %s where valid=1 and category=%d and added_time > 0",
1997 AUDIO_SVC_DB_TABLE_AUDIO, AUDIO_SVC_CATEGORY_MUSIC);
2000 case AUDIO_SVC_TRACK_BY_PLAYED_COUNT:
2001 snprintf(query, sizeof(query),
2002 "select count(*) from %s where valid=1 and category=%d and played_count > 0",
2003 AUDIO_SVC_DB_TABLE_AUDIO, AUDIO_SVC_CATEGORY_MUSIC);
2006 case AUDIO_SVC_TRACK_BY_PLAYLIST:
2008 snprintf(query, sizeof(query),
2009 "select count(*) from %s a, %s b where a.playlist_id=%d and b.audio_uuid=a.audio_uuid and b.valid=1 and b.category=%d",
2010 AUDIO_SVC_DB_TABLE_AUDIO_PLAYLISTS_MAP,
2011 AUDIO_SVC_DB_TABLE_AUDIO, (int)type_string,
2012 AUDIO_SVC_CATEGORY_MUSIC);
2015 g_strlcat(query, " and b.title like ?",
2019 g_strlcat(query, " and b.title like ?",
2029 if (track_type != AUDIO_SVC_TRACK_BY_PLAYLIST) {
2031 g_strlcat(query, " and title like ?", sizeof(query));
2034 g_strlcat(query, " and title like ?", sizeof(query));
2037 audio_svc_debug("[SQL query] : %s", query);
2039 err = sqlite3_prepare_v2(handle, query, -1, &sql_stmt, NULL);
2040 if (err != SQLITE_OK) {
2041 audio_svc_error("prepare error [%s]", sqlite3_errmsg(handle));
2042 return AUDIO_SVC_ERROR_DB_INTERNAL;
2048 sqlite3_bind_text(sql_stmt, text_bind,
2049 (char *)filter_query,
2050 strlen(filter_query), NULL))) {
2052 ("filter_query(%s) binding is failed (%d)",
2054 SQLITE3_FINALIZE(sql_stmt);
2055 return AUDIO_SVC_ERROR_DB_INTERNAL;
2062 sqlite3_bind_text(sql_stmt, text_bind,
2063 (char *)filter_query2,
2064 strlen(filter_query2), NULL))) {
2066 ("filter_query(%s) binding is failed (%d)",
2067 filter_query2, err);
2068 SQLITE3_FINALIZE(sql_stmt);
2069 return AUDIO_SVC_ERROR_DB_INTERNAL;
2074 if ((track_type >= AUDIO_SVC_TRACK_BY_ALBUM)
2075 && (track_type <= AUDIO_SVC_TRACK_BY_COMPOSER)) {
2076 if (type_string && strlen(type_string) > 0) {
2077 if ((track_type == AUDIO_SVC_TRACK_BY_YEAR)
2078 && (!strcmp(type_string, AUDIO_SVC_TAG_UNKNOWN))) {
2081 sqlite3_bind_text(sql_stmt, text_bind,
2085 ("cont_string1(%s) binding is failed (%d)",
2087 SQLITE3_FINALIZE(sql_stmt);
2088 return AUDIO_SVC_ERROR_DB_INTERNAL;
2092 if ((track_type == AUDIO_SVC_TRACK_BY_ARTIST_GENRE)
2094 AUDIO_SVC_TRACK_BY_ARTIST_ALBUM)) {
2095 if (type_string2 && strlen(type_string2) > 0) {
2098 sqlite3_bind_text(sql_stmt,
2106 ("cont_string1(%s) binding is failed (%d)",
2108 SQLITE3_FINALIZE(sql_stmt);
2110 AUDIO_SVC_ERROR_DB_INTERNAL;
2115 sqlite3_bind_text(sql_stmt,
2123 ("cont_string2(%s) binding is failed (%d)",
2125 SQLITE3_FINALIZE(sql_stmt);
2127 AUDIO_SVC_ERROR_DB_INTERNAL;
2133 sqlite3_bind_text(sql_stmt,
2141 ("cont_string1(%s) binding is failed (%d)",
2143 SQLITE3_FINALIZE(sql_stmt);
2145 AUDIO_SVC_ERROR_DB_INTERNAL;
2151 sqlite3_bind_text(sql_stmt, text_bind,
2152 (char *)type_string,
2153 strlen(type_string),
2156 ("cont_string1(%s) binding is failed (%d)",
2158 SQLITE3_FINALIZE(sql_stmt);
2159 return AUDIO_SVC_ERROR_DB_INTERNAL;
2166 audio_svc_debug("[SQL query] : %s", query);
2168 err = sqlite3_step(sql_stmt);
2169 if (err != SQLITE_ROW) {
2170 audio_svc_error("end of row [%s]", sqlite3_errmsg(handle));
2171 SQLITE3_FINALIZE(sql_stmt);
2172 return AUDIO_SVC_ERROR_DB_INTERNAL;
2174 *count = sqlite3_column_int(sql_stmt, 0);
2176 SQLITE3_FINALIZE(sql_stmt);
2178 return AUDIO_SVC_ERROR_NONE;
2182 int _audio_svc_get_music_track_records(sqlite3 *handle, audio_svc_track_type_e track_type,
2183 const char *type_string,
2184 const char *type_string2,
2185 const char *filter_string,
2186 const char *filter_string2, int offset,
2187 int rows, audio_svc_list_item_s *track)
2189 char query[AUDIO_SVC_QUERY_SIZE] = { 0 };
2190 char *result_field = "audio_uuid, path, thumbnail_path, title, artist, duration, rating, album";
2192 char tail_query[70] = { 0 };
2193 char filter_query[AUDIO_SVC_METADATA_LEN_MAX + 5] = { 0 };
2194 char filter_query2[AUDIO_SVC_METADATA_LEN_MAX + 5] = { 0 };
2195 bool filter_mode = FALSE;
2196 bool filter_mode2 = FALSE;
2202 sqlite3_stmt *sql_stmt = NULL;
2204 #define filter_condition(filter_mode, filter_mode2, query) \
2205 if ((filter_mode)) g_strlcat((query), " and title like ?", sizeof((query))); \
2206 if ((filter_mode2)) g_strlcat((query), " and title like ?", sizeof((query)));
2208 filter_mode = STRING_VALID(filter_string);
2209 filter_mode2 = STRING_VALID(filter_string2);
2212 snprintf(filter_query, sizeof(filter_query), "%%%s%%",
2216 snprintf(filter_query2, sizeof(filter_query2), "%%%s%%",
2220 switch (track_type) {
2221 case AUDIO_SVC_TRACK_ALL:
2223 snprintf(query, sizeof(query),
2224 "select %s from %s where valid=1 and category=%d",
2225 result_field, AUDIO_SVC_DB_TABLE_AUDIO,
2226 AUDIO_SVC_CATEGORY_MUSIC);
2227 filter_condition(filter_mode, filter_mode2, query);
2228 snprintf(tail_query, sizeof(tail_query),
2229 " %s limit %d,%d", AUDIO_SVC_ORDER_BY_TITLE, offset, rows);
2230 g_strlcat(query, tail_query, sizeof(query));
2234 case AUDIO_SVC_TRACK_BY_ALBUM:
2236 if (type_string && strlen(type_string) > 0) {
2237 snprintf(query, sizeof(query),
2238 "select %s from %s where valid=1 and category=%d and album=?",
2239 result_field, AUDIO_SVC_DB_TABLE_AUDIO,
2240 AUDIO_SVC_CATEGORY_MUSIC);
2242 snprintf(query, sizeof(query),
2243 "select %s from %s where valid=1 and category=%d and album is null",
2244 result_field, AUDIO_SVC_DB_TABLE_AUDIO,
2245 AUDIO_SVC_CATEGORY_MUSIC);
2247 filter_condition(filter_mode, filter_mode2, query);
2248 snprintf(tail_query, sizeof(tail_query),
2249 " order by track_num limit %d,%d", offset,
2251 g_strlcat(query, tail_query, sizeof(query));
2255 case AUDIO_SVC_TRACK_BY_ARTIST_ALBUM:
2257 if (type_string && strlen(type_string) > 0) {
2258 if (type_string2 && strlen(type_string2) > 0) {
2259 snprintf(query, sizeof(query),
2260 "select %s from %s where valid=1 and category=%d and album=? and artist=?",
2262 AUDIO_SVC_DB_TABLE_AUDIO,
2263 AUDIO_SVC_CATEGORY_MUSIC);
2265 snprintf(query, sizeof(query),
2266 "select %s from %s where valid=1 and category=%d and album=? and artist is null",
2268 AUDIO_SVC_DB_TABLE_AUDIO,
2269 AUDIO_SVC_CATEGORY_MUSIC);
2272 if (type_string2 && strlen(type_string2) > 0) {
2273 snprintf(query, sizeof(query),
2274 "select %s from %s where valid=1 and category=%d and album is null and artist=?",
2276 AUDIO_SVC_DB_TABLE_AUDIO,
2277 AUDIO_SVC_CATEGORY_MUSIC);
2279 snprintf(query, sizeof(query),
2280 "select %s from %s where valid=1 and category=%d and album is null and artist is null",
2282 AUDIO_SVC_DB_TABLE_AUDIO,
2283 AUDIO_SVC_CATEGORY_MUSIC);
2287 filter_condition(filter_mode, filter_mode2, query);
2289 snprintf(tail_query, sizeof(tail_query),
2290 " %s limit %d,%d", AUDIO_SVC_ORDER_BY_TITLE, offset, rows);
2291 g_strlcat(query, tail_query, sizeof(query));
2295 case AUDIO_SVC_TRACK_BY_ARTIST:
2297 if (type_string && strlen(type_string) > 0) {
2298 snprintf(query, sizeof(query),
2299 "select %s, album from %s where valid=1 and category=%d and artist=?",
2300 result_field, AUDIO_SVC_DB_TABLE_AUDIO,
2301 AUDIO_SVC_CATEGORY_MUSIC);
2303 snprintf(query, sizeof(query),
2304 "select %s, album from %s where valid=1 and category=%d and artist is null",
2305 result_field, AUDIO_SVC_DB_TABLE_AUDIO,
2306 AUDIO_SVC_CATEGORY_MUSIC);
2308 filter_condition(filter_mode, filter_mode2, query);
2309 snprintf(tail_query, sizeof(tail_query), " order by album %s, title %s limit %d,%d", AUDIO_SVC_COLLATE_NOCASE, AUDIO_SVC_COLLATE_NOCASE, offset, rows);
2310 g_strlcat(query, tail_query, sizeof(query));
2314 case AUDIO_SVC_TRACK_BY_ARTIST_GENRE:
2316 if (type_string && strlen(type_string) > 0) {
2317 if (type_string2 && strlen(type_string2) > 0) {
2318 snprintf(query, sizeof(query),
2319 "select %s from %s where valid=1 and category=%d and genre=? and artist=?",
2321 AUDIO_SVC_DB_TABLE_AUDIO,
2322 AUDIO_SVC_CATEGORY_MUSIC);
2324 snprintf(query, sizeof(query),
2325 "select %s from %s where valid=1 and category=%d and genre=? and artist is null",
2327 AUDIO_SVC_DB_TABLE_AUDIO,
2328 AUDIO_SVC_CATEGORY_MUSIC);
2331 if (type_string2 && strlen(type_string2) > 0) {
2332 snprintf(query, sizeof(query),
2333 "select %s from %s where valid=1 and category=%d and genre is null and artist=?",
2335 AUDIO_SVC_DB_TABLE_AUDIO,
2336 AUDIO_SVC_CATEGORY_MUSIC);
2338 snprintf(query, sizeof(query),
2339 "select %s from %s where valid=1 and category=%d and genre is null and artist is null",
2341 AUDIO_SVC_DB_TABLE_AUDIO,
2342 AUDIO_SVC_CATEGORY_MUSIC);
2346 filter_condition(filter_mode, filter_mode2, query);
2348 snprintf(tail_query, sizeof(tail_query),
2349 " %s limit %d,%d", AUDIO_SVC_ORDER_BY_TITLE, offset, rows);
2350 g_strlcat(query, tail_query, sizeof(query));
2354 case AUDIO_SVC_TRACK_BY_GENRE:
2356 if (type_string && strlen(type_string) > 0) {
2357 snprintf(query, sizeof(query),
2358 "select %s from %s where valid=1 and category=%d and genre=?",
2359 result_field, AUDIO_SVC_DB_TABLE_AUDIO,
2360 AUDIO_SVC_CATEGORY_MUSIC);
2362 snprintf(query, sizeof(query),
2363 "select %s from %s where valid=1 and category=%d and genre is null",
2364 result_field, AUDIO_SVC_DB_TABLE_AUDIO,
2365 AUDIO_SVC_CATEGORY_MUSIC);
2367 filter_condition(filter_mode, filter_mode2, query);
2369 snprintf(tail_query, sizeof(tail_query),
2370 " %s limit %d,%d", AUDIO_SVC_ORDER_BY_TITLE, offset, rows);
2371 g_strlcat(query, tail_query, sizeof(query));
2375 case AUDIO_SVC_TRACK_BY_FOLDER:
2377 if (type_string && strlen(type_string) > 0) {
2378 snprintf(query, sizeof(query),
2379 "select %s from %s where valid=1 and category=%d and folder_uuid=(select folder_uuid from %s where path = ?)",
2380 result_field, AUDIO_SVC_DB_TABLE_AUDIO,
2381 AUDIO_SVC_CATEGORY_MUSIC,
2382 AUDIO_SVC_DB_TABLE_AUDIO_FOLDER);
2384 snprintf(query, sizeof(query),
2385 "select %s from %s where valid=1 and category=%d and folder_uuid is null",
2386 result_field, AUDIO_SVC_DB_TABLE_AUDIO,
2387 AUDIO_SVC_CATEGORY_MUSIC);
2389 filter_condition(filter_mode, filter_mode2, query);
2391 snprintf(tail_query, sizeof(tail_query),
2392 " %s limit %d,%d", AUDIO_SVC_ORDER_BY_TITLE, offset, rows);
2393 g_strlcat(query, tail_query, sizeof(query));
2397 case AUDIO_SVC_TRACK_BY_YEAR:
2399 if (type_string && strlen(type_string) > 0) {
2400 snprintf(query, sizeof(query),
2401 "select %s from %s where valid=1 and category=%d and year=?",
2402 result_field, AUDIO_SVC_DB_TABLE_AUDIO,
2403 AUDIO_SVC_CATEGORY_MUSIC);
2405 snprintf(query, sizeof(query),
2406 "select %s from %s where valid=1 and category=%d and year is null",
2407 result_field, AUDIO_SVC_DB_TABLE_AUDIO,
2408 AUDIO_SVC_CATEGORY_MUSIC);
2410 filter_condition(filter_mode, filter_mode2, query);
2412 snprintf(tail_query, sizeof(tail_query),
2413 " %s limit %d,%d", AUDIO_SVC_ORDER_BY_TITLE, offset, rows);
2414 g_strlcat(query, tail_query, sizeof(query));
2418 case AUDIO_SVC_TRACK_BY_COMPOSER:
2420 if (type_string && strlen(type_string) > 0) {
2421 snprintf(query, sizeof(query),
2422 "select %s from %s where valid=1 and category=%d and author=?",
2423 result_field, AUDIO_SVC_DB_TABLE_AUDIO,
2424 AUDIO_SVC_CATEGORY_MUSIC);
2426 snprintf(query, sizeof(query),
2427 "select %s from %s where valid=1 and category=%d and author is null",
2428 result_field, AUDIO_SVC_DB_TABLE_AUDIO,
2429 AUDIO_SVC_CATEGORY_MUSIC);
2431 filter_condition(filter_mode, filter_mode2, query);
2433 snprintf(tail_query, sizeof(tail_query),
2434 " %s limit %d,%d", AUDIO_SVC_ORDER_BY_TITLE, offset, rows);
2435 g_strlcat(query, tail_query, sizeof(query));
2439 case AUDIO_SVC_TRACK_BY_TOPRATING:
2441 snprintf(query, sizeof(query),
2442 "select %s from %s where valid=1 and category=%d and rating >= %d",
2443 result_field, AUDIO_SVC_DB_TABLE_AUDIO,
2444 AUDIO_SVC_CATEGORY_MUSIC, AUDIO_SVC_RATING_4);
2446 filter_condition(filter_mode, filter_mode2, query);
2448 snprintf(tail_query, sizeof(tail_query),
2449 " order by rating desc, rated_time desc limit %d,%d",
2451 g_strlcat(query, tail_query, sizeof(query));
2455 case AUDIO_SVC_TRACK_BY_PLAYED_TIME:
2457 snprintf(query, sizeof(query),
2458 "select %s from %s where valid=1 and category=%d and last_played_time > 0",
2459 result_field, AUDIO_SVC_DB_TABLE_AUDIO,
2460 AUDIO_SVC_CATEGORY_MUSIC);
2462 filter_condition(filter_mode, filter_mode2, query);
2464 snprintf(tail_query, sizeof(tail_query),
2465 " order by last_played_time desc limit %d,%d",
2467 g_strlcat(query, tail_query, sizeof(query));
2471 case AUDIO_SVC_TRACK_BY_ADDED_TIME:
2473 snprintf(query, sizeof(query),
2474 "select %s from %s where valid=1 and category=%d and added_time > 0",
2475 result_field, AUDIO_SVC_DB_TABLE_AUDIO,
2476 AUDIO_SVC_CATEGORY_MUSIC);
2478 filter_condition(filter_mode, filter_mode2, query);
2480 snprintf(tail_query, sizeof(tail_query),
2481 " order by added_time desc limit %d,%d",
2483 g_strlcat(query, tail_query, sizeof(query));
2487 case AUDIO_SVC_TRACK_BY_PLAYED_COUNT:
2489 snprintf(query, sizeof(query),
2490 "select %s from %s where valid=1 and category=%d and played_count > 0",
2491 result_field, AUDIO_SVC_DB_TABLE_AUDIO,
2492 AUDIO_SVC_CATEGORY_MUSIC);
2494 filter_condition(filter_mode, filter_mode2, query);
2496 snprintf(tail_query, sizeof(tail_query),
2497 " order by played_count desc limit %d,%d",
2499 g_strlcat(query, tail_query, sizeof(query));
2503 /* To permit duplicated track in a playlist, playlist item index should be returned. */
2504 case AUDIO_SVC_TRACK_BY_PLAYLIST:
2506 char *result_field_for_playlist =
2507 "b.audio_uuid, b.path, b.thumbnail_path, b.title, b.artist, b.duration, b.rating, b.album";
2509 snprintf(query, sizeof(query),
2510 "select %s from %s a, %s b where a.playlist_id=%d and b.audio_uuid=a.audio_uuid and b.valid=1 and b.category=%d ",
2511 result_field_for_playlist,
2512 AUDIO_SVC_DB_TABLE_AUDIO_PLAYLISTS_MAP,
2513 AUDIO_SVC_DB_TABLE_AUDIO, (int)type_string,
2514 AUDIO_SVC_CATEGORY_MUSIC);
2516 if (len < 1 || len >= sizeof(query)) {
2518 ("snprintf error occured or truncated");
2519 return AUDIO_SVC_ERROR_INTERNAL;
2525 snprintf(tail_query, sizeof(tail_query),
2526 " and b.title like ?");
2527 g_strlcat(query, tail_query, sizeof(query));
2530 snprintf(tail_query, sizeof(tail_query),
2531 " and b.title like ?");
2532 g_strlcat(query, tail_query, sizeof(query));
2534 snprintf(tail_query, sizeof(tail_query),
2535 " order by a.play_order limit %d,%d", offset,
2538 g_strlcat(query, tail_query, sizeof(query));
2543 audio_svc_error("Invalid track type");
2544 return AUDIO_SVC_ERROR_INVALID_PARAMETER;
2548 audio_svc_debug("[SQL query] : %s", query);
2550 err = sqlite3_prepare_v2(handle, query, -1, &sql_stmt, NULL);
2551 if (err != SQLITE_OK) {
2552 audio_svc_error("prepare error [%s]", sqlite3_errmsg(handle));
2553 return AUDIO_SVC_ERROR_DB_INTERNAL;
2556 if ((track_type >= AUDIO_SVC_TRACK_BY_ALBUM)
2557 && (track_type <= AUDIO_SVC_TRACK_BY_COMPOSER)) {
2558 if (type_string && strlen(type_string) > 0) {
2559 if ((track_type == AUDIO_SVC_TRACK_BY_YEAR)
2560 && (!strcmp(type_string, AUDIO_SVC_TAG_UNKNOWN))) {
2563 sqlite3_bind_text(sql_stmt, text_bind,
2567 ("cont_string1(%s) binding is failed (%d)",
2569 SQLITE3_FINALIZE(sql_stmt);
2570 return AUDIO_SVC_ERROR_DB_INTERNAL;
2576 sqlite3_bind_text(sql_stmt, text_bind,
2581 ("cont_string1(%s) binding is failed (%d)",
2583 SQLITE3_FINALIZE(sql_stmt);
2584 return AUDIO_SVC_ERROR_DB_INTERNAL;
2591 if ((track_type == AUDIO_SVC_TRACK_BY_ARTIST_GENRE)
2592 || (track_type == AUDIO_SVC_TRACK_BY_ARTIST_ALBUM)) {
2593 if (type_string2 && strlen(type_string2) > 0) {
2596 sqlite3_bind_text(sql_stmt, text_bind,
2597 (char *)type_string2,
2598 strlen(type_string2), NULL))) {
2600 ("cont_string2(%s) binding is failed (%d)",
2602 SQLITE3_FINALIZE(sql_stmt);
2603 return AUDIO_SVC_ERROR_DB_INTERNAL;
2612 sqlite3_bind_text(sql_stmt, text_bind,
2613 (char *)filter_query,
2614 strlen(filter_query), NULL))) {
2616 ("filter_query(%s) binding is failed (%d)",
2618 SQLITE3_FINALIZE(sql_stmt);
2619 return AUDIO_SVC_ERROR_DB_INTERNAL;
2626 sqlite3_bind_text(sql_stmt, text_bind,
2627 (char *)filter_query2,
2628 strlen(filter_query2), NULL))) {
2630 ("filter_query(%s) binding is failed (%d)",
2631 filter_query2, err);
2632 SQLITE3_FINALIZE(sql_stmt);
2633 return AUDIO_SVC_ERROR_DB_INTERNAL;
2638 while (sqlite3_step(sql_stmt) == SQLITE_ROW) {
2639 _strncpy_safe(track[idx].audio_uuid,
2640 (const char *)sqlite3_column_text(sql_stmt, AUDIO_SVC_LIST_ITEM_AUDIO_ID), sizeof(track[idx].audio_uuid));
2641 _strncpy_safe(track[idx].pathname,
2642 (const char *)sqlite3_column_text(sql_stmt, AUDIO_SVC_LIST_ITEM_PATHNAME), sizeof(track[idx].pathname));
2643 _strncpy_safe(track[idx].thumbnail_path,
2644 (const char *)sqlite3_column_text(sql_stmt, AUDIO_SVC_LIST_ITEM_THUMBNAIL_PATH), sizeof(track[idx].thumbnail_path));
2645 _strncpy_safe(track[idx].title,
2646 (const char *)sqlite3_column_text(sql_stmt, AUDIO_SVC_LIST_ITEM_TITLE), sizeof(track[idx].title));
2647 _strncpy_safe(track[idx].artist,
2648 (const char *)sqlite3_column_text(sql_stmt, AUDIO_SVC_LIST_ITEM_ARTIST), sizeof(track[idx].artist));
2649 _strncpy_safe(track[idx].album,
2650 (const char *)sqlite3_column_text(sql_stmt, AUDIO_SVC_LIST_ITEM_ALBUM), sizeof(track[idx].artist));
2652 track[idx].duration = sqlite3_column_int(sql_stmt, AUDIO_SVC_LIST_ITEM_DURATION);
2653 track[idx].rating = sqlite3_column_int(sql_stmt, AUDIO_SVC_LIST_ITEM_RATING);
2654 audio_svc_debug ("Index : %d : audio_uuid = %s, title = %s, pathname = %s, duration = %d",
2655 idx, track[idx].audio_uuid, track[idx].title, track[idx].pathname, track[idx].duration);
2660 SQLITE3_FINALIZE(sql_stmt);
2662 return AUDIO_SVC_ERROR_NONE;
2666 int _audio_svc_delete_music_track_groups(sqlite3 *handle, audio_svc_group_type_e group_type,
2667 const char *type_string)
2672 switch (group_type) {
2673 case AUDIO_SVC_GROUP_BY_ALBUM:
2676 ("delete from %s where album='%q' and valid=1 and category=%d",
2677 AUDIO_SVC_DB_TABLE_AUDIO, type_string,
2678 AUDIO_SVC_CATEGORY_MUSIC);
2680 case AUDIO_SVC_GROUP_BY_ARTIST:
2683 ("delete from %s where artist='%q' and valid=1 and category=%d",
2684 AUDIO_SVC_DB_TABLE_AUDIO, type_string,
2685 AUDIO_SVC_CATEGORY_MUSIC);
2687 case AUDIO_SVC_GROUP_BY_GENRE:
2690 ("delete from %s where genre='%q' and valid=1 and category=%d",
2691 AUDIO_SVC_DB_TABLE_AUDIO, type_string,
2692 AUDIO_SVC_CATEGORY_MUSIC);
2694 case AUDIO_SVC_GROUP_BY_FOLDER:
2697 ("delete from %s where folder_uuid=(select folder_uuid from %s where path = '%q') and valid=1 and category=%d",
2698 AUDIO_SVC_DB_TABLE_AUDIO, AUDIO_SVC_DB_TABLE_AUDIO_FOLDER,
2699 type_string, AUDIO_SVC_CATEGORY_MUSIC);
2701 case AUDIO_SVC_GROUP_BY_YEAR:
2704 ("delete from %s where year= %d and valid=1 and category=%d",
2705 AUDIO_SVC_DB_TABLE_AUDIO, str_2_year(type_string),
2706 AUDIO_SVC_CATEGORY_MUSIC);
2708 case AUDIO_SVC_GROUP_BY_COMPOSER:
2711 ("delete from %s where author='%q' and valid=1 and category=%d",
2712 AUDIO_SVC_DB_TABLE_AUDIO, type_string,
2713 AUDIO_SVC_CATEGORY_MUSIC);
2716 audio_svc_error("Invalid track type");
2717 return AUDIO_SVC_ERROR_INVALID_PARAMETER;
2720 audio_svc_debug("query (%s)", sql);
2722 err = _audio_svc_sql_query(handle, sql);
2724 if (err != SQLITE_OK) {
2725 audio_svc_error("To delete group is failed(%d)", err);
2726 if (err == AUDIO_SVC_ERROR_DB_CONNECT) {
2729 return AUDIO_SVC_ERROR_DB_INTERNAL;
2732 return AUDIO_SVC_ERROR_NONE;
2735 int _audio_svc_search_audio_id_by_path(sqlite3 *handle, const char *path, char *audio_id)
2737 int ret = AUDIO_SVC_ERROR_NONE;
2739 sqlite3_stmt *sql_stmt = NULL;
2743 ("select audio_uuid from %s where valid=1 and category=%d and path='%q'",
2744 AUDIO_SVC_DB_TABLE_AUDIO, AUDIO_SVC_CATEGORY_MUSIC, path);
2746 ret = _audio_svc_sql_prepare_to_step(handle, sql, &sql_stmt);
2748 if (ret != AUDIO_SVC_ERROR_NONE) {
2750 ("error when _audio_svc_search_audio_id_by_path. ret = [%d]",
2755 _strncpy_safe(audio_id, (const char *)sqlite3_column_text(sql_stmt, 0), AUDIO_SVC_UUID_SIZE+1);
2757 SQLITE3_FINALIZE(sql_stmt);
2759 return AUDIO_SVC_ERROR_NONE;
2763 int _audio_svc_update_valid_of_music_records(sqlite3 *handle, audio_svc_storage_type_e
2764 storage_type, int valid)
2766 audio_svc_debug("storage_type: %d", storage_type);
2770 sqlite3_mprintf("update %s set valid = %d where storage_type = %d",
2771 AUDIO_SVC_DB_TABLE_AUDIO, valid, storage_type);
2772 err = _audio_svc_sql_query(handle, sql);
2774 if (err != SQLITE_OK) {
2775 audio_svc_error("To set all items as invalid is failed(%d)",
2777 if (err == AUDIO_SVC_ERROR_DB_CONNECT) {
2780 return AUDIO_SVC_ERROR_DB_INTERNAL;
2783 return AUDIO_SVC_ERROR_NONE;
2786 int _audio_svc_count_record_with_path(sqlite3 *handle, const char *path)
2789 int ret = AUDIO_SVC_ERROR_NONE;
2791 sqlite3_stmt *sql_stmt = NULL;
2793 sqlite3_mprintf("select count(*) from %s where path='%q'",
2794 AUDIO_SVC_DB_TABLE_AUDIO, path);
2796 ret = _audio_svc_sql_prepare_to_step(handle, sql, &sql_stmt);
2798 if (ret != AUDIO_SVC_ERROR_NONE) {
2800 ("error when _audio_svc_count_record_with_path. err = [%d]",
2805 count = sqlite3_column_int(sql_stmt, 0);
2807 SQLITE3_FINALIZE(sql_stmt);
2813 int _audio_svc_delete_invalid_music_records(sqlite3 *handle, audio_svc_storage_type_e
2817 mp_thumbnailpath_record_t *thumbpath_record = NULL;
2819 int invalid_count = 0;
2820 int ret = AUDIO_SVC_ERROR_NONE;
2822 invalid_count = __audio_svc_count_invalid_records_with_thumbnail(handle, storage_type);
2823 audio_svc_debug("invalid count: %d\n", invalid_count);
2825 if (invalid_count > 0) {
2826 thumbpath_record = (mp_thumbnailpath_record_t *)malloc(sizeof(mp_thumbnailpath_record_t) * invalid_count);
2827 if (thumbpath_record == NULL) {
2828 audio_svc_debug("fail to memory allocation");
2829 return AUDIO_SVC_ERROR_OUT_OF_MEMORY;
2831 memset(thumbpath_record, 0, sizeof(mp_thumbnailpath_record_t) * invalid_count);
2833 ret = __audio_svc_get_invalid_records_with_thumbnail(handle, storage_type, invalid_count, thumbpath_record);
2834 if (ret != AUDIO_SVC_ERROR_NONE) {
2835 audio_svc_error("error when get thumbnail record");
2836 SAFE_FREE(thumbpath_record);
2840 audio_svc_debug("There is no item with thumbnail");
2843 char *sql = sqlite3_mprintf("delete from %s where valid = 0 and storage_type=%d", AUDIO_SVC_DB_TABLE_AUDIO, storage_type);
2844 err = _audio_svc_sql_query(handle, sql);
2846 if (err != SQLITE_OK) {
2847 audio_svc_error("To delete invalid items is failed(%d)", err);
2848 SAFE_FREE(thumbpath_record);
2850 if (err == AUDIO_SVC_ERROR_DB_CONNECT) {
2853 return AUDIO_SVC_ERROR_DB_INTERNAL;
2857 for (idx = 0; idx < invalid_count; idx++) {
2858 if (strlen(thumbpath_record[idx].thumbnail_path) > 0) {
2860 _audio_svc_check_and_remove_thumbnail
2861 (thumbpath_record[idx].thumbnail_path);
2862 if (ret != AUDIO_SVC_ERROR_NONE) {
2864 ("error _audio_svc_check_and_remove_thumbnail");
2865 SAFE_FREE(thumbpath_record);
2871 SAFE_FREE(thumbpath_record);
2873 for (idx = 0; idx < invalid_count; idx++) {
2874 if (strlen(thumbpath_record[idx].thumbnail_path) > 0) {
2875 if (_audio_svc_remove_file(thumbpath_record[idx].thumbnail_path) == FALSE) {
2876 audio_svc_error("fail to remove thumbnail file.");
2877 SAFE_FREE(thumbpath_record);
2878 return AUDIO_SVC_ERROR_INTERNAL;
2882 SAFE_FREE(thumbpath_record);
2884 return AUDIO_SVC_ERROR_NONE;
2887 int _audio_svc_update_valid_in_music_record(sqlite3 *handle, const char *path, int valid)
2891 char *sql = sqlite3_mprintf("update %s set valid=%d where path= '%q'",
2892 AUDIO_SVC_DB_TABLE_AUDIO, valid, path);
2893 err = _audio_svc_sql_query(handle, sql);
2895 if (err != SQLITE_OK) {
2896 audio_svc_error("To update item as valid is failed(%d)", err);
2897 if (err == AUDIO_SVC_ERROR_DB_CONNECT) {
2900 return AUDIO_SVC_ERROR_DB_INTERNAL;
2902 return AUDIO_SVC_ERROR_NONE;
2906 int _audio_svc_update_valid_in_music_record_query_add(sqlite3 *handle, const char *path, int valid)
2908 char *sql = sqlite3_mprintf("update %s set valid=%d where path= '%q'",
2909 AUDIO_SVC_DB_TABLE_AUDIO, valid, path);
2911 audio_svc_debug("SQL = [%s]", sql);
2913 _audio_svc_sql_query_add(&g_audio_svc_item_valid_query_list, &sql);
2915 return AUDIO_SVC_ERROR_NONE;
2918 int _audio_svc_move_item_query_add(sqlite3 *handle, const char *src_path, const char *path, audio_svc_storage_type_e storage_type, const char *folder_id)
2921 sql = sqlite3_mprintf("update %s set path='%q', storage_type=%d where valid=1 and path='%q'",
2922 AUDIO_SVC_DB_TABLE_AUDIO, path, storage_type, src_path);
2924 audio_svc_debug("SQL = [%s]", sql);
2925 _audio_svc_sql_query_add(&g_audio_svc_move_item_query_list, &sql);
2927 sql = sqlite3_mprintf("update %s set folder_uuid='%q' where path='%q'",
2928 AUDIO_SVC_DB_TABLE_AUDIO, folder_id, path);
2930 audio_svc_debug("SQL = [%s]", sql);
2931 _audio_svc_sql_query_add(&g_audio_svc_move_item_query_list, &sql);
2933 return AUDIO_SVC_ERROR_NONE;
2936 //call this API after beginning transaction. this API do sqlite_exec for the g_audio_svc_sqli_query_list.
2937 int _audio_svc_list_query_do(sqlite3 *handle, audio_svc_query_type_e query_type)
2939 int ret = AUDIO_SVC_ERROR_NONE;
2941 ret = _audio_svc_sql_begin_trans(handle);
2942 audio_svc_retv_if(ret != AUDIO_SVC_ERROR_NONE, ret);
2944 if (query_type == AUDIO_SVC_QUERY_SET_ITEM_VALID)
2945 ret = _audio_svc_sql_query_list(handle, &g_audio_svc_item_valid_query_list);
2946 else if (query_type == AUDIO_SVC_QUERY_MOVE_ITEM)
2947 ret = _audio_svc_sql_query_list(handle, &g_audio_svc_move_item_query_list);
2948 else if (query_type == AUDIO_SVC_QUERY_INSERT_ITEM)
2949 ret = _audio_svc_sql_query_list(handle, &g_audio_svc_insert_item_query_list);
2951 ret = AUDIO_SVC_ERROR_INVALID_PARAMETER;
2953 if (ret != AUDIO_SVC_ERROR_NONE) {
2954 audio_svc_error("_audio_svc_list_query_do failed. start rollback");
2955 _audio_svc_sql_rollback_trans(handle);
2959 ret = _audio_svc_sql_end_trans(handle);
2960 if (ret != AUDIO_SVC_ERROR_NONE) {
2961 audio_svc_error("mb_svc_sqlite3_commit_trans failed.. Now start to rollback\n");
2962 _audio_svc_sql_rollback_trans(handle);
2966 return AUDIO_SVC_ERROR_NONE;
2969 int _audio_svc_get_path(sqlite3 *handle, const char *audio_id, char *path)
2971 int ret = AUDIO_SVC_ERROR_NONE;
2972 sqlite3_stmt *sql_stmt = NULL;
2976 ("select path from %s where audio_uuid='%q' and valid=1 and category=%d",
2977 AUDIO_SVC_DB_TABLE_AUDIO, audio_id, AUDIO_SVC_CATEGORY_MUSIC);
2979 ret = _audio_svc_sql_prepare_to_step(handle, sql, &sql_stmt);
2981 if (ret != AUDIO_SVC_ERROR_NONE) {
2982 audio_svc_error("error when _audio_svc_get_path. ret = [%d]",
2987 _strncpy_safe(path, (const char *)sqlite3_column_text(sql_stmt, 0),
2988 AUDIO_SVC_PATHNAME_SIZE);
2990 SQLITE3_FINALIZE(sql_stmt);
2992 return AUDIO_SVC_ERROR_NONE;
2996 int _audio_svc_get_and_append_folder_id_by_path(sqlite3 *handle, const char *path, audio_svc_storage_type_e storage_type, char *folder_id)
2998 char *path_name = NULL;
2999 int ret = AUDIO_SVC_ERROR_NONE;
3001 path_name = g_path_get_dirname(path);
3003 ret = _audio_svc_get_folder_id_by_foldername(handle, path_name, folder_id);
3005 if(ret == AUDIO_SVC_ERROR_DB_NO_RECORD) {
3006 char *folder_name = NULL;
3007 int folder_modified_date = 0;
3008 char *folder_uuid = _media_info_generate_uuid();
3009 if(folder_uuid == NULL ) {
3010 audio_svc_error("Invalid UUID");
3011 SAFE_FREE(path_name);
3012 return AUDIO_SVC_ERROR_INTERNAL;
3015 folder_name = g_path_get_basename(path_name);
3016 folder_modified_date = _audio_svc_get_file_dir_modified_date(path_name);
3018 ret = _audio_svc_append_audio_folder(handle, storage_type, folder_uuid, path_name, folder_name, folder_modified_date);
3019 SAFE_FREE(folder_name);
3020 _strncpy_safe(folder_id, folder_uuid, AUDIO_SVC_UUID_SIZE+1);
3023 SAFE_FREE(path_name);
3027 int _audio_svc_get_folder_id_by_foldername(sqlite3 *handle, const char *folder_name, char *folder_id)
3029 int ret = AUDIO_SVC_ERROR_NONE;
3030 sqlite3_stmt *sql_stmt = NULL;
3032 char *sql = sqlite3_mprintf("SELECT folder_uuid FROM %s WHERE path = '%q';", AUDIO_SVC_DB_TABLE_AUDIO_FOLDER, folder_name);
3034 ret = _audio_svc_sql_prepare_to_step(handle, sql, &sql_stmt);
3036 if (ret != AUDIO_SVC_ERROR_NONE) {
3037 if(ret == AUDIO_SVC_ERROR_DB_NO_RECORD) {
3038 audio_svc_debug("there is no folder.");
3041 audio_svc_error("error when _audio_svc_get_folder_id_by_foldername. err = [%d]", ret);
3046 _strncpy_safe(folder_id, (const char *)sqlite3_column_text(sql_stmt, 0), AUDIO_SVC_UUID_SIZE+1);
3048 SQLITE3_FINALIZE(sql_stmt);
3053 int _audio_svc_append_audio_folder(sqlite3 *handle, audio_svc_storage_type_e storage_type,
3054 const char *folder_id, const char *path_name, const char *folder_name, int modified_date)
3058 char *sql = sqlite3_mprintf("insert into %s (folder_uuid, path, folder_name, storage_type, modified_date) values ('%q', '%q', '%q', '%d', '%d'); ",
3059 AUDIO_SVC_DB_TABLE_AUDIO_FOLDER, folder_id, path_name, folder_name, storage_type, modified_date);
3060 err = _audio_svc_sql_query(handle, sql);
3062 if (err != SQLITE_OK) {
3063 audio_svc_error("failed to insert albums");
3064 return AUDIO_SVC_ERROR_DB_INTERNAL;
3067 return AUDIO_SVC_ERROR_NONE;
3070 /* handle by one items. */
3071 int _audio_svc_check_and_update_folder_table(sqlite3 *handle, const char *path_name)
3074 char folder_id[AUDIO_SVC_UUID_SIZE+1] = {0,};
3075 char *folder_path_name = NULL;
3076 int ret = AUDIO_SVC_ERROR_NONE;
3078 folder_path_name = g_path_get_dirname(path_name);
3079 if (folder_path_name == NULL) {
3080 audio_svc_error("error when get path name");
3081 return AUDIO_SVC_ERROR_INTERNAL;
3084 ret = _audio_svc_get_folder_id_by_foldername(handle, folder_path_name, folder_id);
3085 SAFE_FREE(folder_path_name);
3087 if(ret == AUDIO_SVC_ERROR_DB_NO_RECORD) {
3088 audio_svc_error("error when _audio_svc_get_folder_id_by_foldername ");
3089 return AUDIO_SVC_ERROR_DB_NO_RECORD;
3094 ("delete from %s where folder_uuid='%q' and ((SELECT count(*) FROM %s WHERE folder_uuid='%q')=0);",
3095 AUDIO_SVC_DB_TABLE_AUDIO_FOLDER, folder_id,
3096 AUDIO_SVC_DB_TABLE_AUDIO, folder_id);
3097 err = _audio_svc_sql_query(handle, sql);
3099 if (err != SQLITE_OK) {
3100 audio_svc_error("failed to delete audio_folder item");
3101 if (err == AUDIO_SVC_ERROR_DB_CONNECT) {
3104 return AUDIO_SVC_ERROR_DB_INTERNAL;
3107 return AUDIO_SVC_ERROR_NONE;
3110 /* batch processing */
3111 int _audio_svc_update_folder_table(sqlite3 *handle)
3118 ("delete from %s where folder_uuid IN (select folder_uuid FROM %s where folder_uuid NOT IN (select folder_uuid from %s))",
3119 AUDIO_SVC_DB_TABLE_AUDIO_FOLDER, AUDIO_SVC_DB_TABLE_AUDIO_FOLDER,
3120 AUDIO_SVC_DB_TABLE_AUDIO);
3122 err = _audio_svc_sql_query(handle, sql);
3124 if (err != SQLITE_OK) {
3125 audio_svc_error("failed to delete audio_folder item");
3126 if (err == AUDIO_SVC_ERROR_DB_CONNECT) {
3129 return AUDIO_SVC_ERROR_DB_INTERNAL;
3132 return AUDIO_SVC_ERROR_NONE;
3135 int _audio_svc_check_and_update_albums_table(sqlite3 *handle, const char *album)
3140 /* batch processing */
3141 if(!STRING_VALID(album)) {
3142 sql = sqlite3_mprintf("delete from %s where album_id NOT IN (SELECT album_id FROM %s);",
3143 AUDIO_SVC_DB_TABLE_ALBUMS, AUDIO_SVC_DB_TABLE_AUDIO);
3144 err = _audio_svc_sql_query(handle, sql);
3146 if (err != SQLITE_OK) {
3147 audio_svc_error("failed to update albums table");
3148 if (err == AUDIO_SVC_ERROR_DB_CONNECT) {
3151 return AUDIO_SVC_ERROR_DB_INTERNAL;
3154 sql = sqlite3_mprintf("delete from %s where album='%q' and ((SELECT count(*) FROM %s WHERE album='%q')=0);",
3155 AUDIO_SVC_DB_TABLE_ALBUMS, album, AUDIO_SVC_DB_TABLE_AUDIO, album);
3156 err = _audio_svc_sql_query(handle, sql);
3158 if (err != SQLITE_OK) {
3159 audio_svc_error("failed to update albums table");
3160 if (err == AUDIO_SVC_ERROR_DB_CONNECT) {
3163 return AUDIO_SVC_ERROR_DB_INTERNAL;
3167 return AUDIO_SVC_ERROR_NONE;
3170 int _audio_svc_get_thumbnail_path_by_path(sqlite3 *handle, const char *path, char *thumb_path)
3172 int ret = AUDIO_SVC_ERROR_NONE;
3173 sqlite3_stmt *sql_stmt = NULL;
3177 ("select thumbnail_path from %s where valid=1 and path='%q' ",
3178 AUDIO_SVC_DB_TABLE_AUDIO, path);
3180 ret = _audio_svc_sql_prepare_to_step(handle, sql, &sql_stmt);
3182 if (ret != AUDIO_SVC_ERROR_NONE) {
3184 ("error when _audio_svc_get_thumbnail_path_by_path. ret=[%d]",
3189 _strncpy_safe(thumb_path,
3190 (const char *)sqlite3_column_text(sql_stmt, 0),
3191 AUDIO_SVC_PATHNAME_SIZE);
3193 SQLITE3_FINALIZE(sql_stmt);
3195 return AUDIO_SVC_ERROR_NONE;
3198 char *_audio_svc_get_thumbnail_path_by_album_id(sqlite3 *handle, int album_id)
3200 int ret = AUDIO_SVC_ERROR_NONE;
3201 sqlite3_stmt *sql_stmt = NULL;
3202 char thumbnail_path[AUDIO_SVC_PATHNAME_SIZE] = { 0, };
3205 sqlite3_mprintf("select _data from %s where album_id=%d",
3206 AUDIO_SVC_DB_TABLE_ALBUM_ART, album_id);
3208 ret = _audio_svc_sql_prepare_to_step(handle, sql, &sql_stmt);
3210 if (ret != AUDIO_SVC_ERROR_NONE) {
3212 ("error when __audio_svc_get_genre_id. err = [%d]", ret);
3216 _strncpy_safe(thumbnail_path,
3217 (const char *)sqlite3_column_text(sql_stmt, 0),
3218 sizeof(thumbnail_path));
3220 SQLITE3_FINALIZE(sql_stmt);
3222 return g_strdup(thumbnail_path);
3225 int _audio_svc_check_and_remove_thumbnail(sqlite3 *handle, const char *thumbnail_path)
3227 int ret = AUDIO_SVC_ERROR_NONE;
3229 sqlite3_stmt *sql_stmt = NULL;
3231 sqlite3_mprintf("select count(*) from %s where thumbnail_path='%q'",
3232 AUDIO_SVC_DB_TABLE_AUDIO, thumbnail_path);
3234 ret = _audio_svc_sql_prepare_to_step(handle, sql, &sql_stmt);
3235 if (ret != AUDIO_SVC_ERROR_NONE) {
3237 ("error when _audio_svc_check_and_remove_thumbnail. err = [%d]",
3242 count = sqlite3_column_int(sql_stmt, 0);
3243 SQLITE3_FINALIZE(sql_stmt);
3246 if (_audio_svc_remove_file(thumbnail_path) == FALSE) {
3247 audio_svc_error("fail to remove thumbnail file.");
3248 return AUDIO_SVC_ERROR_INTERNAL;
3252 return AUDIO_SVC_ERROR_NONE;
3255 int _audio_svc_list_search(sqlite3 *handle, audio_svc_audio_item_s *item,
3257 audio_svc_search_order_e order_field,
3264 char query[AUDIO_SVC_QUERY_SIZE] = { 0 };
3265 char condition_str[AUDIO_SVC_QUERY_SIZE] = { 0 };
3266 char order_str[AUDIO_SVC_QUERY_SIZE] = { 0 };
3268 sqlite3_stmt *sql_stmt = NULL;
3270 snprintf(query, sizeof(query), "SELECT * FROM audio_media WHERE %s", where_query);
3272 audio_svc_debug("");
3273 if (_audio_svc_get_order_field_str(order_field, order_str,
3274 AUDIO_SVC_QUERY_SIZE) < 0) {
3275 audio_svc_error("_audio_svc_get_order_field_str failure");
3276 return AUDIO_SVC_ERROR_INTERNAL;
3278 audio_svc_debug("");
3280 snprintf(condition_str, sizeof(condition_str), " ORDER BY %s ", order_str);
3281 len = g_strlcat(query, condition_str, sizeof(query));
3283 if (len >= sizeof(query)) {
3284 audio_svc_error("strlcat returns failure ( %d )", len);
3285 return AUDIO_SVC_ERROR_INTERNAL;
3288 memset(condition_str, 0x00, sizeof(condition_str));
3290 snprintf(condition_str, sizeof(condition_str), " LIMIT %d,%d ", offset, count);
3291 len = g_strlcat(query, condition_str, sizeof(query));
3293 if (len >= sizeof(query)) {
3294 audio_svc_error("strlcat returns failure ( %d )", len);
3295 return AUDIO_SVC_ERROR_INTERNAL;
3298 audio_svc_debug("Query : %s", query);
3300 err = sqlite3_prepare_v2(handle, query, -1, &sql_stmt, NULL);
3301 if (err != SQLITE_OK) {
3302 audio_svc_error("prepare error [%s]", sqlite3_errmsg(handle));
3303 return AUDIO_SVC_ERROR_DB_INTERNAL;
3306 while (sqlite3_step(sql_stmt) == SQLITE_ROW) {
3307 __audio_svc_get_next_record(item, sql_stmt);
3311 SQLITE3_FINALIZE(sql_stmt);
3313 return AUDIO_SVC_ERROR_NONE;