sequel_pg 1.4.0 → 1.5.0

Sign up to get free protection for your applications and to get access to all the features.
data/CHANGELOG CHANGED
@@ -1,3 +1,7 @@
1
+ === 1.5.0 (2012-07-02)
2
+
3
+ * Add C-based PostgreSQL array parser, for major speedup in parsing arrays (Dan McClain, jeremyevans)
4
+
1
5
  === 1.4.0 (2012-06-01)
2
6
 
3
7
  * Add support for streaming on PostgreSQL 9.2 using PQsetRowProcessor (jeremyevans)
data/MIT-LICENSE CHANGED
@@ -1,4 +1,4 @@
1
- Copyright (c) 2010-2011 Jeremy Evans
1
+ Copyright (c) 2010-2012 Jeremy Evans
2
2
 
3
3
  Permission is hereby granted, free of charge, to any person obtaining a copy
4
4
  of this software and associated documentation files (the "Software"), to
@@ -17,3 +17,31 @@ THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
17
17
  IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
18
18
  CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
19
19
 
20
+
21
+ The original array parsing code (parse_pg_array, read_array) was taken from
22
+ the pg_array_parser library (https://github.com/dockyard/pg_array_parser)
23
+ and has the following license:
24
+
25
+ Copyright (c) 2012 Dan McClain
26
+
27
+ MIT License
28
+
29
+ Permission is hereby granted, free of charge, to any person obtaining
30
+ a copy of this software and associated documentation files (the
31
+ "Software"), to deal in the Software without restriction, including
32
+ without limitation the rights to use, copy, modify, merge, publish,
33
+ distribute, sublicense, and/or sell copies of the Software, and to
34
+ permit persons to whom the Software is furnished to do so, subject to
35
+ the following conditions:
36
+
37
+ The above copyright notice and this permission notice shall be
38
+ included in all copies or substantial portions of the Software.
39
+
40
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
41
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
42
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
43
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
44
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
45
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
46
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
47
+
@@ -114,6 +114,115 @@ static int enc_get_index(VALUE val)
114
114
  }
115
115
  #endif
116
116
 
