minidlna: update to 1.1.5
[tomato.git] / release / src / router / minidlna / sql.c
blobb2ea2386ba3cf1795980312083c9688dd164b2e8
1 /* MiniDLNA media server
2 * Copyright (C) 2008-2009 Justin Maggard
4 * This file is part of MiniDLNA.
6 * MiniDLNA is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 as
8 * published by the Free Software Foundation.
10 * MiniDLNA is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with MiniDLNA. If not, see <http://www.gnu.org/licenses/>.
18 #include <stdio.h>
19 #include <string.h>
20 #include <unistd.h>
22 #include "sql.h"
23 #include "upnpglobalvars.h"
24 #include "log.h"
26 int
27 sql_exec(sqlite3 *db, const char *fmt, ...)
29 int ret;
30 char *errMsg = NULL;
31 char *sql;
32 va_list ap;
33 //DPRINTF(E_DEBUG, L_DB_SQL, "SQL: %s\n", sql);
35 va_start(ap, fmt);
36 sql = sqlite3_vmprintf(fmt, ap);
37 va_end(ap);
38 ret = sqlite3_exec(db, sql, 0, 0, &errMsg);
39 if( ret != SQLITE_OK )
41 DPRINTF(E_ERROR, L_DB_SQL, "SQL ERROR %d [%s]\n%s\n", ret, errMsg, sql);
42 if (errMsg)
43 sqlite3_free(errMsg);
45 sqlite3_free(sql);
47 return ret;
50 int
51 sql_get_table(sqlite3 *db, const char *sql, char ***pazResult, int *pnRow, int *pnColumn)
53 int ret;
54 char *errMsg = NULL;
55 //DPRINTF(E_DEBUG, L_DB_SQL, "SQL: %s\n", sql);
57 ret = sqlite3_get_table(db, sql, pazResult, pnRow, pnColumn, &errMsg);
58 if( ret != SQLITE_OK )
60 DPRINTF(E_ERROR, L_DB_SQL, "SQL ERROR %d [%s]\n%s\n", ret, errMsg, sql);
61 if (errMsg)
62 sqlite3_free(errMsg);
65 return ret;
68 int
69 sql_get_int_field(sqlite3 *db, const char *fmt, ...)
71 va_list ap;
72 int counter, result;
73 char *sql;
74 int ret;
75 sqlite3_stmt *stmt;
77 va_start(ap, fmt);
78 sql = sqlite3_vmprintf(fmt, ap);
79 va_end(ap);
81 //DPRINTF(E_DEBUG, L_DB_SQL, "sql: %s\n", sql);
83 switch (sqlite3_prepare_v2(db, sql, -1, &stmt, NULL))
85 case SQLITE_OK:
86 break;
87 default:
88 DPRINTF(E_ERROR, L_DB_SQL, "prepare failed: %s\n%s\n", sqlite3_errmsg(db), sql);
89 sqlite3_free(sql);
90 return -1;
93 for (counter = 0;
94 ((result = sqlite3_step(stmt)) == SQLITE_BUSY || result == SQLITE_LOCKED) && counter < 2;
95 counter++) {
96 /* While SQLITE_BUSY has a built in timeout,
97 SQLITE_LOCKED does not, so sleep */
98 if (result == SQLITE_LOCKED)
99 sleep(1);
102 switch (result)
104 case SQLITE_DONE:
105 /* no rows returned */
106 ret = 0;
107 break;
108 case SQLITE_ROW:
109 if (sqlite3_column_type(stmt, 0) == SQLITE_NULL)
111 ret = 0;
112 break;
114 ret = sqlite3_column_int(stmt, 0);
115 break;
116 default:
117 DPRINTF(E_WARN, L_DB_SQL, "%s: step failed: %s\n%s\n", __func__, sqlite3_errmsg(db), sql);
118 ret = -1;
119 break;
121 sqlite3_free(sql);
122 sqlite3_finalize(stmt);
124 return ret;
127 int64_t
128 sql_get_int64_field(sqlite3 *db, const char *fmt, ...)
130 va_list ap;
131 int counter, result;
132 char *sql;
133 int64_t ret;
134 sqlite3_stmt *stmt;
136 va_start(ap, fmt);
137 sql = sqlite3_vmprintf(fmt, ap);
138 va_end(ap);
140 //DPRINTF(E_DEBUG, L_DB_SQL, "sql: %s\n", sql);
142 switch (sqlite3_prepare_v2(db, sql, -1, &stmt, NULL))
144 case SQLITE_OK:
145 break;
146 default:
147 DPRINTF(E_ERROR, L_DB_SQL, "prepare failed: %s\n%s\n", sqlite3_errmsg(db), sql);
148 sqlite3_free(sql);
149 return -1;
152 for (counter = 0;
153 ((result = sqlite3_step(stmt)) == SQLITE_BUSY || result == SQLITE_LOCKED) && counter < 2;
154 counter++) {
155 /* While SQLITE_BUSY has a built in timeout,
156 SQLITE_LOCKED does not, so sleep */
157 if (result == SQLITE_LOCKED)
158 sleep(1);
161 switch (result)
163 case SQLITE_DONE:
164 /* no rows returned */
165 ret = 0;
166 break;
167 case SQLITE_ROW:
168 if (sqlite3_column_type(stmt, 0) == SQLITE_NULL)
170 ret = 0;
171 break;
173 ret = sqlite3_column_int64(stmt, 0);
174 break;
175 default:
176 DPRINTF(E_WARN, L_DB_SQL, "%s: step failed: %s\n%s\n", __func__, sqlite3_errmsg(db), sql);
177 ret = -1;
178 break;
180 sqlite3_free(sql);
181 sqlite3_finalize(stmt);
183 return ret;
186 char *
187 sql_get_text_field(sqlite3 *db, const char *fmt, ...)
189 va_list ap;
190 int counter, result, len;
191 char *sql;
192 char *str;
193 sqlite3_stmt *stmt;
195 if (db == NULL)
197 DPRINTF(E_WARN, L_DB_SQL, "db is NULL\n");
198 return NULL;
201 va_start(ap, fmt);
202 sql = sqlite3_vmprintf(fmt, ap);
203 va_end(ap);
205 //DPRINTF(E_DEBUG, L_DB_SQL, "sql: %s\n", sql);
207 switch (sqlite3_prepare_v2(db, sql, -1, &stmt, NULL))
209 case SQLITE_OK:
210 break;
211 default:
212 DPRINTF(E_ERROR, L_DB_SQL, "prepare failed: %s\n%s\n", sqlite3_errmsg(db), sql);
213 sqlite3_free(sql);
214 return NULL;
216 sqlite3_free(sql);
218 for (counter = 0;
219 ((result = sqlite3_step(stmt)) == SQLITE_BUSY || result == SQLITE_LOCKED) && counter < 2;
220 counter++)
222 /* While SQLITE_BUSY has a built in timeout,
223 * SQLITE_LOCKED does not, so sleep */
224 if (result == SQLITE_LOCKED)
225 sleep(1);
228 switch (result)
230 case SQLITE_DONE:
231 /* no rows returned */
232 str = NULL;
233 break;
235 case SQLITE_ROW:
236 if (sqlite3_column_type(stmt, 0) == SQLITE_NULL)
238 str = NULL;
239 break;
242 len = sqlite3_column_bytes(stmt, 0);
243 if ((str = sqlite3_malloc(len + 1)) == NULL)
245 DPRINTF(E_ERROR, L_DB_SQL, "malloc failed\n");
246 break;
249 strncpy(str, (char *)sqlite3_column_text(stmt, 0), len + 1);
250 break;
252 default:
253 DPRINTF(E_WARN, L_DB_SQL, "SQL step failed: %s\n", sqlite3_errmsg(db));
254 str = NULL;
255 break;
257 sqlite3_finalize(stmt);
259 return str;
263 db_upgrade(sqlite3 *db)
265 int db_vers;
267 db_vers = sql_get_int_field(db, "PRAGMA user_version");
269 if (db_vers == DB_VERSION)
270 return 0;
271 if (db_vers > DB_VERSION)
272 return -2;
273 if (db_vers < 1)
274 return -1;
275 if (db_vers < 9)
276 return db_vers;
277 sql_exec(db, "PRAGMA user_version = %d", DB_VERSION);
279 return 0;