jdbc-helper 0.4.4 → 0.4.5

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.
data/README.rdoc CHANGED
@@ -6,13 +6,18 @@ crucial database operations from primitive selects and updates to more complex o
6
6
  batch updates, prepared statements and transactions.
7
7
  As the name implies, this gem only works on JRuby.
8
8
 
9
- == Examples
9
+ == Installation
10
+ === Install gem
11
+ gem install jdbc-helper
10
12
 
11
- === Prerequisites
12
- Add JDBC driver of the DBMS you're willing to use to your CLASSPATH
13
- export CLASSPATH=$CLASSPATH:~/lib/mysql-connector-java.jar
13
+ === Setting up CLASSPATH
14
+ Add the appropriate JDBC drivers to the CLASSPATH.
15
+ export CLASSPATH=$CLASSPATH:~/lib/mysql-connector-java-5.1.16-bin.jar:~/lib/ojdbc6.jar
14
16
 
17
+ === In Ruby
18
+ require 'jdbc-helper'
15
19
 
20
+ == Examples
16
21
  === Connecting to a database
17
22
 
18
23
  # :driver and :url must be given
@@ -35,9 +40,13 @@ Add JDBC driver of the DBMS you're willing to use to your CLASSPATH
35
40
  conn = JDBCHelper::MySQLConnector.connect('localhost', 'mysql', '', 'test')
36
41
  conn.close
37
42
 
43
+ # Oracle shortcut connector
44
+ conn = JDBCHelper::OracleConnector.connect(host, user, password, service_name)
45
+ conn.close
46
+
38
47
  === Querying database table
39
48
 
40
- conn.query("SELECT a, b, c FROM T") do | row |
49
+ conn.query("SELECT a, b, c FROM T") do |row|
41
50
  row.labels
42
51
  row.rownum
43
52
 
@@ -55,8 +64,8 @@ Add JDBC driver of the DBMS you're willing to use to your CLASSPATH
55
64
  uniq_rows = rows.uniq
56
65
 
57
66
  # You can even nest queries
58
- conn.query("SELECT a FROM T") do | row1 |
59
- conn.query("SELECT * FROM T_#{row1.a}") do | row2 |
67
+ conn.query("SELECT a FROM T") do |row1|
68
+ conn.query("SELECT * FROM T_#{row1.a}") do |row2|
60
69
  # ...
61
70
  end
62
71
  end
@@ -65,7 +74,7 @@ Add JDBC driver of the DBMS you're willing to use to your CLASSPATH
65
74
  # When the result set of the query is expected to be large and you wish to
66
75
  # chain enumerators, `enumerate' is much preferred over `query'. (which returns the
67
76
  # array of the entire rows)
68
- conn.enumerate("SELECT * FROM LARGE_T").each_slice(1000) do | slice |
77
+ conn.enumerate("SELECT * FROM LARGE_T").each_slice(1000) do |slice|
69
78
  slice.each do | row |
70
79
  # ...
71
80
  end
@@ -75,7 +84,7 @@ Add JDBC driver of the DBMS you're willing to use to your CLASSPATH
75
84
  del_count = conn.update("DELETE FROM T")
76
85
 
77
86
  === Transaction
78
- committed = conn.transaction do | tx |
87
+ committed = conn.transaction do |tx|
79
88
  # ...
80
89
  # Transaction logic here
81
90
  # ...
@@ -96,14 +105,14 @@ Add JDBC driver of the DBMS you're willing to use to your CLASSPATH
96
105
 
97
106
  === Using prepared statements
98
107
  p_sel = conn.prepare("SELECT * FROM T WHERE b = ? and c = ?")
99
- p_sel.query(100, 200) do | row |
108
+ p_sel.query(100, 200) do |row|
100
109
  p row
101
110
  end
102
111
  p_sel.close
103
112
 
104
113
  p_upd = conn.prepare("UPDATE T SET a = ? WHERE b = ?")
