Remove TYER value checker
[platform/core/multimedia/libmedia-service.git] / src / common / media-svc-album.c
1 /*
2  * libmedia-service
3  *
4  * Copyright (c) 2000 - 2011 Samsung Electronics Co., Ltd. All rights reserved.
5  *
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  *
18  */
19
20 #include "media-svc-album.h"
21 #include "media-svc-debug.h"
22 #include "media-svc-env.h"
23 #include "media-svc-util.h"
24 #include "media-svc-db-utils.h"
25
26 int _media_svc_get_album_id(sqlite3 *handle, const char *album, const char *artist, int *album_id)
27 {
28         int ret = MS_MEDIA_ERR_NONE;
29         sqlite3_stmt *sql_stmt = NULL;
30         char *sql = NULL;
31
32         media_svc_retvm_if(album == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "album is NULL");
33
34         if (artist)
35                 sql = sqlite3_mprintf("SELECT album_id FROM %s WHERE name=%Q AND artist=%Q", DB_TABLE_ALBUM, album, artist);
36         else
37                 sql = sqlite3_mprintf("SELECT album_id FROM %s WHERE name=%Q AND artist IS NULL", DB_TABLE_ALBUM, album);
38
39         ret = _media_svc_sql_prepare_to_step(handle, sql, &sql_stmt);
40
41         if (ret != MS_MEDIA_ERR_NONE) {
42                 if (ret == MS_MEDIA_ERR_DB_NO_RECORD)
43                         media_svc_debug("there is no album.");
44                 else
45                         media_svc_error("error when _media_svc_get_album_id. err = [%d]", ret);
46
47                 return ret;
48         }
49
50         *album_id = sqlite3_column_int(sql_stmt, 0);
51
52         SQLITE3_FINALIZE(sql_stmt);
53
54         return ret;
55 }
56
57 int _media_svc_append_album(sqlite3 *handle, bool is_direct, const char *album, const char *artist, const char *album_art, int *album_id, uid_t uid)
58 {
59         int ret = MS_MEDIA_ERR_NONE;
60
61         char *sql = sqlite3_mprintf("INSERT INTO %s(name, artist, album_art) VALUES (%Q, %Q, %Q);", DB_TABLE_ALBUM, album, artist, album_art);
62         if (is_direct)
63                 ret = _media_svc_sql_query_direct(sql, uid);
64         else
65                 ret = _media_svc_sql_query(sql, uid);
66
67         SQLITE3_SAFE_FREE(sql);
68         media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
69
70         int inserted_album_id = 0;
71         ret = _media_svc_get_album_id(handle, album, artist, &inserted_album_id);
72         media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
73
74         *album_id = inserted_album_id;
75
76         return MS_MEDIA_ERR_NONE;
77 }