Class: Trilogy

Inherits:
Object
  • Object
show all
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

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
# 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 ((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_rowsObject



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

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

#change_db(database) ⇒ Object



500
501
502
503
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
# File 'ext/trilogy-ruby/cext.c', line 500

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

#closeObject



815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
# File 'ext/trilogy-ruby/cext.c', line 815

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)


849
850
851
852
853
854
855
856
857
858
# File 'ext/trilogy-ruby/cext.c', line 849

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



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



792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
# File 'ext/trilogy-ruby/cext.c', line 792

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)


5
6
7
# File 'lib/trilogy.rb', line 5

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

#last_gtidObject



866
867
868
869
870
871
872
873
874
# File 'ext/trilogy-ruby/cext.c', line 866

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



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

static VALUE rb_trilogy_last_insert_id(VALUE self) { return ULL2NUM(get_open_ctx(self)->conn.last_insert_id); }

#pingObject



759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
# File 'ext/trilogy-ruby/cext.c', line 759

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



719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
# File 'ext/trilogy-ruby/cext.c', line 719

static VALUE rb_trilogy_query(VALUE self, VALUE query)
{
    struct trilogy_ctx *ctx = get_open_ctx(self);

    StringValue(query);

    struct rb_trilogy_cast_options cast_options;
    load_query_options(ctx->query_flags, &cast_options);

    struct read_query_state args = {
        .cast_options = &cast_options,
        .column_info = NULL,
        .ctx = ctx,
        .query = query,
        .row_values = NULL,
        .rc = TRILOGY_OK,
        .msg = NULL,
    };

    int state = 0;
    VALUE result = rb_protect(execute_read_query, (VALUE)&args, &state);

    xfree(args.column_info);
    xfree(args.row_values);

    // If we have seen an unexpected exception, jump to it so it gets raised.
    if (state) {
        trilogy_sock_shutdown(ctx->conn.socket);
        rb_jump_tag(state);
    }

    // Handle errors we can gracefully recover from here that were due to
    // errors signaled at the protocol level, not unexpected exceptions.
    if (result == Qundef) {
        handle_trilogy_error(ctx, args.rc, args.msg);
    }

    return result;
}

#query_flagsObject



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

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

#query_flags=(query_flags) ⇒ Object



878
879
880
881
# File 'ext/trilogy-ruby/cext.c', line 878

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_timeoutObject



883
884
885
886
# File 'ext/trilogy-ruby/cext.c', line 883

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



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

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



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_statusObject



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

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

#server_versionObject



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

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

#warning_countObject



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

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

#write_timeoutObject



899
900
901
902
# File 'ext/trilogy-ruby/cext.c', line 899

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



904
905
906
907
908
909
910
911
912
913
# File 'ext/trilogy-ruby/cext.c', line 904

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