Class: Mysql2::Client

Inherits:
Object
  • Object
show all
Defined in:
ext/mysql2_ext.c

Direct Known Subclasses

EM::Client

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*args) ⇒ Object



179
180
181
# File 'ext/mysql2_ext.c', line 179

static VALUE rb_mysql_client_init(RB_MYSQL_UNUSED int argc, RB_MYSQL_UNUSED VALUE * argv, VALUE self) {
  return self;
}

Class Method Details

.new(*args) ⇒ Object

Mysql2::Client



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
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
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
148
149
150
151
152
153
154
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/mysql2_ext.c', line 48

static VALUE rb_mysql_client_new(int argc, VALUE * argv, VALUE klass) {
  mysql2_client_wrapper * client;
  struct nogvl_connect_args args = {
    .host = "localhost",
    .user = NULL,
    .passwd = NULL,
    .db = NULL,
    .port = 3306,
    .unix_socket = NULL,
    .client_flag = 0
  };
  VALUE obj, opts;
  VALUE rb_host, rb_socket, rb_port, rb_database,
        rb_username, rb_password, rb_reconnect,
        rb_connect_timeout;
  VALUE rb_ssl_client_key, rb_ssl_client_cert, rb_ssl_ca_cert,
        rb_ssl_ca_path, rb_ssl_cipher;
  char *ssl_client_key = NULL, *ssl_client_cert = NULL, *ssl_ca_cert = NULL,
       *ssl_ca_path = NULL, *ssl_cipher = NULL;
  unsigned int connect_timeout = 0;
  my_bool reconnect = 1;

  obj = Data_Make_Struct(klass, mysql2_client_wrapper, NULL, rb_mysql_client_free, client);

  if (rb_scan_args(argc, argv, "01", &opts) == 1) {
    Check_Type(opts, T_HASH);

    if ((rb_host = rb_hash_aref(opts, sym_host)) != Qnil) {
      Check_Type(rb_host, T_STRING);
      args.host = RSTRING_PTR(rb_host);
    }

    if ((rb_socket = rb_hash_aref(opts, sym_socket)) != Qnil) {
      Check_Type(rb_socket, T_STRING);
      args.unix_socket = RSTRING_PTR(rb_socket);
    }

    if ((rb_port = rb_hash_aref(opts, sym_port)) != Qnil) {
      Check_Type(rb_port, T_FIXNUM);
      args.port = FIX2INT(rb_port);
    }

    if ((rb_username = rb_hash_aref(opts, sym_username)) != Qnil) {
      Check_Type(rb_username, T_STRING);
      args.user = RSTRING_PTR(rb_username);
    }

    if ((rb_password = rb_hash_aref(opts, sym_password)) != Qnil) {
      Check_Type(rb_password, T_STRING);
      args.passwd = RSTRING_PTR(rb_password);
    }

    if ((rb_database = rb_hash_aref(opts, sym_database)) != Qnil) {
      Check_Type(rb_database, T_STRING);
      args.db = RSTRING_PTR(rb_database);
    }

    if ((rb_reconnect = rb_hash_aref(opts, sym_reconnect)) != Qnil) {
      reconnect = rb_reconnect == Qfalse ? 0 : 1;
    }

    if ((rb_connect_timeout = rb_hash_aref(opts, sym_connect_timeout)) != Qnil) {
      Check_Type(rb_connect_timeout, T_FIXNUM);
      connect_timeout = FIX2INT(rb_connect_timeout);
    }

    // SSL options
    if ((rb_ssl_client_key = rb_hash_aref(opts, sym_sslkey)) != Qnil) {
      Check_Type(rb_ssl_client_key, T_STRING);
      ssl_client_key = RSTRING_PTR(rb_ssl_client_key);
    }

    if ((rb_ssl_client_cert = rb_hash_aref(opts, sym_sslcert)) != Qnil) {
      Check_Type(rb_ssl_client_cert, T_STRING);
      ssl_client_cert = RSTRING_PTR(rb_ssl_client_cert);
    }

    if ((rb_ssl_ca_cert = rb_hash_aref(opts, sym_sslca)) != Qnil) {
      Check_Type(rb_ssl_ca_cert, T_STRING);
      ssl_ca_cert = RSTRING_PTR(rb_ssl_ca_cert);
    }

    if ((rb_ssl_ca_path = rb_hash_aref(opts, sym_sslcapath)) != Qnil) {
      Check_Type(rb_ssl_ca_path, T_STRING);
      ssl_ca_path = RSTRING_PTR(rb_ssl_ca_path);
    }

    if ((rb_ssl_cipher = rb_hash_aref(opts, sym_sslcipher)) != Qnil) {
      Check_Type(rb_ssl_cipher, T_STRING);
      ssl_cipher = RSTRING_PTR(rb_ssl_cipher);
    }
  }

  if (rb_thread_blocking_region(nogvl_init, &args, RUBY_UBF_IO, 0) == Qfalse) {
    // TODO: warning - not enough memory?
    return rb_raise_mysql2_error(args.mysql);
  }

  // set default reconnect behavior
  if (mysql_options(args.mysql, MYSQL_OPT_RECONNECT, &reconnect) != 0) {
    // TODO: warning - unable to set reconnect behavior
    rb_warn("%s\n", mysql_error(args.mysql));
  }

  // set default connection timeout behavior
  if (connect_timeout != 0 && mysql_options(args.mysql, MYSQL_OPT_CONNECT_TIMEOUT, (const char *)&connect_timeout) != 0) {
    // TODO: warning - unable to set connection timeout
    rb_warn("%s\n", mysql_error(args.mysql));
  }

  // force the encoding to utf8
  if (mysql_options(args.mysql, MYSQL_SET_CHARSET_NAME, "utf8") != 0) {
    // TODO: warning - unable to set charset
    rb_warn("%s\n", mysql_error(args.mysql));
  }

  if (ssl_ca_cert != NULL || ssl_client_key != NULL) {
    mysql_ssl_set(args.mysql, ssl_client_key, ssl_client_cert, ssl_ca_cert, ssl_ca_path, ssl_cipher);
  }

  if (rb_thread_blocking_region(nogvl_connect, &args, RUBY_UBF_IO, 0) == Qfalse) {
    // unable to connect
    return rb_raise_mysql2_error(args.mysql);;
  }

  client->client = args.mysql;

  rb_obj_call_init(obj, argc, argv);
  return obj;
}

