Class Informix::Statement
In: lib/informix.rb
ext/informixc.c
Parent: Object

The Statement class lets you prepare and execute any SQL statement, (usually done with Database#prepare) paremeterized or not, that does not return records. This includes DDL (CREATE, DROP, ALTER), DCL (GRANT, REVOKE) and DML (INSERT, UPDATE, DELETE) statements, and SELECTs that return only one record at most.

To retrieve more than one record, use a Cursor instead.

Methods

[]   drop   new  

External Aliases

[] -> call
[] -> execute
new -> _new

Public Class methods

Creates a Statement object from query.

The Statement object is passed to the block if it‘s given, and automatically dropped when the block terminates, returning the value of the block.

query may contain ’?’ placeholders for input parameters; it must NOT be a query returning more than one row (use Cursor instead.)

[Source]

     # File lib/informix.rb, line 277
277:     def self.new(dbname, query)
278:       stmt = _new(dbname, query)
279:       return stmt if !block_given?
280:       begin
281:         yield stmt
282:       ensure
283:         stmt.drop
284:       end
285:     end

Public Instance methods

Executes the previously prepared statement, binding params as input parameters.

Returns the record retrieved, in the case of a singleton select, or the number of rows affected, in the case of any other statement.

Examples:

Inserting records:

  db.prepare('insert into state values(?, ?)') do |st|
    st.execute('CA', 'California')
    st.call('AZ', 'Arizona')
    st['TX', 'Texas')
  end

Selecting one record (returns a hash):

  cust = db.prepare('select * from customer where num = 101') do |st|
           st.execute
         end

[Source]

/*
 * call-seq:
 * st[*params]  => fixnum or hash
 *
 * Executes the previously prepared statement, binding <i>params</i> as
 * input parameters.
 *
 * Returns the record retrieved, in the case of a singleton select, or the
 * number of rows affected, in the case of any other statement.
 *
 * Examples:
 *
 * Inserting records:
 *   db.prepare('insert into state values(?, ?)') do |st|
 *     st.execute('CA', 'California')
 *     st.call('AZ', 'Arizona')
 *     st['TX', 'Texas')
 *   end
 * Selecting one record (returns a hash):
 *   cust = db.prepare('select * from customer where num = 101') do |st|
 *            st.execute
 *          end
 */
static VALUE
rb_statement_call(int argc, VALUE *argv, VALUE self)
{
        struct sqlda *input, *output;
        cursor_t *c;
/*
 *      EXEC SQL begin declare section;
 */
#line 2499 "informixc.ec"
#line 2500 "informixc.ec"
  char *sid, *did;
/*
 *      EXEC SQL end   declare section;
 */
#line 2501 "informixc.ec"


        Data_Get_Struct(self, cursor_t, c);

        did = c->database_id;
/*
 *      EXEC SQL set connection :did;
 */
#line 2506 "informixc.ec"
  {
#line 2506 "informixc.ec"
  sqli_connect_set(0, did, 0);
#line 2506 "informixc.ec"
  }
        if (SQLCODE < 0)
                raise_ifx_extended();

        output = c->daOutput;
        input = &c->daInput;
        sid = c->stmt_id;

        if (argc != input->sqld)
                rb_raise(rb_eArgError, "wrong number of arguments (%d for %d)",
                        argc, input->sqld);

        if (c->is_select) {
                if (argc) {
                        bind_input_params(c, argv);
/*
 *                      EXEC SQL execute :sid into descriptor output
 *                              using descriptor input;
 */
#line 2521 "informixc.ec"
  {
#line 2522 "informixc.ec"
  sqli_exec(ESQLINTVERSION, sqli_curs_locate(ESQLINTVERSION, sid, 257), input, (char *)0, (struct value *)0, output, (char *)0, (struct value *)0, 0);
#line 2522 "informixc.ec"
  }
                        clean_input_slots(c);
                }
                else
/*
 *                      EXEC SQL execute :sid into descriptor output;
 */
#line 2526 "informixc.ec"
  {
#line 2526 "informixc.ec"
  sqli_exec(ESQLINTVERSION, sqli_curs_locate(ESQLINTVERSION, sid, 257), (ifx_sqlda_t *)0, (char *)0, (struct value *)0, output, (char *)0, (struct value *)0, 0);
#line 2526 "informixc.ec"
  }

                if (SQLCODE < 0)
                        raise_ifx_extended();

                if (SQLCODE == SQLNOTFOUND)
                        return Qnil;
                return make_result(c, rb_hash_new());
        }
        else {
                if (argc)  {
                        bind_input_params(c, argv);
/*
 *                      EXEC SQL execute :sid using descriptor input;
 */
#line 2538 "informixc.ec"
  {
#line 2538 "informixc.ec"
  sqli_exec(ESQLINTVERSION, sqli_curs_locate(ESQLINTVERSION, sid, 257), input, (char *)0, (struct value *)0, (ifx_sqlda_t *)0, (char *)0, (struct value *)0, 0);
#line 2538 "informixc.ec"
  }
                        clean_input_slots(c);
                }
                else
/*
 *                      EXEC SQL execute :sid;
 */
#line 2542 "informixc.ec"
  {
#line 2542 "informixc.ec"
  sqli_exec(ESQLINTVERSION, sqli_curs_locate(ESQLINTVERSION, sid, 257), (ifx_sqlda_t *)0, (char *)0, (struct value *)0, (ifx_sqlda_t *)0, (char *)0, (struct value *)0, 0);
#line 2542 "informixc.ec"
  }
        }
        if (SQLCODE < 0)
                raise_ifx_extended();

        return INT2FIX(sqlca.sqlerrd[2]);
}

Frees the statement and the memory associated with it.

[Source]

/*
 * call-seq:
 * st.drop
 *
 * Frees the statement and the memory associated with it.
 */
static VALUE
rb_statement_drop(VALUE self)
{
        cursor_t *c;

        Data_Get_Struct(self, cursor_t, c);
        st_free(c);

        return Qnil;
}

[Validate]