Class: RDO::MySQL::Driver
- Inherits:
-
Driver
- Object
- Driver
- RDO::MySQL::Driver
- Defined in:
- lib/rdo/mysql/driver.rb
Overview
Principal RDO driver class for MySQL.
Instance Method Summary collapse
-
#close ⇒ Object
Close the connection to MySQL.
-
#execute(*, self) ⇒ Object
Execute a statement with possible bind parameters.
-
#open ⇒ Object
Open a connection to MySQL.
-
#open? ⇒ Boolean
Return true if the connection is open.
-
#quote(obj) ⇒ Object
Quote values for safe insertion into a statement.
Instance Method Details
#close ⇒ Object
Close the connection to MySQL
104 105 106 107 108 109 110 111 112 113 114 |
# File 'ext/rdo_mysql/driver.c', line 104
static VALUE rdo_mysql_driver_close(VALUE self) {
RDOMySQLDriver * driver;
Data_Get_Struct(self, RDOMySQLDriver, driver);
mysql_close(driver->conn);
driver->conn = NULL;
driver->is_open = 0;
driver->encoding = -1;
return Qtrue;
}
|
#execute(*, self) ⇒ Object
Execute a statement with possible bind parameters
169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 |
# File 'ext/rdo_mysql/driver.c', line 169
static VALUE rdo_mysql_driver_execute(int argc, VALUE * args, VALUE self) {
if (argc < 1) {
rb_raise(rb_eArgError, "wrong number of arguments (0 for 1)");
}
RDOMySQLDriver * driver;
Data_Get_Struct(self, RDOMySQLDriver, driver);
if (!(driver->is_open)) {
RDO_ERROR("Cannot execute query: connection is not open");
}
rdo_mysql_driver_normalize_date_times_bang(&args[1], argc - 1);
rdo_mysql_driver_normalize_sets_bang(&args[1], argc - 1);
VALUE stmt = RDO_INTERPOLATE(self, args, argc);
if (mysql_real_query(driver->conn,
RSTRING_PTR(stmt),
RSTRING_LEN(stmt)) != 0) {
RDO_ERROR("Failed to execute query: %s", mysql_error(driver->conn));
}
MYSQL_RES * res = mysql_store_result(driver->conn);
return RDO_RESULT(rdo_mysql_tuple_list_new(res, driver->encoding),
rdo_mysql_result_info_new(driver->conn, res));
}
|
#open ⇒ Object
Open a connection to MySQL
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 |
# File 'ext/rdo_mysql/driver.c', line 52
static VALUE rdo_mysql_driver_open(VALUE self) {
RDOMySQLDriver * driver;
Data_Get_Struct(self, RDOMySQLDriver, driver);
if (driver->is_open) {
return Qtrue;
}
if (!(driver->conn = mysql_init(NULL))) {
RDO_ERROR("Failed to connect to MySQL. Could not allocate memory.");
}
VALUE host = rb_funcall(self, rb_intern("host"), 0);
VALUE port = rb_funcall(self, rb_intern("port"), 0);
VALUE user = rb_funcall(self, rb_intern("user"), 0);
VALUE pass = rb_funcall(self, rb_intern("password"), 0);
VALUE db = rb_funcall(self, rb_intern("database"), 0);
Check_Type(host, T_STRING);
Check_Type(port, T_FIXNUM);
Check_Type(user, T_STRING);
Check_Type(pass, T_STRING);
Check_Type(db, T_STRING);
if (!mysql_real_connect(driver->conn,
RSTRING_PTR(host),
RSTRING_PTR(user),
RSTRING_PTR(pass),
RSTRING_PTR(db),
NUM2INT(port),
NULL, // UNIX socket
0)) {
RDO_ERROR("MySQL connection failed: %s", mysql_error(driver->conn));
} else {
driver->is_open = 1;
driver->encoding = rb_enc_find_index(
RSTRING_PTR(rb_funcall(self, rb_intern("encoding"), 0)));
rb_funcall(self, rb_intern("after_open"), 0);
}
return Qtrue;
}
|
#open? ⇒ Boolean
Return true if the connection is open
96 97 98 99 100 101 |
# File 'ext/rdo_mysql/driver.c', line 96
static VALUE rdo_mysql_driver_open_p(VALUE self) {
RDOMySQLDriver * driver;
Data_Get_Struct(self, RDOMySQLDriver, driver);
return driver->is_open ? Qtrue : Qfalse;
}
|
#quote(obj) ⇒ Object
Quote values for safe insertion into a statement
117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 |
# File 'ext/rdo_mysql/driver.c', line 117
static VALUE rdo_mysql_driver_quote(VALUE self, VALUE obj) {
if (TYPE(obj) != T_STRING) {
obj = RDO_OBJ_TO_S(obj);
}
RDOMySQLDriver * driver;
Data_Get_Struct(self, RDOMySQLDriver, driver);
if (!(driver->is_open)) {
RDO_ERROR("Unable to quote value: connection is closed");
}
char quoted[RSTRING_LEN(obj) * 2 + 1];
mysql_real_escape_string(driver->conn,
quoted, RSTRING_PTR(obj), RSTRING_LEN(obj));
return rb_str_new2(quoted);
}
|