Skip to content
Merged

Dev #76

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion packages/builder/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "@sqb/builder",
"description": "Extensible multi-dialect SQL query builder written with TypeScript",
"version": "4.24.1",
"version": "4.25.0",
"author": "Panates",
"private": false,
"license": "Apache-2.0",
Expand Down
8 changes: 8 additions & 0 deletions packages/builder/src/query/delete-query.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,14 @@ export class DeleteQuery extends Query {
const o = {
table: this._table._serialize(ctx),
where: this._serializeWhere(ctx),
indexHint: this._indexHint.filter(
x =>
!ctx.dialect || !x.dialect?.length || x.dialect.includes(ctx.dialect),
),
noIndexHint: this._noIndexHint.filter(
x =>
!ctx.dialect || !x.dialect?.length || x.dialect.includes(ctx.dialect),
),
};
return ctx.serialize(this._type, o, () => this.__defaultSerialize(ctx, o));
}
Expand Down
8 changes: 8 additions & 0 deletions packages/builder/src/query/insert-query.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,14 @@ export class InsertQuery extends ReturningQuery {
columns: this.__serializeColumns(ctx),
values: this.__serializeValues(ctx),
returning: this.__serializeReturning(ctx),
indexHint: this._indexHint.filter(
x =>
!ctx.dialect || !x.dialect?.length || x.dialect.includes(ctx.dialect),
),
noIndexHint: this._noIndexHint.filter(
x =>
!ctx.dialect || !x.dialect?.length || x.dialect.includes(ctx.dialect),
),
};

let out =
Expand Down
86 changes: 73 additions & 13 deletions packages/builder/src/query/query.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,20 @@ import { GenerateOptions, GenerateResult } from '../types.js';

export declare interface Query extends EventEmitter {}

export interface QueryCommentArgs {
comment: string;
dialect?: string[];
}

export interface QueryIndexHintArgs {
index: string[];
dialect?: string[];
}

