Class: Trilogy
- Inherits:
-
Object
show all
- 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
Classes: BaseConnectionError, BaseError, CastError, ClientError, ConnectionClosed, ConnectionRefusedError, ConnectionResetError, EOFError, ProtocolError, QueryError, Result, SSLError, SyscallError, TimeoutError
Constant Summary
collapse
- VERSION =
"2.6.1"
- 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.
10
11
12
13
14
15
16
17
18
19
|
# File 'lib/trilogy.rb', line 10
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
_connect(encoding, charset, options)
end
|
Instance Method Details
#affected_rows ⇒ Object
1042
|
# File 'ext/trilogy-ruby/cext.c', line 1042
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
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
|
# File 'ext/trilogy-ruby/cext.c', line 598
static VALUE rb_trilogy_change_db(VALUE self, VALUE database)
{
struct trilogy_ctx *ctx = get_open_ctx(self);
StringValue(database);
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");
}
}
return Qtrue;
}
|
#close ⇒ Object
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
|
# File 'ext/trilogy-ruby/cext.c', line 972
static VALUE rb_trilogy_close(VALUE self)
{
struct trilogy_ctx *ctx = get_ctx(self);
if (ctx->conn.socket == NULL) {
return Qnil;
}
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();
trilogy_free(&ctx->conn);
return Qnil;
}
|
#closed? ⇒ Boolean
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
|
# File 'ext/trilogy-ruby/cext.c', line 1010
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
39
40
41
|
# File 'lib/trilogy.rb', line 39
def connected_host
@connected_host ||= query_with_flags("select @@hostname", query_flags | QUERY_FLAGS_FLATTEN_ROWS).rows.first
end
|
#connection_options ⇒ Object
21
22
23
|
# File 'lib/trilogy.rb', line 21
def connection_options
@connection_options.dup.freeze
end
|
#discard! ⇒ Object
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
|
# File 'ext/trilogy-ruby/cext.c', line 1021
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
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
|
# File 'ext/trilogy-ruby/cext.c', line 949
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;
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");
}
return rb_enc_str_new(escaped_str, escaped_len, str_enc);
}
|
#in_transaction? ⇒ Boolean
25
26
27
|
# File 'lib/trilogy.rb', line 25
def in_transaction?
(server_status & SERVER_STATUS_IN_TRANS) != 0
end
|
#last_gtid ⇒ Object
1046
1047
1048
1049
1050
1051
1052
1053
1054
|
# File 'ext/trilogy-ruby/cext.c', line 1046
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
1040
|
# File 'ext/trilogy-ruby/cext.c', line 1040
static VALUE rb_trilogy_last_insert_id(VALUE self) { return ULL2NUM(get_open_ctx(self)->conn.last_insert_id); }
|
#more_results_exist? ⇒ Boolean
884
885
886
887
888
889
890
891
892
893
|
# File 'ext/trilogy-ruby/cext.c', line 884
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
873
874
875
876
877
878
879
880
881
882
|
# File 'ext/trilogy-ruby/cext.c', line 873
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
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
|
# File 'ext/trilogy-ruby/cext.c', line 915
static VALUE rb_trilogy_ping(VALUE self)
{
struct trilogy_ctx *ctx = get_open_ctx(self);
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");
}
}
return Qtrue;
}
|
#query(query) ⇒ Object
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
|
# File 'ext/trilogy-ruby/cext.c', line 895
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, rb_to_encoding(ctx->encoding));
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
1056
|
# File 'ext/trilogy-ruby/cext.c', line 1056
static VALUE rb_trilogy_query_flags(VALUE self) { return UINT2NUM(get_ctx(self)->query_flags); }
|
#query_flags=(query_flags) ⇒ Object
1058
1059
1060
1061
|
# File 'ext/trilogy-ruby/cext.c', line 1058
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
43
44
45
46
47
48
49
50
|
# File 'lib/trilogy.rb', line 43
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
1063
1064
1065
1066
|
# File 'ext/trilogy-ruby/cext.c', line 1063
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
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
|
# File 'ext/trilogy-ruby/cext.c', line 1068
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
29
30
31
32
33
34
35
36
37
|
# File 'lib/trilogy.rb', line 29
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
1095
|
# File 'ext/trilogy-ruby/cext.c', line 1095
static VALUE rb_trilogy_server_status(VALUE self) { return LONG2FIX(get_open_ctx(self)->conn.server_status); }
|
#server_version ⇒ Object
1097
|
# File 'ext/trilogy-ruby/cext.c', line 1097
static VALUE rb_trilogy_server_version(VALUE self) { return rb_str_new_cstr(get_open_ctx(self)->server_version); }
|
#set_server_option(option) ⇒ Object
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
|
# File 'ext/trilogy-ruby/cext.c', line 634
static VALUE rb_trilogy_set_server_option(VALUE self, VALUE option)
{
struct trilogy_ctx *ctx = get_open_ctx(self);
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");
}
}
return Qtrue;
}
|
#warning_count ⇒ Object
1044
|
# File 'ext/trilogy-ruby/cext.c', line 1044
static VALUE rb_trilogy_warning_count(VALUE self) { return UINT2NUM(get_open_ctx(self)->conn.warning_count); }
|
#write_timeout ⇒ Object
1079
1080
1081
1082
|
# File 'ext/trilogy-ruby/cext.c', line 1079
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
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
|
# File 'ext/trilogy-ruby/cext.c', line 1084
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;
}
|