module ActiveRecord::ConnectionAdapters::Quoting

def _quote(value)

def _quote(value)
  case value
  when String, ActiveSupport::Multibyte::Chars
    "'#{quote_string(value.to_s)}'"
  when true       then quoted_true
  when false      then quoted_false
  when nil        then "NULL"
  # BigDecimals need to be put in a non-normalized form and quoted.
  when BigDecimal then value.to_s("F")
  when Numeric, ActiveSupport::Duration then value.to_s
  when Type::Binary::Data then quoted_binary(value)
  when Type::Time::Value then "'#{quoted_time(value)}'"
  when Date, Time then "'#{quoted_date(value)}'"
  when Symbol     then "'#{quote_string(value.to_s)}'"
  when Class      then "'#{value}'"
  else raise TypeError, "can't quote #{value.class.name}"
  end
end

def _type_cast(value)

def _type_cast(value)
  case value
  when Symbol, ActiveSupport::Multibyte::Chars, Type::Binary::Data
    value.to_s
  when true       then unquoted_true
  when false      then unquoted_false
  # BigDecimals need to be put in a non-normalized form and quoted.
  when BigDecimal then value.to_s("F")
  when Type::Time::Value then quoted_time(value)
  when Date, Time then quoted_date(value)
  when *types_which_need_no_typecasting
    value
  else raise TypeError
  end
end

def id_value_for_database(value)

def id_value_for_database(value)
  if primary_key = value.class.primary_key
    value.instance_variable_get(:@attributes)[primary_key].value_for_database
  end
end

def lookup_cast_type(sql_type)

def lookup_cast_type(sql_type)
  type_map.lookup(sql_type)
end

def lookup_cast_type_from_column(column) # :nodoc:

:nodoc:
See docs for #type_cast_from_column
def lookup_cast_type_from_column(column) # :nodoc:
  lookup_cast_type(column.sql_type)
end

def quote(value)

{SQL injection attacks}[https://en.wikipedia.org/wiki/SQL_injection].
Quotes the column value to help prevent
def quote(value)
  value = id_value_for_database(value) if value.is_a?(Base)
  if value.respond_to?(:value_for_database)
    value = value.value_for_database
  end
  _quote(value)
end

def quote_column_name(column_name)

Quotes the column name. Defaults to no quoting.
def quote_column_name(column_name)
  column_name.to_s
end

def quote_default_expression(value, column) # :nodoc:

:nodoc:
def quote_default_expression(value, column) # :nodoc:
  if value.is_a?(Proc)
    value.call
  else
    value = lookup_cast_type(column.sql_type).serialize(value)
    quote(value)
  end
end

def quote_string(s)

characters.
Quotes a string, escaping any ' (single quote) and \ (backslash)
def quote_string(s)
  s.gsub('\\'.freeze, '\&\&'.freeze).gsub("'".freeze, "''".freeze) # ' (for ruby-mode)
end

def quote_table_name(table_name)

Quotes the table name. Defaults to column name quoting.
def quote_table_name(table_name)
  quote_column_name(table_name)
end

def quote_table_name_for_assignment(table, attr)

the column name (as per syntax requirements).
We override this in the sqlite3 and postgresql adapters to use only

resolve ambiguity.
This works for mysql2 where table.column can be used to

table quoting.
Override to return the quoted table name for assignment. Defaults to
def quote_table_name_for_assignment(table, attr)
  quote_table_name("#{table}.#{attr}")
end

def quoted_binary(value) # :nodoc:

:nodoc:
def quoted_binary(value) # :nodoc:
  "'#{quote_string(value.to_s)}'"
end

def quoted_date(value)

if the value is a Time responding to usec.
Quote date/time values for use in SQL input. Includes microseconds
def quoted_date(value)
  if value.acts_like?(:time)
    zone_conversion_method = ActiveRecord::Base.default_timezone == :utc ? :getutc : :getlocal
    if value.respond_to?(zone_conversion_method)
      value = value.send(zone_conversion_method)
    end
  end
  result = value.to_s(:db)
  if value.respond_to?(:usec) && value.usec > 0
    "#{result}.#{sprintf("%06d", value.usec)}"
  else
    result
  end
end

def quoted_false

def quoted_false
  "FALSE".freeze
end

def quoted_time(value) # :nodoc:

:nodoc:
def quoted_time(value) # :nodoc:
  value = value.change(year: 2000, month: 1, day: 1)
  quoted_date(value).sub(/\A\d\d\d\d-\d\d-\d\d /, "")
end

def quoted_true

def quoted_true
  "TRUE".freeze
end

def type_cast(value, column = nil)

to a String.
SQLite does not understand dates, so this method will convert a Date
Cast a +value+ to a type that the database understands. For example,
def type_cast(value, column = nil)
  value = id_value_for_database(value) if value.is_a?(Base)
  if column
    value = type_cast_from_column(column, value)
  end
  _type_cast(value)
rescue TypeError
  to_type = column ? " to #{column.type}" : ""
  raise TypeError, "can't cast #{value.class}#{to_type}"
end

def type_cast_from_column(column, value) # :nodoc:

:nodoc:
should have been provided before reaching the connection adapter.
(varchar vs binary) for example. The type used to get this primitive
represent the type doesn't sufficiently reflect the differences
In almost all cases, the sql type should only be used to change quoting behavior, when the primitive to
AttributeMethods::Serialization::ClassMethods#serialize, +time_zone_aware_attributes+).
Attributes::ClassMethods#attribute) or implicitly (via
provided a custom type on the class level either explicitly (via
wrong. The column does not have sufficient type information if the user
If you are having to call this function, you are likely doing something
def type_cast_from_column(column, value) # :nodoc:
  if column
    type = lookup_cast_type_from_column(column)
    type.serialize(value)
  else
    value
  end
end

def type_casted_binds(binds) # :nodoc:

:nodoc:
def type_casted_binds(binds) # :nodoc:
  if binds.first.is_a?(Array)
    binds.map { |column, value| type_cast(value, column) }
  else
    binds.map { |attr| type_cast(attr.value_for_database) }
  end
end

def types_which_need_no_typecasting

def types_which_need_no_typecasting
  [nil, Numeric, String]
end

def unquoted_false

def unquoted_false
  false
end

def unquoted_true

def unquoted_true
  true
end