MiniDLNA cvs 2010-11-11
[tomato.git] / release / src / router / minidlna / sql.c
blobe38a744efbb732c5b02c2037eb11219623d39898
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 "log.h"
25 int
26 sql_exec(sqlite3 *db, const char *fmt, ...)
28 int ret;
29 char *errMsg = NULL;
30 char *sql;
31 va_list ap;
32 //DPRINTF(E_DEBUG, L_DB_SQL, "SQL: %s\n", sql);
34 va_start(ap, fmt);
36 sql = sqlite3_vmprintf(fmt, ap);
37 ret = sqlite3_exec(db, sql, 0, 0, &errMsg);
38 if( ret != SQLITE_OK )
40 DPRINTF(E_ERROR, L_DB_SQL, "SQL ERROR %d [%s]\n%s\n", ret, errMsg, sql);
41 if (errMsg)
42 sqlite3_free(errMsg);
44 sqlite3_free(sql);
46 return ret;
49 int
50 sql_get_table(sqlite3 *db, const char *sql, char ***pazResult, int *pnRow, int *pnColumn)
52 int ret;
53 char *errMsg = NULL;
54 //DPRINTF(E_DEBUG, L_DB_SQL, "SQL: %s\n", sql);
56 ret = sqlite3_get_table(db, sql, pazResult, pnRow, pnColumn, &errMsg);
57 if( ret != SQLITE_OK )
59 DPRINTF(E_ERROR, L_DB_SQL, "SQL ERROR %d [%s]\n%s\n", ret, errMsg, sql);
60 if (errMsg)
61 sqlite3_free(errMsg);
64 return ret;
67 int
68 sql_get_int_field(sqlite3 *db, const char *fmt, ...)
70 va_list ap;
71 int counter, result;
72 char *sql;
73 int ret;
74 sqlite3_stmt *stmt;
76 va_start(ap, fmt);
78 sql = sqlite3_vmprintf(fmt, ap);
80 //DPRINTF(E_DEBUG, L_DB_SQL, "sql: %s\n", sql);
82 switch (sqlite3_prepare_v2(db, sql, -1, &stmt, NULL))
84 case SQLITE_OK:
85 break;
86 default:
87 DPRINTF(E_ERROR, L_DB_SQL, "prepare failed: %s\n%s\n", sqlite3_errmsg(db), sql);
88 sqlite3_free(sql);
89 return -1;
91 sqlite3_free(sql);
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", __func__, sqlite3_errmsg(db));
118 ret = -1;
119 break;
122 sqlite3_finalize(stmt);
123 return ret;
126 char *
127 sql_get_text_field(void *db, const char *fmt, ...)
129 va_list ap;
130 int counter, result, len;
131 char *sql;
132 char *str;
133 sqlite3_stmt *stmt;
135 va_start(ap, fmt);
137 if (db == NULL)
139 DPRINTF(E_WARN, L_DB_SQL, "%s: db is NULL", __func__);
140 return NULL;
143 sql = sqlite3_vmprintf(fmt, ap);
145 //DPRINTF(E_DEBUG, L_DB_SQL, "sql: %s\n", sql);
147 switch (sqlite3_prepare_v2(db, sql, -1, &stmt, NULL))
149 case SQLITE_OK:
150 break;
151 default:
152 DPRINTF(E_ERROR, L_DB_SQL, "prepare failed: %s\n%s\n", sqlite3_errmsg(db), sql);
153 sqlite3_free(sql);
154 return NULL;
156 sqlite3_free(sql);
158 for (counter = 0;
159 ((result = sqlite3_step(stmt)) == SQLITE_BUSY || result == SQLITE_LOCKED) && counter < 2;
160 counter++)
162 /* While SQLITE_BUSY has a built in timeout,
163 * SQLITE_LOCKED does not, so sleep */
164 if (result == SQLITE_LOCKED)
165 sleep(1);
168 switch (result)
170 case SQLITE_DONE:
171 /* no rows returned */
172 str = NULL;
173 break;
175 case SQLITE_ROW:
176 if (sqlite3_column_type(stmt, 0) == SQLITE_NULL)
178 str = NULL;
179 break;
182 len = sqlite3_column_bytes(stmt, 0);
183 if ((str = sqlite3_malloc(len + 1)) == NULL)
185 DPRINTF(E_ERROR, L_DB_SQL, "malloc failed");
186 break;
189 strncpy(str, (char *)sqlite3_column_text(stmt, 0), len + 1);
190 break;
192 default:
193 DPRINTF(E_WARN, L_DB_SQL, "%s: step failed: %s", __func__, sqlite3_errmsg(db));
194 str = NULL;
195 break;
198 sqlite3_finalize(stmt);
199 return str;