rdbi-driver-postgresql 0.9.0 → 0.9.1

Sign up to get free protection for your applications and to get access to all the features.
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.9.0
1
+ 0.9.1
@@ -0,0 +1,2 @@
1
+ require 'rdbi'
2
+ require 'rdbi/driver/postgresql'
@@ -1,7 +1,6 @@
1
1
  require 'rdbi'
2
2
  require 'epoxy'
3
3
  require 'methlab'
4
- gem 'pg', '= 0.9.0'
5
4
  require 'pg'
6
5
 
7
6
  class RDBI::Driver::PostgreSQL < RDBI::Driver
@@ -71,7 +70,7 @@ class RDBI::Driver::PostgreSQL < RDBI::Driver
71
70
  "SELECT table_type FROM information_schema.tables WHERE table_schema = ? AND table_name = ?",
72
71
  pg_schema,
73
72
  table_name
74
- ).fetch( :all )[ 0 ]
73
+ ).fetch( :first ) rescue nil
75
74
  if info_row.nil?
76
75
  return nil
77
76
  end
@@ -201,13 +200,13 @@ class RDBI::Driver::PostgreSQL < RDBI::Driver
201
200
  def fetch_range(start, stop)
202
201
  # XXX when did PGresult get so stupid?
203
202
  ary = []
204
- (start..stop).to_a.each do |i|
203
+ (start..stop).each do |i|
205
204
  row = []
206
205
  @handle.num_fields.times do |j|
207
206
  row[ j ] = @handle.getvalue( i, j )
208
207
  end
209
208
 
210
- ary.push row
209
+ ary << row
211
210
  end
212
211
  # XXX end stupid rectifier.
213
212
 
@@ -215,27 +214,30 @@ class RDBI::Driver::PostgreSQL < RDBI::Driver
215
214
  end
216
215
 
217
216
  def fix_dates(values)
218
- # probably a better way to do this, but at least it's happening on fetch.
219
- retval = []
220
- values.each_with_index do |val, x|
217
+ index = 0
218
+
219
+ values.collect! do |val|
221
220
  if val.kind_of?(Array)
222
- newval = []
223
- val.each_with_index do |col, i|
224
- if !col.nil? and @schema.columns[i].type == 'timestamp without time zone'
221
+ index2 = 0
222
+ val.collect! do |col|
223
+ if !col.nil? and @schema.columns[index2].type == 'timestamp without time zone'
225
224
  col << @stub_datetime
226
225
  end
227
226
 
228
- newval.push(col)
227
+ index2 += 1
228
+ col
229
229
  end
230
- retval.push(newval)
231
230
  else
232
- if !val.nil? and @schema.columns[x].type == 'timestamp without time zone'
231
+ if !val.nil? and @schema.columns[index].type == 'timestamp without time zone'
233
232
  val << @stub_datetime
234
233
  end
235
- retval.push(val)
236
234
  end
235
+
236
+ index += 1
237
+ val
237
238
  end
238
- return retval
239
+
240
+ return values
239
241
  end
240
242
  end
241
243
 
@@ -251,7 +253,7 @@ class RDBI::Driver::PostgreSQL < RDBI::Driver
251
253
 
252
254
  ep = Epoxy.new( query )
253
255
  @index_map = ep.indexed_binds
254
- query = ep.quote(@index_map.compact.inject({}) { |x,y| x.merge({ y => nil }) }) do |x|
256
+ query = ep.quote(Hash[@index_map.compact.zip([])]) do |x|
255
257
  case x
256
258
  when Integer
257
259
  "$#{x+1}"
@@ -269,10 +271,11 @@ class RDBI::Driver::PostgreSQL < RDBI::Driver
269
271
  # @input_type_map initialized in superclass
270
272
  @output_type_map = RDBI::Type.create_type_hash( RDBI::Type::Out )
271
273
  @output_type_map[ :bigint ] = RDBI::Type.filterlist( RDBI::Type::Filters::STR_TO_INT )
274
+
275
+ prep_finalizer { @pg_result.clear }
272
276
  end
273
277
 
274
- # Returns an Array of things used to fill out the parameters to RDBI::Result.new
275
- def new_execution( *binds )
278
+ def new_modification(*binds)
276
279
  # FIXME move to RDBI::Util or something.
277
280
  hashes, binds = binds.partition { |x| x.kind_of?(Hash) }
