Module: JDBCHelper::SQLPrepared

Defined in:
lib/jdbc-helper/sql/sql_prepared.rb

Overview

WARNING: Does not perform SQL.check to minimize performance overhead

Class Method Summary collapse

Class Method Details

.count(table, conds = nil) ⇒ Object

Generates count SQL with the given conditions



64
65
66
67
68
69
# File 'lib/jdbc-helper/sql/sql_prepared.rb', line 64

def self.count table, conds = nil
  w_c, w_b = where_internal(conds)
  sql = "select count(*) from #{table} #{w_c}".strip

  return sql, w_b
end

.delete(table, conds = nil) ⇒ Object

Generates delete SQL with the given conditions



72
73
74
75
76
# File 'lib/jdbc-helper/sql/sql_prepared.rb', line 72

def self.delete table, conds = nil
  w_c, w_b = where_internal(conds)
  sql =  "delete from #{table} #{w_c}".strip
  return sql, w_b
end

.insert(table, data_hash) ⇒ Object

SQL Helpers

Generates insert SQL with hash



17
18
19
# File 'lib/jdbc-helper/sql/sql_prepared.rb', line 17

def self.insert table, data_hash
  insert_internal 'insert', table, data_hash
end

.insert_ignore(table, data_hash) ⇒ Object

Generates insert ignore SQL (Non-standard syntax)



22
23
24
# File 'lib/jdbc-helper/sql/sql_prepared.rb', line 22

def self.insert_ignore table, data_hash
  insert_internal 'insert ignore', table, data_hash
end

.replace(table, data_hash) ⇒ Object

Generates replace SQL (Non-standard syntax)



27
28
29
# File 'lib/jdbc-helper/sql/sql_prepared.rb', line 27

def self.replace table, data_hash
  insert_internal 'replace', table, data_hash
end

.select(table, opts = {}) ⇒ Object

Generates select SQL with the given conditions



51
52
53
54
55
56
57
58
59
60
61
# File 'lib/jdbc-helper/sql/sql_prepared.rb', line 51

def self.select table, opts = {}
  opts = opts.reject { |k, v| v.nil? }
  w_c, w_b = where_internal(opts.fetch(:where, {}))
  sql = [
    "select #{opts.fetch(:select, ['*']).join(', ')} from #{table}", 
    w_c.to_s,
    SQL.order(opts.fetch(:order, []).join(', '))
  ].reject(&:empty?).join(' ')

  return sql, w_b
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.



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/jdbc-helper/sql/sql_prepared.rb', line 33

def self.update table, data_hash, where
  where_clause, where_binds = where_internal where

  col_binds = []
  sql = ("update #{table} set " + data_hash.map { |k, v|
    case v
    when JDBCHelper::SQL::ScalarExpression
      "#{k} = #{v}"
    else
      col_binds << v
      "#{k} = ?"
    end
  }.join(', ') + " #{where_clause}").strip

  return sql, col_binds + where_binds
end

.where(*conds) ⇒ Object

Generates SQL where cluase with the given conditions. Parameter can be either Hash of String.



10
11
12
# File 'lib/jdbc-helper/sql/sql_prepared.rb', line 10

def self.where *conds
  where_internal conds
end