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.13.0"
- 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)
Synchronization::IMMEDIATE, Synchronization::NEVER
Instance Method Summary
collapse
Constructor Details
#initialize(options = {}) ⇒ Trilogy
Returns a new instance of Trilogy.
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
|
# File 'lib/trilogy.rb', line 50
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.fetch(:connect_timeout, 5) || 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
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
|
# File 'ext/trilogy-ruby/cext.c', line 1101
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
1304
|
# File 'ext/trilogy-ruby/cext.c', line 1304
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
784
785
786
787
788
789
790
791
792
793
794
795
796
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
|
# File 'ext/trilogy-ruby/cext.c', line 784
static VALUE rb_trilogy_change_db(VALUE self, VALUE database)
{
struct trilogy_ctx *ctx = get_open_ctx(self);
StringValue(database);
database = rb_str_new_frozen(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);
RB_GC_GUARD(database);
return Qtrue;
}
|
#check ⇒ Object
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
|
# File 'ext/trilogy-ruby/cext.c', line 1272
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
1219
1220
1221
1222
1223
1224
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
|
# File 'ext/trilogy-ruby/cext.c', line 1219
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
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
|
# File 'ext/trilogy-ruby/cext.c', line 1261
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
126
127
128
|
# File 'lib/trilogy.rb', line 126
def connected_host
@connected_host ||= query_with_flags("select @@hostname", query_flags | QUERY_FLAGS_FLATTEN_ROWS).rows.first
end
|
#connection_options ⇒ Object
108
109
110
|
# File 'lib/trilogy.rb', line 108
def connection_options
@connection_options.dup.freeze
end
|
#discard! ⇒ Object
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
|
# File 'ext/trilogy-ruby/cext.c', line 1283
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
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
|
# File 'ext/trilogy-ruby/cext.c', line 1187
static VALUE rb_trilogy_escape(VALUE self, VALUE str)
{
struct trilogy_ctx *ctx = get_open_ctx(self);
StringValue(str);
str = rb_str_new_frozen(str);
rb_encoding *str_enc = rb_enc_get(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);
RB_GC_GUARD(str);
return escaped_string;
}
|
#in_transaction? ⇒ Boolean
112
113
114
|
# File 'lib/trilogy.rb', line 112
def in_transaction?
(server_status & SERVER_STATUS_IN_TRANS) != 0
end
|
#last_gtid ⇒ Object
1308
1309
1310
1311
1312
1313
1314
1315
1316
|
# File 'ext/trilogy-ruby/cext.c', line 1308
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
1302
|
# File 'ext/trilogy-ruby/cext.c', line 1302
static VALUE rb_trilogy_last_insert_id(VALUE self) { return ULL2NUM(get_open_ctx(self)->conn.last_insert_id); }
|
#more_results_exist? ⇒ Boolean
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
|
# File 'ext/trilogy-ruby/cext.c', line 1090
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
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
|
# File 'ext/trilogy-ruby/cext.c', line 1079
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
1150
1151
1152
1153
1154
1155
1156
1157
1158
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
|
# File 'ext/trilogy-ruby/cext.c', line 1150
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
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
|
# File 'ext/trilogy-ruby/cext.c', line 1126
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);
query = rb_str_new_frozen(query);
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");
}
RB_GC_GUARD(query);
return execute_read_query_response(ctx);
}
|
#query_flags ⇒ Object
1318
|
# File 'ext/trilogy-ruby/cext.c', line 1318
static VALUE rb_trilogy_query_flags(VALUE self) { return UINT2NUM(get_ctx(self)->query_flags); }
|
#query_flags=(query_flags) ⇒ Object
1320
1321
1322
1323
|
# File 'ext/trilogy-ruby/cext.c', line 1320
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
130
131
132
133
134
135
136
137
|
# File 'lib/trilogy.rb', line 130
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
1325
1326
1327
1328
|
# File 'ext/trilogy-ruby/cext.c', line 1325
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
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
|
# File 'ext/trilogy-ruby/cext.c', line 1330
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
116
117
118
119
120
121
122
123
124
|
# File 'lib/trilogy.rb', line 116
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
1357
|
# File 'ext/trilogy-ruby/cext.c', line 1357
static VALUE rb_trilogy_server_status(VALUE self) { return LONG2FIX(get_open_ctx(self)->conn.server_status); }
|
#server_version ⇒ Object
1359
|
# File 'ext/trilogy-ruby/cext.c', line 1359
static VALUE rb_trilogy_server_version(VALUE self) { return rb_str_new_cstr(get_open_ctx(self)->server_version); }
|
#set_server_option(option) ⇒ Object
826
827
828
829
830
831
832
833
834
835
836
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
|
# File 'ext/trilogy-ruby/cext.c', line 826
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
1306
|
# File 'ext/trilogy-ruby/cext.c', line 1306
static VALUE rb_trilogy_warning_count(VALUE self) { return UINT2NUM(get_open_ctx(self)->conn.warning_count); }
|
#write_timeout ⇒ Object
1341
1342
1343
1344
|
# File 'ext/trilogy-ruby/cext.c', line 1341
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
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
|
# File 'ext/trilogy-ruby/cext.c', line 1346
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;
}
|