Class: Swift::DB::Mysql

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

Defined Under Namespace

Classes: Result, Statement

Instance Method Summary collapse

Constructor Details

#initialize(options) ⇒ Object



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
138
139
140
141
142
143
144
145
146
147
# File 'ext/swift/db/mysql/adapter.c', line 89

VALUE db_mysql_adapter_initialize(VALUE self, VALUE options) {
    char MYSQL_BOOL_TRUE = 1;
    VALUE db, user, pass, host, port, ssl, enc;
    Adapter *a = db_mysql_adapter_handle(self);

    if (TYPE(options) != T_HASH)
        rb_raise(eSwiftArgumentError, "options needs to be a hash");

    db   = rb_hash_aref(options, ID2SYM(rb_intern("db")));
    user = rb_hash_aref(options, ID2SYM(rb_intern("user")));
    pass = rb_hash_aref(options, ID2SYM(rb_intern("password")));
    host = rb_hash_aref(options, ID2SYM(rb_intern("host")));
    port = rb_hash_aref(options, ID2SYM(rb_intern("port")));
    ssl  = rb_hash_aref(options, ID2SYM(rb_intern("ssl")));
    enc  = rb_hash_aref(options, ID2SYM(rb_intern("encoding")));

    if (NIL_P(db))
        rb_raise(eSwiftConnectionError, "Invalid db name");
    if (NIL_P(host))
        host = rb_str_new2("127.0.0.1");
    if (NIL_P(port))
        port = rb_str_new2("3306");
    if (NIL_P(user))
        user = sUser;
    if (NIL_P(enc))
        enc = rb_str_new2("utf8");

    a->connection = mysql_init(0);
    mysql_options(a->connection, MYSQL_OPT_RECONNECT, &MYSQL_BOOL_TRUE);
    mysql_options(a->connection, MYSQL_OPT_LOCAL_INFILE, 0);

    if (!NIL_P(ssl)) {
        if (TYPE(ssl) != T_HASH)
            rb_raise(eSwiftArgumentError, "ssl options needs to be a hash");

        mysql_ssl_set(
            a->connection,
            ssl_option(ssl, "key"),
            ssl_option(ssl, "cert"),
            ssl_option(ssl, "ca"),
            ssl_option(ssl, "capath"),
            ssl_option(ssl, "cipher")
        );
    }

    if (!mysql_real_connect(a->connection,
        CSTRING(host), CSTRING(user), CSTRING(pass), CSTRING(db), atoi(CSTRING(port)), 0, CLIENT_FOUND_ROWS))
        rb_raise(eSwiftConnectionError, "%s", mysql_error(a->connection));

    if (mysql_set_character_set(a->connection, CSTRING(enc)) != 0)
        rb_raise(eSwiftConnectionError, "%s", mysql_error(a->connection));

    mysql_set_local_infile_handler(
        a->connection,
        db_mysql_adapter_infile_init, db_mysql_adapter_infile_read, db_mysql_adapter_infile_end, db_mysql_adapter_infile_error,
        (void *)self
    );
    return self;
}

Instance Method Details

#begin(*args) ⇒ Object



179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
# File 'ext/swift/db/mysql/adapter.c', line 179

VALUE db_mysql_adapter_begin(int argc, VALUE *argv, VALUE self) {
    char command[256];
    VALUE savepoint;

    Adapter *a = db_mysql_adapter_handle_safe(self);
    rb_scan_args(argc, argv, "01", &savepoint);

    if (a->t_nesting == 0) {
        strcpy(command, "begin");
        if (mysql_real_query(a->connection, command, strlen(command)) != 0)
            rb_raise(eSwiftRuntimeError, "%s", mysql_error(a->connection));
        a->t_nesting++;
        if (NIL_P(savepoint))
            return Qtrue;
    }

    if (NIL_P(savepoint))
        savepoint = rb_uuid_string();

    snprintf(command, 256, "savepoint %s", CSTRING(savepoint));
    if (mysql_real_query(a->connection, command, strlen(command)) != 0)
        rb_raise(eSwiftRuntimeError, "%s", mysql_error(a->connection));

    a->t_nesting++;
    return savepoint;
}

#closeObject



297
298
299
300
301
302
303
304
305
# File 'ext/swift/db/mysql/adapter.c', line 297

VALUE db_mysql_adapter_close(VALUE self) {
    Adapter *a = db_mysql_adapter_handle(self);
    if (a->connection) {
        mysql_close(a->connection);
        a->connection = 0;
        return Qtrue;
    }
    return Qfalse;
}

#closed?Boolean

Returns:

  • (Boolean)


307
308
309
310
# File 'ext/swift/db/mysql/adapter.c', line 307

VALUE db_mysql_adapter_closed_q(VALUE self) {
    Adapter *a = db_mysql_adapter_handle(self);
    return a->connection ? Qfalse : Qtrue;
}

#commit(*args) ⇒ Object



206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
# File 'ext/swift/db/mysql/adapter.c', line 206

