summaryrefslogtreecommitdiff
path: root/src/media-svc-album.c
blob: be2bb7cc690eb6926fc6da5a0931ab88b73c3826 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
/*
 * libmedia-service
 *
 * Copyright (c) 2000 - 2011 Samsung Electronics Co., Ltd. All rights reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 */

#include "media-svc-album.h"
#include "media-svc-debug.h"
#include "media-svc-env.h"
#include "media-svc-util.h"
#include "media-svc-db-utils.h"

int _media_svc_get_album_id(sqlite3 *handle, const char *album, const char *artist, int *album_id)
{
	int ret = MS_MEDIA_ERR_NONE;
	sqlite3_stmt *sql_stmt = NULL;
	char *sql = NULL;

	media_svc_retvm_if(!album, MS_MEDIA_ERR_INVALID_PARAMETER, "album is NULL");
	media_svc_retvm_if(!artist, MS_MEDIA_ERR_INVALID_PARAMETER, "artist is NULL");

	sql = sqlite3_mprintf("SELECT album_id FROM %s WHERE name=%Q AND artist=%Q", DB_TABLE_ALBUM, album, artist);
	ret = _media_svc_get_result_with_check_record(handle, sql, &sql_stmt);
	SQLITE3_SAFE_FREE(sql);
	if (ret != MS_MEDIA_ERR_NONE) {
		if (ret == MS_MEDIA_ERR_DB_NO_RECORD)
			media_svc_debug("there is no album.");
		else
			media_svc_error("failed to get album id[%d]", ret);

		return ret;
	}

	*album_id = sqlite3_column_int(sql_stmt, 0);

	SQLITE3_FINALIZE(sql_stmt);

	return ret;
}

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)
{
	int ret = MS_MEDIA_ERR_NONE;

	char *sql = sqlite3_mprintf("INSERT INTO %s(name, artist, album_art) VALUES (%Q, %Q, %Q);", DB_TABLE_ALBUM, album, artist, album_art);
	if (is_direct)
		ret = _media_svc_sql_query_direct(sql, uid);
	else
		ret = _media_svc_sql_query(sql, uid);

	SQLITE3_SAFE_FREE(sql);
	media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);

	int inserted_album_id = 0;
	ret = _media_svc_get_album_id(handle, album, artist, &inserted_album_id);
	media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);

	*album_id = inserted_album_id;

	return MS_MEDIA_ERR_NONE;
}