jdbc-helper 0.5.1 → 0.6.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -7,19 +7,19 @@ module JDBCHelper
7
7
  # @example Usage
8
8
  # conn.function(:coalesce).call(nil, nil, 'king')
9
9
  class FunctionWrapper < ObjectWrapper
10
- # Returns the name of the function
11
- # @return [String]
12
- alias to_s name
10
+ # Returns the name of the function
11
+ # @return [String]
12
+ alias to_s name
13
13
 
14
- # Returns the result of the function call with the given parameters
15
- def call(*args)
16
- pstmt = @connection.prepare("select #{name}(#{args.map{'?'}.join ','}) from dual")
17
- begin
18
- pstmt.query(*args)[0][0]
19
- ensure
20
- pstmt.close
21
- end
22
- end
14
+ # Returns the result of the function call with the given parameters
15
+ def call(*args)
16
+ pstmt = @connection.prepare("select #{name}(#{args.map{'?'}.join ','}) from dual")
17
+ begin
18
+ pstmt.query(*args)[0][0]
19
+ ensure
20
+ pstmt.close
21
+ end
22
+ end
23
23
  end#FunctionWrapper
24
24
  end#JDBCHelper
25
25
 
@@ -6,26 +6,26 @@ module JDBCHelper
6
6
  # @abstract
7
7
  # @since 0.2.0
8
8
  class ObjectWrapper
9
- # Underlying JDBCHelper::Connection object
10
- # @return [JDBCHelper::Connection]
11
- attr_reader :connection
9
+ # Underlying JDBCHelper::Connection object
10
+ # @return [JDBCHelper::Connection]
11
+ attr_reader :connection
12
12
 
13
- # Object name (or expression)
14
- # @return [String]
15
- attr_reader :name
13
+ # Object name (or expression)
14
+ # @return [String]
15
+ attr_reader :name
16
16
 
17
- # Base constructor.
18
- # @param [JDBCHelper::Connection] conn JDBCHelper::Connection object
19
- # @param [String/Symbol] name Name of the object to be wrapped
20
- def initialize(conn, name)
21
- raise Exception.new(
22
- "JDBCHelper::ObjectWrapper is an abstract class") if
23
- self.instance_of? ObjectWrapper
17
+ # Base constructor.
18
+ # @param [JDBCHelper::Connection] conn JDBCHelper::Connection object
19
+ # @param [String/Symbol] name Name of the object to be wrapped
20
+ def initialize(conn, name)
21
+ raise NotImplementedError.new(
22
+ "JDBCHelper::ObjectWrapper is an abstract class") if
23
+ self.instance_of? ObjectWrapper
24
24
 
25
- @connection = conn
26
- @name = name.to_s
27
- JDBCHelper::SQL.check @name, true
28
- end
25
+ @connection = conn
26
+ @name = name.to_s
27
+ JDBCHelper::SQL.check @name, true
28
+ end
29
29
  end#ObjectWrapper
30
30
  end#JDBCHelper
31
31
 
@@ -7,131 +7,128 @@ module JDBCHelper
7
7
  # @example Usage
8
8
  # conn.procedure(:my_procedure).call(1, ["a", String], Fixnum)
9
9
  class ProcedureWrapper < ObjectWrapper
