From 59ce7144e8846ee42e87ea5f4ec3e3b91f050ba9 Mon Sep 17 00:00:00 2001 From: automated-signal <37887102+automated-signal@users.noreply.github.com> Date: Wed, 6 Nov 2024 14:06:07 -0600 Subject: [PATCH] Use classes for QueryFragment in sql Co-authored-by: Fedor Indutny <79877362+indutny-signal@users.noreply.github.com> --- ts/sql/util.ts | 27 +++++++++++++++------------ 1 file changed, 15 insertions(+), 12 deletions(-) diff --git a/ts/sql/util.ts b/ts/sql/util.ts index ca0ce0fa55f..452e163b579 100644 --- a/ts/sql/util.ts +++ b/ts/sql/util.ts @@ -1,5 +1,6 @@ // Copyright 2021 Signal Messenger, LLC // SPDX-License-Identifier: AGPL-3.0-only +/* eslint-disable max-classes-per-file */ import { isNumber, last } from 'lodash'; import type { ReadableDB, WritableDB } from './Interface'; @@ -44,10 +45,12 @@ export type QueryTemplateParam = | undefined; export type QueryFragmentValue = QueryFragment | QueryTemplateParam; -export type QueryFragment = [ - { fragment: string }, - ReadonlyArray, -]; +export class QueryFragment { + constructor( + public readonly fragment: string, + public readonly fragmentParams: ReadonlyArray + ) {} +} /** * You can use tagged template literals to build "fragments" of SQL queries @@ -79,8 +82,8 @@ export function sqlFragment( query += string; if (index < values.length) { - if (Array.isArray(value)) { - const [{ fragment }, fragmentParams] = value; + if (value instanceof QueryFragment) { + const { fragment, fragmentParams } = value; query += fragment; params.push(...fragmentParams); } else { @@ -90,7 +93,7 @@ export function sqlFragment( } }); - return [{ fragment: query }, params]; + return new QueryFragment(query, params); } export function sqlConstant(value: QueryTemplateParam): QueryFragment { @@ -104,7 +107,7 @@ export function sqlConstant(value: QueryTemplateParam): QueryFragment { } else { fragment = `'${value}'`; } - return [{ fragment }, []]; + return new QueryFragment(fragment, []); } /** @@ -118,7 +121,7 @@ export function sqlJoin( const params: Array = []; items.forEach((item, index) => { - const [{ fragment }, fragmentParams] = sqlFragment`${item}`; + const { fragment, fragmentParams } = sqlFragment`${item}`; query += fragment; params.push(...fragmentParams); @@ -127,7 +130,7 @@ export function sqlJoin( } }); - return [{ fragment: query }, params]; + return new QueryFragment(query, params); } export type QueryTemplate = [string, ReadonlyArray]; @@ -155,8 +158,8 @@ export function sql( strings: TemplateStringsArray, ...values: Array ): QueryTemplate { - const [{ fragment }, params] = sqlFragment(strings, ...values); - return [fragment, params]; + const { fragment, fragmentParams } = sqlFragment(strings, ...values); + return [fragment, fragmentParams]; } type QueryPlanRow = Readonly<{