VALUE db_mysql_adapter_commit(int argc, VALUE *argv, VALUE self) {
    VALUE savepoint;
    char command[256];

    Adapter *a = db_mysql_adapter_handle_safe(self);
    rb_scan_args(argc, argv, "01", &savepoint);

    if (a->t_nesting == 0)
        return Qfalse;

    if (NIL_P(savepoint)) {
        strcpy(command, "commit");
        if (mysql_real_query(a->connection, command, strlen(command)) != 0)
            rb_raise(eSwiftRuntimeError, "%s", mysql_error(a->connection));
        a->t_nesting--;
    }
    else {
        snprintf(command, 256, "release savepoint %s", CSTRING(savepoint));
        if (mysql_real_query(a->connection, command, strlen(command)) != 0)
            rb_raise(eSwiftRuntimeError, "%s", mysql_error(a->connection));
        a->t_nesting--;
    }
    return Qtrue;
}

#escape(fragment) ⇒ Object



321
322
323
324
325
326
327
# File 'ext/swift/db/mysql/adapter.c', line 321

VALUE db_mysql_adapter_escape(VALUE self, VALUE fragment) {
    VALUE text = TO_S(fragment);
    char escaped[RSTRING_LEN(text) * 2 + 1];
    Adapter *a = db_mysql_adapter_handle_safe(self);
    mysql_real_escape_string(a->connection, escaped, RSTRING_PTR(text), RSTRING_LEN(text));
    return rb_str_new2(escaped);
}

#execute(*args) ⇒ Object



155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
# File 'ext/swift/db/mysql/adapter.c', line 155

VALUE db_mysql_adapter_execute(int argc, VALUE *argv, VALUE self) {
    VALUE sql, bind;
    MYSQL_RES *result;
    Adapter *a = db_mysql_adapter_handle_safe(self);
    MYSQL *c   = a->connection;

    rb_scan_args(argc, argv, "10*", &sql, &bind);
    sql = TO_S(sql);

    rb_gc_register_address(&bind);
    if (RARRAY_LEN(bind) > 0)
        sql = db_mysql_bind_sql(self, sql, bind);
    rb_gc_unregister_address(&bind);

    Command command = {.connection = c, .sql = sql, .status = 0};
    GVL_NOLOCK(nogvl_mysql_adapter_execute, &command, RUBY_UBF_IO, 0);

    if (command.status != 0)
        rb_raise(eSwiftRuntimeError, "%s", mysql_error(c));

    result = mysql_store_result(c);
    return db_mysql_result_load(db_mysql_result_allocate(cDMR), result, mysql_insert_id(c), mysql_affected_rows(c));
}

#filenoObject



329
330
331
332
# File 'ext/swift/db/mysql/adapter.c', line 329

VALUE db_mysql_adapter_fileno(VALUE self) {
    Adapter *a = db_mysql_adapter_handle_safe(self);
    return INT2NUM(a->connection->net.fd);
}

#pingObject



312
313
314
315
# File 'ext/swift/db/mysql/adapter.c', line 312

VALUE db_mysql_adapter_ping(VALUE self) {
    Adapter *a = db_mysql_adapter_handle(self);
    return a->connection && mysql_ping(a->connection) == 0 ? Qtrue : Qfalse;
}

#prepare(sql) ⇒ Object



317
318
319
# File 'ext/swift/db/mysql/adapter.c', line 317

VALUE db_mysql_adapter_prepare(VALUE self, VALUE sql) {
    return db_mysql_statement_initialize(db_mysql_statement_allocate(cDMS), self, sql);
}

#query(*args) ⇒ Object



334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
# File 'ext/swift/db/mysql/adapter.c', line 334

VALUE db_mysql_adapter_query(int argc, VALUE *argv, VALUE self) {
    VALUE sql, bind, result;
    MYSQL_RES *r;
    Adapter *a = db_mysql_adapter_handle_safe(self);
    MYSQL *c   = a->connection;

    rb_scan_args(argc, argv, "10*", &sql, &bind);
    sql = TO_S(sql);

    rb_gc_register_address(&bind);
    if (RARRAY_LEN(bind) > 0)
        sql = db_mysql_bind_sql(self, sql, bind);
    rb_gc_unregister_address(&bind);

    mysql_send_query(c, RSTRING_PTR(sql), RSTRING_LEN(sql));

    if (rb_block_given_p()) {
        rb_thread_wait_fd(a->connection->net.fd);
        if (mysql_read_query_result(c) != 0)
            rb_raise(eSwiftRuntimeError, "%s", mysql_error(c));

        r      = mysql_store_result(c);
        result = db_mysql_result_load(db_mysql_result_allocate(cDMR), r, mysql_insert_id(c), mysql_affected_rows(c));
        return db_mysql_result_each(result);
    }

    return Qtrue;
}

#resultObject



363
364
365
366
367
368
369
370
371
372
373
# File 'ext/swift/db/mysql/adapter.c', line 363

