[db] Add methods db_mprintf() and db_snprintf() which wrap sqlite

This commit is contained in:
ejurgensen 2017-10-14 10:14:09 +02:00
parent 4d47307a02
commit 47585d0d03
3 changed files with 56 additions and 8 deletions

View File

@ -353,6 +353,48 @@ db_escape_string(const char *str)
return ret; return ret;
} }
// Basically a wrapper for sqlite3_mprintf()
char *
db_mprintf(const char *fmt, ...)
{
char *query;
char *ret;
va_list va;
va_start(va, fmt);
ret = sqlite3_vmprintf(fmt, va);
if (!ret)
{
DPRINTF(E_FATAL, L_MISC, "Out of memory for db_mprintf\n");
abort();
}
va_end(va);
query = strdup(ret);
sqlite3_free(ret);
return query;
}
int
db_snprintf(char *s, int n, const char *fmt, ...)
{
char *ret;
va_list va;
if (n < 2)
return -1;
va_start(va, fmt);
ret = sqlite3_vsnprintf(n, s, fmt, va);
va_end(va);
if (!ret || (strlen(ret) == n - 1))
return -1;
return 0;
}
void void
free_pi(struct pairing_info *pi, int content_only) free_pi(struct pairing_info *pi, int content_only)
{ {

View File

@ -424,7 +424,13 @@ struct db_queue_item
}; };
char * char *
db_escape_string(const char *str); db_escape_string(const char *str); // TODO Remove this, use db_mprintf instead
char *
db_mprintf(const char *fmt, ...);
int
db_snprintf(char *s, int n, const char *fmt, ...);
void void
free_pi(struct pairing_info *pi, int content_only); free_pi(struct pairing_info *pi, int content_only);

View File

@ -149,13 +149,6 @@ process_regular_file(int pl_id, char *path)
path[i] = '/'; path[i] = '/';
} }
ret = snprintf(filter, sizeof(filter), "(f.fname = '%s')", filename_from_path(path)); // TODO make case insensitive?
if (ret < 0 || ret >= sizeof(filter))
{
DPRINTF(E_LOG, L_SCAN, "Playlist contains bad filename: '%s'\n", path);
return -1;
}
// Fast path - only works if there are not multiple items in the lib with the // Fast path - only works if there are not multiple items in the lib with the
// same filename // same filename
/* ret = db_pl_add_item_byfile(pl_id, filename_from_path(path)); /* ret = db_pl_add_item_byfile(pl_id, filename_from_path(path));
@ -165,6 +158,13 @@ process_regular_file(int pl_id, char *path)
DPRINTF(E_DBG, L_SCAN, "Fast path adding '%s' to playlist did not work (ret=%d), now searching\n", path, ret); DPRINTF(E_DBG, L_SCAN, "Fast path adding '%s' to playlist did not work (ret=%d), now searching\n", path, ret);
*/ */
ret = db_snprintf(filter, sizeof(filter), "f.fname = '%q'", filename_from_path(path)); // TODO make case insensitive?
if (ret < 0)
{
DPRINTF(E_LOG, L_SCAN, "Path in playlist is too long: '%s'\n", path);
return -1;
}
memset(&qp, 0, sizeof(struct query_params)); memset(&qp, 0, sizeof(struct query_params));
qp.type = Q_BROWSE_PATH; qp.type = Q_BROWSE_PATH;