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 Classes: BaseConnectionError, BaseError, CastError, ClientError, ConnectionClosed, ConnectionRefusedError, ConnectionResetError, EOFError, ProtocolError, QueryError, Result, SSLError, SyscallError, TimeoutError

Constant Summary collapse

VERSION =
"2.6.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
# 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)

  _initialize(encoding, charset, options)
end

Instance Method Details

#affected_rowsObject



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

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



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
633
634
635
# File 'ext/trilogy-ruby/cext.c', line 601

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

#closeObject



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
1009
1010
1011
# File 'ext/trilogy-ruby/cext.c', line 975

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)


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

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



37
38
39
# File 'lib/trilogy.rb', line 37

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

#connection_optionsObject



19
20
21
# File 'lib/trilogy.rb', line 19

def connection_options
  @connection_options.dup.freeze
end

#discard!Object



1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
# File 'ext/trilogy-ruby/cext.c', line 1024

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



952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
# File 'ext/trilogy-ruby/cext.c', line 952

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)


23
24
25
# File 'lib/trilogy.rb', line 23

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

#last_gtidObject



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

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



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

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)


887
888
889
890
891
892
893
894
895
896
# File 'ext/trilogy-ruby/cext.c', line 887

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



876
877
878
879
880
881
882
883
884
885
# File 'ext/trilogy-ruby/cext.c', line 876

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



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
948
949
950
# File 'ext/trilogy-ruby/cext.c', line 918

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



898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
# File 'ext/trilogy-ruby/cext.c', line 898

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



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

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

#query_flags=(query_flags) ⇒ Object



1061
1062
1063
1064
# File 'ext/trilogy-ruby/cext.c', line 1061

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



41
42
43
44
45
46
47
48
# File 'lib/trilogy.rb', line 41

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



1066
1067
1068
1069
# File 'ext/trilogy-ruby/cext.c', line 1066

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



1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
# File 'ext/trilogy-ruby/cext.c', line 1071

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



27
28
29
30
31
32
33
34
35
# File 'lib/trilogy.rb', line 27

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



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

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

#server_versionObject



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

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

#set_server_option(option) ⇒ Object



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
667
668
669
# File 'ext/trilogy-ruby/cext.c', line 637

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



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

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

#write_timeoutObject



1082
1083
1084
1085
# File 'ext/trilogy-ruby/cext.c', line 1082

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



1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
# File 'ext/trilogy-ruby/cext.c', line 1087

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