Module: ActiveRecord::ConnectionAdapters::Quoting
- Included in:
- AbstractAdapter
- Defined in:
- lib/active_record/connection_adapters/abstract/quoting.rb
Instance Method Summary collapse
-
#quote(value, column = nil) ⇒ Object
Quotes the column value to help prevent SQL injection attacks.
-
#quote_column_name(column_name) ⇒ Object
Quotes the column name.
-
#quote_string(s) ⇒ Object
Quotes a string, escaping any ‘ (single quote) and \ (backslash) characters.
-
#quote_table_name(table_name) ⇒ Object
Quotes the table name.
- #quoted_date(value) ⇒ Object
- #quoted_false ⇒ Object
- #quoted_true ⇒ Object
Instance Method Details
#quote(value, column = nil) ⇒ Object
Quotes the column value to help prevent SQL injection attacks.
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
# File 'lib/active_record/connection_adapters/abstract/quoting.rb', line 8 def quote(value, column = nil) # records are quoted as their primary key return value.quoted_id if value.respond_to?(:quoted_id) case value when String, ActiveSupport::Multibyte::Chars value = value.to_s if column && column.type == :binary && column.class.respond_to?(:string_to_binary) "'#{quote_string(column.class.string_to_binary(value))}'" # ' (for ruby-mode) elsif column && [:integer, :float].include?(column.type) value = column.type == :integer ? value.to_i : value.to_f value.to_s else "'#{quote_string(value)}'" # ' (for ruby-mode) end when NilClass then "NULL" when TrueClass then (column && column.type == :integer ? '1' : quoted_true) when FalseClass then (column && column.type == :integer ? '0' : quoted_false) when Float, Fixnum, Bignum then value.to_s # BigDecimals need to be output in a non-normalized form and quoted. when BigDecimal then value.to_s('F') when Symbol then "'#{quote_string(value.to_s)}'" else if value.acts_like?(:date) || value.acts_like?(:time) "'#{quoted_date(value)}'" else "'#{quote_string(value.to_yaml)}'" end end end |
#quote_column_name(column_name) ⇒ Object
Quotes the column name. Defaults to no quoting.
46 47 48 |
# File 'lib/active_record/connection_adapters/abstract/quoting.rb', line 46 def quote_column_name(column_name) column_name end |
#quote_string(s) ⇒ Object
Quotes a string, escaping any ‘ (single quote) and \ (backslash) characters.
41 42 43 |
# File 'lib/active_record/connection_adapters/abstract/quoting.rb', line 41 def quote_string(s) s.gsub(/\\/, '\&\&').gsub(/'/, "''") # ' (for ruby-mode) end |
#quote_table_name(table_name) ⇒ Object
Quotes the table name. Defaults to column name quoting.
51 52 53 |
# File 'lib/active_record/connection_adapters/abstract/quoting.rb', line 51 def quote_table_name(table_name) quote_column_name(table_name) end |
#quoted_date(value) ⇒ Object
63 64 65 66 67 68 69 70 |
# File 'lib/active_record/connection_adapters/abstract/quoting.rb', line 63 def quoted_date(value) if value.acts_like?(:time) zone_conversion_method = ActiveRecord::Base.default_timezone == :utc ? :getutc : :getlocal value.respond_to?(zone_conversion_method) ? value.send(zone_conversion_method) : value else value end.to_s(:db) end |
#quoted_false ⇒ Object
59 60 61 |
# File 'lib/active_record/connection_adapters/abstract/quoting.rb', line 59 def quoted_false "'f'" end |
#quoted_true ⇒ Object
55 56 57 |
# File 'lib/active_record/connection_adapters/abstract/quoting.rb', line 55 def quoted_true "'t'" end |