105
114
  count = 0
106
- 100.times do | i |
115
+ 100.times do |i|
107
116
  count += p_upd.update('updated a', i)
108
117
  end
109
118
 
@@ -114,68 +123,80 @@ Add JDBC driver of the DBMS you're willing to use to your CLASSPATH
114
123
  p_upd.close
115
124
 
116
125
  === Using table wrappers (since 0.2.0)
117
- # For more complex examples, refer to test/test_object_wrapper.rb
126
+ # For more complex examples, refer to test/test_object_wrapper.rb
118
127
 
119
- # Creates a table wrapper
120
- table = conn.table('test.data')
128
+ # Creates a table wrapper
129
+ table = conn.table('test.data')
121
130
 
122
- # Counting the records in the table
123
- table.count
124
- table.count(:a => 10)
125
- table.where(:a => 10).count
131
+ # Counting the records in the table
132
+ table.count
133
+ table.count(:a => 10)
134
+ table.where(:a => 10).count
126
135
 
127
- table.empty?
128
- table.where(:a => 10).empty?
136
+ table.empty?
137
+ table.where(:a => 10).empty?
129
138
 
130
- # Selects the table by combining select, where, and order methods
131
- table.select('a apple', :b).where(:c => (1..10)).order('b desc', 'a asc') do |row|
132
- puts row.apple
133
- end
139
+ # Selects the table by combining select, where, and order methods
140
+ table.select('a apple', :b).where(:c => (1..10)).order('b desc', 'a asc') do |row|
141
+ puts row.apple
142
+ end
143
+
144
+ # Build select SQL
145
+ sql = table.select('a apple', :b).where(:c => (1..10)).order('b desc', 'a asc').sql
134
146
 
135
- # Build select SQL
136
- sql = table.select('a apple', :b).where(:c => (1..10)).order('b desc', 'a asc').sql
147
+ # Updates with conditions
148
+ table.update(:a => 'hello', :b => JDBCHelper::SQL('now()'), :where => { :c => 3 })
149
+ # Or equivalently,
150
+ table.where(:c => 3).update(:a => 'hello', :b => JDBCHelper::SQL('now()'))
137
151
 
138
- # Updates with conditions
139
- table.update(:a => 'hello', :b => JDBCHelper::SQL('now()'), :where => { :c => 3 })
140
- # Or equivalently,
141
- table.where(:c => 3).update(:a => 'hello', :b => JDBCHelper::SQL('now()'))
152
+ # Insert into the table
153
+ table.insert(:a => 10, :b => 20, :c => JDBCHelper::SQL('10 + 20'))
154
+ table.insert_ignore(:a => 10, :b => 20, :c => 30)
155
+ table.replace(:a => 10, :b => 20, :c => 30)
142
156
 
143
- # Insert into the table
144
- table.insert(:a => 10, :b => 20, :c => JDBCHelper::SQL('10 + 20'))
145
- table.insert_ignore(:a => 10, :b => 20, :c => 30)
146
- table.replace(:a => 10, :b => 20, :c => 30)
157
+ # Update with common default values
158
+ with_defaults = table.default(:a => 10, :b => 20)
159
+ with_defaults.insert(:c => 30)
160
+ with_defaults.where('a != 10 or b != 20').update # sets a => 10, b => 20
147
161
 
148
- # Batch updates with batch method
149
- table.batch.insert(:a => 10, :b => 20, :c => JDBCHelper::SQL('10 + 20'))
150
- table.batch.insert_ignore(:a => 10, :b => 20, :c => 30)
151
- conn.execute_batch
162
+ # Batch updates with batch method
163
+ table.batch.insert(:a => 10, :b => 20, :c => JDBCHelper::SQL('10 + 20'))
164
+ table.batch.insert_ignore(:a => 10, :b => 20, :c => 30)
165
+ conn.execute_batch
152
166
 