10
- # Returns the name of the procedure
11
- # @return [String]
12
- alias to_s name
13
-
14
- # Executes the procedure and returns the values of INOUT & OUT parameters in Hash
15
- # @return [Hash]
16
- def call(*args)
17
- params = build_params args
18
- cstmt = @connection.prepare_call "{call #{name}(#{Array.new(@cols.length){'?'}.join ', '})}"
19
- begin
20
- process_result( args, cstmt.call(*params) )
21
- ensure
22
- cstmt.close
23
- end
24
- end
25
-
26
- # Reloads procedure metadata. Metadata is cached for performance.
27
- # However, if you have modified the procedure, you need to reload the
28
- # metadata with this method.
29
- # @return [JDBCHelper::ProcedureWrapper]
30
- def refresh
31
- @cols = @defaults = nil
32
- self
33
- end
10
+ # Returns the name of the procedure
11
+ # @return [String]
12
+ alias to_s name
13
+
14
+ # Executes the procedure and returns the values of INOUT & OUT parameters in Hash
15
+ # @return [Hash]
16
+ def call(*args)
17
+ params = build_params args
18
+ cstmt = @connection.prepare_call "{call #{name}(#{Array.new(@cols.length){'?'}.join ', '})}"
19
+ begin
20
+ process_result( args, cstmt.call(*params) )
21
+ ensure
22
+ cstmt.close
23
+ end
24
+ end
25
+
26
+ # Reloads procedure metadata. Metadata is cached for performance.
27
+ # However, if you have modified the procedure, you need to reload the
28
+ # metadata with this method.
29
+ # @return [JDBCHelper::ProcedureWrapper]
30
+ def refresh
31
+ @cols = @defaults = nil
32
+ self
33
+ end
34
34
 
35
35
  private
