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.
- data/README.markdown +251 -0
- data/lib/jdbc-helper.rb +1 -10
- data/lib/jdbc-helper/connection.rb +347 -351
- data/lib/jdbc-helper/connection/callable_statement.rb +56 -56
- data/lib/jdbc-helper/connection/parameterized_statement.rb +57 -46
- data/lib/jdbc-helper/connection/prepared_statement.rb +88 -88
- data/lib/jdbc-helper/connection/result_set_enumerator.rb +102 -91
- data/lib/jdbc-helper/connection/row.rb +106 -115
- data/lib/jdbc-helper/connection/statement_pool.rb +44 -44
- data/lib/jdbc-helper/connection/type_map.rb +41 -66
- data/lib/jdbc-helper/connector.rb +10 -10
- data/lib/jdbc-helper/connector/mysql_connector.rb +24 -24
- data/lib/jdbc-helper/connector/oracle_connector.rb +33 -32
- data/lib/jdbc-helper/constants.rb +20 -17
- data/lib/jdbc-helper/sql.rb +168 -175
- data/lib/jdbc-helper/wrapper/function_wrapper.rb +12 -12
- data/lib/jdbc-helper/wrapper/object_wrapper.rb +17 -17
- data/lib/jdbc-helper/wrapper/procedure_wrapper.rb +120 -123
- data/lib/jdbc-helper/wrapper/sequence_wrapper.rb +43 -43
- data/lib/jdbc-helper/wrapper/table_wrapper.rb +172 -169
- data/test/helper.rb +2 -10
- data/test/performance.rb +141 -0
- data/test/test_connection.rb +443 -389
- data/test/test_connectors.rb +47 -47
- data/test/test_object_wrapper.rb +541 -432
- data/test/test_sql.rb +143 -135
- metadata +119 -123
- data/README.rdoc +0 -223
- data/test/test_performance.rb +0 -138
@@ -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
|
-
|
11
|
-
|
12
|
-
|
10
|
+
# Returns the name of the function
|
11
|
+
# @return [String]
|
12
|
+
alias to_s name
|
13
13
|
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
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
|
-
|
10
|
-
|
11
|
-
|
9
|
+
# Underlying JDBCHelper::Connection object
|
10
|
+
# @return [JDBCHelper::Connection]
|
11
|
+
attr_reader :connection
|
12
12
|
|
13
|
-
|
14
|
-
|
15
|
-
|
13
|
+
# Object name (or expression)
|
14
|
+
# @return [String]
|
15
|
+
attr_reader :name
|
16
16
|
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
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
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
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
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
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
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
|
80
|
-
|
81
|
-
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
|
86
|
-
|
87
|
-
|
88
|
-
|
89
|
-
|
90
|
-
|
91
|
-
|
92
|
-
|
93
|
-
|
94
|
-
|
95
|
-
|
96
|
-
|
97
|
-
|
98
|
-
|
99
|
-
|
100
|
-
|
101
|
-
|
102
|
-
|
103
|
-
|
104
|
-
|
105
|
-
|
106
|
-
|
107
|
-
|
108
|
-
|
109
|
-
|
110
|
-
|
111
|
-
|
112
|
-
|
113
|
-
|
114
|
-
|
115
|
-
|
116
|
-
|
117
|
-
|
118
|
-
|
119
|
-
|
120
|
-
|
121
|
-
|
122
|
-
|
123
|
-
|
124
|
-
|
125
|
-
|
126
|
-
|
127
|
-
|
128
|
-
|
129
|
-
|
130
|
-
|
131
|
-
|
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
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
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
|
-
|
46
|
-
|
47
|
-
|
45
|
+
# Returns the name of the table
|
46
|
+
# @return [String]
|
47
|
+
alias to_s name
|
48
48
|
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
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
|
-
|
56
|
+
end
|
57
57
|
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
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
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
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
|
-
|
72
|
+
end
|
73
73
|
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
|
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
|
-
|
83
|
+
end
|
84
84
|
|
85
|
-
|
86
|
-
|
87
|
-
|
88
|
-
|
89
|
-
|
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
|
-
|
93
|
+
end
|
94
94
|
|
95
|
-
|
96
|
-
|
97
|
-
|
98
|
-
|
99
|
-
|
100
|
-
|
101
|
-
|
102
|
-
|
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
|
-
|
108
|
+
end
|
109
109
|
|
110
|
-
|
111
|
-
|
112
|
-
|
113
|
-
|
114
|
-
|
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
|
-
|
117
|
+
end
|
118
118
|
|
119
|
-
|
120
|
-
|
121
|
-
|
122
|
-
|
123
|
-
|
124
|
-
|
125
|
-
|
126
|
-
|
127
|
-
|
128
|
-
|
129
|
-
|
130
|
-
|
131
|
-
|
132
|
-
|
133
|
-
|
134
|
-
|
135
|
-
|
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
|
-
|
138
|
-
|
137
|
+
# Select SQL wrapper
|
138
|
+
include Enumerable
|
139
139
|
|
140
|
-
|
141
|
-
|
142
|
-
|
143
|
-
|
144
|
-
|
145
|
-
|
146
|
-
|
147
|
-
|
148
|
-
|
149
|
-
|
150
|
-
|
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
|
-
|
153
|
-
|
154
|
-
|
155
|
-
|
156
|
-
|
157
|
-
|
158
|
-
|
159
|
-
|
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
|
-
|
162
|
-
|
163
|
-
|
164
|
-
|
161
|
+
obj = self.dup
|
162
|
+
obj.instance_variable_set :@query_where, @query_where + conditions
|
163
|
+
ret obj, &block
|
164
|
+
end
|
165
165
|
|
166
|
-
|
167
|
-
|
168
|
-
|
169
|
-
|
170
|
-
|
171
|
-
|
172
|
-
|
173
|
-
|
174
|
-
|
175
|
-
|
176
|
-
|
177
|
-
|
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
|
-
|
180
|
-
|
181
|
-
|
182
|
-
|
183
|
-
|
184
|
-
|
185
|
-
|
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
|
-
|
188
|
-
|
189
|
-
|
190
|
-
|
187
|
+
obj = self.dup
|
188
|
+
obj.instance_variable_set :@query_default, @query_default.merge(data_hash)
|
189
|
+
obj
|
190
|
+
end
|
191
191
|
|
192
|
-
|
193
|
-
|
194
|
-
|
195
|
-
|
196
|
-
|
197
|
-
|
198
|
-
|
199
|
-
|
200
|
-
|
201
|
-
|
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
|
-
|
204
|
+
end
|
205
205
|
|
206
|
-
|
207
|
-
|
208
|
-
|
209
|
-
|
210
|
-
|
211
|
-
|
212
|
-
|
213
|
-
|
214
|
-
|
215
|
-
|
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
|
-
|
218
|
-
|
219
|
-
|
220
|
-
|
221
|
-
|
217
|
+
obj = self.dup
|
218
|
+
obj.instance_variable_set :@update_method, :add_batch
|
219
|
+
obj
|
220
|
+
end
|
221
|
+
end
|
222
222
|
|
223
|
-
|
224
|
-
|
225
|
-
|
226
|
-
|
227
|
-
|
228
|
-
|
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
|
-
|
231
|
-
|
232
|
-
|
233
|
-
|
234
|
-
|
235
|
-
|
236
|
-
|
237
|
-
|
238
|
-
|
239
|
-
|
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
|
-
|
242
|
-
|
243
|
-
|
244
|
-
|
245
|
-
|
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
|
-
|
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
|
-
|
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
|
-
|
278
|
-
|
279
|
-
|
280
|
-
|
281
|
-
|
282
|
-
|
283
|
-
|
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
|
|