153
- # Delete with conditions
154
- table.delete(:c => 3)
155
- # Or equivalently,
156
- table.where(:c => 3).delete
167
+ # Delete with conditions
168
+ table.delete(:c => 3)
169
+ # Or equivalently,
170
+ table.where(:c => 3).delete
157
171
 
158
- # Truncate or drop table (Cannot be undone)
159
- table.truncate!
160
- table.drop!
172
+ # Truncate or drop table (Cannot be undone)
173
+ table.truncate!
174
+ table.drop!
161
175
 
162
176
  === Using function wrappers (since 0.2.2)
163
- conn.function(:mod).call 5, 3
164
- conn.function(:coalesce).call(nil, nil, 'king')
177
+ conn.function(:mod).call 5, 3
178
+ conn.function(:coalesce).call(nil, nil, 'king')
165
179
 
166
180
  === Using procedure wrappers (since 0.3.0)
167
- # Working with IN/INOUT/OUT parameteres
168
- # Bind by ordinal number
169
- conn.procedure(:update_and_fetch_something).call(
181
+ # Working with IN/INOUT/OUT parameteres
182
+ # Bind by ordinal number
183
+ conn.procedure(:update_and_fetch_something).call(
170
184
  100, # Input parameter
171
185
  ["value", String], # Input/Output parameter
172
186
  Fixnum # Output parameter
173
- )
187
+ )
174
188
 
