jdbc-helper 0.7.7 → 0.8.0

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.
@@ -29,7 +29,9 @@ end#JDBCHelper
29
29
 
30
30
  require 'jdbc-helper/connector/oracle'
31
31
  require 'jdbc-helper/connector/mysql'
32
+ require 'jdbc-helper/connector/mariadb'
32
33
  require 'jdbc-helper/connector/postgresql'
33
34
  require 'jdbc-helper/connector/mssql'
34
35
  require 'jdbc-helper/connector/cassandra'
35
36
  require 'jdbc-helper/connector/filemaker'
37
+ require 'jdbc-helper/connector/sqlite'
@@ -21,6 +21,9 @@ module Constants
21
21
  :useServerPrepStmts => 'true',
22
22
  :useCursorFetch => 'true',
23
23
  },
24
+ :mariadb => {
25
+ :driver => 'org.mariadb.jdbc.Driver',
26
+ },
24
27
  :oracle => {
25
28
  :driver => 'oracle.jdbc.driver.OracleDriver',
26
29
  },
@@ -37,6 +40,9 @@ module Constants
37
40
  },
38
41
  :filemaker => {
39
42
  :driver => 'com.filemaker.jdbc.Driver'
43
+ },
44
+ :sqlite => {
45
+ :driver => 'org.sqlite.JDBC',
40
46
  }
41
47
  }
42
48
  end#Connector
@@ -4,141 +4,83 @@
4
4
  module JDBCHelper
5
5
  # SQL generator class methods for prepared operations.
6
6
  # WARNING: Does not perform SQL.check to minimize performance overhead
7
+ # @deprecated
7
8
  module SQL
8
9
  # Generate SQL snippet, prevents the string from being quoted.
9
10
  # @param [String] SQL snippet
10
11
  # @return [JDBCHelper::SQL::Expression]
11
12
  # @since 0.7.0
13
+ # @deprecated
12
14
  def self.expr sql
13
- ScalarExpression.new sql
15
+ { :sql => sql }
14
16
  end
15
17
 
16
18
  # "is not null" expression for where clauses
19
+ # @deprecated
17
20
  # @return [JDBCHelper::SQL::Expression]
18
21
  def self.not_nil
19
- NotNullExpression.singleton
22
+ { :not => nil }
20
23
  end
21
24
  class << self
22
25
  alias not_null not_nil
23
26
  end
24
27
 
25
28
  # Greater-than expression for where clauses
29
+ # @deprecated
26
30
  # @return [JDBCHelper::SQL::Expression]
27
31
  # @since 0.7.0
28
32
  def self.gt v
29
- CriterionExpression.new '>', v
33
+ { :gt => v }
30
34
  end
31
35
 
32
36
  # Less-than expression for where clauses
37
+ # @deprecated
33
38
  # @return [JDBCHelper::SQL::Expression]
34
39
  # @since 0.7.0
35
40
  def self.lt v
36
- CriterionExpression.new '<', v
41
+ { :lt => v }
37
42
  end
38
43
 
39
44
  # Less-than-or-equal-to expression for where clauses
45
+ # @deprecated
40
46
  # @return [JDBCHelper::SQL::Expression]
41
47
  # @since 0.7.0
42
48
  def self.le v
43
- CriterionExpression.new '<=', v
49
+ { :le => v }
44
50
  end
45
51
 
46
52
  # Greater-than-or-equal-to expression for where clauses
53
+ # @deprecated
47
54
  # @return [JDBCHelper::SQL::Expression]
48
55
  # @since 0.7.0
49
56
  def self.ge v
50
- CriterionExpression.new '>=', v
57
+ { :ge => v }
51
58
  end
52
59
 
53
60
  # Not-equal expression for where clauses
61
+ # @deprecated
54
62
  # @return [JDBCHelper::SQL::Expression]
55
63
  # @since 0.7.0
56
64
  def self.ne v
57
- CriterionExpression.new '<>', v
65
+ { :ne => v }
58
66
  end
59
67
 
60
68
  # Like expression for where clauses
69
+ # @deprecated
61
70
  # @return [JDBCHelper::SQL::Expression]
62
71
  # @since 0.7.0
63
72
  def self.like v
64
- raise ArgumentError.new('Like expression must be given as a String') unless v.is_a?(String)
65
- CriterionExpression.new 'like', v
73
+ raise ArgumentError, "expected String" unless v.is_a?(String)
74
+ { :like => v }
66
75
  end
