upload tizen1.0 source
[framework/multimedia/libmedia-service.git] / src / audio / audio-svc-db-utils.c
1 /*
2  * libmedia-service
3  *
4  * Copyright (c) 2000 - 2011 Samsung Electronics Co., Ltd. All rights reserved.
5  *
6  * Contact: Hyunjun Ko <zzoon.ko@samsung.com>, Haejeong Kim <backto.kim@samsung.com>
7  *
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
11  *
12  * http://www.apache.org/licenses/LICENSE-2.0
13  *
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.
19  *
20  */
21
22
23
24 /**
25  * This file defines structure and functions related to database.
26  *
27  * @file        audio-svc-db-utils.c
28  * @version     0.1
29  * @brief       This file defines sqlite utilities for Audio Service.
30  */
31
32
33 #include <stdlib.h>
34 #include <stdio.h>
35 #include <stdbool.h>
36 #include <unistd.h>
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"
41
42 int _audio_svc_sql_busy_handler(void *pData, int count)
43 {
44         usleep(50000);
45         
46         printf("_audio_svc_sql_busy_handler called : %d\n", count);
47         audio_svc_debug("_audio_svc_sql_busy_handler called : %d\n", count);
48
49         return 100 - count;
50 }
51
52 int _audio_svc_sql_query(sqlite3 *handle, const char *sql_str)
53 {
54         int err = -1;
55         char *zErrMsg = NULL;
56         
57         audio_svc_debug("SQL = [%s]", sql_str);
58
59         err = sqlite3_exec(handle, sql_str, NULL, NULL, &zErrMsg);
60
61         if (SQLITE_OK != err) {
62                 audio_svc_error("failed to execute [%s], err[%d]", zErrMsg, err);
63         } else {
64                 audio_svc_debug("query success");
65         }
66
67         if (zErrMsg)
68                 sqlite3_free (zErrMsg);
69
70         return err;
71 }
72
73 int _audio_svc_sql_query_list(sqlite3 *handle, GList **query_list)
74 {
75         int i = 0;
76         int length = g_list_length(*query_list);
77         int err = -1;
78         char *sql = NULL;
79         
80         audio_svc_debug("query list length : [%d]", length);
81
82         for (i = 0; i < length; i++) {
83                 sql = (char*)g_list_nth_data(*query_list, i);
84                 if(sql != NULL) {
85                         err = _audio_svc_sql_query(handle, sql);
86                         sqlite3_free(sql);
87                         sql = NULL;
88                         if (err != SQLITE_OK) {
89                                 _audio_svc_sql_query_release(query_list);
90                                 return AUDIO_SVC_ERROR_DB_INTERNAL;
91                         }
92                 }
93         }
94
95         _audio_svc_sql_query_release(query_list);
96
97         return AUDIO_SVC_ERROR_NONE;
98
99 }
100
101 int _audio_svc_sql_prepare_to_step(sqlite3 *handle, const char *sql_str, sqlite3_stmt** stmt)
102 {
103         int err = -1;
104
105         audio_svc_debug("[SQL query] : %s", sql_str);
106
107         err = sqlite3_prepare_v2(handle, sql_str, -1, stmt, NULL);
108         sqlite3_free((char *)sql_str);
109         
110         if (err != SQLITE_OK) {
111                 audio_svc_error ("prepare error [%s]", sqlite3_errmsg(handle));
112                 return AUDIO_SVC_ERROR_DB_INTERNAL;
113         }
114
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;
120         }
121
122         return AUDIO_SVC_ERROR_NONE;
123 }
124
125 int _audio_svc_sql_begin_trans(sqlite3 *handle)
126 {
127         char *err_msg = NULL;
128
129         audio_svc_debug("========_audio_svc_sql_begin_trans");
130
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;
135         }
136         
137         sqlite3_free(err_msg);
138
139         return AUDIO_SVC_ERROR_NONE;
140 }
141
142 int _audio_svc_sql_end_trans(sqlite3 *handle)
143 {
144         char *err_msg = NULL;
145
146         audio_svc_debug("========_audio_svc_sql_end_trans");
147
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;
152         }
153         
154         sqlite3_free(err_msg);
155
156         return AUDIO_SVC_ERROR_NONE;
157 }
158
159 int _audio_svc_sql_rollback_trans(sqlite3 *handle)
160 {
161         char *err_msg = NULL;
162
163         audio_svc_debug("========_audio_svc_sql_rollback_trans");
164
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;
169         }
170
171         sqlite3_free(err_msg);
172
173         return AUDIO_SVC_ERROR_NONE;
174 }
175
176 void _audio_svc_sql_query_add(GList **query_list, char **query)
177 {
178         *query_list = g_list_append( *query_list, *query);
179 }
180
181 void _audio_svc_sql_query_release(GList **query_list)
182 {
183         if (*query_list) {
184                 audio_svc_debug("_audio_svc_sql_query_release");
185                 g_list_free(*query_list);
186                 *query_list = NULL;
187         }
188 }
189