m4dbi 0.8.9 → 0.9.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: d9b6b21860211863ed3ef697886bc4dd8d017743
4
- data.tar.gz: 75fb2e328554c618b22c8919c05b3039d9a7f750
3
+ metadata.gz: 183465eef922074a8e4a27eaad19aeeda0d085c2
4
+ data.tar.gz: 115540f28694f243b8d580993fe44d8cedef8df3
5
5
  SHA512:
6
- metadata.gz: 333d1c3056960225ab40c2d1db68e397637b8dad22265c1e604ddc20130720a4afa757fb7f25ef3912228db18a8a78a2ddc848f952c8a62e304817af7c61fcc0
7
- data.tar.gz: c971fd38a928c3a4bae6f63e633344728bb1208dc0f5a99d1e9ec0fd12ba5e05cfd421e3aa04982dc9d6fd4471c46e993a62f5d57929922181d0220922a0db80
6
+ metadata.gz: de7799bcc987608482bdf9be412f3cd58214575ef09887b90806d8610c31cc00450c5dcd4a3a765b3e711f020bdaafdd5b9eb1c232ceb373c77f32fac3d6e18b
7
+ data.tar.gz: e843a99ae4027eaaf7c72f6b841751794179935f9d0fd1189b49165fd2f7af419b6e66d05629f04c3efc4289d7401b1f2a6b1930fdb27b28807d74c8d2a567f9
data/CHANGELOG CHANGED
@@ -1,3 +1,7 @@
1
+ ## 0.9.0
2
+
3
+ - Now thread safe.
4
+
1
5
  ## 0.6.1
2
6
 
3
7
  - Fixed major Hash#slice bug that only surfaced in Ruby 1.8.6 and earier.
@@ -1,17 +1,31 @@
1
+ require 'thread'
2
+
1
3
  module M4DBI
2
4
 
3
5
  class Database
4
6
 
5
7
  def initialize( rdbi_dbh )
6
8
  @dbh = rdbi_dbh
9
+ @mutex = Mutex.new
10
+ end
11
+
12
+ def synchronize
13
+ @mutex.synchronize do
14
+ yield
15
+ end
7
16
  end
8
17
 
9
18
  def prepare( *args )
10
- Statement.new( @dbh.prepare(*args) )
19
+ self.synchronize do
20
+ Statement.new( @dbh.prepare(*args), self )
21
+ end
11
22
  end
12
23
 
13
24
  def execute( *args )
14
- result = @dbh.execute(*args)
25
+ result = nil
26
+ self.synchronize do
27
+ result = @dbh.execute(*args)
28
+ end
15
29
  if defined?( RDBI::Driver::PostgreSQL ) && RDBI::Driver::PostgreSQL === @dbh.driver
16
30
  result.finish
17
31
  end
@@ -19,8 +33,12 @@ module M4DBI
19
33
  end
20
34
 
21
35
  def select( sql, *bindvars )
22
- result = @dbh.execute( sql, *bindvars )
23
- rows = result.fetch( :all, RDBI::Result::Driver::Struct )
36
+ result = nil
37
+ rows = nil
38
+ self.synchronize do
39
+ result = @dbh.execute( sql, *bindvars )
40
+ rows = result.fetch( :all, RDBI::Result::Driver::Struct )
41
+ end
24
42
  if defined?( RDBI::Driver::PostgreSQL ) && RDBI::Driver::PostgreSQL === @dbh.driver
25
43
  result.finish
26
44
  end
@@ -32,8 +50,12 @@ module M4DBI
32
50
  end
33
51
 
34
52
  def select_column( sql, *bindvars )
35
- result = @dbh.execute( sql, *bindvars )
36
- rows = result.fetch( 1, RDBI::Result::Driver::Array )
53
+ result = nil
54
+ rows = nil
55
+ self.synchronize do
56
+ result = @dbh.execute( sql, *bindvars )
57
+ rows = result.fetch( 1, RDBI::Result::Driver::Array )
58
+ end
37
59
  if defined?( RDBI::Driver::PostgreSQL ) && RDBI::Driver::PostgreSQL === @dbh.driver
38
60
  result.finish
39
61
  end
@@ -1,19 +1,34 @@
1
+ require 'thread'
2
+
1
3
  module M4DBI
2
4
  class Statement
3
- def initialize( rdbi_statement )
5
+ def initialize( rdbi_statement, m4dbi_dbh = nil )
4
6
  @st = rdbi_statement
7
+ @synchronizer = m4dbi_dbh || Mutex.new
8
+ end
9
+
10
+ def synchronize
11
+ @synchronizer.synchronize do
12
+ yield
13
+ end
5
14
  end
6
15
 
7
16
  def execute( *args )
8
- @st.execute *args
17
+ self.synchronize do
18
+ @st.execute *args
19
+ end
9
20
  end
10
21
 
11
22
  def finish
12
- @st.finish
23
+ self.synchronize do
24
+ @st.finish
25
+ end
13
26
  end
14
27
 
15
28
  def select( *bindvars )
16
- @st.execute( *bindvars ).fetch( :all, RDBI::Result::Driver::Struct )
29
+ self.synchronize do
30
+ @st.execute( *bindvars ).fetch( :all, RDBI::Result::Driver::Struct )
31
+ end
17
32
  end
18
33
 
19
34
  def select_one( *bindvars )
@@ -21,7 +36,10 @@ module M4DBI
21
36
  end
22
37
 
23
38
  def select_column( *bindvars )
24
- rows = @st.execute( *bindvars ).fetch( 1, RDBI::Result::Driver::Array )
39
+ rows = nil
40
+ self.synchronize do
41
+ rows = @st.execute( *bindvars ).fetch( 1, RDBI::Result::Driver::Array )
42
+ end
25
43
  if rows.any?
