Class: Trilogy

Inherits:
Object
  • 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, SyscallError Classes: AuthPluginError, BaseConnectionError, BaseError, CastError, ClientError, ConnectionClosed, EOFError, ProtocolError, QueryError, Result, SSLError, TimeoutError

Constant Summary collapse

ConnectionRefusedError =
SyscallError::ECONNREFUSED
ConnectionResetError =
SyscallError::ECONNRESET
VERSION =
"2.9.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.



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_rowsObject



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

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



623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
# File 'ext/trilogy-ruby/cext.c', line 623

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

#checkObject



1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
# File 'ext/trilogy-ruby/cext.c', line 1046

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

#closeObject



997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
# File 'ext/trilogy-ruby/cext.c', line 997

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

Returns:

  • (Boolean)


1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
# File 'ext/trilogy-ruby/cext.c', line 1035

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



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_optionsObject



21
22
23
# File 'lib/trilogy.rb', line 21

def connection_options
  @connection_options.dup.freeze
end

#discard!Object



1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
# File 'ext/trilogy-ruby/cext.c', line 1057

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



974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
# File 'ext/trilogy-ruby/cext.c', line 974

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)


25
26
27
# File 'lib/trilogy.rb', line 25

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

#last_gtidObject



1082
1083
1084
1085
1086
1087
1088
1089
1090
# File 'ext/trilogy-ruby/cext.c', line 1082

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



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

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)


909
910
911
912
913
914
915
916
917
918
# File 'ext/trilogy-ruby/cext.c', line 909

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



898
899
900
901
902
903
904
905
906
907
# File 'ext/trilogy-ruby/cext.c', line 898

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



940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
# File 'ext/trilogy-ruby/cext.c', line 940

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



920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
# File 'ext/trilogy-ruby/cext.c', line 920

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



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

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

#query_flags=(query_flags) ⇒ Object



1094
1095
1096
1097
# File 'ext/trilogy-ruby/cext.c', line 1094

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_timeoutObject



1099
1100
1101
1102
# File 'ext/trilogy-ruby/cext.c', line 1099

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



1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
# File 'ext/trilogy-ruby/cext.c', line 1104

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



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_statusObject



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

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

#server_versionObject



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

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

#set_server_option(option) ⇒ Object



659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
# File 'ext/trilogy-ruby/cext.c', line 659

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_countObject



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

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

#write_timeoutObject



1115
1116
1117
1118
# File 'ext/trilogy-ruby/cext.c', line 1115

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



1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
# File 'ext/trilogy-ruby/cext.c', line 1120

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