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.
25 * This file defines structure and functions related to database.
27 * @file audio-svc-db-utils.c
29 * @brief This file defines sqlite utilities for Audio Service.
37 #include "audio-svc-debug.h"
38 #include "audio-svc-error.h"
39 #include "audio-svc-db-utils.h"
40 #include "media-svc-util.h"
42 int _audio_svc_sql_busy_handler(void *pData, int count)
46 printf("_audio_svc_sql_busy_handler called : %d\n", count);
47 audio_svc_debug("_audio_svc_sql_busy_handler called : %d\n", count);
52 int _audio_svc_sql_query(sqlite3 *handle, const char *sql_str)
57 audio_svc_debug("SQL = [%s]", sql_str);
59 err = sqlite3_exec(handle, sql_str, NULL, NULL, &zErrMsg);
61 if (SQLITE_OK != err) {
62 audio_svc_error("failed to execute [%s], err[%d]", zErrMsg, err);
64 audio_svc_debug("query success");
68 sqlite3_free (zErrMsg);
73 int _audio_svc_sql_query_list(sqlite3 *handle, GList **query_list)
76 int length = g_list_length(*query_list);
80 audio_svc_debug("query list length : [%d]", length);
82 for (i = 0; i < length; i++) {
83 sql = (char*)g_list_nth_data(*query_list, i);
85 err = _audio_svc_sql_query(handle, sql);
88 if (err != SQLITE_OK) {
89 _audio_svc_sql_query_release(query_list);
90 return AUDIO_SVC_ERROR_DB_INTERNAL;
95 _audio_svc_sql_query_release(query_list);
97 return AUDIO_SVC_ERROR_NONE;
101 int _audio_svc_sql_prepare_to_step(sqlite3 *handle, const char *sql_str, sqlite3_stmt** stmt)
105 audio_svc_debug("[SQL query] : %s", sql_str);
107 err = sqlite3_prepare_v2(handle, sql_str, -1, stmt, NULL);
108 sqlite3_free((char *)sql_str);
110 if (err != SQLITE_OK) {
111 audio_svc_error ("prepare error [%s]", sqlite3_errmsg(handle));
112 return AUDIO_SVC_ERROR_DB_INTERNAL;
115 err = sqlite3_step(*stmt);
116 if (err != SQLITE_ROW) {
117 audio_svc_error("Item not found. end of row [%s]", sqlite3_errmsg(handle));
118 SQLITE3_FINALIZE(*stmt);
119 return AUDIO_SVC_ERROR_DB_NO_RECORD;
122 return AUDIO_SVC_ERROR_NONE;
125 int _audio_svc_sql_begin_trans(sqlite3 *handle)
127 char *err_msg = NULL;
129 audio_svc_debug("========_audio_svc_sql_begin_trans");
131 if (SQLITE_OK != sqlite3_exec(handle, "BEGIN IMMEDIATE;", NULL, NULL, &err_msg)) {
132 audio_svc_error("Error:failed to begin transaction: error=%s", err_msg);
133 sqlite3_free(err_msg);
134 return AUDIO_SVC_ERROR_DB_INTERNAL;
137 sqlite3_free(err_msg);
139 return AUDIO_SVC_ERROR_NONE;
142 int _audio_svc_sql_end_trans(sqlite3 *handle)
144 char *err_msg = NULL;
146 audio_svc_debug("========_audio_svc_sql_end_trans");
148 if (SQLITE_OK != sqlite3_exec(handle, "COMMIT;", NULL, NULL, &err_msg)) {
149 audio_svc_error("Error:failed to end transaction: error=%s", err_msg);
150 sqlite3_free(err_msg);
151 return AUDIO_SVC_ERROR_DB_INTERNAL;
154 sqlite3_free(err_msg);
156 return AUDIO_SVC_ERROR_NONE;
159 int _audio_svc_sql_rollback_trans(sqlite3 *handle)
161 char *err_msg = NULL;
163 audio_svc_debug("========_audio_svc_sql_rollback_trans");
165 if (SQLITE_OK != sqlite3_exec(handle, "ROLLBACK;", NULL, NULL, &err_msg)) {
166 audio_svc_error("Error:failed to rollback transaction: error=%s", err_msg);
167 sqlite3_free(err_msg);
168 return AUDIO_SVC_ERROR_DB_INTERNAL;
171 sqlite3_free(err_msg);
173 return AUDIO_SVC_ERROR_NONE;
176 void _audio_svc_sql_query_add(GList **query_list, char **query)
178 *query_list = g_list_append( *query_list, *query);
181 void _audio_svc_sql_query_release(GList **query_list)
184 audio_svc_debug("_audio_svc_sql_query_release");
185 g_list_free(*query_list);