trilogy_w_prepared_statements 2.2.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (37) hide show
  1. checksums.yaml +7 -0
  2. data/LICENSE +22 -0
  3. data/README.md +80 -0
  4. data/Rakefile +22 -0
  5. data/ext/trilogy-ruby/cast.c +277 -0
  6. data/ext/trilogy-ruby/cext.c +1048 -0
  7. data/ext/trilogy-ruby/extconf.rb +17 -0
  8. data/ext/trilogy-ruby/inc/trilogy/blocking.h +281 -0
  9. data/ext/trilogy-ruby/inc/trilogy/buffer.h +64 -0
  10. data/ext/trilogy-ruby/inc/trilogy/builder.h +165 -0
  11. data/ext/trilogy-ruby/inc/trilogy/charset.h +277 -0
  12. data/ext/trilogy-ruby/inc/trilogy/client.h +760 -0
  13. data/ext/trilogy-ruby/inc/trilogy/error.h +44 -0
  14. data/ext/trilogy-ruby/inc/trilogy/packet_parser.h +34 -0
  15. data/ext/trilogy-ruby/inc/trilogy/protocol.h +1014 -0
  16. data/ext/trilogy-ruby/inc/trilogy/reader.h +216 -0
  17. data/ext/trilogy-ruby/inc/trilogy/socket.h +111 -0
  18. data/ext/trilogy-ruby/inc/trilogy/vendor/curl_hostcheck.h +29 -0
  19. data/ext/trilogy-ruby/inc/trilogy/vendor/openssl_hostname_validation.h +51 -0
  20. data/ext/trilogy-ruby/inc/trilogy.h +8 -0
  21. data/ext/trilogy-ruby/src/blocking.c +358 -0
  22. data/ext/trilogy-ruby/src/buffer.c +60 -0
  23. data/ext/trilogy-ruby/src/builder.c +236 -0
  24. data/ext/trilogy-ruby/src/charset.c +212 -0
  25. data/ext/trilogy-ruby/src/client.c +903 -0
  26. data/ext/trilogy-ruby/src/error.c +17 -0
  27. data/ext/trilogy-ruby/src/packet_parser.c +140 -0
  28. data/ext/trilogy-ruby/src/protocol.c +1175 -0
  29. data/ext/trilogy-ruby/src/reader.c +282 -0
  30. data/ext/trilogy-ruby/src/socket.c +623 -0
  31. data/ext/trilogy-ruby/src/vendor/curl_hostcheck.c +206 -0
  32. data/ext/trilogy-ruby/src/vendor/openssl_hostname_validation.c +175 -0
  33. data/ext/trilogy-ruby/trilogy-ruby.h +37 -0
  34. data/lib/trilogy/version.rb +3 -0
  35. data/lib/trilogy.rb +61 -0
  36. data/trilogy.gemspec +27 -0
  37. metadata +107 -0
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: ec4a87d044d75508eb0098cbc6ac7a51b9048219f4488c7781658b8cba7da314
4
+ data.tar.gz: 2b243fad0e3fa2c77f25dc73840c7bc3119aaa8303d4efa71f6995bc8fffcd88
5
+ SHA512:
6
+ metadata.gz: 79ba68f9552342091f5ec2f7ae68a662d27fc16293b7dbd8973c5aaf8c5c1ddbdf8664c08355ecf5f12d913f967ca05839d5339efbf5901c5d08776a5c35f1da
7
+ data.tar.gz: ab40958952f5f330847a7eb03431c0251c22d8ba1bd3ca0d58d0b4832314e94932f120cf07e4895b4d54fbd12f2645acf59e4b0239813a6bb4675dc5e19b65a9
data/LICENSE ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2012-2021 GitHub, Inc.
2
+
3
+ Permission is hereby granted, free of charge, to any person
4
+ obtaining a copy of this software and associated documentation
5
+ files (the "Software"), to deal in the Software without
6
+ restriction, including without limitation the rights to use,
7
+ copy, modify, merge, publish, distribute, sublicense, and/or sell
8
+ copies of the Software, and to permit persons to whom the
9
+ Software is furnished to do so, subject to the following
10
+ conditions:
11
+
12
+ The above copyright notice and this permission notice shall be
13
+ included in all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
17
+ OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
19
+ HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
20
+ WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21
+ FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
22
+ OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,80 @@
1
+ # trilogy
2
+
3
+ Ruby bindings to the Trilogy client library
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ``` ruby
10
+ gem 'trilogy'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ ```
16
+ $ bundle
17
+ ```
18
+
19
+ Or install it yourself as:
20
+
21
+ ```
22
+ $ gem install trilogy
23
+ ```
24
+
25
+ ## Usage
26
+
27
+ ``` ruby
28
+ client = Trilogy.new(host: "127.0.0.1", port: 3306, username: "root", read_timeout: 2)
29
+ if client.ping
30
+ client.query_options[:database_timezone] = :utc
31
+ client.change_db "mydb"
32
+
33
+ result = client.query("SELECT id, created_at FROM users LIMIT 10")
34
+ result.each_hash do |user|
35
+ p user
36
+ end
37
+
38
+ # Multi-statement
39
+
40
+ results = []
41
+ results << client.query("SELECT name FROM users WHERE id = 1; SELECT name FROM users WHERE id = 2")
42
+ results << client.next_result while client.more_results_exist?
43
+ end
44
+ ```
45
+
46
+ ## Building
47
+ You should use the rake commands to build/install/release the gem
48
+ For instance:
49
+ ```shell
50
+ bundle exec rake build
51
+ ```
52
+
53
+ ## Contributing
54
+
55
+ The official Ruby bindings are inside of the canonical trilogy repository itself.
56
+
57
+ 1. Fork it ( https://github.com/github/trilogy/fork )
58
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
59
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
60
+ 4. Push to the branch (`git push origin my-new-feature`)
61
+ 5. Create a new Pull Request
62
+
63
+ ## mysql2 gem compatibility
64
+
65
+ The trilogy API was heavily inspired by the mysql2 gem but has a few notable
66
+ differences:
67
+
68
+ * The `query_options` hash doesn't inherit from the connection options hash.
69
+ This means that options like turning on/of casting will need to be set before
70
+ a query and not passed in at connect time.
71
+ * For performance reasons there is no `application_timezone` query option. If
72
+ casting is enabled and your database timezone is different than what the
73
+ application is expecting you'll need to do the conversion yourself later.
74
+ * While we still tag strings with the encoding configured on the field they came
75
+ from - for performance reasons no automatic transcoding into
76
+ `Encoding.default_internal` is done. Similarly to not automatically converting
77
+ Time objects from `database_timezone` into `application_timezone`, we leave
78
+ the transcoding step up to the caller.
79
+ * There is no `as` query option. Calling `Trilogy::Result#each` will yield an array
80
+ of row values. If you want a hash you should use `Trilogy::Result#each_hash`.
data/Rakefile ADDED
@@ -0,0 +1,22 @@
1
+ require "bundler/gem_tasks"
2
+ require "rake/extensiontask"
3
+ require "rake/testtask"
4
+
5
+ Rake::ExtensionTask.new do |ext|
6
+ ext.name = "cext"
7
+ ext.ext_dir = "ext/trilogy-ruby"
8
+ ext.lib_dir = "lib/trilogy"
9
+ end
10
+
11
+ Rake::TestTask.new do |t|
12
+ t.libs << "test"
13
+ t.test_files = FileList['test/*_test.rb']
14
+ t.verbose = true
15
+ end
16
+ task :test => :compile
17
+
18
+ task :default => :test
19
+
20
+ task :console => :compile do
21
+ sh "ruby -I lib -r trilogy -S irb"
22
+ end
@@ -0,0 +1,277 @@
1
+ #include <ruby.h>
2
+ #include <ruby/encoding.h>
3
+
4
+ #include <trilogy.h>
5
+
6
+ #include "trilogy-ruby.h"
7
+
8
+ #define CAST_STACK_SIZE 64
9
+
10
+ static ID id_BigDecimal, id_Integer, id_new, id_local, id_localtime, id_utc;
11
+
12
+ static const char *ruby_encoding_name_map[] = {
13
+ [TRILOGY_ENCODING_ARMSCII8] = NULL,
14
+ [TRILOGY_ENCODING_ASCII] = "US-ASCII",
15
+ [TRILOGY_ENCODING_BIG5] = "Big5",
16
+ [TRILOGY_ENCODING_BINARY] = "BINARY",
17
+ [TRILOGY_ENCODING_CP1250] = "Windows-1250",
18
+ [TRILOGY_ENCODING_CP1251] = "Windows-1251",
19
+ [TRILOGY_ENCODING_CP1256] = "Windows-1256",
20
+ [TRILOGY_ENCODING_CP1257] = "Windows-1257",
21
+ [TRILOGY_ENCODING_CP850] = "CP850",
22
+ [TRILOGY_ENCODING_CP852] = "CP852",
23
+ [TRILOGY_ENCODING_CP866] = "IBM866",
24
+ [TRILOGY_ENCODING_CP932] = "Windows-31J",
25
+ [TRILOGY_ENCODING_DEC8] = NULL,
26
+ [TRILOGY_ENCODING_EUCJPMS] = "eucJP-ms",
27
+ [TRILOGY_ENCODING_EUCKR] = "EUC-KR",
28
+ [TRILOGY_ENCODING_GB2312] = "GB2312",
29
+ [TRILOGY_ENCODING_GBK] = "GBK",
30
+ [TRILOGY_ENCODING_GEOSTD8] = NULL,
31
+ [TRILOGY_ENCODING_GREEK] = "ISO-8859-7",
32
+ [TRILOGY_ENCODING_HEBREW] = "ISO-8859-8",
33
+ [TRILOGY_ENCODING_HP8] = NULL,
34
+ [TRILOGY_ENCODING_KEYBCS2] = NULL,
35
+ [TRILOGY_ENCODING_KOI8R] = "KOI8-R",
36
+ [TRILOGY_ENCODING_KOI8U] = "KOI8-U",
37
+ [TRILOGY_ENCODING_LATIN1] = "ISO-8859-1",
38
+ [TRILOGY_ENCODING_LATIN2] = "ISO-8859-2",
39
+ [TRILOGY_ENCODING_LATIN5] = "ISO-8859-9",
40
+ [TRILOGY_ENCODING_LATIN7] = "ISO-8859-13",
41
+ [TRILOGY_ENCODING_MACCE] = "macCentEuro",
42
+ [TRILOGY_ENCODING_MACROMAN] = "macRoman",
43
+ [TRILOGY_ENCODING_NONE] = NULL,
44
+ [TRILOGY_ENCODING_SJIS] = "Shift_JIS",
45
+ [TRILOGY_ENCODING_SWE7] = NULL,
46
+ [TRILOGY_ENCODING_TIS620] = "TIS-620",
47
+ [TRILOGY_ENCODING_UCS2] = "UTF-16BE",
48
+ [TRILOGY_ENCODING_UJIS] = "eucJP-ms",
49
+ [TRILOGY_ENCODING_UTF16] = "UTF-16BE",
50
+ [TRILOGY_ENCODING_UTF32] = "UTF-32",
51
+ [TRILOGY_ENCODING_UTF8] = "UTF-8",
52
+ [TRILOGY_ENCODING_UTF8MB4] = "UTF-8",
53
+
54
+ [TRILOGY_ENCODING_MAX] = NULL,
55
+ };
56
+
57
+ static int encoding_for_charset(TRILOGY_CHARSET_t charset)
58
+ {
59
+ static int map[TRILOGY_CHARSET_MAX];
60
+
61
+ if (map[charset]) {
62
+ return map[charset];
63
+ }
64
+
65
+ const char *encoding_name = ruby_encoding_name_map[trilogy_encoding_from_charset(charset)];
66
+
67
+ return map[charset] = (encoding_name ? rb_enc_find_index(encoding_name) : -1);
68
+ }
69
+
70
+ static void cstr_from_value(char *buf, const trilogy_value_t *value, const char *errmsg)
71
+ {
72
+
73
+ if (value->data_len > CAST_STACK_SIZE - 1) {
74
+ rb_raise(rb_cTrilogyError, errmsg, (int)value->data_len, (char *)value->data);
75
+ }
76
+
77
+ memcpy(buf, value->data, value->data_len);
78
+ buf[value->data_len] = 0;
79
+ }
80
+
81
+ static unsigned long long ull_from_buf(const char *digits, size_t len)
82
+ {
83
+ if (!len)
84
+ return 0;
85
+
86
+ unsigned long long val = 0;
87
+
88
+ while (len--) {
89
+ unsigned digit = *digits++ - '0';
90
+ val = val * 10 + digit;
91
+ }
92
+
93
+ return val;
94
+ }
95
+
96
+ static long long ll_from_buf(const char *digits, size_t len)
97
+ {
98
+ if (!len)
99
+ return 0;
100
+
101
+ if (digits[0] == '-') {
102
+ return -(long long)ull_from_buf(&digits[1], len - 1);
103
+ } else {
104
+ return (long long)ull_from_buf(digits, len);
105
+ }
106
+ }
107
+
108
+ VALUE
109
+ rb_trilogy_cast_value(const trilogy_value_t *value, const struct column_info *column,
110
+ const struct rb_trilogy_cast_options *options)
111
+ {
112
+ if (value->is_null) {
113
+ return Qnil;
114
+ }
115
+
116
+ if (options->cast) {
117
+ switch (column->type) {
118
+ case TRILOGY_TYPE_BIT: {
119
+ if (options->cast_booleans && column->len == 1) {
120
+ return *(const char *)value->data == 1 ? Qtrue : Qfalse;
121
+ }
122
+ break;
123
+ }
124
+ case TRILOGY_TYPE_TINY: {
125
+ if (options->cast_booleans && column->len == 1) {
126
+ return *(const char *)value->data != '0' ? Qtrue : Qfalse;
127
+ }
128
+ /* fall through */
129
+ }
130
+ case TRILOGY_TYPE_SHORT:
131
+ case TRILOGY_TYPE_LONG:
132
+ case TRILOGY_TYPE_LONGLONG:
133
+ case TRILOGY_TYPE_INT24:
134
+ case TRILOGY_TYPE_YEAR: {
135
+ if (column->flags & TRILOGY_COLUMN_FLAG_UNSIGNED) {
136
+ unsigned long long num = ull_from_buf(value->data, value->data_len);
137
+ return ULL2NUM(num);
138
+ } else {
139
+ long long num = ll_from_buf(value->data, value->data_len);
140
+ return LL2NUM(num);
141
+ }
142
+ }
143
+ case TRILOGY_TYPE_DECIMAL:
144
+ case TRILOGY_TYPE_NEWDECIMAL: {
145
+ // TODO - optimize so we don't have to allocate a ruby string for
146
+ // decimal columns
147
+ VALUE str = rb_str_new(value->data, value->data_len);
148
+ if (column->decimals == 0) {
149
+ return rb_funcall(rb_mKernel, id_Integer, 1, str);
150
+ } else {
151
+ return rb_funcall(rb_mKernel, id_BigDecimal, 1, str);
152
+ }
153
+ }
154
+ case TRILOGY_TYPE_FLOAT:
155
+ case TRILOGY_TYPE_DOUBLE: {
156
+ char cstr[CAST_STACK_SIZE];
157
+ cstr_from_value(cstr, value, "Invalid double value: %.*s");
158
+
159
+ char *err;
160
+ double dbl = strtod(cstr, &err);
161
+
162
+ if (*err != 0) {
163
+ rb_raise(rb_cTrilogyError, "Invalid double value: %.*s", (int)value->data_len, (char *)value->data);
164
+ }
165
+ return rb_float_new(dbl);
166
+ }
167
+ case TRILOGY_TYPE_TIMESTAMP:
168
+ case TRILOGY_TYPE_DATETIME: {
169
+ int year, month, day, hour, min, sec;
170
+ char msec_char[7] = {0};
171
+
172
+ char cstr[CAST_STACK_SIZE];
173
+ cstr_from_value(cstr, value, "Invalid date: %.*s");
174
+
175
+ int tokens = sscanf(cstr, "%4u-%2u-%2u %2u:%2u:%2u.%6s", &year, &month, &day, &hour, &min, &sec, msec_char);
176
+
177
+ // msec might not be present, so check for 6 tokens rather than 7
178
+ if (tokens < 6) {
179
+ return Qnil;
180
+ }
181
+
182
+ if (year == 0 && month == 0 && day == 0 && hour == 0 && min == 0 && sec == 0) {
183
+ return Qnil;
184
+ }
185
+
186
+ if (month < 1 || day < 1) {
187
+ rb_raise(rb_cTrilogyError, "Invalid date: %.*s", (int)value->data_len, (char *)value->data);
188
+ }
189
+
190
+ // pad out msec_char with zeroes at the end as it could be at any
191
+ // level of precision
192
+ for (size_t i = strlen(msec_char); i < sizeof(msec_char) - 1; i++) {
193
+ msec_char[i] = '0';
194
+ }
195
+
196
+ return rb_funcall(rb_cTime, options->database_local_time ? id_local : id_utc, 7, INT2NUM(year),
197
+ INT2NUM(month), INT2NUM(day), INT2NUM(hour), INT2NUM(min), INT2NUM(sec),
198
+ INT2NUM(atoi(msec_char)));
199
+ }
200
+ case TRILOGY_TYPE_DATE: {
201
+ int year, month, day;
202
+
203
+ char cstr[CAST_STACK_SIZE];
204
+ cstr_from_value(cstr, value, "Invalid date: %.*s");
205
+
206
+ int tokens = sscanf(cstr, "%4u-%2u-%2u", &year, &month, &day);
207
+ VALUE Date = rb_const_get(rb_cObject, rb_intern("Date"));
208
+
209
+ if (tokens < 3) {
210
+ return Qnil;
211
+ }
212
+
213
+ if (year == 0 && month == 0 && day == 0) {
214
+ return Qnil;
215
+ }
216
+
217
+ if (month < 1 || day < 1) {
218
+ rb_raise(rb_cTrilogyError, "Invalid date: %.*s", (int)value->data_len, (char *)value->data);
219
+ }
220
+
221
+ return rb_funcall(Date, id_new, 3, INT2NUM(year), INT2NUM(month), INT2NUM(day));
222
+ }
223
+ case TRILOGY_TYPE_TIME: {
224
+ int hour, min, sec;
225
+ char msec_char[7] = {0};
226
+
227
+ char cstr[CAST_STACK_SIZE];
228
+ cstr_from_value(cstr, value, "Invalid time: %.*s");
229
+
230
+ int tokens = sscanf(cstr, "%2u:%2u:%2u.%6s", &hour, &min, &sec, msec_char);
231
+
232
+ if (tokens < 3) {
233
+ return Qnil;
234
+ }
235
+
236
+ if (hour == 0 && min == 0 && sec == 0) {
237
+ return Qnil;
238
+ }
239
+
240
+ // pad out msec_char with zeroes at the end as it could be at any
241
+ // level of precision
242
+ for (size_t i = strlen(msec_char); i < sizeof(msec_char) - 1; i++) {
243
+ msec_char[i] = 0;
244
+ }
245
+
246
+ return rb_funcall(rb_cTime, options->database_local_time ? id_local : id_utc, 7, INT2NUM(2000), INT2NUM(1),
247
+ INT2NUM(1), INT2NUM(hour), INT2NUM(min), INT2NUM(sec), INT2NUM(atoi(msec_char)));
248
+ }
249
+ default:
250
+ break;
251
+ }
252
+ }
253
+
254
+ // for all other types, just return a string
255
+
256
+ VALUE str = rb_str_new(value->data, value->data_len);
257
+
258
+ int encoding_index = encoding_for_charset(column->charset);
259
+ if (encoding_index != -1) {
260
+ rb_enc_associate_index(str, encoding_index);
261
+ }
262
+
263
+ return str;
264
+ }
265
+
266
+ void rb_trilogy_cast_init(void)
267
+ {
268
+ rb_require("bigdecimal");
269
+ rb_require("date");
270
+
271
+ id_BigDecimal = rb_intern("BigDecimal");
272
+ id_Integer = rb_intern("Integer");
273
+ id_new = rb_intern("new");
274
+ id_local = rb_intern("local");
275
+ id_localtime = rb_intern("localtime");
276
+ id_utc = rb_intern("utc");
277
+ }