autocompl 0.2.1 → 0.2.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.
Files changed (131) hide show
  1. checksums.yaml +4 -4
  2. data/lib/autocompl/repository.rb +11 -1
  3. data/lib/autocompl/version.rb +1 -1
  4. data/test/dummy/log/development.log +467 -0
  5. data/test/dummy/log/test.log +3 -0
  6. data/test/dummy/vendor/bundle/ruby/2.3.0/bin/console +23 -0
  7. data/test/dummy/vendor/bundle/ruby/2.3.0/cache/pg-0.19.0.gem +0 -0
  8. data/test/dummy/vendor/bundle/ruby/2.3.0/cache/pq-0.0.1.gem +0 -0
  9. data/test/dummy/vendor/bundle/ruby/2.3.0/extensions/x86_64-darwin-15/2.3.0-static/pg-0.19.0/gem.build_complete +0 -0
  10. data/test/dummy/vendor/bundle/ruby/2.3.0/extensions/x86_64-darwin-15/2.3.0-static/pg-0.19.0/gem_make.out +78 -0
  11. data/test/dummy/vendor/bundle/ruby/2.3.0/extensions/x86_64-darwin-15/2.3.0-static/pg-0.19.0/mkmf.log +1346 -0
  12. data/test/dummy/vendor/bundle/ruby/2.3.0/extensions/x86_64-darwin-15/2.3.0-static/pg-0.19.0/pg_ext.bundle +0 -0
  13. data/test/dummy/vendor/bundle/ruby/2.3.0/gems/pg-0.19.0/BSDL +22 -0
  14. data/test/dummy/vendor/bundle/ruby/2.3.0/gems/pg-0.19.0/ChangeLog +6378 -0
  15. data/test/dummy/vendor/bundle/ruby/2.3.0/gems/pg-0.19.0/Contributors.rdoc +46 -0
  16. data/test/dummy/vendor/bundle/ruby/2.3.0/gems/pg-0.19.0/History.rdoc +363 -0
  17. data/test/dummy/vendor/bundle/ruby/2.3.0/gems/pg-0.19.0/LICENSE +56 -0
  18. data/test/dummy/vendor/bundle/ruby/2.3.0/gems/pg-0.19.0/Manifest.txt +85 -0
  19. data/test/dummy/vendor/bundle/ruby/2.3.0/gems/pg-0.19.0/POSTGRES +23 -0
  20. data/test/dummy/vendor/bundle/ruby/2.3.0/gems/pg-0.19.0/README-OS_X.rdoc +68 -0
  21. data/test/dummy/vendor/bundle/ruby/2.3.0/gems/pg-0.19.0/README-Windows.rdoc +56 -0
  22. data/test/dummy/vendor/bundle/ruby/2.3.0/gems/pg-0.19.0/README.ja.rdoc +14 -0
  23. data/test/dummy/vendor/bundle/ruby/2.3.0/gems/pg-0.19.0/README.rdoc +168 -0
  24. data/test/dummy/vendor/bundle/ruby/2.3.0/gems/pg-0.19.0/Rakefile +216 -0
  25. data/test/dummy/vendor/bundle/ruby/2.3.0/gems/pg-0.19.0/Rakefile.cross +301 -0
  26. data/test/dummy/vendor/bundle/ruby/2.3.0/gems/pg-0.19.0/ext/Makefile +261 -0
  27. data/test/dummy/vendor/bundle/ruby/2.3.0/gems/pg-0.19.0/ext/errorcodes.def +947 -0
  28. data/test/dummy/vendor/bundle/ruby/2.3.0/gems/pg-0.19.0/ext/errorcodes.rb +45 -0
  29. data/test/dummy/vendor/bundle/ruby/2.3.0/gems/pg-0.19.0/ext/errorcodes.txt +467 -0
  30. data/test/dummy/vendor/bundle/ruby/2.3.0/gems/pg-0.19.0/ext/extconf.h +38 -0
  31. data/test/dummy/vendor/bundle/ruby/2.3.0/gems/pg-0.19.0/ext/extconf.rb +112 -0
  32. data/test/dummy/vendor/bundle/ruby/2.3.0/gems/pg-0.19.0/ext/gvl_wrappers.c +13 -0
  33. data/test/dummy/vendor/bundle/ruby/2.3.0/gems/pg-0.19.0/ext/gvl_wrappers.h +257 -0
  34. data/test/dummy/vendor/bundle/ruby/2.3.0/gems/pg-0.19.0/ext/gvl_wrappers.o +0 -0
  35. data/test/dummy/vendor/bundle/ruby/2.3.0/gems/pg-0.19.0/ext/pg.c +667 -0
  36. data/test/dummy/vendor/bundle/ruby/2.3.0/gems/pg-0.19.0/ext/pg.h +395 -0
  37. data/test/dummy/vendor/bundle/ruby/2.3.0/gems/pg-0.19.0/ext/pg.o +0 -0
  38. data/test/dummy/vendor/bundle/ruby/2.3.0/gems/pg-0.19.0/ext/pg_binary_decoder.c +162 -0
  39. data/test/dummy/vendor/bundle/ruby/2.3.0/gems/pg-0.19.0/ext/pg_binary_decoder.o +0 -0
  40. data/test/dummy/vendor/bundle/ruby/2.3.0/gems/pg-0.19.0/ext/pg_binary_encoder.c +162 -0
  41. data/test/dummy/vendor/bundle/ruby/2.3.0/gems/pg-0.19.0/ext/pg_binary_encoder.o +0 -0
  42. data/test/dummy/vendor/bundle/ruby/2.3.0/gems/pg-0.19.0/ext/pg_coder.c +500 -0
  43. data/test/dummy/vendor/bundle/ruby/2.3.0/gems/pg-0.19.0/ext/pg_coder.o +0 -0
  44. data/test/dummy/vendor/bundle/ruby/2.3.0/gems/pg-0.19.0/ext/pg_connection.c +4102 -0
  45. data/test/dummy/vendor/bundle/ruby/2.3.0/gems/pg-0.19.0/ext/pg_connection.o +0 -0
  46. data/test/dummy/vendor/bundle/ruby/2.3.0/gems/pg-0.19.0/ext/pg_copy_coder.c +591 -0
  47. data/test/dummy/vendor/bundle/ruby/2.3.0/gems/pg-0.19.0/ext/pg_copy_coder.o +0 -0
  48. data/test/dummy/vendor/bundle/ruby/2.3.0/gems/pg-0.19.0/ext/pg_errors.c +95 -0
  49. data/test/dummy/vendor/bundle/ruby/2.3.0/gems/pg-0.19.0/ext/pg_errors.o +0 -0
  50. data/test/dummy/vendor/bundle/ruby/2.3.0/gems/pg-0.19.0/ext/pg_ext.bundle +0 -0
  51. data/test/dummy/vendor/bundle/ruby/2.3.0/gems/pg-0.19.0/ext/pg_result.c +1271 -0
  52. data/test/dummy/vendor/bundle/ruby/2.3.0/gems/pg-0.19.0/ext/pg_result.o +0 -0
  53. data/test/dummy/vendor/bundle/ruby/2.3.0/gems/pg-0.19.0/ext/pg_text_decoder.c +421 -0
  54. data/test/dummy/vendor/bundle/ruby/2.3.0/gems/pg-0.19.0/ext/pg_text_decoder.o +0 -0
  55. data/test/dummy/vendor/bundle/ruby/2.3.0/gems/pg-0.19.0/ext/pg_text_encoder.c +683 -0
  56. data/test/dummy/vendor/bundle/ruby/2.3.0/gems/pg-0.19.0/ext/pg_text_encoder.o +0 -0
  57. data/test/dummy/vendor/bundle/ruby/2.3.0/gems/pg-0.19.0/ext/pg_type_map.c +159 -0
  58. data/test/dummy/vendor/bundle/ruby/2.3.0/gems/pg-0.19.0/ext/pg_type_map.o +0 -0
  59. data/test/dummy/vendor/bundle/ruby/2.3.0/gems/pg-0.19.0/ext/pg_type_map_all_strings.c +116 -0
  60. data/test/dummy/vendor/bundle/ruby/2.3.0/gems/pg-0.19.0/ext/pg_type_map_all_strings.o +0 -0
  61. data/test/dummy/vendor/bundle/ruby/2.3.0/gems/pg-0.19.0/ext/pg_type_map_by_class.c +239 -0
  62. data/test/dummy/vendor/bundle/ruby/2.3.0/gems/pg-0.19.0/ext/pg_type_map_by_class.o +0 -0
  63. data/test/dummy/vendor/bundle/ruby/2.3.0/gems/pg-0.19.0/ext/pg_type_map_by_column.c +312 -0
  64. data/test/dummy/vendor/bundle/ruby/2.3.0/gems/pg-0.19.0/ext/pg_type_map_by_column.o +0 -0
  65. data/test/dummy/vendor/bundle/ruby/2.3.0/gems/pg-0.19.0/ext/pg_type_map_by_mri_type.c +284 -0
  66. data/test/dummy/vendor/bundle/ruby/2.3.0/gems/pg-0.19.0/ext/pg_type_map_by_mri_type.o +0 -0
  67. data/test/dummy/vendor/bundle/ruby/2.3.0/gems/pg-0.19.0/ext/pg_type_map_by_oid.c +355 -0
  68. data/test/dummy/vendor/bundle/ruby/2.3.0/gems/pg-0.19.0/ext/pg_type_map_by_oid.o +0 -0
  69. data/test/dummy/vendor/bundle/ruby/2.3.0/gems/pg-0.19.0/ext/pg_type_map_in_ruby.c +299 -0
  70. data/test/dummy/vendor/bundle/ruby/2.3.0/gems/pg-0.19.0/ext/pg_type_map_in_ruby.o +0 -0
  71. data/test/dummy/vendor/bundle/ruby/2.3.0/gems/pg-0.19.0/ext/util.c +149 -0
  72. data/test/dummy/vendor/bundle/ruby/2.3.0/gems/pg-0.19.0/ext/util.h +65 -0
  73. data/test/dummy/vendor/bundle/ruby/2.3.0/gems/pg-0.19.0/ext/util.o +0 -0
  74. data/test/dummy/vendor/bundle/ruby/2.3.0/gems/pg-0.19.0/ext/vc/pg.sln +26 -0
  75. data/test/dummy/vendor/bundle/ruby/2.3.0/gems/pg-0.19.0/ext/vc/pg_18/pg.vcproj +216 -0
  76. data/test/dummy/vendor/bundle/ruby/2.3.0/gems/pg-0.19.0/ext/vc/pg_19/pg_19.vcproj +209 -0
  77. data/test/dummy/vendor/bundle/ruby/2.3.0/gems/pg-0.19.0/lib/pg.rb +64 -0
  78. data/test/dummy/vendor/bundle/ruby/2.3.0/gems/pg-0.19.0/lib/pg/basic_type_mapping.rb +426 -0
  79. data/test/dummy/vendor/bundle/ruby/2.3.0/gems/pg-0.19.0/lib/pg/coder.rb +83 -0
  80. data/test/dummy/vendor/bundle/ruby/2.3.0/gems/pg-0.19.0/lib/pg/connection.rb +271 -0
  81. data/test/dummy/vendor/bundle/ruby/2.3.0/gems/pg-0.19.0/lib/pg/constants.rb +11 -0
  82. data/test/dummy/vendor/bundle/ruby/2.3.0/gems/pg-0.19.0/lib/pg/exceptions.rb +11 -0
  83. data/test/dummy/vendor/bundle/ruby/2.3.0/gems/pg-0.19.0/lib/pg/result.rb +30 -0
  84. data/test/dummy/vendor/bundle/ruby/2.3.0/gems/pg-0.19.0/lib/pg/text_decoder.rb +51 -0
  85. data/test/dummy/vendor/bundle/ruby/2.3.0/gems/pg-0.19.0/lib/pg/text_encoder.rb +35 -0
  86. data/test/dummy/vendor/bundle/ruby/2.3.0/gems/pg-0.19.0/lib/pg/type_map_by_column.rb +15 -0
  87. data/test/dummy/vendor/bundle/ruby/2.3.0/gems/pg-0.19.0/lib/pg_ext.bundle +0 -0
  88. data/test/dummy/vendor/bundle/ruby/2.3.0/gems/pg-0.19.0/sample/array_insert.rb +20 -0
  89. data/test/dummy/vendor/bundle/ruby/2.3.0/gems/pg-0.19.0/sample/async_api.rb +106 -0
  90. data/test/dummy/vendor/bundle/ruby/2.3.0/gems/pg-0.19.0/sample/async_copyto.rb +39 -0
  91. data/test/dummy/vendor/bundle/ruby/2.3.0/gems/pg-0.19.0/sample/async_mixed.rb +56 -0
  92. data/test/dummy/vendor/bundle/ruby/2.3.0/gems/pg-0.19.0/sample/check_conn.rb +21 -0
  93. data/test/dummy/vendor/bundle/ruby/2.3.0/gems/pg-0.19.0/sample/copyfrom.rb +81 -0
  94. data/test/dummy/vendor/bundle/ruby/2.3.0/gems/pg-0.19.0/sample/copyto.rb +19 -0
  95. data/test/dummy/vendor/bundle/ruby/2.3.0/gems/pg-0.19.0/sample/cursor.rb +21 -0
  96. data/test/dummy/vendor/bundle/ruby/2.3.0/gems/pg-0.19.0/sample/disk_usage_report.rb +186 -0
  97. data/test/dummy/vendor/bundle/ruby/2.3.0/gems/pg-0.19.0/sample/issue-119.rb +94 -0
  98. data/test/dummy/vendor/bundle/ruby/2.3.0/gems/pg-0.19.0/sample/losample.rb +69 -0
  99. data/test/dummy/vendor/bundle/ruby/2.3.0/gems/pg-0.19.0/sample/minimal-testcase.rb +17 -0
  100. data/test/dummy/vendor/bundle/ruby/2.3.0/gems/pg-0.19.0/sample/notify_wait.rb +72 -0
  101. data/test/dummy/vendor/bundle/ruby/2.3.0/gems/pg-0.19.0/sample/pg_statistics.rb +294 -0
  102. data/test/dummy/vendor/bundle/ruby/2.3.0/gems/pg-0.19.0/sample/replication_monitor.rb +231 -0
  103. data/test/dummy/vendor/bundle/ruby/2.3.0/gems/pg-0.19.0/sample/test_binary_values.rb +33 -0
  104. data/test/dummy/vendor/bundle/ruby/2.3.0/gems/pg-0.19.0/sample/wal_shipper.rb +434 -0
  105. data/test/dummy/vendor/bundle/ruby/2.3.0/gems/pg-0.19.0/sample/warehouse_partitions.rb +320 -0
  106. data/test/dummy/vendor/bundle/ruby/2.3.0/gems/pg-0.19.0/spec/data/expected_trace.out +26 -0
  107. data/test/dummy/vendor/bundle/ruby/2.3.0/gems/pg-0.19.0/spec/data/random_binary_data +0 -0
  108. data/test/dummy/vendor/bundle/ruby/2.3.0/gems/pg-0.19.0/spec/helpers.rb +352 -0
  109. data/test/dummy/vendor/bundle/ruby/2.3.0/gems/pg-0.19.0/spec/pg/basic_type_mapping_spec.rb +305 -0
  110. data/test/dummy/vendor/bundle/ruby/2.3.0/gems/pg-0.19.0/spec/pg/connection_spec.rb +1676 -0
  111. data/test/dummy/vendor/bundle/ruby/2.3.0/gems/pg-0.19.0/spec/pg/result_spec.rb +449 -0
  112. data/test/dummy/vendor/bundle/ruby/2.3.0/gems/pg-0.19.0/spec/pg/type_map_by_class_spec.rb +138 -0
  113. data/test/dummy/vendor/bundle/ruby/2.3.0/gems/pg-0.19.0/spec/pg/type_map_by_column_spec.rb +222 -0
  114. data/test/dummy/vendor/bundle/ruby/2.3.0/gems/pg-0.19.0/spec/pg/type_map_by_mri_type_spec.rb +136 -0
  115. data/test/dummy/vendor/bundle/ruby/2.3.0/gems/pg-0.19.0/spec/pg/type_map_by_oid_spec.rb +149 -0
  116. data/test/dummy/vendor/bundle/ruby/2.3.0/gems/pg-0.19.0/spec/pg/type_map_in_ruby_spec.rb +164 -0
  117. data/test/dummy/vendor/bundle/ruby/2.3.0/gems/pg-0.19.0/spec/pg/type_map_spec.rb +22 -0
  118. data/test/dummy/vendor/bundle/ruby/2.3.0/gems/pg-0.19.0/spec/pg/type_spec.rb +777 -0
  119. data/test/dummy/vendor/bundle/ruby/2.3.0/gems/pg-0.19.0/spec/pg_spec.rb +50 -0
  120. data/test/dummy/vendor/bundle/ruby/2.3.0/gems/pq-0.0.1/Gemfile +4 -0
  121. data/test/dummy/vendor/bundle/ruby/2.3.0/gems/pq-0.0.1/LICENSE.txt +22 -0
  122. data/test/dummy/vendor/bundle/ruby/2.3.0/gems/pq-0.0.1/README.md +76 -0
  123. data/test/dummy/vendor/bundle/ruby/2.3.0/gems/pq-0.0.1/Rakefile +1 -0
  124. data/test/dummy/vendor/bundle/ruby/2.3.0/gems/pq-0.0.1/bin/console +7 -0
  125. data/test/dummy/vendor/bundle/ruby/2.3.0/gems/pq-0.0.1/lib/pq.rb +99 -0
  126. data/test/dummy/vendor/bundle/ruby/2.3.0/gems/pq-0.0.1/pq.gemspec +29 -0
  127. data/test/dummy/vendor/bundle/ruby/2.3.0/gems/pq-0.0.1/spec/helpers.rb +10 -0
  128. data/test/dummy/vendor/bundle/ruby/2.3.0/gems/pq-0.0.1/spec/queue_spec.rb +84 -0
  129. data/test/dummy/vendor/bundle/ruby/2.3.0/specifications/pg-0.19.0.gemspec +63 -0
  130. data/test/dummy/vendor/bundle/ruby/2.3.0/specifications/pq-0.0.1.gemspec +49 -0
  131. metadata +253 -1
