我有一个复杂的连接条件,其中三个内插值被双引号括起来(我认为,作为 AR 引用以避免 SQL 注入攻击的一部分)。
我已经准备好其他问题,说我可以使用:sanitize_sql
或:sanitize_sql_array
与?
占位符一起使用,但是唉,这些似乎是未定义的activerecord-sqlserver-adapter
。
所以,我的问题是,我怎样才能通过 ActiveRecord 与 SQL Server 一起工作?
def self.with_active_deal_counts
joins(<<-SQL.squish
LEFT OUTER JOIN (
SELECT [b].[merchant_id], COUNT(*) deal_count
FROM [tbl_deal] b
WHERE deal_active = 1
AND deal_archive = 2
AND ( deal_end = '1900-01-01'
OR deal_end > '#{Time.now + Time.now.utc_offset - 1.day}')
AND deal_start <= '#{Time.now + Time.now.utc_offset}'
GROUP BY [b].[merchant_id]
) a ON a.[merchant_id] = [tbl_merchant].[merchant_id]
SQL
).select("tbl_merchant.*, a.deal_count")
end
这产生
EXEC sp_executesql N'SELECT tbl_merchant.*, a.deal_count FROM [tbl_merchant] LEFT OUTER JOIN (
SELECT [b].[merchant_id], COUNT(*) deal_count
FROM [tbl_deal] b
WHERE deal_active = 1
AND deal_archive = 2
AND ( deal_end = ''1900-01-01''
OR deal_end > ''2013-09-30 07:07:20 -0700'')
AND deal_start <= ''2013-10-01 07:07:20 -0700''
GROUP BY [b].[merchant_id]
) a ON a.[merchant_id] = [tbl_merchant].[merchant_id]'
...由于双引号值而不起作用,例如。''2013-09-30 07:07:20 -0700''
具体来说,我得到Incorrect syntax near '1900'
(在 SQL Management Studio 中运行查询时。