Instance Method Details

#affected_rowsObject



413
414
415
416
417
418
419
420
421
# File 'ext/mysql2_ext.c', line 413

static VALUE rb_mysql_client_affected_rows(VALUE self) {
  MYSQL * client;
  GetMysql2Client(self, client);
  if (!client) {
    rb_raise(cMysql2Error, "closed MySQL connection");
    return Qnil;
  }
  return ULL2NUM(mysql_affected_rows(client));
}

#async_resultObject



380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
# File 'ext/mysql2_ext.c', line 380

static VALUE rb_mysql_client_async_result(VALUE self) {
  MYSQL * client;
  MYSQL_RES * result;
  GetMysql2Client(self, client);
  if (!client) {
    rb_raise(cMysql2Error, "closed MySQL connection");
    return Qnil;
  }
  if (rb_thread_blocking_region(nogvl_read_query_result, client, RUBY_UBF_IO, 0) == Qfalse) {
    return rb_raise_mysql2_error(client);
  }

  result = (MYSQL_RES *)rb_thread_blocking_region(nogvl_store_result, client, RUBY_UBF_IO, 0);
  if (result == NULL) {
    if (mysql_field_count(client) != 0) {
      rb_raise_mysql2_error(client);
    }
    return Qnil;
  }

  return rb_mysql_result_to_obj(result);
}

#closeObject

Immediately disconnect from the server, normally the garbage collector will disconnect automatically when a connection is no longer needed. Explicitly closing this will free up server resources sooner than waiting for the garbage collector.



221
222
223
224
225
226
227
228
229
230
231
232
233
# File 'ext/mysql2_ext.c', line 221

static VALUE rb_mysql_client_close(VALUE self) {
  mysql2_client_wrapper *client;

	Data_Get_Struct(self, mysql2_client_wrapper, client);

  if (client->client) {
    rb_thread_blocking_region(nogvl_close, client->client, RUBY_UBF_IO, 0);
    client->client = NULL;
  } else {
    rb_raise(cMysql2Error, "already closed MySQL connection");
  }
  return Qnil;
}

#escape(str) ⇒ Object



302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
# File 'ext/mysql2_ext.c', line 302