@@ -0,0 +1,421 @@
1
+ /*
2
+ * pg_text_decoder.c - PG::TextDecoder module
3
+ * $Id: pg_text_decoder.c,v fcf731d3dff7 2015/09/08 12:25:06 jfali $
4
+ *
5
+ */
6
+
7
+ /*
8
+ *
9
+ * Type casts for decoding PostgreSQL string representations to Ruby objects.
10
+ *
11
+ * Decoder classes are defined with pg_define_coder(). This creates a new coder class and
12
+ * assigns a decoder function.
13
+ *
14
+ * Signature of all type cast decoders is:
15
+ * VALUE decoder_function(t_pg_coder *this, char *val, int len, int tuple, int field, int enc_idx)
16
+ *
17
+ * Params:
18
+ * this - The data part of the coder object that belongs to the decoder function.
19
+ * val, len - The text or binary data to decode. The caller ensures, that the data is
20
+ * zero terminated ( that is val[len] = 0 ). The memory should be used read
21
+ * only by the callee.
22
+ * tuple - Row of the value within the result set.
23
+ * field - Column of the value within the result set.
24
+ * enc_idx - Index of the Encoding that any output String should get assigned.
25
+ *
26
+ * Returns:
27
+ * The type casted Ruby object.
28
+ *
29
+ */
30
+
31
+ #include "pg.h"
32
+ #include "util.h"
33
+ #ifdef HAVE_INTTYPES_H
34
+ #include <inttypes.h>
35
+ #endif
36
+
37
+ VALUE rb_mPG_TextDecoder;
38
+ static ID s_id_decode;
39
+
40
+
41
+ /*
42
+ * Document-class: PG::TextDecoder::Boolean < PG::SimpleDecoder
43
+ *
44
+ * This is a decoder class for conversion of PostgreSQL boolean type
45
+ * to Ruby true or false values.
46
+ *
47
+ */
48
+ static VALUE
49
+ pg_text_dec_boolean(t_pg_coder *conv, char *val, int len, int tuple, int field, int enc_idx)
50
+ {
51
+ if (len < 1) {
52
+ rb_raise( rb_eTypeError, "wrong data for text boolean converter in tuple %d field %d", tuple, field);
53
+ }
54
+ return *val == 't' ? Qtrue : Qfalse;
55
+ }
56
+
57
+ /*
58
+ * Document-class: PG::TextDecoder::String < PG::SimpleDecoder
59
+ *
60
+ * This is a decoder class for conversion of PostgreSQL text output to
61
+ * to Ruby String object. The output value will have the character encoding
62
+ * set with PG::Connection#internal_encoding= .
63
+ *
64
+ */
65
+ VALUE
66
+ pg_text_dec_string(t_pg_coder *conv, char *val, int len, int tuple, int field, int enc_idx)
67
+ {
68
+ VALUE ret = rb_tainted_str_new( val, len );
69
+ PG_ENCODING_SET_NOCHECK( ret, enc_idx );
70
+ return ret;
71
+ }
72
+
73
+ /*
74
+ * Document-class: PG::TextDecoder::Integer < PG::SimpleDecoder
75
+ *
76
+ * This is a decoder class for conversion of PostgreSQL integer types
77
+ * to Ruby Integer objects.
78
+ *
79
+ */
80
+ static VALUE
81
+ pg_text_dec_integer(t_pg_coder *conv, char *val, int len, int tuple, int field, int enc_idx)
82
+ {
83
+ long i;
84
+ int max_len;
85
+
86
+ if( sizeof(i) >= 8 && FIXNUM_MAX >= 1000000000000000000LL ){
87
+ /* 64 bit system can safely handle all numbers up to 18 digits as Fixnum */
88
+ max_len = 18;
89
+ } else if( sizeof(i) >= 4 && FIXNUM_MAX >= 1000000000LL ){
90
+ /* 32 bit system can safely handle all numbers up to 9 digits as Fixnum */
91
+ max_len = 9;
92
+ } else {
93
+ /* unknown -> don't use fast path for int conversion */
94
+ max_len = 0;
95
+ }
96
+
97
+ if( len <= max_len ){
98
+ /* rb_cstr2inum() seems to be slow, so we do the int conversion by hand.
99
+ * This proved to be 40% faster by the following benchmark:
100
+ *
101
+ * conn.type_mapping_for_results = PG::BasicTypeMapForResults.new conn
102
+ * Benchmark.measure do
103
+ * conn.exec("select generate_series(1,1000000)").values }
104
+ * end
105
+ */
106
+ char *val_pos = val;
107
+ char digit = *val_pos;
108
+ int neg;
109
+ int error = 0;
110
+
111
+ if( digit=='-' ){
112
+ neg = 1;
113
+ i = 0;
114
+ }else if( digit>='0' && digit<='9' ){
115
+ neg = 0;
116
+ i = digit - '0';
117
+ } else {
118
+ error = 1;
119
+ }
120
+
121
+ while (!error && (digit=*++val_pos)) {
122
+ if( digit>='0' && digit<='9' ){
123
+ i = i * 10 + (digit - '0');
124
+ } else {
125
+ error = 1;
126
+ }
127
+ }
128
+
129
+ if( !error ){
130
+ return LONG2FIX(neg ? -i : i);
131
+ }
132
+ }
133
+ /* Fallback to ruby method if number too big or unrecognized. */
134
+ return rb_cstr2inum(val, 10);
135
+ }
136
+
137
+ /*
138
+ * Document-class: PG::TextDecoder::Float < PG::SimpleDecoder
139
+ *
140
+ * This is a decoder class for conversion of PostgreSQL float4 and float8 types
141
+ * to Ruby Float objects.
142
+ *
143
+ */
144
+ static VALUE
145
+ pg_text_dec_float(t_pg_coder *conv, char *val, int len, int tuple, int field, int enc_idx)
146
+ {
147
+ return rb_float_new(strtod(val, NULL));
148
+ }
149
+
150
+ /*
151
+ * Document-class: PG::TextDecoder::Bytea < PG::SimpleDecoder
152
+ *
153
+ * This is a decoder class for conversion of PostgreSQL bytea type
154
+ * to binary String objects.
155
+ *
156
+ */
157
+ static VALUE
158
+ pg_text_dec_bytea(t_pg_coder *conv, char *val, int len, int tuple, int field, int enc_idx)
159
+ {
160
+ unsigned char *to;
161
+ size_t to_len;
162
+ VALUE ret;
163
+
164
+ to = PQunescapeBytea( (unsigned char *)val, &to_len);
165
+
166
+ ret = rb_tainted_str_new((char*)to, to_len);
167
+ PQfreemem(to);
168
+
169
+ return ret;
170
+ }
171
+
172
+ /*
173
+ * Array parser functions are thankfully borrowed from here:
174
+ * https://github.com/dockyard/pg_array_parser
175
+ */
176
+ static VALUE
177
+ read_array(t_pg_composite_coder *this, int *index, char *c_pg_array_string, int array_string_length, char *word, int enc_idx, int tuple, int field, t_pg_coder_dec_func dec_func)
178
+ {
179
+ /* Return value: array */
180
+ VALUE array;
181
+ int word_index = 0;
182
+
183
+ /* The current character in the input string. */
184
+ char c;
185
+
186
+ /* 0: Currently outside a quoted string, current word never quoted
187
+ * 1: Currently inside a quoted string
188
+ * -1: Currently outside a quoted string, current word previously quoted */
189
+ int openQuote = 0;
190
+
191
+ /* Inside quoted input means the next character should be treated literally,
192
+ * instead of being treated as a metacharacter.
193
+ * Outside of quoted input, means that the word shouldn't be pushed to the array,
194
+ * used when the last entry was a subarray (which adds to the array itself). */
195
+ int escapeNext = 0;
196
+
197
+ array = rb_ary_new();
198
+
199
+ /* Special case the empty array, so it doesn't need to be handled manually inside
200
+ * the loop. */
201
+ if(((*index) < array_string_length) && c_pg_array_string[(*index)] == '}')
202
+ {
203
+ return array;
204
+ }
205
+
206
+ for(;(*index) < array_string_length; ++(*index))
207
+ {
208
+ c = c_pg_array_string[*index];
209
+ if(openQuote < 1)
210
+ {
211
+ if(c == this->delimiter || c == '}')
212
+ {
213
+ if(!escapeNext)
214
+ {
215
+ if(openQuote == 0 && word_index == 4 && !strncmp(word, "NULL", word_index))
216
+ {
217
+ rb_ary_push(array, Qnil);
218
+ }
219
+ else
220
+ {
221
+ VALUE val;
222
+ word[word_index] = 0;
223
+ val = dec_func(this->elem, word, word_index, tuple, field, enc_idx);
224
+ rb_ary_push(array, val);
225
+ }
226
+ }
227
+ if(c == '}')
228
+ {
229
+ return array;
230
+ }
231
+ escapeNext = 0;
232
+ openQuote = 0;
233
+ word_index = 0;
234
+ }
235
+ else if(c == '"')
236
+ {
237
+ openQuote = 1;
238
+ }
239
+ else if(c == '{')
240
+ {
241
+ (*index)++;
242
+ rb_ary_push(array, read_array(this, index, c_pg_array_string, array_string_length, word, enc_idx, tuple, field, dec_func));
243
+ escapeNext = 1;
244
+ }
245
+ else
246
+ {
247
+ word[word_index] = c;
248
+ word_index++;
249
+ }
250
+ }
251
+ else if (escapeNext) {
252
+ word[word_index] = c;
253
+ word_index++;
254
+ escapeNext = 0;
255
+ }
256
+ else if (c == '\\')
257
+ {
258
+ escapeNext = 1;
259
+ }
260
+ else if (c == '"')
261
+ {
262
+ openQuote = -1;
263
+ }
264
+ else
265
+ {
266
+ word[word_index] = c;
267
+ word_index++;
268
+ }
269
+ }
270
+
271
+ return array;
272
+ }
273
+
274
+ /*
275
+ * Document-class: PG::TextDecoder::Array < PG::CompositeDecoder
276
+ *
277
+ * This is the decoder class for PostgreSQL array types.
278
+ *
279
+ * All values are decoded according to the #elements_type
280
+ * accessor. Sub-arrays are decoded recursively.
281
+ *
282
+ */
283
+ static VALUE
284
+ pg_text_dec_array(t_pg_coder *conv, char *val, int len, int tuple, int field, int enc_idx)
285
+ {
286
+ t_pg_composite_coder *this = (t_pg_composite_coder *)conv;
287
+ t_pg_coder_dec_func dec_func = pg_coder_dec_func(this->elem, 0);
288
+ /* create a buffer of the same length, as that will be the worst case */
289
+ char *word = xmalloc(len + 1);
290
+ int index = 1;
291
+
292
+ VALUE return_value = read_array(this, &index, val, len, word, enc_idx, tuple, field, dec_func);
293
+ free(word);
294
+ return return_value;
295
+ }
296
+
297
+ /*
298
+ * Document-class: PG::TextDecoder::Identifier < PG::SimpleDecoder
299
+ *
300
+ * This is the decoder class for PostgreSQL identifiers.
301
+ *
302
+ * Returns an Array of identifiers:
303
+ * PG::TextDecoder::Identifier.new.decode('schema."table"."column"')
304
+ * => ["schema", "table", "column"]
305
+ *
306
+ */
307
+ static VALUE
308
+ pg_text_dec_identifier(t_pg_coder *conv, char *val, int len, int tuple, int field, int enc_idx)
309
+ {
310
+ /* Return value: array */
311
+ VALUE array;
312
+ VALUE elem;
313
+ int word_index = 0;
314
+ int index;
315
+ /* Use a buffer of the same length, as that will be the worst case */
316
+ PG_VARIABLE_LENGTH_ARRAY(char, word, len + 1, NAMEDATALEN)
317
+
318
+ /* The current character in the input string. */
319
+ char c;
320
+
321
+ /* 0: Currently outside a quoted string
322
+ * 1: Currently inside a quoted string, last char was a quote
323
+ * 2: Currently inside a quoted string, last char was no quote */
324
+ int openQuote = 0;
325
+
326
+ array = rb_ary_new();
327
+
328
+ for(index = 0; index < len; ++index) {
329
+ c = val[index];
330
+ if(c == '.' && openQuote < 2 ) {
331
+ word[word_index] = 0;
332
+
333
+ elem = pg_text_dec_string(conv, word, word_index, tuple, field, enc_idx);
334
+ rb_ary_push(array, elem);
335
+
336
+ openQuote = 0;
337
+ word_index = 0;
338
+ } else if(c == '"') {
339
+ if (openQuote == 1) {
340
+ word[word_index] = c;
341
+ word_index++;
342
+ openQuote = 2;
343
+ } else if (openQuote == 2){
344
+ openQuote = 1;
345
+ } else {
346
+ openQuote = 2;
347
+ }
348
+ } else {
349
+ word[word_index] = c;
350
+ word_index++;
351
+ }
352
+ }
353
+
354
+ word[word_index] = 0;
355
+ elem = pg_text_dec_string(conv, word, word_index, tuple, field, enc_idx);
356
+ rb_ary_push(array, elem);
357
+
358
+ return array;
359
+ }
360
+
361
+ /*
362
+ * Document-class: PG::TextDecoder::FromBase64 < PG::CompositeDecoder
363
+ *
364
+ * This is a decoder class for conversion of base64 encoded data
365
+ * to it's binary representation. It outputs a binary Ruby String
366
+ * or some other Ruby object, if a #elements_type decoder was defined.
367
+ *
368
+ */
369
+ static VALUE
370
+ pg_text_dec_from_base64(t_pg_coder *conv, char *val, int len, int tuple, int field, int enc_idx)
371
+ {
372
+ t_pg_composite_coder *this = (t_pg_composite_coder *)conv;
373
+ t_pg_coder_dec_func dec_func = pg_coder_dec_func(this->elem, this->comp.format);
374
+ int decoded_len;
375
+ /* create a buffer of the expected decoded length */
376
+ VALUE out_value = rb_tainted_str_new(NULL, BASE64_DECODED_SIZE(len));
377
+
378
+ decoded_len = base64_decode( RSTRING_PTR(out_value), val, len );
379
+ rb_str_set_len(out_value, decoded_len);
380
+
381
+ /* Is it a pure String conversion? Then we can directly send out_value to the user. */
382
+ if( this->comp.format == 0 && dec_func == pg_text_dec_string ){
383
+ PG_ENCODING_SET_NOCHECK( out_value, enc_idx );
384
+ return out_value;
385
+ }
386
+ if( this->comp.format == 1 && dec_func == pg_bin_dec_bytea ){
387
+ PG_ENCODING_SET_NOCHECK( out_value, rb_ascii8bit_encindex() );
388
+ return out_value;
389
+ }
390
+ out_value = dec_func(this->elem, RSTRING_PTR(out_value), decoded_len, tuple, field, enc_idx);
391
+
392
+ return out_value;
393
+ }
394
+
395
+ void
396
+ init_pg_text_decoder()
397
+ {
398
+ s_id_decode = rb_intern("decode");
399
+
400
+ /* This module encapsulates all decoder classes with text input format */
401
+ rb_mPG_TextDecoder = rb_define_module_under( rb_mPG, "TextDecoder" );
402
+
403
+ /* Make RDoc aware of the decoder classes... */
404
+ /* dummy = rb_define_class_under( rb_mPG_TextDecoder, "Boolean", rb_cPG_SimpleDecoder ); */
405
+ pg_define_coder( "Boolean", pg_text_dec_boolean, rb_cPG_SimpleDecoder, rb_mPG_TextDecoder );
406
+ /* dummy = rb_define_class_under( rb_mPG_TextDecoder, "Integer", rb_cPG_SimpleDecoder ); */
407
+ pg_define_coder( "Integer", pg_text_dec_integer, rb_cPG_SimpleDecoder, rb_mPG_TextDecoder );
408
+ /* dummy = rb_define_class_under( rb_mPG_TextDecoder, "Float", rb_cPG_SimpleDecoder ); */
409
+ pg_define_coder( "Float", pg_text_dec_float, rb_cPG_SimpleDecoder, rb_mPG_TextDecoder );
410
+ /* dummy = rb_define_class_under( rb_mPG_TextDecoder, "String", rb_cPG_SimpleDecoder ); */
411
+ pg_define_coder( "String", pg_text_dec_string, rb_cPG_SimpleDecoder, rb_mPG_TextDecoder );
412
+ /* dummy = rb_define_class_under( rb_mPG_TextDecoder, "Bytea", rb_cPG_SimpleDecoder ); */
413
+ pg_define_coder( "Bytea", pg_text_dec_bytea, rb_cPG_SimpleDecoder, rb_mPG_TextDecoder );
414
+ /* dummy = rb_define_class_under( rb_mPG_TextDecoder, "Identifier", rb_cPG_SimpleDecoder ); */
415
+ pg_define_coder( "Identifier", pg_text_dec_identifier, rb_cPG_SimpleDecoder, rb_mPG_TextDecoder );
416
+
417
+ /* dummy = rb_define_class_under( rb_mPG_TextDecoder, "Array", rb_cPG_CompositeDecoder ); */
418
+ pg_define_coder( "Array", pg_text_dec_array, rb_cPG_CompositeDecoder, rb_mPG_TextDecoder );
419
+ /* dummy = rb_define_class_under( rb_mPG_TextDecoder, "FromBase64", rb_cPG_CompositeDecoder ); */
420
+ pg_define_coder( "FromBase64", pg_text_dec_from_base64, rb_cPG_CompositeDecoder, rb_mPG_TextDecoder );
421
+ }
@@ -0,0 +1,683 @@
1
+ /*
2
+ * pg_text_encoder.c - PG::TextEncoder module
3
+ * $Id: pg_text_encoder.c,v e61a06f1f5ed 2015/12/25 21:14:21 lars $
4
+ *
5
+ */
6
+
7
+ /*
8
+ *
9
+ * Type casts for encoding Ruby objects to PostgreSQL string representations.
10
+ *
11
+ * Encoder classes are defined with pg_define_coder(). This creates a new coder class and
12
+ * assigns an encoder function. The encoder function can decide between two different options
13
+ * to return the encoded data. It can either return it as a Ruby String object or write the
14
+ * encoded data to a memory space provided by the caller. In the second case, the encoder
15
+ * function is called twice, once for deciding the encoding option and returning the expected
16
+ * data length, and a second time when the requested memory space was made available by the
17
+ * calling function, to do the actual conversion and writing. Parameter intermediate can be
18
+ * used to store data between these two calls.
19
+ *
20
+ * Signature of all type cast encoders is:
21
+ * int encoder_function(t_pg_coder *this, VALUE value, char *out, VALUE *intermediate)
22
+ *
23
+ * Params:
24
+ * this - The data part of the coder object that belongs to the encoder function.
25
+ * value - The Ruby object to cast.
26
+ * out - NULL for the first call,
27
+ * pointer to a buffer with the requested size for the second call.
28
+ * intermediate - Pointer to a VALUE that might be set by the encoding function to some
29
+ * value in the first call that can be retrieved later in the second call.
30
+ * This VALUE is not yet initialized by the caller.
31
+ * enc_idx - Index of the output Encoding that strings should be converted to.
32
+ *
33
+ * Returns:
34
+ * >= 0 - If out==NULL the encoder function must return the expected output buffer size.
35
+ * This can be larger than the size of the second call, but may not be smaller.
36
+ * If out!=NULL the encoder function must return the actually used output buffer size
37
+ * without a termination character.
38
+ * -1 - The encoder function can alternatively return -1 to indicate that no second call
39
+ * is required, but the String value in *intermediate should be used instead.
40
+ */
41
+
42
+
43
+ #include "pg.h"
44
+ #include "util.h"
45
+ #ifdef HAVE_INTTYPES_H
46
+ #include <inttypes.h>
47
+ #endif
48
+ #include <math.h>
49
+
50
+ VALUE rb_mPG_TextEncoder;
51
+ static ID s_id_encode;
52
+ static ID s_id_to_i;
53
+
54
+ static int pg_text_enc_integer(t_pg_coder *this, VALUE value, char *out, VALUE *intermediate, int enc_idx);
55
+
56
+ VALUE
57
+ pg_obj_to_i( VALUE value )
58
+ {
59
+ switch (TYPE(value)) {
60
+ case T_FIXNUM:
61
+ case T_FLOAT:
62
+ case T_BIGNUM:
63
+ return value;
64
+ default:
65
+ return rb_funcall(value, s_id_to_i, 0);
66
+ }
67
+ }
68
+
69
+ /*
70
+ * Document-class: PG::TextEncoder::Boolean < PG::SimpleEncoder
71
+ *
72
+ * This is the encoder class for the PostgreSQL bool type.
73
+ *
74
+ * Ruby value false is encoded as SQL +FALSE+ value.
75
+ * Ruby value true is encoded as SQL +TRUE+ value.
76
+ * Any other value is sent as it's string representation.
77
+ *
78
+ */
79
+ static int
80
+ pg_text_enc_boolean(t_pg_coder *this, VALUE value, char *out, VALUE *intermediate, int enc_idx)
81
+ {
82
+ switch( TYPE(value) ){
83
+ case T_FALSE:
84
+ if(out) *out = 'f';
85
+ return 1;
86
+ case T_TRUE:
87
+ if(out) *out = 't';
88
+ return 1;
89
+ case T_FIXNUM:
90
+ case T_BIGNUM:
91
+ if( NUM2LONG(value) == 0 ){
92
+ if(out) *out = '0';
93
+ return 1;
94
+ } else if( NUM2LONG(value) == 1 ){
95
+ if(out) *out = '1';
96
+ return 1;
97
+ } else {
98
+ return pg_text_enc_integer(this, value, out, intermediate, enc_idx);
99
+ }
100
+ default:
101
+ return pg_coder_enc_to_s(this, value, out, intermediate, enc_idx);
102
+ }
103
+ /* never reached */
104
+ return 0;
105
+ }
106
+
107
+
108
+ /*
109
+ * Document-class: PG::TextEncoder::String < PG::SimpleEncoder
110
+ *
111
+ * This is the encoder class for the PostgreSQL text types.
112
+ *
113
+ * Non-String values are expected to have method +to_s+ defined.
114
+ *
115
+ */
116
+ int
117
+ pg_coder_enc_to_s(t_pg_coder *this, VALUE value, char *out, VALUE *intermediate, int enc_idx)
118
+ {
119
+ VALUE str = rb_obj_as_string(value);
120
+ if( ENCODING_GET(str) == enc_idx ){
121
+ *intermediate = str;
122
+ }else{
123
+ *intermediate = rb_str_export_to_enc(str, rb_enc_from_index(enc_idx));
124
+ }
125
+ return -1;
126
+ }
127
+
128
+
129
+ /*
130
+ * Document-class: PG::TextEncoder::Integer < PG::SimpleEncoder
131
+ *
132
+ * This is the encoder class for the PostgreSQL int types.
133
+ *
134
+ * Non-Integer values are expected to have method +to_i+ defined.
135
+ *
136
+ */
137
+ static int
138
+ pg_text_enc_integer(t_pg_coder *this, VALUE value, char *out, VALUE *intermediate, int enc_idx)
139
+ {
140
+ if(out){
141
+ if(TYPE(*intermediate) == T_STRING){
142
+ return pg_coder_enc_to_s(this, value, out, intermediate, enc_idx);
143
+ }else{
144
+ char *start = out;
145
+ int len;
146
+ int neg = 0;
147
+ long long ll = NUM2LL(*intermediate);
148
+
149
+ if (ll < 0) {
150
+ /* We don't expect problems with the most negative integer not being representable
151
+ * as a positive integer, because Fixnum is only up to 63 bits.
152
+ */
153
+ ll = -ll;
154
+ neg = 1;
155
+ }
156
+
157
+ /* Compute the result string backwards. */
158
+ do {
159
+ long long remainder;
160
+ long long oldval = ll;
161
+
162
+ ll /= 10;
163
+ remainder = oldval - ll * 10;
164
+ *out++ = '0' + remainder;
165
+ } while (ll != 0);
166
+
167
+ if (neg)
168
+ *out++ = '-';
169
+
170
+ len = out - start;
171
+
172
+ /* Reverse string. */
173
+ out--;
174
+ while (start < out)
175
+ {
176
+ char swap = *start;
177
+
178
+ *start++ = *out;
179
+ *out-- = swap;
180
+ }
181
+
182
+ return len;
183
+ }
184
+ }else{
185
+ *intermediate = pg_obj_to_i(value);
186
+ if(TYPE(*intermediate) == T_FIXNUM){
187
+ int len;
188
+ long long sll = NUM2LL(*intermediate);
189
+ long long ll = sll < 0 ? -sll : sll;
190
+ if( ll < 100000000 ){
191
+ if( ll < 10000 ){
192
+ if( ll < 100 ){
193
+ len = ll < 10 ? 1 : 2;
194
+ }else{
195
+ len = ll < 1000 ? 3 : 4;
196
+ }
197
+ }else{
198
+ if( ll < 1000000 ){
199
+ len = ll < 100000 ? 5 : 6;
200
+ }else{
201
+ len = ll < 10000000 ? 7 : 8;
202
+ }
203
+ }
204
+ }else{
205
+ if( ll < 1000000000000LL ){
206
+ if( ll < 10000000000LL ){
207
+ len = ll < 1000000000LL ? 9 : 10;
208
+ }else{
209
+ len = ll < 100000000000LL ? 11 : 12;
210
+ }
211
+ }else{
212
+ if( ll < 100000000000000LL ){
213
+ len = ll < 10000000000000LL ? 13 : 14;
214
+ }else{
215
+ return pg_coder_enc_to_s(this, *intermediate, NULL, intermediate, enc_idx);
216
+ }
217
+ }
218
+ }
219
+ return sll < 0 ? len+1 : len;
220
+ }else{
221
+ return pg_coder_enc_to_s(this, *intermediate, NULL, intermediate, enc_idx);
222
+ }
223
+ }
224
+ }
225
+
226
+
227
+ /*
228
+ * Document-class: PG::TextEncoder::Float < PG::SimpleEncoder
229
+ *
230
+ * This is the encoder class for the PostgreSQL float types.
231
+ *
232
+ */
233
+ static int
234
+ pg_text_enc_float(t_pg_coder *conv, VALUE value, char *out, VALUE *intermediate, int enc_idx)
235
+ {
236
+ if(out){
237
+ double dvalue = NUM2DBL(value);
238
+ /* Cast to the same strings as value.to_s . */
239
+ if( isinf(dvalue) ){
240
+ if( dvalue < 0 ){
241
+ memcpy( out, "-Infinity", 9);
242
+ return 9;
243
+ } else {
244
+ memcpy( out, "Infinity", 8);
245
+ return 8;
246
+ }
247
+ } else if (isnan(dvalue)) {
248
+ memcpy( out, "NaN", 3);
249
+ return 3;
250
+ }
251
+ return sprintf( out, "%.16E", dvalue);
252
+ }else{
253
+ return 23;
254
+ }
255
+ }
256
+
257
+ static const char hextab[] = {
258
+ '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'
259
+ };
260
+
261
+ /*
262
+ * Document-class: PG::TextEncoder::Bytea < PG::SimpleEncoder
263
+ *
264
+ * This is an encoder class for the PostgreSQL bytea type for server version 9.0
265
+ * or newer.
266
+ *
267
+ * The binary String is converted to hexadecimal representation for transmission
268
+ * in text format. For query bind parameters it is recommended to use
269
+ * PG::BinaryEncoder::Bytea instead, in order to decrease network traffic and
270
+ * CPU usage.
271
+ *
272
+ */
273
+ static int
274
+ pg_text_enc_bytea(t_pg_coder *conv, VALUE value, char *out, VALUE *intermediate, int enc_idx)
275
+ {
276
+ if(out){
277
+ size_t strlen = RSTRING_LEN(*intermediate);
278
+ char *iptr = RSTRING_PTR(*intermediate);
279
+ char *eptr = iptr + strlen;
280
+ char *optr = out;
281
+ *optr++ = '\\';
282
+ *optr++ = 'x';
283
+
284
+ for( ; iptr < eptr; iptr++ ){
285
+ unsigned char c = *iptr;
286
+ *optr++ = hextab[c >> 4];
287
+ *optr++ = hextab[c & 0xf];
288
+ }
289
+ return optr - out;
290
+ }else{
291
+ *intermediate = rb_obj_as_string(value);
292
+ /* The output starts with "\x" and each character is converted to hex. */
293
+ return 2 + RSTRING_LEN(*intermediate) * 2;
294
+ }
295
+ }
296
+
297
+ typedef int (*t_quote_func)( void *_this, char *p_in, int strlen, char *p_out );
298
+
299
+ static int
300
+ quote_array_buffer( void *_this, char *p_in, int strlen, char *p_out ){
301
+ t_pg_composite_coder *this = _this;
302
+ char *ptr1;
303
+ char *ptr2;
304
+ int backslashs = 0;
305
+ int needquote;
306
+
307
+ /* count data plus backslashes; detect chars needing quotes */
308
+ if (strlen == 0)
309
+ needquote = 1; /* force quotes for empty string */
310
+ else if (strlen == 4 && rbpg_strncasecmp(p_in, "NULL", strlen) == 0)
311
+ needquote = 1; /* force quotes for literal NULL */
312
+ else
313
+ needquote = 0;
314
+
315
+ /* count required backlashs */
316
+ for(ptr1 = p_in; ptr1 != p_in + strlen; ptr1++) {
317
+ char ch = *ptr1;
318
+
319
+ if (ch == '"' || ch == '\\'){
320
+ needquote = 1;
321
+ backslashs++;
322
+ } else if (ch == '{' || ch == '}' || ch == this->delimiter ||
323
+ ch == ' ' || ch == '\t' || ch == '\n' || ch == '\r' || ch == '\v' || ch == '\f'){
324
+ needquote = 1;
325
+ }
326
+ }
327
+
328
+ if( needquote ){
329
+ ptr1 = p_in + strlen;
330
+ ptr2 = p_out + strlen + backslashs + 2;
331
+ /* Write end quote */
332
+ *--ptr2 = '"';
333
+
334
+ /* Then store the escaped string on the final position, walking
335
+ * right to left, until all backslashs are placed. */
336
+ while( ptr1 != p_in ) {
337
+ *--ptr2 = *--ptr1;
338
+ if(*ptr2 == '"' || *ptr2 == '\\'){
339
+ *--ptr2 = '\\';
340
+ }
341
+ }
342
+ /* Write start quote */
343
+ *p_out = '"';
344
+ return strlen + backslashs + 2;
345
+ } else {
346
+ if( p_in != p_out )
347
+ memcpy( p_out, p_in, strlen );
348
+ return strlen;
349
+ }
350
+ }
351
+
352
+ static char *
353
+ quote_string(t_pg_coder *this, VALUE value, VALUE string, char *current_out, int with_quote, t_quote_func quote_buffer, void *func_data, int enc_idx)
354
+ {
355
+ int strlen;
356
+ VALUE subint;
357
+ t_pg_coder_enc_func enc_func = pg_coder_enc_func(this);
358
+
359
+ strlen = enc_func(this, value, NULL, &subint, enc_idx);
360
+
361
+ if( strlen == -1 ){
362
+ /* we can directly use String value in subint */
363
+ strlen = RSTRING_LENINT(subint);
364
+
365
+ if(with_quote){
366
+ /* size of string assuming the worst case, that every character must be escaped. */
367
+ current_out = pg_rb_str_ensure_capa( string, strlen * 2 + 2, current_out, NULL );
368
+
369
+ current_out += quote_buffer( func_data, RSTRING_PTR(subint), strlen, current_out );
370
+ } else {
371
+ current_out = pg_rb_str_ensure_capa( string, strlen, current_out, NULL );
372
+ memcpy( current_out, RSTRING_PTR(subint), strlen );
373
+ current_out += strlen;
374
+ }
375
+
376
+ } else {
377
+
378
+ if(with_quote){
379
+ /* size of string assuming the worst case, that every character must be escaped
380
+ * plus two bytes for quotation.
381
+ */
382
+ current_out = pg_rb_str_ensure_capa( string, 2 * strlen + 2, current_out, NULL );
383
+
384
+ /* Place the unescaped string at current output position. */
385
+ strlen = enc_func(this, value, current_out, &subint, enc_idx);
386
+
387
+ current_out += quote_buffer( func_data, current_out, strlen, current_out );
388
+ }else{
389
+ /* size of the unquoted string */
390
+ current_out = pg_rb_str_ensure_capa( string, strlen, current_out, NULL );
391
+ current_out += enc_func(this, value, current_out, &subint, enc_idx);
392
+ }
393
+ }
394
+ return current_out;
395
+ }
396
+
397
+ static char *
398
+ write_array(t_pg_composite_coder *this, VALUE value, char *current_out, VALUE string, int quote, int enc_idx)
399
+ {
400
+ int i;
401
+
402
+ /* size of "{}" */
403
+ current_out = pg_rb_str_ensure_capa( string, 2, current_out, NULL );
404
+ *current_out++ = '{';
405
+
406
+ for( i=0; i<RARRAY_LEN(value); i++){
407
+ VALUE entry = rb_ary_entry(value, i);
408
+
409
+ if( i > 0 ){
410
+ current_out = pg_rb_str_ensure_capa( string, 1, current_out, NULL );
411
+ *current_out++ = this->delimiter;
412
+ }
413
+
414
+ switch(TYPE(entry)){
415
+ case T_ARRAY:
416
+ current_out = write_array(this, entry, current_out, string, quote, enc_idx);
417
+ break;
418
+ case T_NIL:
419
+ current_out = pg_rb_str_ensure_capa( string, 4, current_out, NULL );
420
+ *current_out++ = 'N';
421
+ *current_out++ = 'U';
422
+ *current_out++ = 'L';
423
+ *current_out++ = 'L';
424
+ break;
425
+ default:
426
+ current_out = quote_string( this->elem, entry, string, current_out, quote, quote_array_buffer, this, enc_idx );
427
+ }
428
+ }
429
+ current_out = pg_rb_str_ensure_capa( string, 1, current_out, NULL );
430
+ *current_out++ = '}';
431
+ return current_out;
432
+ }
433
+
434
+
435
+ /*
436
+ * Document-class: PG::TextEncoder::Array < PG::CompositeEncoder
437
+ *
438
+ * This is the encoder class for PostgreSQL array types.
439
+ *
440
+ * All values are encoded according to the #elements_type
441
+ * accessor. Sub-arrays are encoded recursively.
442
+ *
443
+ * This encoder expects an Array of values or sub-arrays as input.
444
+ * Other values are passed through as text without interpretation.
445
+ *
446
+ */
447
+ static int
448
+ pg_text_enc_array(t_pg_coder *conv, VALUE value, char *out, VALUE *intermediate, int enc_idx)
449
+ {
450
+ char *end_ptr;
451
+ t_pg_composite_coder *this = (t_pg_composite_coder *)conv;
452
+
453
+ if( TYPE(value) == T_ARRAY){
454
+ VALUE out_str = rb_str_new(NULL, 0);
455
+ PG_ENCODING_SET_NOCHECK(out_str, enc_idx);
456
+
457
+ end_ptr = write_array(this, value, RSTRING_PTR(out_str), out_str, this->needs_quotation, enc_idx);
458
+
459
+ rb_str_set_len( out_str, end_ptr - RSTRING_PTR(out_str) );
460
+ *intermediate = out_str;
461
+
462
+ return -1;
463
+ } else {
464
+ return pg_coder_enc_to_s( conv, value, out, intermediate, enc_idx );
465
+ }
466
+ }
467
+
468
+ static char *
469
+ quote_identifier( VALUE value, VALUE out_string, char *current_out ){
470
+ char *p_in = RSTRING_PTR(value);
471
+ char *ptr1;
472
+ size_t strlen = RSTRING_LEN(value);
473
+ char *end_capa = current_out;
474
+
475
+ PG_RB_STR_ENSURE_CAPA( out_string, strlen + 2, current_out, end_capa );
476
+ *current_out++ = '"';
477
+ for(ptr1 = p_in; ptr1 != p_in + strlen; ptr1++) {
478
+ char c = *ptr1;
479
+ if (c == '"'){
480
+ strlen++;
481
+ PG_RB_STR_ENSURE_CAPA( out_string, p_in - ptr1 + strlen + 1, current_out, end_capa );
482
+ *current_out++ = '"';
483
+ } else if (c == 0){
484
+ break;
485
+ }
486
+ *current_out++ = c;
487
+ }
488
+ PG_RB_STR_ENSURE_CAPA( out_string, 1, current_out, end_capa );
489
+ *current_out++ = '"';
490
+
491
+ return current_out;
492
+ }
493
+
494
+ static char *
495
+ pg_text_enc_array_identifier(VALUE value, VALUE string, char *out, int enc_idx)
496
+ {
497
+ int i;
498
+ int nr_elems;
499
+
500
+ Check_Type(value, T_ARRAY);
501
+ nr_elems = RARRAY_LEN(value);
502
+
503
+ for( i=0; i<nr_elems; i++){
504
+ VALUE entry = rb_ary_entry(value, i);
505
+
506
+ StringValue(entry);
507
+ if( ENCODING_GET(entry) != enc_idx ){
508
+ entry = rb_str_export_to_enc(entry, rb_enc_from_index(enc_idx));
509
+ }
510
+ out = quote_identifier(entry, string, out);
511
+ if( i < nr_elems-1 ){
512
+ out = pg_rb_str_ensure_capa( string, 1, out, NULL );
513
+ *out++ = '.';
514
+ }
515
+ }
516
+ return out;
517
+ }
518
+
519
+ /*
520
+ * Document-class: PG::TextEncoder::Identifier < PG::SimpleEncoder
521
+ *
522
+ * This is the encoder class for PostgreSQL identifiers.
523
+ *
524
+ * An Array value can be used for "schema.table.column" type identifiers:
525
+ * PG::TextEncoder::Identifier.new.encode(['schema', 'table', 'column'])
526
+ * => '"schema"."table"."column"'
527
+ *
528
+ * This encoder can also be used per PG::Connection#quote_ident .
529
+ */
530
+ int
531
+ pg_text_enc_identifier(t_pg_coder *this, VALUE value, char *out, VALUE *intermediate, int enc_idx)
532
+ {
533
+ VALUE out_str;
534
+ UNUSED( this );
535
+ if( TYPE(value) == T_ARRAY){
536
+ out_str = rb_str_new(NULL, 0);
537
+ out = RSTRING_PTR(out_str);
538
+ out = pg_text_enc_array_identifier(value, out_str, out, enc_idx);
539
+ } else {
540
+ StringValue(value);
541
+ if( ENCODING_GET(value) != enc_idx ){
542
+ value = rb_str_export_to_enc(value, rb_enc_from_index(enc_idx));
543
+ }
544
+ out_str = rb_str_new(NULL, RSTRING_LEN(value) + 2);
545
+ out = RSTRING_PTR(out_str);
546
+ out = quote_identifier(value, out_str, out);
547
+ }
548
+ rb_str_set_len( out_str, out - RSTRING_PTR(out_str) );
549
+ PG_ENCODING_SET_NOCHECK(out_str, enc_idx);
550
+ *intermediate = out_str;
551
+ return -1;
552
+ }
553
+
554
+
555
+ static int
556
+ quote_literal_buffer( void *_this, char *p_in, int strlen, char *p_out ){
557
+ char *ptr1;
558
+ char *ptr2;
559
+ int backslashs = 0;
560
+
561
+ /* count required backlashs */
562
+ for(ptr1 = p_in; ptr1 != p_in + strlen; ptr1++) {
563
+ if (*ptr1 == '\''){
564
+ backslashs++;
565
+ }
566
+ }
567
+
568
+ ptr1 = p_in + strlen;
569
+ ptr2 = p_out + strlen + backslashs + 2;
570
+ /* Write end quote */
571
+ *--ptr2 = '\'';
572
+
573
+ /* Then store the escaped string on the final position, walking
574
+ * right to left, until all backslashs are placed. */
575
+ while( ptr1 != p_in ) {
576
+ *--ptr2 = *--ptr1;
577
+ if(*ptr2 == '\''){
578
+ *--ptr2 = '\'';
579
+ }
580
+ }
581
+ /* Write start quote */
582
+ *p_out = '\'';
583
+ return strlen + backslashs + 2;
584
+ }
585
+
586
+
587
+ /*
588
+ * Document-class: PG::TextEncoder::QuotedLiteral < PG::CompositeEncoder
589
+ *
590
+ * This is the encoder class for PostgreSQL literals.
591
+ *
592
+ * A literal is quoted and escaped by the +'+ character.
593
+ *
594
+ */
595
+ static int
596
+ pg_text_enc_quoted_literal(t_pg_coder *conv, VALUE value, char *out, VALUE *intermediate, int enc_idx)
597
+ {
598
+ t_pg_composite_coder *this = (t_pg_composite_coder *)conv;
599
+ VALUE out_str = rb_str_new(NULL, 0);
600
+ PG_ENCODING_SET_NOCHECK(out_str, enc_idx);
601
+
602
+ out = RSTRING_PTR(out_str);
603
+ out = quote_string(this->elem, value, out_str, out, this->needs_quotation, quote_literal_buffer, this, enc_idx);
604
+ rb_str_set_len( out_str, out - RSTRING_PTR(out_str) );
605
+ *intermediate = out_str;
606
+ return -1;
607
+ }
608
+
609
+ /*
610
+ * Document-class: PG::TextEncoder::ToBase64 < PG::CompositeEncoder
611
+ *
612
+ * This is an encoder class for conversion of binary to base64 data.
613
+ *
614
+ */
615
+ static int
616
+ pg_text_enc_to_base64(t_pg_coder *conv, VALUE value, char *out, VALUE *intermediate, int enc_idx)
617
+ {
618
+ int strlen;
619
+ VALUE subint;
620
+ t_pg_composite_coder *this = (t_pg_composite_coder *)conv;
621
+ t_pg_coder_enc_func enc_func = pg_coder_enc_func(this->elem);
622
+
623
+ if(out){
624
+ /* Second encoder pass, if required */
625
+ strlen = enc_func(this->elem, value, out, intermediate, enc_idx);
626
+ base64_encode( out, out, strlen );
627
+
628
+ return BASE64_ENCODED_SIZE(strlen);
629
+ } else {
630
+ /* First encoder pass */
631
+ strlen = enc_func(this->elem, value, NULL, &subint, enc_idx);
632
+
633
+ if( strlen == -1 ){
634
+ /* Encoded string is returned in subint */
635
+ VALUE out_str;
636
+
637
+ strlen = RSTRING_LENINT(subint);
638
+ out_str = rb_str_new(NULL, BASE64_ENCODED_SIZE(strlen));
639
+ PG_ENCODING_SET_NOCHECK(out_str, enc_idx);
640
+
641
+ base64_encode( RSTRING_PTR(out_str), RSTRING_PTR(subint), strlen);
642
+ *intermediate = out_str;
643
+
644
+ return -1;
645
+ } else {
646
+ *intermediate = subint;
647
+
648
+ return BASE64_ENCODED_SIZE(strlen);
649
+ }
650
+ }
651
+ }
652
+
653
+
654
+ void
655
+ init_pg_text_encoder()
656
+ {
657
+ s_id_encode = rb_intern("encode");
658
+ s_id_to_i = rb_intern("to_i");
659
+
660
+ /* This module encapsulates all encoder classes with text output format */
661
+ rb_mPG_TextEncoder = rb_define_module_under( rb_mPG, "TextEncoder" );
662
+
663
+ /* Make RDoc aware of the encoder classes... */
664
+ /* dummy = rb_define_class_under( rb_mPG_TextEncoder, "Boolean", rb_cPG_SimpleEncoder ); */
665
+ pg_define_coder( "Boolean", pg_text_enc_boolean, rb_cPG_SimpleEncoder, rb_mPG_TextEncoder );
666
+ /* dummy = rb_define_class_under( rb_mPG_TextEncoder, "Integer", rb_cPG_SimpleEncoder ); */
667
+ pg_define_coder( "Integer", pg_text_enc_integer, rb_cPG_SimpleEncoder, rb_mPG_TextEncoder );
668
+ /* dummy = rb_define_class_under( rb_mPG_TextEncoder, "Float", rb_cPG_SimpleEncoder ); */
669
+ pg_define_coder( "Float", pg_text_enc_float, rb_cPG_SimpleEncoder, rb_mPG_TextEncoder );
670
+ /* dummy = rb_define_class_under( rb_mPG_TextEncoder, "String", rb_cPG_SimpleEncoder ); */
671
+ pg_define_coder( "String", pg_coder_enc_to_s, rb_cPG_SimpleEncoder, rb_mPG_TextEncoder );
672
+ /* dummy = rb_define_class_under( rb_mPG_TextEncoder, "Bytea", rb_cPG_SimpleEncoder ); */
673
+ pg_define_coder( "Bytea", pg_text_enc_bytea, rb_cPG_SimpleEncoder, rb_mPG_TextEncoder );
674
+ /* dummy = rb_define_class_under( rb_mPG_TextEncoder, "Identifier", rb_cPG_SimpleEncoder ); */
675
+ pg_define_coder( "Identifier", pg_text_enc_identifier, rb_cPG_SimpleEncoder, rb_mPG_TextEncoder );
676
+
677
+ /* dummy = rb_define_class_under( rb_mPG_TextEncoder, "Array", rb_cPG_CompositeEncoder ); */
678
+ pg_define_coder( "Array", pg_text_enc_array, rb_cPG_CompositeEncoder, rb_mPG_TextEncoder );
679
+ /* dummy = rb_define_class_under( rb_mPG_TextEncoder, "QuotedLiteral", rb_cPG_CompositeEncoder ); */
680
+ pg_define_coder( "QuotedLiteral", pg_text_enc_quoted_literal, rb_cPG_CompositeEncoder, rb_mPG_TextEncoder );
681
+ /* dummy = rb_define_class_under( rb_mPG_TextEncoder, "ToBase64", rb_cPG_CompositeEncoder ); */
682
+ pg_define_coder( "ToBase64", pg_text_enc_to_base64, rb_cPG_CompositeEncoder, rb_mPG_TextEncoder );
683
+ }