提交 d31c2e2c 编写于 作者: R Ryuta Kamizono

Extract `type_casted_binds` method

Because `type_cast` against `binds` always requires
`attr.value_for_database` and this pattern appears frequently.
上级 a8a3a8cc
......@@ -156,6 +156,10 @@ def prepare_binds_for_database(binds) # :nodoc:
private
def type_casted_binds(binds)
binds.map { |attr| type_cast(attr.value_for_database) }
end
def types_which_need_no_typecasting
[nil, Numeric, String]
end
......
......@@ -77,7 +77,7 @@ def exec_stmt_and_free(sql, name, binds, cache_stmt: false)
@connection.query_options[:database_timezone] = ActiveRecord::Base.default_timezone
end
type_casted_binds = binds.map { |attr| type_cast(attr.value_for_database) }
type_casted_binds = type_casted_binds(binds)
log(sql, name, binds, type_casted_binds) do
if cache_stmt
......
......@@ -597,13 +597,13 @@ def execute_and_clear(sql, name, binds, prepare: false)
end
def exec_no_cache(sql, name, binds)
type_casted_binds = binds.map { |attr| type_cast(attr.value_for_database) }
type_casted_binds = type_casted_binds(binds)
log(sql, name, binds, type_casted_binds) { @connection.async_exec(sql, type_casted_binds) }
end
def exec_cache(sql, name, binds)
stmt_key = prepare_statement(sql)
type_casted_binds = binds.map { |attr| type_cast(attr.value_for_database) }
type_casted_binds = type_casted_binds(binds)
log(sql, name, binds, type_casted_binds, stmt_key) do
@connection.exec_prepared(stmt_key, type_casted_binds)
......
......@@ -188,7 +188,7 @@ def explain(arel, binds = [])
end
def exec_query(sql, name = nil, binds = [], prepare: false)
type_casted_binds = binds.map { |attr| type_cast(attr.value_for_database) }
type_casted_binds = type_casted_binds(binds)
log(sql, name, binds, type_casted_binds) do
# Don't cache statements if they are not prepared
......@@ -203,7 +203,6 @@ def exec_query(sql, name = nil, binds = [], prepare: false)
ensure
stmt.close
end
stmt = records
else
cache = @statements[sql] ||= {
:stmt => @connection.prepare(sql)
......@@ -212,9 +211,10 @@ def exec_query(sql, name = nil, binds = [], prepare: false)
cols = cache[:cols] ||= stmt.columns
stmt.reset!
stmt.bind_params(type_casted_binds)
records = stmt.to_a
end
ActiveRecord::Result.new(cols, stmt.to_a)
ActiveRecord::Result.new(cols, records)
end
end
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册