m4dbi 0.8.8 → 0.8.9

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: d9b6b21860211863ed3ef697886bc4dd8d017743
4
+ data.tar.gz: 75fb2e328554c618b22c8919c05b3039d9a7f750
5
+ SHA512:
6
+ metadata.gz: 333d1c3056960225ab40c2d1db68e397637b8dad22265c1e604ddc20130720a4afa757fb7f25ef3912228db18a8a78a2ddc848f952c8a62e304817af7c61fcc0
7
+ data.tar.gz: c971fd38a928c3a4bae6f63e633344728bb1208dc0f5a99d1e9ec0fd12ba5e05cfd421e3aa04982dc9d6fd4471c46e993a62f5d57929922181d0220922a0db80
@@ -33,10 +33,12 @@ module M4DBI
33
33
  AND #{@the_many_model.pk_clause}
34
34
  }
35
35
  )
36
- stm.execute(
36
+ success = stm.execute(
37
37
  @the_one.pk,
38
38
  arg.pk
39
39
  ).affected_count > 0
40
+ stm.finish
41
+ success
40
42
  when Hash
41
43
  hash = arg
42
44
  keys = hash.keys
@@ -51,14 +53,16 @@ module M4DBI
51
53
  AND #{where_subclause}
52
54
  }
53
55
  )