36
- def metadata_lookup
37
- return if @cols
38
-
39
- procedure, package, schema = name.split('.').reverse
40
- procedure_u, package_u, schema_u = name.upcase.split('.').reverse
41
-
42
- # Alas, metadata lookup can be case-sensitive. e.g. Oracle
43
- dbmd = @connection.java_obj.get_meta_data
44
- lookups =
45
- if schema
46
- [
47
- lambda { dbmd.getProcedureColumns(package, schema, procedure, nil) },
48
- lambda { dbmd.getProcedureColumns(package_u, schema_u, procedure_u, nil) }
49
- ]
50
- else
51
- # schema or catalog? we don't know.
52
- # - too inefficient for parameter-less procedures
53
- [ lambda { dbmd.getProcedureColumns(nil, package, procedure, nil) },
54
- lambda { dbmd.getProcedureColumns(nil, package_u, procedure_u, nil) },
55
- lambda { dbmd.getProcedureColumns(package, nil, procedure, nil) },
56
- lambda { dbmd.getProcedureColumns(package_u, nil, procedure_u, nil) },
57
- ]
58
- end
59
-
60
- cols = []
61
- @defaults = {}
62
- lookups.each do |ld|
63
- rset = ld.call
64
- default_support = rset.get_meta_data.get_column_count >= 14
65
- while rset.next
66
- next if rset.getString("COLUMN_TYPE") == Java::JavaSql::DatabaseMetaData.procedureColumnReturn
67
-
68
- cols << rset.getString("COLUMN_NAME").upcase
69
- # TODO/NOT TESTED
70
- # - MySQL doesn't support default values for procedure parameters
71
- # - Oracle supports default values, however it does not provide
72
- # standard interface to query default values. COLUMN_DEF always returns null.
73
- # http://download.oracle.com/docs/cd/E11882_01/appdev.112/e13995/oracle/jdbc/OracleDatabaseMetaData.html#getProcedureColumns_java_lang_String__java_lang_String__java_lang_String__java_lang_String_
74
- if default_support && (default = rset.getString("COLUMN_DEF"))
75
- @defaults[cols.length - 1] << default
76
- end
77
- end
78
- unless cols.empty?
79
- @cols = cols
80
- break
81
- end
82
- end
83
- @cols ||= []
84
- end
85
-
86
- def build_params args
87
- metadata_lookup
88
-
89
- params = []
90
- # Array
91
- unless args.first.kind_of?(Hash)
92
- if args.length < @cols.length
93
- # Fill the Array with default values
94
- @cols.length.times do |idx|
95
- if @defaults[idx]
96
- params << @defaults[idx]
97
- else
98
- params << args[idx]
99
- end
100
- end
101
- else
102
- params = args
103
- end
104
-
105
- # Hash
106
- else
107
- raise ArgumentError.new("More than one Hash given") if args.length > 1
108
-
109
- # Set to default,
110
- @defaults.each do |idx, v|
111
- params[idx] = v
112
- end
113
-
114
- # then override
115
- args.first.each do |k, v|
116
- idx = @cols.index k.to_s.upcase
117
- params[idx] = v
118
- end
119
- end
120
-
121
- return params
122
- end
123
-
124
- def process_result args, result
125
- input = args.first
126
- return result unless input.kind_of? Hash
127
-
128
- final = {}
129
- result.each do |idx, ret|
130
- key = input.keys.find { |key| key.to_s.upcase == @cols[idx - 1] }
131
- final[key] = ret
132
- end
133
- final
134
- end
36
+ def metadata_lookup
37
+ return if @cols
38
+
39
+ procedure, package, schema = name.split('.').reverse
40
+ procedure_u, package_u, schema_u = name.upcase.split('.').reverse
41
+
42
+ # Alas, metadata lookup can be case-sensitive. e.g. Oracle
43
+ dbmd = @connection.java_obj.get_meta_data
44
+ lookups =
45
+ if schema
46
+ [ lambda { dbmd.getProcedureColumns(package, schema, procedure, nil) },
47
+ lambda { dbmd.getProcedureColumns(package_u, schema_u, procedure_u, nil) } ]
48
+ else
49
+ # schema or catalog? we don't know.
50
+ # - too inefficient for parameter-less procedures
51
+ [ lambda { dbmd.getProcedureColumns(nil, package, procedure, nil) },
52
+ lambda { dbmd.getProcedureColumns(nil, package_u, procedure_u, nil) },
53
+ lambda { dbmd.getProcedureColumns(package, nil, procedure, nil) },
54
+ lambda { dbmd.getProcedureColumns(package_u, nil, procedure_u, nil) } ]
55
+ end
56
+
57
+ cols = []
58
+ @defaults = {}
59
+ lookups.each do |ld|
60
+ rset = ld.call
61
+ default_support = rset.get_meta_data.get_column_count >= 14
62
+ while rset.next
63
+ next if rset.getString("COLUMN_TYPE") == java.sql.DatabaseMetaData.procedureColumnReturn
64
+
65
+ cols << rset.getString("COLUMN_NAME").upcase
66
+ # TODO/NOT TESTED
67
+ # - MySQL doesn't support default values for procedure parameters
68
+ # - Oracle supports default values, however it does not provide
69
+ # standard interface to query default values. COLUMN_DEF always returns null.
70
+ # http://download.oracle.com/docs/cd/E11882_01/appdev.112/e13995/oracle/jdbc/OracleDatabaseMetaData.html#getProcedureColumns_java_lang_String__java_lang_String__java_lang_String__java_lang_String_
71
+ if default_support && (default = rset.getString("COLUMN_DEF"))
72
+ @defaults[cols.length - 1] << default
73
+ end
74
+ end
75
+ unless cols.empty?
76
+ @cols = cols
77
+ break
78
+ end
79
+ end
80
+ @cols ||= []
81
+ end
82
+
83
+ def build_params args
84
+ metadata_lookup
85
+
86
+ params = []
87
+ # Array
88
+ unless args.first.kind_of?(Hash)
89
+ if args.length < @cols.length
90
+ # Fill the Array with default values
91
+ @cols.length.times do |idx|
92
+ if @defaults[idx]
93
+ params << @defaults[idx]
94
+ else
95
+ params << args[idx]
96
+ end
97
+ end
98
+ else
99
+ params = args
100
+ end
101
+
102
+ # Hash
103
+ else
104
+ raise ArgumentError.new("More than one Hash given") if args.length > 1
105
+
106
+ # Set to default,
107
+ @defaults.each do |idx, v|
108
+ params[idx] = v
109
+ end
110
+
111
+ # then override
112
+ args.first.each do |k, v|
113
+ idx = @cols.index k.to_s.upcase
114
+ params[idx] = v
115
+ end
116
+ end
117
+
118
+ return params
119
+ end
120
+
121
+ def process_result args, result
122
+ input = args.first
123
+ return result unless input.kind_of? Hash
124
+
125
+ final = {}
126
+ result.each do |idx, ret|
127
+ key = input.keys.find { |key| key.to_s.upcase == @cols[idx - 1] }
128
+ final[key] = ret
129
+ end
130
+ final
131
+ end
135
132
  end#ProcedureWrapper
136
133
  end#JDBCHelper
