trilogy 2.13.1 → 2.13.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 50f7b9c6e6db317bed3d230ffc0714c109ac78cce85a391a3530cee82c8676e2
4
- data.tar.gz: 7c95de9ace5aa5669ce8fac0bd95513be353cce52bed60a6441a9558dd18abe5
3
+ metadata.gz: 4ea4b038d25e87b1d26c6dc0af6aa37b3cfa2b7244ea4788155bb392f7a5e93e
4
+ data.tar.gz: c4201dd5f2c3974244cea9ae97af6052d3d1991509e64c2dde8319882b00a53e
5
5
  SHA512:
6
- metadata.gz: 88a4f0114420039839374a8526dea80982862772d7b89e940dd509109b61500191723912372d3798415c02b8af9168b8f7d5dceaec79518577096a0314c1d0f5
7
- data.tar.gz: 16d595b50c2804a377cdb8899add3ac9158b2991297fe6163c83d7699b216222fc3991c84c5dc67a8331bbe1480cd7c3afc24938297a46a34630d4bb47452602
6
+ metadata.gz: 2e58cdf8bf20b4d31e43b811ceb7972a772c2ccb44133f715a4d9c0723c1c1ed4b3139c5e7be68575b119932348f5d8d393d088b88fd73ba058eeef9d8203796
7
+ data.tar.gz: ef9527a11371f0b6dbab901eb6c682e5a6e7cbf48d81e483b647682355b825a08ca342491a523859f543bfcaaf598588a713c5859baf8a883fe5adcda14f59ea
@@ -56,8 +56,11 @@ static const char *ruby_encoding_name_map[] = {
56
56
 
57
57
  static rb_encoding * encoding_for_charset(TRILOGY_CHARSET_t charset)
58
58
  {
59
- static rb_encoding * map[TRILOGY_CHARSET_MAX];
59
+ if (RB_UNLIKELY(charset >= TRILOGY_CHARSET_MAX)) {
60
+ return rb_enc_find(ruby_encoding_name_map[trilogy_encoding_from_charset(charset)]);
61
+ }
60
62
 
63
+ static rb_encoding * map[TRILOGY_CHARSET_MAX];
61
64
  if (RB_LIKELY(map[charset])) {
62
65
  return map[charset];
63
66
  }
@@ -1128,7 +1128,6 @@ static VALUE rb_trilogy_query(VALUE self, VALUE query)
1128
1128
  struct trilogy_ctx *ctx = get_open_ctx(self);
1129
1129
 
1130
1130
  StringValue(query);
1131
- query = rb_str_export_to_enc(query, ctx->encoding);
1132
1131
  query = rb_str_new_frozen(query);
1133
1132
 
1134
1133
  rb_trilogy_acquire_buffer(ctx);
@@ -1189,13 +1188,6 @@ static VALUE rb_trilogy_escape(VALUE self, VALUE str)
1189
1188
  struct trilogy_ctx *ctx = get_open_ctx(self);
1190
1189
 
1191
1190
  StringValue(str);
1192
- str = rb_str_new_frozen(str);
1193
-
1194
- rb_encoding *str_enc = rb_enc_get(str);
1195
-
1196
- if (!rb_enc_asciicompat(str_enc)) {
1197
- rb_raise(rb_eEncCompatError, "input string must be ASCII-compatible");
1198
- }
1199
1191
 
1200
1192
  const char *escaped_str;
1201
1193
  size_t escaped_len;
@@ -1208,7 +1200,7 @@ static VALUE rb_trilogy_escape(VALUE self, VALUE str)
1208
1200
  handle_trilogy_error(ctx, rc, "trilogy_escape");
1209
1201
  }
1210
1202
 
1211
- VALUE escaped_string = rb_enc_str_new(escaped_str, escaped_len, str_enc);
1203
+ VALUE escaped_string = rb_enc_str_new(escaped_str, escaped_len, ctx->encoding);
1212
1204
 
1213
1205
  rb_trilogy_release_buffer(ctx);
1214
1206
 
@@ -1371,9 +1363,9 @@ RUBY_FUNC_EXPORTED void Init_cext(void)
1371
1363
  rb_define_private_method(Trilogy, "_connect", rb_trilogy_connect, 4);
1372
1364
  rb_define_method(Trilogy, "change_db", rb_trilogy_change_db, 1);
1373
1365
  rb_define_alias(Trilogy, "select_db", "change_db");
1374
- rb_define_method(Trilogy, "query", rb_trilogy_query, 1);
1366
+ rb_define_private_method(Trilogy, "_query", rb_trilogy_query, 1);
1375
1367
  rb_define_method(Trilogy, "ping", rb_trilogy_ping, 0);
1376
- rb_define_method(Trilogy, "escape", rb_trilogy_escape, 1);
1368
+ rb_define_private_method(Trilogy, "_escape", rb_trilogy_escape, 1);
1377
1369
  rb_define_method(Trilogy, "close", rb_trilogy_close, 0);
1378
1370
  rb_define_method(Trilogy, "closed?", rb_trilogy_closed, 0);
1379
1371
  rb_define_method(Trilogy, "check", rb_trilogy_check, 0);
@@ -84,7 +84,13 @@ class Trilogy
84
84
  "binary" => CHARSET_BINARY,
85
85
  }.freeze
