This is the low level function that we use to model all the higher level * functions. * * Queries can contain ?s ?b ?i and ?d special specifiers that are bound to * the SQL query, and must be present later as additional arguments after * the 'sql' argument. * * ?s -- TEXT field: char* argument. * ?b -- Blob field: char* argument followed by size_t argument. * ?i -- INT f
| 37 | * SQLITE_ROW, since in such case row->stmt is set to NULL. |
| 38 | */ |
| 39 | int sqlGenericQuery(sqlite3 *dbhandle, sqlRow *row, const char *sql, va_list ap) { |
| 40 | int rc = SQLITE_ERROR; |
| 41 | sqlite3_stmt *stmt = NULL; |
| 42 | sds query = sdsempty(); |
| 43 | if (row) row->stmt = NULL; /* On error sqlNextRow() should return false. */ |
| 44 | |
| 45 | /* We need to build the query, substituting the following three |
| 46 | * classes of patterns with just "?", remembering the order and |
| 47 | * type, and later using the sql3 binding API in order to prepare |
| 48 | * the query: |
| 49 | * |
| 50 | * ?s string |
| 51 | * ?b blob (varargs must have char ptr and size_t len) |
| 52 | * ?i int64_t |
| 53 | * ?d double |
| 54 | */ |
| 55 | char spec[SQL_MAX_SPEC]; |
| 56 | int numspec = 0; |
| 57 | const char *p = sql; |
| 58 | while(p[0]) { |
| 59 | if (p[0] == '?') { |
| 60 | if (p[1] == 's' || p[1] == 'i' || p[1] == 'd' || p[1] == 'b') { |
| 61 | if (numspec == SQL_MAX_SPEC) goto error; |
| 62 | spec[numspec++] = p[1]; |
| 63 | } else { |
| 64 | goto error; |
| 65 | } |
| 66 | query = sdscatlen(query,"?",1); |
| 67 | p++; /* Skip the specifier. */ |
| 68 | } else { |
| 69 | query = sdscatlen(query,p,1); |
| 70 | } |
| 71 | p++; |
| 72 | } |
| 73 | |
| 74 | /* Prepare the query and bind the query arguments. */ |
| 75 | rc = sqlite3_prepare_v2(dbhandle,query,-1,&stmt,NULL); |
| 76 | if (rc != SQLITE_OK) { |
| 77 | if (SHOW_QUERY_ERRORS) printf("%p: Query error: %s: %s\n", |
| 78 | (void*)dbhandle, |
| 79 | query, |
| 80 | sqlite3_errmsg(dbhandle)); |
| 81 | goto error; |
| 82 | } |
| 83 | |
| 84 | for (int j = 0; j < numspec; j++) { |
| 85 | switch(spec[j]) { |
| 86 | case 'b': { |
| 87 | char *blobptr = va_arg(ap,char*); |
| 88 | size_t bloblen = va_arg(ap,size_t); |
| 89 | rc = sqlite3_bind_blob64(stmt,j+1,blobptr,bloblen,NULL); |
| 90 | } |
| 91 | break; |
| 92 | case 's': rc = sqlite3_bind_text(stmt,j+1,va_arg(ap,char*),-1,NULL); |
| 93 | break; |
| 94 | case 'i': rc = sqlite3_bind_int64(stmt,j+1,va_arg(ap,int64_t)); |
| 95 | break; |
| 96 | case 'd': rc = sqlite3_bind_double(stmt,j+1,va_arg(ap,double)); |
no test coverage detected