m4dbi 0.8.9 → 0.9.0
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.
- checksums.yaml +4 -4
- data/CHANGELOG +4 -0
- data/lib/m4dbi/database.rb +28 -6
- data/lib/m4dbi/statement.rb +23 -5
- data/lib/m4dbi/version.rb +1 -1
- data/spec/database.rb +63 -59
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 183465eef922074a8e4a27eaad19aeeda0d085c2
|
4
|
+
data.tar.gz: 115540f28694f243b8d580993fe44d8cedef8df3
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: de7799bcc987608482bdf9be412f3cd58214575ef09887b90806d8610c31cc00450c5dcd4a3a765b3e711f020bdaafdd5b9eb1c232ceb373c77f32fac3d6e18b
|
7
|
+
data.tar.gz: e843a99ae4027eaaf7c72f6b841751794179935f9d0fd1189b49165fd2f7af419b6e66d05629f04c3efc4289d7401b1f2a6b1930fdb27b28807d74c8d2a567f9
|
data/CHANGELOG
CHANGED
data/lib/m4dbi/database.rb
CHANGED
@@ -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
|
-
|
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 =
|
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 =
|
23
|
-
rows =
|
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 =
|
36
|
-
rows =
|
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
|
data/lib/m4dbi/statement.rb
CHANGED
@@ -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
|
-
|
17
|
+
self.synchronize do
|
18
|
+
@st.execute *args
|
19
|
+
end
|
9
20
|
end
|
10
21
|
|
11
22
|
def finish
|
12
|
-
|
23
|
+
self.synchronize do
|
24
|
+
@st.finish
|
25
|
+
end
|
13
26
|
end
|
14
27
|
|
15
28
|
def select( *bindvars )
|
16
|
-
|
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 =
|
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
|
data/lib/m4dbi/version.rb
CHANGED
data/spec/database.rb
CHANGED
@@ -9,76 +9,80 @@ describe 'M4DBI.last_dbh' do
|
|
9
9
|
end
|
10
10
|
end
|
11
11
|
|
12
|
-
describe 'M4DBI::Database
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
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
|
-
|
31
|
-
$dbh.select_column(
|
32
|
-
|
33
|
-
|
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
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
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
|
-
|
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
|
-
|
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
|
-
|
55
|
-
|
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
|
-
|
61
|
-
|
65
|
+
row.text.should.equal 'Second post.'
|
66
|
+
end
|
62
67
|
|
63
|
-
|
64
|
-
|
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.
|
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-
|
11
|
+
date: 2013-08-01 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: metaid
|