Class: Swift::DB::Mysql::Statement

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

Instance Method Summary collapse

Constructor Details

#initialize(adapter, sql) ⇒ Object



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

VALUE db_mysql_statement_initialize(VALUE self, VALUE adapter, VALUE sql) {
    MYSQL *connection;
    Statement *s = db_mysql_statement_handle(self);

    s->adapter   = adapter;
    connection   = db_mysql_adapter_handle_safe(adapter)->connection;
    s->statement = mysql_stmt_init(connection);
    sql          = TO_S(sql);

    if (mysql_stmt_prepare(s->statement, RSTRING_PTR(sql), RSTRING_LEN(sql)) != 0)
        rb_raise(eSwiftRuntimeError, "%s", mysql_stmt_error(s->statement));

    return self;
}

Instance Method Details

#execute(*args) ⇒ Object



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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
# File 'ext/swift/db/mysql/statement.c', line 75

VALUE db_mysql_statement_execute(int argc, VALUE *argv, VALUE self) {
    int n;
    VALUE bind, data, result, typecast_bind;
    MYSQL_BIND *mysql_bind;
    char MYSQL_BOOL_TRUE = 1, MYSQL_BOOL_FALSE = 0;

    Statement *s = db_mysql_statement_handle_safe(self);
    Command command = {.statement = s->statement, .status = 0};

    rb_scan_args(argc, argv, "00*", &bind);

    mysql_stmt_free_result(s->statement);

    if (RARRAY_LEN(bind) > 0) {
        n = mysql_stmt_param_count(s->statement);
        if (RARRAY_LEN(bind) != n)
            rb_raise(eSwiftArgumentError, "expected %d bind arguments got %d instead", n, RARRAY_LEN(bind));
        mysql_bind = (MYSQL_BIND *)malloc(sizeof(MYSQL_BIND) * RARRAY_LEN(bind));
        memset(mysql_bind, 0, sizeof(MYSQL_BIND) * RARRAY_LEN(bind));

        typecast_bind = rb_ary_new();
        rb_gc_register_address(&typecast_bind);
        rb_gc_register_address(&bind);
        for (n = 0; n < RARRAY_LEN(bind); n++) {
            data = rb_ary_entry(bind, n);
            if (NIL_P(data)) {
                mysql_bind[n].is_null     = &MYSQL_BOOL_TRUE;
                mysql_bind[n].buffer_type = MYSQL_TYPE_NULL;
            }
            else {
                data = typecast_to_string(data);
                rb_ary_push(typecast_bind, data);
                mysql_bind[n].is_null       = &MYSQL_BOOL_FALSE;
                mysql_bind[n].buffer_type   = MYSQL_TYPE_STRING;
                mysql_bind[n].buffer        = RSTRING_PTR(data);
                mysql_bind[n].buffer_length = RSTRING_LEN(data);
            }
        }

        if (mysql_stmt_bind_param(s->statement, mysql_bind) != 0) {
            rb_gc_unregister_address(&typecast_bind);
            rb_gc_unregister_address(&bind);
            free(mysql_bind);
            rb_raise(eSwiftRuntimeError, mysql_stmt_error(s->statement));
        }

        GVL_NOLOCK(nogvl_mysql_statement_execute, &command, RUBY_UBF_IO, 0);
        rb_gc_unregister_address(&typecast_bind);
        rb_gc_unregister_address(&bind);
        free(mysql_bind);
    }
    else {
        if ((n = mysql_stmt_param_count(s->statement)) > 0)
            rb_raise(eSwiftArgumentError, "expected %d bind arguments got 0 instead", n);
        GVL_NOLOCK(nogvl_mysql_statement_execute, &command, RUBY_UBF_IO, 0);
    }

    if (command.status != 0)
        rb_raise(eSwiftRuntimeError, mysql_stmt_error(s->statement));

    result = db_mysql_result_allocate(cDMR);
    return db_mysql_result_from_statement(result, self);
}

#releaseObject



139
140
141
142
143
144
145
146
147
148
# File 'ext/swift/db/mysql/statement.c', line 139

VALUE db_mysql_statement_release(VALUE self) {
    Statement *s = db_mysql_statement_handle(self);
    if (s->statement) {
        mysql_stmt_free_result(s->statement);
        mysql_stmt_close(s->statement);
        s->statement = 0;
        return Qtrue;
    }
    return Qfalse;
}