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.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.



167
168
169
170
171
172
173
174
175
# File 'lib/trilogy.rb', line 167

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



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

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

#change_db(database) ⇒ Object



533
534
535
536
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
# File 'ext/trilogy-ruby/cext.c', line 533

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



901
902
903
904
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
# File 'ext/trilogy-ruby/cext.c', line 901

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)


935
936
937
938
939
940
941
942
943
944
# File 'ext/trilogy-ruby/cext.c', line 935

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



195
196
197
# File 'lib/trilogy.rb', line 195

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

#connection_optionsObject



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

def connection_options
  @connection_options.dup.freeze
end

#discard!Object



946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
# File 'ext/trilogy-ruby/cext.c', line 946

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



878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
# File 'ext/trilogy-ruby/cext.c', line 878

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)


181
182
183
# File 'lib/trilogy.rb', line 181

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

#last_gtidObject



971
972
973
974
975
976
977
978
979
# File 'ext/trilogy-ruby/cext.c', line 971

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



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

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)


814
815
816
817
818
819
820
821
822
823
# File 'ext/trilogy-ruby/cext.c', line 814

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



803
804
805
806
807
808
809
810
811
812
# File 'ext/trilogy-ruby/cext.c', line 803

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



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
874
875
876
# File 'ext/trilogy-ruby/cext.c', line 845

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



825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
# File 'ext/trilogy-ruby/cext.c', line 825

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



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

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

#query_flags=(query_flags) ⇒ Object



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

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



199
200
201
202
203
204
205
206
# File 'lib/trilogy.rb', line 199

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



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

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



993
994
995
996
997
998
999
1000
1001
1002
# File 'ext/trilogy-ruby/cext.c', line 993

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



185
186
187
188
189
190
191
192
193
# File 'lib/trilogy.rb', line 185

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



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

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

#server_versionObject



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

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

#set_server_option(option) ⇒ Object



568
569
570
571
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
# File 'ext/trilogy-ruby/cext.c', line 568

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



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

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

#write_timeoutObject



1004
1005
1006
1007
# File 'ext/trilogy-ruby/cext.c', line 1004

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



1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
# File 'ext/trilogy-ruby/cext.c', line 1009

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