54
- stm.execute(
56
+ num_deleted = stm.execute(
55
57
  @the_one.pk,
56
58
  *( keys.map { |k| hash[ k ] } )
57
59
  ).affected_count
60
+ stm.finish
61
+ num_deleted
58
62
  end
59
63
  end
60
64
 
61
- # Returns the number of records deleted
65
+ # @return the number of records deleted
62
66
  def clear
63
67
  stm = @the_many_model.dbh.prepare(
64
68
  %{
@@ -66,7 +70,9 @@ module M4DBI
66
70
  WHERE #{@the_one_fk} = ?
67
71
  }
68
72
  )
69
- stm.execute( @the_one.pk ).affected_count
73
+ num_deleted = stm.execute( @the_one.pk ).affected_count
74
+ stm.finish
75
+ num_deleted
70
76
  end
71
77
  end
72
78
  end
@@ -11,21 +11,34 @@ module M4DBI
11
11
  end
12
12
 
13
13
  def execute( *args )
14
- @dbh.execute *args
14
+ result = @dbh.execute(*args)
15
+ if defined?( RDBI::Driver::PostgreSQL ) && RDBI::Driver::PostgreSQL === @dbh.driver
16
+ result.finish
17
+ end
18
+ result
15
19
  end
16
20
 
17
21
  def select( sql, *bindvars )
18
- execute( sql, *bindvars ).fetch( :all, RDBI::Result::Driver::Struct )
22
+ result = @dbh.execute( sql, *bindvars )
23
+ rows = result.fetch( :all, RDBI::Result::Driver::Struct )
24
+ if defined?( RDBI::Driver::PostgreSQL ) && RDBI::Driver::PostgreSQL === @dbh.driver
25
+ result.finish
26
+ end
27
+ rows
19
28
  end
20
29
 
21
30
  def select_one( sql, *bindvars )
22
- select( sql, *bindvars )[ 0 ]
31
+ select( sql, *bindvars )[0]
23
32
  end
24
33
 
25
34
  def select_column( sql, *bindvars )
26
- rows = execute( sql, *bindvars ).fetch( 1, RDBI::Result::Driver::Array )
35
+ result = @dbh.execute( sql, *bindvars )
36
+ rows = result.fetch( 1, RDBI::Result::Driver::Array )
37
+ if defined?( RDBI::Driver::PostgreSQL ) && RDBI::Driver::PostgreSQL === @dbh.driver
38
+ result.finish
39
+ end
27
40
  if rows.any?
28
- rows[ 0 ][ 0 ]
41
+ rows[0][0]
29
42
  else
30
43
  raise RDBI::Error.new( "Query returned no rows. SQL: #{@dbh.last_query}" )
31
44
  end
data/lib/m4dbi/model.rb CHANGED
@@ -2,14 +2,14 @@ module M4DBI
2
2
  class Model
3
3
  #attr_reader :row
4
4
  ancestral_trait_reader :dbh, :table
5
- ancestral_trait_class_reader :dbh, :table, :pk, :columns, :st, :hooks
5
+ ancestral_trait_class_reader :dbh, :table, :pk, :columns, :hooks
6
6
 
7
7
  M4DBI_UNASSIGNED = '__m4dbi_unassigned__'
8
8
 
9
9
  extend Enumerable
10
10
 
11
11
  def self.prepare( sql )
12
- st[sql] ||= dbh.prepare(sql)
12
+ dbh.prepare(sql)
13
13
  end
14
14
 
15
15
  def self.[]( first_arg, *args )
@@ -32,6 +32,7 @@ module M4DBI
32
32
  sql = "SELECT * FROM #{table} WHERE #{clause}"
33
33
  stm = prepare(sql)
34
34
  row = stm.select_one(*values)
35
+ stm.finish
35
36
 
36
37
  if row
37
38
  self.new( row )
@@ -72,9 +73,11 @@ module M4DBI
72
73
  end
73
74
 
74
75
  stm = prepare(sql)
75
- self.from_rows(
76
+ rows = self.from_rows(
76
77
  stm.select_all(*params)
77
78
  )
79
+ stm.finish
80
+ rows
78
81
  end
79
82
 
80
83
  def self.one_where( conditions, *args )
@@ -89,6 +92,7 @@ module M4DBI
89
92
 
90
93
  stm = prepare(sql)
91
94
  row = stm.select_one( *params )
95
+ stm.finish
92
96
  if row
93
97
  self.new( row )
94
98
  end
@@ -96,7 +100,9 @@ module M4DBI
96
100
 
97
101
  def self.all
98
102
  stm = prepare("SELECT * FROM #{table}")
99
- self.from_rows( stm.select_all )
103
+ records = self.from_rows( stm.select_all )
104
+ stm.finish
105
+ records
100
106
  end
101
107
 
102
108
  # TODO: Perhaps we'll use cursors for Model#each.
@@ -107,6 +113,7 @@ module M4DBI
107
113
  def self.one
108
114
  stm = prepare("SELECT * FROM #{table} LIMIT 1")
109
115
  row = stm.select_one
116
+ stm.finish
110
117
  if row
111
118
  self.new( row )
112
119
  end
@@ -114,7 +121,9 @@ module M4DBI
114
121
 
115
122
  def self.count
116
123
  stm = prepare("SELECT COUNT(*) FROM #{table}")
117
- stm.select_column.to_i
124
+ retval = stm.select_column.to_i
125
+ stm.finish
126
+ retval
118
127
  end
119
128
 
120
129
  def self.create( hash = {} )
@@ -145,6 +154,7 @@ module M4DBI
145
154
  end
146
155
  stm = prepare(sql)
147
156
  num_inserted = stm.execute(*values).affected_count
157
+ stm.finish
148
158
  if num_inserted > 0
149
159
  pk_hash = hash.slice( *(
150
160
  self.pk.map { |pk_col| pk_col.to_sym }
@@ -199,14 +209,17 @@ module M4DBI
199
209
 
200
210
  def self.select_all( sql, *binds )
201
211
  stm = prepare(sql)
202
- self.from_rows(
212
+ records = self.from_rows(
203
213
  stm.select_all( *binds )
204
214
  )
215
+ stm.finish
216
+ records
205
217
  end
206
218
 
207
219
  def self.select_one( sql, *binds )
208
220
  stm = prepare(sql)
209
221
  row = stm.select_one( *binds )
222
+ stm.finish
210
223
  if row
211
224
  self.new( row )
212
225
  end
@@ -232,7 +245,9 @@ module M4DBI
232
245
  set_clause, set_params = set_hash.to_set_clause
233
246
  params = set_params + where_params
234
247
  stm = prepare("UPDATE #{table} SET #{set_clause} WHERE #{where_clause}")
235
- stm.execute( *params )
248
+ result = stm.execute( *params )
249
+ stm.finish
250
+ result
236
251
  end
237
252
 
238
253
  def self.update_one( *args )
@@ -240,7 +255,9 @@ module M4DBI
240
255
  pk_values = args[ 0..-2 ]
241
256
  params = set_params + pk_values
242
257
  stm = prepare("UPDATE #{table} SET #{set_clause} WHERE #{pk_clause}")
243
- stm.execute( *params )
258
+ result = stm.execute( *params )
259
+ stm.finish
260
+ result
244
261
  end
245
262
 
246
263
  def self.after_create(&block)
@@ -343,11 +360,10 @@ module M4DBI
343
360
  # warn "Do not call M4DBI::Model#new directly; use M4DBI::Model#create instead."
344
361
  # end
345
362
  @row = row
346
- @st = Hash.new
347
363
  end
348
364
 
349
365
  def prepare( sql )
350
- @st[sql] ||= dbh.prepare(sql)
366
+ dbh.prepare(sql)
351
367
  end
352
368
 
353
369
  def method_missing( method, *args )
@@ -420,6 +436,7 @@ module M4DBI
420
436
  state_before = self.to_h
421
437
  st = prepare("UPDATE #{table} SET #{set_clause} WHERE #{pk_clause}")
422
438
  num_updated = st.execute( *set_params ).affected_count
439
+ st.finish if defined?( RDBI::Driver::PostgreSQL ) && RDBI::Driver::PostgreSQL === dbh.driver
423
440
  if num_updated > 0
424
441
  hash.each do |key,value|
425
442
  @row[ key ] = value
@@ -447,6 +464,7 @@ module M4DBI
447
464
 
448
465
  st = prepare("DELETE FROM #{table} WHERE #{pk_clause}")
449
466
  num_deleted = st.execute( *pk_values ).affected_count
467
+ st.finish
450
468
  if num_deleted != 1
451
469
  false
452
470
  else
@@ -507,7 +525,6 @@ module M4DBI
507
525
  :table => table,
508
526
  :pk => pk_,
509
527
  :columns => h.table_schema( table.to_sym ).columns,
510
- :st => Hash.new, # prepared statements for all queries
511
528
  :hooks => {
512
529
  after_create: [],
513
530
  after_update: [],
@@ -563,6 +580,9 @@ module M4DBI
563
580
  new_value,
564
581
  *pk_values
565
582
  ).affected_count
583
+ if defined?( RDBI::Driver::PostgreSQL ) && RDBI::Driver::PostgreSQL === h.driver
584
+ stm.finish
585
+ end
566
586
  if num_changed > 0
567
587
  @row[ colname ] = new_value
568
588
  end
@@ -8,6 +8,10 @@ module M4DBI
8
8
  @st.execute *args
9
9
  end
10
10
 
11
+ def finish
12
+ @st.finish
13
+ end
14
+
11
15
  def select( *bindvars )
12
16
  @st.execute( *bindvars ).fetch( :all, RDBI::Result::Driver::Struct )
13
17
  end
data/lib/m4dbi/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module M4DBI
2
- VERSION = '0.8.8'
2
+ VERSION = '0.8.9'
3
3
  end
metadata CHANGED
@@ -1,61 +1,53 @@
1
- --- !ruby/object:Gem::Specification
1
+ --- !ruby/object:Gem::Specification
2
2
  name: m4dbi
3
- version: !ruby/object:Gem::Version
4
- hash: 47
5
- prerelease:
6
- segments:
7
- - 0
8
- - 8
9
- - 8
10
- version: 0.8.8
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.8.9
11
5
  platform: ruby
12
- authors:
6
+ authors:
13
7
  - Pistos
14
8
  autorequire:
15
9
  bindir: bin
16
10
  cert_chain: []
17
-
18
- date: 2013-05-13 00:00:00 Z
19
- dependencies:
20
- - !ruby/object:Gem::Dependency
11
+ date: 2013-06-14 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
21
14
  name: metaid
22
- prerelease: false
23
- requirement: &id001 !ruby/object:Gem::Requirement
24
- none: false
25
- requirements:
26
- - - ">="
27
- - !ruby/object:Gem::Version
28
- hash: 3
29
- segments:
30
- - 0
31
- version: "0"
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - '>='
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
32
20
  type: :runtime
33
- version_requirements: *id001
34
- - !ruby/object:Gem::Dependency
35
- name: rdbi
36
21
  prerelease: false
37
- requirement: &id002 !ruby/object:Gem::Requirement
38
- none: false
39
- requirements:
40
- - - ">="
41
- - !ruby/object:Gem::Version
42
- hash: 3
43
- segments:
44
- - 0
45
- version: "0"
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - '>='
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rdbi
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - '>='
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
46
34
  type: :runtime
47
- version_requirements: *id002
48
- description: M4DBI provides models, associations and some convenient extensions to RDBI.
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - '>='
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ description: M4DBI provides models, associations and some convenient extensions to
42
+ RDBI.
49
43
  email: m4dbi dot pistos at purepistos dot net
50
44
  executables: []
51
-
52
45
  extensions: []
53
-
54
- extra_rdoc_files:
46
+ extra_rdoc_files:
55
47
  - README.rdoc
56
48
  - CHANGELOG
57
49
  - LICENCE
58
- files:
50
+ files:
59
51
  - README.rdoc
60
52
  - CHANGELOG
61
53
  - LICENCE
@@ -76,38 +68,29 @@ files:
76
68
  - spec/hash.rb
77
69
  homepage: https://github.com/Pistos/m4dbi
78
70
  licenses: []
79
-
71
+ metadata: {}
80
72
  post_install_message:
81
73
  rdoc_options: []
82
-
83
- require_paths:
74
+ require_paths:
84
75
  - lib
85
- required_ruby_version: !ruby/object:Gem::Requirement
86
- none: false
87
- requirements:
88
- - - ">="
89
- - !ruby/object:Gem::Version
90
- hash: 3
91
- segments:
92
- - 0
93
- version: "0"
94
- required_rubygems_version: !ruby/object:Gem::Requirement
95
- none: false
96
- requirements:
97
- - - ">="
98
- - !ruby/object:Gem::Version
99
- hash: 3
100
- segments:
101
- - 0
102
- version: "0"
103
- requirements:
76
+ required_ruby_version: !ruby/object:Gem::Requirement
77
+ requirements:
78
+ - - '>='
79
+ - !ruby/object:Gem::Version
80
+ version: '0'
81
+ required_rubygems_version: !ruby/object:Gem::Requirement
82
+ requirements:
83
+ - - '>='
84
+ - !ruby/object:Gem::Version
85
+ version: '0'
86
+ requirements:
104
87
  - bacon (optional)
105
88
  rubyforge_project:
106
- rubygems_version: 1.8.24
89
+ rubygems_version: 2.0.3
107
90
  signing_key:
108
- specification_version: 3
91
+ specification_version: 4
109
92
  summary: Models (and More) for RDBI
110
- test_files:
93
+ test_files:
111
94
  - spec/database.rb
112
95
  - spec/model.rb
113
96
  - spec/helper.rb