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