Skip to content

🐸 Rybbit - open-source and privacy-friendly alternative to Google Analytics that is 10x more intuitive.

Refactor SQL queries in UsageService for parameterized execution

Bill Yangdb3d6ef866ec
diff --git a/server/src/services/usageService.ts b/server/src/services/usageService.ts
index 87672892f..cc84e5af7 100644
--- a/server/src/services/usageService.ts
+++ b/server/src/services/usageService.ts
@@ -163,10 +163,14 @@ class UsageService {
query: `
SELECT COUNT(*) as count
FROM events
- WHERE site_id IN (${grandfatheredSites.join(",")}) AND type = 'pageview'
- AND timestamp >= toDate('${periodStart}')
+ WHERE site_id IN {siteIds:Array(Int32)} AND type = 'pageview'
+ AND timestamp >= toDate({periodStart:String})
`,
format: "JSONEachRow",
+ query_params: {
+ siteIds: grandfatheredSites,
+ periodStart: periodStart,
+ },
});
const grandfatheredRows = await processResults<{ count: string }>(grandfatheredResult);
totalCount += parseInt(grandfatheredRows[0].count, 10);
@@ -178,11 +182,15 @@ class UsageService {
query: `
SELECT COUNT(*) as count
FROM events
- WHERE site_id IN (${newSites.join(",")})
+ WHERE site_id IN {siteIds:Array(Int32)}
AND type IN ('pageview', 'custom_event', 'performance')
- AND timestamp >= toDate('${periodStart}')
+ AND timestamp >= toDate({periodStart:String})
`,
format: "JSONEachRow",
+ query_params: {
+ siteIds: newSites,
+ periodStart: periodStart,
+ },
});
const newSitesRows = await processResults<{ count: string }>(newSitesResult);
totalCount += parseInt(newSitesRows[0].count, 10);