Active Record 連線適配器之引號處理
方法
- Q
- T
- U
實例公開方法
quote(value) 連結
將欄位值加上引號,以防止 SQL 注入攻擊。
來源:顯示 | 在 GitHub 上
# File activerecord/lib/active_record/connection_adapters/abstract/quoting.rb, line 73 def quote(value) case value when String, Symbol, 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 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 Class then "'#{value}'" else raise TypeError, "can't quote #{value.class.name}" end end
quote_column_name(column_name) 連結
將欄位名稱加上引號。
來源:顯示 | 在 GitHub 上
# File activerecord/lib/active_record/connection_adapters/abstract/quoting.rb, line 136 def quote_column_name(column_name) self.class.quote_column_name(column_name) end
quote_string(s) 連結
將字串加上引號,並將任何 '(單引號)和 \(反斜線)字元跳脫。
來源:顯示 | 在 GitHub 上
# File activerecord/lib/active_record/connection_adapters/abstract/quoting.rb, line 131 def quote_string(s) s.gsub("\\", '\&\&').gsub("'", "''") # ' (for ruby-mode) end
quote_table_name(table_name) 連結
將表格名稱加上引號。
來源:顯示 | 在 GitHub 上
# File activerecord/lib/active_record/connection_adapters/abstract/quoting.rb, line 141 def quote_table_name(table_name) self.class.quote_table_name(table_name) end
quote_table_name_for_assignment(table, attr) 連結
覆寫以返回賦值時所使用的引號括起的表格名稱。預設為表格引號處理。
這適用於 MySQL
,其中可以使用 table.column 來解決歧義。
我們在 sqlite3 和 postgresql 適配器中覆寫此方法,以僅使用欄位名稱(根據語法要求)。
來源:顯示 | 在 GitHub 上
# File activerecord/lib/active_record/connection_adapters/abstract/quoting.rb, line 153 def quote_table_name_for_assignment(table, attr) quote_table_name("#{table}.#{attr}") end
quoted_date(value) 連結
將日期/時間值加上引號,以用於 SQL 輸入。如果值是具有 usec 方法的 Time
,則包含微秒。
來源:顯示 | 在 GitHub 上
# File activerecord/lib/active_record/connection_adapters/abstract/quoting.rb, line 184 def quoted_date(value) if value.acts_like?(:time) if default_timezone == :utc value = value.getutc if !value.utc? else value = value.getlocal end end result = value.to_fs(:db) if value.respond_to?(:usec) && value.usec > 0 result << "." << sprintf("%06d", value.usec) else result end end
quoted_false() 連結
來源:顯示 | 在 GitHub 上
# File activerecord/lib/active_record/connection_adapters/abstract/quoting.rb, line 174 def quoted_false "FALSE" end
quoted_true() 連結
來源:顯示 | 在 GitHub 上
# File activerecord/lib/active_record/connection_adapters/abstract/quoting.rb, line 166 def quoted_true "TRUE" end
type_cast(value) 連結
來源:顯示 | 在 GitHub 上
# File activerecord/lib/active_record/connection_adapters/abstract/quoting.rb, line 94 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 nil, Numeric, String then value when Type::Time::Value then quoted_time(value) when Date, Time then quoted_date(value) else raise TypeError, "can't cast #{value.class.name}" end end
unquoted_false() 連結
來源:顯示 | 在 GitHub 上
# File activerecord/lib/active_record/connection_adapters/abstract/quoting.rb, line 178 def unquoted_false false end
unquoted_true() 連結
來源:顯示 | 在 GitHub 上
# File activerecord/lib/active_record/connection_adapters/abstract/quoting.rb, line 170 def unquoted_true true end