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/>.
23 #include "upnpglobalvars.h"
27 sql_exec(sqlite3
*db
, const char *fmt
, ...)
33 //DPRINTF(E_DEBUG, L_DB_SQL, "SQL: %s\n", sql);
36 sql
= sqlite3_vmprintf(fmt
, 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
);
51 sql_get_table(sqlite3
*db
, const char *sql
, char ***pazResult
, int *pnRow
, int *pnColumn
)
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
);
69 sql_get_int_field(sqlite3
*db
, const char *fmt
, ...)
78 sql
= sqlite3_vmprintf(fmt
, ap
);
81 //DPRINTF(E_DEBUG, L_DB_SQL, "sql: %s\n", sql);
83 switch (sqlite3_prepare_v2(db
, sql
, -1, &stmt
, NULL
))
88 DPRINTF(E_ERROR
, L_DB_SQL
, "prepare failed: %s\n%s\n", sqlite3_errmsg(db
), sql
);
94 ((result
= sqlite3_step(stmt
)) == SQLITE_BUSY
|| result
== SQLITE_LOCKED
) && counter
< 2;
96 /* While SQLITE_BUSY has a built in timeout,
97 SQLITE_LOCKED does not, so sleep */
98 if (result
== SQLITE_LOCKED
)
105 /* no rows returned */
109 if (sqlite3_column_type(stmt
, 0) == SQLITE_NULL
)
114 ret
= sqlite3_column_int(stmt
, 0);
117 DPRINTF(E_WARN
, L_DB_SQL
, "%s: step failed: %s\n%s\n", __func__
, sqlite3_errmsg(db
), sql
);
122 sqlite3_finalize(stmt
);
128 sql_get_int64_field(sqlite3
*db
, const char *fmt
, ...)
137 sql
= sqlite3_vmprintf(fmt
, ap
);
140 //DPRINTF(E_DEBUG, L_DB_SQL, "sql: %s\n", sql);
142 switch (sqlite3_prepare_v2(db
, sql
, -1, &stmt
, NULL
))
147 DPRINTF(E_ERROR
, L_DB_SQL
, "prepare failed: %s\n%s\n", sqlite3_errmsg(db
), sql
);
153 ((result
= sqlite3_step(stmt
)) == SQLITE_BUSY
|| result
== SQLITE_LOCKED
) && counter
< 2;
155 /* While SQLITE_BUSY has a built in timeout,
156 SQLITE_LOCKED does not, so sleep */
157 if (result
== SQLITE_LOCKED
)
164 /* no rows returned */
168 if (sqlite3_column_type(stmt
, 0) == SQLITE_NULL
)
173 ret
= sqlite3_column_int64(stmt
, 0);
176 DPRINTF(E_WARN
, L_DB_SQL
, "%s: step failed: %s\n%s\n", __func__
, sqlite3_errmsg(db
), sql
);
181 sqlite3_finalize(stmt
);
187 sql_get_text_field(sqlite3
*db
, const char *fmt
, ...)
190 int counter
, result
, len
;
197 DPRINTF(E_WARN
, L_DB_SQL
, "db is NULL\n");
202 sql
= sqlite3_vmprintf(fmt
, ap
);
205 //DPRINTF(E_DEBUG, L_DB_SQL, "sql: %s\n", sql);
207 switch (sqlite3_prepare_v2(db
, sql
, -1, &stmt
, NULL
))
212 DPRINTF(E_ERROR
, L_DB_SQL
, "prepare failed: %s\n%s\n", sqlite3_errmsg(db
), sql
);
219 ((result
= sqlite3_step(stmt
)) == SQLITE_BUSY
|| result
== SQLITE_LOCKED
) && counter
< 2;
222 /* While SQLITE_BUSY has a built in timeout,
223 * SQLITE_LOCKED does not, so sleep */
224 if (result
== SQLITE_LOCKED
)
231 /* no rows returned */
236 if (sqlite3_column_type(stmt
, 0) == SQLITE_NULL
)
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");
249 strncpy(str
, (char *)sqlite3_column_text(stmt
, 0), len
+ 1);
253 DPRINTF(E_WARN
, L_DB_SQL
, "SQL step failed: %s\n", sqlite3_errmsg(db
));
257 sqlite3_finalize(stmt
);
263 db_upgrade(sqlite3
*db
)
267 db_vers
= sql_get_int_field(db
, "PRAGMA user_version");
269 if (db_vers
== DB_VERSION
)
271 if (db_vers
> DB_VERSION
)
277 sql_exec(db
, "PRAGMA user_version = %d", DB_VERSION
);