278
281
  hash = hashes.inject({}) { |x, y| x.merge(y) }
@@ -283,21 +286,39 @@ class RDBI::Driver::PostgreSQL < RDBI::Driver
283
286
  end
284
287
 
285
288
  pg_result = @dbh.pg_conn.exec_prepared( @stmt_name, binds )
289
+ return pg_result.cmd_tuples
290
+ end
286
291
 
287
- columns = []
288
- stub_datetime = DateTime.now.strftime( " %z" )
289
- (0...pg_result.num_fields).each do |i|
290
- c = RDBI::Column.new
291
- c.name = pg_result.fname( i ).to_sym
292
- c.type = @dbh.pg_conn.exec(
293
- "SELECT format_type( #{ pg_result.ftype(i) }, #{ pg_result.fmod(i) } )"
294
- )[ 0 ].values[ 0 ]
295
- if c.type.start_with? 'timestamp'
296
- c.ruby_type = 'timestamp'.to_sym
297
- else
298
- c.ruby_type = c.type.to_sym
292
+ # Returns an Array of things used to fill out the parameters to RDBI::Result.new
293
+ def new_execution( *binds )
294
+ # FIXME move to RDBI::Util or something.
295
+ hashes, binds = binds.partition { |x| x.kind_of?(Hash) }
296
+ hash = hashes.inject({}, :merge)
297
+ hash.each do |key, value|
298
+ if index = @index_map.index(key)
299
+ binds.insert(index, value)
300
+ end
301
+ end
302
+
303
+ pg_result = @dbh.pg_conn.exec_prepared( @stmt_name, binds )
304
+
305
+ columns = []
306
+ column_query = (0...pg_result.num_fields).map do |x|
307
+ "format_type(#{ pg_result.ftype(x) }, #{ pg_result.fmod(x) }) as col#{x}"
308
+ end.join(", ")
309
+
310
+ unless column_query.empty?
311
+ @dbh.pg_conn.exec("select #{column_query}")[0].values.each_with_index do |type, i|
312
+ c = RDBI::Column.new
313
+ c.name = pg_result.fname( i ).to_sym
314
+ c.type = type
315
+ if c.type.start_with? 'timestamp'
316
+ c.ruby_type = 'timestamp'.to_sym
317
+ else
318
+ c.ruby_type = c.type.to_sym
319
+ end
320
+ columns << c
299
321
  end
300
- columns << c
301
322
  end
302
323
 
303
324
  this_schema = RDBI::Schema.new
@@ -305,10 +326,5 @@ class RDBI::Driver::PostgreSQL < RDBI::Driver
305
326
 
306
327
  [ Cursor.new(pg_result, this_schema), this_schema, @output_type_map ]
307
328
  end
308
-
309
- def finish
310
- @pg_result.clear
311
- super
312
- end
313
329
  end
314
330
  end
@@ -1,40 +1,40 @@
1
1
  # Generated by jeweler
2
2
  # DO NOT EDIT THIS FILE DIRECTLY
3
- # Instead, edit Jeweler::Tasks in Rakefile, and run the gemspec command
3
+ # Instead, edit Jeweler::Tasks in Rakefile, and run 'rake gemspec'
4
4
  # -*- encoding: utf-8 -*-
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{rdbi-driver-postgresql}
8
- s.version = "0.9.0"
8
+ s.version = "0.9.1"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["Pistos", "Erik Hollensbe"]
12
- s.date = %q{2010-08-21}
12
+ s.date = %q{2010-12-10}
13
13
  s.description = %q{PostgreSQL driver for RDBI}
14
14
  s.email = %q{rdbi@pistos.oib.com}
15
15
  s.extra_rdoc_files = [
16
16
  "LICENSE",
17
- "README.rdoc"
17
+ "README.rdoc"
18
18
  ]
19
19
  s.files = [
20
20
  "LICENCE",
21
- "LICENSE",
22
- "README.rdoc",
23
- "Rakefile",
24
- "VERSION",
25
- "lib/rdbi/driver/postgresql.rb",
26
- "rdbi-driver-postgresql.gemspec",
27
- "test/helper.rb",
28
- "test/test_database.rb"
21
+ "LICENSE",
22
+ "README.rdoc",
23
+ "Rakefile",
24
+ "VERSION",
25
+ "lib/rdbi-driver-postgresql.rb",
26
+ "lib/rdbi/driver/postgresql.rb",
27
+ "rdbi-driver-postgresql.gemspec",
28
+ "test/helper.rb",
29
+ "test/test_database.rb"
29
30
  ]