117
+ static VALUE read_array(int *index, char *c_pg_array_string, int array_string_length, char *word, VALUE converter)
118
+ {
119
+ int word_index = 0;
120
+
121
+ /* The current character in the input string. */
122
+ char c;
123
+
124
+ /* 0: Currently outside a quoted string, current word never quoted
125
+ * 1: Currently inside a quoted string
126
+ * -1: Currently outside a quoted string, current word previously quoted */
127
+ int openQuote = 0;
128
+
129
+ /* Inside quoted input means the next character should be treated literally,
130
+ * instead of being treated as a metacharacter.
131
+ * Outside of quoted input, means that the word shouldn't be pushed to the array,
132
+ * used when the last entry was a subarray (which adds to the array itself). */
133
+ int escapeNext = 0;
134
+
135
+ VALUE array = rb_ary_new();
136
+
137
+ /* Special case the empty array, so it doesn't need to be handled manually inside
138
+ * the loop. */
139
+ if(((*index) < array_string_length) && c_pg_array_string[(*index)] == '}')
140
+ {
141
+ return array;
142
+ }
143
+
144
+ for(;(*index) < array_string_length; ++(*index))
145
+ {
146
+ c = c_pg_array_string[*index];
147
+ if(openQuote < 1)
148
+ {
149
+ if(c == ',' || c == '}')
150
+ {
151
+ if(!escapeNext)
152
+ {
153
+ if(openQuote == 0 && word_index == 4 && !strncmp(word, "NULL", word_index))
154
+ {
155
+ rb_ary_push(array, Qnil);
156
+ }
157
+ else if (RTEST(converter))
158
+ {
159
+ rb_ary_push(array, rb_funcall(converter, spg_id_call, 1, rb_str_new(word, word_index)));
160
+ }
161
+ else
162
+ {
163
+ rb_ary_push(array, rb_str_new(word, word_index));
164
+ }
165
+ }
166
+ if(c == '}')
167
+ {
168
+ return array;
169
+ }
170
+ escapeNext = 0;
171
+ openQuote = 0;
172
+ word_index = 0;
173
+ }
174
+ else if(c == '"')
175
+ {
176
+ openQuote = 1;
177
+ }
178
+ else if(c == '{')
179
+ {
180
+ (*index)++;
181
+ rb_ary_push(array, read_array(index, c_pg_array_string, array_string_length, word, converter));
182
+ escapeNext = 1;
183
+ }
184
+ else
185
+ {
186
+ word[word_index] = c;
187
+ word_index++;
188
+ }
189
+ }
190
+ else if (escapeNext) {
191
+ word[word_index] = c;
192
+ word_index++;
193
+ escapeNext = 0;
194
+ }
195
+ else if (c == '\\')
196
+ {
197
+ escapeNext = 1;
198
+ }
199
+ else if (c == '"')
200
+ {
201
+ openQuote = -1;
202
+ }
203
+ else
204
+ {
205
+ word[word_index] = c;
206
+ word_index++;
207
+ }
208
+ }
209
+
210
+ return array;
211
+ }
212
+
213
+ static VALUE parse_pg_array(VALUE self, VALUE pg_array_string, VALUE converter) {
214
+
215
+ /* convert to c-string, create additional ruby string buffer of
216
+ * the same length, as that will be the worst case. */
217
+ char *c_pg_array_string = StringValueCStr(pg_array_string);
218
+ int array_string_length = RSTRING_LEN(pg_array_string);
219
+ VALUE buf = rb_str_buf_new(array_string_length);
220
+ char *word = RSTRING_PTR(buf);
221
+ int index = 1;
222
+
223
+ return read_array(&index, c_pg_array_string, array_string_length, word, converter);
224
+ }
225
+
117
226
  static VALUE spg_time(const char *s) {
118
227
  VALUE now;
119
228
  int hour, minute, second, tokens;
@@ -973,5 +1082,7 @@ void Init_sequel_pg(void) {
973
1082
  rb_define_private_method(c, "with_row_processor", spg_with_row_processor, 3);
974
1083
  #endif
975
1084
 
1085
+ rb_define_singleton_method(spg_Postgres, "parse_pg_array", parse_pg_array, 2);
1086
+
976
1087
  rb_require("sequel_pg/sequel_pg");
977
1088
  }
@@ -83,3 +83,17 @@ class Sequel::Postgres::Dataset
83
83
  (rp = row_proc).is_a?(Class) && (rp < Sequel::Model) && optimize_model_load && !opts[:use_cursor] && !opts[:graph]
84
84
  end
85
85
  end
86
+
87
+ if defined?(Sequel::Postgres::PGArray)
88
+ # pg_array extension previously loaded
89
+
90
+ class Sequel::Postgres::PGArray::Creator
91
+ # Override Creator to use sequel_pg's C-based parser instead of the pure ruby parser.
92
+ def call(string)
93
+ Sequel::Postgres::PGArray.new(Sequel::Postgres.parse_pg_array(string, @converter), @type)
94
+ end
95
+ end
96
+
97
+ # Remove the pure-ruby parser, no longer needed.
98
+ Sequel::Postgres::PGArray.send(:remove_const, :Parser)
99
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: sequel_pg
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.4.0
4
+ version: 1.5.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,11 +9,11 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-06-01 00:00:00.000000000 Z
12
+ date: 2012-07-02 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: pg
16
- requirement: &4392569800 !ruby/object:Gem::Requirement
16
+ requirement: &4423931800 !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
19
  - - ! '>='
@@ -21,10 +21,10 @@ dependencies:
21
21
  version: 0.8.0
22
22
  type: :runtime
23
23
  prerelease: false
24
- version_requirements: *4392569800
24
+ version_requirements: *4423931800
25
25
  - !ruby/object:Gem::Dependency
26
26
  name: sequel
27
- requirement: &4392569320 !ruby/object:Gem::Requirement
27
+ requirement: &4423930780 !ruby/object:Gem::Requirement
28
28
  none: false
29
29
  requirements:
30
30
  - - ! '>='
@@ -32,7 +32,7 @@ dependencies:
32
32
  version: 3.36.0
33
33
  type: :runtime
34
34
  prerelease: false
35
- version_requirements: *4392569320
35
+ version_requirements: *4423930780
36
36
  description: ! 'sequel_pg overwrites the inner loop of the Sequel postgres
37
37
 
38
38
  adapter row fetching code with a C version. The C version