86
86
 
87
+ UNSUPPORTED_ENCODINGS = %w[big5 sjis gbk cp932 gb18030 ucs2 utf16 utf16le utf32].freeze
88
+
87
89
  def self.find(mysql_encoding)
90
+ if UNSUPPORTED_ENCODINGS.include?(mysql_encoding)
91
+ raise ArgumentError, "Unsupported encoding: #{mysql_encoding}"
92
+ end
93
+
88
94
  unless rb_encoding = RUBY_ENCODINGS[mysql_encoding]
89
95
  raise ArgumentError, "Unknown or unsupported encoding: #{mysql_encoding}"
90
96
  end
@@ -1,3 +1,3 @@
1
1
  class Trilogy
2
- VERSION = "2.13.1"
2
+ VERSION = "2.13.2"
3
3
  end
data/lib/trilogy.rb CHANGED
@@ -16,6 +16,27 @@ class Trilogy
16
16
  end
17
17
  private_constant :IO_TIMEOUT_ERROR
18
18
 
19
+ def query(arg)
20
+ str = String.try_convert(arg) or raise TypeError, "no implicit conversion of #{arg.class} into String"
21
+ str = str.dup if str.equal?(arg)
22
+ str.freeze
23
+
24
+ str_encoding = str.encoding
25
+ if @encoding != str_encoding && ::Encoding::BINARY != str_encoding && !(str.ascii_only? && @encoding.ascii_compatible?)
26
+ raise ::Encoding::CompatibilityError, "Trilogy connection uses #{@encoding} but the query was provided in #{str_encoding}"
27
+ end
28
+
29
+ _query(str)
30
+ end
31
+
32
+ def escape(arg)
33
+ str = String.try_convert(arg) or raise TypeError, "no implicit conversion of #{arg.class} into String"
34
+ if @encoding != str.encoding && !(str.ascii_only? && @encoding.ascii_compatible?) && str.encoding != ::Encoding::ASCII_8BIT
35
+ str = str.encode(@encoding)
36
+ end
37
+ _escape(str)
38
+ end
39
+
19
40
  module Synchronization
20
41
  def initialize(...)
21
42
  @mutex = Mutex.new
@@ -47,10 +68,12 @@ class Trilogy
47
68
 
48
69
  prepend(Synchronization)
49
70
 
71
+ attr_reader :encoding
72
+
50
73
  def initialize(options = {})
51
74
  options[:port] = options[:port].to_i if options[:port]
52
75
  mysql_encoding = options[:encoding] || "utf8mb4"
53
- encoding = Trilogy::Encoding.find(mysql_encoding)
76
+ @encoding = Trilogy::Encoding.find(mysql_encoding)
54
77
  charset = Trilogy::Encoding.charset(mysql_encoding)
55
78
  @connection_options = options
56
79
  @connected_host = nil
@@ -99,7 +122,7 @@ class Trilogy
99
122
  end
100
123
  end
101
124
 
102
- _connect(socket, encoding, charset, options)
125
+ _connect(socket, @encoding, charset, options)
103
126
  ensure
104
127
  # Socket's fd will be dup'd in C
105
128
  socket&.close
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: trilogy
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.13.1
4
+ version: 2.13.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - GitHub Engineering
@@ -87,7 +87,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
87
87
  - !ruby/object:Gem::Version
88
88
  version: '0'
89
89
  requirements: []
90
- rubygems_version: 4.0.6
90
+ rubygems_version: 4.0.20
91
91
  specification_version: 4
92
92
  summary: A friendly MySQL-compatible library for Ruby, binding to libtrilogy
93
93
  test_files: []