kysely 0.19.11 → 0.20.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.
Files changed (116) hide show
  1. package/README.md +1 -1
  2. package/dist/cjs/dialect/mysql/mysql-introspector.js +2 -0
  3. package/dist/cjs/dialect/mysql/mysql-query-compiler.d.ts +5 -0
  4. package/dist/cjs/dialect/mysql/mysql-query-compiler.js +16 -0
  5. package/dist/cjs/dialect/postgres/postgres-introspector.js +17 -9
  6. package/dist/cjs/dialect/postgres/postgres-query-compiler.d.ts +1 -3
  7. package/dist/cjs/dialect/postgres/postgres-query-compiler.js +3 -8
  8. package/dist/cjs/dialect/sqlite/sqlite-introspector.js +2 -0
  9. package/dist/cjs/dialect/sqlite/sqlite-query-compiler.d.ts +3 -0
  10. package/dist/cjs/dialect/sqlite/sqlite-query-compiler.js +10 -0
  11. package/dist/cjs/index.d.ts +3 -0
  12. package/dist/cjs/index.js +3 -0
  13. package/dist/cjs/operation-node/create-table-node.d.ts +9 -2
  14. package/dist/cjs/operation-node/create-table-node.js +18 -1
  15. package/dist/cjs/operation-node/delete-query-node.d.ts +3 -0
  16. package/dist/cjs/operation-node/delete-query-node.js +6 -0
  17. package/dist/cjs/operation-node/explain-node.d.ts +16 -0
  18. package/dist/cjs/operation-node/explain-node.js +19 -0
  19. package/dist/cjs/operation-node/foreign-key-constraint-node.d.ts +2 -2
  20. package/dist/cjs/operation-node/insert-query-node.d.ts +2 -0
  21. package/dist/cjs/operation-node/operation-node-transformer.d.ts +2 -0
  22. package/dist/cjs/operation-node/operation-node-transformer.js +14 -0
  23. package/dist/cjs/operation-node/operation-node-visitor.d.ts +2 -0
  24. package/dist/cjs/operation-node/operation-node-visitor.js +1 -0
  25. package/dist/cjs/operation-node/operation-node.d.ts +1 -1
  26. package/dist/cjs/operation-node/reference-node.d.ts +1 -1
  27. package/dist/cjs/operation-node/reference-node.js +1 -2
  28. package/dist/cjs/operation-node/references-node.d.ts +3 -1
  29. package/dist/cjs/operation-node/references-node.js +8 -1
  30. package/dist/cjs/operation-node/select-query-node.d.ts +3 -0
  31. package/dist/cjs/operation-node/select-query-node.js +6 -0
  32. package/dist/cjs/operation-node/selection-node.d.ts +2 -1
  33. package/dist/cjs/operation-node/update-query-node.d.ts +3 -0
  34. package/dist/cjs/operation-node/update-query-node.js +6 -0
  35. package/dist/cjs/parser/on-commit-action-parse.d.ts +2 -0
  36. package/dist/cjs/parser/on-commit-action-parse.js +11 -0
  37. package/dist/cjs/parser/on-modify-action-parser.d.ts +2 -0
  38. package/dist/cjs/parser/on-modify-action-parser.js +11 -0
  39. package/dist/cjs/parser/select-parser.js +2 -1
  40. package/dist/cjs/query-builder/delete-query-builder.d.ts +19 -1
  41. package/dist/cjs/query-builder/delete-query-builder.js +24 -0
  42. package/dist/cjs/query-builder/function-builder.d.ts +3 -3
  43. package/dist/cjs/query-builder/insert-query-builder.d.ts +20 -2
  44. package/dist/cjs/query-builder/insert-query-builder.js +26 -0
  45. package/dist/cjs/query-builder/select-query-builder.d.ts +38 -1
  46. package/dist/cjs/query-builder/select-query-builder.js +45 -2
  47. package/dist/cjs/query-builder/update-query-builder.d.ts +20 -1
  48. package/dist/cjs/query-builder/update-query-builder.js +25 -0
  49. package/dist/cjs/query-compiler/default-query-compiler.d.ts +7 -0
  50. package/dist/cjs/query-compiler/default-query-compiler.js +74 -1
  51. package/dist/cjs/raw-builder/sql.d.ts +96 -96
  52. package/dist/cjs/raw-builder/sql.js +0 -96
  53. package/dist/cjs/schema/column-definition-builder.js +3 -2
  54. package/dist/cjs/schema/create-table-builder.d.ts +54 -0
  55. package/dist/cjs/schema/create-table-builder.js +66 -1
  56. package/dist/cjs/schema/foreign-key-constraint-builder.js +3 -2
  57. package/dist/cjs/util/explainable.d.ts +5 -0
  58. package/dist/cjs/util/explainable.js +2 -0
  59. package/dist/esm/dialect/mysql/mysql-introspector.js +2 -0
  60. package/dist/esm/dialect/mysql/mysql-query-compiler.d.ts +5 -0
  61. package/dist/esm/dialect/mysql/mysql-query-compiler.js +16 -0
  62. package/dist/esm/dialect/postgres/postgres-introspector.js +17 -9
  63. package/dist/esm/dialect/postgres/postgres-query-compiler.d.ts +1 -3
  64. package/dist/esm/dialect/postgres/postgres-query-compiler.js +3 -8
  65. package/dist/esm/dialect/sqlite/sqlite-introspector.js +2 -0
  66. package/dist/esm/dialect/sqlite/sqlite-query-compiler.d.ts +3 -0
  67. package/dist/esm/dialect/sqlite/sqlite-query-compiler.js +10 -0
  68. package/dist/esm/index.d.ts +3 -0
  69. package/dist/esm/index.js +3 -0
  70. package/dist/esm/operation-node/create-table-node.d.ts +9 -2
  71. package/dist/esm/operation-node/create-table-node.js +17 -0
  72. package/dist/esm/operation-node/delete-query-node.d.ts +3 -0
  73. package/dist/esm/operation-node/delete-query-node.js +6 -0
  74. package/dist/esm/operation-node/explain-node.d.ts +16 -0
  75. package/dist/esm/operation-node/explain-node.js +17 -0
  76. package/dist/esm/operation-node/foreign-key-constraint-node.d.ts +2 -2
  77. package/dist/esm/operation-node/insert-query-node.d.ts +2 -0
  78. package/dist/esm/operation-node/operation-node-transformer.d.ts +2 -0
  79. package/dist/esm/operation-node/operation-node-transformer.js +14 -0
  80. package/dist/esm/operation-node/operation-node-visitor.d.ts +2 -0
  81. package/dist/esm/operation-node/operation-node-visitor.js +1 -0
  82. package/dist/esm/operation-node/operation-node.d.ts +1 -1
  83. package/dist/esm/operation-node/reference-node.d.ts +1 -1
  84. package/dist/esm/operation-node/reference-node.js +1 -2
  85. package/dist/esm/operation-node/references-node.d.ts +3 -1
  86. package/dist/esm/operation-node/references-node.js +7 -0
  87. package/dist/esm/operation-node/select-query-node.d.ts +3 -0
  88. package/dist/esm/operation-node/select-query-node.js +6 -0
  89. package/dist/esm/operation-node/selection-node.d.ts +2 -1
  90. package/dist/esm/operation-node/update-query-node.d.ts +3 -0
  91. package/dist/esm/operation-node/update-query-node.js +6 -0
  92. package/dist/esm/parser/on-commit-action-parse.d.ts +2 -0
  93. package/dist/esm/parser/on-commit-action-parse.js +8 -0
  94. package/dist/esm/parser/on-modify-action-parser.d.ts +2 -0
  95. package/dist/esm/parser/on-modify-action-parser.js +8 -0
  96. package/dist/esm/parser/select-parser.js +2 -1
  97. package/dist/esm/query-builder/delete-query-builder.d.ts +19 -1
  98. package/dist/esm/query-builder/delete-query-builder.js +24 -0
  99. package/dist/esm/query-builder/function-builder.d.ts +3 -3
  100. package/dist/esm/query-builder/insert-query-builder.d.ts +20 -2
  101. package/dist/esm/query-builder/insert-query-builder.js +26 -0
  102. package/dist/esm/query-builder/select-query-builder.d.ts +38 -1
  103. package/dist/esm/query-builder/select-query-builder.js +45 -2
  104. package/dist/esm/query-builder/update-query-builder.d.ts +20 -1
  105. package/dist/esm/query-builder/update-query-builder.js +25 -0
  106. package/dist/esm/query-compiler/default-query-compiler.d.ts +7 -0
  107. package/dist/esm/query-compiler/default-query-compiler.js +74 -1
  108. package/dist/esm/raw-builder/sql.d.ts +96 -96
  109. package/dist/esm/raw-builder/sql.js +0 -96
  110. package/dist/esm/schema/column-definition-builder.js +3 -2
  111. package/dist/esm/schema/create-table-builder.d.ts +54 -0
  112. package/dist/esm/schema/create-table-builder.js +66 -1
  113. package/dist/esm/schema/foreign-key-constraint-builder.js +3 -2
  114. package/dist/esm/util/explainable.d.ts +5 -0
  115. package/dist/esm/util/explainable.js +2 -0
  116. package/package.json +3 -1
