swift-db-postgres 0.2.4 → 0.2.5

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.
data/CHANGELOG CHANGED
@@ -1,3 +1,7 @@
1
+ == 0.2.5 (2012-09-26)
2
+
3
+ * allow more than 99 placeholders. contributor: @filterfish
4
+
1
5
  == 0.2.4 (2012-08-18)
2
6
 
3
7
  * gc protect bind args.
@@ -18,22 +18,33 @@ VALUE rb_uuid_string() {
18
18
  return rb_str_new(uuid_hex, sizeof(uuid_t) * 2 + 1);
19
19
  }
20
20
 
21
- /* NOTE: very naive, no regex etc. */
22
- /* TODO: a better ragel based replace thingamajigy */
21
+ /* TODO: very naive, a better ragel based replace thingamajigy */
23
22
  VALUE db_postgres_normalized_sql(VALUE sql) {
24
- int i = 0, j = 0, n = 1;
25
- char normalized[RSTRING_LEN(sql) * 2], *ptr = RSTRING_PTR(sql);
23
+ VALUE result;
24
+ int i = 0, j = 0, n = 1, size;
25
+ char *normalized, *ptr = RSTRING_PTR(sql);
26
+
27
+ size = RSTRING_LEN(sql) * 2;
28
+ normalized = (char *)malloc(size);
26
29
 
27
30
  while (i < RSTRING_LEN(sql)) {
28
31
  if (*ptr == '?')
29
- j += snprintf(normalized + j, 4, "$%d", n++);
32
+ j += snprintf(normalized + j, 6, "$%d", n++);
30
33
  else
31
34
  normalized[j++] = *ptr;
35
+
36
+ if (j >= size - 6) {
37
+ size = size * 2;
38
+ normalized = (char *)realloc(normalized, size);
39
+ }
40
+
32
41
  ptr++;
33
42
  i++;
34
43
  }
35
44
 
36
- return rb_str_new(normalized, j);
45
+ result = rb_str_new(normalized, j);
46
+ free(normalized);
47
+ return result;
37
48
  }
38
49
 
39
50
  void db_postgres_check_result(PGresult *result) {
data/test/test_adapter.rb CHANGED
@@ -74,6 +74,20 @@ describe 'postgres adapter' do
74
74
  assert_raises(Swift::RuntimeError) { s.execute(1) }
75
75
  end
76
76
 
77
+ it 'should allow a statement with more than 99 placeholders' do
78
+ statement_placeholders = Array.new(100, "(?)").join(",")
79
+ data = Array.new(100, "test")
80
+
81
+ assert db.execute('drop table if exists users')
82
+ assert db.execute("create table users(id serial primary key, name text)")
83
+ assert db.execute("insert into users (name) values #{statement_placeholders}", *data)
84
+
85
+ res = db.execute("select * from users")
86
+
87
+ assert_equal 100, res.count
88
+ assert_equal ["test"], res.map {|u| u[:name] }.uniq
89
+ end
90
+
77
91
  it 'should escape whatever' do
78
92
  assert_equal "foo''bar", db.escape("foo'bar")
79
93
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: swift-db-postgres
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.4
4
+ version: 0.2.5
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-08-18 00:00:00.000000000 Z
12
+ date: 2012-09-26 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rake