Class: Oedipus::Mysql
- Inherits:
-
Object
- Object
- Oedipus::Mysql
- Defined in:
- ext/oedipus/oedipus.c
Class Method Summary collapse
-
.new(host, port) ⇒ Object
– Public methods –.
Instance Method Summary collapse
- #close ⇒ Object
- #execute(*, self) ⇒ Object
- #initialize(host, port) ⇒ Object constructor
- #open ⇒ Object
- #query(*, self) ⇒ Object
Constructor Details
#initialize(host, port) ⇒ Object
33 34 35 36 37 38 39 40 41 42 43 |
# File 'ext/oedipus/oedipus.c', line 33
static VALUE odp_initialize(VALUE self, VALUE host, VALUE port) {
Check_Type(host, T_STRING);
Check_Type(port, T_FIXNUM);
rb_iv_set(self, "@host", host);
rb_iv_set(self, "@port", port);
odp_open(self);
return self;
}
|
Class Method Details
.new(host, port) ⇒ Object
– Public methods –
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
# File 'ext/oedipus/oedipus.c', line 15
static VALUE odp_new(VALUE klass, VALUE host, VALUE port) {
OdpMysql * conn;
VALUE self;
VALUE args[2];
conn = malloc(sizeof(OdpMysql));
conn->connected = 0;
self = Data_Wrap_Struct(klass, 0, odp_free, conn);
args[0] = host;
args[1] = port;
rb_obj_call_init(self, 2, args);
return self;
}
|
Instance Method Details
#close ⇒ Object
74 75 76 77 78 79 80 81 82 83 84 85 86 87 |
# File 'ext/oedipus/oedipus.c', line 74
static VALUE odp_close(VALUE self) {
OdpMysql * conn;
Data_Get_Struct(self, OdpMysql, conn);
if (!conn->connected) {
return Qfalse;
}
mysql_close(conn->ptr);
conn->connected = 0;
return Qtrue;
}
|
#execute(*, self) ⇒ Object
89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 |
# File 'ext/oedipus/oedipus.c', line 89
static VALUE odp_execute(int argc, VALUE * args, VALUE self) {
VALUE sql;
OdpMysql * conn;
if (0 == argc) {
rb_raise(rb_eArgError, "Wrong number of arguments (0 for 1..*)");
}
Check_Type(args[0], T_STRING);
Data_Get_Struct(self, OdpMysql, conn);
if (!conn->connected) {
odp_raise(self, "Cannot execute query on a closed connection");
}
sql = odp_replace_bind_values(conn, args[0], &args[1], argc - 1);
if (mysql_query(conn->ptr, RSTRING_PTR(sql))) {
odp_raise(self, "Failed to execute statement(s)");
}
return INT2NUM(mysql_affected_rows(conn->ptr));
}
|
#open ⇒ Object
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 |
# File 'ext/oedipus/oedipus.c', line 45
static VALUE odp_open(VALUE self) {
OdpMysql * conn;
Data_Get_Struct(self, OdpMysql, conn);
if (conn->connected) {
return Qfalse;
}
if ((conn->ptr = mysql_init(NULL)) == NULL) {
odp_raise(self, "Unable to initialize mysql");
}
if (mysql_real_connect(conn->ptr,
RSTRING_PTR(rb_iv_get(self, "@host")),
"",
"",
NULL,
NUM2UINT(rb_iv_get(self, "@port")),
NULL,
CLIENT_MULTI_STATEMENTS) == NULL) {
odp_raise(self, "Unable to connect to mysql");
}
conn->connected = 1;
return Qtrue;
}
|
#query(*, self) ⇒ Object
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 |
# File 'ext/oedipus/oedipus.c', line 114
static VALUE odp_query(int argc, VALUE * args, VALUE self) {
VALUE sql;
OdpMysql * conn;
MYSQL_RES * rs;
int status;
int num_fields;
MYSQL_ROW row;
MYSQL_FIELD * fields;
unsigned long * lengths;
int i;
VALUE rows;
VALUE hash;
VALUE results;
if (0 == argc) {
rb_raise(rb_eArgError, "Wrong number of arguments (0 for 1..*)");
}
Check_Type(args[0], T_STRING);
Data_Get_Struct(self, OdpMysql, conn);
if (!conn->connected) {
odp_raise(self, "Cannot execute query on a closed connection");
}
sql = odp_replace_bind_values(conn, args[0], &args[1], argc - 1);
if (mysql_query(conn->ptr, RSTRING_PTR(sql))) {
odp_raise(self, "Failed to execute statement(s)");
}
results = rb_ary_new();
do {
if ((rs = mysql_store_result(conn->ptr)) != NULL) {
rb_ary_push(results, (rows = rb_ary_new()));
num_fields = mysql_num_fields(rs);
fields = mysql_fetch_fields(rs);
while ((row = mysql_fetch_row(rs))) {
lengths = mysql_fetch_lengths(rs);
rb_ary_push(rows, (hash = rb_hash_new()));
for (i = 0; i < num_fields; ++i) {
rb_hash_aset(hash,
rb_str_new2(fields[i].name),
odp_cast_value(fields[i], row[i], lengths[i]));
}
}
mysql_free_result(rs);
}
if ((status = mysql_next_result(conn->ptr)) > 0) {
odp_raise(self, "Query execution failed");
}
} while (status == 0);
return results;
}
|