sql: introduce sql_get_int64_field()
Returns int64_t instead of int.
This commit is contained in:
parent
27a29f9e92
commit
0721528bcd
59
sql.c
59
sql.c
@ -124,6 +124,65 @@ sql_get_int_field(sqlite3 *db, const char *fmt, ...)
|
|||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
int64_t
|
||||||
|
sql_get_int64_field(sqlite3 *db, const char *fmt, ...)
|
||||||
|
{
|
||||||
|
va_list ap;
|
||||||
|
int counter, result;
|
||||||
|
char *sql;
|
||||||
|
int64_t ret;
|
||||||
|
sqlite3_stmt *stmt;
|
||||||
|
|
||||||
|
va_start(ap, fmt);
|
||||||
|
sql = sqlite3_vmprintf(fmt, ap);
|
||||||
|
va_end(ap);
|
||||||
|
|
||||||
|
//DPRINTF(E_DEBUG, L_DB_SQL, "sql: %s\n", sql);
|
||||||
|
|
||||||
|
switch (sqlite3_prepare_v2(db, sql, -1, &stmt, NULL))
|
||||||
|
{
|
||||||
|
case SQLITE_OK:
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
DPRINTF(E_ERROR, L_DB_SQL, "prepare failed: %s\n%s\n", sqlite3_errmsg(db), sql);
|
||||||
|
sqlite3_free(sql);
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
for (counter = 0;
|
||||||
|
((result = sqlite3_step(stmt)) == SQLITE_BUSY || result == SQLITE_LOCKED) && counter < 2;
|
||||||
|
counter++) {
|
||||||
|
/* While SQLITE_BUSY has a built in timeout,
|
||||||
|
SQLITE_LOCKED does not, so sleep */
|
||||||
|
if (result == SQLITE_LOCKED)
|
||||||
|
sleep(1);
|
||||||
|
}
|
||||||
|
|
||||||
|
switch (result)
|
||||||
|
{
|
||||||
|
case SQLITE_DONE:
|
||||||
|
/* no rows returned */
|
||||||
|
ret = 0;
|
||||||
|
break;
|
||||||
|
case SQLITE_ROW:
|
||||||
|
if (sqlite3_column_type(stmt, 0) == SQLITE_NULL)
|
||||||
|
{
|
||||||
|
ret = 0;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
ret = sqlite3_column_int64(stmt, 0);
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
DPRINTF(E_WARN, L_DB_SQL, "%s: step failed: %s\n%s\n", __func__, sqlite3_errmsg(db), sql);
|
||||||
|
ret = -1;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
sqlite3_free(sql);
|
||||||
|
sqlite3_finalize(stmt);
|
||||||
|
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
|
||||||
char *
|
char *
|
||||||
sql_get_text_field(sqlite3 *db, const char *fmt, ...)
|
sql_get_text_field(sqlite3 *db, const char *fmt, ...)
|
||||||
{
|
{
|
||||||
|
21
sql.h
21
sql.h
@ -24,6 +24,7 @@
|
|||||||
#ifndef __SQL_H__
|
#ifndef __SQL_H__
|
||||||
#define __SQL_H__
|
#define __SQL_H__
|
||||||
|
|
||||||
|
#include <stdint.h>
|
||||||
#include <sqlite3.h>
|
#include <sqlite3.h>
|
||||||
|
|
||||||
#ifndef HAVE_SQLITE3_MALLOC
|
#ifndef HAVE_SQLITE3_MALLOC
|
||||||
@ -33,19 +34,11 @@
|
|||||||
#define sqlite3_prepare_v2 sqlite3_prepare
|
#define sqlite3_prepare_v2 sqlite3_prepare
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
int
|
int sql_exec(sqlite3 *db, const char *fmt, ...);
|
||||||
sql_exec(sqlite3 *db, const char *fmt, ...);
|
int sql_get_table(sqlite3 *db, const char *zSql, char ***pazResult, int *pnRow, int *pnColumn);
|
||||||
|
int sql_get_int_field(sqlite3 *db, const char *fmt, ...);
|
||||||
int
|
int64_t sql_get_int64_field(sqlite3 *db, const char *fmt, ...);
|
||||||
sql_get_table(sqlite3 *db, const char *zSql, char ***pazResult, int *pnRow, int *pnColumn);
|
char * sql_get_text_field(sqlite3 *db, const char *fmt, ...);
|
||||||
|
int db_upgrade(sqlite3 *db);
|
||||||
int
|
|
||||||
sql_get_int_field(sqlite3 *db, const char *fmt, ...);
|
|
||||||
|
|
||||||
char *
|
|
||||||
sql_get_text_field(sqlite3 *db, const char *fmt, ...);
|
|
||||||
|
|
||||||
int
|
|
||||||
db_upgrade(sqlite3 *db);
|
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
Loading…
x
Reference in New Issue
Block a user