137
134
 
@@ -11,49 +11,49 @@ module JDBCHelper
11
11
  # conn.sequence(:my_seq).reset!(1)
12
12
  # conn.sequence(:my_seq).drop!
13
13
  class SequenceWrapper < ObjectWrapper
14
- # Returns the name of the sequence
15
- # @return [String]
16
- alias to_s name
17
-
18
- # Increments the sequence and returns the value
19
- # @return [Fixnum]
20
- def nextval
21
- @connection.query("select #{name}.nextval from dual")[0][0].to_i
22
- end
23
-
24
- # Returns the incremented value of the sequence
25
- # @return [Fixnum]
26
- def currval
27
- @connection.query("select #{name}.currval from dual")[0][0].to_i
28
- end
29
-
30
- # Recreates the sequence. Cannot be undone.
31
- # @param [Fixnum] value Initial value
32
- # @param [Fixnum] increment_by Increment size for a single nextval call
33
- # @return [JDBCHelper::SequenceWrapper] Self.
34
- def reset! value = 1, increment_by = 1
35
- drop! rescue nil
36
- create! value, increment_by
37
- self
38
- end
39
-
40
- # Drops the sequence. Cannot be undone.
41
- # @return [JDBCHelper::SequenceWrapper] Self.
42
- def drop!
43
- @connection.update("drop sequence #{name}")
44
- self
45
- end
46
-
47
- # Creates the sequence. Cannot be undone.
48
- # @param [Fixnum] value Initial value
49
- # @param [Fixnum] increment_by Increment size for a single nextval call
50
- # @return [JDBCHelper::SequenceWrapper] Self.
51
- def create! value = 1, increment_by = 1
52
- create = JDBCHelper::SQL.check(
53
- "create sequence #{name} start with #{value} increment by #{increment_by}")
54
- @connection.update(create)
55
- self
56
- end
14
+ # Returns the name of the sequence
15
+ # @return [String]
16
+ alias to_s name
17
+
18
+ # Increments the sequence and returns the value
19
+ # @return [Fixnum]
20
+ def nextval
21
+ @connection.query("select #{name}.nextval from dual")[0][0].to_i
22
+ end
23
+
24
+ # Returns the incremented value of the sequence
25
+ # @return [Fixnum]
26
+ def currval
27
+ @connection.query("select #{name}.currval from dual")[0][0].to_i
28
+ end
29
+
30
+ # Recreates the sequence. Cannot be undone.
31
+ # @param [Fixnum] value Initial value
32
+ # @param [Fixnum] increment_by Increment size for a single nextval call
33
+ # @return [JDBCHelper::SequenceWrapper] Self.
34
+ def reset! value = 1, increment_by = 1
35
+ drop! rescue nil
36
+ create! value, increment_by
37
+ self
38
+ end
39
+
40
+ # Drops the sequence. Cannot be undone.
41
+ # @return [JDBCHelper::SequenceWrapper] Self.
42
+ def drop!
43
+ @connection.update("drop sequence #{name}")
44
+ self
45
+ end
46
+
47
+ # Creates the sequence. Cannot be undone.
48
+ # @param [Fixnum] value Initial value
49
+ # @param [Fixnum] increment_by Increment size for a single nextval call
50
+ # @return [JDBCHelper::SequenceWrapper] Self.
51
+ def create! value = 1, increment_by = 1
52
+ create = JDBCHelper::SQL.check(
53
+ "create sequence #{name} start with #{value} increment by #{increment_by}")
54
+ @connection.update(create)
55
+ self
56
+ end
57
57
  end#SequenceWrapper
58
58
  end#JDBCHelper
59
59
 
@@ -42,207 +42,207 @@ module JDBCHelper
42
42
  # table.truncate_table!
43
43
  # table.drop_table!
44
44
  class TableWrapper < ObjectWrapper
45
- # Returns the name of the table
46
- # @return [String]
47
- alias to_s name
45
+ # Returns the name of the table
46
+ # @return [String]
47
+ alias to_s name
48
48
 
