class SqlStmt

unless there is something better to return, methods return self so they can be chained together

Attributes

data[R]

Public Class Methods

new() click to toggle source
# File lib/sqlstmt/sqlstmt.rb, line 19
def initialize
  @data = SqlStmtLib::SqlData.new
end

Public Instance Methods

any_join(kwstr, ref, exprs) click to toggle source
# File lib/sqlstmt/sqlstmt.rb, line 83
def any_join(kwstr, ref, exprs)
  tbl = include_table(ref)
  @data.joins << SqlStmtLib::SqlJoin.new(kwstr, tbl, "ON #{exprs.join(' AND ')}")
  return self
end
delete(*tables) click to toggle source
# File lib/sqlstmt/sqlstmt.rb, line 43
def delete(*tables)
  type('delete')
  @data.tables_to_delete = tables
  return self
end
includes_table?(table_to_find) click to toggle source
# File lib/sqlstmt/sqlstmt.rb, line 89
def includes_table?(table_to_find)
  return @data.table_ids.include?(table_to_find)
end
initialize_copy(_orig) click to toggle source
Calls superclass method
# File lib/sqlstmt/sqlstmt.rb, line 23
def initialize_copy(_orig)
  # I don't really know why this is necessary, but it seems to be
  super
  @data = @data.dup
end
insert() click to toggle source
# File lib/sqlstmt/sqlstmt.rb, line 39
def insert
  return type('insert')
end
join(table, *exprs) click to toggle source
# File lib/sqlstmt/sqlstmt.rb, line 75
def join(table, *exprs)
  return any_join('JOIN', table, exprs)
end
left_join(table, *exprs) click to toggle source
# File lib/sqlstmt/sqlstmt.rb, line 79
def left_join(table, *exprs)
  return any_join('LEFT JOIN', table, exprs)
end
no_where() click to toggle source

if any where clauses have been added, the statement will fail to build

# File lib/sqlstmt/sqlstmt.rb, line 105
def no_where
  @data.where_behavior = :exclude
  return self
end
optional_where() click to toggle source

this disables a where clause requirement

# File lib/sqlstmt/sqlstmt.rb, line 111
def optional_where
  @data.where_behavior = :optional
  return self
end
require_where() click to toggle source

if no where clauses have been added, the statement will fail to build this is the default value, but in case it got changed, we can reset it back

# File lib/sqlstmt/sqlstmt.rb, line 99
def require_where
  @data.where_behavior = :require
  return self
end
select() click to toggle source

pick statement type

# File lib/sqlstmt/sqlstmt.rb, line 31
def select
  return type('select')
end
set(field, value) click to toggle source

nil can be passed in for the field, in which case it won't be added this is only for the special case of INSERT INTO table SELECT b.* FROM blah b WHERE … where there are no specific fields listed

# File lib/sqlstmt/sqlstmt.rb, line 121
def set(field, value)
  if @data.set_fields.include?(field)
    raise SqlStmtError, "trying to set field #{field} again"
  end

  if field
    @data.set_fields << field
  end
  value = value.is_a?(String) ? value : value.to_sql
  @data.set_values << value
  return self
end
setq(field, value) click to toggle source
# File lib/sqlstmt/sqlstmt.rb, line 134
def setq(field, value)
  return set(field, value.to_sql)
end
switch_type(new_type, clear_set_calls = true) click to toggle source

switch statement type

# File lib/sqlstmt/sqlstmt.rb, line 59
def switch_type(new_type, clear_set_calls = true)
  @data.stmt_type = new_type
  if clear_set_calls
    @data.set_fields.clear
    @data.set_values.clear
  end
  return self
end
table(ref, use_index = nil) click to toggle source

tables & joins

# File lib/sqlstmt/sqlstmt.rb, line 70
def table(ref, use_index = nil)
  @data.tables << include_table(ref, use_index)
  return self
end
to_s() click to toggle source
# File lib/sqlstmt/build.rb, line 5
def to_s
  SqlStmtLib::MysqlChecker.new(@data).run
  return SqlStmtLib::MysqlBuilder.new(@data).build_stmt
end
Also aliased as: to_sql
to_sql()
Alias for: to_s
type(stmt_type) click to toggle source
# File lib/sqlstmt/sqlstmt.rb, line 49
def type(stmt_type)
  if @data.stmt_type && @data.stmt_type != stmt_type
    raise SqlStmtError, "statement type already set to #{@data.stmt_type}"
  end
  @data.stmt_type = stmt_type
  return self
end
update() click to toggle source
# File lib/sqlstmt/sqlstmt.rb, line 35
def update
  return type('update')
end

Private Instance Methods

include_table(ref, use_index = nil) click to toggle source

this is used for method calls to :table and :any_join

# File lib/sqlstmt/sqlstmt.rb, line 163
def include_table(ref, use_index = nil)
  parts = ref.split(' ')
  if parts.size == 3
    parts.delete_at(1)
  end
  @data.table_ids.merge(parts)

  if parts.size == 2
    tbl_name, tbl_alias = parts
  else
    tbl_name = tbl_alias = parts.first
  end
  return SqlStmtLib::SqlTable.new(ref, tbl_name, tbl_alias, use_index)
end