30
31
  s.homepage = %q{http://github.com/Pistos/rdbi-dbd-postgresql}
31
- s.rdoc_options = ["--charset=UTF-8"]
32
32
  s.require_paths = ["lib"]
33
33
  s.rubygems_version = %q{1.3.7}
34
34
  s.summary = %q{PostgreSQL driver for RDBI}
35
35
  s.test_files = [
36
36
  "test/helper.rb",
37
- "test/test_database.rb"
37
+ "test/test_database.rb"
38
38
  ]
39
39
 
40
40
  if s.respond_to? :specification_version then
@@ -2,9 +2,7 @@ require 'rubygems'
2
2
  gem 'test-unit'
3
3
  require 'test/unit'
4
4
  require 'fileutils'
5
-
6
- $LOAD_PATH.unshift(File.dirname(__FILE__))
7
- $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
5
+ gem 'pg', '= 0.9.0'
8
6
  gem 'rdbi-dbrc'
9
7
  require 'rdbi-dbrc'
10
8
  require 'rdbi/driver/postgresql'
@@ -31,7 +29,7 @@ class Test::Unit::TestCase
31
29
 
32
30
  def init_database
33
31
  dbh = new_database
34
- SQL.each { |query| dbh.execute(query) }
32
+ SQL.each { |query| dbh.execute_modification(query) }
35
33
  return dbh
36
34
  end
37
35
 
@@ -39,10 +39,7 @@ class TestDatabase < Test::Unit::TestCase
39
39
 
40
40
  def test_03_execute
41
41
  self.dbh = init_database
42
- res = dbh.execute( "insert into foo (bar) values (?)", 1 )
43
- assert res
44
- assert_kind_of( RDBI::Result, res )
45
- assert_equal( 1, res.affected_count )
42
+ assert_equal(1, dbh.execute_modification( "insert into foo (bar) values (?)", 1 ))
46
43
 
47
44
  res = dbh.execute( "select * from foo" )
48
45
  assert res
@@ -84,10 +81,11 @@ class TestDatabase < Test::Unit::TestCase
84
81
  assert sth
85
82
  assert_kind_of( RDBI::Statement, sth )
86
83
  assert_respond_to( sth, :execute )
84
+ assert_respond_to( sth, :execute_modification )
87
85
 
88
86
  5.times do
89
- res = sth.execute( 1 )
90
- assert_equal( 1, res.affected_count )
87
+ res = sth.execute_modification( 1 )
88
+ assert_equal( 1, res )
91
89
  end
92
90
 
93
91
  assert_equal( dbh.last_statement.object_id, sth.object_id )
@@ -118,7 +116,7 @@ class TestDatabase < Test::Unit::TestCase
118
116
 
119
117
  dbh.transaction do
120
118
  assert dbh.in_transaction?
121
- 5.times { dbh.execute( "insert into foo (bar) values (?)", 1 ) }
119
+ 5.times { dbh.execute_modification( "insert into foo (bar) values (?)", 1 ) }
122
120
  dbh.rollback
123
121
  assert ! dbh.in_transaction?
124
122
  end
@@ -129,7 +127,7 @@ class TestDatabase < Test::Unit::TestCase
129
127
 
130
128
  dbh.transaction do
131
129
  assert dbh.in_transaction?
132
- 5.times { dbh.execute("insert into foo (bar) values (?)", 1) }
130
+ 5.times { dbh.execute_modification("insert into foo (bar) values (?)", 1) }
133
131
  assert_equal( [[1]] * 5, dbh.execute("select * from foo").fetch(:all) )
134
132
  dbh.commit
135
133
  assert ! dbh.in_transaction?
@@ -169,7 +167,7 @@ class TestDatabase < Test::Unit::TestCase
169
167
  def test_07_schema
170
168
  self.dbh = init_database
171
169
 
172
- dbh.execute( "insert into bar (foo, bar) values (?, ?)", "foo", 1 )
170
+ dbh.execute_modification( "insert into bar (foo, bar) values (?, ?)", "foo", 1 )
173
171
  res = dbh.execute( "select * from bar" )
174
172
 
175
173
  assert res
@@ -183,7 +181,7 @@ class TestDatabase < Test::Unit::TestCase
183
181
  self.dbh = init_database
184
182
 
185
183
  dt = DateTime.now
186
- dbh.execute( 'insert into time_test (my_date) values (?)', dt )
184
+ dbh.execute_modification( 'insert into time_test (my_date) values (?)', dt )
187
185
  dt2 = dbh.execute( 'select * from time_test limit 1' ).fetch(1)[0][0]
188
186
 
189
187
  assert_kind_of( DateTime, dt2 )
metadata CHANGED
@@ -1,13 +1,12 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rdbi-driver-postgresql
3
3
  version: !ruby/object:Gem::Version
4
- hash: 59
5
4
  prerelease: false
6
5
  segments:
7
6
  - 0
8
7
  - 9
9
- - 0
10
- version: 0.9.0
8
+ - 1
9
+ version: 0.9.1
11
10
  platform: ruby
12
11
  authors:
13
12
  - Pistos
@@ -16,7 +15,7 @@ autorequire:
16
15
  bindir: bin
17
16
  cert_chain: []
18
17
 
19
- date: 2010-08-21 00:00:00 -04:00
18
+ date: 2010-12-10 00:00:00 -05:00
20
19
  default_executable:
21
20
  dependencies:
22
21
  - !ruby/object:Gem::Dependency
@@ -27,7 +26,6 @@ dependencies:
27
26
  requirements:
28
27
  - - ">="
29
28
  - !ruby/object:Gem::Version
30
- hash: 3
31
29
  segments:
32
30
  - 0
33
31
  version: "0"
@@ -41,7 +39,6 @@ dependencies:
41
39
  requirements:
42
40
  - - ">="
43
41
  - !ruby/object:Gem::Version
44
- hash: 3
45
42
  segments:
46
43
  - 0
47
44
  version: "0"
@@ -55,7 +52,6 @@ dependencies:
55
52
  requirements:
56
53
  - - ">="
57
54
  - !ruby/object:Gem::Version
58
- hash: 3
59
55
  segments:
60
56
  - 0
61
57
  version: "0"
@@ -69,7 +65,6 @@ dependencies:
69
65
  requirements:
70
66
  - - ">="
71
67
  - !ruby/object:Gem::Version
72
- hash: 3
73
68
  segments:
74
69
  - 0
75
70
  version: "0"
@@ -83,7 +78,6 @@ dependencies:
83
78
  requirements:
84
79
  - - "="
85
80
  - !ruby/object:Gem::Version
86
- hash: 59
87
81
  segments:
88
82
  - 0
89
83
  - 9
@@ -99,7 +93,6 @@ dependencies:
99
93
  requirements:
100
94
  - - ">="
101
95
  - !ruby/object:Gem::Version
102
- hash: 3
103
96
  segments:
104
97
  - 0
105
98
  version: "0"
@@ -113,7 +106,6 @@ dependencies:
113
106
  requirements:
114
107
  - - ">="
115
108
  - !ruby/object:Gem::Version
116
- hash: 17
117
109
  segments:
118
110
  - 0
119
111
  - 3
@@ -136,6 +128,7 @@ files:
136
128
  - README.rdoc
137
129
  - Rakefile
138
130
  - VERSION
131
+ - lib/rdbi-driver-postgresql.rb
139
132
  - lib/rdbi/driver/postgresql.rb
140
133
  - rdbi-driver-postgresql.gemspec
141
134
  - test/helper.rb
@@ -145,8 +138,8 @@ homepage: http://github.com/Pistos/rdbi-dbd-postgresql
145
138
  licenses: []
146
139
 
147
140
  post_install_message:
148
- rdoc_options:
149
- - --charset=UTF-8
141
+ rdoc_options: []
142
+
150
143
  require_paths:
151
144
  - lib
152
145
  required_ruby_version: !ruby/object:Gem::Requirement
@@ -154,7 +147,6 @@ required_ruby_version: !ruby/object:Gem::Requirement
154
147
  requirements:
155
148
  - - ">="
156
149
  - !ruby/object:Gem::Version
157
- hash: 3
158
150
  segments:
159
151
  - 0
160
152
  version: "0"
@@ -163,7 +155,6 @@ required_rubygems_version: !ruby/object:Gem::Requirement
163
155
  requirements:
164
156
  - - ">="
165
157
  - !ruby/object:Gem::Version
166
- hash: 3
167
158
  segments:
168
159
  - 0
169
160
  version: "0"