175
- # Bind by parameter name
176
- conn.procedure(:update_and_fetch_something).call(
189
+ # Bind by parameter name
190
+ conn.procedure(:update_and_fetch_something).call(
177
191
  :a => 100, :b => ["value", String], :c => Fixnum)
178
192
 
193
+ === Using sequence wrappers (since 0.4.2)
194
+ seq = conn.sequence(:my_seq)
195
+ next = seq.nextval
196
+ curr = seq.currval
197
+ seq.reset!
198
+ seq.reset! 100
199
+
179
200
  == Contributing to jdbc-helper
180
201
 
181
202
  * Check out the latest master to make sure the feature hasn't been implemented or the bug hasn't been fixed yet
@@ -50,7 +50,7 @@ class TableWrapper < ObjectWrapper
50
50
  # @param [List of Hash/String] where Filter conditions
51
51
  # @return [Fixnum] Count of the records.
52
52
  def count *where
53
- @connection.query(JDBCHelper::SQL.count name, merge_where(self, where))[0][0].to_i
53
+ @connection.query(JDBCHelper::SQL.count(name, @query_where + where))[0][0].to_i
54
54
  end
55
55
 
56
56
  # Sees if the table is empty
@@ -63,8 +63,8 @@ class TableWrapper < ObjectWrapper
63
63
  # Inserts a record into the table with the given hash
64
64
  # @param [Hash] data_hash Column values in Hash
65
65
  # @return [Fixnum] Number of affected records
66
- def insert data_hash
67
- @connection.send @update_method, JDBCHelper::SQL.insert(name, data_hash)
66
+ def insert data_hash = {}
67
+ @connection.send @update_method, JDBCHelper::SQL.insert(name, @query_default.merge(data_hash))
68
68
  end
69
69
 
70
70
  # Inserts a record into the table with the given hash.
@@ -72,16 +72,16 @@ class TableWrapper < ObjectWrapper
72
72
  # @note This is not SQL standard. Only works if the database supports insert ignore syntax.
73
73
  # @param [Hash] data_hash Column values in Hash
74
74
  # @return [Fixnum] Number of affected records
75
- def insert_ignore data_hash
76
- @connection.send @update_method, JDBCHelper::SQL.insert_ignore(name, data_hash)
75
+ def insert_ignore data_hash = {}
76
+ @connection.send @update_method, JDBCHelper::SQL.insert_ignore(name, @query_default.merge(data_hash))
77
77
  end
78
78
 
79
79
  # Replaces a record in the table with the new one with the same unique key.
80
80
  # @note This is not SQL standard. Only works if the database supports replace syntax.
81
81
  # @param [Hash] data_hash Column values in Hash
82
82
  # @return [Fixnum] Number of affected records
83
- def replace data_hash
84
- @connection.send @update_method, JDBCHelper::SQL.replace(name, data_hash)
83
+ def replace data_hash = {}
84
+ @connection.send @update_method, JDBCHelper::SQL.replace(name, @query_default.merge(data_hash))
85
85
  end
86
86
 
87
87
  # Executes update with the given hash.
@@ -89,19 +89,18 @@ class TableWrapper < ObjectWrapper
89
89
  # @param [Hash] data_hash_with_where Column values in Hash.
90
90
  # :where element of the given hash can (usually should) point to another Hash representing update filters.
91
91
  # @return [Fixnum] Number of affected records
92
- def update data_hash_with_where
92
+ def update data_hash_with_where = {}
93
93
  where_ext = data_hash_with_where.delete(:where)
94
94
  where_ext = [where_ext] unless where_ext.is_a? Array
95
- where = merge_where(self, where_ext.compact)
96
- @connection.send @update_method, JDBCHelper::SQL.update(name, data_hash_with_where, where)
95
+ @connection.send @update_method,
96
+ JDBCHelper::SQL.update(name, @query_default.merge(data_hash_with_where), @query_where + where_ext.compact)
97
97
  end
98
98
 
99
99
  # Deletes records matching given condtion
100
100
  # @param [List of Hash/String] where Delete filters
101
101
  # @return [Fixnum] Number of affected records
102
102
  def delete *where
103
- where = merge_where(self, where)
104
- @connection.send @update_method, JDBCHelper::SQL.delete(name, where)
103
+ @connection.send @update_method, JDBCHelper::SQL.delete(name, @query_where + where)
105
104
  end
106
105
 
107
106
  # Empties the table.
@@ -128,7 +127,7 @@ class TableWrapper < ObjectWrapper
128
127
  # Returns a new TableWrapper object which can be used to execute a select
129
128
  # statement for the table selecting only the specified fields.
130
129
  # If a block is given, executes the select statement and yields each row to the block.
131
- # @return [*String/*Symbol] List of fields to select
130
+ # @param [*String/*Symbol] fields List of fields to select
132
131
  # @return [JDBCHelper::TableWrapper]
133
132
  # @since 0.4.0
134
133
  def select *fields, &block
@@ -140,21 +139,21 @@ class TableWrapper < ObjectWrapper
140
139
  # Returns a new TableWrapper object which can be used to execute a select
141
140
  # statement for the table with the specified filter conditions.
142
141
  # If a block is given, executes the select statement and yields each row to the block.
143
- # @param [List of Hash/String] Filter conditions
142
+ # @param [List of Hash/String] conditions Filter conditions
144
143
  # @return [JDBCHelper::TableWrapper]
145
144
  # @since 0.4.0
146
145
  def where *conditions, &block
147
146
  raise ArgumentError.new("Wrong number of arguments") if conditions.empty?
148
147
 
149
148
  obj = self.dup
150
- merge_where(obj, conditions, true)
149
+ obj.instance_variable_set :@query_where, @query_where + conditions
151
150
  ret obj, &block
152
151
  end
153
152
 
154
153
  # Returns a new TableWrapper object which can be used to execute a select
155
154
  # statement for the table with the given sorting criteria.
156
155
  # If a block is given, executes the select statement and yields each row to the block.
157
- # @param [*String/*Symbol] Sorting criteria
156
+ # @param [*String/*Symbol] criteria Sorting criteria
158
157
  # @return [JDBCHelper::TableWrapper]
159
158
  # @since 0.4.0
160
159
  def order *criteria, &block
@@ -164,6 +163,19 @@ class TableWrapper < ObjectWrapper
164
163
  ret obj, &block
165
164
  end
166
165
 
166
+ # Returns a new TableWrapper object with default values, which will be applied to
167
+ # the subsequent inserts and updates.
168
+ # @param [Hash] data_hash Default values
169
+ # @return [JDBCHelper::TableWrapper]
170
+ # @since 0.4.5
171
+ def default data_hash
172
+ raise ArgumentError.new("Hash required") unless data_hash.kind_of? Hash
173
+
174
+ obj = self.dup
175
+ obj.instance_variable_set :@query_default, @query_default.merge(data_hash)
176
+ obj
177
+ end
178
+
167
179
  # Executes a select SQL for the table and returns an Enumerable object,
168
180
  # or yields each row if block is given.
169
181
  # @return [JDBCHelper::Connection::ResultSetEnumerator]
@@ -209,6 +221,8 @@ class TableWrapper < ObjectWrapper
209
221
  def initialize connection, table_name
210
222
  super connection, table_name
211
223
  @update_method = :update
224
+ @query_default = {}
225
+ @query_where = []
212
226
  end
213
227
  private
214
228
  def ret obj, &block
@@ -218,12 +232,6 @@ private
218
232
  obj
219
233
  end
220
234
  end
221
-
222
- def merge_where obj, where, apply = false
223
- merged = (obj.instance_variable_get(:@query_where) || []) + (where || [])
224
- obj.instance_variable_set :@query_where, merged if apply
225
- merged
226
- end
227
235
  end#TableWrapper
228
236
  end#JDBCHelper
229
237
 
@@ -72,12 +72,15 @@ class TestObjectWrapper < Test::Unit::TestCase
72
72
 
73
73
  def insert table, cnt = 100
74
74
  params = {
75
- :alpha => 100,
75
+ :alpha => 100,
76
76
  :beta => JDBCHelper::SQL('0.1 + 0.2'),
77
- :gamma => 'hello world' }
77
+ }
78
78
 
