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);
}
Exemplo n.º 2
0
int pysqlite_statement_create(pysqlite_Statement* self, pysqlite_Connection* connection, PyObject* sql)
{
    const char* tail;
    int rc;
    PyObject* sql_str;
    char* sql_cstr;

    self->st = NULL;
    self->in_use = 0;

    if (PyString_Check(sql)) {
        sql_str = sql;
        Py_INCREF(sql_str);
    } else if (PyUnicode_Check(sql)) {
        sql_str = PyUnicode_AsUTF8String(sql);
        if (!sql_str) {
            rc = PYSQLITE4_SQL_WRONG_TYPE;
            return rc;
        }
    } else {
        rc = PYSQLITE4_SQL_WRONG_TYPE;
        return rc;
    }

    self->in_weakreflist = NULL;
    self->sql = sql_str;

    sql_cstr = PyString_AsString(sql_str);

    Py_BEGIN_ALLOW_THREADS
    rc = sqlite4_prepare(connection->db,
                         sql_cstr,
                         -1,
                         &self->st,
                         &tail);
    Py_END_ALLOW_THREADS

    self->db = connection->db;

    if (rc == SQLITE4_OK && pysqlite_check_remaining_sql(tail)) {
        (void)sqlite4_finalize(self->st);
        self->st = NULL;
        rc = PYSQLITE4_TOO_MUCH_SQL;
    }

    return rc;
}
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);
}
Exemplo n.º 4
0
void* sqlhandler(void *arg) {
        char sql[MAX_MSG];
        int ret, err;
        SSL *ssl;
        sqlite4_stmt *pStmt;

        struct conn_ctx *conn = (struct conn_ctx *)arg;
        struct freeq_ctx *freeqctx = conn->srvctx->freeqctx;
        BIO *client = conn->client;

        if (!(ssl = freeq_ssl_new(freeqctx)))
        {
                err(freeqctx, "couldn't allocate new ssl instance");
                BIO_free_all(client);
                free(conn);
                pthread_exit(NULL);
        }

        SSL_set_bio(ssl, client, client);
        if (SSL_accept(ssl) <= 0)
        {
                int_error("Error accepting SSL connection");
                BIO_free_all(client);
                free(conn);
                pthread_exit(NULL);
        }

        if ((err = post_connection_check(freeqctx, ssl, "localhost")) != X509_V_OK)
        {
                err(freeqctx, "error: peer certificate: %s\n", X509_verify_cert_error_string(err));
                BIO_free_all(client);
                free(conn);
                pthread_exit(NULL);
        }

        BIO *b, *ssl_bio;
        b = BIO_new(BIO_f_buffer());
        ssl_bio = BIO_new(BIO_f_ssl());
        BIO_set_ssl(ssl_bio, ssl, BIO_CLOSE);
        BIO_push(b, ssl_bio);

        memset(sql, 0, MAX_MSG);
        ret = BIO_gets(b, sql, MAX_MSG);

        ret = sqlite4_prepare(pDb, sql, strlen(sql), &pStmt, 0);
        if (ret != SQLITE4_OK)
        {
                dbg(freeqctx, "prepare failed for %s sending error, ret was %d\n", sql, ret);
                //freeq_error_write_sock(freeqctx, sqlite4_errmsg(pDb), b);
                dbg(freeqctx, sqlite4_errmsg(pDb));
                //BIO_printf(b, "error: %s\n", sqlite4_errmsg(pDb));
                sqlite4_finalize(pStmt);
                //pthread_exit(FREEQ_ERR);
        }

        freeq_sqlite_to_bio(freeqctx, b, pStmt);

        SSL_shutdown(ssl);
        SSL_free(ssl);

        BIO_free_all(client);
        free(conn);
        pthread_exit(FREEQ_OK);

}
Exemplo n.º 5
0
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;
}