49
- # Retrieves the count of the table
50
- # @param [List of Hash/String] where Filter conditions
51
- # @return [Fixnum] Count of the records.
52
- def count *where
53
- sql, binds = JDBCHelper::SQLPrepared.count(name, @query_where + where)
49
+ # Retrieves the count of the table
50
+ # @param [List of Hash/String] where Filter conditions
51
+ # @return [Fixnum] Count of the records.
52
+ def count *where
53
+ sql, binds = JDBCHelper::SQLPrepared.count(name, @query_where + where)
54
54
  pstmt = prepare :count, sql
55
55
  pstmt.query(*binds)[0][0].to_i
56
- end
56
+ end
57
57
 
58
- # Sees if the table is empty
59
- # @param [Hash/String] Filter conditions
60
- # @return [boolean]
61
- def empty? *where
62
- count(*where) == 0
63
- end
58
+ # Sees if the table is empty
59
+ # @param [Hash/String] Filter conditions
60
+ # @return [boolean]
61
+ def empty? *where
62
+ count(*where) == 0
63
+ end
64
64
 
65
- # Inserts a record into the table with the given hash
66
- # @param [Hash] data_hash Column values in Hash
67
- # @return [Fixnum] Number of affected records
68
- def insert data_hash = {}
65
+ # Inserts a record into the table with the given hash
66
+ # @param [Hash] data_hash Column values in Hash
67
+ # @return [Fixnum] Number of affected records
68
+ def insert data_hash = {}
69
69
  sql, binds = JDBCHelper::SQLPrepared.insert(name, @query_default.merge(data_hash))
70
70
  pstmt = prepare :insert, sql
71
71
  pstmt.send @update_method, *binds
72
- end
72
+ end
73
73
 
74
- # Inserts a record into the table with the given hash.
75
- # Skip insertion when duplicate record is found.
76
- # @note This is not SQL standard. Only works if the database supports insert ignore syntax.
77
- # @param [Hash] data_hash Column values in Hash
78
- # @return [Fixnum] Number of affected records
79
- def insert_ignore data_hash = {}
74
+ # Inserts a record into the table with the given hash.
75
+ # Skip insertion when duplicate record is found.
76
+ # @note This is not SQL standard. Only works if the database supports insert ignore syntax.
77
+ # @param [Hash] data_hash Column values in Hash
78
+ # @return [Fixnum] Number of affected records
79
+ def insert_ignore data_hash = {}
80
80
  sql, binds = JDBCHelper::SQLPrepared.insert_ignore(name, @query_default.merge(data_hash))
81
81
  pstmt = prepare :insert, sql
82
82
  pstmt.send @update_method, *binds
83
- end
83
+ end
84
84
 
85
- # Replaces a record in the table with the new one with the same unique key.
86
- # @note This is not SQL standard. Only works if the database supports replace syntax.
87
- # @param [Hash] data_hash Column values in Hash
88
- # @return [Fixnum] Number of affected records
89
- def replace data_hash = {}
85
+ # Replaces a record in the table with the new one with the same unique key.
86
+ # @note This is not SQL standard. Only works if the database supports replace syntax.
87
+ # @param [Hash] data_hash Column values in Hash
88
+ # @return [Fixnum] Number of affected records
89
+ def replace data_hash = {}
90
90
  sql, binds = JDBCHelper::SQLPrepared.replace(name, @query_default.merge(data_hash))
91
91
  pstmt = prepare :insert, sql
92
92
  pstmt.send @update_method, *binds
93
- end
93
+ end
94
94
 
95
- # Executes update with the given hash.
96
- # :where element of the hash is taken out to generate where clause of the update SQL.
97
- # @param [Hash] data_hash_with_where Column values in Hash.
98
- # :where element of the given hash can (usually should) point to another Hash representing update filters.
99
- # @return [Fixnum] Number of affected records
100
- def update data_hash_with_where = {}
101
- where_ext = data_hash_with_where.delete(:where)
102
- where_ext = [where_ext] unless where_ext.is_a? Array
95
+ # Executes update with the given hash.
96
+ # :where element of the hash is taken out to generate where clause of the update SQL.
97
+ # @param [Hash] data_hash_with_where Column values in Hash.
98
+ # :where element of the given hash can (usually should) point to another Hash representing update filters.
99
+ # @return [Fixnum] Number of affected records
100
+ def update data_hash_with_where = {}
101
+ where_ext = data_hash_with_where.delete(:where)
102
+ where_ext = [where_ext] unless where_ext.is_a? Array
103
103
  sql, binds = JDBCHelper::SQLPrepared.update(name,
104
104
  @query_default.merge(data_hash_with_where),
105
105
  @query_where + where_ext.compact)