@@ -3,7 +3,7 @@ import { CompiledQuery } from '../query-compiler/compiled-query.js';
3
3
  import { SelectExpression } from '../parser/select-parser.js';
4
4
  import { InsertObject } from '../parser/insert-values-parser.js';
5
5
  import { InsertQueryNode } from '../operation-node/insert-query-node.js';
6
- import { MergePartial, SingleResultType } from '../util/type-utils.js';
6
+ import { AnyRawBuilder, MergePartial, SingleResultType } from '../util/type-utils.js';
7
7
  import { MutationObject } from '../parser/update-set-parser.js';
8
8
  import { Compilable } from '../util/compilable.js';
9
9
  import { QueryExecutor } from '../query-executor/query-executor.js';
@@ -16,7 +16,8 @@ import { ComplexExpression } from '../parser/complex-expression-parser.js';
16
16
  import { ReturningInterface } from './returning-interface.js';
17
17
  import { OnConflictBuilder, OnConflictDoNothingBuilder, OnConflictUpdateBuilder } from './on-conflict-builder.js';
18
18
  import { Selectable } from '../util/column-type.js';
19
- export declare class InsertQueryBuilder<DB, TB extends keyof DB, O> implements ReturningInterface<DB, TB, O>, OperationNodeSource, Compilable {
19
+ import { Explainable, ExplainFormat } from '../util/explainable.js';
20
+ export declare class InsertQueryBuilder<DB, TB extends keyof DB, O> implements ReturningInterface<DB, TB, O>, OperationNodeSource, Compilable, Explainable {
20
21
  #private;
21
22
  constructor(props: InsertQueryBuilderProps);
22
23
  /**
@@ -541,6 +542,23 @@ export declare class InsertQueryBuilder<DB, TB extends keyof DB, O> implements R
541
542
  * error.
542
543
  */
543
544
  executeTakeFirstOrThrow(errorConstructor?: NoResultErrorConstructor): Promise<O>;
545
+ /**
546
+ * Executes query with `explain` statement before `insert` keyword.
547
+ *
548
+ * ```ts
549
+ * const explained = await db
550
+ * .insertInto('person')
551
+ * .values(values)
552
+ * .explain('json')
553
+ * ```
554
+ *
555
+ * The generated SQL (MySQL):
556
+ *
557
+ * ```sql
558
+ * explain format=json insert into `person` (`id`, `first_name`, `last_name`) values (?, ?, ?) (?, ?, ?)
559
+ * ```
560
+ */
561
+ explain<ER extends Record<string, any> = Record<string, any>>(format?: ExplainFormat, options?: AnyRawBuilder): Promise<ER[]>;
544
562
  }
545
563
  export interface InsertQueryBuilderProps {
546
564
  readonly queryId: QueryId;
@@ -13,6 +13,7 @@ import { parseComplexExpression, } from '../parser/complex-expression-parser.js'
13
13
  import { ColumnNode } from '../operation-node/column-node.js';
14
14
  import { OnConflictBuilder, } from './on-conflict-builder.js';
15
15
  import { OnConflictNode } from '../operation-node/on-conflict-node.js';
16
+ import { ExplainNode } from '../operation-node/explain-node.js';
16
17
  export class InsertQueryBuilder {
17
18
  #props;
18
19
  constructor(props) {
@@ -444,5 +445,30 @@ export class InsertQueryBuilder {
444
445
  }
445
446
  return result;
446
447
  }
448
+ /**
449
+ * Executes query with `explain` statement before `insert` keyword.
450
+ *
451
+ * ```ts
452
+ * const explained = await db
453
+ * .insertInto('person')
454
+ * .values(values)
455
+ * .explain('json')
456
+ * ```
457
+ *
458
+ * The generated SQL (MySQL):
459
+ *
460
+ * ```sql
461
+ * explain format=json insert into `person` (`id`, `first_name`, `last_name`) values (?, ?, ?) (?, ?, ?)
462
+ * ```
463
+ */
464
+ async explain(format, options) {
465
+ const builder = new InsertQueryBuilder({
466
+ ...this.#props,
467
+ queryNode: InsertQueryNode.cloneWith(this.#props.queryNode, {
468
+ explain: ExplainNode.create(format, options),
469
+ }),
470
+ });
471
+ return await builder.execute();
472
+ }
447
473
  }
448
474
  preventAwait(InsertQueryBuilder, "don't await InsertQueryBuilder instances directly. To execute the query you need to call `execute` or `executeTakeFirst`.");
@@ -18,7 +18,8 @@ import { WhereInterface } from './where-interface.js';
18
18
  import { NoResultErrorConstructor } from './no-result-error.js';
19
19
  import { HavingInterface } from './having-interface.js';
20
20
  import { AliasedRawBuilder } from '../raw-builder/raw-builder.js';
21
- export declare class SelectQueryBuilder<DB, TB extends keyof DB, O> implements WhereInterface<DB, TB>, HavingInterface<DB, TB>, OperationNodeSource, Compilable {
21
+ import { Explainable, ExplainFormat } from '../util/explainable.js';
22
+ export declare class SelectQueryBuilder<DB, TB extends keyof DB, O> implements WhereInterface<DB, TB>, HavingInterface<DB, TB>, OperationNodeSource, Compilable, Explainable {
22
23
  #private;
23
24
  constructor(props: SelectQueryBuilderProps);
24
25
  /**
@@ -1370,6 +1371,42 @@ export declare class SelectQueryBuilder<DB, TB extends keyof DB, O> implements W
1370
1371
  * ```
1371
1372
  */
1372
1373
  stream(chunkSize?: number): AsyncIterableIterator<O>;
1374
+ /**
1375
+ * Executes query with `explain` statement before `select` keyword.
1376
+ *
1377
+ * ```ts
1378
+ * const explained = await db
1379
+ * .selectFrom('person')
1380
+ * .where('gender', '=', 'female')
1381
+ * .selectAll()
1382
+ * .explain('json')
1383
+ * ```
1384
+ *
1385
+ * The generated SQL (MySQL):
1386
+ *
1387
+ * ```sql
1388
+ * explain format=json select * from `person` where `gender` = ?
1389
+ * ```
1390
+ *
1391
+ * You can also execute `explain analyze` statements.
1392
+ *
1393
+ * ```ts
1394
+ * import { sql } from 'kysely'
1395
+ *
1396
+ * const explained = await db
1397
+ * .selectFrom('person')
1398
+ * .where('gender', '=', 'female')
1399
+ * .selectAll()
1400
+ * .explain('json', sql`analyze`)
1401
+ * ```
1402
+ *
1403
+ * The generated SQL (PostgreSQL):
1404
+ *
1405
+ * ```sql
1406
+ * explain (analyze, format json) select * from "person" where "gender" = $1
1407
+ * ```
1408
+ */
1409
+ explain<ER extends Record<string, any> = Record<string, any>>(format?: ExplainFormat, options?: AnyRawBuilder): Promise<ER[]>;
1373
1410
  }
1374
1411
  export interface SelectQueryBuilderProps {
1375
1412
  readonly queryId: QueryId;
@@ -15,6 +15,7 @@ import { parseGroupBy } from '../parser/group-by-parser.js';
15
15
  import { parseUnion } from '../parser/union-parser.js';
16
16
  import { NoResultError } from './no-result-error.js';
17
17
  import { IdentifierNode } from '../operation-node/identifier-node.js';
18
+ import { ExplainNode } from '../operation-node/explain-node.js';
18
19
  export class SelectQueryBuilder {
19
20
  #props;
20
21
  constructor(props) {
@@ -627,8 +628,8 @@ export class SelectQueryBuilder {
627
628
  * Also see the {@link executeTakeFirst} and {@link executeTakeFirstOrThrow} methods.
628
629
  */
629
630
  async execute() {
630
- const compildQuery = this.compile();
631
- const result = await this.#props.executor.executeQuery(compildQuery, this.#props.queryId);
631
+ const compiledQuery = this.compile();
632
+ const result = await this.#props.executor.executeQuery(compiledQuery, this.#props.queryId);
632
633
  return result.rows;
633
634
  }
634
635
  /**
@@ -687,6 +688,48 @@ export class SelectQueryBuilder {
687
688
  yield* item.rows;
688
689
  }
689
690
  }
691
+ /**
692
+ * Executes query with `explain` statement before `select` keyword.
693
+ *
694
+ * ```ts
695
+ * const explained = await db
696
+ * .selectFrom('person')
697
+ * .where('gender', '=', 'female')
698
+ * .selectAll()
699
+ * .explain('json')
700
+ * ```
701
+ *
702
+ * The generated SQL (MySQL):
703
+ *
704
+ * ```sql
705
+ * explain format=json select * from `person` where `gender` = ?
706
+ * ```
707
+ *
708
+ * You can also execute `explain analyze` statements.
709
+ *
710
+ * ```ts
711
+ * import { sql } from 'kysely'
712
+ *
713
+ * const explained = await db
714
+ * .selectFrom('person')
715
+ * .where('gender', '=', 'female')
716
+ * .selectAll()
717
+ * .explain('json', sql`analyze`)
718
+ * ```
719
+ *
720
+ * The generated SQL (PostgreSQL):
721
+ *
722
+ * ```sql
723
+ * explain (analyze, format json) select * from "person" where "gender" = $1
724
+ * ```
725
+ */
726
+ async explain(format, options) {
727
+ const builder = new SelectQueryBuilder({
728
+ ...this.#props,
729
+ queryNode: SelectQueryNode.cloneWithExplain(this.#props.queryNode, ExplainNode.create(format, options)),
730
+ });
731
+ return await builder.execute();
732
+ }
690
733
  }
691
734
  preventAwait(SelectQueryBuilder, "don't await SelectQueryBuilder instances directly. To execute the query you need to call `execute` or `executeTakeFirst`.");
692
735
  /**
@@ -20,7 +20,8 @@ import { NoResultErrorConstructor } from './no-result-error.js';
20
20
  import { Selectable } from '../util/column-type.js';
21
21
  import { AliasedQueryBuilder } from './select-query-builder.js';
22
22
  import { AliasedRawBuilder } from '../raw-builder/raw-builder.js';
23
- export declare class UpdateQueryBuilder<DB, UT extends keyof DB, TB extends keyof DB, O> implements WhereInterface<DB, TB>, ReturningInterface<DB, TB, O>, OperationNodeSource, Compilable {
23
+ import { Explainable, ExplainFormat } from '../util/explainable.js';
24
+ export declare class UpdateQueryBuilder<DB, UT extends keyof DB, TB extends keyof DB, O> implements WhereInterface<DB, TB>, ReturningInterface<DB, TB, O>, OperationNodeSource, Compilable, Explainable {
24
25
  #private;
25
26
  constructor(props: UpdateQueryBuilderProps);
26
27
  /**
@@ -809,6 +810,24 @@ export declare class UpdateQueryBuilder<DB, UT extends keyof DB, TB extends keyo
809
810
  * error.
810
811
  */
811
812
  executeTakeFirstOrThrow(errorConstructor?: NoResultErrorConstructor): Promise<O>;
813
+ /**
814
+ * Executes query with `explain` statement before `update` keyword.
815
+ *
816
+ * ```ts
817
+ * const explained = await db
818
+ * .updateTable('person')
819
+ * .set(updates)
820
+ * .where('id', '=', 123)
821
+ * .explain('json')
822
+ * ```
823
+ *
824
+ * The generated SQL (MySQL):
825
+ *
826
+ * ```sql
827
+ * explain format=json update `person` set `first_name` = ?, `last_name` = ? where `id` = ?
828
+ * ```
829
+ */
830
+ explain<ER extends Record<string, any> = Record<string, any>>(format?: ExplainFormat, options?: AnyRawBuilder): Promise<ER[]>;
812
831
  }
813
832
  export interface UpdateQueryBuilderProps {
814
833
  readonly queryId: QueryId;
@@ -10,6 +10,7 @@ import { preventAwait } from '../util/prevent-await.js';
10
10
  import { freeze } from '../util/object-utils.js';
11
11
  import { UpdateResult } from './update-result.js';
12
12
  import { NoResultError } from './no-result-error.js';
13
+ import { ExplainNode } from '../operation-node/explain-node.js';
13
14
  export class UpdateQueryBuilder {
14
15
  #props;
15
16
  constructor(props) {
@@ -330,5 +331,29 @@ export class UpdateQueryBuilder {
330
331
  }
331
332
  return result;
332
333
  }
334
+ /**
335
+ * Executes query with `explain` statement before `update` keyword.
336
+ *
337
+ * ```ts
338
+ * const explained = await db
339
+ * .updateTable('person')
340
+ * .set(updates)
341
+ * .where('id', '=', 123)
342
+ * .explain('json')
343
+ * ```
344
+ *
345
+ * The generated SQL (MySQL):
346
+ *
347
+ * ```sql
348
+ * explain format=json update `person` set `first_name` = ?, `last_name` = ? where `id` = ?
349
+ * ```
350
+ */
351
+ async explain(format, options) {
352
+ const builder = new UpdateQueryBuilder({
353
+ ...this.#props,
354
+ queryNode: UpdateQueryNode.cloneWithExplain(this.#props.queryNode, ExplainNode.create(format, options)),
355
+ });
356
+ return await builder.execute();
357
+ }
333
358
  }
334
359
  preventAwait(UpdateQueryBuilder, "don't await UpdateQueryBuilder instances directly. To execute the query you need to call `execute` or `executeTakeFirst`.");
@@ -71,6 +71,7 @@ import { CommonTableExpressionNameNode } from '../operation-node/common-table-ex
71
71
  import { SelectModifierNode } from '../operation-node/select-modifier-node.js';
72
72
  import { CreateTypeNode } from '../operation-node/create-type-node.js';
73
73
  import { DropTypeNode } from '../operation-node/drop-type-node.js';
74
+ import { ExplainNode } from '../operation-node/explain-node.js';
74
75
  export declare class DefaultQueryCompiler extends OperationNodeVisitor implements QueryCompiler {
75
76
  #private;
76
77
  protected get numParameters(): number;
@@ -149,11 +150,17 @@ export declare class DefaultQueryCompiler extends OperationNodeVisitor implement
149
150
  protected visitSelectModifier(node: SelectModifierNode): void;
150
151
  protected visitCreateType(node: CreateTypeNode): void;
151
152
  protected visitDropType(node: DropTypeNode): void;
153
+ protected visitExplain(node: ExplainNode): void;
152
154
  protected append(str: string): void;
153
155
  protected appendValue(parameter: unknown): void;
154
156
  protected getLeftIdentifierWrapper(): string;
155
157
  protected getRightIdentifierWrapper(): string;
156
158
  protected getCurrentParameterPlaceholder(): string;
159
+ protected getLeftExplainOptionsWrapper(): string;
160
+ protected getExplainOptionAssignment(): string;
161
+ protected getExplainOptionsDelimiter(): string;
162
+ protected getRightExplainOptionsWrapper(): string;
163
+ protected sanitizeIdentifier(identifier: string): string;
157
164
  protected addParameter(parameter: unknown): void;
158
165
  protected appendImmediateValue(value: unknown): void;
159
166
  }
@@ -30,6 +30,10 @@ export class DefaultQueryCompiler extends OperationNodeVisitor {
30
30
  !InsertQueryNode.is(this.parentNode) &&
31
31
  !CreateViewNode.is(this.parentNode) &&
32
32
  !UnionNode.is(this.parentNode);
33
+ if (this.parentNode === undefined && node.explain) {
34
+ this.visitNode(node.explain);
35
+ this.append(' ');
36
+ }
33
37
  if (wrapInParens) {
34
38
  this.append('(');
35
39
  }
@@ -125,6 +129,10 @@ export class DefaultQueryCompiler extends OperationNodeVisitor {
125
129
  }
126
130
  visitInsertQuery(node) {
127
131
  const isSubQuery = this.nodeStack.find(QueryNode.is) !== node;
132
+ if (!isSubQuery && node.explain) {
133
+ this.visitNode(node.explain);
134
+ this.append(' ');
135
+ }
128
136
  if (isSubQuery) {
129
137
  this.append('(');
130
138
  }
@@ -169,6 +177,10 @@ export class DefaultQueryCompiler extends OperationNodeVisitor {
169
177
  }
170
178
  visitDeleteQuery(node) {
171
179
  const isSubQuery = this.nodeStack.find(QueryNode.is) !== node;
180
+ if (!isSubQuery && node.explain) {
181
+ this.visitNode(node.explain);
182
+ this.append(' ');
183
+ }
172
184
  if (isSubQuery) {
173
185
  this.append('(');
174
186
  }
@@ -225,7 +237,10 @@ export class DefaultQueryCompiler extends OperationNodeVisitor {
225
237
  this.append(this.getRightIdentifierWrapper());
226
238
  }
227
239
  compileUnwrappedIdentifier(node) {
228
- this.append(node.identifier);
240
+ if (!isString(node.identifier)) {
241
+ throw new Error('a non-string identifier was passed to compileUnwrappedIdentifier.');
242
+ }
243
+ this.append(this.sanitizeIdentifier(node.identifier));
229
244
  }
230
245
  visitFilter(node) {
231
246
  if (node.left) {
@@ -309,6 +324,10 @@ export class DefaultQueryCompiler extends OperationNodeVisitor {
309
324
  }
310
325
  visitCreateTable(node) {
311
326
  this.append('create ');
327
+ if (node.frontModifiers) {
328
+ this.compileList(node.frontModifiers, ' ');
329
+ this.append(' ');
330
+ }
312
331
  if (node.temporary) {
313
332
  this.append('temporary ');
314
333
  }
@@ -324,6 +343,10 @@ export class DefaultQueryCompiler extends OperationNodeVisitor {
324
343
  this.append(' on commit ');
325
344
  this.append(node.onCommit);
326
345
  }
346
+ if (node.endModifiers) {
347
+ this.append(' ');
348
+ this.compileList(node.endModifiers, ' ');
349
+ }
327
350
  }
328
351
  visitColumnDefinition(node) {
329
352
  this.visitNode(node.column);
@@ -410,6 +433,10 @@ export class DefaultQueryCompiler extends OperationNodeVisitor {
410
433
  }
411
434
  visitUpdateQuery(node) {
412
435
  const isSubQuery = this.nodeStack.find(QueryNode.is) !== node;
436
+ if (!isSubQuery && node.explain) {
437
+ this.visitNode(node.explain);
438
+ this.append(' ');
439
+ }
413
440
  if (isSubQuery) {
414
441
  this.append('(');
415
442
  }
@@ -804,6 +831,25 @@ export class DefaultQueryCompiler extends OperationNodeVisitor {
804
831
  }
805
832
  this.visitNode(node.name);
806
833
  }
834
+ visitExplain(node) {
835
+ this.append('explain');
836
+ if (node.options || node.format) {
837
+ this.append(' ');
838
+ this.append(this.getLeftExplainOptionsWrapper());
839
+ if (node.options) {
840
+ this.visitNode(node.options);
841
+ if (node.format) {
842
+ this.append(this.getExplainOptionsDelimiter());
843
+ }
844
+ }
845
+ if (node.format) {
846
+ this.append('format');
847
+ this.append(this.getExplainOptionAssignment());
848
+ this.append(node.format);
849
+ }
850
+ this.append(this.getRightExplainOptionsWrapper());
851
+ }
852
+ }
807
853
  append(str) {
808
854
  this.#sql += str;
809
855
  }
@@ -820,6 +866,33 @@ export class DefaultQueryCompiler extends OperationNodeVisitor {
820
866
  getCurrentParameterPlaceholder() {
821
867
  return '$' + this.numParameters;
822
868
  }
869
+ getLeftExplainOptionsWrapper() {
870
+ return '(';
871
+ }
872
+ getExplainOptionAssignment() {
873
+ return ' ';
874
+ }
875
+ getExplainOptionsDelimiter() {
876
+ return ', ';
877
+ }
878
+ getRightExplainOptionsWrapper() {
879
+ return ')';
880
+ }
881
+ sanitizeIdentifier(identifier) {
882
+ const leftWrap = this.getLeftIdentifierWrapper();
883
+ const rightWrap = this.getRightIdentifierWrapper();
884
+ let sanitized = '';
885
+ for (const c of identifier) {
886
+ sanitized += c;
887
+ if (c === leftWrap) {
888
+ sanitized += leftWrap;
889
+ }
890
+ else if (c === rightWrap) {
891
+ sanitized += rightWrap;
892
+ }
893
+ }
894
+ return sanitized;
895
+ }
823
896
  addParameter(parameter) {
824
897
  this.#parameters.push(parameter);
825
898
  }
@@ -1,5 +1,101 @@
1
1
  import { RawBuilder } from './raw-builder.js';
2
2
  export interface Sql {
3
+ /**
4
+ * Template tag for creating raw SQL snippets and queries.
5
+ *
6
+ * ```ts
7
+ * import { sql } from 'kysely'
8
+ *
9
+ * const id = 123
10
+ *
11
+ * const snippet = sql<Person[]>`select * from person where id = ${id}`
12
+ * ```
13
+ *
14
+ * Substitutions (the things inside `${}`) are automatically passed to the database
15
+ * as parameters and are NOT interpolated to the SQL string. There's no need to worry
16
+ * about SQL injection vulnerabilities.
17
+ *
18
+ * If you need your substitutions to be interpreted as identifiers, value literals or
19
+ * lists of things, see the {@link Sql.ref}, {@link Sql.table}, {@link Sql.id},
20
+ * {@link Sql.literal}, {@link Sql.raw} and {@link Sql.join} functions.
21
+ *
22
+ * You can pass sql snippets returned by the `sql` tag pretty much anywhere. Whenever
23
+ * something can't be done using the Kysely API, you should be able to drop down to
24
+ * raw SQL using the `sql` tag. Here's an example query that uses raw sql in a bunch
25
+ * of methods:
26
+ *
27
+ * ```ts
28
+ * const persons = await db
29
+ * .selectFrom('person')
30
+ * .select(
31
+ * // If you use `sql` in a select statement, remember to call the `as`
32
+ * // method to give it an alias. Otherwise Kysely is not able to add
33
+ * // the column to the result type.
34
+ * sql<string>`concat(first_name, ' ', last_name)`.as('full_name')
35
+ * )
36
+ * .where(sql`birthdate between ${date1} and ${date2}`)
37
+ * // Here we assume we have list of nicknames for the person
38
+ * // (a list of strings) and we use the PostgreSQL `@>` operator
39
+ * // to test if all of them are valid nicknames for the user.
40
+ * .where('nicknames', '@>', sql`ARRAY[${sql.join(nicknames)}]`)
41
+ * .orderBy(sql`concat(first_name, ' ', last_name)`)
42
+ * .execute()
43
+ * ```
44
+ *
45
+ * The generated SQL (PostgreSQL):
46
+ *
47
+ * ```sql
48
+ * select concat(first_name, ' ', last_name) as "full_name"
49
+ * from "person"
50
+ * where birthdate between $1 and $2
51
+ * and "nicknames" @> ARRAY[$3, $4, $5, $6, $7, $8, $9, $10]
52
+ * order by concat(first_name, ' ', last_name)
53
+ * ```
54
+ *
55
+ * SQL snippets can be executed by calling the `execute` method and passing a `Kysely`
56
+ * instance as the only argument:
57
+ *
58
+ * ```ts
59
+ * const result = await sql<Person[]>`select * from person`.execute(db)
60
+ * ```
61
+ *
62
+ * You can merge other `sql` expressions and queries using substitutions:
63
+ *
64
+ * ```ts
65
+ * const petName = db.selectFrom('pet').select('name').limit(1)
66
+ * const fullName = sql`concat(first_name, ' ', last_name)`
67
+ *
68
+ * sql`
69
+ * select ${fullName} as full_name, ${petName} as pet_name
70
+ * from person
71
+ * `
72
+ * ```
73
+ *
74
+ * Substitutions also handle {@link ExpressionBuilder.ref},
75
+ * {@link DynamicModule.ref} and pretty much anything else you
76
+ * throw at it. Here's an example of calling a function in a
77
+ * type-safe way:
78
+ *
79
+ * ```ts
80
+ * db.selectFrom('person')
81
+ * .select([
82
+ * 'first_name',
83
+ * 'last_name',
84
+ * (eb) => {
85
+ * // The `eb.ref` method is type-safe and only accepts
86
+ * // column references that are possible.
87
+ * const firstName = eb.ref('first_name')
88
+ * const lastName = eb.ref('last_name')
89
+ *
90
+ * const fullName = sql<string>`concat(${firstName}, ' ', ${lastName})`
91
+ * return fullName.as('full_name')
92
+ * }
93
+ * ])
94
+ * ```
95
+ *
96
+ * don't know if that amount of ceremony is worth the small increase in
97
+ * type-safety though... But it's possible.
98
+ */
3
99
  <T = unknown>(sqlFragments: TemplateStringsArray, ...parameters: unknown[]): RawBuilder<T>;
4
100
  /**
5
101
  * This can be used to add runtime column references to SQL snippets.
@@ -232,100 +328,4 @@ export interface Sql {
232
328
  */
233
329
  join(array: unknown[], separator?: RawBuilder<any>): RawBuilder<unknown>;
234
330
  }
235
- /**
236
- * Template tag for creating raw SQL snippets and queries.
237
- *
238
- * ```ts
239
- * import { sql } from 'kysely'
240
- *
241
- * const id = 123
242
- *
243
- * const snippet = sql<Person[]>`select * from person where id = ${id}`
244
- * ```
245
- *
246
- * Substitutions (the things inside `${}`) are automatically passed to the database
247
- * as parameters and are NOT interpolated to the SQL string. There's no need to worry
248
- * about SQL injection vulnerabilities.
249
- *
250
- * If you need your substitutions to be interpreted as identifiers, value literals or
251
- * lists of things, see the {@link Sql.ref}, {@link Sql.table}, {@link Sql.id},
252
- * {@link Sql.literal}, {@link Sql.raw} and {@link Sql.join} functions.
253
- *
254
- * You can pass sql snippets returned by the `sql` tag pretty much anywhere. Whenever
255
- * something can't be done using the Kysely API, you should be able to drop down to
256
- * raw SQL using the `sql` tag. Here's an example query that uses raw sql in a bunch
257
- * of methods:
258
- *
259
- * ```ts
260
- * const persons = await db
261
- * .selectFrom('person')
262
- * .select(
263
- * // If you use `sql` in a select statement, remember to call the `as`
264
- * // method to give it an alias. Otherwise Kysely is not able to add
265
- * // the column to the result type.
266
- * sql<string>`concat(first_name, ' ', last_name)`.as('full_name')
267
- * )
268
- * .where(sql`birthdate between ${date1} and ${date2}`)
269
- * // Here we assume we have list of nicknames for the person
270
- * // (a list of strings) and we use the PostgreSQL `@>` operator
271
- * // to test if all of them are valid nicknames for the user.
272
- * .where('nicknames', '@>', sql`ARRAY[${sql.join(nicknames)}]`)
273
- * .orderBy(sql`concat(first_name, ' ', last_name)`)
274
- * .execute()
275
- * ```
276
- *
277
- * The generated SQL (PostgreSQL):
278
- *
279
- * ```sql
280
- * select concat(first_name, ' ', last_name) as "full_name"
281
- * from "person"
282
- * where birthdate between $1 and $2
283
- * and "nicknames" @> ARRAY[$3, $4, $5, $6, $7, $8, $9, $10]
284
- * order by concat(first_name, ' ', last_name)
285
- * ```
286
- *
287
- * SQL snippets can be executed by calling the `execute` method and passing a `Kysely`
288
- * instance as the only argument:
289
- *
290
- * ```ts
291
- * const result = await sql<Person[]>`select * from person`.execute(db)
292
- * ```
293
- *
294
- * You can merge other `sql` expressions and queries using substitutions:
295
- *
296
- * ```ts
297
- * const petName = db.selectFrom('pet').select('name').limit(1)
298
- * const fullName = sql`concat(first_name, ' ', last_name)`
299
- *
300
- * sql`
301
- * select ${fullName} as full_name, ${petName} as pet_name
302
- * from person
303
- * `
304
- * ```
305
- *
306
- * Substitutions also handle {@link ExpressionBuilder.ref},
307
- * {@link DynamicModule.ref} and pretty much anything else you
308
- * throw at it. Here's an example of calling a function in a
309
- * type-safe way:
310
- *
311
- * ```ts
312
- * db.selectFrom('person')
313
- * .select([
314
- * 'first_name',
315
- * 'last_name',
316
- * (eb) => {
317
- * // The `eb.ref` method is type-safe and only accepts
318
- * // column references that are possible.
319
- * const firstName = eb.ref('first_name')
320
- * const lastName = eb.ref('last_name')
321
- *
322
- * const fullName = sql<string>`concat(${firstName}, ' ', ${lastName})`
323
- * return fullName.as('full_name')
324
- * }
325
- * ])
326
- * ```
327
- *
328
- * don't know if that amount of ceremony is worth the small increase in
329
- * type-safety though... But it's possible.
330
- */
331
331
  export declare const sql: Sql;