VALUE db_mysql_adapter_result(VALUE self) {
    MYSQL_RES *r;
    Adapter *a = db_mysql_adapter_handle_safe(self);
    MYSQL *c   = a->connection;

    if (mysql_read_query_result(c) != 0)
        rb_raise(eSwiftRuntimeError, "%s", mysql_error(c));

    r = mysql_store_result(c);
    return db_mysql_result_load(db_mysql_result_allocate(cDMR), r, mysql_insert_id(c), mysql_affected_rows(c));
}

#rollback(*args) ⇒ Object



231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
# File 'ext/swift/db/mysql/adapter.c', line 231

VALUE db_mysql_adapter_rollback(int argc, VALUE *argv, VALUE self) {
    VALUE savepoint;
    char command[256];

    Adapter *a = db_mysql_adapter_handle_safe(self);
    rb_scan_args(argc, argv, "01", &savepoint);

    if (a->t_nesting == 0)
        return Qfalse;

    if (NIL_P(savepoint)) {
        strcpy(command, "rollback");
        if (mysql_real_query(a->connection, command, strlen(command)) != 0)
            rb_raise(eSwiftRuntimeError, "%s", mysql_error(a->connection));
        a->t_nesting--;
    }
    else {
        snprintf(command, 256, "rollback to savepoint %s", CSTRING(savepoint));
        if (mysql_real_query(a->connection, command, strlen(command)) != 0)
            rb_raise(eSwiftRuntimeError, "%s", mysql_error(a->connection));
        a->t_nesting--;
    }
    return Qtrue;
}

#transaction(*args) ⇒ Object



256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
# File 'ext/swift/db/mysql/adapter.c', line 256

VALUE db_mysql_adapter_transaction(int argc, VALUE *argv, VALUE self) {
    int status;
    VALUE savepoint, block, block_result;

    Adapter *a = db_mysql_adapter_handle_safe(self);
    rb_scan_args(argc, argv, "01&", &savepoint, &block);

    if (NIL_P(block))
        rb_raise(eSwiftRuntimeError, "mysql transaction requires a block");

    if (a->t_nesting == 0) {
        db_mysql_adapter_begin(1, &savepoint, self);
        block_result = rb_protect(rb_yield, self, &status);
        if (!status) {
            db_mysql_adapter_commit(1, &savepoint, self);
            if (!NIL_P(savepoint))
                db_mysql_adapter_commit(0, 0, self);
        }
        else {
            db_mysql_adapter_rollback(1, &savepoint, self);
            if (!NIL_P(savepoint))
                db_mysql_adapter_rollback(0, 0, self);
            rb_jump_tag(status);
        }
    }
    else {
        if (NIL_P(savepoint))
            savepoint = rb_uuid_string();
        db_mysql_adapter_begin(1, &savepoint, self);
        block_result = rb_protect(rb_yield, self, &status);
        if (!status)
            db_mysql_adapter_commit(1, &savepoint, self);
        else {
            db_mysql_adapter_rollback(1, &savepoint, self);
            rb_jump_tag(status);
        }
    }

    return block_result;
}

#write(*args) ⇒ Object



375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
# File 'ext/swift/db/mysql/adapter.c', line 375

VALUE db_mysql_adapter_write(int argc, VALUE *argv, VALUE self) {
    VALUE table, fields, io, data;

    char *sql;
    Adapter *a = db_mysql_adapter_handle_safe(self);
    MYSQL   *c = a->connection;

    if (argc < 2 || argc > 3)
        rb_raise(rb_eArgError, "wrong number of arguments (%d for 2..3)", argc);

    table = fields = io = Qnil;
    switch (argc) {
        case 2:
            table = argv[0];
            io    = argv[1];
            break;
        case 3:
            table  = argv[0];
            fields = argv[1];
            io     = argv[2];
            if (TYPE(fields) != T_ARRAY)
                rb_raise(eSwiftArgumentError, "fields needs to be an array");
            if (RARRAY_LEN(fields) < 1)
                fields = Qnil;
    }

    if (argc > 1) {
        sql = (char *)malloc(4096);
        if (NIL_P(fields))
            snprintf(sql, 4096, "load data local infile 'swift' replace into table %s", CSTRING(table));
        else
            snprintf(sql, 4096, "load data local infile 'swift' replace into table %s(%s)",
                CSTRING(table), CSTRING(rb_ary_join(fields, rb_str_new2(", "))));

        a->io = rb_respond_to(io, rb_intern("read")) ? io : rb_funcall(cStringIO, rb_intern("new"), 1, TO_S(io));
        if (mysql_real_query(a->connection, sql, strlen(sql)) != 0) {
            free(sql);
            a->io = 0;
            rb_raise(eSwiftRuntimeError, "%s", mysql_error(a->connection));
        }

        free(sql);
    }

    return db_mysql_result_load(db_mysql_result_allocate(cDMR), 0, mysql_insert_id(c), mysql_affected_rows(c));
}