跳至内容 跳至搜索
方法
S

实例公共方法

sanitize_sql(condition)

sanitize_sql_array(ary)

接受一组条件。该数组将对每个值进行清理并将其插入 SQL 语句中。如果在 SQL 语句中使用命名绑定变量,其中需要使用冒号,请使用反斜杠进行转义。

sanitize_sql_array(["name=? and group_id=?", "foo'bar", 4])
# => "name='foo''bar' and group_id=4"

sanitize_sql_array(["name=:name and group_id=:group_id", name: "foo'bar", group_id: 4])
# => "name='foo''bar' and group_id=4"

sanitize_sql_array(["TO_TIMESTAMP(:date, 'YYYY/MM/DD HH12\\:MI\\:SS')", date: "foo"])
# => "TO_TIMESTAMP('foo', 'YYYY/MM/DD HH12:MI:SS')"

sanitize_sql_array(["name='%s' and group_id='%s'", "foo'bar", 4])
# => "name='foo''bar' and group_id='4'"

请注意,这种清理方法不是模式感知的,因此不会进行任何类型转换,而是直接使用数据库适配器的 quote 方法。对于 MySQL,这意味着数字参数将被引用为字符串,以防止查询操作攻击。

sanitize_sql_array(["role = ?", 0])
# => "role = '0'"
# File activerecord/lib/active_record/sanitization.rb, line 164
def sanitize_sql_array(ary)
  statement, *values = ary
  if values.first.is_a?(Hash) && /:\w+/.match?(statement)
    with_connection do |c|
      replace_named_bind_variables(c, statement, values.first)
    end
  elsif statement.include?("?")
    with_connection do |c|
      replace_bind_variables(c, statement, values)
    end
  elsif statement.blank?
    statement
  else
    with_connection do |c|
      statement % values.collect { |value| c.quote_string(value.to_s) }
    end
  end
end

sanitize_sql_for_assignment(assignments, default_table_name = table_name)

接受 SQL 条件的数组或哈希,并将它们清理为 SET 子句的有效 SQL 片段。

sanitize_sql_for_assignment(["name=? and group_id=?", nil, 4])
# => "name=NULL and group_id=4"

sanitize_sql_for_assignment(["name=:name and group_id=:group_id", name: nil, group_id: 4])
# => "name=NULL and group_id=4"

Post.sanitize_sql_for_assignment({ name: nil, group_id: 4 })
# => "`posts`.`name` = NULL, `posts`.`group_id` = 4"

此方法不会清理 SQL 字符串,因为它不包含任何条件,并且将原样返回字符串。

sanitize_sql_for_assignment("name=NULL and group_id='4'")
# => "name=NULL and group_id='4'"

请注意,这种清理方法不是模式感知的,因此不会进行任何类型转换,而是直接使用数据库适配器的 quote 方法。对于 MySQL,这意味着数字参数将被引用为字符串,以防止查询操作攻击。

sanitize_sql_for_assignment(["role = ?", 0])
# => "role = '0'"
# File activerecord/lib/active_record/sanitization.rb, line 68
def sanitize_sql_for_assignment(assignments, default_table_name = table_name)
  case assignments
  when Array; sanitize_sql_array(assignments)
  when Hash;  sanitize_sql_hash_for_assignment(assignments, default_table_name)
  else        assignments
  end
end

sanitize_sql_for_conditions(condition)

接受一组 SQL 条件,并将它们清理为 WHERE 子句的有效 SQL 片段。

sanitize_sql_for_conditions(["name=? and group_id=?", "foo'bar", 4])
# => "name='foo''bar' and group_id=4"

sanitize_sql_for_conditions(["name=:name and group_id=:group_id", name: "foo'bar", group_id: 4])
# => "name='foo''bar' and group_id='4'"

sanitize_sql_for_conditions(["name='%s' and group_id='%s'", "foo'bar", 4])
# => "name='foo''bar' and group_id='4'"

此方法不会清理 SQL 字符串,因为它不包含任何条件,并且将原样返回字符串。

sanitize_sql_for_conditions("name='foo''bar' and group_id='4'")
# => "name='foo''bar' and group_id='4'"

请注意,这种清理方法不是模式感知的,因此不会进行任何类型转换,而是直接使用数据库适配器的 quote 方法。对于 MySQL,这意味着数字参数将被引用为字符串,以防止查询操作攻击。

sanitize_sql_for_conditions(["role = ?", 0])
# => "role = '0'"
也称为: sanitize_sql
# File activerecord/lib/active_record/sanitization.rb, line 33
def sanitize_sql_for_conditions(condition)
  return nil if condition.blank?

  case condition
  when Array; sanitize_sql_array(condition)
  else        condition
  end
end

sanitize_sql_for_order(condition)

接受 SQL 条件的数组或字符串,并将它们清理为 ORDER 子句的有效 SQL 片段。

sanitize_sql_for_order([Arel.sql("field(id, ?)"), [1,3,2]])
# => "field(id, 1,3,2)"

sanitize_sql_for_order("id ASC")
# => "id ASC"
# File activerecord/lib/active_record/sanitization.rb, line 84
def sanitize_sql_for_order(condition)
  if condition.is_a?(Array) && condition.first.to_s.include?("?")
    disallow_raw_sql!(
      [condition.first],
      permit: adapter_class.column_name_with_order_matcher
    )

    # Ensure we aren't dealing with a subclass of String that might
    # override methods we use (e.g. Arel::Nodes::SqlLiteral).
    if condition.first.kind_of?(String) && !condition.first.instance_of?(String)
      condition = [String.new(condition.first), *condition[1..-1]]
    end

    Arel.sql(sanitize_sql_array(condition))
  else
    condition
  end
end

sanitize_sql_hash_for_assignment(attrs, table)

将属性/值对的哈希清理为 SET 子句的 SQL 条件。

sanitize_sql_hash_for_assignment({ status: nil, group_id: 1 }, "posts")
# => "`posts`.`status` = NULL, `posts`.`group_id` = 1"
# File activerecord/lib/active_record/sanitization.rb, line 107
def sanitize_sql_hash_for_assignment(attrs, table)
  with_connection do |c|
    attrs.map do |attr, value|
      type = type_for_attribute(attr)
      value = type.serialize(type.cast(value))
      "#{c.quote_table_name_for_assignment(table, attr)} = #{c.quote(value)}"
    end.join(", ")
  end
end

sanitize_sql_like(string, escape_character = "\\")

清理 string,使其安全地用于 SQL LIKE 语句中。此方法使用 escape_character 对自身、“_” 和“%”的所有出现进行转义。

sanitize_sql_like("100% true!")
# => "100\\% true!"

sanitize_sql_like("snake_cased_string")
# => "snake\\_cased\\_string"

sanitize_sql_like("100% true!", "!")
# => "100!% true!!"

sanitize_sql_like("snake_cased_string", "!")
# => "snake!_cased!_string"
# File activerecord/lib/active_record/sanitization.rb, line 132
def sanitize_sql_like(string, escape_character = "\\")
  if string.include?(escape_character) && escape_character != "%" && escape_character != "_"
    string = string.gsub(escape_character, '\0\0')
  end

  string.gsub(/(?=[%_])/, escape_character)
end