Class: Trilogy
- Inherits:
-
Object
- Object
- Trilogy
- Defined in:
- lib/trilogy.rb,
lib/trilogy/version.rb,
ext/trilogy-ruby/cext.c
Defined Under Namespace
Classes: DatabaseError, Error, Result
Constant Summary collapse
- VERSION =
"2.2.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
- #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
344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 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 |
# File 'ext/trilogy-ruby/cext.c', line 344
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);
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_syserr_fail(ETIMEDOUT, "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
889 |
# File 'ext/trilogy-ruby/cext.c', line 889
static VALUE rb_trilogy_affected_rows(VALUE self) { return ULL2NUM(get_open_ctx(self)->conn.affected_rows); }
|
#change_db(database) ⇒ Object
504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 |
# File 'ext/trilogy-ruby/cext.c', line 504
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_syserr_fail(ETIMEDOUT, "trilogy_change_db_recv");
}
}
return Qtrue;
}
|
#close ⇒ Object
842 843 844 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 |
# File 'ext/trilogy-ruby/cext.c', line 842
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
876 877 878 879 880 881 882 883 884 885 |
# File 'ext/trilogy-ruby/cext.c', line 876
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
19 20 21 |
# File 'lib/trilogy.rb', line 19 def connected_host @connected_host ||= query_with_flags("select @@hostname", query_flags | QUERY_FLAGS_FLATTEN_ROWS).rows.first end |
#escape(str) ⇒ Object
819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 |
# File 'ext/trilogy-ruby/cext.c', line 819
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
5 6 7 |
# File 'lib/trilogy.rb', line 5 def in_transaction? (server_status & SERVER_STATUS_IN_TRANS) != 0 end |
#last_gtid ⇒ Object
893 894 895 896 897 898 899 900 901 |
# File 'ext/trilogy-ruby/cext.c', line 893
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
887 |
# File 'ext/trilogy-ruby/cext.c', line 887
static VALUE rb_trilogy_last_insert_id(VALUE self) { return ULL2NUM(get_open_ctx(self)->conn.last_insert_id); }
|
#more_results_exist? ⇒ Boolean
756 757 758 759 760 761 762 763 764 765 |
# File 'ext/trilogy-ruby/cext.c', line 756
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
745 746 747 748 749 750 751 752 753 754 |
# File 'ext/trilogy-ruby/cext.c', line 745
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
786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 |
# File 'ext/trilogy-ruby/cext.c', line 786
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_syserr_fail(ETIMEDOUT, "trilogy_ping_recv");
}
}
return Qtrue;
}
|
#query(query) ⇒ Object
767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 |
# File 'ext/trilogy-ruby/cext.c', line 767
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
903 |
# File 'ext/trilogy-ruby/cext.c', line 903
static VALUE rb_trilogy_query_flags(VALUE self) { return UINT2NUM(get_ctx(self)->query_flags); }
|
#query_flags=(query_flags) ⇒ Object
905 906 907 908 |
# File 'ext/trilogy-ruby/cext.c', line 905
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
23 24 25 26 27 28 29 30 |
# File 'lib/trilogy.rb', line 23 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
910 911 912 913 |
# File 'ext/trilogy-ruby/cext.c', line 910
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
915 916 917 918 919 920 921 922 923 924 |
# File 'ext/trilogy-ruby/cext.c', line 915
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
9 10 11 12 13 14 15 16 17 |
# File 'lib/trilogy.rb', line 9 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
942 |
# File 'ext/trilogy-ruby/cext.c', line 942
static VALUE rb_trilogy_server_status(VALUE self) { return LONG2FIX(get_open_ctx(self)->conn.server_status); }
|
#server_version ⇒ Object
944 |
# File 'ext/trilogy-ruby/cext.c', line 944
static VALUE rb_trilogy_server_version(VALUE self) { return rb_str_new_cstr(get_open_ctx(self)->server_version); }
|
#warning_count ⇒ Object
891 |
# File 'ext/trilogy-ruby/cext.c', line 891
static VALUE rb_trilogy_warning_count(VALUE self) { return UINT2NUM(get_open_ctx(self)->conn.warning_count); }
|
#write_timeout ⇒ Object
926 927 928 929 |
# File 'ext/trilogy-ruby/cext.c', line 926
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
931 932 933 934 935 936 937 938 939 940 |
# File 'ext/trilogy-ruby/cext.c', line 931
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;
}
|