Class: Swift::DB::Sqlite3::Statement

Inherits:
Object
  • Object
show all
Defined in:
ext/swift/db/sqlite3/statement.c

Instance Method Summary collapse

Constructor Details

#initialize(adapter, sql) ⇒ Object



53
54
55
56
57
58
59
60
61
62
63
64
# File 'ext/swift/db/sqlite3/statement.c', line 53

VALUE db_sqlite3_statement_initialize(VALUE self, VALUE adapter, VALUE sql) {
    Statement *s = db_sqlite3_statement_handle(self);

    s->s       = 0;
    s->c       = db_sqlite3_adapter_handle_safe(adapter)->connection;
    s->adapter = adapter;

    if (sqlite3_prepare_v2(s->c, RSTRING_PTR(sql), RSTRING_LEN(sql), &(s->s), 0) != SQLITE_OK)
        rb_raise(eSwiftRuntimeError, "%s\nSQL: %s", sqlite3_errmsg(s->c), RSTRING_PTR(sql));

    return self;
}

Instance Method Details

#execute(*args) ⇒ Object



71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'ext/swift/db/sqlite3/statement.c', line 71

VALUE db_sqlite3_statement_execute(int argc, VALUE *argv, VALUE self) {
    int expect, n;
    VALUE bind, result;

    Statement *s = db_sqlite3_statement_handle_safe(self);

    sqlite3_reset(s->s);
    sqlite3_clear_bindings(s->s);

    rb_scan_args(argc, argv, "00*", &bind);
    expect = sqlite3_bind_parameter_count(s->s);
    if (expect != RARRAY_LEN(bind))
        rb_raise(eSwiftArgumentError, "expected %d bind values got %d", expect, (int)RARRAY_LEN(bind));

    rb_gc_register_address(&bind);
    for (n = 0; n < expect; n++) {
        VALUE value = rb_ary_entry(bind, n);
        if (NIL_P(value))
            sqlite3_bind_null(s->s, n + 1);
        else {
            VALUE text = typecast_to_string(value);
            sqlite3_bind_text(s->s, n + 1, RSTRING_PTR(text), RSTRING_LEN(text), 0);
        }
    }

    result = db_sqlite3_result_allocate(cDSR);
    db_sqlite3_result_initialize(result, self);
    db_sqlite3_result_consume(result);
    rb_gc_unregister_address(&bind);
    return result;
}

#releaseObject



103
104
105
106
107
108
# File 'ext/swift/db/sqlite3/statement.c', line 103

VALUE db_sqlite3_statement_release(VALUE self) {
    Statement *s = db_sqlite3_statement_handle_safe(self);
    sqlite3_finalize(s->s);
    s->s = 0;
    return Qtrue;
}