Class: Trilogy

Inherits:
Object
  • Object
show all
Defined in:
lib/trilogy.rb,
lib/trilogy/version.rb

Defined Under Namespace

Modules: ConnectionError, DatabaseError, Error Classes: BaseConnectionError, BaseError, CastError, ClientError, ConnectionClosed, ConnectionRefusedError, ConnectionResetError, ProtocolError, QueryError, Result, SSLError, SyscallError, TimeoutError

Constant Summary collapse

MYSQL_TO_RUBY_ENCODINGS_MAP =
{
  "big5"     => "Big5",
  "dec8"     => nil,
  "cp850"    => "CP850",
  "hp8"      => nil,
  "koi8r"    => "KOI8-R",
  "latin1"   => "ISO-8859-1",
  "latin2"   => "ISO-8859-2",
  "swe7"     => nil,
  "ascii"    => "US-ASCII",
  "ujis"     => "eucJP-ms",
  "sjis"     => "Shift_JIS",
  "hebrew"   => "ISO-8859-8",
  "tis620"   => "TIS-620",
  "euckr"    => "EUC-KR",
  "koi8u"    => "KOI8-R",
  "gb2312"   => "GB2312",
  "greek"    => "ISO-8859-7",
  "cp1250"   => "Windows-1250",
  "gbk"      => "GBK",
  "latin5"   => "ISO-8859-9",
  "armscii8" => nil,
  "utf8"     => "UTF-8",
  "ucs2"     => "UTF-16BE",
  "cp866"    => "IBM866",
  "keybcs2"  => nil,
  "macce"    => "macCentEuro",
  "macroman" => "macRoman",
  "cp852"    => "CP852",
  "latin7"   => "ISO-8859-13",
  "utf8mb4"  => "UTF-8",
  "cp1251"   => "Windows-1251",
  "utf16"    => "UTF-16",
  "cp1256"   => "Windows-1256",
  "cp1257"   => "Windows-1257",
  "utf32"    => "UTF-32",
  "binary"   => "ASCII-8BIT",
  "geostd8"  => nil,
  "cp932"    => "Windows-31J",
  "eucjpms"  => "eucJP-ms",
  "utf16le"  => "UTF-16LE",
  "gb18030"  => "GB18030",
}.freeze
VERSION =
"2.4.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)

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Trilogy

Returns a new instance of Trilogy.



162
163
164
165
166
167
168
169
170
# File 'lib/trilogy.rb', line 162

def initialize(options = {})
  mysql_encoding = options[:encoding] || "utf8mb4"
  unless rb_encoding = MYSQL_TO_RUBY_ENCODINGS_MAP[mysql_encoding]
    raise ArgumentError, "Unknown or unsupported encoding: #{mysql_encoding}"
  end
  encoding = Encoding.find(rb_encoding)
  charset = charset_for_mysql_encoding(mysql_encoding)
  _initialize(encoding, charset, **options)
end

Instance Method Details

#affected_rowsObject



971
# File 'ext/trilogy-ruby/cext.c', line 971

static VALUE rb_trilogy_affected_rows(VALUE self) { return ULL2NUM(get_open_ctx(self)->conn.affected_rows); }

#change_db(database) ⇒ Object



537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
# File 'ext/trilogy-ruby/cext.c', line 537

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");
        }

        if (trilogy_sock_wait_read(ctx->conn.socket) < 0) {
            rb_raise(Trilogy_TimeoutError, "trilogy_change_db_recv");
        }
    }

    return Qtrue;
}

#closeObject



905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
# File 'ext/trilogy-ruby/cext.c', line 905

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;
            }
        }
    }

    trilogy_free(&ctx->conn);

    return Qnil;
}

#closed?Boolean

Returns:

  • (Boolean)


939
940
941
942
943
944
945
946
947
948
# File 'ext/trilogy-ruby/cext.c', line 939

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_hostObject



190
191
192
# File 'lib/trilogy.rb', line 190

