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(, *) ⇒ Object



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

static VALUE rb_mysql_client_init(VALUE self, int argc, VALUE * argv) {
  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 = 0;

  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 == Qtrue ? 1 : 0;
    }

    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

#escape(str) ⇒ Object



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

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



209
210
211
212
213
214
# File 'ext/mysql2_ext.c', line 209

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

#query(sql) ⇒ 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
# File 'ext/mysql2_ext.c', line 139

static VALUE rb_mysql_client_query(VALUE self, VALUE sql) {
  MYSQL * client;
  MYSQL_RES * result;
  fd_set fdset;
  int fd, retval;
  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;
  }

  // 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;
    }
  }

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

#server_infoObject



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

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



227
228
229
230
# File 'ext/mysql2_ext.c', line 227

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