module SQB::Escaping

Private Instance Methods

escape(name) click to toggle source
# File lib/sqb/escaping.rb, line 6
def escape(name)
  if name.is_a?(SafeString)
    name
  elsif name == SQB::STAR
    "*"
  else
    "`#{name.to_s.gsub('`', '``')}`"
  end
end
escape_and_join(*parts) click to toggle source
# File lib/sqb/escaping.rb, line 57
def escape_and_join(*parts)
  if parts.last.is_a?(SafeString)
    # If a safe string is provided as a column name, we'll
    # always use this even if a table name is provided too.
    parts.last
  else
    parts.compact.map { |part| escape(part) }.join('.')
  end
end
escape_function(name) click to toggle source
# File lib/sqb/escaping.rb, line 16
def escape_function(name)
  if name.is_a?(SafeString)
    name
  else
    name.to_s.gsub(/[^a-z0-9\_]/i, '').upcase
  end
end
value_escape(value) click to toggle source
# File lib/sqb/escaping.rb, line 24
def value_escape(value)
  if value == true
    1
  elsif value == false
    0
  elsif value.nil?
    'NULL'
  elsif value.is_a?(Integer)
    value.to_i
  elsif value.is_a?(SQB::SafeString)
    value.to_s
  else
    if @options[:escaper]
      @options[:escaper].call(value.to_s)
    else
      @prepared_arguments << value.to_s
      '?'
    end
  end
end
with_table_and_column(input, &block) click to toggle source
# File lib/sqb/escaping.rb, line 45
def with_table_and_column(input, &block)
  if input.is_a?(Hash)
    if input.size == 1
      block.call(input.first[0], input.first[1])
    else
      raise QueryError, "Column names provided as a hash must only contain a single item"
    end
  else
    block.call(@table_name, input)
  end
end