static VALUE rb_mysql_client_escape(VALUE self, VALUE str) {
  MYSQL * client;
  VALUE newStr;
  unsigned long newLen, oldLen;

  Check_Type(str, T_STRING);
  oldLen = RSTRING_LEN(str);
  char escaped[(oldLen*2)+1];

  GetMysql2Client(self, client);
  if (!client) {
    rb_raise(cMysql2Error, "closed MySQL connection");
    return Qnil;
  }
  newLen = mysql_real_escape_string(client, escaped, RSTRING_PTR(str), RSTRING_LEN(str));
  if (newLen == oldLen) {
    // no need to return a new ruby string if nothing changed
    return str;
  } else {
    newStr = rb_str_new(escaped, newLen);
#ifdef HAVE_RUBY_ENCODING_H
    rb_enc_associate_index(newStr, utf8Encoding);
#endif
    return newStr;
  }
}

#infoObject



329
330
331
332
333
334
# File 'ext/mysql2_ext.c', line 329

static VALUE rb_mysql_client_info(RB_MYSQL_UNUSED VALUE self) {
  VALUE version = rb_hash_new();
  rb_hash_aset(version, sym_id, LONG2FIX(mysql_get_client_version()));
  rb_hash_aset(version, sym_version, rb_str_new2(mysql_get_client_info()));
  return version;
}

#last_idObject



403
404
405
406
407
408
409
410
411
# File 'ext/mysql2_ext.c', line 403

static VALUE rb_mysql_client_last_id(VALUE self) {
  MYSQL * client;
  GetMysql2Client(self, client);
  if (!client) {
    rb_raise(cMysql2Error, "closed MySQL connection");
    return Qnil;
  }
  return ULL2NUM(mysql_insert_id(client));
}

#query(*args) ⇒ Object



252
253
254
255
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
296
297
298
299
300
# File 'ext/mysql2_ext.c', line 252

static VALUE rb_mysql_client_query(int argc, VALUE * argv, VALUE self) {
  struct nogvl_send_query_args args;
  fd_set fdset;
  int fd, retval;
  int async = 0;
  VALUE opts;
  VALUE rb_async;

  if (rb_scan_args(argc, argv, "11", &args.sql, &opts) == 2) {
    if ((rb_async = rb_hash_aref(opts, sym_async)) != Qnil) {
      async = rb_async == Qtrue ? 1 : 0;
    }
  }

  Check_Type(args.sql, T_STRING);

  GetMysql2Client(self, args.mysql);
  if (!args.mysql) {
    rb_raise(cMysql2Error, "closed MySQL connection");
    return Qnil;
  }
  if (rb_thread_blocking_region(nogvl_send_query, &args, RUBY_UBF_IO, 0) == Qfalse) {
    return rb_raise_mysql2_error(args.mysql);;
  }

  if (!async) {
    // the below code is largely from do_mysql
    // http://github.com/datamapper/do
    fd = args.mysql->net.fd;
    for(;;) {
      FD_ZERO(&fdset);
      FD_SET(fd, &fdset);

      retval = rb_thread_select(fd + 1, &fdset, NULL, NULL, NULL);

      if (retval < 0) {
          rb_sys_fail(0);
      }

      if (retval > 0) {
          break;
      }
    }

    return rb_mysql_client_async_result(self);
  } else {
    return Qnil;
  }
}

#server_infoObject



336
337
338
339
340
341
342
343
344
345
346
347
348
349
# File 'ext/mysql2_ext.c', line 336

static VALUE rb_mysql_client_server_info(VALUE self) {
  MYSQL * client;
  VALUE version;

  GetMysql2Client(self, client);
  if (!client) {
    rb_raise(cMysql2Error, "closed MySQL connection");
    return Qnil;
  }
  version = rb_hash_new();
  rb_hash_aset(version, sym_id, LONG2FIX(mysql_get_server_version(client)));
  rb_hash_aset(version, sym_version, rb_str_new2(mysql_get_server_info(client)));
  return version;
}

#socketObject



351
352
353
354
355
356
357
358
# File 'ext/mysql2_ext.c', line 351

static VALUE rb_mysql_client_socket(VALUE self) {
  MYSQL * client = GetMysql2Client(self, client);
  if (!client) {
    rb_raise(cMysql2Error, "closed MySQL connection");
    return Qnil;
  }
  return INT2NUM(client->net.fd);
}