def connected_host
  @connected_host ||= query_with_flags("select @@hostname", query_flags | QUERY_FLAGS_FLATTEN_ROWS).rows.first
end

#connection_optionsObject



172
173
174
# File 'lib/trilogy.rb', line 172

def connection_options
  @connection_options.dup.freeze
end

#discard!Object



950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
# File 'ext/trilogy-ruby/cext.c', line 950

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



882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
# File 'ext/trilogy-ruby/cext.c', line 882

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

Returns:

  • (Boolean)


176
177
178
# File 'lib/trilogy.rb', line 176

def in_transaction?
  (server_status & SERVER_STATUS_IN_TRANS) != 0
end

#last_gtidObject



975
976
977
978
979
980
981
982
983
# File 'ext/trilogy-ruby/cext.c', line 975

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_idObject



969
# File 'ext/trilogy-ruby/cext.c', line 969

static VALUE rb_trilogy_last_insert_id(VALUE self) { return ULL2NUM(get_open_ctx(self)->conn.last_insert_id); }

#more_results_exist?Boolean

Returns:

  • (Boolean)


818
819
820
821
822
823
824
825
826
827
# File 'ext/trilogy-ruby/cext.c', line 818

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_resultObject



807
808
809
810
811
812
813
814
815
816
# File 'ext/trilogy-ruby/cext.c', line 807

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);
}

#pingObject



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
874
875
876
877
878
879
880
# File 'ext/trilogy-ruby/cext.c', line 849

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");
        }

        if (trilogy_sock_wait_read(ctx->conn.socket) < 0) {
            rb_raise(Trilogy_TimeoutError, "trilogy_ping_recv");
        }
    }

    return Qtrue;
}

#query(query) ⇒ Object



829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
# File 'ext/trilogy-ruby/cext.c', line 829

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_flagsObject



985
# File 'ext/trilogy-ruby/cext.c', line 985

static VALUE rb_trilogy_query_flags(VALUE self) { return UINT2NUM(get_ctx(self)->query_flags); }

#query_flags=(query_flags) ⇒ Object



987
988
989
990
# File 'ext/trilogy-ruby/cext.c', line 987

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



194
195
196
197
198
199
200
201
# File 'lib/trilogy.rb', line 194

def query_with_flags(sql, flags)
  old_flags = query_flags
  self.query_flags = flags

  query(sql)
ensure
  self.query_flags = old_flags
end

#read_timeoutObject



992
993
994
995
# File 'ext/trilogy-ruby/cext.c', line 992

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



997
998
999
1000
1001
1002
1003
1004
1005
1006
# File 'ext/trilogy-ruby/cext.c', line 997

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_infoObject



180
181
182
183
184
185
186
187
188
# File 'lib/trilogy.rb', line 180

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_statusObject



1024
# File 'ext/trilogy-ruby/cext.c', line 1024

static VALUE rb_trilogy_server_status(VALUE self) { return LONG2FIX(get_open_ctx(self)->conn.server_status); }

#server_versionObject



1026
# File 'ext/trilogy-ruby/cext.c', line 1026

static VALUE rb_trilogy_server_version(VALUE self) { return rb_str_new_cstr(get_open_ctx(self)->server_version); }

#set_server_option(option) ⇒ Object



572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
# File 'ext/trilogy-ruby/cext.c', line 572

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");
        }

        if (trilogy_sock_wait_read(ctx->conn.socket) < 0) {
            rb_raise(Trilogy_TimeoutError, "trilogy_set_option_recv");
        }
    }

    return Qtrue;
}

#warning_countObject



973
# File 'ext/trilogy-ruby/cext.c', line 973

static VALUE rb_trilogy_warning_count(VALUE self) { return UINT2NUM(get_open_ctx(self)->conn.warning_count); }

#write_timeoutObject



1008
1009
1010
1011
# File 'ext/trilogy-ruby/cext.c', line 1008

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



1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
# File 'ext/trilogy-ruby/cext.c', line 1013

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;
}