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/>.
26 sql_exec(sqlite3
*db
, const char *fmt
, ...)
32 //DPRINTF(E_DEBUG, L_DB_SQL, "SQL: %s\n", sql);
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
);
50 sql_get_table(sqlite3
*db
, const char *sql
, char ***pazResult
, int *pnRow
, int *pnColumn
)
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
);
68 sql_get_int_field(sqlite3
*db
, const char *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
))
87 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", __func__
, sqlite3_errmsg(db
));
122 sqlite3_finalize(stmt
);
127 sql_get_text_field(void *db
, const char *fmt
, ...)
130 int counter
, result
, len
;
139 DPRINTF(E_WARN
, L_DB_SQL
, "%s: db is NULL", __func__
);
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
))
152 DPRINTF(E_ERROR
, L_DB_SQL
, "prepare failed: %s\n%s\n", sqlite3_errmsg(db
), sql
);
159 ((result
= sqlite3_step(stmt
)) == SQLITE_BUSY
|| result
== SQLITE_LOCKED
) && counter
< 2;
162 /* While SQLITE_BUSY has a built in timeout,
163 * SQLITE_LOCKED does not, so sleep */
164 if (result
== SQLITE_LOCKED
)
171 /* no rows returned */
176 if (sqlite3_column_type(stmt
, 0) == SQLITE_NULL
)
182 len
= sqlite3_column_bytes(stmt
, 0);
183 if ((str
= sqlite3_malloc(len
+ 1)) == NULL
)
185 DPRINTF(E_ERROR
, L_DB_SQL
, "malloc failed");
189 strncpy(str
, (char *)sqlite3_column_text(stmt
, 0), len
+ 1);
193 DPRINTF(E_WARN
, L_DB_SQL
, "%s: step failed: %s", __func__
, sqlite3_errmsg(db
));
198 sqlite3_finalize(stmt
);