67
76
 
68
77
  # "Not like" expression for where clauses
78
+ # @deprecated
69
79
  # @return [JDBCHelper::SQL::Expression]
70
80
  # @since 0.7.0
71
81
  def self.not_like v
72
- raise ArgumentError.new('Like expression must be given as a String') unless v.is_a?(String)
73
- CriterionExpression.new 'not like', v
74
- end
75
-
76
- # @since 0.7.0
77
- class Expression
78
- def initialize
79
- raise Exception.new("JDBCHelper::SQL::Expression is an abstract class")
80
- end
81
-
82
- def == other
83
- self.to_s == other.to_s
84
- end
85
-
86
- def eql? other
87
- self.class == other.class && self.to_s == other.to_s
88
- end
89
-
90
- def hash
91
- [self.class, self.to_s].hash
92
- end
93
- end
94
-
95
- # @since 0.7.0
96
- class ScalarExpression < Expression
97
- def initialize sql
98
- @sql = SQL.check sql.to_s
99
- end
100
-
101
- def to_s
102
- @sql
103
- end
104
-
105
- def to_bind
106
- [@to_s, []]
107
- end
108
- end
109
-
110
- # @since 0.7.0
111
- class NotNullExpression < Expression
112
- def self.singleton
113
- @@singleton ||= NotNullExpression.new
114
- end
115
-
116
- def initialize
117
- end
118
-
119
- def to_s
120
- "is not null"
121
- end
122
-
123
- def to_bind
124
- ["is not null", []]
125
- end
126
- end
127
-
128
- # @since 0.7.0
129
- class CriterionExpression < Expression
130
- def initialize operator, param
131
- @operator = operator
132
- @param = param
133
- end
134
-
135
- def to_s
136
- [@operator, SQL.value(@param)].join(' ')
137
- end
138
-
139
- def to_bind
140
- [[@operator, '?'].join(' '), [@param]]
141
- end
82
+ raise ArgumentError, "expected String" unless v.is_a?(String)
83
+ { :not => { :like => v } }
142
84
  end
143
85
  end#SQL
144
86
  end#JDBCHelper
@@ -4,13 +4,13 @@
4
4
  require 'bigdecimal'
5
5
 
6
6
  module JDBCHelper
7
-
8
7
  # Generate SQL snippet, prevents the string from being quoted.
8
+ # @deprecated
9
9
  # @param [String] SQL snippet
10
10
  # @return [JDBCHelper::SQL::Expression]
11
11
  # @deprecated Use JDBCHelper::SQL.expr instead
12
12
  def self.sql str
13
- JDBCHelper::SQL::ScalarExpression.new str
13
+ { :sql => str }
14
14
  end
15
15
  class << self
16
16
  # @deprecated Use JDBCHelper::SQL.expr instead
@@ -18,89 +18,85 @@ class << self
18
18
  end
19
19
 
20
20
  # Class representing an SQL snippet. Also has many SQL generator class methods.
21
+ # @deprecated
21
22
  module SQL
22
23
  # Formats the given data so that it can be injected into SQL
24
+ # @deprecated
23
25
  def self.value data
24
- case data
25
- when BigDecimal
26
- data.to_s("F")
27
- when Numeric
28
- data
29
- when String, Symbol
30
- "'#{esc data}'"
31
- when NilClass
32
- 'null'
33
- when JDBCHelper::SQL::ScalarExpression
34
- data.to_s
35
- else
36
- raise NotImplementedError.new("Unsupported datatype: #{data.class}")
37
- end
26
+ SQLHelper.quote(data)
38
27
  end
39
28
 
40
29
  # Generates SQL where cluase with the given conditions.
41
30
  # Parameter can be either Hash of String.
31
+ # @deprecated
42
32
  def self.where *conds
43
- where_clause = where_internal conds
44
- where_clause.empty? ? where_clause : check(where_clause)
33
+ SQLHelper.where(*conds)
45
34
  end
46
35
 
36
+ # @deprecated
47
37
  def self.where_prepared *conds
38
+ SQLHelper.where_prepared(*conds)
48
39
  end
49
40
 
50
41
  # Generates SQL order by cluase with the given conditions.
42
+ # @deprecated
51
43
  def self.order *criteria
52
- str = criteria.map(&:to_s).reject(&:empty?).join(', ')
53
- str.empty? ? str : check('order by ' + str)
44
+ SQLHelper.order(*criteria)
54
45
  end
55
46
 
56
47
  # SQL Helpers
57
48
  # ===========
58
49
 
59
50
  # Generates insert SQL with hash
51
+ # @deprecated
60
52
  def self.insert table, data_hash
61
- insert_internal 'insert', table, data_hash
53
+ SQLHelper.insert :table => table, :data => data_hash, :prepared => false
62
54
  end
63
55
 
64
56
  # Generates insert ignore SQL (Non-standard syntax)
57
+ # @deprecated
65
58
  def self.insert_ignore table, data_hash
66
- insert_internal 'insert ignore', table, data_hash
59
+ SQLHelper.insert_ignore :table => table, :data => data_hash, :prepared => false
67
60
  end
68
61
 
69
62
  # Generates replace SQL (Non-standard syntax)
63
+ # @deprecated
70
64
  def self.replace table, data_hash
71
- insert_internal 'replace', table, data_hash
65
+ SQLHelper.replace :table => table, :data => data_hash, :prepared => false
72
66
  end
73
67
 
74
68
  # Generates update SQL with hash.
75
69
  # :where element of the given hash is taken out to generate where clause.
70
+ # @deprecated
76
71
  def self.update table, data_hash, where
77
- where_clause = where_internal where
78
- updates = data_hash.map { |k, v| "#{k} = #{value v}" }.join(', ')
79
- check "update #{table} set #{updates} #{where_clause}".strip
72
+ SQLHelper.update :table => table, :data => data_hash, :where => where, :prepared => false
80
73
  end
81
74
 
82
75
  # Generates select SQL with the given conditions
76
+ # @deprecated
83
77
  def self.select table, opts = {}
84
- opts = opts.reject { |k, v| v.nil? }
85
- check [
86
- "select #{opts.fetch(:select, ['*']).join(', ')} from #{table}",
87
- where_internal(opts.fetch(:where, {})),
88
- order(opts.fetch(:order, []).join(', '))
89
- ].reject(&:empty?).join(' ')
78
+ SQLHelper.select :table => table,
79
+ :project => opts[:select],
80
+ :where => opts[:where],
81
+ :order => opts[:order],
82
+ :prepared => false
90
83
  end
91
84
 
92
85
  # Generates count SQL with the given conditions
86
+ # @deprecated
93
87
  def self.count table, conds = nil
94
- check "select count(*) from #{table} #{where_internal conds}".strip
88
+ SQLHelper.count :table => table, :where => conds, :prepared => false
95
89
  end
96
90
 
97
91
  # Generates delete SQL with the given conditions
92
+ # @deprecated
98
93
  def self.delete table, conds = nil
99
- check "delete from #{table} #{where_internal conds}".strip
94
+ SQLHelper.delete :table => table, :where => conds, :prepared => false
100
95
  end
101
96
 
102
97
  # FIXME: Naive protection for SQL Injection
103
98
  # TODO: check caching?
99
+ # @deprecated
104
100
  def self.check expr, is_name = false
105
101
  return nil if expr.nil?
106
102
 
@@ -118,63 +114,6 @@ module SQL
118
114
 
119
115
  return expr
120
116
  end
121
-
122
- protected
123
- def self.esc str
124
- str.to_s.gsub("'", "''")
125
- end
126
-
127
- # No check
128
- def self.where_internal conds
129
- conds = [conds] unless conds.is_a? Array
130
- where_clause = conds.compact.map { |cond| where_unit cond }.reject(&:empty?).join(' and ')
131
- where_clause.empty? ? '' : 'where ' + where_clause
132
- end
133
-
134
- def self.where_unit conds
135
- case conds
136
- when String, JDBCHelper::SQL::ScalarExpression
137
- conds = conds.to_s.strip
138
- conds.empty? ? '' : "(#{conds})"
139
- when Hash
140
- conds.map { |k, v|
141
- "#{k} " +
142
- case v
143
- when NilClass
144
- "is null"
145
- when Numeric, String, JDBCHelper::SQL::ScalarExpression
146
- "= #{value v}"
147
- when JDBCHelper::SQL::Expression
148
- v.to_s
149
- when Range
150
- ">= #{v.first} and #{k} <#{'=' unless v.exclude_end?} #{v.last}"
151
- when Array
152
- "in (#{ v.map { |e| value(e) }.join(', ') })"
153
- else
154
- raise NotImplementedError.new("Unsupported class: #{v.class}")
155
- end
156
- }.join(' and ')
157
- when Array
158
- if conds.empty?
159
- ''
160
- else
161
- base = conds.first.to_s
162
- params = conds[1..-1] || []
163
- '(' +
164
- base.gsub('?') {
165
- param = params.shift
166
- param ? value(param) : '?'
167
- } + ')'
168
- end
169
- else
170
- raise NotImplementedError.new("Parameter to where must be either Hash or String")
171
- end
172
- end
173
-
174
- def self.insert_internal cmd, table, data_hash
175
- cols = data_hash.keys
176
- check "#{cmd} into #{table} (#{cols.join ', '}) values (#{cols.map{|c|value data_hash[c]}.join ', '})"
177
- end
178
117
  end#SQL
