ext/statement.cc in swift-0.7.2 vs ext/statement.cc in swift-0.8.0

- old
+ new

@@ -10,14 +10,11 @@ rb_gc_mark(handle->adapter); } void statement_free(StatementWrapper *handle) { if (handle) { - if (handle->free) { - handle->statement->cleanup(); - delete handle->statement; - } + if (handle->free) delete handle->statement; delete handle; } } VALUE statement_alloc(VALUE klass) { @@ -53,22 +50,36 @@ 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 (RARRAY_LEN(bind_values) > 0) query_bind_values(&query, bind_values); 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; + StatementWrapper *handle; + Data_Get_Struct(self, StatementWrapper, handle); + + VALUE result = result_wrap_handle(cSwiftResult, handle->adapter, statement->result(), true); + rb_iv_set(result, "@scheme", rb_iv_get(self, "@scheme")); + + return rb_block_given_p() ? result_each(result) : result; } +VALUE statement_insert_id(VALUE self) { + dbi::AbstractStatement *statement = statement_handle(self); + try { + return SIZET2NUM(statement->lastInsertID()); + } + CATCH_DBI_EXCEPTIONS(); + return Qnil; +} + 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."); @@ -88,25 +99,11 @@ } void init_swift_statement() { VALUE mSwift = rb_define_module("Swift"); - /* - TODO Inheritance confusion. - - dbic++ has this, - dbi::Statement < dbi::AbstractStatement - dbi::AbstractStatement < dbi::AbstractResult - - Swift has this, - Statement < Result - - Not sure if this hierarchy is correct or perfect. I reckon Statement should not - inherit Result and just return a Result on execute() - maybe cleaner but very - inefficient when doing tons on non-select style queries. - */ - - cSwiftStatement = rb_define_class_under(mSwift, "Statement", cSwiftResult); - rb_define_method(cSwiftStatement, "execute", RUBY_METHOD_FUNC(statement_execute), -1); - rb_define_method(cSwiftStatement, "initialize", RUBY_METHOD_FUNC(statement_initialize), 2); + cSwiftStatement = rb_define_class_under(mSwift, "Statement", rb_cObject); + rb_define_method(cSwiftStatement, "execute", RUBY_METHOD_FUNC(statement_execute), -1); + rb_define_method(cSwiftStatement, "initialize", RUBY_METHOD_FUNC(statement_initialize), 2); + rb_define_method(cSwiftStatement, "insert_id", RUBY_METHOD_FUNC(statement_insert_id), 0); rb_define_alloc_func(cSwiftStatement, statement_alloc); }