@contember/database 2.0.0 → 2.1.0-alpha.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/development/index.cjs +1 -0
- package/dist/development/index.cjs.map +1 -1
- package/dist/development/index.js +2 -1
- package/dist/development/src/builders/ColumnExpressionFactory.cjs +16 -6
- package/dist/development/src/builders/ColumnExpressionFactory.cjs.map +1 -1
- package/dist/development/src/builders/ColumnExpressionFactory.js +12 -2
- package/dist/development/src/builders/ColumnExpressionFactory.js.map +1 -1
- package/dist/development/src/builders/ConditionBuilder.cjs +24 -15
- package/dist/development/src/builders/ConditionBuilder.cjs.map +1 -1
- package/dist/development/src/builders/ConditionBuilder.js +17 -8
- package/dist/development/src/builders/ConditionBuilder.js.map +1 -1
- package/dist/development/src/builders/SelectBuilder.cjs +1 -2
- package/dist/development/src/builders/SelectBuilder.cjs.map +1 -1
- package/dist/development/src/builders/SelectBuilder.js +2 -3
- package/dist/development/src/builders/SelectBuilder.js.map +1 -1
- package/dist/development/src/builders/WindowFunction.cjs +12 -2
- package/dist/development/src/builders/WindowFunction.cjs.map +1 -1
- package/dist/development/src/builders/WindowFunction.js +12 -2
- package/dist/development/src/builders/WindowFunction.js.map +1 -1
- package/dist/development/src/builders/internal/Returning.cjs +10 -2
- package/dist/development/src/builders/internal/Returning.cjs.map +1 -1
- package/dist/development/src/builders/internal/Returning.js +10 -2
- package/dist/development/src/builders/internal/Returning.js.map +1 -1
- package/dist/development/src/builders/utils.cjs +12 -3
- package/dist/development/src/builders/utils.cjs.map +1 -1
- package/dist/development/src/builders/utils.js +13 -4
- package/dist/development/src/builders/utils.js.map +1 -1
- package/dist/development/src/client/Client.cjs +1 -1
- package/dist/development/src/client/Client.js +1 -1
- package/dist/development/src/utils/createDatabaseIfNotExists.cjs +1 -1
- package/dist/development/src/utils/createDatabaseIfNotExists.js +1 -1
- package/dist/development/src/utils/retryTransaction.cjs +1 -1
- package/dist/development/src/utils/retryTransaction.js +1 -1
- package/dist/development/src/utils/sql.cjs +8 -0
- package/dist/development/src/utils/sql.cjs.map +1 -1
- package/dist/development/src/utils/sql.js +8 -0
- package/dist/development/src/utils/sql.js.map +1 -1
- package/dist/production/index.cjs +1 -0
- package/dist/production/index.cjs.map +1 -1
- package/dist/production/index.js +2 -1
- package/dist/production/src/builders/ColumnExpressionFactory.cjs +16 -6
- package/dist/production/src/builders/ColumnExpressionFactory.cjs.map +1 -1
- package/dist/production/src/builders/ColumnExpressionFactory.js +12 -2
- package/dist/production/src/builders/ColumnExpressionFactory.js.map +1 -1
- package/dist/production/src/builders/ConditionBuilder.cjs +24 -15
- package/dist/production/src/builders/ConditionBuilder.cjs.map +1 -1
- package/dist/production/src/builders/ConditionBuilder.js +17 -8
- package/dist/production/src/builders/ConditionBuilder.js.map +1 -1
- package/dist/production/src/builders/SelectBuilder.cjs +1 -2
- package/dist/production/src/builders/SelectBuilder.cjs.map +1 -1
- package/dist/production/src/builders/SelectBuilder.js +2 -3
- package/dist/production/src/builders/SelectBuilder.js.map +1 -1
- package/dist/production/src/builders/WindowFunction.cjs +12 -2
- package/dist/production/src/builders/WindowFunction.cjs.map +1 -1
- package/dist/production/src/builders/WindowFunction.js +12 -2
- package/dist/production/src/builders/WindowFunction.js.map +1 -1
- package/dist/production/src/builders/internal/Returning.cjs +10 -2
- package/dist/production/src/builders/internal/Returning.cjs.map +1 -1
- package/dist/production/src/builders/internal/Returning.js +10 -2
- package/dist/production/src/builders/internal/Returning.js.map +1 -1
- package/dist/production/src/builders/utils.cjs +12 -3
- package/dist/production/src/builders/utils.cjs.map +1 -1
- package/dist/production/src/builders/utils.js +13 -4
- package/dist/production/src/builders/utils.js.map +1 -1
- package/dist/production/src/client/Client.cjs +1 -1
- package/dist/production/src/client/Client.js +1 -1
- package/dist/production/src/utils/createDatabaseIfNotExists.cjs +1 -1
- package/dist/production/src/utils/createDatabaseIfNotExists.js +1 -1
- package/dist/production/src/utils/retryTransaction.cjs +1 -1
- package/dist/production/src/utils/retryTransaction.js +1 -1
- package/dist/production/src/utils/sql.cjs +8 -0
- package/dist/production/src/utils/sql.cjs.map +1 -1
- package/dist/production/src/utils/sql.js +8 -0
- package/dist/production/src/utils/sql.js.map +1 -1
- package/dist/tests/tsconfig.tsbuildinfo +1 -1
- package/dist/types/builders/ColumnExpressionFactory.d.ts.map +1 -1
- package/dist/types/builders/ConditionBuilder.d.ts +2 -2
- package/dist/types/builders/ConditionBuilder.d.ts.map +1 -1
- package/dist/types/builders/SelectBuilder.d.ts.map +1 -1
- package/dist/types/builders/WindowFunction.d.ts.map +1 -1
- package/dist/types/builders/utils.d.ts +0 -2
- package/dist/types/builders/utils.d.ts.map +1 -1
- package/dist/types/index.d.ts +1 -1
- package/dist/types/index.d.ts.map +1 -1
- package/dist/types/tsconfig.tsbuildinfo +1 -1
- package/dist/types/utils/sql.d.ts +1 -0
- package/dist/types/utils/sql.d.ts.map +1 -1
- package/package.json +3 -3
- package/src/builders/ColumnExpressionFactory.ts +3 -3
- package/src/builders/ConditionBuilder.ts +14 -15
- package/src/builders/SelectBuilder.ts +4 -4
- package/src/builders/WindowFunction.ts +3 -2
- package/src/builders/internal/Returning.ts +2 -2
- package/src/builders/utils.ts +2 -4
- package/src/index.ts +1 -0
- package/src/utils/sql.ts +8 -0
- package/dist/development/src/builders/formatUtils.cjs +0 -23
- package/dist/development/src/builders/formatUtils.cjs.map +0 -1
- package/dist/development/src/builders/formatUtils.js +0 -23
- package/dist/development/src/builders/formatUtils.js.map +0 -1
- package/dist/production/src/builders/formatUtils.cjs +0 -23
- package/dist/production/src/builders/formatUtils.cjs.map +0 -1
- package/dist/production/src/builders/formatUtils.js +0 -23
- package/dist/production/src/builders/formatUtils.js.map +0 -1
- package/dist/types/builders/formatUtils.d.ts +0 -4
- package/dist/types/builders/formatUtils.d.ts.map +0 -1
- package/src/builders/formatUtils.ts +0 -17
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"ConditionBuilder.cjs","sources":["../../../../../packages/database/src/builders/ConditionBuilder.ts"],"sourcesContent":["import { Value } from '../types'\nimport { Literal } from '../Literal'\nimport { QueryBuilder } from './QueryBuilder'\nimport { SelectBuilder } from './SelectBuilder'\nimport { toFqnWrap } from './formatUtils'\nimport { Compiler } from './Compiler'\nimport { createSubQueryLiteralFactory, SubQueryExpression } from './internal/Subqueries'\n\nexport type ConditionCallback = (builder: ConditionBuilder) => ConditionBuilder\nexport type ConditionExpression = ConditionBuilder | ConditionCallback\n\nexport enum Operator {\n\t'notEq' = '!=',\n\t'eq' = '=',\n\t'gt' = '>',\n\t'gte' = '>=',\n\t'lt' = '<',\n\t'lte' = '<=',\n\t'contains' = 'contains',\n\t'startsWith' = 'startsWith',\n\t'endsWith' = 'endsWith',\n\n\t'containsCI' = 'containsCI',\n\t'startsWithCI' = 'startsWithCI',\n\t'endsWithCI' = 'endsWithCI',\n}\n\nconst likeOperators = [\n\tOperator.contains,\n\tOperator.containsCI,\n\tOperator.startsWith,\n\tOperator.startsWithCI,\n\tOperator.endsWith,\n\tOperator.endsWithCI,\n]\n\nconst isReadonlyArray = (arr: any): arr is readonly any[] => Array.isArray(arr)\n\nexport class ConditionBuilder {\n\tprivate constructor(public readonly expressions: Literal[]) {}\n\n\tpublic static create(expressions: Literal[] = []) {\n\t\treturn new ConditionBuilder(expressions)\n\t}\n\n\tpublic static process(condition: ConditionExpression): ConditionBuilder {\n\t\treturn typeof condition === 'function' ? condition(ConditionBuilder.create()) : condition\n\t}\n\n\tpublic static not(condition: ConditionExpression): ConditionBuilder {\n\t\treturn ConditionBuilder.create().not(condition)\n\t}\n\n\tand(condition: ConditionExpression): ConditionBuilder {\n\t\treturn this.processConditionExpression(ConditionBuilder.process(condition), ['and', false])\n\t}\n\n\tor(condition: ConditionExpression): ConditionBuilder {\n\t\treturn this.processConditionExpression(ConditionBuilder.process(condition), ['or', false])\n\t}\n\n\tnot(condition: ConditionExpression): ConditionBuilder {\n\t\treturn this.processConditionExpression(ConditionBuilder.process(condition), ['and', true])\n\t}\n\n\tcompare(columnName: QueryBuilder.ColumnIdentifier, operator: Operator, value: Value): ConditionBuilder {\n\t\tif (!Object.values(Operator).includes(operator)) {\n\t\t\tthrow new Error(`Operator ${operator} is not supported`)\n\t\t}\n\n\t\tif (likeOperators.includes(operator)) {\n\t\t\tif (typeof value !== 'string') {\n\t\t\t\tthrow new Error(`Operator ${operator} supports only a string value`)\n\t\t\t}\n\t\t\tvalue = value.replace(/([\\\\%_])/g, v => '\\\\' + v)\n\t\t}\n\t\treturn this.with(new Literal(ConditionBuilder.createOperatorSql(toFqnWrap(columnName), '?', operator), [value]))\n\t}\n\n\tcolumnsEq(columnName1: QueryBuilder.ColumnIdentifier, columnName2: QueryBuilder.ColumnIdentifier): ConditionBuilder {\n\t\treturn this.compareColumns(columnName1, Operator.eq, columnName2)\n\t}\n\n\tcompareColumns(columnName1: QueryBuilder.ColumnIdentifier, operator: Operator, columnName2: QueryBuilder.ColumnIdentifier) {\n\t\treturn this.with(new Literal(ConditionBuilder.createOperatorSql(toFqnWrap(columnName1), toFqnWrap(columnName2), operator)))\n\t}\n\n\tin(columnName: QueryBuilder.ColumnIdentifier, values: ReadonlyArray<Value> | SelectBuilder<SelectBuilder.Result>, columnType?: string): ConditionBuilder {\n\t\tif (!isReadonlyArray(values)) {\n\t\t\t// todo: replace placeholder with some kind of callback\n\t\t\tconst query = values.createQuery(new Compiler.Context(Compiler.SCHEMA_PLACEHOLDER, new Set()))\n\t\t\treturn this.with(new Literal(`${toFqnWrap(columnName)} in (${query.sql})`, query.parameters))\n\t\t}\n\t\tvalues = values.filter(it => it !== undefined)\n\t\tif (columnType && values.length > 100) {\n\t\t\treturn this.with(new Literal(`${toFqnWrap(columnName)} = any(?::${columnType}[])`, [values]))\n\t\t}\n\t\tif (values.length > 0) {\n\t\t\tconst parameters = values.map(() => '?').join(', ')\n\t\t\treturn this.with(new Literal(`${toFqnWrap(columnName)} in (${parameters})`, values))\n\t\t}\n\t\treturn this.raw('false')\n\t}\n\n\texists(subQuery: SubQueryExpression<SelectBuilder>): ConditionBuilder {\n\t\tconst context = new Compiler.Context(Compiler.SCHEMA_PLACEHOLDER, new Set())\n\t\tconst query = createSubQueryLiteralFactory<SelectBuilder>(subQuery)(context, subQuery => {\n\t\t\tif (subQuery.options.select.length === 0) {\n\t\t\t\treturn subQuery.select(expr => expr.raw('?::int', 1))\n\t\t\t}\n\t\t\treturn subQuery\n\t\t})\n\t\treturn this.with(new Literal(`exists (${query.sql})`, query.parameters))\n\t}\n\n\tisNull(columnName: QueryBuilder.ColumnIdentifier): ConditionBuilder {\n\t\treturn this.with(new Literal(`${toFqnWrap(columnName)} is null`))\n\t}\n\n\tisNotNull(columnName: QueryBuilder.ColumnIdentifier): ConditionBuilder {\n\t\treturn this.with(new Literal(`${toFqnWrap(columnName)} is not null`))\n\t}\n\n\traw(sql: string, ...bindings: Value[]): ConditionBuilder {\n\t\treturn this.with(new Literal(sql, bindings))\n\t}\n\n\twith(expression?: Literal | null | undefined): ConditionBuilder {\n\t\tif (!expression) {\n\t\t\treturn this\n\t\t}\n\t\treturn new ConditionBuilder([...this.expressions, expression])\n\t}\n\n\tpublic getSql(): Literal | null {\n\t\treturn ConditionBuilder.createLiteral(this.expressions)\n\t}\n\n\tisEmpty(): boolean {\n\t\treturn this.expressions.length === 0\n\t}\n\n\tprivate processConditionExpression(builder: ConditionBuilder, parameters: ['and' | 'or', boolean]): ConditionBuilder {\n\t\tconst sql = ConditionBuilder.createLiteral(builder.expressions, ...parameters)\n\t\tif (sql) {\n\t\t\treturn new ConditionBuilder([...this.expressions, sql])\n\t\t}\n\t\treturn this\n\t}\n\n\tprivate static createOperatorSql(left: string, right: string, operator: Operator): string {\n\t\tif (!Object.values(Operator).includes(operator)) {\n\t\t\tthrow new Error(`Operator ${operator} is not supported`)\n\t\t}\n\t\tswitch (operator) {\n\t\t\tcase Operator.contains:\n\t\t\t\treturn `${left} LIKE '%' || ${right} || '%'`\n\t\t\tcase Operator.containsCI:\n\t\t\t\treturn `${left} ILIKE '%' || ${right} || '%'`\n\t\t\tcase Operator.startsWith:\n\t\t\t\treturn `${left} LIKE ${right} || '%'`\n\t\t\tcase Operator.startsWithCI:\n\t\t\t\treturn `${left} ILIKE ${right} || '%'`\n\t\t\tcase Operator.endsWith:\n\t\t\t\treturn `${left} LIKE '%' || ${right}`\n\t\t\tcase Operator.endsWithCI:\n\t\t\t\treturn `${left} ILIKE '%' || ${right}`\n\t\t}\n\t\treturn `${left} ${operator} ${right}`\n\t}\n\n\tprivate static createLiteral(expressions: Literal[], operator: 'or' | 'and' = 'and', not: boolean = false): Literal | null {\n\t\tif (expressions.length === 0) {\n\t\t\treturn null\n\t\t}\n\t\tconst sql = expressions.map(it => it.sql).join(` ${operator} `)\n\n\t\tconst bindings: Value[] = ([] as Value[]).concat(...expressions.map(it => it.parameters))\n\n\t\treturn new Literal(not ? `not(${sql})` : operator === 'or' ? `(${sql})` : sql, bindings)\n\t}\n}\n"],"names":["Operator","Literal","toFqnWrap","Compiler","createSubQueryLiteralFactory","subQuery"],"mappings":";;;;;;AAWY,IAAA,6BAAAA,cAAL;AACNA,YAAA,OAAU,IAAA;AACVA,YAAA,IAAO,IAAA;AACPA,YAAA,IAAO,IAAA;AACPA,YAAA,KAAQ,IAAA;AACRA,YAAA,IAAO,IAAA;AACPA,YAAA,KAAQ,IAAA;AACRA,YAAA,UAAa,IAAA;AACbA,YAAA,YAAe,IAAA;AACfA,YAAA,UAAa,IAAA;AAEbA,YAAA,YAAe,IAAA;AACfA,YAAA,cAAiB,IAAA;AACjBA,YAAA,YAAe,IAAA;AAbJA,SAAAA;AAAA,GAAA,YAAA,CAAA,CAAA;AAgBZ,MAAM,gBAAgB;AAAA,EACrB;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA;AACD;AAEA,MAAM,kBAAkB,CAAC,QAAoC,MAAM,QAAQ,GAAG;AAEvE,MAAM,iBAAiB;AAAA,EACrB,YAA4B,a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MACjB,KAAK;AACG,eAAA,GAAG,IAAI,gBAAgB,KAAK;AAAA,MACpC,KAAK;AACG,eAAA,GAAG,IAAI,iBAAiB,KAAK;AAAA,MACrC,KAAK;AACG,eAAA,GAAG,IAAI,SAAS,KAAK;AAAA,MAC7B,KAAK;AACG,eAAA,GAAG,IAAI,UAAU,KAAK;AAAA,MAC9B,KAAK;AACG,eAAA,GAAG,IAAI,gBAAgB,KAAK;AAAA,MACpC,KAAK;AACG,eAAA,GAAG,IAAI,iBAAiB,KAAK;AAAA,IAAA;AAEtC,WAAO,GAAG,IAAI,IAAI,QAAQ,IAAI,KAAK;AAAA,EAAA;AAAA,EAGpC,OAAe,cAAc,aAAwB,WAAyB,OAAO,MAAe,OAAuB;AACtH,QAAA,YAAY,WAAW,GAAG;AACtB,aAAA;AAAA,IAAA;AAEF,UAAA,MAAM,YAAY,IAAI,CAAM,OAAA,GAAG,GAAG,EAAE,KAAK,IAAI,QAAQ,GAAG;AAExD,UAAA,WAAqB,GAAe,OAAO,GAAG,YAAY,IAAI,CAAA,OAAM,GAAG,UAAU,CAAC;AAExF,WAAO,IAAIA,QAAA,QAAQ,MAAM,OAAO,GAAG,MAAM,aAAa,OAAO,IAAI,GAAG,MAAM,KAAK,QAAQ;AAAA,EAAA;AAEzF;;;"}
|
|
1
|
+
{"version":3,"file":"ConditionBuilder.cjs","sources":["../../../../../packages/database/src/builders/ConditionBuilder.ts"],"sourcesContent":["import { Value } from '../types'\nimport { Literal } from '../Literal'\nimport { QueryBuilder } from './QueryBuilder'\nimport { SelectBuilder } from './SelectBuilder'\nimport { Compiler } from './Compiler'\nimport { createSubQueryLiteralFactory, SubQueryExpression } from './internal/Subqueries'\nimport { formatColumnIdentifier } from '../utils'\n\nexport type ConditionCallback = (builder: ConditionBuilder) => ConditionBuilder\nexport type ConditionExpression = ConditionBuilder | ConditionCallback\n\nexport enum Operator {\n\t'notEq' = '!=',\n\t'eq' = '=',\n\t'gt' = '>',\n\t'gte' = '>=',\n\t'lt' = '<',\n\t'lte' = '<=',\n\t'contains' = 'contains',\n\t'startsWith' = 'startsWith',\n\t'endsWith' = 'endsWith',\n\t'containsCI' = 'containsCI',\n\t'startsWithCI' = 'startsWithCI',\n\t'endsWithCI' = 'endsWithCI',\n}\n\nconst likeOperators: string[] = [\n\tOperator.contains,\n\tOperator.containsCI,\n\tOperator.startsWith,\n\tOperator.startsWithCI,\n\tOperator.endsWith,\n\tOperator.endsWithCI,\n]\n\nconst isReadonlyArray = (arr: any): arr is readonly any[] => Array.isArray(arr)\n\nexport class ConditionBuilder {\n\tprivate constructor(public readonly expressions: Literal[]) {}\n\n\tpublic static create(expressions: Literal[] = []) {\n\t\treturn new ConditionBuilder(expressions)\n\t}\n\n\tpublic static process(condition: ConditionExpression): ConditionBuilder {\n\t\treturn typeof condition === 'function' ? condition(ConditionBuilder.create()) : condition\n\t}\n\n\tpublic static not(condition: ConditionExpression): ConditionBuilder {\n\t\treturn ConditionBuilder.create().not(condition)\n\t}\n\n\tand(condition: ConditionExpression): ConditionBuilder {\n\t\treturn this.processConditionExpression(ConditionBuilder.process(condition), ['and', false])\n\t}\n\n\tor(condition: ConditionExpression): ConditionBuilder {\n\t\treturn this.processConditionExpression(ConditionBuilder.process(condition), ['or', false])\n\t}\n\n\tnot(condition: ConditionExpression): ConditionBuilder {\n\t\treturn this.processConditionExpression(ConditionBuilder.process(condition), ['and', true])\n\t}\n\n\tcompare(columnName: QueryBuilder.ColumnIdentifier, operator: Operator | `${Operator}`, value: Value | Literal): ConditionBuilder {\n\t\tif (!(Object.values(Operator) as string[]).includes(operator)) {\n\t\t\tthrow new Error(`Operator ${operator} is not supported`)\n\t\t}\n\n\t\tif (likeOperators.includes(operator)) {\n\t\t\tif (typeof value !== 'string') {\n\t\t\t\tthrow new Error(`Operator ${operator} supports only a string value`)\n\t\t\t}\n\t\t\tvalue = value.replace(/([\\\\%_])/g, v => '\\\\' + v)\n\t\t}\n\t\treturn this.with(new Literal(ConditionBuilder.createOperatorSql(formatColumnIdentifier(columnName), '?', operator), [value]))\n\t}\n\n\tcolumnsEq(columnName1: QueryBuilder.ColumnIdentifier, columnName2: QueryBuilder.ColumnIdentifier): ConditionBuilder {\n\t\treturn this.compareColumns(columnName1, Operator.eq, columnName2)\n\t}\n\n\tcompareColumns(columnName1: QueryBuilder.ColumnIdentifier, operator: Operator | `${Operator}`, columnName2: QueryBuilder.ColumnIdentifier) {\n\t\treturn this.with(new Literal(ConditionBuilder.createOperatorSql(formatColumnIdentifier(columnName1), formatColumnIdentifier(columnName2), operator)))\n\t}\n\n\tin(columnName: QueryBuilder.ColumnIdentifier, values: ReadonlyArray<Value> | SelectBuilder<SelectBuilder.Result>, columnType?: string): ConditionBuilder {\n\t\tif (!isReadonlyArray(values)) {\n\t\t\t// todo: replace placeholder with some kind of callback\n\t\t\tconst query = values.createQuery(new Compiler.Context(Compiler.SCHEMA_PLACEHOLDER, new Set()))\n\t\t\treturn this.with(new Literal(`${formatColumnIdentifier(columnName)} in (${query.sql})`, query.parameters))\n\t\t}\n\t\tvalues = values.filter(it => it !== undefined)\n\t\tif (columnType && values.length > 100) {\n\t\t\treturn this.with(new Literal(`${formatColumnIdentifier(columnName)} = any(?::${columnType}[])`, [values]))\n\t\t}\n\t\tif (values.length > 0) {\n\t\t\tconst parameters = values.map(() => '?').join(', ')\n\t\t\treturn this.with(new Literal(`${formatColumnIdentifier(columnName)} in (${parameters})`, values))\n\t\t}\n\t\treturn this.raw('false')\n\t}\n\n\texists(subQuery: SubQueryExpression<SelectBuilder>): ConditionBuilder {\n\t\tconst context = new Compiler.Context(Compiler.SCHEMA_PLACEHOLDER, new Set())\n\t\tconst query = createSubQueryLiteralFactory<SelectBuilder>(subQuery)(context, subQuery => {\n\t\t\tif (subQuery.options.select.length === 0) {\n\t\t\t\treturn subQuery.select(expr => expr.raw('?::int', 1))\n\t\t\t}\n\t\t\treturn subQuery\n\t\t})\n\t\treturn this.with(new Literal(`exists (${query.sql})`, query.parameters))\n\t}\n\n\tisNull(columnName: QueryBuilder.ColumnIdentifier): ConditionBuilder {\n\t\treturn this.with(new Literal(`${formatColumnIdentifier(columnName)} is null`))\n\t}\n\n\tisNotNull(columnName: QueryBuilder.ColumnIdentifier): ConditionBuilder {\n\t\treturn this.with(new Literal(`${formatColumnIdentifier(columnName)} is not null`))\n\t}\n\n\traw(sql: string, ...bindings: Value[]): ConditionBuilder {\n\t\treturn this.with(new Literal(sql, bindings))\n\t}\n\n\twith(expression?: Literal | null | undefined): ConditionBuilder {\n\t\tif (!expression) {\n\t\t\treturn this\n\t\t}\n\t\treturn new ConditionBuilder([...this.expressions, expression])\n\t}\n\n\tpublic getSql(): Literal | null {\n\t\treturn ConditionBuilder.createLiteral(this.expressions)\n\t}\n\n\tisEmpty(): boolean {\n\t\treturn this.expressions.length === 0\n\t}\n\n\tprivate processConditionExpression(builder: ConditionBuilder, parameters: ['and' | 'or', boolean]): ConditionBuilder {\n\t\tconst sql = ConditionBuilder.createLiteral(builder.expressions, ...parameters)\n\t\tif (sql) {\n\t\t\treturn new ConditionBuilder([...this.expressions, sql])\n\t\t}\n\t\treturn this\n\t}\n\n\tprivate static createOperatorSql(left: string, right: string, operator: Operator | `${Operator}`): string {\n\t\tif (!(Object.values(Operator) as string[]).includes(operator)) {\n\t\t\tthrow new Error(`Operator ${operator} is not supported`)\n\t\t}\n\t\tswitch (operator) {\n\t\t\tcase Operator.contains:\n\t\t\t\treturn `${left} LIKE '%' || ${right} || '%'`\n\t\t\tcase Operator.containsCI:\n\t\t\t\treturn `${left} ILIKE '%' || ${right} || '%'`\n\t\t\tcase Operator.startsWith:\n\t\t\t\treturn `${left} LIKE ${right} || '%'`\n\t\t\tcase Operator.startsWithCI:\n\t\t\t\treturn `${left} ILIKE ${right} || '%'`\n\t\t\tcase Operator.endsWith:\n\t\t\t\treturn `${left} LIKE '%' || ${right}`\n\t\t\tcase Operator.endsWithCI:\n\t\t\t\treturn `${left} ILIKE '%' || ${right}`\n\t\t}\n\t\treturn `${left} ${operator} ${right}`\n\t}\n\n\tprivate static createLiteral(expressions: Literal[], operator: 'or' | 'and' = 'and', not: boolean = false): Literal | null {\n\t\tif (expressions.length === 0) {\n\t\t\treturn null\n\t\t}\n\t\tconst sql = expressions.map(it => it.sql).join(` ${operator} `)\n\n\t\tconst bindings: Value[] = ([] as Value[]).concat(...expressions.map(it => it.parameters))\n\n\t\treturn new Literal(not ? `not(${sql})` : operator === 'or' ? `(${sql})` : sql, bindings)\n\t}\n}\n"],"names":["Operator","Literal","formatColumnIdentifier","Compiler","createSubQueryLiteralFactory","subQuery","sql"],"mappings":";;;;;;;;;;;;;;;AAWY,IAAA,6BAAAA,cAAL;AACNA,YAAA,OAAU,IAAA;AACVA,YAAA,IAAO,IAAA;AACPA,YAAA,IAAO,IAAA;AACPA,YAAA,KAAQ,IAAA;AACRA,YAAA,IAAO,IAAA;AACPA,YAAA,KAAQ,IAAA;AACRA,YAAA,UAAa,IAAA;AACbA,YAAA,YAAe,IAAA;AACfA,YAAA,UAAa,IAAA;AACbA,YAAA,YAAe,IAAA;AACfA,YAAA,cAAiB,IAAA;AACjBA,YAAA,YAAe,IAAA;AAZJA,SAAAA;AAAA,GAAA,YAAA,CAAA,CAAA;AAeZ,MAAM,gBAA0B;AAAA,EAC/B;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA;AACD;AAEA,MAAM,kBAAkB,CAAC,QAAoC,MAAM,QAAQ,GAAG;AAEvE,MAAM,iBAAiB;AAAA,EACrB,YAA4B,aAAwB;AAAxB,SAAA,cAAA;AAAA,EAAA;AAAA,EAEpC,OAAc,OAAO,cAAyB,IAAI;AAC1C,WAAA,IAAI,iBAAiB,WAAW;AAAA,EAAA;AAAA,EAGxC,OAAc,QAAQ,WAAkD;AACvE,WAAO,OAAO,cAAc,aAAa,UAAU,iBAAiB,OAAQ,CAAA,IAAI;AAAA,EAAA;AAAA,EAGjF,OAAc,IAAI,WAAkD;AACnE,WAAO,iBAAiB,SAAS,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KAAK;AAAA,MACpC,KAAK;AACG,eAAA,GAAG,IAAI,iBAAiB,KAAK;AAAA,IAAA;AAEtC,WAAO,GAAG,IAAI,IAAI,QAAQ,IAAI,KAAK;AAAA,EAAA;AAAA,EAGpC,OAAe,cAAc,aAAwB,WAAyB,OAAO,MAAe,OAAuB;AACtH,QAAA,YAAY,WAAW,GAAG;AACtB,aAAA;AAAA,IAAA;AAEF,UAAAA,OAAM,YAAY,IAAI,CAAM,OAAA,GAAG,GAAG,EAAE,KAAK,IAAI,QAAQ,GAAG;AAExD,UAAA,WAAqB,GAAe,OAAO,GAAG,YAAY,IAAI,CAAA,OAAM,GAAG,UAAU,CAAC;AAExF,WAAO,IAAIL,QAAA,QAAQ,MAAM,OAAOK,IAAG,MAAM,aAAa,OAAO,IAAIA,IAAG,MAAMA,MAAK,QAAQ;AAAA,EAAA;AAEzF;;;"}
|
|
@@ -1,7 +1,16 @@
|
|
|
1
1
|
import { Literal } from "../Literal.js";
|
|
2
|
-
import { toFqnWrap } from "./formatUtils.js";
|
|
3
2
|
import { Compiler } from "./Compiler.js";
|
|
4
3
|
import { createSubQueryLiteralFactory } from "./internal/Subqueries.js";
|
|
4
|
+
import { formatColumnIdentifier } from "../utils/sql.js";
|
|
5
|
+
import "./internal/With.js";
|
|
6
|
+
import "./internal/Where.js";
|
|
7
|
+
import "./SelectBuilder.js";
|
|
8
|
+
import "../client/EventManager.js";
|
|
9
|
+
import "@contember/queryable";
|
|
10
|
+
import "../client/Connection.js";
|
|
11
|
+
import "node:timers";
|
|
12
|
+
import "../utils/Mutex.js";
|
|
13
|
+
import "pg";
|
|
5
14
|
var Operator = /* @__PURE__ */ ((Operator2) => {
|
|
6
15
|
Operator2["notEq"] = "!=";
|
|
7
16
|
Operator2["eq"] = "=";
|
|
@@ -59,26 +68,26 @@ class ConditionBuilder {
|
|
|
59
68
|
}
|
|
60
69
|
value = value.replace(/([\\%_])/g, (v) => "\\" + v);
|
|
61
70
|
}
|
|
62
|
-
return this.with(new Literal(ConditionBuilder.createOperatorSql(
|
|
71
|
+
return this.with(new Literal(ConditionBuilder.createOperatorSql(formatColumnIdentifier(columnName), "?", operator), [value]));
|
|
63
72
|
}
|
|
64
73
|
columnsEq(columnName1, columnName2) {
|
|
65
74
|
return this.compareColumns(columnName1, "=", columnName2);
|
|
66
75
|
}
|
|
67
76
|
compareColumns(columnName1, operator, columnName2) {
|
|
68
|
-
return this.with(new Literal(ConditionBuilder.createOperatorSql(
|
|
77
|
+
return this.with(new Literal(ConditionBuilder.createOperatorSql(formatColumnIdentifier(columnName1), formatColumnIdentifier(columnName2), operator)));
|
|
69
78
|
}
|
|
70
79
|
in(columnName, values, columnType) {
|
|
71
80
|
if (!isReadonlyArray(values)) {
|
|
72
81
|
const query = values.createQuery(new Compiler.Context(Compiler.SCHEMA_PLACEHOLDER, /* @__PURE__ */ new Set()));
|
|
73
|
-
return this.with(new Literal(`${
|
|
82
|
+
return this.with(new Literal(`${formatColumnIdentifier(columnName)} in (${query.sql})`, query.parameters));
|
|
74
83
|
}
|
|
75
84
|
values = values.filter((it) => it !== void 0);
|
|
76
85
|
if (columnType && values.length > 100) {
|
|
77
|
-
return this.with(new Literal(`${
|
|
86
|
+
return this.with(new Literal(`${formatColumnIdentifier(columnName)} = any(?::${columnType}[])`, [values]));
|
|
78
87
|
}
|
|
79
88
|
if (values.length > 0) {
|
|
80
89
|
const parameters = values.map(() => "?").join(", ");
|
|
81
|
-
return this.with(new Literal(`${
|
|
90
|
+
return this.with(new Literal(`${formatColumnIdentifier(columnName)} in (${parameters})`, values));
|
|
82
91
|
}
|
|
83
92
|
return this.raw("false");
|
|
84
93
|
}
|
|
@@ -93,10 +102,10 @@ class ConditionBuilder {
|
|
|
93
102
|
return this.with(new Literal(`exists (${query.sql})`, query.parameters));
|
|
94
103
|
}
|
|
95
104
|
isNull(columnName) {
|
|
96
|
-
return this.with(new Literal(`${
|
|
105
|
+
return this.with(new Literal(`${formatColumnIdentifier(columnName)} is null`));
|
|
97
106
|
}
|
|
98
107
|
isNotNull(columnName) {
|
|
99
|
-
return this.with(new Literal(`${
|
|
108
|
+
return this.with(new Literal(`${formatColumnIdentifier(columnName)} is not null`));
|
|
100
109
|
}
|
|
101
110
|
raw(sql, ...bindings) {
|
|
102
111
|
return this.with(new Literal(sql, bindings));
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"ConditionBuilder.js","sources":["../../../../../packages/database/src/builders/ConditionBuilder.ts"],"sourcesContent":["import { Value } from '../types'\nimport { Literal } from '../Literal'\nimport { QueryBuilder } from './QueryBuilder'\nimport { SelectBuilder } from './SelectBuilder'\nimport { toFqnWrap } from './formatUtils'\nimport { Compiler } from './Compiler'\nimport { createSubQueryLiteralFactory, SubQueryExpression } from './internal/Subqueries'\n\nexport type ConditionCallback = (builder: ConditionBuilder) => ConditionBuilder\nexport type ConditionExpression = ConditionBuilder | ConditionCallback\n\nexport enum Operator {\n\t'notEq' = '!=',\n\t'eq' = '=',\n\t'gt' = '>',\n\t'gte' = '>=',\n\t'lt' = '<',\n\t'lte' = '<=',\n\t'contains' = 'contains',\n\t'startsWith' = 'startsWith',\n\t'endsWith' = 'endsWith',\n\n\t'containsCI' = 'containsCI',\n\t'startsWithCI' = 'startsWithCI',\n\t'endsWithCI' = 'endsWithCI',\n}\n\nconst likeOperators = [\n\tOperator.contains,\n\tOperator.containsCI,\n\tOperator.startsWith,\n\tOperator.startsWithCI,\n\tOperator.endsWith,\n\tOperator.endsWithCI,\n]\n\nconst isReadonlyArray = (arr: any): arr is readonly any[] => Array.isArray(arr)\n\nexport class ConditionBuilder {\n\tprivate constructor(public readonly expressions: Literal[]) {}\n\n\tpublic static create(expressions: Literal[] = []) {\n\t\treturn new ConditionBuilder(expressions)\n\t}\n\n\tpublic static process(condition: ConditionExpression): ConditionBuilder {\n\t\treturn typeof condition === 'function' ? condition(ConditionBuilder.create()) : condition\n\t}\n\n\tpublic static not(condition: ConditionExpression): ConditionBuilder {\n\t\treturn ConditionBuilder.create().not(condition)\n\t}\n\n\tand(condition: ConditionExpression): ConditionBuilder {\n\t\treturn this.processConditionExpression(ConditionBuilder.process(condition), ['and', false])\n\t}\n\n\tor(condition: ConditionExpression): ConditionBuilder {\n\t\treturn this.processConditionExpression(ConditionBuilder.process(condition), ['or', false])\n\t}\n\n\tnot(condition: ConditionExpression): ConditionBuilder {\n\t\treturn this.processConditionExpression(ConditionBuilder.process(condition), ['and', true])\n\t}\n\n\tcompare(columnName: QueryBuilder.ColumnIdentifier, operator: Operator, value: Value): ConditionBuilder {\n\t\tif (!Object.values(Operator).includes(operator)) {\n\t\t\tthrow new Error(`Operator ${operator} is not supported`)\n\t\t}\n\n\t\tif (likeOperators.includes(operator)) {\n\t\t\tif (typeof value !== 'string') {\n\t\t\t\tthrow new Error(`Operator ${operator} supports only a string value`)\n\t\t\t}\n\t\t\tvalue = value.replace(/([\\\\%_])/g, v => '\\\\' + v)\n\t\t}\n\t\treturn this.with(new Literal(ConditionBuilder.createOperatorSql(toFqnWrap(columnName), '?', operator), [value]))\n\t}\n\n\tcolumnsEq(columnName1: QueryBuilder.ColumnIdentifier, columnName2: QueryBuilder.ColumnIdentifier): ConditionBuilder {\n\t\treturn this.compareColumns(columnName1, Operator.eq, columnName2)\n\t}\n\n\tcompareColumns(columnName1: QueryBuilder.ColumnIdentifier, operator: Operator, columnName2: QueryBuilder.ColumnIdentifier) {\n\t\treturn this.with(new Literal(ConditionBuilder.createOperatorSql(toFqnWrap(columnName1), toFqnWrap(columnName2), operator)))\n\t}\n\n\tin(columnName: QueryBuilder.ColumnIdentifier, values: ReadonlyArray<Value> | SelectBuilder<SelectBuilder.Result>, columnType?: string): ConditionBuilder {\n\t\tif (!isReadonlyArray(values)) {\n\t\t\t// todo: replace placeholder with some kind of callback\n\t\t\tconst query = values.createQuery(new Compiler.Context(Compiler.SCHEMA_PLACEHOLDER, new Set()))\n\t\t\treturn this.with(new Literal(`${toFqnWrap(columnName)} in (${query.sql})`, query.parameters))\n\t\t}\n\t\tvalues = values.filter(it => it !== undefined)\n\t\tif (columnType && values.length > 100) {\n\t\t\treturn this.with(new Literal(`${toFqnWrap(columnName)} = any(?::${columnType}[])`, [values]))\n\t\t}\n\t\tif (values.length > 0) {\n\t\t\tconst parameters = values.map(() => '?').join(', ')\n\t\t\treturn this.with(new Literal(`${toFqnWrap(columnName)} in (${parameters})`, values))\n\t\t}\n\t\treturn this.raw('false')\n\t}\n\n\texists(subQuery: SubQueryExpression<SelectBuilder>): ConditionBuilder {\n\t\tconst context = new Compiler.Context(Compiler.SCHEMA_PLACEHOLDER, new Set())\n\t\tconst query = createSubQueryLiteralFactory<SelectBuilder>(subQuery)(context, subQuery => {\n\t\t\tif (subQuery.options.select.length === 0) {\n\t\t\t\treturn subQuery.select(expr => expr.raw('?::int', 1))\n\t\t\t}\n\t\t\treturn subQuery\n\t\t})\n\t\treturn this.with(new Literal(`exists (${query.sql})`, query.parameters))\n\t}\n\n\tisNull(columnName: QueryBuilder.ColumnIdentifier): ConditionBuilder {\n\t\treturn this.with(new Literal(`${toFqnWrap(columnName)} is null`))\n\t}\n\n\tisNotNull(columnName: QueryBuilder.ColumnIdentifier): ConditionBuilder {\n\t\treturn this.with(new Literal(`${toFqnWrap(columnName)} is not null`))\n\t}\n\n\traw(sql: string, ...bindings: Value[]): ConditionBuilder {\n\t\treturn this.with(new Literal(sql, bindings))\n\t}\n\n\twith(expression?: Literal | null | undefined): ConditionBuilder {\n\t\tif (!expression) {\n\t\t\treturn this\n\t\t}\n\t\treturn new ConditionBuilder([...this.expressions, expression])\n\t}\n\n\tpublic getSql(): Literal | null {\n\t\treturn ConditionBuilder.createLiteral(this.expressions)\n\t}\n\n\tisEmpty(): boolean {\n\t\treturn this.expressions.length === 0\n\t}\n\n\tprivate processConditionExpression(builder: ConditionBuilder, parameters: ['and' | 'or', boolean]): ConditionBuilder {\n\t\tconst sql = ConditionBuilder.createLiteral(builder.expressions, ...parameters)\n\t\tif (sql) {\n\t\t\treturn new ConditionBuilder([...this.expressions, sql])\n\t\t}\n\t\treturn this\n\t}\n\n\tprivate static createOperatorSql(left: string, right: string, operator: Operator): string {\n\t\tif (!Object.values(Operator).includes(operator)) {\n\t\t\tthrow new Error(`Operator ${operator} is not supported`)\n\t\t}\n\t\tswitch (operator) {\n\t\t\tcase Operator.contains:\n\t\t\t\treturn `${left} LIKE '%' || ${right} || '%'`\n\t\t\tcase Operator.containsCI:\n\t\t\t\treturn `${left} ILIKE '%' || ${right} || '%'`\n\t\t\tcase Operator.startsWith:\n\t\t\t\treturn `${left} LIKE ${right} || '%'`\n\t\t\tcase Operator.startsWithCI:\n\t\t\t\treturn `${left} ILIKE ${right} || '%'`\n\t\t\tcase Operator.endsWith:\n\t\t\t\treturn `${left} LIKE '%' || ${right}`\n\t\t\tcase Operator.endsWithCI:\n\t\t\t\treturn `${left} ILIKE '%' || ${right}`\n\t\t}\n\t\treturn `${left} ${operator} ${right}`\n\t}\n\n\tprivate static createLiteral(expressions: Literal[], operator: 'or' | 'and' = 'and', not: boolean = false): Literal | null {\n\t\tif (expressions.length === 0) {\n\t\t\treturn null\n\t\t}\n\t\tconst sql = expressions.map(it => it.sql).join(` ${operator} `)\n\n\t\tconst bindings: Value[] = ([] as Value[]).concat(...expressions.map(it => it.parameters))\n\n\t\treturn new Literal(not ? `not(${sql})` : operator === 'or' ? `(${sql})` : sql, bindings)\n\t}\n}\n"],"names":["Operator","subQuery"],"mappings":";;;;AAWY,IAAA,6BAAAA,cAAL;AACNA,YAAA,OAAU,IAAA;AACVA,YAAA,IAAO,IAAA;AACPA,YAAA,IAAO,IAAA;AACPA,YAAA,KAAQ,IAAA;AACRA,YAAA,IAAO,IAAA;AACPA,YAAA,KAAQ,IAAA;AACRA,YAAA,UAAa,IAAA;AACbA,YAAA,YAAe,IAAA;AACfA,YAAA,UAAa,IAAA;AAEbA,YAAA,YAAe,IAAA;AACfA,YAAA,cAAiB,IAAA;AACjBA,YAAA,YAAe,IAAA;AAbJA,SAAAA;AAAA,GAAA,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AAI,MAAM,YAAY,QAAQ,mBAAmB;AAAA,IAAA;AAExD,YAAQ,UAAU;AAAA,MACjB,KAAK;AACG,eAAA,GAAG,IAAI,gBAAgB,KAAK;AAAA,MACpC,KAAK;AACG,eAAA,GAAG,IAAI,iBAAiB,KAAK;AAAA,MACrC,KAAK;AACG,eAAA,GAAG,IAAI,SAAS,KAAK;AAAA,MAC7B,KAAK;AACG,eAAA,GAAG,IAAI,UAAU,KAAK;AAAA,MAC9B,KAAK;AACG,eAAA,GAAG,IAAI,gBAAgB,KAAK;AAAA,MACpC,KAAK;AACG,eAAA,GAAG,IAAI,iBAAiB,KAAK;AAAA,IAAA;AAEtC,WAAO,GAAG,IAAI,IAAI,QAAQ,IAAI,KAAK;AAAA,EAAA;AAAA,EAGpC,OAAe,cAAc,aAAwB,WAAyB,OAAO,MAAe,OAAuB;AACtH,QAAA,YAAY,WAAW,GAAG;AACtB,aAAA;AAAA,IAAA;AAEF,UAAA,MAAM,YAAY,IAAI,CAAM,OAAA,GAAG,GAAG,EAAE,KAAK,IAAI,QAAQ,GAAG;AAExD,UAAA,WAAqB,GAAe,OAAO,GAAG,YAAY,IAAI,CAAA,OAAM,GAAG,UAAU,CAAC;AAExF,WAAO,IAAI,QAAQ,MAAM,OAAO,GAAG,MAAM,aAAa,OAAO,IAAI,GAAG,MAAM,KAAK,QAAQ;AAAA,EAAA;AAEzF;"}
|
|
1
|
+
{"version":3,"file":"ConditionBuilder.js","sources":["../../../../../packages/database/src/builders/ConditionBuilder.ts"],"sourcesContent":["import { Value } from '../types'\nimport { Literal } from '../Literal'\nimport { QueryBuilder } from './QueryBuilder'\nimport { SelectBuilder } from './SelectBuilder'\nimport { Compiler } from './Compiler'\nimport { createSubQueryLiteralFactory, SubQueryExpression } from './internal/Subqueries'\nimport { formatColumnIdentifier } from '../utils'\n\nexport type ConditionCallback = (builder: ConditionBuilder) => ConditionBuilder\nexport type ConditionExpression = ConditionBuilder | ConditionCallback\n\nexport enum Operator {\n\t'notEq' = '!=',\n\t'eq' = '=',\n\t'gt' = '>',\n\t'gte' = '>=',\n\t'lt' = '<',\n\t'lte' = '<=',\n\t'contains' = 'contains',\n\t'startsWith' = 'startsWith',\n\t'endsWith' = 'endsWith',\n\t'containsCI' = 'containsCI',\n\t'startsWithCI' = 'startsWithCI',\n\t'endsWithCI' = 'endsWithCI',\n}\n\nconst likeOperators: string[] = [\n\tOperator.contains,\n\tOperator.containsCI,\n\tOperator.startsWith,\n\tOperator.startsWithCI,\n\tOperator.endsWith,\n\tOperator.endsWithCI,\n]\n\nconst isReadonlyArray = (arr: any): arr is readonly any[] => Array.isArray(arr)\n\nexport class ConditionBuilder {\n\tprivate constructor(public readonly expressions: Literal[]) {}\n\n\tpublic static create(expressions: Literal[] = []) {\n\t\treturn new ConditionBuilder(expressions)\n\t}\n\n\tpublic static process(condition: ConditionExpression): ConditionBuilder {\n\t\treturn typeof condition === 'function' ? condition(ConditionBuilder.create()) : condition\n\t}\n\n\tpublic static not(condition: ConditionExpression): ConditionBuilder {\n\t\treturn ConditionBuilder.create().not(condition)\n\t}\n\n\tand(condition: ConditionExpression): ConditionBuilder {\n\t\treturn this.processConditionExpression(ConditionBuilder.process(condition), ['and', false])\n\t}\n\n\tor(condition: ConditionExpression): ConditionBuilder {\n\t\treturn this.processConditionExpression(ConditionBuilder.process(condition), ['or', false])\n\t}\n\n\tnot(condition: ConditionExpression): ConditionBuilder {\n\t\treturn this.processConditionExpression(ConditionBuilder.process(condition), ['and', true])\n\t}\n\n\tcompare(columnName: QueryBuilder.ColumnIdentifier, operator: Operator | `${Operator}`, value: Value | Literal): ConditionBuilder {\n\t\tif (!(Object.values(Operator) as string[]).includes(operator)) {\n\t\t\tthrow new Error(`Operator ${operator} is not supported`)\n\t\t}\n\n\t\tif (likeOperators.includes(operator)) {\n\t\t\tif (typeof value !== 'string') {\n\t\t\t\tthrow new Error(`Operator ${operator} supports only a string value`)\n\t\t\t}\n\t\t\tvalue = value.replace(/([\\\\%_])/g, v => '\\\\' + v)\n\t\t}\n\t\treturn this.with(new Literal(ConditionBuilder.createOperatorSql(formatColumnIdentifier(columnName), '?', operator), [value]))\n\t}\n\n\tcolumnsEq(columnName1: QueryBuilder.ColumnIdentifier, columnName2: QueryBuilder.ColumnIdentifier): ConditionBuilder {\n\t\treturn this.compareColumns(columnName1, Operator.eq, columnName2)\n\t}\n\n\tcompareColumns(columnName1: QueryBuilder.ColumnIdentifier, operator: Operator | `${Operator}`, columnName2: QueryBuilder.ColumnIdentifier) {\n\t\treturn this.with(new Literal(ConditionBuilder.createOperatorSql(formatColumnIdentifier(columnName1), formatColumnIdentifier(columnName2), operator)))\n\t}\n\n\tin(columnName: QueryBuilder.ColumnIdentifier, values: ReadonlyArray<Value> | SelectBuilder<SelectBuilder.Result>, columnType?: string): ConditionBuilder {\n\t\tif (!isReadonlyArray(values)) {\n\t\t\t// todo: replace placeholder with some kind of callback\n\t\t\tconst query = values.createQuery(new Compiler.Context(Compiler.SCHEMA_PLACEHOLDER, new Set()))\n\t\t\treturn this.with(new Literal(`${formatColumnIdentifier(columnName)} in (${query.sql})`, query.parameters))\n\t\t}\n\t\tvalues = values.filter(it => it !== undefined)\n\t\tif (columnType && values.length > 100) {\n\t\t\treturn this.with(new Literal(`${formatColumnIdentifier(columnName)} = any(?::${columnType}[])`, [values]))\n\t\t}\n\t\tif (values.length > 0) {\n\t\t\tconst parameters = values.map(() => '?').join(', ')\n\t\t\treturn this.with(new Literal(`${formatColumnIdentifier(columnName)} in (${parameters})`, values))\n\t\t}\n\t\treturn this.raw('false')\n\t}\n\n\texists(subQuery: SubQueryExpression<SelectBuilder>): ConditionBuilder {\n\t\tconst context = new Compiler.Context(Compiler.SCHEMA_PLACEHOLDER, new Set())\n\t\tconst query = createSubQueryLiteralFactory<SelectBuilder>(subQuery)(context, subQuery => {\n\t\t\tif (subQuery.options.select.length === 0) {\n\t\t\t\treturn subQuery.select(expr => expr.raw('?::int', 1))\n\t\t\t}\n\t\t\treturn subQuery\n\t\t})\n\t\treturn this.with(new Literal(`exists (${query.sql})`, query.parameters))\n\t}\n\n\tisNull(columnName: QueryBuilder.ColumnIdentifier): ConditionBuilder {\n\t\treturn this.with(new Literal(`${formatColumnIdentifier(columnName)} is null`))\n\t}\n\n\tisNotNull(columnName: QueryBuilder.ColumnIdentifier): ConditionBuilder {\n\t\treturn this.with(new Literal(`${formatColumnIdentifier(columnName)} is not null`))\n\t}\n\n\traw(sql: string, ...bindings: Value[]): ConditionBuilder {\n\t\treturn this.with(new Literal(sql, bindings))\n\t}\n\n\twith(expression?: Literal | null | undefined): ConditionBuilder {\n\t\tif (!expression) {\n\t\t\treturn this\n\t\t}\n\t\treturn new ConditionBuilder([...this.expressions, expression])\n\t}\n\n\tpublic getSql(): Literal | null {\n\t\treturn ConditionBuilder.createLiteral(this.expressions)\n\t}\n\n\tisEmpty(): boolean {\n\t\treturn this.expressions.length === 0\n\t}\n\n\tprivate processConditionExpression(builder: ConditionBuilder, parameters: ['and' | 'or', boolean]): ConditionBuilder {\n\t\tconst sql = ConditionBuilder.createLiteral(builder.expressions, ...parameters)\n\t\tif (sql) {\n\t\t\treturn new ConditionBuilder([...this.expressions, sql])\n\t\t}\n\t\treturn this\n\t}\n\n\tprivate static createOperatorSql(left: string, right: string, operator: Operator | `${Operator}`): string {\n\t\tif (!(Object.values(Operator) as string[]).includes(operator)) {\n\t\t\tthrow new Error(`Operator ${operator} is not supported`)\n\t\t}\n\t\tswitch (operator) {\n\t\t\tcase Operator.contains:\n\t\t\t\treturn `${left} LIKE '%' || ${right} || '%'`\n\t\t\tcase Operator.containsCI:\n\t\t\t\treturn `${left} ILIKE '%' || ${right} || '%'`\n\t\t\tcase Operator.startsWith:\n\t\t\t\treturn `${left} LIKE ${right} || '%'`\n\t\t\tcase Operator.startsWithCI:\n\t\t\t\treturn `${left} ILIKE ${right} || '%'`\n\t\t\tcase Operator.endsWith:\n\t\t\t\treturn `${left} LIKE '%' || ${right}`\n\t\t\tcase Operator.endsWithCI:\n\t\t\t\treturn `${left} ILIKE '%' || ${right}`\n\t\t}\n\t\treturn `${left} ${operator} ${right}`\n\t}\n\n\tprivate static createLiteral(expressions: Literal[], operator: 'or' | 'and' = 'and', not: boolean = false): Literal | null {\n\t\tif (expressions.length === 0) {\n\t\t\treturn null\n\t\t}\n\t\tconst sql = expressions.map(it => it.sql).join(` ${operator} `)\n\n\t\tconst bindings: Value[] = ([] as Value[]).concat(...expressions.map(it => it.parameters))\n\n\t\treturn new Literal(not ? `not(${sql})` : operator === 'or' ? `(${sql})` : sql, bindings)\n\t}\n}\n"],"names":["Operator","subQuery"],"mappings":";;;;;;;;;;;;;AAWY,IAAA,6BAAAA,cAAL;AACNA,YAAA,OAAU,IAAA;AACVA,YAAA,IAAO,IAAA;AACPA,YAAA,IAAO,IAAA;AACPA,YAAA,KAAQ,IAAA;AACRA,YAAA,IAAO,IAAA;AACPA,YAAA,KAAQ,IAAA;AACRA,YAAA,UAAa,IAAA;AACbA,YAAA,YAAe,IAAA;AACfA,YAAA,UAAa,IAAA;AACbA,YAAA,YAAe,IAAA;AACfA,YAAA,cAAiB,IAAA;AACjBA,YAAA,YAAe,IAAA;AAZJA,SAAAA;AAAA,GAAA,YAAA,CAAA,CAAA;AAeZ,MAAM,gBAA0B;AAAA,EAC/B;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA;AACD;AAEA,MAAM,kBAAkB,CAAC,QAAoC,MAAM,QAAQ,GAAG;AAEvE,MAAM,iBAAiB;AAAA,EACrB,YAA4B,aAAwB;AAAxB,SAAA,cAAA;AAAA,EAAA;AAAA,EAEpC,OAAc,OAAO,cAAyB,IAAI;AAC1C,WAAA,IAAI,iBAAiB,WAAW;AAAA,EAAA;AAAA,EAGxC,OAAc,QAAQ,WAAkD;AACvE,WAAO,OAAO,cAAc,aAAa,UAAU,iBAAiB,OAAQ,CAAA,IAAI;AAAA,EAAA;AAAA,EAGjF,OAAc,IAAI,WAAkD;AACnE,WAAO,iBA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AAA,EAGpC,OAAe,cAAc,aAAwB,WAAyB,OAAO,MAAe,OAAuB;AACtH,QAAA,YAAY,WAAW,GAAG;AACtB,aAAA;AAAA,IAAA;AAEF,UAAA,MAAM,YAAY,IAAI,CAAM,OAAA,GAAG,GAAG,EAAE,KAAK,IAAI,QAAQ,GAAG;AAExD,UAAA,WAAqB,GAAe,OAAO,GAAG,YAAY,IAAI,CAAA,OAAM,GAAG,UAAU,CAAC;AAExF,WAAO,IAAI,QAAQ,MAAM,OAAO,GAAG,MAAM,aAAa,OAAO,IAAI,GAAG,MAAM,KAAK,QAAQ;AAAA,EAAA;AAEzF;"}
|
|
@@ -14,7 +14,6 @@ require("../client/Connection.cjs");
|
|
|
14
14
|
require("node:timers");
|
|
15
15
|
require("../utils/Mutex.cjs");
|
|
16
16
|
require("pg");
|
|
17
|
-
const formatUtils = require("./formatUtils.cjs");
|
|
18
17
|
class SelectBuilder {
|
|
19
18
|
constructor(options) {
|
|
20
19
|
this.options = options;
|
|
@@ -74,7 +73,7 @@ class SelectBuilder {
|
|
|
74
73
|
return this.withOption("where", this.options.where.withWhere(where));
|
|
75
74
|
}
|
|
76
75
|
orderBy(expression, direction = "asc") {
|
|
77
|
-
const literal = expression instanceof Literal.Literal ? expression : new Literal.Literal(
|
|
76
|
+
const literal = expression instanceof Literal.Literal ? expression : new Literal.Literal(sql.formatColumnIdentifier(expression));
|
|
78
77
|
return this.withOption("orderBy", [...this.options.orderBy, [literal, direction]]);
|
|
79
78
|
}
|
|
80
79
|
join(table, alias, condition) {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"SelectBuilder.cjs","sources":["../../../../../packages/database/src/builders/SelectBuilder.ts"],"sourcesContent":["import { With } from './internal/With'\nimport { Where } from './internal/Where'\nimport { aliasLiteral } from '../utils'\nimport { Compiler } from './Compiler'\nimport { QueryBuilder } from './QueryBuilder'\nimport { Client, Connection } from '../client'\nimport { Literal } from '../Literal'\nimport { ConditionBuilder, ConditionCallback, ConditionExpression } from './ConditionBuilder'\nimport { LockModifier, LockType } from './LockType'\nimport { columnExpressionToLiteral, toFqnWrap } from './utils'\nimport { createSubQueryLiteralFactory, SubQueryExpression, SubQueryLiteralFactory } from './internal/Subqueries'\n\nexport type SelectBuilderSpecification = <Result>(qb: SelectBuilder<Result>) => SelectBuilder<Result>\n\nclass SelectBuilder<Result = SelectBuilder.Result> implements With.Aware, Where.Aware, QueryBuilder.Orderable<SelectBuilder<Result>>, QueryBuilder {\n\tconstructor(public readonly options: SelectBuilder.Options) {}\n\n\tpublic static create<Result = SelectBuilder.Result>() {\n\t\treturn new SelectBuilder<Result>({\n\t\t\tfrom: undefined,\n\t\t\torderBy: [],\n\t\t\tjoin: [],\n\t\t\tlimit: [undefined, undefined],\n\t\t\tselect: [],\n\t\t\twith: new With.Statement({}),\n\t\t\twhere: new Where.Statement([]),\n\t\t\tgrouping: {\n\t\t\t\tgroupingElement: [],\n\t\t\t},\n\t\t\tlock: undefined,\n\t\t\tmeta: {},\n\t\t})\n\t}\n\n\tpublic with(alias: string, expression: SubQueryExpression): SelectBuilder<Result> {\n\t\treturn this.withOption('with', this.options.with.withCte(alias, createSubQueryLiteralFactory(expression), false))\n\t}\n\n\tpublic withRecursive(alias: string, expression: SubQueryExpression, columns?: string[]): SelectBuilder<Result> {\n\t\treturn this.withOption(\n\t\t\t'with',\n\t\t\tthis.options.with.withCte(alias, createSubQueryLiteralFactory(expression), true, columns),\n\t\t)\n\t}\n\n\tpublic unionAll(expression: SubQueryExpression): SelectBuilder<Result> {\n\t\treturn this.withOption('union', {\n\t\t\ttype: 'all',\n\t\t\tliteral: createSubQueryLiteralFactory(expression),\n\t\t})\n\t}\n\n\tpublic unionDistinct(expression: SubQueryExpression): SelectBuilder<Result> {\n\t\treturn this.withOption('union', {\n\t\t\ttype: 'distinct',\n\t\t\tliteral: createSubQueryLiteralFactory(expression),\n\t\t})\n\t}\n\n\tpublic from(tableName: string | Literal, alias?: string): SelectBuilder<Result> {\n\t\treturn this.withOption('from', [...(this.options.from || []), [tableName, alias]])\n\t}\n\n\tpublic distinct(...on: (QueryBuilder.ColumnIdentifier | QueryBuilder.ColumnExpression)[]): SelectBuilder<Result> {\n\t\tconst onLiterals = on.map(columnExpressionToLiteral).filter((it): it is Literal => it !== undefined)\n\t\treturn this.withOption('distinct', onLiterals)\n\t}\n\n\tpublic select(columnName: QueryBuilder.ColumnIdentifier, alias?: string): SelectBuilder<Result>\n\tpublic select(callback: QueryBuilder.ColumnExpression, alias?: string): SelectBuilder<Result>\n\tpublic select(\n\t\texpr: QueryBuilder.ColumnIdentifier | QueryBuilder.ColumnExpression,\n\t\talias?: string,\n\t): SelectBuilder<Result> {\n\t\tlet raw = columnExpressionToLiteral(expr)\n\t\tif (raw === undefined) {\n\t\t\treturn this\n\t\t}\n\t\treturn this.withOption('select', [...this.options.select, aliasLiteral(raw, alias)])\n\t}\n\n\tpublic where(where: Where.Expression): SelectBuilder<Result> {\n\t\treturn this.withOption('where', this.options.where.withWhere(where))\n\t}\n\n\tpublic orderBy(\n\t\texpression: QueryBuilder.ColumnIdentifier | Literal,\n\t\tdirection: SelectBuilder.OrderByDirection = 'asc',\n\t): SelectBuilder<Result> {\n\t\tconst literal = expression instanceof Literal ? expression : new Literal(toFqnWrap(expression))\n\t\treturn this.withOption('orderBy', [...this.options.orderBy, [literal, direction]])\n\t}\n\n\tpublic join(table: string | Literal, alias?: string, condition?: SelectBuilder.JoinCondition): SelectBuilder<Result> {\n\t\treturn this.withOption('join', [\n\t\t\t...this.options.join,\n\t\t\t{ type: 'inner', table, alias, condition: this.joinConditionToLiteral(condition) },\n\t\t])\n\t}\n\n\tpublic leftJoin(\n\t\ttable: string | Literal,\n\t\talias?: string,\n\t\tcondition?: SelectBuilder.JoinCondition,\n\t): SelectBuilder<Result> {\n\t\treturn this.withOption('join', [\n\t\t\t...this.options.join,\n\t\t\t{ type: 'left', table, alias, condition: this.joinConditionToLiteral(condition) },\n\t\t])\n\t}\n\n\tprivate joinConditionToLiteral(joinCondition?: SelectBuilder.JoinCondition): Literal | undefined {\n\t\tif (joinCondition === undefined) {\n\t\t\treturn undefined\n\t\t}\n\t\tconst builder = ConditionBuilder.process(joinCondition)\n\n\t\treturn builder.getSql() || undefined\n\t}\n\n\tpublic groupBy(columnName: QueryBuilder.ColumnIdentifier): SelectBuilder<Result>\n\tpublic groupBy(callback: QueryBuilder.ColumnExpression): SelectBuilder<Result>\n\tpublic groupBy(expr: QueryBuilder.ColumnIdentifier | QueryBuilder.ColumnExpression): SelectBuilder<Result> {\n\t\tlet raw = columnExpressionToLiteral(expr)\n\t\tif (raw === undefined) {\n\t\t\treturn this\n\t\t}\n\t\treturn this.withOption('grouping', {\n\t\t\t...this.options.grouping,\n\t\t\tgroupingElement: [...this.options.grouping.groupingElement, raw],\n\t\t})\n\t}\n\n\tpublic limit(limit?: number, offset?: number): SelectBuilder<Result> {\n\t\treturn this.withOption('limit', [limit, offset])\n\t}\n\n\tpublic lock(type: LockType, modifier?: LockModifier): SelectBuilder<Result> {\n\t\treturn this.withOption('lock', { type, modifier })\n\t}\n\n\tpublic meta(key: string, value: any): SelectBuilder<Result>\n\tpublic meta(values: Record<string, any>): SelectBuilder<Result>\n\tpublic meta(keyOrValues: string | Record<string, any>, value?: any): SelectBuilder<Result> {\n\t\treturn this.withOption('meta', {\n\t\t\t...this.options.meta,\n\t\t\t...(typeof keyOrValues === 'string' ? { [keyOrValues]: value } : keyOrValues),\n\t\t})\n\t}\n\n\tpublic match(spec: SelectBuilderSpecification): SelectBuilder<Result> {\n\t\treturn spec(this)\n\t}\n\n\tpublic async getResult(db: Client): Promise<Result[]> {\n\t\tconst namespaceContext = new Compiler.Context(db.schema, new Set())\n\t\tconst query = this.createQuery(namespaceContext)\n\t\tconst result: Connection.Result = await db.query(query.sql, query.parameters, query.meta)\n\n\t\treturn result.rows as any as Result[]\n\t}\n\n\tpublic createQuery(context: Compiler.Context): Literal {\n\t\tconst compiler = new Compiler()\n\n\t\tconst compiled = compiler.compileSelect(this.options, context)\n\n\t\treturn new Literal(compiled.sql, compiled.parameters, this.options.meta)\n\t}\n\n\tprotected withOption<K extends keyof SelectBuilder.Options, V extends SelectBuilder.Options[K]>(\n\t\tkey: K,\n\t\tvalue: V,\n\t): SelectBuilder<Result> {\n\t\treturn new SelectBuilder<Result>({ ...this.options, [key]: value })\n\t}\n}\n\nnamespace SelectBuilder {\n\texport type Callback = (qb: SelectBuilder<any>) => SelectBuilder<any>\n\n\texport type Result = Record<string, any>\n\n\texport type OrderByDirection = 'asc' | 'desc' | 'asc nulls last' | 'asc nulls first' | 'desc nulls first' | 'desc nulls last'\n\texport const orderByDirection = new Set<OrderByDirection>(['asc', 'desc', 'asc nulls last', 'asc nulls first', 'desc nulls first', 'desc nulls last'])\n\texport type Options = Readonly<\n\t\tWith.Options &\n\t\tWhere.Options & {\n\t\t\tdistinct?: Literal[]\n\t\t\tselect: Literal[]\n\t\t\tlimit: [number | undefined, number | undefined]\n\t\t\tfrom: undefined | [Literal | string, string | undefined][]\n\t\t\torderBy: [Literal, OrderByDirection][]\n\t\t\tjoin: {\n\t\t\t\ttype: 'inner' | 'left'\n\t\t\t\ttable: string | Literal\n\t\t\t\talias: string | undefined\n\t\t\t\tcondition: Literal | undefined\n\t\t\t}[]\n\t\t\tgrouping: {\n\t\t\t\tgroupingElement: Literal[]\n\t\t\t}\n\t\t\tlock?: { type: LockType; modifier?: LockModifier }\n\t\t\tmeta: Record<string, any>\n\t\t\tunion?: {\n\t\t\t\ttype: 'all' | 'distinct'\n\t\t\t\tliteral: SubQueryLiteralFactory\n\t\t\t}\n\t\t}\n\t>\n\n\texport type JoinCondition = ConditionExpression\n}\n\nexport { SelectBuilder }\n"],"names":["With","Where","createSubQueryLiteralFactory","columnExpressionToLiteral","aliasLiteral","Literal","toFqnWrap","ConditionBuilder","Compiler","SelectBuilder"],"mappings":";;;;;;;;;;;;;;;;;AAcA,MAAM,cAA6I;AAAA,EAClJ,YAA4B,SAAgC;AAAhC,SAAA,U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K,SAAS,CAAC,GAAG,GAAG,OAAO;AAAA,EAAA;AAEpE;AAAA,CAEA,CAAUI,mBAAV;AAMcA,iBAAA,mBAAmB,oBAAI,IAAsB,CAAC,OAAO,QAAQ,kBAAkB,mBAAmB,oBAAoB,iBAAiB,CAAC;AAAA,GAN5I,kBAAA,gBAAA,CAAA,EAAA;;"}
|
|
1
|
+
{"version":3,"file":"SelectBuilder.cjs","sources":["../../../../../packages/database/src/builders/SelectBuilder.ts"],"sourcesContent":["import { With } from './internal/With'\nimport { Where } from './internal/Where'\nimport { aliasLiteral, formatColumnIdentifier } from '../utils'\nimport { Compiler } from './Compiler'\nimport { QueryBuilder } from './QueryBuilder'\nimport { Client, Connection } from '../client'\nimport { Literal } from '../Literal'\nimport { ConditionBuilder, ConditionExpression } from './ConditionBuilder'\nimport { LockModifier, LockType } from './LockType'\nimport { columnExpressionToLiteral } from './utils'\nimport { createSubQueryLiteralFactory, SubQueryExpression, SubQueryLiteralFactory } from './internal/Subqueries'\n\nexport type SelectBuilderSpecification = <Result>(qb: SelectBuilder<Result>) => SelectBuilder<Result>\n\nclass SelectBuilder<Result = SelectBuilder.Result> implements With.Aware, Where.Aware, QueryBuilder.Orderable<SelectBuilder<Result>>, QueryBuilder {\n\tconstructor(public readonly options: SelectBuilder.Options) {}\n\n\tpublic static create<Result = SelectBuilder.Result>() {\n\t\treturn new SelectBuilder<Result>({\n\t\t\tfrom: undefined,\n\t\t\torderBy: [],\n\t\t\tjoin: [],\n\t\t\tlimit: [undefined, undefined],\n\t\t\tselect: [],\n\t\t\twith: new With.Statement({}),\n\t\t\twhere: new Where.Statement([]),\n\t\t\tgrouping: {\n\t\t\t\tgroupingElement: [],\n\t\t\t},\n\t\t\tlock: undefined,\n\t\t\tmeta: {},\n\t\t})\n\t}\n\n\tpublic with(alias: string, expression: SubQueryExpression): SelectBuilder<Result> {\n\t\treturn this.withOption('with', this.options.with.withCte(alias, createSubQueryLiteralFactory(expression), false))\n\t}\n\n\tpublic withRecursive(alias: string, expression: SubQueryExpression, columns?: string[]): SelectBuilder<Result> {\n\t\treturn this.withOption(\n\t\t\t'with',\n\t\t\tthis.options.with.withCte(alias, createSubQueryLiteralFactory(expression), true, columns),\n\t\t)\n\t}\n\n\tpublic unionAll(expression: SubQueryExpression): SelectBuilder<Result> {\n\t\treturn this.withOption('union', {\n\t\t\ttype: 'all',\n\t\t\tliteral: createSubQueryLiteralFactory(expression),\n\t\t})\n\t}\n\n\tpublic unionDistinct(expression: SubQueryExpression): SelectBuilder<Result> {\n\t\treturn this.withOption('union', {\n\t\t\ttype: 'distinct',\n\t\t\tliteral: createSubQueryLiteralFactory(expression),\n\t\t})\n\t}\n\n\tpublic from(tableName: string | Literal, alias?: string): SelectBuilder<Result> {\n\t\treturn this.withOption('from', [...(this.options.from || []), [tableName, alias]])\n\t}\n\n\tpublic distinct(...on: (QueryBuilder.ColumnIdentifier | QueryBuilder.ColumnExpression)[]): SelectBuilder<Result> {\n\t\tconst onLiterals = on.map(columnExpressionToLiteral).filter((it): it is Literal => it !== undefined)\n\t\treturn this.withOption('distinct', onLiterals)\n\t}\n\n\tpublic select(columnName: QueryBuilder.ColumnIdentifier, alias?: string): SelectBuilder<Result>\n\tpublic select(callback: QueryBuilder.ColumnExpression, alias?: string): SelectBuilder<Result>\n\tpublic select(\n\t\texpr: QueryBuilder.ColumnIdentifier | QueryBuilder.ColumnExpression,\n\t\talias?: string,\n\t): SelectBuilder<Result> {\n\t\tlet raw = columnExpressionToLiteral(expr)\n\t\tif (raw === undefined) {\n\t\t\treturn this\n\t\t}\n\t\treturn this.withOption('select', [...this.options.select, aliasLiteral(raw, alias)])\n\t}\n\n\tpublic where(where: Where.Expression): SelectBuilder<Result> {\n\t\treturn this.withOption('where', this.options.where.withWhere(where))\n\t}\n\n\tpublic orderBy(\n\t\texpression: QueryBuilder.ColumnIdentifier | Literal,\n\t\tdirection: SelectBuilder.OrderByDirection = 'asc',\n\t): SelectBuilder<Result> {\n\t\tconst literal = expression instanceof Literal ? expression : new Literal(formatColumnIdentifier(expression))\n\t\treturn this.withOption('orderBy', [...this.options.orderBy, [literal, direction]])\n\t}\n\n\tpublic join(table: string | Literal, alias?: string, condition?: SelectBuilder.JoinCondition): SelectBuilder<Result> {\n\t\treturn this.withOption('join', [\n\t\t\t...this.options.join,\n\t\t\t{ type: 'inner', table, alias, condition: this.joinConditionToLiteral(condition) },\n\t\t])\n\t}\n\n\tpublic leftJoin(\n\t\ttable: string | Literal,\n\t\talias?: string,\n\t\tcondition?: SelectBuilder.JoinCondition,\n\t): SelectBuilder<Result> {\n\t\treturn this.withOption('join', [\n\t\t\t...this.options.join,\n\t\t\t{ type: 'left', table, alias, condition: this.joinConditionToLiteral(condition) },\n\t\t])\n\t}\n\n\tprivate joinConditionToLiteral(joinCondition?: SelectBuilder.JoinCondition): Literal | undefined {\n\t\tif (joinCondition === undefined) {\n\t\t\treturn undefined\n\t\t}\n\t\tconst builder = ConditionBuilder.process(joinCondition)\n\n\t\treturn builder.getSql() || undefined\n\t}\n\n\tpublic groupBy(columnName: QueryBuilder.ColumnIdentifier): SelectBuilder<Result>\n\tpublic groupBy(callback: QueryBuilder.ColumnExpression): SelectBuilder<Result>\n\tpublic groupBy(expr: QueryBuilder.ColumnIdentifier | QueryBuilder.ColumnExpression): SelectBuilder<Result> {\n\t\tlet raw = columnExpressionToLiteral(expr)\n\t\tif (raw === undefined) {\n\t\t\treturn this\n\t\t}\n\t\treturn this.withOption('grouping', {\n\t\t\t...this.options.grouping,\n\t\t\tgroupingElement: [...this.options.grouping.groupingElement, raw],\n\t\t})\n\t}\n\n\tpublic limit(limit?: number, offset?: number): SelectBuilder<Result> {\n\t\treturn this.withOption('limit', [limit, offset])\n\t}\n\n\tpublic lock(type: LockType, modifier?: LockModifier): SelectBuilder<Result> {\n\t\treturn this.withOption('lock', { type, modifier })\n\t}\n\n\tpublic meta(key: string, value: any): SelectBuilder<Result>\n\tpublic meta(values: Record<string, any>): SelectBuilder<Result>\n\tpublic meta(keyOrValues: string | Record<string, any>, value?: any): SelectBuilder<Result> {\n\t\treturn this.withOption('meta', {\n\t\t\t...this.options.meta,\n\t\t\t...(typeof keyOrValues === 'string' ? { [keyOrValues]: value } : keyOrValues),\n\t\t})\n\t}\n\n\tpublic match(spec: SelectBuilderSpecification): SelectBuilder<Result> {\n\t\treturn spec(this)\n\t}\n\n\tpublic async getResult(db: Client): Promise<Result[]> {\n\t\tconst namespaceContext = new Compiler.Context(db.schema, new Set())\n\t\tconst query = this.createQuery(namespaceContext)\n\t\tconst result: Connection.Result = await db.query(query.sql, query.parameters, query.meta)\n\n\t\treturn result.rows as any as Result[]\n\t}\n\n\tpublic createQuery(context: Compiler.Context): Literal {\n\t\tconst compiler = new Compiler()\n\n\t\tconst compiled = compiler.compileSelect(this.options, context)\n\n\t\treturn new Literal(compiled.sql, compiled.parameters, this.options.meta)\n\t}\n\n\tprotected withOption<K extends keyof SelectBuilder.Options, V extends SelectBuilder.Options[K]>(\n\t\tkey: K,\n\t\tvalue: V,\n\t): SelectBuilder<Result> {\n\t\treturn new SelectBuilder<Result>({ ...this.options, [key]: value })\n\t}\n}\n\nnamespace SelectBuilder {\n\texport type Callback = (qb: SelectBuilder<any>) => SelectBuilder<any>\n\n\texport type Result = Record<string, any>\n\n\texport type OrderByDirection = 'asc' | 'desc' | 'asc nulls last' | 'asc nulls first' | 'desc nulls first' | 'desc nulls last'\n\texport const orderByDirection = new Set<OrderByDirection>(['asc', 'desc', 'asc nulls last', 'asc nulls first', 'desc nulls first', 'desc nulls last'])\n\texport type Options = Readonly<\n\t\tWith.Options &\n\t\tWhere.Options & {\n\t\t\tdistinct?: Literal[]\n\t\t\tselect: Literal[]\n\t\t\tlimit: [number | undefined, number | undefined]\n\t\t\tfrom: undefined | [Literal | string, string | undefined][]\n\t\t\torderBy: [Literal, OrderByDirection][]\n\t\t\tjoin: {\n\t\t\t\ttype: 'inner' | 'left'\n\t\t\t\ttable: string | Literal\n\t\t\t\talias: string | undefined\n\t\t\t\tcondition: Literal | undefined\n\t\t\t}[]\n\t\t\tgrouping: {\n\t\t\t\tgroupingElement: Literal[]\n\t\t\t}\n\t\t\tlock?: { type: LockType; modifier?: LockModifier }\n\t\t\tmeta: Record<string, any>\n\t\t\tunion?: {\n\t\t\t\ttype: 'all' | 'distinct'\n\t\t\t\tliteral: SubQueryLiteralFactory\n\t\t\t}\n\t\t}\n\t>\n\n\texport type JoinCondition = ConditionExpression\n}\n\nexport { SelectBuilder }\n"],"names":["With","Where","createSubQueryLiteralFactory","columnExpressionToLiteral","aliasLiteral","Literal","formatColumnIdentifier","ConditionBuilder","Compiler","SelectBuilder"],"mappings":";;;;;;;;;;;;;;;;AAcA,MAAM,cAA6I;AAAA,EAClJ,YAA4B,SAAgC;AAAhC,SAAA,UAAA;AAAA,EAAA;AAAA,EAE5B,OAAc,SAAwC;AACrD,WAAO,IAAI,cAAsB;AAAA,MAChC,MAAM;AAAA,MACN,SAAS,CAAC;AAAA,MACV,MAAM,CAAC;AAAA,MACP,OAAO,CAAC,QAAW,MAAS;AAAA,MAC5B,QAAQ,CAAC;AAAA,MACT,MAAM,IAAIA,KAAAA,KAAK,UAAU,EAAE;AAAA,MAC3B,OAAO,IAAIC,MAAAA,MAAM,UAAU,EAAE;AAAA,MAC7B,UAAU;AAAA,QACT,iBAAiB,CAAA;AAAA,MAClB;AAAA,MACA,MAAM;AAAA,MACN,MAAM,CAAA;AAAA,IAAC,CACP;AAAA,EAAA;AAAA,EAGK,KAAK,OAAe,YAAuD;AACjF,WAAO,KAAK,WAAW,QAAQ,KAAK,QAAQ,KAAK,QAAQ,OAAOC,WAA6B,6BAAA,UAAU,GAAG,KAAK,CAAC;AAAA,EAAA;AAAA,EAG1G,cAAc,OAAe,YAAgC,SAA2C;AAC9G,WAAO,KAAK;AAAA,MACX;AAAA,MACA,KAAK,QAAQ,KAAK,QAAQ,OAAOA,wCAA6B,UAAU,GAAG,MAAM,OAAO;AAAA,IACzF;AAAA,EAAA;AAAA,EAGM,SAA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AAC/C,UAAA,mBAAmB,IAAIK,kBAAS,QAAQ,GAAG,QAAQ,oBAAI,KAAK;AAC5D,UAAA,QAAQ,KAAK,YAAY,gBAAgB;AACzC,UAAA,SAA4B,MAAM,GAAG,MAAM,MAAM,KAAK,MAAM,YAAY,MAAM,IAAI;AAExF,WAAO,OAAO;AAAA,EAAA;AAAA,EAGR,YAAY,SAAoC;AAChD,UAAA,WAAW,IAAIA,kBAAS;AAE9B,UAAM,WAAW,SAAS,cAAc,KAAK,SAAS,OAAO;AAEtD,WAAA,IAAIH,QAAAA,QAAQ,SAAS,KAAK,SAAS,YAAY,KAAK,QAAQ,IAAI;AAAA,EAAA;AAAA,EAG9D,WACT,KACA,OACwB;AACjB,WAAA,IAAI,cAAsB,EAAE,GAAG,KAAK,SAAS,CAAC,GAAG,GAAG,OAAO;AAAA,EAAA;AAEpE;AAAA,CAEA,CAAUI,mBAAV;AAMcA,iBAAA,mBAAmB,oBAAI,IAAsB,CAAC,OAAO,QAAQ,kBAAkB,mBAAmB,oBAAoB,iBAAiB,CAAC;AAAA,GAN5I,kBAAA,gBAAA,CAAA,EAAA;;"}
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { With } from "./internal/With.js";
|
|
2
2
|
import { Where } from "./internal/Where.js";
|
|
3
|
-
import { aliasLiteral } from "../utils/sql.js";
|
|
3
|
+
import { aliasLiteral, formatColumnIdentifier } from "../utils/sql.js";
|
|
4
4
|
import { Compiler } from "./Compiler.js";
|
|
5
5
|
import { Literal } from "../Literal.js";
|
|
6
6
|
import { columnExpressionToLiteral } from "./utils.js";
|
|
@@ -12,7 +12,6 @@ import "../client/Connection.js";
|
|
|
12
12
|
import "node:timers";
|
|
13
13
|
import "../utils/Mutex.js";
|
|
14
14
|
import "pg";
|
|
15
|
-
import { toFqnWrap } from "./formatUtils.js";
|
|
16
15
|
class SelectBuilder {
|
|
17
16
|
constructor(options) {
|
|
18
17
|
this.options = options;
|
|
@@ -72,7 +71,7 @@ class SelectBuilder {
|
|
|
72
71
|
return this.withOption("where", this.options.where.withWhere(where));
|
|
73
72
|
}
|
|
74
73
|
orderBy(expression, direction = "asc") {
|
|
75
|
-
const literal = expression instanceof Literal ? expression : new Literal(
|
|
74
|
+
const literal = expression instanceof Literal ? expression : new Literal(formatColumnIdentifier(expression));
|
|
76
75
|
return this.withOption("orderBy", [...this.options.orderBy, [literal, direction]]);
|
|
77
76
|
}
|
|
78
77
|
join(table, alias, condition) {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"SelectBuilder.js","sources":["../../../../../packages/database/src/builders/SelectBuilder.ts"],"sourcesContent":["import { With } from './internal/With'\nimport { Where } from './internal/Where'\nimport { aliasLiteral } from '../utils'\nimport { Compiler } from './Compiler'\nimport { QueryBuilder } from './QueryBuilder'\nimport { Client, Connection } from '../client'\nimport { Literal } from '../Literal'\nimport { ConditionBuilder, ConditionCallback, ConditionExpression } from './ConditionBuilder'\nimport { LockModifier, LockType } from './LockType'\nimport { columnExpressionToLiteral, toFqnWrap } from './utils'\nimport { createSubQueryLiteralFactory, SubQueryExpression, SubQueryLiteralFactory } from './internal/Subqueries'\n\nexport type SelectBuilderSpecification = <Result>(qb: SelectBuilder<Result>) => SelectBuilder<Result>\n\nclass SelectBuilder<Result = SelectBuilder.Result> implements With.Aware, Where.Aware, QueryBuilder.Orderable<SelectBuilder<Result>>, QueryBuilder {\n\tconstructor(public readonly options: SelectBuilder.Options) {}\n\n\tpublic static create<Result = SelectBuilder.Result>() {\n\t\treturn new SelectBuilder<Result>({\n\t\t\tfrom: undefined,\n\t\t\torderBy: [],\n\t\t\tjoin: [],\n\t\t\tlimit: [undefined, undefined],\n\t\t\tselect: [],\n\t\t\twith: new With.Statement({}),\n\t\t\twhere: new Where.Statement([]),\n\t\t\tgrouping: {\n\t\t\t\tgroupingElement: [],\n\t\t\t},\n\t\t\tlock: undefined,\n\t\t\tmeta: {},\n\t\t})\n\t}\n\n\tpublic with(alias: string, expression: SubQueryExpression): SelectBuilder<Result> {\n\t\treturn this.withOption('with', this.options.with.withCte(alias, createSubQueryLiteralFactory(expression), false))\n\t}\n\n\tpublic withRecursive(alias: string, expression: SubQueryExpression, columns?: string[]): SelectBuilder<Result> {\n\t\treturn this.withOption(\n\t\t\t'with',\n\t\t\tthis.options.with.withCte(alias, createSubQueryLiteralFactory(expression), true, columns),\n\t\t)\n\t}\n\n\tpublic unionAll(expression: SubQueryExpression): SelectBuilder<Result> {\n\t\treturn this.withOption('union', {\n\t\t\ttype: 'all',\n\t\t\tliteral: createSubQueryLiteralFactory(expression),\n\t\t})\n\t}\n\n\tpublic unionDistinct(expression: SubQueryExpression): SelectBuilder<Result> {\n\t\treturn this.withOption('union', {\n\t\t\ttype: 'distinct',\n\t\t\tliteral: createSubQueryLiteralFactory(expression),\n\t\t})\n\t}\n\n\tpublic from(tableName: string | Literal, alias?: string): SelectBuilder<Result> {\n\t\treturn this.withOption('from', [...(this.options.from || []), [tableName, alias]])\n\t}\n\n\tpublic distinct(...on: (QueryBuilder.ColumnIdentifier | QueryBuilder.ColumnExpression)[]): SelectBuilder<Result> {\n\t\tconst onLiterals = on.map(columnExpressionToLiteral).filter((it): it is Literal => it !== undefined)\n\t\treturn this.withOption('distinct', onLiterals)\n\t}\n\n\tpublic select(columnName: QueryBuilder.ColumnIdentifier, alias?: string): SelectBuilder<Result>\n\tpublic select(callback: QueryBuilder.ColumnExpression, alias?: string): SelectBuilder<Result>\n\tpublic select(\n\t\texpr: QueryBuilder.ColumnIdentifier | QueryBuilder.ColumnExpression,\n\t\talias?: string,\n\t): SelectBuilder<Result> {\n\t\tlet raw = columnExpressionToLiteral(expr)\n\t\tif (raw === undefined) {\n\t\t\treturn this\n\t\t}\n\t\treturn this.withOption('select', [...this.options.select, aliasLiteral(raw, alias)])\n\t}\n\n\tpublic where(where: Where.Expression): SelectBuilder<Result> {\n\t\treturn this.withOption('where', this.options.where.withWhere(where))\n\t}\n\n\tpublic orderBy(\n\t\texpression: QueryBuilder.ColumnIdentifier | Literal,\n\t\tdirection: SelectBuilder.OrderByDirection = 'asc',\n\t): SelectBuilder<Result> {\n\t\tconst literal = expression instanceof Literal ? expression : new Literal(toFqnWrap(expression))\n\t\treturn this.withOption('orderBy', [...this.options.orderBy, [literal, direction]])\n\t}\n\n\tpublic join(table: string | Literal, alias?: string, condition?: SelectBuilder.JoinCondition): SelectBuilder<Result> {\n\t\treturn this.withOption('join', [\n\t\t\t...this.options.join,\n\t\t\t{ type: 'inner', table, alias, condition: this.joinConditionToLiteral(condition) },\n\t\t])\n\t}\n\n\tpublic leftJoin(\n\t\ttable: string | Literal,\n\t\talias?: string,\n\t\tcondition?: SelectBuilder.JoinCondition,\n\t): SelectBuilder<Result> {\n\t\treturn this.withOption('join', [\n\t\t\t...this.options.join,\n\t\t\t{ type: 'left', table, alias, condition: this.joinConditionToLiteral(condition) },\n\t\t])\n\t}\n\n\tprivate joinConditionToLiteral(joinCondition?: SelectBuilder.JoinCondition): Literal | undefined {\n\t\tif (joinCondition === undefined) {\n\t\t\treturn undefined\n\t\t}\n\t\tconst builder = ConditionBuilder.process(joinCondition)\n\n\t\treturn builder.getSql() || undefined\n\t}\n\n\tpublic groupBy(columnName: QueryBuilder.ColumnIdentifier): SelectBuilder<Result>\n\tpublic groupBy(callback: QueryBuilder.ColumnExpression): SelectBuilder<Result>\n\tpublic groupBy(expr: QueryBuilder.ColumnIdentifier | QueryBuilder.ColumnExpression): SelectBuilder<Result> {\n\t\tlet raw = columnExpressionToLiteral(expr)\n\t\tif (raw === undefined) {\n\t\t\treturn this\n\t\t}\n\t\treturn this.withOption('grouping', {\n\t\t\t...this.options.grouping,\n\t\t\tgroupingElement: [...this.options.grouping.groupingElement, raw],\n\t\t})\n\t}\n\n\tpublic limit(limit?: number, offset?: number): SelectBuilder<Result> {\n\t\treturn this.withOption('limit', [limit, offset])\n\t}\n\n\tpublic lock(type: LockType, modifier?: LockModifier): SelectBuilder<Result> {\n\t\treturn this.withOption('lock', { type, modifier })\n\t}\n\n\tpublic meta(key: string, value: any): SelectBuilder<Result>\n\tpublic meta(values: Record<string, any>): SelectBuilder<Result>\n\tpublic meta(keyOrValues: string | Record<string, any>, value?: any): SelectBuilder<Result> {\n\t\treturn this.withOption('meta', {\n\t\t\t...this.options.meta,\n\t\t\t...(typeof keyOrValues === 'string' ? { [keyOrValues]: value } : keyOrValues),\n\t\t})\n\t}\n\n\tpublic match(spec: SelectBuilderSpecification): SelectBuilder<Result> {\n\t\treturn spec(this)\n\t}\n\n\tpublic async getResult(db: Client): Promise<Result[]> {\n\t\tconst namespaceContext = new Compiler.Context(db.schema, new Set())\n\t\tconst query = this.createQuery(namespaceContext)\n\t\tconst result: Connection.Result = await db.query(query.sql, query.parameters, query.meta)\n\n\t\treturn result.rows as any as Result[]\n\t}\n\n\tpublic createQuery(context: Compiler.Context): Literal {\n\t\tconst compiler = new Compiler()\n\n\t\tconst compiled = compiler.compileSelect(this.options, context)\n\n\t\treturn new Literal(compiled.sql, compiled.parameters, this.options.meta)\n\t}\n\n\tprotected withOption<K extends keyof SelectBuilder.Options, V extends SelectBuilder.Options[K]>(\n\t\tkey: K,\n\t\tvalue: V,\n\t): SelectBuilder<Result> {\n\t\treturn new SelectBuilder<Result>({ ...this.options, [key]: value })\n\t}\n}\n\nnamespace SelectBuilder {\n\texport type Callback = (qb: SelectBuilder<any>) => SelectBuilder<any>\n\n\texport type Result = Record<string, any>\n\n\texport type OrderByDirection = 'asc' | 'desc' | 'asc nulls last' | 'asc nulls first' | 'desc nulls first' | 'desc nulls last'\n\texport const orderByDirection = new Set<OrderByDirection>(['asc', 'desc', 'asc nulls last', 'asc nulls first', 'desc nulls first', 'desc nulls last'])\n\texport type Options = Readonly<\n\t\tWith.Options &\n\t\tWhere.Options & {\n\t\t\tdistinct?: Literal[]\n\t\t\tselect: Literal[]\n\t\t\tlimit: [number | undefined, number | undefined]\n\t\t\tfrom: undefined | [Literal | string, string | undefined][]\n\t\t\torderBy: [Literal, OrderByDirection][]\n\t\t\tjoin: {\n\t\t\t\ttype: 'inner' | 'left'\n\t\t\t\ttable: string | Literal\n\t\t\t\talias: string | undefined\n\t\t\t\tcondition: Literal | undefined\n\t\t\t}[]\n\t\t\tgrouping: {\n\t\t\t\tgroupingElement: Literal[]\n\t\t\t}\n\t\t\tlock?: { type: LockType; modifier?: LockModifier }\n\t\t\tmeta: Record<string, any>\n\t\t\tunion?: {\n\t\t\t\ttype: 'all' | 'distinct'\n\t\t\t\tliteral: SubQueryLiteralFactory\n\t\t\t}\n\t\t}\n\t>\n\n\texport type JoinCondition = ConditionExpression\n}\n\nexport { SelectBuilder }\n"],"names":["SelectBuilder"],"mappings":";;;;;;;;;;;;;;;AAcA,MAAM,cAA6I;AAAA,EAClJ,YAA4B,SAAgC;AAAhC,SAAA,UAAA;AAAA,EAAA;AAAA,EAE5B,OAAc,SAAwC;AACrD,WAAO,IAAI,cAAsB;AAAA,MAChC,MAAM;AAAA,MACN,SAAS,CAAC;AAAA,MACV,MAAM,CAAC;AAAA,MACP,OAAO,CAAC,QAAW,MAAS;AAAA,MAC5B,QAAQ,CAAC;AAAA,MACT,MAAM,IAAI,KAAK,UAAU,EAAE;AAAA,MAC3B,OAAO,IAAI,MAAM,UAAU,EAAE;AAAA,MAC7B,UAAU;AAAA,QACT,iBAAiB,CAAA;AAAA,MAClB;AAAA,MACA,MAAM;AAAA,MACN,MAAM,CAAA;AAAA,IAAC,CACP;AAAA,EAAA;AAAA,EAGK,KAAK,OAAe,YAAuD;AACjF,WAAO,KAAK,WAAW,QAAQ,KAAK,QAAQ,KAAK,QAAQ,OAAO,6BAA6B,UAAU,GAAG,KAAK,CAAC;AAAA,EAAA;AAAA,EAG1G,cAAc,OAAe,YAAgC,SAA2C;AAC9G,WAAO,KAAK;AAAA,MACX;AAAA,MACA,KAAK,QAAQ,KAAK,QAAQ,OAAO,6BAA6B,UAAU,GAAG,MAAM,OAAO;AAAA,IACzF;AAAA,EAAA;AAAA,EAGM,SAAS,YAAuD;AAC/D,WAAA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AAK;AAC5D,UAAA,QAAQ,KAAK,YAAY,gBAAgB;AACzC,UAAA,SAA4B,MAAM,GAAG,MAAM,MAAM,KAAK,MAAM,YAAY,MAAM,IAAI;AAExF,WAAO,OAAO;AAAA,EAAA;AAAA,EAGR,YAAY,SAAoC;AAChD,UAAA,WAAW,IAAI,SAAS;AAE9B,UAAM,WAAW,SAAS,cAAc,KAAK,SAAS,OAAO;AAEtD,WAAA,IAAI,QAAQ,SAAS,KAAK,SAAS,YAAY,KAAK,QAAQ,IAAI;AAAA,EAAA;AAAA,EAG9D,WACT,KACA,OACwB;AACjB,WAAA,IAAI,cAAsB,EAAE,GAAG,KAAK,SAAS,CAAC,GAAG,GAAG,OAAO;AAAA,EAAA;AAEpE;AAAA,CAEA,CAAUA,mBAAV;AAMcA,iBAAA,mBAAmB,oBAAI,IAAsB,CAAC,OAAO,QAAQ,kBAAkB,mBAAmB,oBAAoB,iBAAiB,CAAC;AAAA,GAN5I,kBAAA,gBAAA,CAAA,EAAA;"}
|
|
1
|
+
{"version":3,"file":"SelectBuilder.js","sources":["../../../../../packages/database/src/builders/SelectBuilder.ts"],"sourcesContent":["import { With } from './internal/With'\nimport { Where } from './internal/Where'\nimport { aliasLiteral, formatColumnIdentifier } from '../utils'\nimport { Compiler } from './Compiler'\nimport { QueryBuilder } from './QueryBuilder'\nimport { Client, Connection } from '../client'\nimport { Literal } from '../Literal'\nimport { ConditionBuilder, ConditionExpression } from './ConditionBuilder'\nimport { LockModifier, LockType } from './LockType'\nimport { columnExpressionToLiteral } from './utils'\nimport { createSubQueryLiteralFactory, SubQueryExpression, SubQueryLiteralFactory } from './internal/Subqueries'\n\nexport type SelectBuilderSpecification = <Result>(qb: SelectBuilder<Result>) => SelectBuilder<Result>\n\nclass SelectBuilder<Result = SelectBuilder.Result> implements With.Aware, Where.Aware, QueryBuilder.Orderable<SelectBuilder<Result>>, QueryBuilder {\n\tconstructor(public readonly options: SelectBuilder.Options) {}\n\n\tpublic static create<Result = SelectBuilder.Result>() {\n\t\treturn new SelectBuilder<Result>({\n\t\t\tfrom: undefined,\n\t\t\torderBy: [],\n\t\t\tjoin: [],\n\t\t\tlimit: [undefined, undefined],\n\t\t\tselect: [],\n\t\t\twith: new With.Statement({}),\n\t\t\twhere: new Where.Statement([]),\n\t\t\tgrouping: {\n\t\t\t\tgroupingElement: [],\n\t\t\t},\n\t\t\tlock: undefined,\n\t\t\tmeta: {},\n\t\t})\n\t}\n\n\tpublic with(alias: string, expression: SubQueryExpression): SelectBuilder<Result> {\n\t\treturn this.withOption('with', this.options.with.withCte(alias, createSubQueryLiteralFactory(expression), false))\n\t}\n\n\tpublic withRecursive(alias: string, expression: SubQueryExpression, columns?: string[]): SelectBuilder<Result> {\n\t\treturn this.withOption(\n\t\t\t'with',\n\t\t\tthis.options.with.withCte(alias, createSubQueryLiteralFactory(expression), true, columns),\n\t\t)\n\t}\n\n\tpublic unionAll(expression: SubQueryExpression): SelectBuilder<Result> {\n\t\treturn this.withOption('union', {\n\t\t\ttype: 'all',\n\t\t\tliteral: createSubQueryLiteralFactory(expression),\n\t\t})\n\t}\n\n\tpublic unionDistinct(expression: SubQueryExpression): SelectBuilder<Result> {\n\t\treturn this.withOption('union', {\n\t\t\ttype: 'distinct',\n\t\t\tliteral: createSubQueryLiteralFactory(expression),\n\t\t})\n\t}\n\n\tpublic from(tableName: string | Literal, alias?: string): SelectBuilder<Result> {\n\t\treturn this.withOption('from', [...(this.options.from || []), [tableName, alias]])\n\t}\n\n\tpublic distinct(...on: (QueryBuilder.ColumnIdentifier | QueryBuilder.ColumnExpression)[]): SelectBuilder<Result> {\n\t\tconst onLiterals = on.map(columnExpressionToLiteral).filter((it): it is Literal => it !== undefined)\n\t\treturn this.withOption('distinct', onLiterals)\n\t}\n\n\tpublic select(columnName: QueryBuilder.ColumnIdentifier, alias?: string): SelectBuilder<Result>\n\tpublic select(callback: QueryBuilder.ColumnExpression, alias?: string): SelectBuilder<Result>\n\tpublic select(\n\t\texpr: QueryBuilder.ColumnIdentifier | QueryBuilder.ColumnExpression,\n\t\talias?: string,\n\t): SelectBuilder<Result> {\n\t\tlet raw = columnExpressionToLiteral(expr)\n\t\tif (raw === undefined) {\n\t\t\treturn this\n\t\t}\n\t\treturn this.withOption('select', [...this.options.select, aliasLiteral(raw, alias)])\n\t}\n\n\tpublic where(where: Where.Expression): SelectBuilder<Result> {\n\t\treturn this.withOption('where', this.options.where.withWhere(where))\n\t}\n\n\tpublic orderBy(\n\t\texpression: QueryBuilder.ColumnIdentifier | Literal,\n\t\tdirection: SelectBuilder.OrderByDirection = 'asc',\n\t): SelectBuilder<Result> {\n\t\tconst literal = expression instanceof Literal ? expression : new Literal(formatColumnIdentifier(expression))\n\t\treturn this.withOption('orderBy', [...this.options.orderBy, [literal, direction]])\n\t}\n\n\tpublic join(table: string | Literal, alias?: string, condition?: SelectBuilder.JoinCondition): SelectBuilder<Result> {\n\t\treturn this.withOption('join', [\n\t\t\t...this.options.join,\n\t\t\t{ type: 'inner', table, alias, condition: this.joinConditionToLiteral(condition) },\n\t\t])\n\t}\n\n\tpublic leftJoin(\n\t\ttable: string | Literal,\n\t\talias?: string,\n\t\tcondition?: SelectBuilder.JoinCondition,\n\t): SelectBuilder<Result> {\n\t\treturn this.withOption('join', [\n\t\t\t...this.options.join,\n\t\t\t{ type: 'left', table, alias, condition: this.joinConditionToLiteral(condition) },\n\t\t])\n\t}\n\n\tprivate joinConditionToLiteral(joinCondition?: SelectBuilder.JoinCondition): Literal | undefined {\n\t\tif (joinCondition === undefined) {\n\t\t\treturn undefined\n\t\t}\n\t\tconst builder = ConditionBuilder.process(joinCondition)\n\n\t\treturn builder.getSql() || undefined\n\t}\n\n\tpublic groupBy(columnName: QueryBuilder.ColumnIdentifier): SelectBuilder<Result>\n\tpublic groupBy(callback: QueryBuilder.ColumnExpression): SelectBuilder<Result>\n\tpublic groupBy(expr: QueryBuilder.ColumnIdentifier | QueryBuilder.ColumnExpression): SelectBuilder<Result> {\n\t\tlet raw = columnExpressionToLiteral(expr)\n\t\tif (raw === undefined) {\n\t\t\treturn this\n\t\t}\n\t\treturn this.withOption('grouping', {\n\t\t\t...this.options.grouping,\n\t\t\tgroupingElement: [...this.options.grouping.groupingElement, raw],\n\t\t})\n\t}\n\n\tpublic limit(limit?: number, offset?: number): SelectBuilder<Result> {\n\t\treturn this.withOption('limit', [limit, offset])\n\t}\n\n\tpublic lock(type: LockType, modifier?: LockModifier): SelectBuilder<Result> {\n\t\treturn this.withOption('lock', { type, modifier })\n\t}\n\n\tpublic meta(key: string, value: any): SelectBuilder<Result>\n\tpublic meta(values: Record<string, any>): SelectBuilder<Result>\n\tpublic meta(keyOrValues: string | Record<string, any>, value?: any): SelectBuilder<Result> {\n\t\treturn this.withOption('meta', {\n\t\t\t...this.options.meta,\n\t\t\t...(typeof keyOrValues === 'string' ? { [keyOrValues]: value } : keyOrValues),\n\t\t})\n\t}\n\n\tpublic match(spec: SelectBuilderSpecification): SelectBuilder<Result> {\n\t\treturn spec(this)\n\t}\n\n\tpublic async getResult(db: Client): Promise<Result[]> {\n\t\tconst namespaceContext = new Compiler.Context(db.schema, new Set())\n\t\tconst query = this.createQuery(namespaceContext)\n\t\tconst result: Connection.Result = await db.query(query.sql, query.parameters, query.meta)\n\n\t\treturn result.rows as any as Result[]\n\t}\n\n\tpublic createQuery(context: Compiler.Context): Literal {\n\t\tconst compiler = new Compiler()\n\n\t\tconst compiled = compiler.compileSelect(this.options, context)\n\n\t\treturn new Literal(compiled.sql, compiled.parameters, this.options.meta)\n\t}\n\n\tprotected withOption<K extends keyof SelectBuilder.Options, V extends SelectBuilder.Options[K]>(\n\t\tkey: K,\n\t\tvalue: V,\n\t): SelectBuilder<Result> {\n\t\treturn new SelectBuilder<Result>({ ...this.options, [key]: value })\n\t}\n}\n\nnamespace SelectBuilder {\n\texport type Callback = (qb: SelectBuilder<any>) => SelectBuilder<any>\n\n\texport type Result = Record<string, any>\n\n\texport type OrderByDirection = 'asc' | 'desc' | 'asc nulls last' | 'asc nulls first' | 'desc nulls first' | 'desc nulls last'\n\texport const orderByDirection = new Set<OrderByDirection>(['asc', 'desc', 'asc nulls last', 'asc nulls first', 'desc nulls first', 'desc nulls last'])\n\texport type Options = Readonly<\n\t\tWith.Options &\n\t\tWhere.Options & {\n\t\t\tdistinct?: Literal[]\n\t\t\tselect: Literal[]\n\t\t\tlimit: [number | undefined, number | undefined]\n\t\t\tfrom: undefined | [Literal | string, string | undefined][]\n\t\t\torderBy: [Literal, OrderByDirection][]\n\t\t\tjoin: {\n\t\t\t\ttype: 'inner' | 'left'\n\t\t\t\ttable: string | Literal\n\t\t\t\talias: string | undefined\n\t\t\t\tcondition: Literal | undefined\n\t\t\t}[]\n\t\t\tgrouping: {\n\t\t\t\tgroupingElement: Literal[]\n\t\t\t}\n\t\t\tlock?: { type: LockType; modifier?: LockModifier }\n\t\t\tmeta: Record<string, any>\n\t\t\tunion?: {\n\t\t\t\ttype: 'all' | 'distinct'\n\t\t\t\tliteral: SubQueryLiteralFactory\n\t\t\t}\n\t\t}\n\t>\n\n\texport type JoinCondition = ConditionExpression\n}\n\nexport { SelectBuilder }\n"],"names":["SelectBuilder"],"mappings":";;;;;;;;;;;;;;AAcA,MAAM,cAA6I;AAAA,EAClJ,YAA4B,SAAgC;AAAhC,SAAA,UAAA;AAAA,EAAA;AAAA,EAE5B,OAAc,SAAwC;AACrD,WAAO,IAAI,cAAsB;AAAA,MAChC,MAAM;AAAA,MACN,SAAS,CAAC;AAAA,MACV,MAAM,CAAC;AAAA,MACP,OAAO,CAAC,QAAW,MAAS;AAAA,MAC5B,QAAQ,CAAC;AAAA,MACT,MAAM,IAAI,KAAK,UAAU,EAAE;AAAA,MAC3B,OAAO,IAAI,MAAM,UAAU,EAAE;AAAA,MAC7B,UAAU;AAAA,QACT,iBAAiB,CAAA;AAAA,MAClB;AAAA,MACA,MAAM;AAAA,MACN,MAAM,CAAA;AAAA,IAAC,CACP;AAAA,EAAA;AAAA,EAGK,KAAK,OAAe,YAAuD;A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AA,KAAK,WAAW,QAAQ;AAAA,MAC9B,GAAG,KAAK,QAAQ;AAAA,MAChB,GAAI,OAAO,gBAAgB,WAAW,EAAE,CAAC,WAAW,GAAG,MAAA,IAAU;AAAA,IAAA,CACjE;AAAA,EAAA;AAAA,EAGK,MAAM,MAAyD;AACrE,WAAO,KAAK,IAAI;AAAA,EAAA;AAAA,EAGjB,MAAa,UAAU,IAA+B;AAC/C,UAAA,mBAAmB,IAAI,SAAS,QAAQ,GAAG,QAAQ,oBAAI,KAAK;AAC5D,UAAA,QAAQ,KAAK,YAAY,gBAAgB;AACzC,UAAA,SAA4B,MAAM,GAAG,MAAM,MAAM,KAAK,MAAM,YAAY,MAAM,IAAI;AAExF,WAAO,OAAO;AAAA,EAAA;AAAA,EAGR,YAAY,SAAoC;AAChD,UAAA,WAAW,IAAI,SAAS;AAE9B,UAAM,WAAW,SAAS,cAAc,KAAK,SAAS,OAAO;AAEtD,WAAA,IAAI,QAAQ,SAAS,KAAK,SAAS,YAAY,KAAK,QAAQ,IAAI;AAAA,EAAA;AAAA,EAG9D,WACT,KACA,OACwB;AACjB,WAAA,IAAI,cAAsB,EAAE,GAAG,KAAK,SAAS,CAAC,GAAG,GAAG,OAAO;AAAA,EAAA;AAEpE;AAAA,CAEA,CAAUA,mBAAV;AAMcA,iBAAA,mBAAmB,oBAAI,IAAsB,CAAC,OAAO,QAAQ,kBAAkB,mBAAmB,oBAAoB,iBAAiB,CAAC;AAAA,GAN5I,kBAAA,gBAAA,CAAA,EAAA;"}
|
|
@@ -2,7 +2,17 @@
|
|
|
2
2
|
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
3
3
|
const Literal = require("../Literal.cjs");
|
|
4
4
|
const utils = require("./utils.cjs");
|
|
5
|
-
const
|
|
5
|
+
const sql = require("../utils/sql.cjs");
|
|
6
|
+
require("./Compiler.cjs");
|
|
7
|
+
require("./SelectBuilder.cjs");
|
|
8
|
+
require("./internal/With.cjs");
|
|
9
|
+
require("./internal/Where.cjs");
|
|
10
|
+
require("../client/EventManager.cjs");
|
|
11
|
+
require("@contember/queryable");
|
|
12
|
+
require("../client/Connection.cjs");
|
|
13
|
+
require("node:timers");
|
|
14
|
+
require("../utils/Mutex.cjs");
|
|
15
|
+
require("pg");
|
|
6
16
|
class WindowFunction {
|
|
7
17
|
constructor(windowFunction, partitionByExpr, orderByColumns) {
|
|
8
18
|
this.windowFunction = windowFunction;
|
|
@@ -23,7 +33,7 @@ class WindowFunction {
|
|
|
23
33
|
return new WindowFunction(this.windowFunction, raw, this.orderByColumns);
|
|
24
34
|
}
|
|
25
35
|
orderBy(expression, direction = "asc") {
|
|
26
|
-
const raw = expression instanceof Literal.Literal ? expression : new Literal.Literal(
|
|
36
|
+
const raw = expression instanceof Literal.Literal ? expression : new Literal.Literal(sql.formatColumnIdentifier(expression));
|
|
27
37
|
return new WindowFunction(this.windowFunction, this.partitionByExpr, [
|
|
28
38
|
...this.orderByColumns,
|
|
29
39
|
raw.appendString(direction === "asc" ? " asc" : " desc")
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"WindowFunction.cjs","sources":["../../../../../packages/database/src/builders/WindowFunction.ts"],"sourcesContent":["import { QueryBuilder } from './QueryBuilder'\nimport { Literal } from '../Literal'\nimport { columnExpressionToLiteral
|
|
1
|
+
{"version":3,"file":"WindowFunction.cjs","sources":["../../../../../packages/database/src/builders/WindowFunction.ts"],"sourcesContent":["import { QueryBuilder } from './QueryBuilder'\nimport { Literal } from '../Literal'\nimport { columnExpressionToLiteral } from './utils'\nimport { formatColumnIdentifier } from '../utils'\n\nclass WindowFunction<HasFunction extends boolean> implements QueryBuilder.Orderable<WindowFunction<HasFunction>> {\n\tprivate constructor(\n\t\tprivate readonly windowFunction: Literal | undefined,\n\t\tprivate readonly partitionByExpr: Literal | undefined,\n\t\tprivate readonly orderByColumns: Literal[],\n\t) {}\n\n\tpublic static createEmpty(): WindowFunction<false> {\n\t\treturn new WindowFunction<false>(undefined, undefined, [])\n\t}\n\n\tpublic rowNumber(): WindowFunction<true> {\n\t\treturn new WindowFunction(new Literal('row_number()'), this.partitionByExpr, this.orderByColumns)\n\t}\n\n\tpublic partitionBy(columnName: QueryBuilder.ColumnIdentifier): WindowFunction<HasFunction>\n\tpublic partitionBy(callback: QueryBuilder.ColumnExpression): WindowFunction<HasFunction>\n\tpublic partitionBy(expr: QueryBuilder.ColumnIdentifier | QueryBuilder.ColumnExpression): WindowFunction<HasFunction> {\n\t\tconst raw = columnExpressionToLiteral(expr)\n\t\tif (raw === undefined) {\n\t\t\treturn this\n\t\t}\n\t\treturn new WindowFunction(this.windowFunction, raw, this.orderByColumns)\n\t}\n\n\torderBy(\n\t\texpression: QueryBuilder.ColumnIdentifier | Literal,\n\t\tdirection: 'asc' | 'desc' = 'asc',\n\t): WindowFunction<HasFunction> {\n\t\tconst raw = expression instanceof Literal ? expression : new Literal(formatColumnIdentifier(expression))\n\t\treturn new WindowFunction(this.windowFunction, this.partitionByExpr, [\n\t\t\t...this.orderByColumns,\n\t\t\traw.appendString(direction === 'asc' ? ' asc' : ' desc'),\n\t\t])\n\t}\n\n\tcompile(): Literal {\n\t\tif (this.windowFunction === undefined) {\n\t\t\tthrow new Error()\n\t\t}\n\t\tlet windowDefinition = new Literal('')\n\t\tif (this.partitionByExpr !== undefined) {\n\t\t\twindowDefinition = windowDefinition.appendString('partition by ').append(this.partitionByExpr)\n\t\t}\n\t\tif (this.orderByColumns.length > 0) {\n\t\t\twindowDefinition = windowDefinition.appendString(' order by ').appendAll(this.orderByColumns, ', ')\n\t\t}\n\t\treturn new Literal(this.windowFunction.sql + ' over(' + windowDefinition.sql + ')', [\n\t\t\t...this.windowFunction.parameters,\n\t\t\t...windowDefinition.parameters,\n\t\t])\n\t}\n}\n\nexport { WindowFunction }\n"],"names":["Literal","columnExpressionToLiteral","formatColumnIdentifier"],"mappings":";;;;;;;;;;;;;;;AAKA,MAAM,eAA2G;AAAA,EACxG,YACU,gBACA,iBACA,gBAChB;AA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AAA;AAEH;;"}
|
|
@@ -1,6 +1,16 @@
|
|
|
1
1
|
import { Literal } from "../Literal.js";
|
|
2
2
|
import { columnExpressionToLiteral } from "./utils.js";
|
|
3
|
-
import {
|
|
3
|
+
import { formatColumnIdentifier } from "../utils/sql.js";
|
|
4
|
+
import "./Compiler.js";
|
|
5
|
+
import "./SelectBuilder.js";
|
|
6
|
+
import "./internal/With.js";
|
|
7
|
+
import "./internal/Where.js";
|
|
8
|
+
import "../client/EventManager.js";
|
|
9
|
+
import "@contember/queryable";
|
|
10
|
+
import "../client/Connection.js";
|
|
11
|
+
import "node:timers";
|
|
12
|
+
import "../utils/Mutex.js";
|
|
13
|
+
import "pg";
|
|
4
14
|
class WindowFunction {
|
|
5
15
|
constructor(windowFunction, partitionByExpr, orderByColumns) {
|
|
6
16
|
this.windowFunction = windowFunction;
|
|
@@ -21,7 +31,7 @@ class WindowFunction {
|
|
|
21
31
|
return new WindowFunction(this.windowFunction, raw, this.orderByColumns);
|
|
22
32
|
}
|
|
23
33
|
orderBy(expression, direction = "asc") {
|
|
24
|
-
const raw = expression instanceof Literal ? expression : new Literal(
|
|
34
|
+
const raw = expression instanceof Literal ? expression : new Literal(formatColumnIdentifier(expression));
|
|
25
35
|
return new WindowFunction(this.windowFunction, this.partitionByExpr, [
|
|
26
36
|
...this.orderByColumns,
|
|
27
37
|
raw.appendString(direction === "asc" ? " asc" : " desc")
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"WindowFunction.js","sources":["../../../../../packages/database/src/builders/WindowFunction.ts"],"sourcesContent":["import { QueryBuilder } from './QueryBuilder'\nimport { Literal } from '../Literal'\nimport { columnExpressionToLiteral
|
|
1
|
+
{"version":3,"file":"WindowFunction.js","sources":["../../../../../packages/database/src/builders/WindowFunction.ts"],"sourcesContent":["import { QueryBuilder } from './QueryBuilder'\nimport { Literal } from '../Literal'\nimport { columnExpressionToLiteral } from './utils'\nimport { formatColumnIdentifier } from '../utils'\n\nclass WindowFunction<HasFunction extends boolean> implements QueryBuilder.Orderable<WindowFunction<HasFunction>> {\n\tprivate constructor(\n\t\tprivate readonly windowFunction: Literal | undefined,\n\t\tprivate readonly partitionByExpr: Literal | undefined,\n\t\tprivate readonly orderByColumns: Literal[],\n\t) {}\n\n\tpublic static createEmpty(): WindowFunction<false> {\n\t\treturn new WindowFunction<false>(undefined, undefined, [])\n\t}\n\n\tpublic rowNumber(): WindowFunction<true> {\n\t\treturn new WindowFunction(new Literal('row_number()'), this.partitionByExpr, this.orderByColumns)\n\t}\n\n\tpublic partitionBy(columnName: QueryBuilder.ColumnIdentifier): WindowFunction<HasFunction>\n\tpublic partitionBy(callback: QueryBuilder.ColumnExpression): WindowFunction<HasFunction>\n\tpublic partitionBy(expr: QueryBuilder.ColumnIdentifier | QueryBuilder.ColumnExpression): WindowFunction<HasFunction> {\n\t\tconst raw = columnExpressionToLiteral(expr)\n\t\tif (raw === undefined) {\n\t\t\treturn this\n\t\t}\n\t\treturn new WindowFunction(this.windowFunction, raw, this.orderByColumns)\n\t}\n\n\torderBy(\n\t\texpression: QueryBuilder.ColumnIdentifier | Literal,\n\t\tdirection: 'asc' | 'desc' = 'asc',\n\t): WindowFunction<HasFunction> {\n\t\tconst raw = expression instanceof Literal ? expression : new Literal(formatColumnIdentifier(expression))\n\t\treturn new WindowFunction(this.windowFunction, this.partitionByExpr, [\n\t\t\t...this.orderByColumns,\n\t\t\traw.appendString(direction === 'asc' ? ' asc' : ' desc'),\n\t\t])\n\t}\n\n\tcompile(): Literal {\n\t\tif (this.windowFunction === undefined) {\n\t\t\tthrow new Error()\n\t\t}\n\t\tlet windowDefinition = new Literal('')\n\t\tif (this.partitionByExpr !== undefined) {\n\t\t\twindowDefinition = windowDefinition.appendString('partition by ').append(this.partitionByExpr)\n\t\t}\n\t\tif (this.orderByColumns.length > 0) {\n\t\t\twindowDefinition = windowDefinition.appendString(' order by ').appendAll(this.orderByColumns, ', ')\n\t\t}\n\t\treturn new Literal(this.windowFunction.sql + ' over(' + windowDefinition.sql + ')', [\n\t\t\t...this.windowFunction.parameters,\n\t\t\t...windowDefinition.parameters,\n\t\t])\n\t}\n}\n\nexport { WindowFunction }\n"],"names":[],"mappings":";;;;;;;;;;;;;AAKA,MAAM,eAA2G;AAAA,EACxG,YACU,gBACA,iBACA,gBAChB;AAHgB,SAAA,iBAAA;AACA,SAAA,kBAAA;AACA,SAAA,iBAAA;AAAA,EAAA;AAAA,EAGlB,OAAc,cAAqC;AAClD,WAAO,IAAI,eAAsB,QAAW,QAAW,CAAA,CAAE;AAAA,EAAA;AAAA,EAGnD,YAAkC;AACjC,WAAA,IAAI,eAAe,IAAI,QAAQ,cAAc,GAAG,KAAK,iBAAiB,KAAK,cAAc;AAAA,EAAA;AAAA,EAK1F,YAAY,MAAkG;AAC9G,UAAA,MAAM,0BAA0B,IAAI;AAC1C,QAAI,QAAQ,QAAW;AACf,aAAA;AAAA,IAAA;AAER,WAAO,IAAI,eAAe,KAAK,gBAAgB,KAAK,KAAK,cAAc;AAAA,EAAA;AAAA,EAGxE,QACC,YACA,YAA4B,OACE;AACxB,UAAA,MAAM,sBAAsB,UAAU,aAAa,IAAI,QAAQ,uBAAuB,UAAU,CAAC;AACvG,WAAO,IAAI,eAAe,KAAK,gBAAgB,KAAK,iBAAiB;AAAA,MACpE,GAAG,KAAK;AAAA,MACR,IAAI,aAAa,cAAc,QAAQ,SAAS,OAAO;AAAA,IAAA,CACvD;AAAA,EAAA;AAAA,EAGF,UAAmB;AACd,QAAA,KAAK,mBAAmB,QAAW;AACtC,YAAM,IAAI,MAAM;AAAA,IAAA;AAEb,QAAA,mBAAmB,IAAI,QAAQ,EAAE;AACjC,QAAA,KAAK,oBAAoB,QAAW;AACvC,yBAAmB,iBAAiB,aAAa,eAAe,EAAE,OAAO,KAAK,eAAe;AAAA,IAAA;AAE1F,QAAA,KAAK,eAAe,SAAS,GAAG;AACnC,yBAAmB,iBAAiB,aAAa,YAAY,EAAE,UAAU,KAAK,gBAAgB,IAAI;AAAA,IAAA;AAE5F,WAAA,IAAI,QAAQ,KAAK,eAAe,MAAM,WAAW,iBAAiB,MAAM,KAAK;AAAA,MACnF,GAAG,KAAK,eAAe;AAAA,MACvB,GAAG,iBAAiB;AAAA,IAAA,CACpB;AAAA,EAAA;AAEH;"}
|
|
@@ -1,9 +1,17 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
3
3
|
const Literal = require("../../Literal.cjs");
|
|
4
|
-
const
|
|
4
|
+
const sql = require("../../utils/sql.cjs");
|
|
5
5
|
require("../Compiler.cjs");
|
|
6
6
|
require("../SelectBuilder.cjs");
|
|
7
|
+
require("./With.cjs");
|
|
8
|
+
require("./Where.cjs");
|
|
9
|
+
require("../../client/EventManager.cjs");
|
|
10
|
+
require("@contember/queryable");
|
|
11
|
+
require("../../client/Connection.cjs");
|
|
12
|
+
require("node:timers");
|
|
13
|
+
require("../../utils/Mutex.cjs");
|
|
14
|
+
require("pg");
|
|
7
15
|
class Returning {
|
|
8
16
|
constructor(columns = []) {
|
|
9
17
|
this.columns = columns;
|
|
@@ -12,7 +20,7 @@ class Returning {
|
|
|
12
20
|
if (this.columns.length === 0) {
|
|
13
21
|
return new Literal.Literal("");
|
|
14
22
|
}
|
|
15
|
-
const columns = this.columns.map((it) => it instanceof Literal.Literal ? it : new Literal.Literal(
|
|
23
|
+
const columns = this.columns.map((it) => it instanceof Literal.Literal ? it : new Literal.Literal(sql.formatColumnIdentifier(it)));
|
|
16
24
|
return new Literal.Literal(" returning ").appendAll(columns, ", ");
|
|
17
25
|
}
|
|
18
26
|
parseResponse(result) {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"Returning.cjs","sources":["../../../../../../packages/database/src/builders/internal/Returning.ts"],"sourcesContent":["import { QueryBuilder } from '../'\nimport { Literal } from '../../Literal'\nimport { Connection } from '../../client'\nimport {
|
|
1
|
+
{"version":3,"file":"Returning.cjs","sources":["../../../../../../packages/database/src/builders/internal/Returning.ts"],"sourcesContent":["import { QueryBuilder } from '../'\nimport { Literal } from '../../Literal'\nimport { Connection } from '../../client'\nimport { formatColumnIdentifier } from '../../utils'\n\nclass Returning {\n\tconstructor(private readonly columns: (QueryBuilder.ColumnIdentifier | Literal)[] = []) {}\n\n\tpublic compile(): Literal {\n\t\tif (this.columns.length === 0) {\n\t\t\treturn new Literal('')\n\t\t}\n\t\tconst columns = this.columns.map(it => (it instanceof Literal ? it : new Literal(formatColumnIdentifier(it))))\n\t\treturn new Literal(' returning ').appendAll(columns, ', ')\n\t}\n\n\tpublic parseResponse<ProcessedResult extends number | Returning.Result[]>(\n\t\tresult: Connection.Result,\n\t): ProcessedResult {\n\t\tif (this.columns.length === 0) {\n\t\t\treturn result.rowCount as ProcessedResult\n\t\t}\n\t\treturn result.rows as ProcessedResult\n\t}\n}\n\nnamespace Returning {\n\texport interface Aware {\n\t\treturning(...columns: ReturningColumn[]): any\n\t}\n\texport type Result = Record<string, any>\n\texport type ReturningColumn = QueryBuilder.ColumnIdentifier | Literal\n}\n\nexport { Returning }\n"],"names":["Literal","formatColumnIdentifier"],"mappings":";;;;;;;;;;;;;;AAKA,MAAM,UAAU;AAAA,EACf,YAA6B,UAAuD,IAAI;AAA3D,SAAA,UAAA;AAAA,EAAA;AAAA,EAEtB,UAAmB;AACrB,QAAA,KAAK,QAAQ,WAAW,GAAG;AACvB,aAAA,IAAIA,gBAAQ,EAAE;AAAA,IAAA;AAEtB,UAAM,UAAU,KAAK,QAAQ,IAAI,CAAO,OAAA,cAAcA,QAAU,UAAA,KAAK,IAAIA,QAAAA,QAAQC,IAAAA,uBAAuB,EAAE,CAAC,CAAE;AAC7G,WAAO,IAAID,QAAQ,QAAA,aAAa,EAAE,UAAU,SAAS,IAAI;AAAA,EAAA;AAAA,EAGnD,cACN,QACkB;AACd,QAAA,KAAK,QAAQ,WAAW,GAAG;AAC9B,aAAO,OAAO;AAAA,IAAA;AAEf,WAAO,OAAO;AAAA,EAAA;AAEhB;;"}
|
|
@@ -1,7 +1,15 @@
|
|
|
1
1
|
import { Literal } from "../../Literal.js";
|
|
2
|
-
import {
|
|
2
|
+
import { formatColumnIdentifier } from "../../utils/sql.js";
|
|
3
3
|
import "../Compiler.js";
|
|
4
4
|
import "../SelectBuilder.js";
|
|
5
|
+
import "./With.js";
|
|
6
|
+
import "./Where.js";
|
|
7
|
+
import "../../client/EventManager.js";
|
|
8
|
+
import "@contember/queryable";
|
|
9
|
+
import "../../client/Connection.js";
|
|
10
|
+
import "node:timers";
|
|
11
|
+
import "../../utils/Mutex.js";
|
|
12
|
+
import "pg";
|
|
5
13
|
class Returning {
|
|
6
14
|
constructor(columns = []) {
|
|
7
15
|
this.columns = columns;
|
|
@@ -10,7 +18,7 @@ class Returning {
|
|
|
10
18
|
if (this.columns.length === 0) {
|
|
11
19
|
return new Literal("");
|
|
12
20
|
}
|
|
13
|
-
const columns = this.columns.map((it) => it instanceof Literal ? it : new Literal(
|
|
21
|
+
const columns = this.columns.map((it) => it instanceof Literal ? it : new Literal(formatColumnIdentifier(it)));
|
|
14
22
|
return new Literal(" returning ").appendAll(columns, ", ");
|
|
15
23
|
}
|
|
16
24
|
parseResponse(result) {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"Returning.js","sources":["../../../../../../packages/database/src/builders/internal/Returning.ts"],"sourcesContent":["import { QueryBuilder } from '../'\nimport { Literal } from '../../Literal'\nimport { Connection } from '../../client'\nimport {
|
|
1
|
+
{"version":3,"file":"Returning.js","sources":["../../../../../../packages/database/src/builders/internal/Returning.ts"],"sourcesContent":["import { QueryBuilder } from '../'\nimport { Literal } from '../../Literal'\nimport { Connection } from '../../client'\nimport { formatColumnIdentifier } from '../../utils'\n\nclass Returning {\n\tconstructor(private readonly columns: (QueryBuilder.ColumnIdentifier | Literal)[] = []) {}\n\n\tpublic compile(): Literal {\n\t\tif (this.columns.length === 0) {\n\t\t\treturn new Literal('')\n\t\t}\n\t\tconst columns = this.columns.map(it => (it instanceof Literal ? it : new Literal(formatColumnIdentifier(it))))\n\t\treturn new Literal(' returning ').appendAll(columns, ', ')\n\t}\n\n\tpublic parseResponse<ProcessedResult extends number | Returning.Result[]>(\n\t\tresult: Connection.Result,\n\t): ProcessedResult {\n\t\tif (this.columns.length === 0) {\n\t\t\treturn result.rowCount as ProcessedResult\n\t\t}\n\t\treturn result.rows as ProcessedResult\n\t}\n}\n\nnamespace Returning {\n\texport interface Aware {\n\t\treturning(...columns: ReturningColumn[]): any\n\t}\n\texport type Result = Record<string, any>\n\texport type ReturningColumn = QueryBuilder.ColumnIdentifier | Literal\n}\n\nexport { Returning }\n"],"names":[],"mappings":";;;;;;;;;;;;AAKA,MAAM,UAAU;AAAA,EACf,YAA6B,UAAuD,IAAI;AAA3D,SAAA,UAAA;AAAA,EAAA;AAAA,EAEtB,UAAmB;AACrB,QAAA,KAAK,QAAQ,WAAW,GAAG;AACvB,aAAA,IAAI,QAAQ,EAAE;AAAA,IAAA;AAEtB,UAAM,UAAU,KAAK,QAAQ,IAAI,CAAO,OAAA,cAAc,UAAU,KAAK,IAAI,QAAQ,uBAAuB,EAAE,CAAC,CAAE;AAC7G,WAAO,IAAI,QAAQ,aAAa,EAAE,UAAU,SAAS,IAAI;AAAA,EAAA;AAAA,EAGnD,cACN,QACkB;AACd,QAAA,KAAK,QAAQ,WAAW,GAAG;AAC9B,aAAO,OAAO;AAAA,IAAA;AAEf,WAAO,OAAO;AAAA,EAAA;AAEhB;"}
|
|
@@ -2,7 +2,17 @@
|
|
|
2
2
|
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
3
3
|
const Literal = require("../Literal.cjs");
|
|
4
4
|
const ColumnExpressionFactory = require("./ColumnExpressionFactory.cjs");
|
|
5
|
-
const
|
|
5
|
+
const sql = require("../utils/sql.cjs");
|
|
6
|
+
require("./Compiler.cjs");
|
|
7
|
+
require("./SelectBuilder.cjs");
|
|
8
|
+
require("./internal/With.cjs");
|
|
9
|
+
require("./internal/Where.cjs");
|
|
10
|
+
require("../client/EventManager.cjs");
|
|
11
|
+
require("@contember/queryable");
|
|
12
|
+
require("../client/Connection.cjs");
|
|
13
|
+
require("node:timers");
|
|
14
|
+
require("../utils/Mutex.cjs");
|
|
15
|
+
require("pg");
|
|
6
16
|
function resolveValues(values) {
|
|
7
17
|
return Object.entries(values).map(([key, value]) => {
|
|
8
18
|
const resolved = typeof value === "function" ? value(new ColumnExpressionFactory.ColumnExpressionFactory()) : value;
|
|
@@ -16,11 +26,10 @@ function columnExpressionToLiteral(expr) {
|
|
|
16
26
|
if (typeof expr === "function") {
|
|
17
27
|
return expr(new ColumnExpressionFactory.ColumnExpressionFactory());
|
|
18
28
|
} else if (typeof expr === "string" || Array.isArray(expr)) {
|
|
19
|
-
return new Literal.Literal(
|
|
29
|
+
return new Literal.Literal(sql.formatColumnIdentifier(expr));
|
|
20
30
|
}
|
|
21
31
|
return expr;
|
|
22
32
|
}
|
|
23
|
-
exports.toFqnWrap = formatUtils.toFqnWrap;
|
|
24
33
|
exports.columnExpressionToLiteral = columnExpressionToLiteral;
|
|
25
34
|
exports.resolveValues = resolveValues;
|
|
26
35
|
//# sourceMappingURL=utils.cjs.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"utils.cjs","sources":["../../../../../packages/database/src/builders/utils.ts"],"sourcesContent":["import { Literal } from '../Literal'\nimport { QueryBuilder } from './QueryBuilder'\nimport { ColumnExpressionFactory } from './ColumnExpressionFactory'\nimport {
|
|
1
|
+
{"version":3,"file":"utils.cjs","sources":["../../../../../packages/database/src/builders/utils.ts"],"sourcesContent":["import { Literal } from '../Literal'\nimport { QueryBuilder } from './QueryBuilder'\nimport { ColumnExpressionFactory } from './ColumnExpressionFactory'\nimport { formatColumnIdentifier } from '../utils'\n\nexport function resolveValues(values: QueryBuilder.Values): QueryBuilder.ResolvedValues {\n\treturn Object.entries(values)\n\t\t.map(([key, value]) => {\n\t\t\tconst resolved = typeof value === 'function' ? value(new ColumnExpressionFactory()) : value\n\t\t\treturn {\n\t\t\t\tcolumnName: key,\n\t\t\t\tvalue: resolved === undefined || resolved instanceof Literal ? resolved : new Literal('?', [resolved]),\n\t\t\t}\n\t\t})\n\t\t.filter((it): it is QueryBuilder.ResolvedValue => it.value !== undefined)\n}\n\nexport function columnExpressionToLiteral(\n\texpr: QueryBuilder.ColumnIdentifier | QueryBuilder.ColumnExpression,\n): Literal | undefined {\n\tif (typeof expr === 'function') {\n\t\treturn expr(new ColumnExpressionFactory())\n\t} else if (typeof expr === 'string' || Array.isArray(expr)) {\n\t\treturn new Literal(formatColumnIdentifier(expr))\n\t}\n\treturn expr\n}\n"],"names":["ColumnExpressionFactory","Literal","formatColumnIdentifier"],"mappings":";;;;;;;;;;;;;;;AAKO,SAAS,cAAc,QAA0D;AAChF,SAAA,OAAO,QAAQ,MAAM,EAC1B,IAAI,CAAC,CAAC,KAAK,KAAK,MAAM;AAChB,UAAA,WAAW,OAAO,UAAU,aAAa,MAAM,IAAIA,wBAAAA,wBAAyB,CAAA,IAAI;AAC/E,WAAA;AAAA,MACN,YAAY;AAAA,MACZ,OAAO,aAAa,UAAa,oBAAoBC,QAAAA,UAAU,WAAW,IAAIA,QAAQ,QAAA,KAAK,CAAC,QAAQ,CAAC;AAAA,IACtG;AAAA,EAAA,CACA,EACA,OAAO,CAAC,OAAyC,GAAG,UAAU,MAAS;AAC1E;AAEO,SAAS,0BACf,MACsB;AAClB,MAAA,OAAO,SAAS,YAAY;AACxB,WAAA,KAAK,IAAID,wBAAAA,yBAAyB;AAAA,EAAA,WAC/B,OAAO,SAAS,YAAY,MAAM,QAAQ,IAAI,GAAG;AAC3D,WAAO,IAAIC,QAAA,QAAQC,2BAAuB,IAAI,CAAC;AAAA,EAAA;AAEzC,SAAA;AACR;;;"}
|
|
@@ -1,6 +1,16 @@
|
|
|
1
1
|
import { Literal } from "../Literal.js";
|
|
2
2
|
import { ColumnExpressionFactory } from "./ColumnExpressionFactory.js";
|
|
3
|
-
import {
|
|
3
|
+
import { formatColumnIdentifier } from "../utils/sql.js";
|
|
4
|
+
import "./Compiler.js";
|
|
5
|
+
import "./SelectBuilder.js";
|
|
6
|
+
import "./internal/With.js";
|
|
7
|
+
import "./internal/Where.js";
|
|
8
|
+
import "../client/EventManager.js";
|
|
9
|
+
import "@contember/queryable";
|
|
10
|
+
import "../client/Connection.js";
|
|
11
|
+
import "node:timers";
|
|
12
|
+
import "../utils/Mutex.js";
|
|
13
|
+
import "pg";
|
|
4
14
|
function resolveValues(values) {
|
|
5
15
|
return Object.entries(values).map(([key, value]) => {
|
|
6
16
|
const resolved = typeof value === "function" ? value(new ColumnExpressionFactory()) : value;
|
|
@@ -14,13 +24,12 @@ function columnExpressionToLiteral(expr) {
|
|
|
14
24
|
if (typeof expr === "function") {
|
|
15
25
|
return expr(new ColumnExpressionFactory());
|
|
16
26
|
} else if (typeof expr === "string" || Array.isArray(expr)) {
|
|
17
|
-
return new Literal(
|
|
27
|
+
return new Literal(formatColumnIdentifier(expr));
|
|
18
28
|
}
|
|
19
29
|
return expr;
|
|
20
30
|
}
|
|
21
31
|
export {
|
|
22
32
|
columnExpressionToLiteral,
|
|
23
|
-
resolveValues
|
|
24
|
-
toFqnWrap
|
|
33
|
+
resolveValues
|
|
25
34
|
};
|
|
26
35
|
//# sourceMappingURL=utils.js.map
|