1/* MiniDLNA media server 2 * Copyright (C) 2008-2009 Justin Maggard 3 * 4 * This program is free software; you can redistribute it and/or modify 5 * it under the terms of the GNU General Public License as published by 6 * the Free Software Foundation; either version 2 of the License, or 7 * (at your option) any later version. 8 * 9 * This program is distributed in the hope that it will be useful, 10 * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 * GNU General Public License for more details. 13 * 14 * You should have received a copy of the GNU General Public License 15 * along with this program; if not, write to the Free Software 16 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 17 */ 18#include <stdio.h> 19#include <unistd.h> 20#include "sql.h" 21#include "log.h" 22 23int 24sql_exec(sqlite3 *db, const char *fmt, ...) 25{ 26 int ret; 27 char *errMsg = NULL; 28 char *sql; 29 va_list ap; 30 //DPRINTF(E_DEBUG, L_DB_SQL, "SQL: %s\n", sql); 31 32 va_start(ap, fmt); 33 34 sql = sqlite3_vmprintf(fmt, ap); 35 ret = sqlite3_exec(db, sql, 0, 0, &errMsg); 36 if( ret != SQLITE_OK ) 37 { 38 DPRINTF(E_ERROR, L_DB_SQL, "SQL ERROR %d [%s]\n%s\n", ret, errMsg, sql); 39 if (errMsg) 40 sqlite3_free(errMsg); 41 } 42 sqlite3_free(sql); 43 44 return ret; 45} 46 47int 48sql_get_table(sqlite3 *db, const char *sql, char ***pazResult, int *pnRow, int *pnColumn) 49{ 50 int ret; 51 char *errMsg = NULL; 52 //DPRINTF(E_DEBUG, L_DB_SQL, "SQL: %s\n", sql); 53 54 ret = sqlite3_get_table(db, sql, pazResult, pnRow, pnColumn, &errMsg); 55 if( ret != SQLITE_OK ) 56 { 57 DPRINTF(E_ERROR, L_DB_SQL, "SQL ERROR %d [%s]\n%s\n", ret, errMsg, sql); 58 if (errMsg) 59 sqlite3_free(errMsg); 60 } 61 62 return ret; 63} 64 65int 66sql_get_int_field(sqlite3 *db, const char *fmt, ...) 67{ 68 va_list ap; 69 int counter, result; 70 char *sql; 71 int ret; 72 sqlite3_stmt *stmt; 73 74 va_start(ap, fmt); 75 76 sql = sqlite3_vmprintf(fmt, ap); 77 78 //DPRINTF(E_DEBUG, L_DB_SQL, "sql: %s\n", sql); 79 80 switch (sqlite3_prepare_v2(db, sql, -1, &stmt, NULL)) 81 { 82 case SQLITE_OK: 83 break; 84 default: 85 DPRINTF(E_ERROR, L_DB_SQL, "prepare failed: %s\n", sqlite3_errmsg(db)); 86 sqlite3_free(sql); 87 return -1; 88 } 89 sqlite3_free(sql); 90 91 for (counter = 0; 92 ((result = sqlite3_step(stmt)) == SQLITE_BUSY || result == SQLITE_LOCKED) && counter < 2; 93 counter++) { 94 /* While SQLITE_BUSY has a built in timeout, 95 SQLITE_LOCKED does not, so sleep */ 96 if (result == SQLITE_LOCKED) 97 sleep(1); 98 } 99 100 switch (result) 101 { 102 case SQLITE_DONE: 103 /* no rows returned */ 104 ret = 0; 105 break; 106 case SQLITE_ROW: 107 if (sqlite3_column_type(stmt, 0) == SQLITE_NULL) 108 { 109 ret = 0; 110 break; 111 } 112 ret = sqlite3_column_int(stmt, 0); 113 break; 114 default: 115 DPRINTF(E_WARN, L_DB_SQL, "%s: step failed: %s\n", __func__, sqlite3_errmsg(db)); 116 ret = -1; 117 break; 118 } 119 120 sqlite3_finalize(stmt); 121 return ret; 122} 123 124