Class: Mysql2::Client

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

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*args) ⇒ Object



128
129
130
# File 'ext/mysql2_ext.c', line 128

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

Class Method Details

.new(*args) ⇒ Object

Mysql2::Client



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
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
# File 'ext/mysql2_ext.c', line 4

static VALUE rb_mysql_client_new(int argc, VALUE * argv, VALUE klass) {
  MYSQL * client;
  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 *host = "localhost", *socket = NULL, *username = NULL,
       *password = NULL, *database = NULL;
  char *ssl_client_key = NULL, *ssl_client_cert = NULL, *ssl_ca_cert = NULL,
       *ssl_ca_path = NULL, *ssl_cipher = NULL;
  unsigned int port = 3306, connect_timeout = 0;
  my_bool reconnect = 1;

  obj = Data_Make_Struct(klass, MYSQL, 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);
      host = RSTRING_PTR(rb_host);
    }

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

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

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

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

    if ((rb_database = rb_hash_aref(opts, sym_database)) != Qnil) {
      Check_Type(rb_database, T_STRING);
      database = 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 (!mysql_init(client)) {
    // TODO: warning - not enough memory?
    rb_raise(cMysql2Error, "%s", mysql_error(client));
    return Qnil;
  }

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

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

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

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

  if (mysql_real_connect(client, host, username, password, database, port, socket, 0) == NULL) {
    // unable to connect
    rb_raise(cMysql2Error, "%s", mysql_error(client));
    return Qnil;
  }

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

Instance Method Details

#affected_rowsObject



262
263
264
265
266
267
# File 'ext/mysql2_ext.c', line 262

static VALUE rb_mysql_client_affected_rows(VALUE self) {
  MYSQL * client;
  GetMysql2Client(self, client);

  return ULL2NUM(mysql_affected_rows(client));
}

#async_resultObject



234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
# File 'ext/mysql2_ext.c', line 234

static VALUE rb_mysql_client_async_result(VALUE self) {
  MYSQL * client;
  MYSQL_RES * result;
  GetMysql2Client(self, client);

  if (mysql_read_query_result(client) != 0) {
    rb_raise(cMysql2Error, "%s", mysql_error(client));
    return Qnil;
  }

  result = mysql_store_result(client);
  if (result == NULL) {
    if (mysql_field_count(client) != 0) {
      rb_raise(cMysql2Error, "%s", mysql_error(client));
    }
    return Qnil;
  }

  return rb_mysql_result_to_obj(result);
}

#escape(str) ⇒ Object



187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
# File 'ext/mysql2_ext.c', line 187

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);

  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



211
212
213
214
215
216
# File 'ext/mysql2_ext.c', line 211

static VALUE rb_mysql_client_info(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



255
256
257
258
259
260
# File 'ext/mysql2_ext.c', line 255

static VALUE rb_mysql_client_last_id(VALUE self) {
  MYSQL * client;
  GetMysql2Client(self, client);

  return ULL2NUM(mysql_insert_id(client));
}

#query(*args) ⇒ Object



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
178
179
180
181
182
183
184
185
# File 'ext/mysql2_ext.c', line 139

static VALUE rb_mysql_client_query(int argc, VALUE * argv, VALUE self) {
  MYSQL * client;
  MYSQL_RES * result;
  fd_set fdset;
  int fd, retval;
  int async = 0;
  VALUE sql, opts;
  VALUE rb_async;

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

  Check_Type(sql, T_STRING);

  GetMysql2Client(self, client);
  if (mysql_send_query(client, RSTRING_PTR(sql), RSTRING_LEN(sql)) != 0) {
    rb_raise(cMysql2Error, "%s", mysql_error(client));
    return Qnil;
  }

  if (!async) {
    // the below code is largely from do_mysql
    // http://github.com/datamapper/do
    fd = client->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



218
219
220
221
222
223
224
225
226
227
# File 'ext/mysql2_ext.c', line 218

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

  GetMysql2Client(self, client);
  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



229
230
231
232
# File 'ext/mysql2_ext.c', line 229

static VALUE rb_mysql_client_socket(VALUE self) {
  MYSQL * client = GetMysql2Client(self, client);;
  return INT2NUM(client->net.fd);
}