Module: JDBCHelper::SQL Deprecated
- Defined in:
- lib/jdbc-helper/sql/sql.rb,
lib/jdbc-helper/sql/expression.rb
Overview
Class Method Summary collapse
- .check(expr, is_name = false) ⇒ Object deprecated Deprecated.
- .count(table, conds = nil) ⇒ Object deprecated Deprecated.
- .delete(table, conds = nil) ⇒ Object deprecated Deprecated.
- .expr(sql) ⇒ JDBCHelper::SQL::Expression deprecated Deprecated.
- .ge(v) ⇒ JDBCHelper::SQL::Expression deprecated Deprecated.
- .gt(v) ⇒ JDBCHelper::SQL::Expression deprecated Deprecated.
- .insert(table, data_hash) ⇒ Object deprecated Deprecated.
- .insert_ignore(table, data_hash) ⇒ Object deprecated Deprecated.
- .le(v) ⇒ JDBCHelper::SQL::Expression deprecated Deprecated.
- .like(v) ⇒ JDBCHelper::SQL::Expression deprecated Deprecated.
- .lt(v) ⇒ JDBCHelper::SQL::Expression deprecated Deprecated.
- .ne(v) ⇒ JDBCHelper::SQL::Expression deprecated Deprecated.
- .not_like(v) ⇒ JDBCHelper::SQL::Expression deprecated Deprecated.
- .not_nil ⇒ JDBCHelper::SQL::Expression (also: not_null) deprecated Deprecated.
- .order(*criteria) ⇒ Object deprecated Deprecated.
- .replace(table, data_hash) ⇒ Object deprecated Deprecated.
- .select(table, opts = {}) ⇒ Object deprecated Deprecated.
- .update(table, data_hash, where) ⇒ Object deprecated Deprecated.
- .value(data) ⇒ Object deprecated Deprecated.
- .where(*conds) ⇒ Object deprecated Deprecated.
- .where_prepared(*conds) ⇒ Object deprecated Deprecated.
Class Method Details
.check(expr, is_name = false) ⇒ Object
FIXME: Naive protection for SQL Injection TODO: check caching?
100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 |
# File 'lib/jdbc-helper/sql/sql.rb', line 100 def self.check expr, is_name = false return nil if expr.nil? tag = is_name ? 'Object name' : 'Expression' test = expr.gsub(/'[^']*'/, '').gsub(/`[^`]*`/, '').gsub(/"[^"]*"/, '').strip raise ArgumentError.new("#{tag} cannot contain (unquoted) semi-colons: #{expr}") if test.include?(';') raise ArgumentError.new("#{tag} cannot contain (unquoted) comments: #{expr}") if test.match(%r{--|/\*|\*/}) raise ArgumentError.new("Unclosed quotation mark: #{expr}") if test.match(/['"`]/) raise ArgumentError.new("#{tag} is blank") if test.empty? if is_name raise ArgumentError.new( "#{tag} cannot contain (unquoted) parentheses: #{expr}") if test.match(%r{\(|\)}) end return expr end |
.count(table, conds = nil) ⇒ Object
Generates count SQL with the given conditions
87 88 89 |
# File 'lib/jdbc-helper/sql/sql.rb', line 87 def self.count table, conds = nil SQLHelper.count :table => table, :where => conds, :prepared => false end |
.delete(table, conds = nil) ⇒ Object
Generates delete SQL with the given conditions
93 94 95 |
# File 'lib/jdbc-helper/sql/sql.rb', line 93 def self.delete table, conds = nil SQLHelper.delete :table => table, :where => conds, :prepared => false end |
.expr(sql) ⇒ JDBCHelper::SQL::Expression
Generate SQL snippet, prevents the string from being quoted.
14 15 16 |
# File 'lib/jdbc-helper/sql/expression.rb', line 14 def self.expr sql { :sql => sql } end |
.ge(v) ⇒ JDBCHelper::SQL::Expression
Greater-than-or-equal-to expression for where clauses
56 57 58 |
# File 'lib/jdbc-helper/sql/expression.rb', line 56 def self.ge v { :ge => v } end |
.gt(v) ⇒ JDBCHelper::SQL::Expression
Greater-than expression for where clauses
32 33 34 |
# File 'lib/jdbc-helper/sql/expression.rb', line 32 def self.gt v { :gt => v } end |
.insert(table, data_hash) ⇒ Object
Generates insert SQL with hash
52 53 54 |
# File 'lib/jdbc-helper/sql/sql.rb', line 52 def self.insert table, data_hash SQLHelper.insert :table => table, :data => data_hash, :prepared => false end |
.insert_ignore(table, data_hash) ⇒ Object
Generates insert ignore SQL (Non-standard syntax)
58 59 60 |
# File 'lib/jdbc-helper/sql/sql.rb', line 58 def self.insert_ignore table, data_hash SQLHelper.insert_ignore :table => table, :data => data_hash, :prepared => false end |
.le(v) ⇒ JDBCHelper::SQL::Expression
Less-than-or-equal-to expression for where clauses
48 49 50 |
# File 'lib/jdbc-helper/sql/expression.rb', line 48 def self.le v { :le => v } end |
.like(v) ⇒ JDBCHelper::SQL::Expression
Like expression for where clauses
72 73 74 75 |
# File 'lib/jdbc-helper/sql/expression.rb', line 72 def self.like v raise ArgumentError, "expected String" unless v.is_a?(String) { :like => v } end |
.lt(v) ⇒ JDBCHelper::SQL::Expression
Less-than expression for where clauses
40 41 42 |
# File 'lib/jdbc-helper/sql/expression.rb', line 40 def self.lt v { :lt => v } end |
.ne(v) ⇒ JDBCHelper::SQL::Expression
Not-equal expression for where clauses
64 65 66 |
# File 'lib/jdbc-helper/sql/expression.rb', line 64 def self.ne v { :ne => v } end |
.not_like(v) ⇒ JDBCHelper::SQL::Expression
“Not like” expression for where clauses
81 82 83 84 |
# File 'lib/jdbc-helper/sql/expression.rb', line 81 def self.not_like v raise ArgumentError, "expected String" unless v.is_a?(String) { :not => { :like => v } } end |
.not_nil ⇒ JDBCHelper::SQL::Expression Also known as: not_null
“is not null” expression for where clauses
21 22 23 |
# File 'lib/jdbc-helper/sql/expression.rb', line 21 def self.not_nil { :not => nil } end |
.order(*criteria) ⇒ Object
Generates SQL order by cluase with the given conditions.
43 44 45 |
# File 'lib/jdbc-helper/sql/sql.rb', line 43 def self.order *criteria SQLHelper.order(*criteria) end |
.replace(table, data_hash) ⇒ Object
Generates replace SQL (Non-standard syntax)
64 65 66 |
# File 'lib/jdbc-helper/sql/sql.rb', line 64 def self.replace table, data_hash SQLHelper.replace :table => table, :data => data_hash, :prepared => false end |
.select(table, opts = {}) ⇒ Object
Generates select SQL with the given conditions
77 78 79 80 81 82 83 |
# File 'lib/jdbc-helper/sql/sql.rb', line 77 def self.select table, opts = {} SQLHelper.select :table => table, :project => opts[:select], :where => opts[:where], :order => opts[:order], :prepared => false end |
.update(table, data_hash, where) ⇒ Object
Generates update SQL with hash. :where element of the given hash is taken out to generate where clause.
71 72 73 |
# File 'lib/jdbc-helper/sql/sql.rb', line 71 def self.update table, data_hash, where SQLHelper.update :table => table, :data => data_hash, :where => where, :prepared => false end |
.value(data) ⇒ Object
Formats the given data so that it can be injected into SQL
25 26 27 |
# File 'lib/jdbc-helper/sql/sql.rb', line 25 def self.value data SQLHelper.quote(data) end |
.where(*conds) ⇒ Object
Generates SQL where cluase with the given conditions. Parameter can be either Hash of String.
32 33 34 |
# File 'lib/jdbc-helper/sql/sql.rb', line 32 def self.where *conds SQLHelper.where(*conds) end |
.where_prepared(*conds) ⇒ Object
37 38 39 |
# File 'lib/jdbc-helper/sql/sql.rb', line 37 def self.where_prepared *conds SQLHelper.where_prepared(*conds) end |