26
44
  rows[0][0]
27
45
  else
@@ -1,3 +1,3 @@
1
1
  module M4DBI
2
- VERSION = '0.8.9'
2
+ VERSION = '0.9.0'
3
3
  end
@@ -9,76 +9,80 @@ describe 'M4DBI.last_dbh' do
9
9
  end
10
10
  end
11
11
 
12
- describe 'M4DBI::Database#select_column' do
13
-
14
- it 'selects one column' do
15
- name = $dbh.select_column(
16
- "SELECT name FROM authors LIMIT 1"
17
- )
18
- name.class.should.not.equal Array
19
- name.should.equal 'author1'
20
-
21
- null = $dbh.select_column(
22
- "SELECT c4 FROM many_col_table WHERE c3 = 40"
23
- )
24
- null.should.be.nil
25
-
26
- should.raise( RDBI::Error ) do
27
- $dbh.select_column( "SELECT name FROM authors WHERE 1+1 = 3" )
12
+ describe 'M4DBI::Database' do
13
+
14
+ describe '#select_column' do
15
+
16
+ it 'selects one column' do
17
+ name = $dbh.select_column(
18
+ "SELECT name FROM authors LIMIT 1"
19
+ )
20
+ name.class.should.not.equal Array
21
+ name.should.equal 'author1'
22
+
23
+ null = $dbh.select_column(
24
+ "SELECT c4 FROM many_col_table WHERE c3 = 40"
25
+ )
26
+ null.should.be.nil
27
+
28
+ should.raise( RDBI::Error ) do
29
+ $dbh.select_column( "SELECT name FROM authors WHERE 1+1 = 3" )
30
+ end
31
+
32
+ begin
33
+ $dbh.select_column( "SELECT name FROM authors WHERE 1+1 = 3" )
34
+ rescue RDBI::Error => e
35
+ e.message.should.match /SELECT name FROM authors WHERE 1\+1 = 3/
36
+ end
28
37
  end
29
38
 
30
- begin
31
- $dbh.select_column( "SELECT name FROM authors WHERE 1+1 = 3" )
32
- rescue RDBI::Error => e
33
- e.message.should.match /SELECT name FROM authors WHERE 1\+1 = 3/
39
+ it 'selects one column of first row' do
40
+ name = $dbh.select_column(
41
+ "SELECT name FROM authors ORDER BY name DESC"
42
+ )
43
+ name.should.equal 'author3'
34
44
  end
35
- end
36
45
 
37
- it 'selects one column of first row' do
38
- name = $dbh.select_column(
39
- "SELECT name FROM authors ORDER BY name DESC"
40
- )
41
- name.should.equal 'author3'
46
+ it 'selects first column of first row' do
47
+ name = $dbh.select_column(
48
+ "SELECT name, id FROM authors ORDER BY name DESC"
49
+ )
50
+ name.should.equal 'author3'
51
+ end
42
52
  end
43
53
 
44
- it 'selects first column of first row' do
45
- name = $dbh.select_column(
46
- "SELECT name, id FROM authors ORDER BY name DESC"
47
- )
48
- name.should.equal 'author3'
49
- end
50
- end
54
+ describe 'row accessors' do
51
55
 
52
- describe 'row accessors' do
56
+ it 'provide read access via #fieldname' do
57
+ row = $dbh.select_one(
58
+ "SELECT * FROM posts ORDER BY author_id DESC LIMIT 1"
59
+ )
60
+ row.should.not.equal nil
53
61
 
54
- it 'provide read access via #fieldname' do
55
- row = $dbh.select_one(
56
- "SELECT * FROM posts ORDER BY author_id DESC LIMIT 1"
57
- )
58
- row.should.not.equal nil
62
+ row.author_id.should.be.same_as row[ 'author_id' ]
63
+ row.text.should.be.same_as row[ 'text' ]
59
64
 
60
- row.author_id.should.be.same_as row[ 'author_id' ]
61
- row.text.should.be.same_as row[ 'text' ]
65
+ row.text.should.equal 'Second post.'
66
+ end
62
67
 
63
- row.text.should.equal 'Second post.'
64
- end
68
+ it 'provide in-memory (non-syncing) write access via #fieldname=' do
69
+ row = $dbh.select_one(
70
+ "SELECT * FROM posts ORDER BY author_id DESC LIMIT 1"
71
+ )
72
+ row.should.not.equal nil
73
+
74
+ old_id = row[ :id ]
75
+ row[ :id ] = old_id + 1
76
+ row[ :id ].should.not.equal old_id
77
+ row[ :id ].should.equal( old_id + 1 )
78
+
79
+ old_text = row.text
80
+ new_text = 'This is the new post text.'
81
+ row.text = new_text
82
+ row.text.should.not.equal old_text
83
+ row.text.should.equal new_text
84
+ end
65
85
 
66
- it 'provide in-memory (non-syncing) write access via #fieldname=' do
67
- row = $dbh.select_one(
68
- "SELECT * FROM posts ORDER BY author_id DESC LIMIT 1"
69
- )
70
- row.should.not.equal nil
71
-
72
- old_id = row[ :id ]
73
- row[ :id ] = old_id + 1
74
- row[ :id ].should.not.equal old_id
75
- row[ :id ].should.equal( old_id + 1 )
76
-
77
- old_text = row.text
78
- new_text = 'This is the new post text.'
79
- row.text = new_text
80
- row.text.should.not.equal old_text
81
- row.text.should.equal new_text
82
86
  end
83
87
 
84
88
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: m4dbi
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.8.9
4
+ version: 0.9.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Pistos
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2013-06-14 00:00:00.000000000 Z
11
+ date: 2013-08-01 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: metaid