rdbi-driver-mysql 0.9.1 → 0.9.2
Sign up to get free protection for your applications and to get access to all the features.
- data/VERSION +1 -1
- data/lib/rdbi-driver-mysql.rb +2 -0
- data/lib/rdbi/driver/mysql.rb +76 -38
- data/rdbi-driver-mysql.gemspec +71 -0
- data/test/test_database.rb +13 -14
- data/test/test_statement.rb +30 -1
- data/test/test_types.rb +6 -0
- metadata +7 -14
- data/.gitignore +0 -21
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.9.
|
1
|
+
0.9.2
|
data/lib/rdbi/driver/mysql.rb
CHANGED
@@ -144,11 +144,16 @@ class RDBI::Driver::MySQL < RDBI::Driver
|
|
144
144
|
end
|
145
145
|
|
146
146
|
def table_schema( table_name )
|
147
|
-
info_row =
|
147
|
+
info_row = nil
|
148
|
+
|
149
|
+
execute(
|
148
150
|
"SELECT table_type FROM information_schema.tables WHERE table_schema = ? and table_name = ?",
|
149
151
|
self.database_name,
|
150
152
|
table_name.to_s
|
151
|
-
)
|
153
|
+
) do |res|
|
154
|
+
info_row = res.fetch(1)[0]
|
155
|
+
end
|
156
|
+
|
152
157
|
if info_row.nil?
|
153
158
|
return nil
|
154
159
|
end
|
@@ -166,14 +171,16 @@ class RDBI::Driver::MySQL < RDBI::Driver
|
|
166
171
|
execute( "SELECT column_name, data_type, is_nullable FROM information_schema.columns WHERE table_schema = ? and table_name = ?",
|
167
172
|
self.database_name,
|
168
173
|
table_name.to_s
|
169
|
-
)
|
170
|
-
|
171
|
-
|
172
|
-
|
173
|
-
|
174
|
-
|
175
|
-
|
176
|
-
|
174
|
+
) do |res|
|
175
|
+
res.fetch( :all ).each do |row|
|
176
|
+
col = RDBI::Column.new
|
177
|
+
col.name = row[0].to_sym
|
178
|
+
col.type = row[1].to_sym
|
179
|
+
# TODO: ensure this ruby_type is solid, especially re: dates and times
|
180
|
+
col.ruby_type = row[1].to_sym
|
181
|
+
col.nullable = row[2] == "YES"
|
182
|
+
sch.columns << col
|
183
|
+
end
|
177
184
|
end
|
178
185
|
|
179
186
|
sch
|
@@ -181,10 +188,11 @@ class RDBI::Driver::MySQL < RDBI::Driver
|
|
181
188
|
|
182
189
|
def schema
|
183
190
|
schemata = []
|
184
|
-
execute( "SELECT table_name FROM information_schema.tables where table_schema = ?", self.database_name )
|
185
|
-
schemata
|
191
|
+
execute( "SELECT table_name FROM information_schema.tables where table_schema = ?", self.database_name ) do |res|
|
192
|
+
schemata = res.fetch( :all )
|
186
193
|
end
|
187
|
-
|
194
|
+
|
195
|
+
schemata.collect { |x| table_schema(x[0]) }
|
188
196
|
end
|
189
197
|
|
190
198
|
def ping
|
@@ -249,11 +257,11 @@ class RDBI::Driver::MySQL < RDBI::Driver
|
|
249
257
|
if @array_handle
|
250
258
|
@array_handle.first
|
251
259
|
else
|
252
|
-
|
260
|
+
cur = @handle.row_tell
|
253
261
|
@handle.data_seek(0)
|
254
262
|
res = @handle.fetch
|
255
|
-
@handle.
|
256
|
-
res
|
263
|
+
@handle.row_seek(cur)
|
264
|
+
return res
|
257
265
|
end
|
258
266
|
end
|
259
267
|
|
@@ -261,21 +269,33 @@ class RDBI::Driver::MySQL < RDBI::Driver
|
|
261
269
|
if @array_handle
|
262
270
|
@array_handle.last
|
263
271
|
else
|
264
|
-
|
265
|
-
@handle.
|
272
|
+
#raise RDBI::Cursor::NotRewindableError, "last requires a rewindable result in mysql"
|
273
|
+
cur = @handle.row_tell
|
274
|
+
@handle.data_seek(@handle.num_rows - 1)
|
266
275
|
res = @handle.fetch
|
267
|
-
@handle.
|
268
|
-
res
|
276
|
+
@handle.row_seek(cur)
|
277
|
+
return res
|
269
278
|
end
|
270
279
|
end
|
271
280
|
|
272
281
|
def rest
|
273
|
-
oindex
|
282
|
+
oindex = nil
|
283
|
+
|
284
|
+
if @array_handle
|
285
|
+
oindex, @index = @index, @array_handle.size
|
286
|
+
else
|
287
|
+
oindex, @index = @index, @handle.num_rows
|
288
|
+
end
|
289
|
+
|
274
290
|
fetch_range(oindex, @index)
|
275
291
|
end
|
276
292
|
|
277
293
|
def all
|
278
|
-
|
294
|
+
if @array_handle
|
295
|
+
fetch_range(0, @array_handle.size)
|
296
|
+
else
|
297
|
+
fetch_range(0, @handle.num_rows)
|
298
|
+
end
|
279
299
|
end
|
280
300
|
|
281
301
|
def [](index)
|
@@ -286,7 +306,7 @@ class RDBI::Driver::MySQL < RDBI::Driver
|
|
286
306
|
if @array_handle
|
287
307
|
@index == @array_handle.size
|
288
308
|
else
|
289
|
-
@handle.
|
309
|
+
@index == @handle.num_rows
|
290
310
|
end
|
291
311
|
end
|
292
312
|
|
@@ -298,7 +318,11 @@ class RDBI::Driver::MySQL < RDBI::Driver
|
|
298
318
|
end
|
299
319
|
|
300
320
|
def empty?
|
301
|
-
@array_handle
|
321
|
+
if @array_handle
|
322
|
+
@array_handle.empty?
|
323
|
+
else
|
324
|
+
@handle.num_rows == 0
|
325
|
+
end
|
302
326
|
end
|
303
327
|
|
304
328
|
def finish
|
@@ -319,14 +343,16 @@ class RDBI::Driver::MySQL < RDBI::Driver
|
|
319
343
|
|
320
344
|
def fetch_range(start, stop)
|
321
345
|
if @array_handle
|
322
|
-
@array_handle[start, stop]
|
346
|
+
return @array_handle[start, stop]
|
323
347
|
else
|
324
348
|
ary = []
|
325
349
|
|
326
350
|
@handle.data_seek(start)
|
327
351
|
(stop - start).times do
|
328
|
-
@handle.fetch
|
352
|
+
ary.push @handle.fetch
|
329
353
|
end
|
354
|
+
|
355
|
+
return ary
|
330
356
|
end
|
331
357
|
end
|
332
358
|
end
|
@@ -350,6 +376,23 @@ class RDBI::Driver::MySQL < RDBI::Driver
|
|
350
376
|
@output_type_map = RDBI::Type.create_type_hash( RDBI::Type::Out )
|
351
377
|
|
352
378
|
manipulate_type_maps
|
379
|
+
|
380
|
+
prep_finalizer { @my_query.close rescue nil }
|
381
|
+
end
|
382
|
+
|
383
|
+
def new_modification(*binds)
|
384
|
+
# FIXME move to RDBI::Util or something.
|
385
|
+
hashes, binds = binds.partition { |x| x.kind_of?(Hash) }
|
386
|
+
hash = hashes.inject({}) { |x, y| x.merge(y) }
|
387
|
+
hash.keys.each do |key|
|
388
|
+
if index = @index_map.index(key)
|
389
|
+
binds.insert(index, hash[key])
|
390
|
+
end
|
391
|
+
end
|
392
|
+
|
393
|
+
res = @my_query.execute(*binds)
|
394
|
+
|
395
|
+
return res.affected_rows
|
353
396
|
end
|
354
397
|
|
355
398
|
def new_execution(*binds)
|
@@ -384,11 +427,6 @@ class RDBI::Driver::MySQL < RDBI::Driver
|
|
384
427
|
[ Cursor.new(res), schema, @output_type_map ]
|
385
428
|
end
|
386
429
|
|
387
|
-
def finish
|
388
|
-
@my_query.close rescue nil
|
389
|
-
super
|
390
|
-
end
|
391
|
-
|
392
430
|
protected
|
393
431
|
|
394
432
|
def map_type(col)
|
@@ -405,23 +443,23 @@ class RDBI::Driver::MySQL < RDBI::Driver
|
|
405
443
|
# XXX yep. slow as snot.
|
406
444
|
datetime_conv = proc { |x| DateTime.parse(x.to_s + " #{zone}") }
|
407
445
|
|
408
|
-
@output_type_map[:datetime] =
|
446
|
+
@output_type_map[:datetime] = [TypeLib::Filter.new(datetime_check, datetime_conv)]
|
409
447
|
|
410
|
-
@input_type_map[TrueClass] = TypeLib::Filter.new(
|
448
|
+
@input_type_map[TrueClass] = [TypeLib::Filter.new(
|
411
449
|
RDBI::Type::Checks::IS_BOOLEAN,
|
412
450
|
proc { |x| 1 }
|
413
|
-
)
|
451
|
+
)]
|
414
452
|
|
415
|
-
@input_type_map[FalseClass] = TypeLib::Filter.new(
|
453
|
+
@input_type_map[FalseClass] = [TypeLib::Filter.new(
|
416
454
|
RDBI::Type::Checks::IS_BOOLEAN,
|
417
455
|
proc { |x| 0 }
|
418
|
-
)
|
456
|
+
)]
|
419
457
|
|
420
458
|
if dbh.cast_booleans
|
421
|
-
boolean_filter = TypeLib::Filter.new(
|
459
|
+
boolean_filter = [TypeLib::Filter.new(
|
422
460
|
proc { |x| x == 1 or x == 0 },
|
423
461
|
proc { |x| x == 1 }
|
424
|
-
)
|
462
|
+
)]
|
425
463
|
|
426
464
|
@output_type_map[:boolean] = boolean_filter
|
427
465
|
end
|
@@ -0,0 +1,71 @@
|
|
1
|
+
# Generated by jeweler
|
2
|
+
# DO NOT EDIT THIS FILE DIRECTLY
|
3
|
+
# Instead, edit Jeweler::Tasks in Rakefile, and run 'rake gemspec'
|
4
|
+
# -*- encoding: utf-8 -*-
|
5
|
+
|
6
|
+
Gem::Specification.new do |s|
|
7
|
+
s.name = %q{rdbi-driver-mysql}
|
8
|
+
s.version = "0.9.2"
|
9
|
+
|
10
|
+
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
11
|
+
s.authors = ["Erik Hollensbe"]
|
12
|
+
s.date = %q{2010-12-10}
|
13
|
+
s.description = %q{mysql gem-based driver for RDBI}
|
14
|
+
s.email = %q{erik@hollensbe.org}
|
15
|
+
s.extra_rdoc_files = [
|
16
|
+
"LICENSE",
|
17
|
+
"README.rdoc"
|
18
|
+
]
|
19
|
+
s.files = [
|
20
|
+
".document",
|
21
|
+
"LICENSE",
|
22
|
+
"README.rdoc",
|
23
|
+
"Rakefile",
|
24
|
+
"VERSION",
|
25
|
+
"lib/rdbi-driver-mysql.rb",
|
26
|
+
"lib/rdbi/driver/mysql.rb",
|
27
|
+
"rdbi-driver-mysql.gemspec",
|
28
|
+
"test/helper.rb",
|
29
|
+
"test/test_connect.rb",
|
30
|
+
"test/test_database.rb",
|
31
|
+
"test/test_statement.rb",
|
32
|
+
"test/test_types.rb"
|
33
|
+
]
|
34
|
+
s.homepage = %q{http://github.com/RDBI/rdbi-driver-mysql}
|
35
|
+
s.require_paths = ["lib"]
|
36
|
+
s.rubygems_version = %q{1.3.7}
|
37
|
+
s.summary = %q{mysql gem-based driver for RDBI}
|
38
|
+
s.test_files = [
|
39
|
+
"test/helper.rb",
|
40
|
+
"test/test_connect.rb",
|
41
|
+
"test/test_database.rb",
|
42
|
+
"test/test_statement.rb",
|
43
|
+
"test/test_types.rb"
|
44
|
+
]
|
45
|
+
|
46
|
+
if s.respond_to? :specification_version then
|
47
|
+
current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
|
48
|
+
s.specification_version = 3
|
49
|
+
|
50
|
+
if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
|
51
|
+
s.add_runtime_dependency(%q<rdbi>, [">= 0"])
|
52
|
+
s.add_runtime_dependency(%q<mysql>, [">= 2.8.1"])
|
53
|
+
s.add_development_dependency(%q<test-unit>, [">= 0"])
|
54
|
+
s.add_development_dependency(%q<rdoc>, [">= 0"])
|
55
|
+
s.add_development_dependency(%q<rdbi-dbrc>, [">= 0"])
|
56
|
+
else
|
57
|
+
s.add_dependency(%q<rdbi>, [">= 0"])
|
58
|
+
s.add_dependency(%q<mysql>, [">= 2.8.1"])
|
59
|
+
s.add_dependency(%q<test-unit>, [">= 0"])
|
60
|
+
s.add_dependency(%q<rdoc>, [">= 0"])
|
61
|
+
s.add_dependency(%q<rdbi-dbrc>, [">= 0"])
|
62
|
+
end
|
63
|
+
else
|
64
|
+
s.add_dependency(%q<rdbi>, [">= 0"])
|
65
|
+
s.add_dependency(%q<mysql>, [">= 2.8.1"])
|
66
|
+
s.add_dependency(%q<test-unit>, [">= 0"])
|
67
|
+
s.add_dependency(%q<rdoc>, [">= 0"])
|
68
|
+
s.add_dependency(%q<rdbi-dbrc>, [">= 0"])
|
69
|
+
end
|
70
|
+
end
|
71
|
+
|
data/test/test_database.rb
CHANGED
@@ -29,27 +29,26 @@ class TestDatabase < Test::Unit::TestCase
|
|
29
29
|
|
30
30
|
def test_03_execute
|
31
31
|
self.dbh = init_database
|
32
|
-
|
33
|
-
assert res
|
34
|
-
assert_kind_of( RDBI::Result, res )
|
35
|
-
assert_equal( 1, res.affected_count )
|
32
|
+
assert_equal(1, dbh.execute_modification( "insert into foo (bar) values (?)", 1 ))
|
36
33
|
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
34
|
+
dbh.execute( "select * from foo" ) do |res|
|
35
|
+
assert res
|
36
|
+
assert_kind_of( RDBI::Result, res )
|
37
|
+
assert_equal( [[1]], res.fetch(:all) )
|
41
38
|
|
42
|
-
|
43
|
-
|
44
|
-
|
39
|
+
rows = res.as( :Struct ).fetch( :all )
|
40
|
+
row = rows[ 0 ]
|
41
|
+
assert_equal( 1, row.bar )
|
42
|
+
end
|
45
43
|
end
|
46
44
|
|
47
45
|
def test_04_transaction
|
48
46
|
self.dbh = init_database
|
47
|
+
dbh.rewindable_result = true
|
49
48
|
|
50
49
|
dbh.transaction do
|
51
50
|
assert dbh.in_transaction?
|
52
|
-
5.times { dbh.
|
51
|
+
5.times { dbh.execute_modification( "insert into foo (bar) values (?)", 1 ) }
|
53
52
|
dbh.rollback
|
54
53
|
assert ! dbh.in_transaction?
|
55
54
|
end
|
@@ -60,7 +59,7 @@ class TestDatabase < Test::Unit::TestCase
|
|
60
59
|
|
61
60
|
dbh.transaction do
|
62
61
|
assert dbh.in_transaction?
|
63
|
-
5.times { dbh.
|
62
|
+
5.times { dbh.execute_modification("insert into foo (bar) values (?)", 1) }
|
64
63
|
assert_equal( [[1]] * 5, dbh.execute("select * from foo").fetch(:all) )
|
65
64
|
dbh.commit
|
66
65
|
assert ! dbh.in_transaction?
|
@@ -100,7 +99,7 @@ class TestDatabase < Test::Unit::TestCase
|
|
100
99
|
def test_06_schema
|
101
100
|
self.dbh = init_database
|
102
101
|
|
103
|
-
dbh.
|
102
|
+
dbh.execute_modification( "insert into bar (foo, bar) values (?, ?)", "foo", 1 )
|
104
103
|
res = dbh.execute( "select * from bar" )
|
105
104
|
|
106
105
|
assert res
|
data/test/test_statement.rb
CHANGED
@@ -17,6 +17,35 @@ class TestStatement < Test::Unit::TestCase
|
|
17
17
|
sth.execute(1)
|
18
18
|
# FIXME affected rows
|
19
19
|
sth.finish
|
20
|
-
|
20
|
+
|
21
|
+
dbh.execute("select * from integer_test") do |res|
|
22
|
+
assert_equal([[1]], res.fetch(:all))
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
def test_03_rewindables
|
27
|
+
|
28
|
+
dbh.execute("select * from integer_test") do |res|
|
29
|
+
assert(res.empty?)
|
30
|
+
end
|
31
|
+
|
32
|
+
sth = dbh.prepare("insert into integer_test (id) values (?)")
|
33
|
+
sth.execute(1)
|
34
|
+
sth.execute(2)
|
35
|
+
sth.finish
|
36
|
+
|
37
|
+
sth = dbh.prepare("select * from integer_test")
|
38
|
+
res = sth.execute
|
39
|
+
|
40
|
+
assert_equal([1], res.fetch(:first))
|
41
|
+
assert_equal([2], res.fetch(:last))
|
42
|
+
assert_equal([[1], [2]], res.fetch(:rest))
|
43
|
+
assert_equal([], res.fetch(:rest))
|
44
|
+
|
45
|
+
res.rewind
|
46
|
+
|
47
|
+
assert_equal([[1], [2]], res.fetch(:rest))
|
48
|
+
|
49
|
+
sth.finish
|
21
50
|
end
|
22
51
|
end
|
data/test/test_types.rb
CHANGED
metadata
CHANGED
@@ -1,13 +1,12 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: rdbi-driver-mysql
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
hash: 57
|
5
4
|
prerelease: false
|
6
5
|
segments:
|
7
6
|
- 0
|
8
7
|
- 9
|
9
|
-
-
|
10
|
-
version: 0.9.
|
8
|
+
- 2
|
9
|
+
version: 0.9.2
|
11
10
|
platform: ruby
|
12
11
|
authors:
|
13
12
|
- Erik Hollensbe
|
@@ -15,7 +14,7 @@ autorequire:
|
|
15
14
|
bindir: bin
|
16
15
|
cert_chain: []
|
17
16
|
|
18
|
-
date: 2010-
|
17
|
+
date: 2010-12-10 00:00:00 -05:00
|
19
18
|
default_executable:
|
20
19
|
dependencies:
|
21
20
|
- !ruby/object:Gem::Dependency
|
@@ -26,7 +25,6 @@ dependencies:
|
|
26
25
|
requirements:
|
27
26
|
- - ">="
|
28
27
|
- !ruby/object:Gem::Version
|
29
|
-
hash: 3
|
30
28
|
segments:
|
31
29
|
- 0
|
32
30
|
version: "0"
|
@@ -40,7 +38,6 @@ dependencies:
|
|
40
38
|
requirements:
|
41
39
|
- - ">="
|
42
40
|
- !ruby/object:Gem::Version
|
43
|
-
hash: 45
|
44
41
|
segments:
|
45
42
|
- 2
|
46
43
|
- 8
|
@@ -56,7 +53,6 @@ dependencies:
|
|
56
53
|
requirements:
|
57
54
|
- - ">="
|
58
55
|
- !ruby/object:Gem::Version
|
59
|
-
hash: 3
|
60
56
|
segments:
|
61
57
|
- 0
|
62
58
|
version: "0"
|
@@ -70,7 +66,6 @@ dependencies:
|
|
70
66
|
requirements:
|
71
67
|
- - ">="
|
72
68
|
- !ruby/object:Gem::Version
|
73
|
-
hash: 3
|
74
69
|
segments:
|
75
70
|
- 0
|
76
71
|
version: "0"
|
@@ -84,7 +79,6 @@ dependencies:
|
|
84
79
|
requirements:
|
85
80
|
- - ">="
|
86
81
|
- !ruby/object:Gem::Version
|
87
|
-
hash: 3
|
88
82
|
segments:
|
89
83
|
- 0
|
90
84
|
version: "0"
|
@@ -101,12 +95,13 @@ extra_rdoc_files:
|
|
101
95
|
- README.rdoc
|
102
96
|
files:
|
103
97
|
- .document
|
104
|
-
- .gitignore
|
105
98
|
- LICENSE
|
106
99
|
- README.rdoc
|
107
100
|
- Rakefile
|
108
101
|
- VERSION
|
102
|
+
- lib/rdbi-driver-mysql.rb
|
109
103
|
- lib/rdbi/driver/mysql.rb
|
104
|
+
- rdbi-driver-mysql.gemspec
|
110
105
|
- test/helper.rb
|
111
106
|
- test/test_connect.rb
|
112
107
|
- test/test_database.rb
|
@@ -117,8 +112,8 @@ homepage: http://github.com/RDBI/rdbi-driver-mysql
|
|
117
112
|
licenses: []
|
118
113
|
|
119
114
|
post_install_message:
|
120
|
-
rdoc_options:
|
121
|
-
|
115
|
+
rdoc_options: []
|
116
|
+
|
122
117
|
require_paths:
|
123
118
|
- lib
|
124
119
|
required_ruby_version: !ruby/object:Gem::Requirement
|
@@ -126,7 +121,6 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
126
121
|
requirements:
|
127
122
|
- - ">="
|
128
123
|
- !ruby/object:Gem::Version
|
129
|
-
hash: 3
|
130
124
|
segments:
|
131
125
|
- 0
|
132
126
|
version: "0"
|
@@ -135,7 +129,6 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
135
129
|
requirements:
|
136
130
|
- - ">="
|
137
131
|
- !ruby/object:Gem::Version
|
138
|
-
hash: 3
|
139
132
|
segments:
|
140
133
|
- 0
|
141
134
|
version: "0"
|