ext/Connection.c in rubyfb-0.5.9 vs ext/Connection.c in rubyfb-0.6

- old
+ new

@@ -39,18 +39,16 @@ static VALUE closeConnection(VALUE); static VALUE getConnectionDatabase(VALUE); static VALUE startConnectionTransaction(VALUE); static VALUE connectionToString(VALUE); static VALUE executeOnConnection(VALUE, VALUE, VALUE); +static VALUE executeOnConnectionWithParams(VALUE, VALUE, VALUE, VALUE); static VALUE executeOnConnectionImmediate(VALUE, VALUE); +static VALUE createStatement(VALUE, VALUE); static VALUE getConnectionUser(VALUE); VALUE startTransactionBlock(VALUE); VALUE startTransactionRescue(VALUE, VALUE); -VALUE executeBlock(VALUE); -VALUE executeRescue(VALUE, VALUE); -VALUE executeImmediateBlock(VALUE); -VALUE executeImmediateRescue(VALUE, VALUE); char *createDPB(VALUE, VALUE, VALUE, short *); /* Globals. */ VALUE cConnection; @@ -298,60 +296,60 @@ * * @return Either a ResultSet object for a query statement or nil for a * non-query statement. * */ - static VALUE executeOnConnection(VALUE self, VALUE sql, VALUE transaction) { - return rb_execute_sql(self, transaction, sql); +VALUE executeOnConnection(VALUE self, VALUE sql, VALUE transaction) { + return executeOnConnectionWithParams(self, sql, rb_ary_new(), transaction); } /** + * This function provides the execute_for method for the Connection class. + * + * @param self A reference to the connection object to perform the + * execution through. + * @param sql A reference to the SQL statement to be executed. + * @param params Array containing parameter values for the statement. + * @param transaction A reference to the transction that the statement will + * be executed under. + * + * @return Either a ResultSet object for a query statement or nil for a + * non-query statement. + * + */ +VALUE executeOnConnectionWithParams(VALUE self, VALUE sql, VALUE params, VALUE transaction) { + return rb_execute_sql(self, sql, params, transaction); +} + +/** * This function provides the execute_immediate method for the Connection class. * * @param self A reference to the connection object to perform the execution * through. * @param sql A reference to the SQL statement to be executed. * * @return Always returns nil. * */ -static VALUE executeOnConnectionImmediate(VALUE self, VALUE sql) { - VALUE transaction = rb_transaction_new(self), - set = Qnil, - results = Qnil, - array = rb_ary_new(), - dialect = INT2FIX(3), - statement = rb_statement_new(self, transaction, sql, dialect); +VALUE executeOnConnectionImmediate(VALUE self, VALUE sql) { + return executeOnConnection(self, sql, Qnil); +} - rb_ary_push(array, self); - rb_ary_push(array, transaction); - rb_ary_push(array, sql); - rb_ary_push(array, statement); - - set = rb_rescue(executeBlock, array, executeRescue, array); - if(set != Qnil) { - if(TYPE(set) == T_DATA && - RDATA(set)->dfree == (RUBY_DATA_FUNC)resultSetFree) { - rb_assign_transaction(set, transaction); - if(rb_block_given_p()) { - results = rb_rescue(executeImmediateBlock, set, - executeImmediateRescue, set); - } else { - results = set; - } - } else { - rb_funcall(transaction, rb_intern("commit"), 0); - results = set; - } - } else { - rb_funcall(transaction, rb_intern("commit"), 0); - } - - return(results); +/** + * Create statement object + * + * @param self A reference to the connection object to perform the execution + * through. + * @param sql A reference to the SQL statement to be executed. + * + * @return Reference to the statement object. + * + */ +VALUE createStatement(VALUE self, VALUE sql) { + return rb_statement_new(self, sql); } - /** * This function provides the user accessor method for the Connection object. * * @param self A reference to the Connection object to fetch theuser from. * @@ -396,98 +394,11 @@ rb_funcall(transaction, rb_intern("rollback"), 0); rb_exc_raise(error); return(Qnil); } - /** - * This function is used to wrap the call to the executeOnConnection() function - * made by the executeOnConnectionImmediate() function to help insure that the - * transaction is rolled back in case of an error. - * - * @param array An array of the parameters for the function to use. - * - * @return The ResultSet object generated by execution or nil if it wasn't a - * query. - * - */ -VALUE executeBlock(VALUE array) { - VALUE result = Qnil, - connection = rb_ary_entry(array, 0), - transaction = rb_ary_entry(array, 1), - sql = rb_ary_entry(array, 2), - statement = rb_ary_entry(array, 3); - - result = rb_execute_statement(statement); - rb_statement_close(statement); - - return(result); -} - - -/** - * This function provides clean up for the execution of a block associated - * with the execute method. - * - * @param array An array of the parameters for the function to use. - * @param error A reference to details relating to the exception raised. - * - * @return Would always returns nil except that it always raises an exception. - * - */ -VALUE executeRescue(VALUE array, VALUE error) { - VALUE transaction = rb_ary_entry(array, 1), - statement = rb_ary_entry(array, 3); - - rb_funcall(transaction, rb_intern("rollback"), 0); - rb_statement_close(statement); - rb_exc_raise(error); - return(Qnil); -} - - -/** - * This function is executed to process a block passed to the execute_immedate - * method. - * - * @param set A reference to the ResultSet to be processed by the block. - * - * @return A reference to the return value generated by the block. - * - */ -VALUE executeImmediateBlock(VALUE set) { - VALUE result = Qnil, - row = rb_funcall(set, rb_intern("fetch"), 0); - - while(row != Qnil) { - result = rb_yield(row); - row = rb_funcall(set, rb_intern("fetch"), 0); - } - rb_funcall(set, rb_intern("close"), 0); - - return(result); -} - - -/** - * This function provides clean up for the execution of a block associated - * with the execute_immediate method. - * - * @param set A reference to the ResultSet object for the block. - * @param error A reference to details relating to the exception raised. - * - * @return Would always returns nil except that it always raises an exception. - * - */ -VALUE executeImmediateRescue(VALUE set, VALUE error) { - rb_funcall(set, rb_intern("close"), 0); - rb_exc_raise(error); - return(Qnil); -} - - -/** * This method creates a database parameter buffer to be used in creating a * database connection. * * @param user A reference to a string containing the user name to be used * in making the connection. @@ -674,10 +585,11 @@ return(connection); } + /** * This function is called to record the beginnings of a transactions against * a related connection. * * @param transaction A reference to the newly created Transaction object. @@ -758,10 +670,12 @@ rb_define_method(cConnection, "close", closeConnection, 0); rb_define_method(cConnection, "database", getConnectionDatabase, 0); rb_define_method(cConnection, "start_transaction", startConnectionTransaction, 0); rb_define_method(cConnection, "to_s", connectionToString, 0); rb_define_method(cConnection, "execute", executeOnConnection, 2); + rb_define_method(cConnection, "execute_for", executeOnConnectionWithParams, 3); rb_define_method(cConnection, "execute_immediate", executeOnConnectionImmediate, 1); + rb_define_method(cConnection, "create_statement", createStatement, 1); rb_define_const(cConnection, "MARK_DATABASE_DAMAGED", INT2FIX(isc_dpb_damaged)); rb_define_const(cConnection, "WRITE_POLICY", INT2FIX(isc_dpb_force_write)); rb_define_const(cConnection, "CHARACTER_SET", INT2FIX(isc_dpb_lc_ctype)); rb_define_const(cConnection, "MESSAGE_FILE", INT2FIX(isc_dpb_lc_messages));