179
118
  end#JDBCHelper
180
119
 
@@ -4,163 +4,72 @@
4
4
  module JDBCHelper
5
5
  # SQL generator class methods for prepared operations.
6
6
  # WARNING: Does not perform SQL.check to minimize performance overhead
7
+ # @deprecated
7
8
  module SQLPrepared
8
9
  # Generates SQL where cluase with the given conditions.
9
10
  # Parameter can be either Hash of String.
11
+ # @deprecated
10
12
  def self.where *conds
11
- where_internal conds
13
+ sql, *binds = SQLHelper.where_prepared(*conds)
14
+ [sql, binds]
12
15
  end
13
16
 
14
17
  # SQL Helpers
15
18
  # ===========
16
19
  # Generates insert SQL with hash
20
+ # @deprecated
17
21
  def self.insert table, data_hash
18
- insert_internal 'insert', table, data_hash
22
+ sql, *binds = SQLHelper.insert :table => table, :data => data_hash, :prepared => true
23
+ [sql, binds]
19
24
  end
20
25
 
21
26
  # Generates insert ignore SQL (Non-standard syntax)
27
+ # @deprecated
22
28
  def self.insert_ignore table, data_hash
23
- insert_internal 'insert ignore', table, data_hash
29
+ sql, *binds = SQLHelper.insert_ignore :table => table, :data => data_hash, :prepared => true
30
+ [sql, binds]
24
31
  end
25
32
 
26
33
  # Generates replace SQL (Non-standard syntax)
34
+ # @deprecated
27
35
  def self.replace table, data_hash
28
- insert_internal 'replace', table, data_hash
36
+ sql, *binds = SQLHelper.replace :table => table, :data => data_hash, :prepared => true
37
+ [sql, binds]
29
38
  end
30
39
 
31
40
  # Generates update SQL with hash.
32
41
  # :where element of the given hash is taken out to generate where clause.
42
+ # @deprecated
33
43
  def self.update table, data_hash, where
34
- where_clause, where_binds = where_internal where
35
-
36
- col_binds = []
37
- sql = ("update #{table} set " + data_hash.map { |k, v|
38
- case v
39
- when JDBCHelper::SQL::ScalarExpression
40
- "#{k} = #{v}"
41
- else
42
- col_binds << v
43
- "#{k} = ?"
44
- end
45
- }.join(', ') + " #{where_clause}").strip
46
-
47
- return sql, col_binds + where_binds
44
+ sql, *binds =
45
+ SQLHelper.update :table => table, :data => data_hash, :where => where, :prepared => true
46
+ [sql, binds]
48
47
  end
49
48
 
50
49
  # Generates select SQL with the given conditions
50
+ # @deprecated
51
51
  def self.select table, opts = {}
52
- opts = opts.reject { |k, v| v.nil? }
53
- w_c, w_b = where_internal(opts.fetch(:where, {}))
54
- sql = [
55
- "select #{opts.fetch(:select, ['*']).join(', ')} from #{table}",
56
- w_c.to_s,
57
- SQL.order(opts.fetch(:order, []).join(', '))
58
- ].reject(&:empty?).join(' ')
59
-
60
- return sql, w_b
52
+ sql, *binds = SQLHelper.select :table => table,
53
+ :project => opts[:select],
54
+ :where => opts[:where],
55
+ :order => opts[:order],
56
+ :prepared => true
57
+ [sql, binds]
61
58
  end
62
59
 
63
60
  # Generates count SQL with the given conditions
61
+ # @deprecated
64
62
  def self.count table, conds = nil
65
- w_c, w_b = where_internal(conds)
66
- sql = "select count(*) from #{table} #{w_c}".strip
67
-
68
- return sql, w_b
63
+ sql, *binds =
64
+ SQLHelper.count :table => table, :where => conds, :prepared => true
65
+ [sql, binds]
69
66
  end
