Class: SQL::Maker

Inherits:
Object
  • Object
show all
Includes:
Util
Defined in:
lib/sql/maker.rb,
lib/sql/maker/error.rb

Defined Under Namespace

Modules: Helper, Quoting, Util Classes: Condition, Error, Select, SelectSet

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Util

#array_wrap, #bind_param, bind_param, #croak, included, #parse_args, #quote_identifier, quote_identifier

Constructor Details

#initialize(args) ⇒ Maker

Returns a new instance of Maker.



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/sql/maker.rb', line 17

def initialize(args)
  unless @driver = args[:driver].to_s.downcase
    croak(":driver is required for creating new instance of SQL::Maker")
  end
  unless @quote_char = args[:quote_char]
    @quote_char =
      if @driver == 'mysql'
        %q{`}
      else
        %q{"}
      end
  end
  @select_class = @driver == 'oracle' ? SQL::Maker::Select::Oracle : SQL::Maker::Select

  @name_sep = args[:name_sep] || '.'
  @new_line = args[:new_line] || "\n"
  @strict = args[:strict] || false
  @auto_bind = args[:auto_bind] || false # apply client-side prepared statement binding autocatically
end

Instance Attribute Details

#auto_bindObject

todo def self.load_plugin(role)

load "sql/maker/plugin/#{role}.rb"
self.include "SQL::Maker::Plugin::#{role.camelize}"

end



15
16
17
# File 'lib/sql/maker.rb', line 15

def auto_bind
  @auto_bind
end

#driverObject

todo def self.load_plugin(role)

load "sql/maker/plugin/#{role}.rb"
self.include "SQL::Maker::Plugin::#{role.camelize}"

end



15
16
17
# File 'lib/sql/maker.rb', line 15

def driver
  @driver
end

#name_sepObject

todo def self.load_plugin(role)

load "sql/maker/plugin/#{role}.rb"
self.include "SQL::Maker::Plugin::#{role.camelize}"

end



15
16
17
# File 'lib/sql/maker.rb', line 15

def name_sep
  @name_sep
end

#new_lineObject

todo def self.load_plugin(role)

load "sql/maker/plugin/#{role}.rb"
self.include "SQL::Maker::Plugin::#{role.camelize}"

end



15
16
17
# File 'lib/sql/maker.rb', line 15

def new_line
  @new_line
end

#quote_charObject

todo def self.load_plugin(role)

load "sql/maker/plugin/#{role}.rb"
self.include "SQL::Maker::Plugin::#{role.camelize}"

end



15
16
17
# File 'lib/sql/maker.rb', line 15

def quote_char
  @quote_char
end

#select_classObject

todo def self.load_plugin(role)

load "sql/maker/plugin/#{role}.rb"
self.include "SQL::Maker::Plugin::#{role.camelize}"

end



15
16
17
# File 'lib/sql/maker.rb', line 15

def select_class
  @select_class
end

#strictObject

todo def self.load_plugin(role)

load "sql/maker/plugin/#{role}.rb"
self.include "SQL::Maker::Plugin::#{role.camelize}"

end



15
16
17
# File 'lib/sql/maker.rb', line 15

def strict
  @strict
end

Instance Method Details

#_make_where_clause(where = nil) ⇒ Object



232
233
234
235
236
237
238
# File 'lib/sql/maker.rb', line 232

def _make_where_clause(where = nil)
  return ['', []] unless where

  w = self._make_where_condition(where)
  sql = w.as_sql
  return [sql.empty? ? "" : " WHERE #{sql}", array_wrap(w.bind)]
end

#_make_where_condition(where = nil) ⇒ Object



210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
# File 'lib/sql/maker.rb', line 210

def _make_where_condition(where = nil)
  return self.new_condition unless where
  if where.respond_to?(:as_sql)
    return where
  end

  cond = self.new_condition
  while true
    col, val =
      if where.is_a?(Hash)
        where.shift
      elsif where.is_a?(Array)
        where.slice!(0, 2)
      else
        [where, nil]
      end
    break unless col
    cond.add(col => val)
  end
  return cond
end

#_quote(label) ⇒ Object



119
120
121
# File 'lib/sql/maker.rb', line 119

def _quote(label) 
  SQL::Maker::Util::quote_identifier(label, self.quote_char, self.name_sep)
end

#delete(*args) ⇒ Object



123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
# File 'lib/sql/maker.rb', line 123

def delete(*args)
  table, where, opt =
    if args.size == 1 and args.first.is_a?(Hash)
      args = args.first.dup
      [args.delete(:table), args.delete(:where) || {}, args]
    else
      [args[0], args[1] || {}, args[2] || {}]
    end

  w = self._make_where_clause(where)
  quoted_table = self._quote(table)
  sql = "DELETE FROM #{quoted_table}"
  if opt[:using]
    # bulder.delete('foo', \%where, { :using => 'bar' })
    # bulder.delete('foo', \%where, { :using => ['bar', 'qux'] })
    tables = array_wrap(opt[:using])
    sql += " USING " + tables.map {|t| self._quote(t) }.join(', ')
  end
  sql += w[0]

  @auto_bind ? bind_param(sql, w[1]) : [sql, w[1]]
end

#insert(*args) ⇒ Object



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
# File 'lib/sql/maker.rb', line 54

def insert(*args)
  table, values, opt =
    if args.size == 1 and args.first.is_a?(Hash)
      args = args.first.dup
      [args.delete(:table), args.delete(:values) || {}, args]
    else
      [args[0], args[1] || {}, args[2] || {}]
    end

  prefix = opt[:prefix] || 'INSERT INTO'

  quoted_table = self._quote(table)
  quoted_columns = []
  columns = []
  bind_columns = []

  while true
    col, val =
      if values.is_a?(Hash)
        values.shift
      elsif values.is_a?(Array)
        values.slice!(0, 2)
      elsif values.nil?
        [nil, nil]
      else
        [values.first, nil]
      end
    break unless col
    quoted_columns += [self._quote(col)]
    if val.respond_to?(:as_sql)
      columns += [val.as_sql(nil, Proc.new {|e| self._quote(e) })]
      bind_columns += val.bind
    else
      if val.is_a?(Array) && self.strict
        croak("cannot pass in an unblessed ref as an argument in strict mode")
      end

      if val.is_a?(Array)
        # builder.insert( :foo => { :created_on => ["NOW()"] })
        # builder.insert( :foo => [ 'UNIX_TIMESTAMP(?)', '2011-04-12 00:34:12' ] )
        stmt, sub_bind = [val.first, val[1..-1]]
        columns += [stmt]
        bind_columns += sub_bind
      else
        # normal values
        columns += ['?']
        bind_columns += [val]
      end
    end
  end

  # Insert an empty record in SQLite.
  # ref. https://github.com/tokuhirom/SQL-Maker/issues/11
  if self.driver == 'sqlite' && columns.empty?
    sql  = "#{prefix} #{quoted_table}" + self.new_line + 'DEFAULT VALUES'
    return [sql, []]
  end

  sql  = "#{prefix} #{quoted_table}" + self.new_line
  sql += '(' + quoted_columns.join(', ') + ')' + self.new_line +
    'VALUES (' + columns.join(', ') + ')'

  @auto_bind ? bind_param(sql, bind_columns) : [sql, bind_columns]
end

#make_set_clause(args) ⇒ Object

make “SET” clause.



167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
# File 'lib/sql/maker.rb', line 167

def make_set_clause(args)
  columns = []
  bind_columns = []
  while true
    col, val =
      if args.is_a?(Hash)
        args.shift
      elsif args.is_a?(Array)
        args.slice!(0, 2)
      else
        [args, nil]
      end
    break unless col
    quoted_col = self._quote(col)
    if val.respond_to?(:as_sql)
      columns += ["#{quoted_col} = " + val.as_sql(nil, Proc.new {|label| self._quote(label) })]
      bind_columns += val.bind
    else
      if val.is_a?(Array) && self.strict
        croak("cannot pass in an unblessed ref as an argument in strict mode")
      end

      if val.is_a?(Array)
        # builder.update( :foo => [ 'NOW()' ] )
        # builder.update( :foo => [ 'VALUES(foo) + ?', 10 ] )
        stmt, sub_bind = [val.first, val[1..-1]]
        columns += ["#{quoted_col} = " + stmt]
        bind_columns += sub_bind
      else
        # normal values
        columns += ["#{quoted_col} = ?"]
        bind_columns += [val]
      end
    end
  end
  return [columns, bind_columns]
end

#new_conditionObject



37
38
39
40
41
42
43
# File 'lib/sql/maker.rb', line 37

def new_condition
  SQL::Maker::Condition.new(
    :quote_char => self.quote_char,
    :name_sep   => self.name_sep,
    :strict     => self.strict,
  )
end

#new_select(args = {}) ⇒ Object



45
46
47
48
49
50
51
52
# File 'lib/sql/maker.rb', line 45

def new_select(args = {})
  return self.select_class.new({
    :name_sep   => self.name_sep,
    :quote_char => self.quote_char,
    :new_line   => self.new_line,
    :strict     => self.strict,
  }.merge(args))
end

#select(*args) ⇒ Object

my(stmt, @bind) = sql−>select(table, @fields, %where, %opt)



241
242
243
244
245
# File 'lib/sql/maker.rb', line 241

def select(*args)
  stmt = self.select_query(*args)

  @auto_bind ? bind_param(stmt.as_sql, stmt.bind) : [stmt.as_sql, stmt.bind]
end

#select_query(*args) ⇒ Object



247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
# File 'lib/sql/maker.rb', line 247

def select_query(*args)
  table, fields, where, opt =
    if args.size == 1 and args.first.is_a?(Hash)
      args = args.first.dup
      [args.delete(:table), args.delete(:fields) || [], args.delete(:where) || {}, args]
    else
      [args[0], args[1] || [], args[2] || {}, args[3] || {}]
    end

  unless fields.is_a?(Array)
    croak("SQL::Maker::select_query: fields should be Array")
  end

  stmt = self.new_select
  fields.each do |field|
    stmt.add_select(field)
  end

  if table
    if table.is_a?(Array)
      table.each do |t|
        stmt.add_from(t)
      end
    else
      stmt.add_from( table )
    end
  end

  stmt.prefix(opt[:prefix]) if opt[:prefix]

  if where
    stmt.set_where(self._make_where_condition(where))
  end

  if joins = opt[:joins]
    joins.each do |join|
      stmt.add_join(join)
    end
  end

  if o = opt[:order_by]
    if o.is_a?(Array)
      o.each do |order|
        if order.is_a?(Hash)
          # Skinny-ish [{:foo => 'DESC'}, {:bar => 'ASC'}]
          stmt.add_order_by(order)
        else
          # just ['foo DESC', 'bar ASC']
          stmt.add_order_by(order)
        end
      end
    elsif o.is_a?(Hash)
      # Skinny-ish {:foo => 'DESC'}
      stmt.add_order_by(o)
    else
      # just 'foo DESC, bar ASC'
      stmt.add_order_by(o)
    end
  end
  if o = opt[:group_by]
    if o.is_a?(Array)
      o.each do | group|
        if group.is_a?(Hash)
          # Skinny-ish [{:foo => 'DESC'}, {:bar => 'ASC'}]
          stmt.add_group_by(group)
        else
          # just ['foo DESC', 'bar ASC']
          stmt.add_group_by(group)
        end
      end
    elsif o.is_a?(Hash)
      # Skinny-ish {:foo => 'DESC'end
      stmt.add_group_by(o)
    else
      # just 'foo DESC, bar ASC'
      stmt.add_group_by(o)
    end
  end
  if o = opt[:index_hint]
    stmt.add_index_hint(table, o)
  end

  stmt.limit(opt[:limit])    if opt[:limit]
  stmt.offset(opt[:offset])  if opt[:offset]

  if terms = opt[:having]
    term.each do |col, val|
      stmt.add_having(:col => val)
    end
  end

  stmt.for_update(1) if opt[:for_update]
  return stmt
end

#update(*args) ⇒ Object



146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
# File 'lib/sql/maker.rb', line 146

def update(*args)
  table, values, where, opt =
    if args.size == 1 and args.first.is_a?(Hash)
      args = args.first.dup
      [args.delete(:table), args.delete(:set) || {}, args.delete(:where) || {}, args]
    else
      [args[0], args[1] || {}, args[2] || {}, args[3] || {}]
    end

  columns, bind_columns = self.make_set_clause(values)

  w = self._make_where_clause(where)
  bind_columns += array_wrap(w[1])

  quoted_table = self._quote(table)
  sql = "UPDATE #{quoted_table} SET " + columns.join(', ') + w[0]

  @auto_bind ? bind_param(sql, bind_columns) : [sql, bind_columns]
end

#where(where) ⇒ Object



205
206
207
208
# File 'lib/sql/maker.rb', line 205

def where(where)
  cond = self._make_where_condition(where)
  return [cond.as_sql, cond.bind]
end