Class: Trilogy
- Inherits:
-
Object
- Object
- Trilogy
- Defined in:
- lib/trilogy.rb,
lib/trilogy/version.rb
Defined Under Namespace
Modules: ConnectionError, DatabaseError, Error Classes: BaseConnectionError, BaseError, CastError, ClientError, ConnectionClosed, ProtocolError, QueryError, Result, SSLError, TimeoutError
Constant Summary collapse
- VERSION =
"2.3.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_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
- #affected_rows ⇒ Object
- #change_db(database) ⇒ Object
- #close ⇒ Object
- #closed? ⇒ Boolean
- #connected_host ⇒ Object
- #connection_options ⇒ Object
- #escape(str) ⇒ Object
- #in_transaction? ⇒ Boolean
- #initialize(opts) ⇒ Object constructor
- #last_gtid ⇒ Object
- #last_insert_id ⇒ Object
- #more_results_exist? ⇒ Boolean
- #next_result ⇒ Object
- #ping ⇒ Object
- #query(query) ⇒ Object
- #query_flags ⇒ Object
- #query_flags=(query_flags) ⇒ Object
- #query_with_flags(sql, flags) ⇒ Object
- #read_timeout ⇒ Object
- #read_timeout=(read_timeout) ⇒ Object
- #server_info ⇒ Object
- #server_status ⇒ Object
- #server_version ⇒ Object
- #warning_count ⇒ Object
- #write_timeout ⇒ Object
- #write_timeout=(write_timeout) ⇒ Object
Constructor Details
#initialize(opts) ⇒ Object
361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 |
# File 'ext/trilogy-ruby/cext.c', line 361
static VALUE rb_trilogy_initialize(VALUE self, VALUE opts)
{
struct trilogy_ctx *ctx = get_ctx(self);
trilogy_sockopt_t connopt = {0};
trilogy_handshake_t handshake;
VALUE val;
Check_Type(opts, T_HASH);
rb_ivar_set(self, id_connection_options, opts);
if ((val = rb_hash_lookup(opts, ID2SYM(id_ssl_mode))) != Qnil) {
Check_Type(val, T_FIXNUM);
connopt.ssl_mode = (trilogy_ssl_mode_t)NUM2INT(val);
}
if ((val = rb_hash_aref(opts, ID2SYM(id_connect_timeout))) != Qnil) {
connopt.connect_timeout = double_to_timeval(NUM2DBL(val));
}
if ((val = rb_hash_aref(opts, ID2SYM(id_read_timeout))) != Qnil) {
connopt.read_timeout = double_to_timeval(NUM2DBL(val));
}
if ((val = rb_hash_aref(opts, ID2SYM(id_write_timeout))) != Qnil) {
connopt.write_timeout = double_to_timeval(NUM2DBL(val));
}
if (RTEST(rb_hash_aref(opts, ID2SYM(id_keepalive_enabled)))) {
connopt.keepalive_enabled = true;
}
if ((val = rb_hash_lookup(opts, ID2SYM(id_keepalive_idle))) != Qnil) {
Check_Type(val, T_FIXNUM);
connopt.keepalive_idle = NUM2USHORT(val);
}
if ((val = rb_hash_lookup(opts, ID2SYM(id_keepalive_count))) != Qnil) {
Check_Type(val, T_FIXNUM);
connopt.keepalive_count = NUM2USHORT(val);
}
if ((val = rb_hash_lookup(opts, ID2SYM(id_keepalive_interval))) != Qnil) {
Check_Type(val, T_FIXNUM);
connopt.keepalive_interval = NUM2USHORT(val);
}
if ((val = rb_hash_lookup(opts, ID2SYM(id_host))) != Qnil) {
Check_Type(val, T_STRING);
connopt.hostname = StringValueCStr(val);
connopt.port = 3306;
if ((val = rb_hash_lookup(opts, ID2SYM(id_port))) != Qnil) {
Check_Type(val, T_FIXNUM);
connopt.port = NUM2USHORT(val);
}
} else {
connopt.path = (char *)"/tmp/mysql.sock";
if ((val = rb_hash_lookup(opts, ID2SYM(id_socket))) != Qnil) {
Check_Type(val, T_STRING);
connopt.path = StringValueCStr(val);
}
}
if ((val = rb_hash_aref(opts, ID2SYM(id_username))) != Qnil) {
Check_Type(val, T_STRING);
connopt.username = StringValueCStr(val);
}
if ((val = rb_hash_aref(opts, ID2SYM(id_password))) != Qnil) {
Check_Type(val, T_STRING);
connopt.password = RSTRING_PTR(val);
connopt.password_len = RSTRING_LEN(val);
}
if ((val = rb_hash_aref(opts, ID2SYM(id_database))) != Qnil) {
Check_Type(val, T_STRING);
connopt.database = StringValueCStr(val);
connopt.flags |= TRILOGY_CAPABILITIES_CONNECT_WITH_DB;
}
if (RTEST(rb_hash_aref(opts, ID2SYM(id_found_rows)))) {
connopt.flags |= TRILOGY_CAPABILITIES_FOUND_ROWS;
}
if (RTEST(rb_hash_aref(opts, ID2SYM(id_multi_statement)))) {
connopt.flags |= TRILOGY_CAPABILITIES_MULTI_STATEMENTS;
}
if ((val = rb_hash_aref(opts, ID2SYM(id_ssl_ca))) != Qnil) {
Check_Type(val, T_STRING);
connopt.ssl_ca = StringValueCStr(val);
}
if ((val = rb_hash_aref(opts, ID2SYM(id_ssl_capath))) != Qnil) {
Check_Type(val, T_STRING);
connopt.ssl_capath = StringValueCStr(val);
}
if ((val = rb_hash_aref(opts, ID2SYM(id_ssl_cert))) != Qnil) {
Check_Type(val, T_STRING);
connopt.ssl_cert = StringValueCStr(val);
}
if ((val = rb_hash_aref(opts, ID2SYM(id_ssl_cipher))) != Qnil) {
Check_Type(val, T_STRING);
connopt.ssl_cipher = StringValueCStr(val);
}
if ((val = rb_hash_aref(opts, ID2SYM(id_ssl_crl))) != Qnil) {
Check_Type(val, T_STRING);
connopt.ssl_crl = StringValueCStr(val);
}
if ((val = rb_hash_aref(opts, ID2SYM(id_ssl_crlpath))) != Qnil) {
Check_Type(val, T_STRING);
connopt.ssl_crlpath = StringValueCStr(val);
}
if ((val = rb_hash_aref(opts, ID2SYM(id_ssl_key))) != Qnil) {
Check_Type(val, T_STRING);
connopt.ssl_key = StringValueCStr(val);
}
if ((val = rb_hash_aref(opts, ID2SYM(id_tls_ciphersuites))) != Qnil) {
Check_Type(val, T_STRING);
connopt.tls_ciphersuites = StringValueCStr(val);
}
if ((val = rb_hash_aref(opts, ID2SYM(id_tls_min_version))) != Qnil) {
Check_Type(val, T_FIXNUM);
connopt.tls_min_version = NUM2INT(val);
}
if ((val = rb_hash_aref(opts, ID2SYM(id_tls_max_version))) != Qnil) {
Check_Type(val, T_FIXNUM);
connopt.tls_max_version = NUM2INT(val);
}
int rc = try_connect(ctx, &handshake, &connopt);
if (rc == TRILOGY_RB_TIMEOUT) {
rb_raise(Trilogy_TimeoutError, "trilogy_connect_recv");
}
if (rc != TRILOGY_OK) {
if (connopt.path) {
handle_trilogy_error(ctx, rc, "trilogy_connect - unable to connect to %s", connopt.path);
} else {
handle_trilogy_error(ctx, rc, "trilogy_connect - unable to connect to %s:%hu", connopt.hostname,
connopt.port);
}
}
memcpy(ctx->server_version, handshake.server_version, TRILOGY_SERVER_VERSION_SIZE);
ctx->server_version[TRILOGY_SERVER_VERSION_SIZE] = 0;
authenticate(ctx, &handshake, connopt.ssl_mode);
return Qnil;
}
|
Instance Method Details
#affected_rows ⇒ Object
907 |
# File 'ext/trilogy-ruby/cext.c', line 907
static VALUE rb_trilogy_affected_rows(VALUE self) { return ULL2NUM(get_open_ctx(self)->conn.affected_rows); }
|
#change_db(database) ⇒ Object
522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 |
# File 'ext/trilogy-ruby/cext.c', line 522
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;
}
|
#close ⇒ Object
860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 |
# File 'ext/trilogy-ruby/cext.c', line 860
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
894 895 896 897 898 899 900 901 902 903 |
# File 'ext/trilogy-ruby/cext.c', line 894
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
113 114 115 |
# File 'lib/trilogy.rb', line 113 def connected_host @connected_host ||= query_with_flags("select @@hostname", query_flags | QUERY_FLAGS_FLATTEN_ROWS).rows.first end |
#connection_options ⇒ Object
95 96 97 |
# File 'lib/trilogy.rb', line 95 def @connection_options.dup.freeze end |
#escape(str) ⇒ Object
837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 |
# File 'ext/trilogy-ruby/cext.c', line 837
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
99 100 101 |
# File 'lib/trilogy.rb', line 99 def in_transaction? (server_status & SERVER_STATUS_IN_TRANS) != 0 end |
#last_gtid ⇒ Object
911 912 913 914 915 916 917 918 919 |
# File 'ext/trilogy-ruby/cext.c', line 911
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
905 |
# File 'ext/trilogy-ruby/cext.c', line 905
static VALUE rb_trilogy_last_insert_id(VALUE self) { return ULL2NUM(get_open_ctx(self)->conn.last_insert_id); }
|
#more_results_exist? ⇒ Boolean
774 775 776 777 778 779 780 781 782 783 |
# File 'ext/trilogy-ruby/cext.c', line 774
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
763 764 765 766 767 768 769 770 771 772 |
# File 'ext/trilogy-ruby/cext.c', line 763
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
804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 |
# File 'ext/trilogy-ruby/cext.c', line 804
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
785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 |
# File 'ext/trilogy-ruby/cext.c', line 785
static VALUE rb_trilogy_query(VALUE self, VALUE query)
{
struct trilogy_ctx *ctx = get_open_ctx(self);
StringValue(query);
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
921 |
# File 'ext/trilogy-ruby/cext.c', line 921
static VALUE rb_trilogy_query_flags(VALUE self) { return UINT2NUM(get_ctx(self)->query_flags); }
|
#query_flags=(query_flags) ⇒ Object
923 924 925 926 |
# File 'ext/trilogy-ruby/cext.c', line 923
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
117 118 119 120 121 122 123 124 |
# File 'lib/trilogy.rb', line 117 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
928 929 930 931 |
# File 'ext/trilogy-ruby/cext.c', line 928
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
933 934 935 936 937 938 939 940 941 942 |
# File 'ext/trilogy-ruby/cext.c', line 933
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
103 104 105 106 107 108 109 110 111 |
# File 'lib/trilogy.rb', line 103 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
960 |
# File 'ext/trilogy-ruby/cext.c', line 960
static VALUE rb_trilogy_server_status(VALUE self) { return LONG2FIX(get_open_ctx(self)->conn.server_status); }
|
#server_version ⇒ Object
962 |
# File 'ext/trilogy-ruby/cext.c', line 962
static VALUE rb_trilogy_server_version(VALUE self) { return rb_str_new_cstr(get_open_ctx(self)->server_version); }
|
#warning_count ⇒ Object
909 |
# File 'ext/trilogy-ruby/cext.c', line 909
static VALUE rb_trilogy_warning_count(VALUE self) { return UINT2NUM(get_open_ctx(self)->conn.warning_count); }
|
#write_timeout ⇒ Object
944 945 946 947 |
# File 'ext/trilogy-ruby/cext.c', line 944
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
949 950 951 952 953 954 955 956 957 958 |
# File 'ext/trilogy-ruby/cext.c', line 949
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;
}
|