70
67
 
71
68
  # Generates delete SQL with the given conditions
69
+ # @deprecated
72
70
  def self.delete table, conds = nil
73
- w_c, w_b = where_internal(conds)
74
- sql = "delete from #{table} #{w_c}".strip
75
- return sql, w_b
76
- end
77
-
78
- private
79
- def self.where_internal conds
80
- conds = [conds] unless conds.is_a? Array
81
- binds = []
82
- clauses = []
83
- conds.compact.each do |cond|
84
- c, b = where_unit cond
85
- next if c.empty?
86
-
87
- binds += b
88
- clauses << c
89
- end
90
- where_clause = clauses.join(' and ')
91
- if where_clause.empty?
92
- return nil, []
93
- else
94
- where_clause = 'where ' + where_clause
95
- return where_clause, binds
96
- end
97
- end
98
-
99
- def self.where_unit conds
100
- binds = []
101
-
102
- clause = case conds
103
- when String, JDBCHelper::SQL::ScalarExpression
104
- conds = conds.strip
105
- conds.empty? ? '' : "(#{conds})"
106
- when Hash
107
- conds.map { |k, v|
108
- "#{k} " +
109
- case v
110
- when NilClass
111
- "is null"
112
- when Numeric
113
- binds << v
114
- "= ?"
115
- when JDBCHelper::SQL::ScalarExpression
116
- "= #{v.to_s}"
117
- when JDBCHelper::SQL::NotNullExpression
118
- v.to_s
119
- when JDBCHelper::SQL::CriterionExpression
120
- e, b = v.to_bind
121
- case b.first
122
- when JDBCHelper::SQL::ScalarExpression
123
- v.to_s
124
- else
125
- binds += b
126
- e
127
- end
128
- when Range
129
- binds << v.begin << v.end
130
- ">= ? and #{k} <#{'=' unless v.exclude_end?} ?"
131
- when Array
132
- "in (" + v.map { |ie| SQL.value ie }.join(', ') + ")"
133
- else
134
- binds << v
135
- "= ?"
136
- end
137
- }.join(' and ')
138
- when Array
139
- if conds.empty?
140
- ''
141
- else
142
- binds += conds[1..-1] if conds.length > 1
143
- "(#{conds.first})"
144
- end
145
- else
146
- raise NotImplementedError.new("Parameter to where must be either Hash or String")
147
- end
148
- return clause, binds
149
- end
150
-
151
- def self.insert_internal cmd, table, data_hash
152
- binds = []
153
- values = data_hash.values.map { |v|
154
- case v
155
- when JDBCHelper::SQL::ScalarExpression
156
- v
157
- else
158
- binds << v
159
- '?'
160
- end
161
- }
162
- sql = "#{cmd} into #{table} (#{data_hash.keys.join ', '}) values (#{values.join(', ')})"
163
- return sql, binds
71
+ sql, *binds = SQLHelper.delete :table => table, :where => conds, :prepared => true
72
+ [sql, binds]
164
73
  end
165
74
  end#SQL
166
75
  end#JDBCHelper
@@ -1,3 +1,3 @@
1
1
  module JDBCHelper
2
- VERSION = '0.7.7'
2
+ VERSION = '0.8.0'
3
3
  end
@@ -27,7 +27,7 @@ class FunctionWrapper < ObjectWrapper
27
27
  def call(*args)
28
28
  pstmt = @connection.prepare("select #{name}(#{args.map{'?'}.join ','})#{@suffix}")
29
29
  begin
30
- pstmt.query(*args)[0][0]
30
+ pstmt.query(*args).to_a[0][0]
31
31
  ensure
32
32
  pstmt.close
33
33
  end
@@ -31,13 +31,13 @@ class SequenceWrapper < ObjectWrapper
31
31
  # Increments the sequence and returns the value
32
32
  # @return [Fixnum]
33
33
  def nextval
34
- @connection.query(@nextval_sql)[0][0].to_i
34
+ @connection.query(@nextval_sql).to_a[0][0].to_i
35
35
  end
36
36
 
37
37
  # Returns the incremented value of the sequence
38
38
  # @return [Fixnum]
39
39
  def currval
40
- @connection.query(@currval_sql)[0][0].to_i
40
+ @connection.query(@currval_sql).to_a[0][0].to_i
41
41
  end
42
42
 
43
43
  # Recreates the sequence. Cannot be undone.