79
79
  (1..cnt).each do |pk|
80
- icnt = table.insert(params.merge(:id => pk))
80
+ icnt = table.
81
+ default(:gamma => 'hello world').
82
+ default(:alpha => 200).
83
+ insert(params.merge(:id => pk))
81
84
  assert_equal 1, icnt unless table.batch?
82
85
  end
83
86
  end
@@ -334,7 +337,13 @@ class TestObjectWrapper < Test::Unit::TestCase
334
337
 
335
338
  assert_equal 10, table.update(:beta => 0, :where => { :id => (1..10) })
336
339
  assert_equal 2, table.where(:id => (55..56)).update(:beta => 0, :where => { :id => (51..60) })
337
- assert_equal 10, table.where(:id => (11..20)).update(:beta => 0)
340
+ assert_equal 10, table.where(:id => (11..20)).update(:beta => 1)
341
+
342
+ with_default = table.default(:beta => 0)
343
+ assert_equal 5, with_default.where(:id => (11..15)).update
344
+ assert_equal 5, with_default.where(:id => (16..20)).update
345
+ with_default = table.default(:beta => 1)
346
+ assert_equal 5, with_default.where(:id => (16..20)).update(:beta => 0) # override
338
347
  assert_equal 22, table.count(:beta => 0)
339
348
  assert_equal 100, table.update(:beta => 1)
340
349
  end
metadata CHANGED
@@ -2,7 +2,7 @@
2
2
  name: jdbc-helper
3
3
  version: !ruby/object:Gem::Version
4
4
  prerelease:
5
- version: 0.4.4
5
+ version: 0.4.5
6
6
  platform: ruby
7
7
  authors:
8
8
  - Junegunn Choi
@@ -10,7 +10,7 @@ autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
12
 
13
- date: 2011-06-09 00:00:00 +09:00
13
+ date: 2011-06-10 00:00:00 +09:00
14
14
  default_executable:
15
15
  dependencies:
16
16
  - !ruby/object:Gem::Dependency