Class: RDO::SQLite::StatementExecutor

Inherits:
Object
  • Object
show all
Defined in:
ext/rdo_sqlite/statements.c

Instance Method Summary collapse

Constructor Details

#initialize(driver) ⇒ Object

Initialize the statement (prepare it)



103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
# File 'ext/rdo_sqlite/statements.c', line 103

static VALUE rdo_sqlite_statement_executor_initialize(VALUE self, VALUE driver) {
  rb_iv_set(self, "driver", driver); // GC safety
  RDOSQLiteStatementExecutor * executor;
  Data_Get_Struct(self, RDOSQLiteStatementExecutor, executor);

  if (!(executor->driver->is_open)) {
    RDO_ERROR("Cannot execute prepare statement: database is not open");
  }

  const char * tail;

  int status = sqlite3_prepare_v2(executor->driver->db,
      executor->cmd,
      (int) strlen(executor->cmd) + 1,
      &(executor->stmt),
      &tail);

  if ((status != SQLITE_OK) || sqlite3_errcode(executor->driver->db)) {
    RDO_ERROR("Failed to prepare statement: %s",
        sqlite3_errmsg(executor->driver->db));
  }

  if (!rdo_sqlite_inert_p((char *) tail)) {
    rb_raise(rb_eArgError, "Only one statement can be executed at a time");
  }

  return self;
}

Instance Method Details

#commandObject

Get the command this statement will execute



133
134
135
136
137
# File 'ext/rdo_sqlite/statements.c', line 133

static VALUE rdo_sqlite_statement_executor_command(VALUE self) {
  RDOSQLiteStatementExecutor * executor;
  Data_Get_Struct(self, RDOSQLiteStatementExecutor, executor);
  return rb_str_new2(executor->cmd);
}

#execute(*, self) ⇒ Object

Execute the statement with the given bind parameters and return a Result



238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
# File 'ext/rdo_sqlite/statements.c', line 238

static VALUE rdo_sqlite_statement_executor_execute(int argc, VALUE * args, VALUE self) {
  RDOSQLiteStatementExecutor * executor;
  Data_Get_Struct(self, RDOSQLiteStatementExecutor, executor);

  if (!(executor->driver->is_open)) {
    RDO_ERROR("Cannot execute execute statement: database is not open");
  }

  rdo_sqlite_statement_bind_args(executor->stmt, argc, args);

  VALUE tuples = rdo_sqlite_statement_extract_tuples(executor->driver->db, executor->stmt);
  VALUE info   = rdo_sqlite_result_info(executor->driver->db, executor->stmt);
  sqlite3_reset(executor->stmt);

  return RDO_RESULT(tuples, info);
}