106
106
  pstmt = prepare :update, sql
107
107
  pstmt.send @update_method, *binds
108
- end
108
+ end
109
109
 
110
- # Deletes records matching given condtion
111
- # @param [List of Hash/String] where Delete filters
112
- # @return [Fixnum] Number of affected records
113
- def delete *where
114
- sql, binds = JDBCHelper::SQLPrepared.delete(name, @query_where + where)
110
+ # Deletes records matching given condtion
111
+ # @param [List of Hash/String] where Delete filters
112
+ # @return [Fixnum] Number of affected records
113
+ def delete *where
114
+ sql, binds = JDBCHelper::SQLPrepared.delete(name, @query_where + where)
115
115
  pstmt = prepare :delete, sql
116
116
  pstmt.send @update_method, *binds
117
- end
117
+ end
118
118
 
119
- # Empties the table.
120
- # @note This operation cannot be undone
121
- # @return [JDBCHelper::TableWrapper] Self.
122
- def truncate!
123
- @connection.update(JDBCHelper::SQL.check "truncate table #{name}")
124
- self
125
- end
126
- alias truncate_table! truncate!
127
-
128
- # Drops the table.
129
- # @note This operation cannot be undone
130
- # @return [JDBCHelper::TableWrapper] Self.
131
- def drop!
132
- @connection.update(JDBCHelper::SQL.check "drop table #{name}")
133
- self
134
- end
135
- alias drop_table! drop!
119
+ # Empties the table.
120
+ # @note This operation cannot be undone
121
+ # @return [JDBCHelper::TableWrapper] Self.
122
+ def truncate!
123
+ @connection.update(JDBCHelper::SQL.check "truncate table #{name}")
124
+ self
125
+ end
126
+ alias truncate_table! truncate!
127
+
128
+ # Drops the table.
129
+ # @note This operation cannot be undone
130
+ # @return [JDBCHelper::TableWrapper] Self.
131
+ def drop!
132
+ @connection.update(JDBCHelper::SQL.check "drop table #{name}")
133
+ self
134
+ end
135
+ alias drop_table! drop!
136
136
 
137
- # Select SQL wrapper
138
- include Enumerable
137
+ # Select SQL wrapper
138
+ include Enumerable
139
139
 
140
- # Returns a new TableWrapper object which can be used to execute a select
141
- # statement for the table selecting only the specified fields.
142
- # If a block is given, executes the select statement and yields each row to the block.
143
- # @param [*String/*Symbol] fields List of fields to select
144
- # @return [JDBCHelper::TableWrapper]
145
- # @since 0.4.0
146
- def select *fields, &block
147
- obj = self.dup
148
- obj.instance_variable_set :@query_select, fields unless fields.empty?
149
- ret obj, &block
150
- end
140
+ # Returns a new TableWrapper object which can be used to execute a select
141
+ # statement for the table selecting only the specified fields.
142
+ # If a block is given, executes the select statement and yields each row to the block.
143
+ # @param [*String/*Symbol] fields List of fields to select
144
+ # @return [JDBCHelper::TableWrapper]
145
+ # @since 0.4.0
146
+ def select *fields, &block
147
+ obj = self.dup
148
+ obj.instance_variable_set :@query_select, fields unless fields.empty?
149
+ ret obj, &block
150
+ end
151
151
 
