Skip to content

Commit d889f4d

Browse files
feat: use prepared statements in Org Search API (#903)
* use prepared statements * use prepared statements * fix tests * use goqu postgres dialect and fix tests
1 parent 04067da commit d889f4d

File tree

2 files changed

+42
-32
lines changed

2 files changed

+42
-32
lines changed

internal/store/postgres/org_billing_repository.go

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -205,7 +205,7 @@ func prepareDataQuery(rql *rql.Query) (string, []interface{}, error) {
205205
rankedSubscriptions := getSubQuery()
206206

207207
// pick the first entry from the above subquery result
208-
baseQ := goqu.From(rankedSubscriptions.As("ranked_subscriptions")).
208+
baseQ := dialect.From(rankedSubscriptions.As("ranked_subscriptions")).Prepared(true).
209209
Select(dataQuerySelects...).Where(goqu.I(COLUMN_ROW_NUM).Eq(1))
210210

211211
withFilterQ, err := addRQLFiltersInQuery(baseQ, rql)
@@ -223,7 +223,7 @@ func prepareDataQuery(rql *rql.Query) (string, []interface{}, error) {
223223
return "", nil, fmt.Errorf("addRQLSortInQuery: %w", err)
224224
}
225225

226-
return withSortAndFilterAndSearchQ.Offset(uint(rql.Offset)).Limit(uint(rql.Limit)).ToSQL()
226+
return withSortAndFilterAndSearchQ.Offset(uint(rql.Offset)).Limit(uint(rql.Limit)).Prepared(true).ToSQL()
227227
}
228228

229229
// for each organization, fetch the last created billing_subscription entry grouped by first key in rql.GroupBy list
@@ -255,7 +255,7 @@ func prepareGroupByQuery(rql *rql.Query) (string, []interface{}, error) {
255255
rankedSubscriptions := getSubQuery()
256256

257257
// pick the first entry from the above subquery result
258-
baseQ := goqu.From(rankedSubscriptions.As("ranked_subscriptions")).
258+
baseQ := dialect.From(rankedSubscriptions.As("ranked_subscriptions")).
259259
Select(finalQuerySelects...).Where(goqu.I(COLUMN_ROW_NUM).Eq(1))
260260

261261
withFiltersQ, err := addRQLFiltersInQuery(baseQ, rql)
@@ -269,7 +269,7 @@ func prepareGroupByQuery(rql *rql.Query) (string, []interface{}, error) {
269269
}
270270

271271
finalQuery := withSearchAndFilterQ.GroupBy(groupByKey)
272-
return finalQuery.ToSQL()
272+
return finalQuery.Prepared(true).ToSQL()
273273
}
274274

275275
// prepare a subquery by left joining organizations and billing subscriptions tables
@@ -295,7 +295,7 @@ func getSubQuery() *goqu.SelectDataset {
295295
goqu.I(TABLE_BILLING_SUBSCRIPTIONS+"."+COLUMN_CREATED_AT)).As(COLUMN_ROW_NUM),
296296
}
297297

298-
rankedSubscriptions := goqu.From(TABLE_ORGANIZATIONS).
298+
rankedSubscriptions := dialect.From(TABLE_ORGANIZATIONS).
299299
Select(subquerySelects...).
300300
LeftJoin(
301301
goqu.T(TABLE_BILLING_CUSTOMERS),
@@ -357,7 +357,6 @@ func addRQLFiltersInQuery(query *goqu.SelectDataset, rqlInput *rql.Query) (*goqu
357357
}
358358

359359
func addRQLSearchInQuery(query *goqu.SelectDataset, rql *rql.Query) (*goqu.SelectDataset, error) {
360-
// this should contain only those columns that are sql string(text, varchar etc) datatype
361360
rqlSearchSupportedColumns := []string{
362361
COLUMN_ID,
363362
COLUMN_TITLE,
@@ -369,10 +368,11 @@ func addRQLSearchInQuery(query *goqu.SelectDataset, rql *rql.Query) (*goqu.Selec
369368

370369
searchExpressions := make([]goqu.Expression, 0)
371370
if rql.Search != "" {
371+
searchPattern := "%" + rql.Search + "%"
372372
for _, col := range rqlSearchSupportedColumns {
373-
searchExpressions = append(searchExpressions, goqu.L(
374-
fmt.Sprintf(`"%s"::TEXT ILIKE '%%%s%%'`, col, rql.Search),
375-
))
373+
searchExpressions = append(searchExpressions,
374+
goqu.Cast(goqu.I(col), "TEXT").ILike(searchPattern),
375+
)
376376
}
377377
}
378378
return query.Where(goqu.Or(searchExpressions...)), nil

0 commit comments

Comments
 (0)