Class: Swift::Statement

Inherits:
Result
  • Object
show all
Defined in:
ext/statement.cc

Instance Method Summary collapse

Methods inherited from Result

#clone, #columns, #dup, #each, #fields, #finish, #insert_id, #rows

Constructor Details

#initialize(adapter, sql) ⇒ Object



70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'ext/statement.cc', line 70

VALUE statement_initialize(VALUE self, VALUE adapter, VALUE sql) {
  dbi::Handle *handle = adapter_handle(adapter);

  if (NIL_P(adapter)) rb_raise(eSwiftArgumentError, "Statement#new called without an Adapter instance.");
  if (NIL_P(sql))     rb_raise(eSwiftArgumentError, "Statement#new called without a command.");

  try {
    // needs to happen before wrapping in case it raises errors.
    dbi::AbstractStatement *statement  = handle->conn()->prepare(CSTRING(sql));
    StatementWrapper *statement_handle = new StatementWrapper;
    statement_handle->statement        = statement;
    statement_handle->adapter          = adapter;
    statement_handle->free             = true;
    DATA_PTR(self)                     = statement_handle;
  }
  CATCH_DBI_EXCEPTIONS();

  return Qnil;
}

Instance Method Details

#execute(*args) ⇒ Object

TODO: Change bind_values to an array in the interface? Avoid array -> splat -> array.



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'ext/statement.cc', line 50

static VALUE statement_execute(int argc, VALUE *argv, VALUE self) {
  VALUE bind_values, block;
  rb_scan_args(argc, argv, "0*&", &bind_values, &block);

  dbi::AbstractStatement *statement = (dbi::AbstractStatement*)statement_handle(self);
  try {
    Query query;
    query.statement = statement;
    if (RARRAY_LEN(bind_values) > 0) query_bind_values(&query, bind_values, statement->driver());
    if (dbi::_trace)                 dbi::logMessage(dbi::_trace_fd, dbi::formatParams(statement->command(), query.bind));

    if (rb_thread_blocking_region(((VALUE (*)(void*))query_execute_statement), &query, RUBY_UBF_IO, 0) == Qfalse)
      rb_raise(eSwiftRuntimeError, "%s", query.error);
  }
  CATCH_DBI_EXCEPTIONS();

  if (rb_block_given_p()) return result_each(self);
  return self;
}