152
- # Returns a new TableWrapper object which can be used to execute a select
153
- # statement for the table with the specified filter conditions.
154
- # If a block is given, executes the select statement and yields each row to the block.
155
- # @param [List of Hash/String] conditions Filter conditions
156
- # @return [JDBCHelper::TableWrapper]
157
- # @since 0.4.0
158
- def where *conditions, &block
159
- raise ArgumentError.new("Wrong number of arguments") if conditions.empty?
152
+ # Returns a new TableWrapper object which can be used to execute a select
153
+ # statement for the table with the specified filter conditions.
154
+ # If a block is given, executes the select statement and yields each row to the block.
155
+ # @param [List of Hash/String] conditions Filter conditions
156
+ # @return [JDBCHelper::TableWrapper]
157
+ # @since 0.4.0
158
+ def where *conditions, &block
159
+ raise ArgumentError.new("Wrong number of arguments") if conditions.empty?
160
160
 
161
- obj = self.dup
162
- obj.instance_variable_set :@query_where, @query_where + conditions
163
- ret obj, &block
164
- end
161
+ obj = self.dup
162
+ obj.instance_variable_set :@query_where, @query_where + conditions
163
+ ret obj, &block
164
+ end
165
165
 
166
- # Returns a new TableWrapper object which can be used to execute a select
167
- # statement for the table with the given sorting criteria.
168
- # If a block is given, executes the select statement and yields each row to the block.
169
- # @param [*String/*Symbol] criteria Sorting criteria
170
- # @return [JDBCHelper::TableWrapper]
171
- # @since 0.4.0
172
- def order *criteria, &block
173
- raise ArgumentError.new("Wrong number of arguments") if criteria.empty?
174
- obj = self.dup
175
- obj.instance_variable_set :@query_order, criteria
176
- ret obj, &block
177
- end
166
+ # Returns a new TableWrapper object which can be used to execute a select
167
+ # statement for the table with the given sorting criteria.
168
+ # If a block is given, executes the select statement and yields each row to the block.
169
+ # @param [*String/*Symbol] criteria Sorting criteria
170
+ # @return [JDBCHelper::TableWrapper]
171
+ # @since 0.4.0
172
+ def order *criteria, &block
173
+ raise ArgumentError.new("Wrong number of arguments") if criteria.empty?
174
+ obj = self.dup
175
+ obj.instance_variable_set :@query_order, criteria
176
+ ret obj, &block
177
+ end
178
178
 
179
- # Returns a new TableWrapper object with default values, which will be applied to
180
- # the subsequent inserts and updates.
181
- # @param [Hash] data_hash Default values
182
- # @return [JDBCHelper::TableWrapper]
183
- # @since 0.4.5
184
- def default data_hash
185
- raise ArgumentError.new("Hash required") unless data_hash.kind_of? Hash
179
+ # Returns a new TableWrapper object with default values, which will be applied to
180
+ # the subsequent inserts and updates.
181
+ # @param [Hash] data_hash Default values
182
+ # @return [JDBCHelper::TableWrapper]
183
+ # @since 0.4.5
184
+ def default data_hash
185
+ raise ArgumentError.new("Hash required") unless data_hash.kind_of? Hash
186
186
 
187
- obj = self.dup
188
- obj.instance_variable_set :@query_default, @query_default.merge(data_hash)
189
- obj
190
- end
187
+ obj = self.dup
188
+ obj.instance_variable_set :@query_default, @query_default.merge(data_hash)
189
+ obj
190
+ end
191
191
 
192
- # Executes a select SQL for the table and returns an Enumerable object,
193
- # or yields each row if block is given.
194
- # @return [JDBCHelper::Connection::ResultSetEnumerator]
195
- # @since 0.4.0
196
- def each &block
197
- sql, binds = JDBCHelper::SQLPrepared.select(
198
- name,
199
- :select => @query_select,
200
- :where => @query_where,
201
- :order => @query_order)
192
+ # Executes a select SQL for the table and returns an Enumerable object,
193
+ # or yields each row if block is given.
194
+ # @return [JDBCHelper::Connection::ResultSetEnumerator]
195
+ # @since 0.4.0
196
+ def each &block
197
+ sql, binds = JDBCHelper::SQLPrepared.select(
198
+ name,
199
+ :select => @query_select,
200
+ :where => @query_where,
201
+ :order => @query_order)
202
202
  pstmt = prepare :select, sql
