static int queryTokenizer( sqlite4 *db, char *zName, const sqlite4_tokenizer_module **pp ){ int rc; sqlite4_stmt *pStmt; const char zSql[] = "SELECT fts3_tokenizer(?)"; *pp = 0; rc = sqlite4_prepare_v2(db, zSql, -1, &pStmt, 0); if( rc!=SQLITE4_OK ){ return rc; } sqlite4_bind_text(pStmt, 1, zName, -1, SQLITE4_STATIC); if( SQLITE4_ROW==sqlite4_step(pStmt) ){ if( sqlite4_column_type(pStmt, 0)==SQLITE4_BLOB ){ memcpy((void *)pp, sqlite4_column_blob(pStmt, 0), sizeof(*pp)); } } return sqlite4_finalize(pStmt); }
static int registerTokenizer( sqlite4 *db, char *zName, const sqlite4_tokenizer_module *p ){ int rc; sqlite4_stmt *pStmt; const char zSql[] = "SELECT fts3_tokenizer(?, ?)"; rc = sqlite4_prepare_v2(db, zSql, -1, &pStmt, 0); if( rc!=SQLITE4_OK ){ return rc; } sqlite4_bind_text(pStmt, 1, zName, -1, SQLITE4_STATIC); sqlite4_bind_blob(pStmt, 2, &p, sizeof(p), SQLITE4_STATIC); sqlite4_step(pStmt); return sqlite4_finalize(pStmt); }
int tbl_to_db(struct freeq_ctx *ctx, struct freeq_table *tbl, sqlite4 *mDb) { sqlite4_stmt *stmt; GString *sql = g_string_sized_new(255); GSList *colp[tbl->numcols]; memset(colp, 0, tbl->numcols * sizeof(GSList *) ); for (int j = 0; j < tbl->numcols; j++) colp[j] = tbl->columns[j].data; int res; freeq_table_print(ctx, tbl, stdout); if (sqlite4_exec(mDb, "BEGIN TRANSACTION;", NULL, NULL) != SQLITE4_OK) { dbg(ctx, "unable to start transaction: %s\n", sqlite4_errmsg(mDb)); return 1; } g_string_printf(sql, "DROP TABLE %s;", tbl->name); if (sqlite4_exec(mDb, sql->str, NULL, NULL) != SQLITE4_OK) dbg(ctx, "failed to drop table, ignoring\n"); table_ddl(ctx, tbl, sql); if (sqlite4_exec(mDb, sql->str, NULL, NULL) != SQLITE4_OK) { dbg(ctx, "failed to create table, rolling back\n"); sqlite4_exec(mDb, "ROLLBACK;", NULL, NULL); g_string_free(sql, 1); return 1; } ddl_insert(ctx, tbl, sql); if ((res = sqlite4_prepare(mDb, sql->str, sql->len, &stmt, NULL)) != SQLITE4_OK) { dbg(ctx, "failed to create statement (%d), rolling back\n", res); sqlite4_exec(mDb, "ROLLBACK;", NULL, NULL); g_string_free(sql,1); return 1; } g_string_free(sql,1); for (uint32_t i = 0; i < tbl->numrows; i++) { for (uint32_t j = 0; j < tbl->numcols; j++) { switch (tbl->columns[j].coltype) { case FREEQ_COL_STRING: res = sqlite4_bind_text(stmt, j+1, colp[j]->data == NULL ? "" : colp[j]->data, colp[j]->data == NULL ? 0 : strlen(colp[j]->data), SQLITE4_TRANSIENT, NULL); if (res != SQLITE4_OK) { dbg(ctx, "stmt: %s\n", (char *)stmt); dbg(ctx, "row %d failed binding string column %d %s: %s (%d)\n", i, j, (char *)colp[j]->data, sqlite4_errmsg(mDb), res); } break; case FREEQ_COL_NUMBER: res = sqlite4_bind_int(stmt, j, GPOINTER_TO_INT(colp[j]->data)); if (res != SQLITE4_OK) { dbg(ctx, "row %d failed bind: %s\n", i, sqlite4_errmsg(mDb)); } break; default: break; } colp[j] = g_slist_next(colp[j]); } if (sqlite4_step(stmt) != SQLITE4_DONE) { dbg(ctx, "execute failed: %s\n", sqlite4_errmsg(mDb)); sqlite4_exec(mDb, "ROLLBACK;", NULL, NULL); sqlite4_finalize(stmt); return -1; } else { sqlite4_reset(stmt); } } dbg(ctx, "committing transaction\n"); res = sqlite4_exec(mDb, "COMMIT TRANSACTION;", NULL, NULL); dbg(ctx, "result of commit was %d\n", res); sqlite4_finalize(stmt); return 0; }