@sqb/builder 4.24.1 → 4.26.0

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/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@sqb/builder",
3
3
  "description": "Extensible multi-dialect SQL query builder written with TypeScript",
4
- "version": "4.24.1",
4
+ "version": "4.26.0",
5
5
  "author": "Panates",
6
6
  "license": "Apache-2.0",
7
7
  "dependencies": {
@@ -7,7 +7,7 @@ import { Query } from './query.js';
7
7
  export declare class DeleteQuery extends Query {
8
8
  _table: TableName | RawStatement;
9
9
  _where?: LogicalOperator;
10
- constructor(tableName: string | RawStatement);
10
+ constructor(tableName: string | TableName | RawStatement);
11
11
  get _type(): SerializationType;
12
12
  /**
13
13
  * Defines "where" part of query
@@ -8,12 +8,18 @@ export class DeleteQuery extends Query {
8
8
  _where;
9
9
  constructor(tableName) {
10
10
  super();
11
- if (!tableName ||
12
- !(typeof tableName === 'string' || isRawStatement(tableName))) {
11
+ if (!(tableName &&
12
+ (tableName instanceof TableName ||
13
+ typeof tableName === 'string' ||
14
+ isRawStatement(tableName)))) {
13
15
  throw new TypeError('String or Raw instance required as first argument (tableName) for UpdateQuery');
14
16
  }
15
17
  this._table =
16
- typeof tableName === 'string' ? new TableName(tableName) : tableName;
18
+ tableName instanceof TableName
19
+ ? tableName
20
+ : typeof tableName === 'string'
21
+ ? new TableName(tableName)
22
+ : tableName;
17
23
  }
18
24
  get _type() {
19
25
  return SerializationType.DELETE_QUERY;
package/query/query.d.ts CHANGED
@@ -3,9 +3,16 @@ import { Serializable } from '../serializable.js';
3
3
  import { GenerateOptions, GenerateResult } from '../types.js';
4
4
  export declare interface Query extends EventEmitter {
5
5
  }
6
+ export interface QueryCommentArgs {
7
+ comment: string;
8
+ dialect?: string[];
9
+ }
10
+ export interface QueryIndexHintArgs {
11
+ index: string[];
12
+ dialect?: string[];
13
+ }
6
14
  export declare abstract class Query extends Serializable {
7
- protected _comment?: string;
8
- protected _commentDialect?: string[];
15
+ protected _comment: QueryCommentArgs[];
9
16
  protected _params?: Record<string, any>;
10
17
  constructor();
11
18
  /**
@@ -13,5 +20,6 @@ export declare abstract class Query extends Serializable {
13
20
  */
14
21
  generate(options?: GenerateOptions): GenerateResult;
15
22
  values(obj: any): this;
23
+ comment(args: QueryCommentArgs): this;
16
24
  comment(text: string, dialect?: string[]): this;
17
25
  }
package/query/query.js CHANGED
@@ -4,8 +4,7 @@ import merge from 'putil-merge';
4
4
  import { Serializable } from '../serializable.js';
5
5
  import { SerializeContext } from '../serialize-context.js';
6
6
  export class Query extends Serializable {
7
- _comment;
8
- _commentDialect;
7
+ _comment = [];
9
8
  _params;
10
9
  constructor() {
11
10
  super();
@@ -22,12 +21,16 @@ export class Query extends Serializable {
22
21
  /* generate output */
23
22
  let sql = this._serialize(ctx);
24
23
  sql = flattenText(sql, { noWrap: !ctx.prettyPrint });
25
- if (this._comment &&
26
- (!ctx.dialect ||
27
- !this._commentDialect ||
28
- this._commentDialect.includes(ctx.dialect))) {
29
- const lines = '-- ' + this._comment.split('\n').join('\n-- ') + '\n';
30
- sql = lines + sql;
24
+ if (this._comment.length) {
25
+ const comment = this._comment
26
+ .filter(x => !ctx.dialect ||
27
+ !x.dialect?.length ||
28
+ x.dialect.includes(ctx.dialect))
29
+ .map(x => x.comment.replace(/\n/g, '\n '))
30
+ .join('\n');
31
+ if (comment) {
32
+ sql = `/*${comment}*/\n${sql}`;
33
+ }
31
34
  }
32
35
  return {
33
36
  sql,
@@ -42,9 +45,17 @@ export class Query extends Serializable {
42
45
  this._params = obj;
43
46
  return this;
44
47
  }
45
- comment(text, dialect) {
46
- this._comment = text;
47
- this._commentDialect = dialect;
48
+ comment(arg0, dialect) {
49
+ if (typeof arg0 === 'string')
50
+ this._comment.push({
51
+ comment: arg0,
52
+ dialect: Array.isArray(dialect) ? dialect : undefined,
53
+ });
54
+ else if (typeof arg0 === 'object' && typeof arg0.comment === 'string')
55
+ this._comment.push({
56
+ comment: arg0.comment,
57
+ dialect: Array.isArray(arg0.dialect) ? arg0.dialect : undefined,
58
+ });
48
59
  return this;
49
60
  }
50
61
  }
@@ -6,6 +6,7 @@ import { JoinStatement } from '../sql-objects/join-statement.js';
6
6
  import { LogicalOperator } from '../sql-objects/operators/logical-operator.js';
7
7
  import { OrderColumn } from '../sql-objects/order-column.js';
8
8
  import { RawStatement } from '../sql-objects/raw-statement.js';
9
+ import { TableName } from '../sql-objects/table-name.js';
9
10
  import { Query } from './query.js';
10
11
  import type { UnionQuery } from './union-query.js';
11
12
  export declare class SelectQuery extends Query {
@@ -28,7 +29,7 @@ export declare class SelectQuery extends Query {
28
29
  /**
29
30
  * Defines "from" part of query.
30
31
  */
31
- from(...table: (string | RawStatement | SelectQuery | UnionQuery)[]): this;
32
+ from(...table: (string | TableName | RawStatement | SelectQuery | UnionQuery)[]): this;
32
33
  /**
33
34
  * Adds "join" statements to query
34
35
  */
@@ -51,7 +51,11 @@ export class SelectQuery extends Query {
51
51
  for (const arg of table) {
52
52
  if (!arg)
53
53
  continue;
54
- this._tables.push(typeof arg === 'string' ? new TableName(arg) : arg);
54
+ this._tables.push(arg instanceof TableName
55
+ ? arg
56
+ : typeof arg === 'string'
57
+ ? new TableName(arg)
58
+ : arg);
55
59
  }
56
60
  return this;
57
61
  }
@@ -142,6 +146,7 @@ export class SelectQuery extends Query {
142
146
  */
143
147
  _serialize(ctx) {
144
148
  const o = {
149
+ query: this,
145
150
  columns: this.__serializeSelectColumns(ctx),
146
151
  from: this.__serializeFrom(ctx),
147
152
  join: this.__serializeJoins(ctx),
@@ -150,11 +155,14 @@ export class SelectQuery extends Query {
150
155
  orderBy: this.__serializeOrderColumns(ctx),
151
156
  limit: this._limit,
152
157
  offset: this._offset,
158
+ optimizerHint: '',
153
159
  };
154
160
  return ctx.serialize(this._type, o, () => {
155
161
  let out = 'select';
156
162
  if (this._distinct)
157
163
  out += ' distinct';
164
+ if (o.optimizerHint)
165
+ out += ' ' + o.optimizerHint;
158
166
  // columns part
159
167
  /* istanbul ignore else */
160
168
  if (o.columns) {
@@ -219,15 +227,21 @@ export class SelectQuery extends Query {
219
227
  if (t instanceof SelectQuery) {
220
228
  if (!t._alias)
221
229
  throw new TypeError('Alias required for sub-select in "from"');
222
- arr.push('\n\t(' + s + ') ' + t._alias);
230
+ arr.push({
231
+ source: t,
232
+ text: '\n\t(' + s + ') ' + t._alias,
233
+ });
223
234
  }
224
235
  else
225
- arr.push(s);
236
+ arr.push({
237
+ source: t,
238
+ text: s,
239
+ });
226
240
  }
227
241
  }
228
242
  }
229
243
  return ctx.serialize(SerializationType.SELECT_QUERY_FROM, arr, () => {
230
- const s = arr.join(',');
244
+ const s = arr.map(x => x.text).join(',');
231
245
  return s ? 'from' + (s.substring(0, 1) !== '\n' ? ' ' : '') + s : '';
232
246
  });
233
247
  }
@@ -8,7 +8,7 @@ export declare class UpdateQuery extends ReturningQuery {
8
8
  _table: TableName | RawStatement;
9
9
  _input: any;
10
10
  _where?: LogicalOperator;
11
- constructor(tableName: string | RawStatement, input: any);
11
+ constructor(tableName: string | TableName | RawStatement, input: any);
12
12
  get _type(): SerializationType;
13
13
  /**
14
14
  * Defines "where" part of query
@@ -10,8 +10,10 @@ export class UpdateQuery extends ReturningQuery {
10
10
  _where;
11
11
  constructor(tableName, input) {
12
12
  super();
13
- if (!tableName ||
14
- !(typeof tableName === 'string' || isRawStatement(tableName))) {
13
+ if (!(tableName &&
14
+ (tableName instanceof TableName ||
15
+ typeof tableName === 'string' ||
16
+ isRawStatement(tableName)))) {
15
17
  throw new TypeError('String or Raw instance required as first argument (tableName) for UpdateQuery');
16
18
  }
17
19
  if (!input ||
@@ -19,7 +21,11 @@ export class UpdateQuery extends ReturningQuery {
19
21
  throw new TypeError('Object or Raw instance required as second argument (input) for UpdateQuery');
20
22
  }
21
23
  this._table =
22
- typeof tableName === 'string' ? new TableName(tableName) : tableName;
24
+ tableName instanceof TableName
25
+ ? tableName
26
+ : typeof tableName === 'string'
27
+ ? new TableName(tableName)
28
+ : tableName;
23
29
  this._input = input;
24
30
  }
25
31
  get _type() {
@@ -1,11 +1,22 @@
1
1
  import { SerializationType } from '../enums.js';
2
2
  import { Serializable } from '../serializable.js';
3
3
  import { SerializeContext } from '../serialize-context.js';
4
+ export interface OptimizerHintArgs {
5
+ hint: string;
6
+ dialect?: string[];
7
+ }
8
+ export interface TableNameArgs {
9
+ schema?: string;
10
+ table: string;
11
+ alias?: string;
12
+ optimizerHint?: string | string[] | OptimizerHintArgs | OptimizerHintArgs[];
13
+ }
4
14
  export declare class TableName extends Serializable {
5
15
  schema?: string;
6
16
  table?: string;
7
17
  alias?: string;
8
- constructor(tableName: string);
18
+ optimizerHint?: OptimizerHintArgs[];
19
+ constructor(tableName: string | TableNameArgs);
9
20
  get _type(): SerializationType;
10
21
  _serialize(ctx: SerializeContext): string;
11
22
  }
@@ -4,17 +4,40 @@ export class TableName extends Serializable {
4
4
  schema;
5
5
  table;
6
6
  alias;
7
+ optimizerHint;
7
8
  constructor(tableName) {
8
9
  super();
9
- const m = tableName.match(/^(?:([a-zA-Z][\w$]*)\.)? *([a-zA-Z][\w$]*) *(?:as)? *(\w+)?$/);
10
- if (!m)
11
- throw new TypeError(`(${tableName}) does not match table name format`);
12
- if (m[1])
13
- this.schema = m[1];
14
- if (m[2])
15
- this.table = m[2];
16
- if (m[3])
17
- this.alias = m[3];
10
+ if (typeof tableName === 'string') {
11
+ const m = tableName.match(/^(?:([a-zA-Z][\w$]*)\.)? *([a-zA-Z][\w$]*) *(?:as)? *(\w+)?$/);
12
+ if (!m)
13
+ throw new TypeError(`(${tableName}) does not match table name format`);
14
+ if (m[1])
15
+ this.schema = m[1];
16
+ if (m[2])
17
+ this.table = m[2];
18
+ if (m[3])
19
+ this.alias = m[3];
20
+ }
21
+ else {
22
+ this.schema = tableName.schema;
23
+ this.table = tableName.table;
24
+ this.alias = tableName.alias;
25
+ if (tableName.optimizerHint) {
26
+ const arg0 = tableName.optimizerHint;
27
+ this.optimizerHint = [];
28
+ if (typeof arg0 === 'object' && !Array.isArray(arg0)) {
29
+ this.optimizerHint.push({
30
+ hint: String(arg0.hint),
31
+ dialect: Array.isArray(arg0.dialect) ? arg0.dialect : undefined,
32
+ });
33
+ }
34
+ else {
35
+ this.optimizerHint.push({
36
+ hint: String(arg0),
37
+ });
38
+ }
39
+ }
40
+ }
18
41
  }
19
42
  get _type() {
20
43
  return SerializationType.TABLE_NAME;
@@ -24,6 +47,7 @@ export class TableName extends Serializable {
24
47
  schema: this.schema,
25
48
  table: this.table,
26
49
  alias: this.alias,
50
+ optimizerHint: this.optimizerHint,
27
51
  }, () => (this.schema ? this.schema + '.' : '') +
28
52
  this.table +
29
53
  (this.alias ? ' ' + this.alias : ''));
@@ -18,14 +18,16 @@ import { ParamExpression } from './sql-objects/param-expression.js';
18
18
  import { RawStatement } from './sql-objects/raw-statement.js';
19
19
  import { SequenceGetterStatement } from './sql-objects/sequence-getter-statement.js';
20
20
  import { StringAGGStatement } from './sql-objects/string-agg-statement.js';
21
+ import { TableName as TableNameClass, TableNameArgs } from './sql-objects/table-name.js';
21
22
  import { UpperStatement } from './sql-objects/upper-statement.js';
22
23
  export declare function Raw(text: string): RawStatement;
23
24
  export declare function Select(...column: (string | string[] | Serializable)[]): SelectQuery;
24
25
  export declare function Insert(tableName: string | RawStatement, input: any): InsertQuery;
25
- export declare function Update(tableName: string | RawStatement, input: any): UpdateQuery;
26
- export declare function Delete(tableName: string | RawStatement): DeleteQuery;
26
+ export declare function Update(tableName: string | TableNameClass | RawStatement, input: any): UpdateQuery;
27
+ export declare function Delete(tableName: string | TableNameClass | RawStatement): DeleteQuery;
27
28
  export declare function Union(...queries: Query[]): UnionQuery;
28
29
  export declare function UnionAll(...queries: Query[]): UnionQuery;
30
+ export declare function TableName(tableName: string | TableNameArgs): TableNameClass;
29
31
  export declare function Join(table: string | SelectQuery | RawStatement): JoinStatement;
30
32
  export declare function InnerJoin(table: string | SelectQuery | RawStatement): JoinStatement;
31
33
  export declare function LeftJoin(table: string | SelectQuery | RawStatement): JoinStatement;
@@ -16,6 +16,7 @@ import { ParamExpression } from './sql-objects/param-expression.js';
16
16
  import { RawStatement } from './sql-objects/raw-statement.js';
17
17
  import { SequenceGetterStatement } from './sql-objects/sequence-getter-statement.js';
18
18
  import { StringAGGStatement } from './sql-objects/string-agg-statement.js';
19
+ import { TableName as TableNameClass, } from './sql-objects/table-name.js';
19
20
  import { UpperStatement } from './sql-objects/upper-statement.js';
20
21
  export function Raw(text) {
21
22
  return new RawStatement(text);
@@ -38,6 +39,9 @@ export function Union(...queries) {
38
39
  export function UnionAll(...queries) {
39
40
  return new UnionQuery(queries, 'all');
40
41
  }
42
+ export function TableName(tableName) {
43
+ return new TableNameClass(tableName);
44
+ }
41
45
  export function Join(table) {
42
46
  return new JoinStatement(JoinType.INNER, table);
43
47
  }