203
203
  pstmt.enumerate *binds, &block
204
- end
204
+ end
205
205
 
206
- # Returns a new TableWrapper object whose subsequent inserts, updates,
207
- # and deletes are added to batch for JDBC batch-execution. The actual execution
208
- # is deferred until JDBCHelper::Connection#execute_batch method is called.
209
- # Self is returned when batch is called more than once.
210
- # @return [JDBCHelper::TableWrapper]
211
- # @since 0.4.0
212
- def batch
213
- if batch?
214
- self
215
- else
206
+ # Returns a new TableWrapper object whose subsequent inserts, updates,
207
+ # and deletes are added to batch for JDBC batch-execution. The actual execution
208
+ # is deferred until JDBCHelper::Connection#execute_batch method is called.
209
+ # Self is returned when batch is called more than once.
210
+ # @return [JDBCHelper::TableWrapper]
211
+ # @since 0.4.0
212
+ def batch
213
+ if batch?
214
+ self
215
+ else
216
216
  # dup makes @pstmts to be shared
217
- obj = self.dup
218
- obj.instance_variable_set :@update_method, :add_batch
219
- obj
220
- end
221
- end
217
+ obj = self.dup
218
+ obj.instance_variable_set :@update_method, :add_batch
219
+ obj
220
+ end
221
+ end
222
222
 
223
- # Returns if the subsequent updates for this wrapper will be batched
224
- # @return [Boolean]
225
- # @since 0.4.0
226
- def batch?
227
- @update_method == :add_batch
228
- end
223
+ # Returns if the subsequent updates for this wrapper will be batched
224
+ # @return [Boolean]
225
+ # @since 0.4.0
226
+ def batch?
227
+ @update_method == :add_batch
228
+ end
229
229
 
230
- # Returns the select SQL for this wrapper object
231
- # @return [String] Select SQL
232
- # @since 0.4.0
233
- def sql
234
- JDBCHelper::SQL.select(
235
- name,
236
- :select => @query_select,
237
- :where => @query_where,
238
- :order => @query_order)
239
- end
230
+ # Returns the select SQL for this wrapper object
231
+ # @return [String] Select SQL
232
+ # @since 0.4.0
233
+ def sql
234
+ JDBCHelper::SQL.select(
235
+ name,
236
+ :select => @query_select,
237
+ :where => @query_where,
238
+ :order => @query_order)
239
+ end
240
240
 
241
- def initialize connection, table_name
242
- super connection, table_name
243
- @update_method = :update
244
- @query_default = {}
245
- @query_where = []
241
+ def initialize connection, table_name
242
+ super connection, table_name
243
+ @update_method = :update
244
+ @query_default = {}
245
+ @query_where = []
246
246
  @pstmts = {
247
247
  :select => {},
248
248
  :insert => {},
@@ -250,7 +250,7 @@ class TableWrapper < ObjectWrapper
250
250
  :count => {},
251
251
  :update => {}
252
252
  }
253
- end
253
+ end
254
254
 
255
255
  # Closes the prepared statements
256
256
  # @since 0.5.0
@@ -271,16 +271,19 @@ class TableWrapper < ObjectWrapper
271
271
 
272
272
  private
273
273
  def prepare type, sql
274
- @pstmts[type][sql] ||= @connection.prepare(JDBCHelper::SQL.check(sql))
274
+ sql = JDBCHelper::SQL.check(sql)
275
+ pstmt = @pstmts[type][sql] ||= @connection.prepare(sql)
276
+ pstmt = @pstmts[type][sql] = @connection.prepare(sql) if pstmt.closed?
277
+ pstmt
275
278
  end
276
279
 
277
- def ret obj, &block
278
- if block_given?
279
- obj.each &block
280
- else
281
- obj
282
- end
283
- end
280
+ def ret obj, &block
281
+ if block_given?
282
+ obj.each &block
283
+ else
284
+ obj
285
+ end
286
+ end
284
287
  end#TableWrapper
285
288
  end#JDBCHelper
286
289