export abstract class Query extends Serializable {
protected _comment?: string;
protected _commentDialect?: string[];
protected _comment: QueryCommentArgs[] = [];
protected _indexHint: QueryIndexHintArgs[] = [];
protected _noIndexHint: QueryIndexHintArgs[] = [];
protected _params?: Record<string, any>;

constructor() {
Expand All @@ -28,14 +39,19 @@ export abstract class Query extends Serializable {
/* generate output */
let sql = this._serialize(ctx);
sql = flattenText(sql, { noWrap: !ctx.prettyPrint });
if (
this._comment &&
(!ctx.dialect ||
!this._commentDialect ||
this._commentDialect.includes(ctx.dialect))
) {
const lines = '-- ' + this._comment.split('\n').join('\n-- ') + '\n';
sql = lines + sql;
if (this._comment.length) {
const comment = this._comment
.filter(
x =>
!ctx.dialect ||
!x.dialect?.length ||
x.dialect.includes(ctx.dialect),
)
.map(x => x.comment.replace(/\n/g, '\n '))
.join('\n');
if (comment) {
sql = `/*${comment}*/\n${sql}`;
}
}
return {
sql,
Expand All @@ -52,9 +68,53 @@ export abstract class Query extends Serializable {
return this;
}

comment(text: string, dialect?: string[]): this {
this._comment = text;
this._commentDialect = dialect;
comment(args: QueryCommentArgs): this;
comment(text: string, dialect?: string[]): this;
comment(arg0: any, dialect?: string[]): this {
if (typeof arg0 === 'string')
this._comment.push({
comment: arg0,
dialect: Array.isArray(dialect) ? dialect : undefined,
});
else if (typeof arg0 === 'object' && typeof arg0.comment === 'string')
this._comment.push({
comment: arg0.comment,
dialect: Array.isArray(arg0.dialect) ? arg0.dialect : undefined,
});
return this;
}

indexHint(args: QueryIndexHintArgs): this;
indexHint(index: string | string[], dialect?: string[]): this;
indexHint(arg0: any, dialect?: string[]): this {
if (typeof arg0 === 'object' && !Array.isArray(arg0))
this._indexHint.push({
index: Array.isArray(arg0.index) ? arg0.index : [arg0.index],
dialect: Array.isArray(arg0.dialect) ? arg0.dialect : undefined,
});
else {
this._indexHint.push({
index: Array.isArray(arg0) ? arg0 : [arg0],
dialect: Array.isArray(dialect) ? dialect : undefined,
});
}
return this;
}

noIndexHint(args: QueryIndexHintArgs): this;
noIndexHint(index: string | string[], dialect?: string[]): this;
noIndexHint(arg0: any, dialect?: string[]): this {
if (typeof arg0 === 'object' && !Array.isArray(arg0))
this._noIndexHint.push({
index: Array.isArray(arg0.index) ? arg0.index : [arg0.index],
dialect: Array.isArray(arg0.dialect) ? arg0.dialect : undefined,
});
else {
this._noIndexHint.push({
index: Array.isArray(arg0) ? arg0 : [arg0],
dialect: Array.isArray(dialect) ? dialect : undefined,
});
}
return this;
}
}
Expand Down
8 changes: 8 additions & 0 deletions packages/builder/src/query/select-query.ts
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,14 @@ export class SelectQuery extends Query {
orderBy: this.__serializeOrderColumns(ctx),
limit: this._limit,
offset: this._offset,
indexHint: this._indexHint.filter(
x =>
!ctx.dialect || !x.dialect?.length || x.dialect.includes(ctx.dialect),
),
noIndexHint: this._noIndexHint.filter(
x =>
!ctx.dialect || !x.dialect?.length || x.dialect.includes(ctx.dialect),
),
};

return ctx.serialize(this._type, o, () => {
Expand Down
8 changes: 8 additions & 0 deletions packages/builder/src/query/update-query.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,14 @@ export class UpdateQuery extends ReturningQuery {
values: this.__serializeValues(ctx),
where: this.__serializeWhere(ctx),
returning: this.__serializeReturning(ctx),
indexHint: this._indexHint.filter(
x =>
!ctx.dialect || !x.dialect?.length || x.dialect.includes(ctx.dialect),
),
noIndexHint: this._noIndexHint.filter(
x =>
!ctx.dialect || !x.dialect?.length || x.dialect.includes(ctx.dialect),
),
};
let out = 'update ' + o.table + ' set \n\t' + o.values + '\b';
if (o.where) out += '\n' + o.where;
Expand Down
4 changes: 2 additions & 2 deletions packages/builder/test/query-objects/select-query.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -239,14 +239,14 @@ describe('builder:serialize "SelectQuery"', () => {
it('should serialize comment', () => {
const query = Select().comment('This is a comment');
const result = query.generate(options);
expect(result.sql).toStrictEqual('-- This is a comment\nselect *');
expect(result.sql).toStrictEqual('/*This is a comment*/\nselect *');
});

it('should serialize multiline comment', () => {
const query = Select().comment('This is a comment\nline 2\nline 3');
const result = query.generate(options);
expect(result.sql).toStrictEqual(
'-- This is a comment\n-- line 2\n-- line 3\nselect *',
'/*This is a comment\n line 2\n line 3*/\nselect *',
);
});
});
4 changes: 2 additions & 2 deletions packages/connect/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "@sqb/connect",
"description": "Multi-dialect database connection framework written with TypeScript",
"version": "4.24.1",
"version": "4.25.0",
"author": "Panates",
"license": "Apache-2.0",
"scripts": {
Expand Down Expand Up @@ -44,7 +44,7 @@
"postgrejs": "^2.22.9"
},
"peerDependencies": {
"@sqb/builder": "^4.24.1",
"@sqb/builder": "^4.25.0",
"reflect-metadata": "^0.2.2"
},
"type": "module",
Expand Down
6 changes: 5 additions & 1 deletion packages/connect/src/orm/commands/count.command.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,11 @@ export class CountCommand {
await prepareFilter(entity, filter, where, 'T');
}
const query = Select(Count()).from(entity.tableName + ' T');
if (args.comment) query.comment(args.comment, args.commentDialect);
if (args.comment) {
if (Array.isArray(args.comment))
args.comment.forEach(c => query.comment(c));
else query.comment(args.comment as any);
}
if (where) query.where(where);
// Execute query
const resp = await connection.execute(query, {
Expand Down
6 changes: 5 additions & 1 deletion packages/connect/src/orm/commands/create.command.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,11 @@ export class CreateCommand {
throw new Error('No field given to create new entity instance');

const query = Insert(tableName, ctx.queryValues);
if (args.comment) query.comment(args.comment, args.commentDialect);
if (args.comment) {
if (Array.isArray(args.comment))
args.comment.forEach(c => query.comment(c));
else query.comment(args.comment as any);
}
if (args.returning) {
const primaryIndexColumns = EntityMetadata.getPrimaryIndexColumns(entity);
if (primaryIndexColumns.length)
Expand Down
6 changes: 5 additions & 1 deletion packages/connect/src/orm/commands/delete.command.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,11 @@ export class DeleteCommand {
await prepareFilter(entity, filter, where);
}
const query = Delete(entity.tableName + ' T');
if (args.comment) query.comment(args.comment, args.commentDialect);
if (args.comment) {
if (Array.isArray(args.comment))
args.comment.forEach(c => query.comment(c));
else query.comment(args.comment as any);
}
if (where) query.where(...where._items);
// Execute query
const resp = await connection.execute(query, {
Expand Down
21 changes: 19 additions & 2 deletions packages/connect/src/orm/commands/find.command.ts
Original file line number Diff line number Diff line change
Expand Up @@ -337,7 +337,8 @@ export class FindCommand {
| 'onTransformRow'
| 'prettyPrint'
| 'comment'
| 'commentDialect'
| 'indexHint'
| 'noIndexHint'
>,
): Promise<any[]> {
// Generate select query
Expand All @@ -350,7 +351,23 @@ export class FindCommand {
this.mainEntity.tableName + ' as ' + this.mainAlias,
);

if (args.comment) query.comment(args.comment, args.commentDialect);
if (args.comment) {
if (Array.isArray(args.comment))
args.comment.forEach(c => query.comment(c));
else query.comment(args.comment as any);
}

if (args.indexHint) {
if (Array.isArray(args.indexHint))
args.indexHint.forEach(c => query.indexHint(c));
else query.indexHint(args.indexHint as any);
}

if (args.noIndexHint) {
if (Array.isArray(args.noIndexHint))
args.noIndexHint.forEach(c => query.noIndexHint(c));
else query.noIndexHint(args.noIndexHint as any);
}

if (args.distinct) query.distinct();

Expand Down
35 changes: 33 additions & 2 deletions packages/connect/src/orm/repository.class.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,39 @@ export namespace Repository {
export interface CommandOptions {
connection?: SqbConnection;
prettyPrint?: boolean;
comment?: string;
commentDialect?: string[];
comment?:
| string
| string[]
| {
comment: string;
dialect?: string[];
}
| {
comment: string;
dialect?: string[];
}[];
indexHint?:
| string
| string[]
| {
index: string[];
dialect?: string[];
}
| {
index: string[];
dialect?: string[];
}[];
noIndexHint?:
| string
| string[]
| {
index: string[];
dialect?: string[];
}
| {
index: string[];
dialect?: string[];
}[];
}

export interface CreateOptions extends CommandOptions, Projection {}
Expand Down
8 changes: 4 additions & 4 deletions packages/migrator/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "@sqb/migrator",
"description": "Database migrator for SQB",
"version": "4.24.1",
"version": "4.25.0",
"author": "Panates",
"license": "Apache-2.0",
"scripts": {
Expand All @@ -28,9 +28,9 @@
"tslib": "^2.8.1"
},
"peerDependencies": {
"@sqb/builder": "^4.24.1",
"@sqb/connect": "^4.24.1",
"@sqb/postgres": "^4.24.1"
"@sqb/builder": "^4.25.0",
"@sqb/connect": "^4.25.0",
"@sqb/postgres": "^4.25.0"
},
"devDependencies": {
"expect": "^30.3.0",
Expand Down
4 changes: 2 additions & 2 deletions packages/mssql-dialect/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "@sqb/mssql-dialect",
"description": "SQB serialization extension for MS-SQL database",
"version": "4.24.1",
"version": "4.25.0",
"author": "Panates",
"license": "Apache-2.0",
"scripts": {
Expand All @@ -24,7 +24,7 @@
"tslib": "^2.8.1"
},
"peerDependencies": {
"@sqb/builder": "^4.24.1"
"@sqb/builder": "^4.25.0"
},
"devDependencies": {
"expect": "^30.3.0"
Expand Down
6 changes: 3 additions & 3 deletions packages/nestjs/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "@sqb/nestjs",
"description": "Nestjs module for data connection using SQB",
"version": "4.24.1",
"version": "4.25.0",
"author": "Panates",
"license": "Apache-2.0",
"scripts": {
Expand Down Expand Up @@ -39,8 +39,8 @@
"peerDependencies": {
"@nestjs/common": ">=7.4.0",
"@nestjs/core": ">=7.4.0",
"@sqb/builder": "^4.24.1",
"@sqb/connect": "^4.24.1",
"@sqb/builder": "^4.25.0",
"@sqb/connect": "^4.25.0",
"reflect-metadata": "^0.2.2",
"rxjs": ">=6.6.0"
},
Expand Down
Loading