Class: Trilogy
- Inherits:
-
Object
show all
- Includes:
- Synchronization
- Defined in:
- lib/trilogy.rb,
lib/trilogy/error.rb,
lib/trilogy/result.rb,
lib/trilogy/version.rb,
lib/trilogy/encoding.rb
Defined Under Namespace
Modules: ConnectionError, DatabaseError, Encoding, Error, Synchronization, SyscallError
Classes: AuthPluginError, BaseConnectionError, BaseError, CastError, ClientError, ConnectionClosed, EOFError, ProtocolError, QueryError, Result, SSLError, SynchronizationError, TimeoutError
Constant Summary
collapse
- ConnectionRefusedError =
SyscallError::ECONNREFUSED
- ConnectionResetError =
SyscallError::ECONNRESET
- VERSION =
"2.12.6"
- TLS_VERSION_10 =
INT2NUM(TRILOGY_TLS_VERSION_10)
- TLS_VERSION_11 =
INT2NUM(TRILOGY_TLS_VERSION_11)
- TLS_VERSION_12 =
INT2NUM(TRILOGY_TLS_VERSION_12)
- TLS_VERSION_13 =
INT2NUM(TRILOGY_TLS_VERSION_13)
- SSL_DISABLED =
INT2NUM(TRILOGY_SSL_DISABLED)
- SSL_VERIFY_IDENTITY =
INT2NUM(TRILOGY_SSL_VERIFY_IDENTITY)
- SSL_VERIFY_CA =
INT2NUM(TRILOGY_SSL_VERIFY_CA)
- SSL_REQUIRED_NOVERIFY =
INT2NUM(TRILOGY_SSL_REQUIRED_NOVERIFY)
- SSL_PREFERRED_NOVERIFY =
INT2NUM(TRILOGY_SSL_PREFERRED_NOVERIFY)
- QUERY_FLAGS_NONE =
INT2NUM(0)
- QUERY_FLAGS_CAST =
INT2NUM(TRILOGY_FLAGS_CAST)
- QUERY_FLAGS_CAST_BOOLEANS =
INT2NUM(TRILOGY_FLAGS_CAST_BOOLEANS)
- QUERY_FLAGS_CAST_ALL_DECIMALS_TO_BIGDECIMALS =
INT2NUM(TRILOGY_FLAGS_CAST_ALL_DECIMALS_TO_BIGDECIMALS)
- QUERY_FLAGS_LOCAL_TIMEZONE =
INT2NUM(TRILOGY_FLAGS_LOCAL_TIMEZONE)
- QUERY_FLAGS_FLATTEN_ROWS =
INT2NUM(TRILOGY_FLAGS_FLATTEN_ROWS)
- QUERY_FLAGS_DEFAULT =
INT2NUM(TRILOGY_FLAGS_DEFAULT)
Instance Method Summary
collapse
Constructor Details
#initialize(options = {}) ⇒ Trilogy
Returns a new instance of Trilogy.
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
|
# File 'lib/trilogy.rb', line 43
def initialize(options = {})
options[:port] = options[:port].to_i if options[:port]
mysql_encoding = options[:encoding] || "utf8mb4"
encoding = Trilogy::Encoding.find(mysql_encoding)
charset = Trilogy::Encoding.charset(mysql_encoding)
@connection_options = options
@connected_host = nil
socket = nil
begin
if host = options[:host]
port = options[:port] || 3306
connect_timeout = options[:connect_timeout] || options[:write_timeout]
socket = TCPSocket.new(host, port, connect_timeout: connect_timeout)
socket.setsockopt(Socket::IPPROTO_TCP, Socket::TCP_NODELAY, 1)
if keepalive_enabled = options[:keepalive_enabled]
keepalive_idle = options[:keepalive_idle]
keepalive_interval = options[:keepalive_interval]
keepalive_count = options[:keepalive_count]
socket.setsockopt(Socket::SOL_SOCKET, Socket::SO_KEEPALIVE, true)
if keepalive_idle > 0 && defined?(Socket::TCP_KEEPIDLE)
socket.setsockopt(Socket::IPPROTO_TCP, Socket::TCP_KEEPIDLE, keepalive_idle)
end
if keepalive_interval > 0 && defined?(Socket::TCP_KEEPINTVL)
socket.setsockopt(Socket::IPPROTO_TCP, Socket::TCP_KEEPINTVL, keepalive_interval)
end
if keepalive_count > 0 && defined?(Socket::TCP_KEEPCNT)
socket.setsockopt(Socket::IPPROTO_TCP, Socket::TCP_KEEPCNT, keepalive_count)
end
end
else
path = options[:socket] ||= "/tmp/mysql.sock"
socket = UNIXSocket.new(path)
end
rescue Errno::ETIMEDOUT, IO_TIMEOUT_ERROR => e
raise Trilogy::TimeoutError, e.message
rescue SocketError => e
connection_str = host ? "#{host}:#{port}" : path
raise Trilogy::BaseConnectionError, "unable to connect to \"#{connection_str}\": #{e.message}"
rescue => e
if e.respond_to?(:errno)
raise Trilogy::SyscallError.from_errno(e.errno, e.message)
else
raise
end
end
_connect(socket, encoding, charset, options)
ensure
socket&.close
end
|
Instance Method Details
#abandon_results! ⇒ Object
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
|
# File 'ext/trilogy-ruby/cext.c', line 1112
static VALUE rb_trilogy_abandon_results(VALUE self)
{
struct trilogy_ctx *ctx = get_open_ctx(self);
long count = 0;
while (ctx->conn.server_status & TRILOGY_SERVER_STATUS_MORE_RESULTS_EXISTS) {
count++;
int rc = trilogy_drain_results(&ctx->conn);
while (rc == TRILOGY_AGAIN) {
rc = trilogy_sock_wait_read(ctx->conn.socket);
if (rc != TRILOGY_OK) {
handle_trilogy_error(ctx, rc, "trilogy_sock_wait_read");
}
rc = trilogy_drain_results(&ctx->conn);
}
if (rc != TRILOGY_OK) {
handle_trilogy_error(ctx, rc, "trilogy_drain_results");
}
}
return LONG2NUM(count);
}
|
#affected_rows ⇒ Object
1310
|
# File 'ext/trilogy-ruby/cext.c', line 1310
static VALUE rb_trilogy_affected_rows(VALUE self) { return ULL2NUM(get_open_ctx(self)->conn.affected_rows); }
|
#change_db(database) ⇒ Object
Also known as:
select_db
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
|
# File 'ext/trilogy-ruby/cext.c', line 797
static VALUE rb_trilogy_change_db(VALUE self, VALUE database)
{
struct trilogy_ctx *ctx = get_open_ctx(self);
StringValue(database);
rb_trilogy_acquire_buffer(ctx);
int rc = trilogy_change_db_send(&ctx->conn, RSTRING_PTR(database), RSTRING_LEN(database));
if (rc == TRILOGY_AGAIN) {
rc = flush_writes(ctx);
}
if (rc != TRILOGY_OK) {
handle_trilogy_error(ctx, rc, "trilogy_change_db_send");
}
while (1) {
rc = trilogy_change_db_recv(&ctx->conn);
if (rc == TRILOGY_OK) {
break;
}
if (rc != TRILOGY_AGAIN) {
handle_trilogy_error(ctx, rc, "trilogy_change_db_recv");
}
rc = trilogy_sock_wait_read(ctx->conn.socket);
if (rc != TRILOGY_OK) {
handle_trilogy_error(ctx, rc, "trilogy_change_db_recv");
}
}
rb_trilogy_release_buffer(ctx);
return Qtrue;
}
|
#check ⇒ Object
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
|
# File 'ext/trilogy-ruby/cext.c', line 1278
static VALUE rb_trilogy_check(VALUE self)
{
struct trilogy_ctx *ctx = get_open_ctx(self);
int rc = trilogy_sock_check(ctx->conn.socket);
if (rc != TRILOGY_OK && rc != TRILOGY_AGAIN) {
handle_trilogy_error(ctx, rc, "trilogy_sock_check");
}
return Qtrue;
}
|
#close ⇒ Object
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
|
# File 'ext/trilogy-ruby/cext.c', line 1225
static VALUE rb_trilogy_close(VALUE self)
{
struct trilogy_ctx *ctx = get_ctx(self);
if (ctx->conn.socket == NULL) {
return Qnil;
}
rb_trilogy_acquire_buffer(ctx);
int rc = trilogy_close_send(&ctx->conn);
if (rc == TRILOGY_AGAIN) {
rc = flush_writes(ctx);
}
if (rc == TRILOGY_OK) {
while (1) {
rc = trilogy_close_recv(&ctx->conn);
if (rc != TRILOGY_AGAIN) {
break;
}
if (trilogy_sock_wait_read(ctx->conn.socket) < 0) {
// timed out
break;
}
}
}
// We aren't checking or raising errors here (we need close to always close the socket and free the connection), so
// we must clear any SSL errors left in the queue from a read/write.
ERR_clear_error();
rb_trilogy_release_buffer(ctx);
trilogy_free(&ctx->conn);
return Qnil;
}
|
#closed? ⇒ Boolean
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
|
# File 'ext/trilogy-ruby/cext.c', line 1267
static VALUE rb_trilogy_closed(VALUE self)
{
struct trilogy_ctx *ctx = get_ctx(self);
if (ctx->conn.socket == NULL) {
return Qtrue;
} else {
return Qfalse;
}
}
|
#connected_host ⇒ Object
119
120
121
|
# File 'lib/trilogy.rb', line 119
def connected_host
@connected_host ||= query_with_flags("select @@hostname", query_flags | QUERY_FLAGS_FLATTEN_ROWS).rows.first
end
|
#connection_options ⇒ Object
101
102
103
|
# File 'lib/trilogy.rb', line 101
def connection_options
@connection_options.dup.freeze
end
|
#discard! ⇒ Object
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
|
# File 'ext/trilogy-ruby/cext.c', line 1289
static VALUE rb_trilogy_discard(VALUE self)
{
struct trilogy_ctx *ctx = get_ctx(self);
if (ctx->conn.socket == NULL) {
return Qtrue;
}
int rc = trilogy_discard(&ctx->conn);
switch (rc) {
case TRILOGY_OK:
return Qtrue;
case TRILOGY_SYSERR:
trilogy_syserr_fail_str(errno, rb_str_new_cstr("Failed to discard connection"));
UNREACHABLE_RETURN(Qfalse);
}
return Qfalse;
}
|
#escape(str) ⇒ Object
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
|
# File 'ext/trilogy-ruby/cext.c', line 1196
static VALUE rb_trilogy_escape(VALUE self, VALUE str)
{
struct trilogy_ctx *ctx = get_open_ctx(self);
rb_encoding *str_enc = rb_enc_get(str);
StringValue(str);
if (!rb_enc_asciicompat(str_enc)) {
rb_raise(rb_eEncCompatError, "input string must be ASCII-compatible");
}
const char *escaped_str;
size_t escaped_len;
rb_trilogy_acquire_buffer(ctx);
int rc = trilogy_escape(&ctx->conn, RSTRING_PTR(str), RSTRING_LEN(str), &escaped_str, &escaped_len);
if (rc < 0) {
handle_trilogy_error(ctx, rc, "trilogy_escape");
}
VALUE escaped_string = rb_enc_str_new(escaped_str, escaped_len, str_enc);
rb_trilogy_release_buffer(ctx);
return escaped_string;
}
|
#in_transaction? ⇒ Boolean
105
106
107
|
# File 'lib/trilogy.rb', line 105
def in_transaction?
(server_status & SERVER_STATUS_IN_TRANS) != 0
end
|
#last_gtid ⇒ Object
1314
1315
1316
1317
1318
1319
1320
1321
1322
|
# File 'ext/trilogy-ruby/cext.c', line 1314
static VALUE rb_trilogy_last_gtid(VALUE self)
{
struct trilogy_ctx *ctx = get_open_ctx(self);
if (ctx->conn.last_gtid_len > 0) {
return rb_str_new(ctx->conn.last_gtid, ctx->conn.last_gtid_len);
} else {
return Qnil;
}
}
|
#last_insert_id ⇒ Object
1308
|
# File 'ext/trilogy-ruby/cext.c', line 1308
static VALUE rb_trilogy_last_insert_id(VALUE self) { return ULL2NUM(get_open_ctx(self)->conn.last_insert_id); }
|
#more_results_exist? ⇒ Boolean
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
|
# File 'ext/trilogy-ruby/cext.c', line 1101
static VALUE rb_trilogy_more_results_exist(VALUE self)
{
struct trilogy_ctx *ctx = get_open_ctx(self);
if (ctx->conn.server_status & TRILOGY_SERVER_STATUS_MORE_RESULTS_EXISTS) {
return Qtrue;
} else {
return Qfalse;
}
}
|
#next_result ⇒ Object
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
|
# File 'ext/trilogy-ruby/cext.c', line 1090
static VALUE rb_trilogy_next_result(VALUE self)
{
struct trilogy_ctx *ctx = get_open_ctx(self);
if (!(ctx->conn.server_status & TRILOGY_SERVER_STATUS_MORE_RESULTS_EXISTS)) {
return Qnil;
}
return execute_read_query_response(ctx);
}
|
#ping ⇒ Object
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
|
# File 'ext/trilogy-ruby/cext.c', line 1159
static VALUE rb_trilogy_ping(VALUE self)
{
struct trilogy_ctx *ctx = get_open_ctx(self);
rb_trilogy_acquire_buffer(ctx);
int rc = trilogy_ping_send(&ctx->conn);
if (rc == TRILOGY_AGAIN) {
rc = flush_writes(ctx);
}
if (rc < 0) {
handle_trilogy_error(ctx, rc, "trilogy_ping_send");
}
while (1) {
rc = trilogy_ping_recv(&ctx->conn);
if (rc == TRILOGY_OK) {
break;
}
if (rc != TRILOGY_AGAIN) {
handle_trilogy_error(ctx, rc, "trilogy_ping_recv");
}
rc = trilogy_sock_wait_read(ctx->conn.socket);
if (rc != TRILOGY_OK) {
handle_trilogy_error(ctx, rc, "trilogy_ping_recv");
}
}
rb_trilogy_release_buffer(ctx);
return Qtrue;
}
|
#query(query) ⇒ Object
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
|
# File 'ext/trilogy-ruby/cext.c', line 1137
static VALUE rb_trilogy_query(VALUE self, VALUE query)
{
struct trilogy_ctx *ctx = get_open_ctx(self);
StringValue(query);
query = rb_str_export_to_enc(query, ctx->encoding);
rb_trilogy_acquire_buffer(ctx);
int rc = trilogy_query_send(&ctx->conn, RSTRING_PTR(query), RSTRING_LEN(query));
if (rc == TRILOGY_AGAIN) {
rc = flush_writes(ctx);
}
if (rc < 0) {
handle_trilogy_error(ctx, rc, "trilogy_query_send");
}
return execute_read_query_response(ctx);
}
|
#query_flags ⇒ Object
1324
|
# File 'ext/trilogy-ruby/cext.c', line 1324
static VALUE rb_trilogy_query_flags(VALUE self) { return UINT2NUM(get_ctx(self)->query_flags); }
|
#query_flags=(query_flags) ⇒ Object
1326
1327
1328
1329
|
# File 'ext/trilogy-ruby/cext.c', line 1326
static VALUE rb_trilogy_query_flags_set(VALUE self, VALUE query_flags)
{
return get_ctx(self)->query_flags = NUM2UINT(query_flags);
}
|
#query_with_flags(sql, flags) ⇒ Object
123
124
125
126
127
128
129
130
|
# File 'lib/trilogy.rb', line 123
def query_with_flags(sql, flags)
old_flags = query_flags
self.query_flags = flags
query(sql)
ensure
self.query_flags = old_flags
end
|
#read_timeout ⇒ Object
1331
1332
1333
1334
|
# File 'ext/trilogy-ruby/cext.c', line 1331
static VALUE rb_trilogy_read_timeout(VALUE self) {
struct trilogy_ctx *ctx = get_open_ctx(self);
return DBL2NUM(timeval_to_double(ctx->conn.socket->opts.read_timeout));
}
|
#read_timeout=(read_timeout) ⇒ Object
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
|
# File 'ext/trilogy-ruby/cext.c', line 1336
static VALUE rb_trilogy_read_timeout_set(VALUE self, VALUE read_timeout)
{
struct trilogy_ctx *ctx = get_open_ctx(self);
if (read_timeout == Qnil) {
ctx->conn.socket->opts.read_timeout = double_to_timeval(0.0);
} else {
ctx->conn.socket->opts.read_timeout = double_to_timeval(NUM2DBL(read_timeout));
}
return read_timeout;
}
|
#server_info ⇒ Object
109
110
111
112
113
114
115
116
117
|
# File 'lib/trilogy.rb', line 109
def server_info
version_str = server_version
if /\A(\d+)\.(\d+)\.(\d+)/ =~ version_str
version_num = ($1.to_i * 10000) + ($2.to_i * 100) + $3.to_i
end
{ :version => version_str, :id => version_num }
end
|
#server_status ⇒ Object
1363
|
# File 'ext/trilogy-ruby/cext.c', line 1363
static VALUE rb_trilogy_server_status(VALUE self) { return LONG2FIX(get_open_ctx(self)->conn.server_status); }
|
#server_version ⇒ Object
1365
|
# File 'ext/trilogy-ruby/cext.c', line 1365
static VALUE rb_trilogy_server_version(VALUE self) { return rb_str_new_cstr(get_open_ctx(self)->server_version); }
|
#set_server_option(option) ⇒ Object
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
|
# File 'ext/trilogy-ruby/cext.c', line 837
static VALUE rb_trilogy_set_server_option(VALUE self, VALUE option)
{
struct trilogy_ctx *ctx = get_open_ctx(self);
rb_trilogy_acquire_buffer(ctx);
int rc = trilogy_set_option_send(&ctx->conn, NUM2INT(option));
if (rc == TRILOGY_AGAIN) {
rc = flush_writes(ctx);
}
if (rc != TRILOGY_OK) {
handle_trilogy_error(ctx, rc, "trilogy_set_option_send");
}
while (1) {
rc = trilogy_set_option_recv(&ctx->conn);
if (rc == TRILOGY_OK) {
break;
}
if (rc != TRILOGY_AGAIN) {
handle_trilogy_error(ctx, rc, "trilogy_set_option_recv");
}
rc = trilogy_sock_wait_read(ctx->conn.socket);
if (rc != TRILOGY_OK) {
handle_trilogy_error(ctx, rc, "trilogy_set_option_recv");
}
}
rb_trilogy_release_buffer(ctx);
return Qtrue;
}
|
#warning_count ⇒ Object
1312
|
# File 'ext/trilogy-ruby/cext.c', line 1312
static VALUE rb_trilogy_warning_count(VALUE self) { return UINT2NUM(get_open_ctx(self)->conn.warning_count); }
|
#write_timeout ⇒ Object
1347
1348
1349
1350
|
# File 'ext/trilogy-ruby/cext.c', line 1347
static VALUE rb_trilogy_write_timeout(VALUE self) {
struct trilogy_ctx *ctx = get_open_ctx(self);
return DBL2NUM(timeval_to_double(ctx->conn.socket->opts.write_timeout));
}
|
#write_timeout=(write_timeout) ⇒ Object
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
|
# File 'ext/trilogy-ruby/cext.c', line 1352
static VALUE rb_trilogy_write_timeout_set(VALUE self, VALUE write_timeout)
{
struct trilogy_ctx *ctx = get_open_ctx(self);
if (write_timeout == Qnil) {
ctx->conn.socket->opts.write_timeout = double_to_timeval(0.0);
} else {
ctx->conn.socket->opts.write_timeout = double_to_timeval(NUM2DBL(write_timeout));
}
return write_timeout;
}
|