module Rebel::SQLB

Public Instance Methods

and_clause(*clause) click to toggle source
# File lib/rebel/sql.rb, line 344
def and_clause(*clause)
  clause.map do |e|
    case e
    when Hash then and_clause(*e.to_a)
    when Array then clause_term(e[0], e[1])
    when Raw then e.parens?
    when String then e
    else raise NotImplementedError, e.class
    end
  end.join(' AND ')
end
assign_clause(clause) click to toggle source
# File lib/rebel/sql.rb, line 329
def assign_clause(clause)
  list(clause.map { |k, v| equal(k, v) })
end
by(*n) click to toggle source
# File lib/rebel/sql.rb, line 248
def by(*n)
  raw("BY #{names(*n)}")
end
clause_term(left, right) click to toggle source
# File lib/rebel/sql.rb, line 333
def clause_term(left, right)
  case right
  when Array
    name(left).in(*right)
  when nil
    name(left).is(name_or_value(right))
  else
    name(left).eq(name_or_value(right))
  end
end
count(*n) click to toggle source
# File lib/rebel/sql.rb, line 252
def count(*n)
  raw("COUNT(#{names(*n)})")
end
create_table(table_name, desc) click to toggle source
# File lib/rebel/sql.rb, line 185
def create_table(table_name, desc)
  raw %[CREATE TABLE #{name(table_name)} (#{list(desc.map { |k, v| "#{name(k)} #{v}" })})]
end
delete_from(table_name, where: nil, inner: nil, left: nil, right: nil) click to toggle source
# File lib/rebel/sql.rb, line 227
def delete_from(table_name, where: nil, inner: nil, left: nil, right: nil)
  raw [
    "DELETE FROM #{name(table_name)}",
    inner?(inner),
    left?(left),
    right?(right),
    where?(where),
  ].join(' ')
end
drop_table(table_name) click to toggle source
# File lib/rebel/sql.rb, line 189
def drop_table(table_name)
  raw "DROP TABLE #{name(table_name)}"
end
equal(l, r) click to toggle source
# File lib/rebel/sql.rb, line 325
def equal(l, r)
  "#{name_or_value(l)} = #{name_or_value(r)}"
end
escape_str(str) click to toggle source
# File lib/rebel/sql.rb, line 294
def escape_str(str)
  str.dup.tap do |s|
    s.gsub!('\\') { @escaped_string_backslash } if @escaped_string_backslash
    s.gsub!(@string_quote) { @escaped_string_quote }
  end
end
fn(name, *args)
Alias for: function
from?(from) click to toggle source
# File lib/rebel/sql.rb, line 356
def from?(from)
  from ? "FROM #{name(from)}" : nil
end
function(name, *args) click to toggle source

Functions

# File lib/rebel/sql.rb, line 243
def function(name, *args)
  raw("#{name}(#{names_or_values(*args)})")
end
Also aliased as: fn
group?(group) click to toggle source
# File lib/rebel/sql.rb, line 376
def group?(group)
  group ? "GROUP #{name(group)}" : nil
end
inner?(join) click to toggle source
# File lib/rebel/sql.rb, line 364
def inner?(join)
  join ? "INNER #{join}" : nil
end
inner_join(table, on: nil) click to toggle source
# File lib/rebel/sql.rb, line 264
def inner_join(table, on: nil)
  raw(inner? join(table, on: on))
end
insert_into(table_name, *rows) click to toggle source
# File lib/rebel/sql.rb, line 207
def insert_into(table_name, *rows)
  raw [
    "INSERT INTO #{name(table_name)} (#{names(*rows.first.keys)})",
    "VALUES #{list(rows.map { |r| "(#{values(*r.values)})" })}",
  ].join(' ')
end
join(table, on: nil) click to toggle source
# File lib/rebel/sql.rb, line 256
def join(table, on: nil)
  raw("JOIN #{name(table)}").on?(on)
end
left?(join) click to toggle source
# File lib/rebel/sql.rb, line 368
def left?(join)
  join ? "LEFT #{join}" : nil
end
left_outer_join(table, on: nil) click to toggle source
# File lib/rebel/sql.rb, line 268
def left_outer_join(table, on: nil)
  raw(left? outer_join(table, on: on))
end
limit?(limit, offset) click to toggle source
# File lib/rebel/sql.rb, line 384
def limit?(limit, offset)
  limit ? "LIMIT #{value(limit)}" << (offset ? " OFFSET #{offset}" : "") : nil
end
list(*items) click to toggle source
# File lib/rebel/sql.rb, line 290
def list(*items)
  items.join(', ')
end
name(name = nil) click to toggle source

Support

Calls superclass method
# File lib/rebel/sql.rb, line 278
def name(name = nil)
  super() if name.nil? # workaround for pry and introspection
  return name if name.is_a?(Raw)
  return raw('*') if name == :*

  raw(name.to_s.split('.').map { |e| "#{@identifier_quote}#{e}#{@identifier_quote}" }.join('.'))
end
name_or_value(item) click to toggle source
# File lib/rebel/sql.rb, line 317
def name_or_value(item)
  item.is_a?(Symbol) ? name(item) : value(item)
end
names(*names) click to toggle source
# File lib/rebel/sql.rb, line 286
def names(*names)
  list(names.map { |k| name(k) })
end
names_or_values(*items) click to toggle source
# File lib/rebel/sql.rb, line 321
def names_or_values(*items)
  list(items.map { |v| name_or_value(v) })
end
order?(order) click to toggle source
# File lib/rebel/sql.rb, line 380
def order?(order)
  order ? "ORDER #{name(order)}" : nil
end
outer_join(table, on: nil) click to toggle source
# File lib/rebel/sql.rb, line 260
def outer_join(table, on: nil)
  raw("OUTER JOIN #{name(table)}").on?(on)
end
raw(str) click to toggle source
# File lib/rebel/sql.rb, line 181
def raw(str)
  Raw.new(str).tap { |r| r.instance_variable_set(:@sql, self) }
end
right?(join) click to toggle source
# File lib/rebel/sql.rb, line 372
def right?(join)
  join ? "RIGHT #{join}" : nil
end
right_outer_join(table, on: nil) click to toggle source
# File lib/rebel/sql.rb, line 272
def right_outer_join(table, on: nil)
  raw(right? outer_join(table, on: on))
end
select(*fields, distinct: nil, from: nil, where: nil, inner: nil, left: nil, right: nil, group: nil, order: nil, limit: nil, offset: nil) click to toggle source
# File lib/rebel/sql.rb, line 193
def select(*fields, distinct: nil, from: nil, where: nil, inner: nil, left: nil, right: nil, group: nil, order: nil, limit: nil, offset: nil)
  raw [
    "SELECT #{distinct ? "DISTINCT #{names(*distinct)}" : names(*fields)}",
    from?(from),
    inner?(inner),
    left?(left),
    right?(right),
    where?(where),
    group?(group),
    order?(order),
    limit?(limit, offset),
  ].compact.join(' ')
end
truncate(table_name) click to toggle source
# File lib/rebel/sql.rb, line 237
def truncate(table_name)
  raw "TRUNCATE #{name(table_name)}"
end
update(table_name, set: nil, where: nil, inner: nil, left: nil, right: nil) click to toggle source
# File lib/rebel/sql.rb, line 214
def update(table_name, set: nil, where: nil, inner: nil, left: nil, right: nil)
  raise ArgumentError if set.nil?

  raw [
    "UPDATE #{name(table_name)}",
    "SET #{assign_clause(set)}",
    inner?(inner),
    left?(left),
    right?(right),
    where?(where),
  ].compact.join(' ')
end
value(v) click to toggle source
# File lib/rebel/sql.rb, line 301
def value(v)
  case v
  when Raw then v
  when String then raw "#{@string_quote}#{escape_str(v)}#{@string_quote}"
  when Integer then raw v.to_s
  when TrueClass, FalseClass then raw(v ? @true_literal : @false_literal)
  when Date, Time, DateTime then value(v.iso8601)
  when nil then raw 'NULL'
  else raise NotImplementedError, "#{v.class}: #{v.inspect}"
  end
end
values(*values) click to toggle source
# File lib/rebel/sql.rb, line 313
def values(*values)
  list(values.map { |v| value(v) })
end
where?(*clause) click to toggle source
# File lib/rebel/sql.rb, line 360
def where?(*clause)
  clause.any? ? "WHERE #{and_clause(*clause)}" : nil
end