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.
22 #include "media-svc-album.h"
23 #include "media-svc-error.h"
24 #include "media-svc-debug.h"
25 #include "media-svc-env.h"
26 #include "media-svc-util.h"
27 #include "media-svc-db-utils.h"
29 int _media_svc_get_album_id(sqlite3 *handle, const char *album, const char *artist, int * album_id)
31 int ret = MEDIA_INFO_ERROR_NONE;
32 sqlite3_stmt *sql_stmt = NULL;
35 media_svc_retvm_if(album == NULL, MEDIA_INFO_ERROR_INVALID_PARAMETER, "album is NULL");
38 sql = sqlite3_mprintf("SELECT album_id FROM %s WHERE name = '%q' AND artist = '%q';", MEDIA_SVC_DB_TABLE_ALBUM, album, artist);
40 sql = sqlite3_mprintf("SELECT album_id FROM %s WHERE name = '%q' AND artist IS NULL;", MEDIA_SVC_DB_TABLE_ALBUM, album, artist);
43 ret = _media_svc_sql_prepare_to_step(handle, sql, &sql_stmt);
45 if (ret != MEDIA_INFO_ERROR_NONE) {
46 if(ret == MEDIA_INFO_ERROR_DATABASE_NO_RECORD) {
47 media_svc_debug("there is no album.");
50 media_svc_error("error when _media_svc_get_album_id. err = [%d]", ret);
55 *album_id = sqlite3_column_int(sql_stmt, 0);
57 SQLITE3_FINALIZE(sql_stmt);
62 int _media_svc_get_album_art_by_album_id(sqlite3 *handle, int album_id, char **album_art)
64 int ret = MEDIA_INFO_ERROR_NONE;
65 sqlite3_stmt *sql_stmt = NULL;
68 char *sql = sqlite3_mprintf("SELECT album_art FROM %s WHERE album_id=%d", MEDIA_SVC_DB_TABLE_ALBUM, album_id);
70 ret = _media_svc_sql_prepare_to_step(handle, sql, &sql_stmt);
72 if (ret != MEDIA_INFO_ERROR_NONE) {
73 if(ret == MEDIA_INFO_ERROR_DATABASE_NO_RECORD) {
74 media_svc_debug("there is no album_id.");
77 media_svc_error("error when _media_svc_get_folder_id_by_foldername. err = [%d]", ret);
82 value = (char *)sqlite3_column_text(sql_stmt, 0);
84 if (STRING_VALID(value)) {
85 ret = __media_svc_malloc_and_strncpy(album_art, value);
87 media_svc_error("__media_svc_malloc_and_strncpy failed: %d", ret);
88 SQLITE3_FINALIZE(sql_stmt);
95 SQLITE3_FINALIZE(sql_stmt);
97 return MEDIA_INFO_ERROR_NONE;
100 int _media_svc_append_album(sqlite3 *handle, const char *album, const char *artist, const char *album_art, int * album_id, uid_t uid)
104 char *sql = sqlite3_mprintf("INSERT INTO %s (name, artist, album_art, album_art) values (%Q, %Q, %Q, %Q); ",
105 MEDIA_SVC_DB_TABLE_ALBUM, album, artist, album_art, album_art);
106 err = _media_svc_sql_query(handle, sql, uid);
108 if (err != SQLITE_OK) {
109 media_svc_error("failed to insert albums");
110 return MEDIA_INFO_ERROR_DATABASE_INTERNAL;
113 //*album_id = sqlite3_last_insert_rowid(handle);
114 int inserted_album_id = 0;
115 err = _media_svc_get_album_id(handle, album, artist, &inserted_album_id);
117 media_svc_error("Failed _media_svc_get_album_id : %d", err);
121 *album_id = inserted_album_id;
123 return MEDIA_INFO_ERROR_NONE;
126 int _media_svc_get_media_count_with_album_id_by_path(sqlite3 *handle, const char *path, int *count)
128 int ret = MEDIA_INFO_ERROR_NONE;
129 sqlite3_stmt *sql_stmt = NULL;
132 media_svc_retvm_if(path == NULL, MEDIA_INFO_ERROR_INVALID_PARAMETER, "path is NULL");
134 sql = sqlite3_mprintf("select count(media_uuid) from %s INNER JOIN (select album_id from %s where path=%Q and album_id > 0) as album ON album.album_id=media.album_id;", MEDIA_SVC_DB_TABLE_MEDIA, MEDIA_SVC_DB_TABLE_MEDIA, path);
135 ret = _media_svc_sql_prepare_to_step(handle, sql, &sql_stmt);
137 if (ret != MEDIA_INFO_ERROR_NONE) {
138 if(ret == MEDIA_INFO_ERROR_DATABASE_NO_RECORD) {
139 media_svc_debug("there is no media in relted to this media's album.");
142 media_svc_error("error when _media_svc_get_media_count_with_album_id_by_path. err = [%d]", ret);
147 *count = sqlite3_column_int(sql_stmt, 0);
148 media_svc_debug("Media count : %d", *count);
150 SQLITE3_FINALIZE(sql_stmt);