我有以下查詢。
invalid_records
.unscoped
.group(:timezone)
.pluck(<<~INFO_SQL)
timezone as timezone,
count(1)
FILTER(WHERE control_test_id IN (#{table_ids_params})
且local_run_at為空
和時區=時區)
AS records_count。
array_agg(distinct control_test_id)
FILTER(WHERE control_test_id IN (["?", #{table_ids_params}] )
作為control_test_ids。
array_agg(distinct run_at::text)
FILTER(WHERE timezone = timezone
且local_run_at為空)
AS invalid_run_times
INFO_SQL
def invalid_records
范圍=Trigger
.where(control_test_id: table_ids)
.where(local_run_at: nil)
.where.not(run_at: nil)
scope = scope.where(timezone: timezone) if timezone
scope = scope.where(run_at: run_at) if run_at
范圍
結束 運行_at
class Trigger < ApplicationRecord
default_scope lambda {
if $flipper[: explicit_id_ordering].enabled?
order(position: :asc, updated_at: :asc, id: :asc)
else[/span
order(position: :asc)
末尾
}
# 一些代碼
end
查詢必須以下列格式回傳結果:
{
"Berlin": {
total: 323, # total count of triggers with given param
無效: 100, # `local_run_at`欄位為空的觸發器的數量。
table_ids: [23, 4343, 34, 44], # 無效觸發器所屬表的id。
invalid_run_times: ["14:00"/span>, "23:00"/span>, "12:30"/span>] # 無效觸發器的運行時間串列。
},
"波多黎各": {
總計: 3,
無效: 1,
table_ids: [77] 。
invalid_run_times: ["13:00"].
}
}
在過濾器FILTER(where control_test_id IN (#{table_ids_params})需要得到所有id在table_ids_params中的控制測驗。變數table_ids_params對于防止SQL注入來說是不安全的? 有什么方法可以解決嗎?
uj5u.com熱心網友回復:
根據你更新的資訊,在我看來,你不需要通過control_test_id來過濾(再次),因為過濾器被應用到where條件。你不需要在過濾器中添加時區,因為它是由GROUP BY子句暗示的。
然后在你的鏈的開始有一個令人驚訝的unscoped,為什么你要取消一個范圍?(更新:基于你定義的默認作用域,你只需要洗掉排序)
如果你對你的代碼進行如下修改,你應該能夠獲得正確的資料。
invalid_records
.unscope(:oorder)
.group(:timezone)
.pluck(<<~INFO_SQL)
時區 作為時區。
count(1)
FILTER(WHERE local_run_at is null)
and timezone = timezone)
AS records_count。
array_agg(distinct control_test_id)
AS control_test_ids。
array_agg(distinct run_at::text)
FILTER(WHERE local_run_at is null)
AS invalid_run_times
INFO_SQL
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/332303.html
標籤:
