swift-db-postgres 0.1.2 → 0.2.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
+ == 0.2.0 (2012-07-29)
2
+
3
+ * Added Adapter#native_bind_format and Adapter#native_bind_format=
4
+
1
5
  == 0.1.2 (2012-07-29)
2
6
 
3
7
  * gc guard bind parameters during execute.
data/README.md CHANGED
@@ -21,6 +21,8 @@ MRI adapter for PostgreSQL
21
21
  #commit(savepoint = nil)
22
22
  #rollback(savepoint = nil)
23
23
  #transaction(savepoint = nil, &block)
24
+ #native_bind_format(&block)
25
+ #native_bind_format=(value)
24
26
  #ping
25
27
  #close
26
28
  #closed?
@@ -45,6 +47,22 @@ MRI adapter for PostgreSQL
45
47
  #insert_id
46
48
  ```
47
49
 
50
+ ## Bind parameters and hstore operators
51
+
52
+ Swift::DB::Postgres uses '?' as a bind parameter and replaces them with the '$' equivalents. This causes issues when
53
+ you try to use the HStore '?' operator. You can permanently or temporarily disable the replacement strategy as below:
54
+
55
+ ```ruby
56
+
57
+ db.native_bind_format = true
58
+ db.execute("select * from users where tags ? $1", 'mayor')
59
+ db.native_bind_format = false
60
+
61
+ db.native_bind_format do
62
+ db.execute("select * from users where tags ? $1", 'mayor')
63
+ end
64
+ ```
65
+
48
66
  ## Example
49
67
 
50
68
  ```ruby
