Class: SQL::QueryMaker

Inherits:
Object
  • Object
show all
Includes:
Maker::Util
Defined in:
lib/sql/query_maker.rb

Constant Summary collapse

FNOP =
{
  'is_null' => 'IS NULL',
  'is_not_null' => 'IS NOT NULL',
  'eq' => '= ?',
  'ne' => '!= ?',
  'lt' => '< ?',
  'gt' => '> ?',
  'le' => '<= ?',
  'ge' => '>= ?',
  'like' => 'LIKE ?',
  'between' => 'BETWEEN ? AND ?',
  'not_between' => 'NOT BETWEEN ? AND ?',
  'not' => 'NOT @',
}

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Maker::Util

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

Constructor Details

#initialize(column, as_sql, bind) ⇒ QueryMaker

Returns a new instance of QueryMaker.



184
185
186
187
188
189
190
191
192
# File 'lib/sql/query_maker.rb', line 184

def initialize(column, as_sql, bind)
  bind = bind.nil? ? [] : array_wrap(bind)
  bind.each do |b|
    croak("cannot bind an array or an hash") if b.is_a?(Array) or b.is_a?(Hash)
  end
  @column = column
  @as_sql = as_sql
  @bind  = bind
end

Instance Attribute Details

#as_sql(supplied_colname = nil, quote_cb = nil) ⇒ Object

Returns the value of attribute as_sql.



183
184
185
# File 'lib/sql/query_maker.rb', line 183

def as_sql
  @as_sql
end

#bindObject

Returns the value of attribute bind.



183
184
185
# File 'lib/sql/query_maker.rb', line 183

def bind
  @bind
end

#columnObject

Returns the value of attribute column.



183
184
185
# File 'lib/sql/query_maker.rb', line 183

def column
  @column
end

Class Method Details

._compile_builder(expr) ⇒ Object



171
172
173
174
175
176
177
178
179
180
# File 'lib/sql/query_maker.rb', line 171

def _compile_builder(expr)
  # substitute the column character
  expr = "@ #{expr}" if expr !~ /@/
  num_args = expr.count('?')
  exprs = expr.split(/@/, -1)
  builder = Proc.new {|quoted_column|
    exprs.join(quoted_column)
  }
  return [num_args, builder]
end

._sql_op(fn, builder, column, bind) ⇒ Object



157
158
159
160
161
162
# File 'lib/sql/query_maker.rb', line 157

def _sql_op(fn, builder, column, bind)
  return SQL::QueryMaker.new(column, Proc.new {|column, quote_cb|
    croak("no column binding for fn(bind...)") unless column
    term = builder.call(quote_cb.call(column))
  }, bind)
end

.sql_op(*args) ⇒ Object

sql_op(‘IN (SELECT foo_id FROM bar WHERE t=?)’, [44]) sql_op(‘foo’,‘IN (SELECT foo_id FROM bar WHERE t=?)’, [44])



150
151
152
153
154
155
# File 'lib/sql/query_maker.rb', line 150

def sql_op(*args)
  column, expr, bind = (args.size >= 3 ? args : [nil] + args)
  (num_bind, builder) = _compile_builder(expr)
  croak("the operator expects num_bind but got #{bind.size}") if num_bind != bind.size
  return _sql_op("sql_op", builder, column, bind)
end

.sql_raw(*args) ⇒ Object

sql_raw(‘SELECT foo_id FROM bar WHERE t=44’) sql_raw(‘SELECT foo_id FROM bar WHERE t=?’, [44])



166
167
168
169
# File 'lib/sql/query_maker.rb', line 166

def sql_raw(*args)
  sql, bind = parse_args(*args)
  return SQL::QueryMaker.new(nil, Proc.new { sql }, bind)
end

Instance Method Details

#bind_column(column = nil) ⇒ Object



194
195
196
197
198
199
# File 'lib/sql/query_maker.rb', line 194

def bind_column(column = nil)
  if column
    croak('cannot rebind column for \`' + self.column + "` to: `column`") if self.column
  end
  @column = column
end

#quote_identifier(label) ⇒ Object



207
208
209
# File 'lib/sql/query_maker.rb', line 207

def quote_identifier(label)
  label.to_s.split(/\./).map {|e| "`#{e}`"}.join('.')
end