swift-db-mysql 0.1.0 → 0.1.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.
data/CHANGELOG CHANGED
@@ -1,3 +1,11 @@
1
+ == 0.1.2 (2012-07-29)
2
+
3
+ * gc guard bind parameters during execute.
4
+
5
+ == 0.1.1 (2012-07-26)
6
+
7
+ * Updated compile flags.
8
+
1
9
  == 0.1.0 (2012-07-20)
2
10
 
3
11
  * Initial version.
data/README.md CHANGED
@@ -21,6 +21,7 @@ MRI adapter for MySQL
21
21
  #commit(savepoint = nil)
22
22
  #rollback(savepoint = nil)
23
23
  #transaction(savepoint = nil, &block)
24
+ #ping
24
25
  #close
25
26
  #closed?
26
27
  #escape(text)
@@ -103,6 +104,10 @@ Don't read too much into it. Each library has its advantages and disadvantages.
103
104
  ```
104
105
  # insert 1000 rows and read them back 100 times
105
106
 
107
+ $ ruby -v
108
+
109
+ ruby 1.9.3p125 (2012-02-16 revision 34643) [x86_64-linux]
110
+
106
111
  $ ruby check.rb
107
112
  user system total real
108
113
  do_mysql insert 0.170000 0.100000 0.270000 ( 0.629025)
@@ -95,7 +95,7 @@ VALUE db_mysql_adapter_initialize(VALUE self, VALUE options) {
95
95
 
96
96
  db = rb_hash_aref(options, ID2SYM(rb_intern("db")));
97
97
  user = rb_hash_aref(options, ID2SYM(rb_intern("user")));
98
- pass = rb_hash_aref(options, ID2SYM(rb_intern("pass")));
98
+ pass = rb_hash_aref(options, ID2SYM(rb_intern("password")));
99
99
  host = rb_hash_aref(options, ID2SYM(rb_intern("host")));
100
100
  port = rb_hash_aref(options, ID2SYM(rb_intern("port")));
101
101
  ssl = rb_hash_aref(options, ID2SYM(rb_intern("ssl")));
@@ -159,8 +159,10 @@ VALUE db_mysql_adapter_execute(int argc, VALUE *argv, VALUE self) {
159
159
  rb_scan_args(argc, argv, "10*", &sql, &bind);
160
160
  sql = TO_S(sql);
161
161
 
162
+ rb_gc_register_address(&bind);
162
163
  if (RARRAY_LEN(bind) > 0)
163
164
  sql = db_mysql_bind_sql(self, sql, bind);
165
+ rb_gc_unregister_address(&bind);
164
166
 
165
167
  Query q = {.connection = c, .sql = sql};
166
168
 
@@ -304,6 +306,11 @@ VALUE db_mysql_adapter_closed_q(VALUE self) {
304
306
  return a->connection ? Qfalse : Qtrue;
305
307
  }
306
308
 
309
+ VALUE db_mysql_adapter_ping(VALUE self) {
310
+ Adapter *a = db_mysql_adapter_handle(self);
311
+ return a->connection && mysql_ping(a->connection) == 0 ? Qtrue : Qfalse;
312
+ }
313
+
307
314
  VALUE db_mysql_adapter_prepare(VALUE self, VALUE sql) {
308
315
  return db_mysql_statement_initialize(db_mysql_statement_allocate(cDMS), self, sql);
309
316
  }
@@ -424,6 +431,7 @@ void init_swift_db_mysql_adapter() {
424
431
  rb_define_method(cDMA, "transaction", db_mysql_adapter_transaction, -1);
425
432
  rb_define_method(cDMA, "close", db_mysql_adapter_close, 0);
426
433
  rb_define_method(cDMA, "closed?", db_mysql_adapter_closed_q, 0);
434
+ rb_define_method(cDMA, "ping", db_mysql_adapter_ping, 0);
427
435
  rb_define_method(cDMA, "escape", db_mysql_adapter_escape, 1);
428
436
  rb_define_method(cDMA, "fileno", db_mysql_adapter_fileno, 0);
429
437
  rb_define_method(cDMA, "query", db_mysql_adapter_query, -1);
@@ -2,7 +2,7 @@
2
2
 
3
3
  require 'mkmf'
4
4
 
5
- $CFLAGS = '-std=c99'
5
+ $CFLAGS = '-std=c99 -Os'
6
6
 
7
7
  inc_paths = %w(
8
8
  /usr/include
@@ -89,6 +89,7 @@ VALUE db_mysql_statement_execute(int argc, VALUE *argv, VALUE self) {
89
89
  mysql_bind = (MYSQL_BIND *)malloc(sizeof(MYSQL_BIND) * RARRAY_LEN(bind));
90
90
  memset(mysql_bind, 0, sizeof(MYSQL_BIND) * RARRAY_LEN(bind));
91
91
 
92
+ rb_gc_register_address(&bind);
92
93
  for (n = 0; n < RARRAY_LEN(bind); n++) {
93
94
  data = rb_ary_entry(bind, n);
94
95
  if (NIL_P(data)) {
@@ -105,12 +106,14 @@ VALUE db_mysql_statement_execute(int argc, VALUE *argv, VALUE self) {
105
106
  }
106
107
 
107
108
  if (mysql_stmt_bind_param(s->statement, mysql_bind) != 0) {
109
+ rb_gc_unregister_address(&bind);
108
110
  free(mysql_bind);
109
111
  rb_raise(eSwiftRuntimeError, mysql_stmt_error(s->statement));
110
112
  }
111
113
 
112
114
  error = (int)rb_thread_blocking_region(nogvl_mysql_statement_execute, s->statement, RUBY_UBF_IO, 0);
113
115
  free(mysql_bind);
116
+ rb_gc_unregister_address(&bind);
114
117
  }
115
118
  else {
116
119
  if ((n = mysql_stmt_param_count(s->statement)) > 0)
data/test/test_adapter.rb CHANGED
@@ -1,54 +1,56 @@
1
1
  require 'helper'
2
2
 
3
3
  describe 'mysql adapter' do
4
- it 'should initialize' do
5
- assert db
6
- end
7
-
8
- it 'should execute sql' do
9
- assert db.execute("select * from information_schema.tables limit 1")
10
- end
11
-
12
- it 'should expect the correct number of bind args' do
13
- assert_raises(Swift::ArgumentError) { db.execute("select * from information_schema.tables where table_name = ?", 1, 2) }
14
- end
15
-
16
- it 'should return result on #execute' do
17
- now = Time.now.utc
18
- assert db.execute('drop table if exists users')
19
- assert db.execute('create table users (id int auto_increment primary key, name text, age integer, created_at datetime)')
20
- assert db.execute('insert into users(name, age, created_at) values(?, ?, ?)', 'test', nil, now)
21
-
22
- result = db.execute('select * from users')
23
-
24
- assert_equal 1, result.selected_rows
25
- assert_equal 0, result.affected_rows
26
- assert_equal %w(id name age created_at).map(&:to_sym), result.fields
27
- assert_equal %w(integer text integer timestamp), result.types
28
-
29
- row = result.first
30
- assert_equal 1, row[:id]
31
- assert_equal 'test', row[:name]
32
- assert_equal nil, row[:age]
33
- assert_equal now.to_i, row[:created_at].to_time.to_i
34
-
35
- result = db.execute('delete from users where id = 0')
36
- assert_equal 0, result.selected_rows
37
- assert_equal 0, result.affected_rows
38
-
39
- assert_equal 1, db.execute('select count(*) as count from users').first[:count]
40
-
41
- result = db.execute('delete from users')
42
- assert_equal 0, result.selected_rows
43
- assert_equal 1, result.affected_rows
44
- end
45
-
46
- it 'should close handle' do
47
- assert !db.closed?
48
- assert db.close
49
- assert db.closed?
50
-
51
- assert_raises(Swift::ConnectionError) { db.execute("select * from users") }
4
+ it 'should initialize' do
5
+ assert db
6
+ end
7
+
8
+ it 'should execute sql' do
9
+ assert db.execute("select * from information_schema.tables limit 1")
10
+ end
11
+
12
+ it 'should expect the correct number of bind args' do
13
+ assert_raises(Swift::ArgumentError) { db.execute("select * from information_schema.tables where table_name = ?", 1, 2) }
14
+ end
15
+
16
+ it 'should return result on #execute' do
17
+ now = Time.now.utc
18
+ assert db.execute('drop table if exists users')
19
+ assert db.execute('create table users (id int auto_increment primary key, name text, age integer, created_at datetime)')
20
+ assert db.execute('insert into users(name, age, created_at) values(?, ?, ?)', 'test', nil, now)
21
+
22
+ result = db.execute('select * from users')
23
+
24
+ assert_equal 1, result.selected_rows
25
+ assert_equal 0, result.affected_rows
26
+ assert_equal %w(id name age created_at).map(&:to_sym), result.fields
27
+ assert_equal %w(integer text integer timestamp), result.types
28
+
29
+ row = result.first
30
+ assert_equal 1, row[:id]
31
+ assert_equal 'test', row[:name]
32
+ assert_equal nil, row[:age]
33
+ assert_equal now.to_i, row[:created_at].to_time.to_i
34
+
35
+ result = db.execute('delete from users where id = 0')
36
+ assert_equal 0, result.selected_rows
37
+ assert_equal 0, result.affected_rows
38
+
39
+ assert_equal 1, db.execute('select count(*) as count from users').first[:count]
40
+
41
+ result = db.execute('delete from users')
42
+ assert_equal 0, result.selected_rows
43
+ assert_equal 1, result.affected_rows
44
+ end
45
+
46
+ it 'should close handle' do
47
+ assert db.ping
48
+ assert !db.closed?
49
+ assert db.close
50
+ assert db.closed?
51
+ assert !db.ping
52
+
53
+ assert_raises(Swift::ConnectionError) { db.execute("select * from users") }
52
54
  end
53
55
 
54
56
  it 'should prepare & release statement' do
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: swift-db-mysql
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.1.2
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-07-20 00:00:00.000000000 Z
12
+ date: 2012-07-29 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rake
@@ -36,11 +36,11 @@ extensions:
36
36
  extra_rdoc_files: []
37
37
  files:
38
38
  - ext/swift/db/mysql/datetime.c
39
- - ext/swift/db/mysql/statement.c
40
39
  - ext/swift/db/mysql/main.c
41
- - ext/swift/db/mysql/typecast.c
42
- - ext/swift/db/mysql/result.c
43
40
  - ext/swift/db/mysql/adapter.c
41
+ - ext/swift/db/mysql/result.c
42
+ - ext/swift/db/mysql/typecast.c
43
+ - ext/swift/db/mysql/statement.c
44
44
  - ext/swift/db/mysql/common.c
45
45
  - ext/swift/db/mysql/typecast.h
46
46
  - ext/swift/db/mysql/datetime.h
@@ -52,8 +52,8 @@ files:
52
52
  - test/helper.rb
53
53
  - test/test_ssl.rb
54
54
  - test/test_encoding.rb
55
- - test/test_async.rb
56
55
  - test/test_adapter.rb
56
+ - test/test_async.rb
57
57
  - lib/swift/db/mysql.rb
58
58
  - lib/swift-db-mysql.rb
59
59
  - README.md