@@ -130,7 +130,8 @@ VALUE db_postgres_adapter_execute(int argc, VALUE *argv, VALUE self) {
130
130
  Adapter *a = db_postgres_adapter_handle_safe(self);
131
131
 
132
132
  rb_scan_args(argc, argv, "10*", &sql, &bind);
133
- sql = db_postgres_normalized_sql(sql);
133
+ if (!a->native)
134
+ sql = db_postgres_normalized_sql(sql);
134
135
 
135
136
  if (RARRAY_LEN(bind) > 0) {
136
137
  bind_args_size = (int *) malloc(sizeof(int) * RARRAY_LEN(bind));
@@ -361,6 +362,26 @@ VALUE db_postgres_adapter_result(VALUE self) {
361
362
  return db_postgres_result_load(db_postgres_result_allocate(cDPR), result);
362
363
  }
363
364
 
365
+ VALUE db_postgres_adapter_native(VALUE self) {
366
+ int status, native;
367
+ VALUE result;
368
+ Adapter *a = db_postgres_adapter_handle_safe(self);
369
+
370
+ native = a->native;
371
+ a->native = 1;
372
+ result = rb_protect(rb_yield, Qnil, &status);
373
+ a->native = native;
374
+ if (status)
375
+ rb_jump_tag(status);
376
+ return result;
377
+ }
378
+
379
+ VALUE db_postgres_adapter_native_set(VALUE self, VALUE flag) {
380
+ Adapter *a = db_postgres_adapter_handle_safe(self);
381
+ a->native = Qtrue == flag ? 1 : 0;
382
+ return flag;
383
+ }
384
+
364
385
  VALUE db_postgres_adapter_query(int argc, VALUE *argv, VALUE self) {
365
386
  VALUE sql, bind, data;
366
387
  char **bind_args_data = 0;
@@ -368,7 +389,8 @@ VALUE db_postgres_adapter_query(int argc, VALUE *argv, VALUE self) {
368
389
  Adapter *a = db_postgres_adapter_handle_safe(self);
369
390
 
370
391
  rb_scan_args(argc, argv, "10*", &sql, &bind);
371
- sql = db_postgres_normalized_sql(sql);
392
+ if (!a->native)
393
+ sql = db_postgres_normalized_sql(sql);
372
394
 
373
395
  if (RARRAY_LEN(bind) > 0) {
374
396
  bind_args_size = (int *) malloc(sizeof(int) * RARRAY_LEN(bind));
@@ -583,5 +605,8 @@ void init_swift_db_postgres_adapter() {
583
605
  rb_define_method(cDPA, "write", db_postgres_adapter_write, -1);
584
606
  rb_define_method(cDPA, "read", db_postgres_adapter_read, -1);
585
607
 
608
+ rb_define_method(cDPA, "native_bind_format", db_postgres_adapter_native, 0);
609
+ rb_define_method(cDPA, "native_bind_format=", db_postgres_adapter_native_set, 1);
610
+
586
611
  rb_global_variable(&sUser);
587
612
  }
@@ -9,6 +9,7 @@
9
9
  typedef struct Adapter {
10
10
  PGconn *connection;
11
11
  int t_nesting;
12
+ int native;
12
13
  } Adapter;
13
14
 
14
15
  void init_swift_db_postgres_adapter();
@@ -54,16 +54,17 @@ VALUE db_postgres_statement_allocate(VALUE klass) {
54
54
 
55
55
  VALUE db_postgres_statement_initialize(VALUE self, VALUE adapter, VALUE sql) {
56
56
  PGresult *result;
57
- PGconn *connection;
58
57
  Statement *s = db_postgres_statement_handle(self);
58
+ Adapter *a = db_postgres_adapter_handle_safe(adapter);
59
59
 
60
60
  snprintf(s->id, 64, "s%s", CSTRING(rb_uuid_string()));
61
61
  s->adapter = adapter;
62
62
  rb_gc_mark(s->adapter);
63
63
 
64
- connection = db_postgres_adapter_handle_safe(adapter)->connection;
65
- result = PQprepare(connection, s->id, CSTRING(db_postgres_normalized_sql(sql)), 0, 0);
64
+ if (!a->native)
65
+ sql = db_postgres_normalized_sql(sql);
66
66
 
67
+ result = PQprepare(a->connection, s->id, CSTRING(sql), 0, 0);
67
68
  db_postgres_check_result(result);
68
69
  PQclear(result);
69
70
  return self;
data/test/test_adapter.rb CHANGED
@@ -1,6 +1,6 @@
1
1
  require 'helper'
2
2
 
3
- describe 'postgres adapter' do
3
+ describe 'postgres adapter' do
4
4
  it 'should initialize' do
5
5
  assert db
6
6
  end
@@ -109,19 +109,20 @@ require 'helper'
109
109
  assert_raises(Swift::RuntimeError) { db.write("users", %w(name), "bar") }
110
110
  end
111
111
 
112
- # TODO
113
112
  it 'should not change the hstore ? operator' do
114
- skip
115
113
  assert db.execute('create extension if not exists hstore')
116
114
  assert db.execute('drop table if exists hstore_test')
117
115
  assert db.execute('create table hstore_test(id int, payload hstore)')
118
116
  assert db.execute('insert into hstore_test values(1, ?)', 'a => 1, b => 2')
119
117
 
118
+ db.native_bind_format = true
120
119
  assert_equal 1, db.execute('select * from hstore_test where payload ? $1', 'a').selected_rows
121
120
  assert_equal 0, db.execute('select * from hstore_test where payload ? $1', 'c').selected_rows
122
- assert_equal 1, db.execute('select * from hstore_test where payload ? ?', 'a').selected_rows
121
+ db.native_bind_format = false
123
122
 
124
- assert_equal 1, db.execute('select * from hstore_test where payload ?| ARRAY[?, ?]', 'a', 'b').selected_rows
125
- assert_equal 1, db.execute('select * from hstore_test where payload ?& ARRAY[?, ?]', 'a', 'b').selected_rows
123
+ db.native_bind_format do
124
+ assert_equal 1, db.execute('select * from hstore_test where payload ?| ARRAY[$1, $2]', 'a', 'b').selected_rows
125
+ assert_equal 1, db.execute('select * from hstore_test where payload ?& ARRAY[$1, $2]', 'a', 'b').selected_rows
126
+ end
126
127
  end
127
128
  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.1.2
4
+ version: 0.2.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -37,17 +37,17 @@ extra_rdoc_files: []
37
37
  files:
38
38
  - ext/swift/db/postgres/datetime.c
39
39
  - ext/swift/db/postgres/statement.c
40
+ - ext/swift/db/postgres/adapter.c
40
41
  - ext/swift/db/postgres/typecast.c
41
42
  - ext/swift/db/postgres/common.c
42
43
  - ext/swift/db/postgres/main.c
43
44
  - ext/swift/db/postgres/result.c
44
- - ext/swift/db/postgres/adapter.c
45
45
  - ext/swift/db/postgres/typecast.h
46
46
  - ext/swift/db/postgres/common.h
47
47
  - ext/swift/db/postgres/datetime.h
48
48
  - ext/swift/db/postgres/result.h
49
- - ext/swift/db/postgres/adapter.h
50
49
  - ext/swift/db/postgres/statement.h
50
+ - ext/swift/db/postgres/adapter.h
51
51
  - ext/swift/db/postgres/extconf.rb
52
52
  - test/test_ssl.rb
53
53
  - test/test_adapter.rb
@@ -84,3 +84,4 @@ signing_key:
84
84
  specification_version: 3
85
85
  summary: Swift postgres adapter
86
86
  test_files: []
87
+ has_rdoc: