rawsql-ts 0.5.0-beta → 0.6.0-beta
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/README.md +11 -0
- package/dist/esm/models/Clause.js +104 -6
- package/dist/esm/models/Clause.js.map +1 -1
- package/dist/esm/models/CreateTableQuery.js +1 -1
- package/dist/esm/models/CreateTableQuery.js.map +1 -1
- package/dist/esm/models/InsertQuery.js +2 -9
- package/dist/esm/models/InsertQuery.js.map +1 -1
- package/dist/esm/models/UpdateQuery.js +25 -0
- package/dist/esm/models/UpdateQuery.js.map +1 -0
- package/dist/esm/models/ValueComponent.js +31 -14
- package/dist/esm/models/ValueComponent.js.map +1 -1
- package/dist/esm/parsers/FullNameParser.js +89 -0
- package/dist/esm/parsers/FullNameParser.js.map +1 -0
- package/dist/esm/parsers/FunctionExpressionParser.js +33 -32
- package/dist/esm/parsers/FunctionExpressionParser.js.map +1 -1
- package/dist/esm/parsers/IdentifierParser.js +5 -30
- package/dist/esm/parsers/IdentifierParser.js.map +1 -1
- package/dist/esm/parsers/InsertQueryParser.js +6 -27
- package/dist/esm/parsers/InsertQueryParser.js.map +1 -1
- package/dist/esm/parsers/ReturningClauseParser.js +31 -0
- package/dist/esm/parsers/ReturningClauseParser.js.map +1 -0
- package/dist/esm/parsers/SetClauseParser.js +39 -0
- package/dist/esm/parsers/SetClauseParser.js.map +1 -0
- package/dist/esm/parsers/SourceExpressionParser.js +19 -0
- package/dist/esm/parsers/SourceExpressionParser.js.map +1 -1
- package/dist/esm/parsers/SourceParser.js +16 -8
- package/dist/esm/parsers/SourceParser.js.map +1 -1
- package/dist/esm/parsers/UpdateClauseParser.js +17 -0
- package/dist/esm/parsers/UpdateClauseParser.js.map +1 -0
- package/dist/esm/parsers/UpdateQueryParser.js +84 -0
- package/dist/esm/parsers/UpdateQueryParser.js.map +1 -0
- package/dist/esm/parsers/ValueParser.js +13 -11
- package/dist/esm/parsers/ValueParser.js.map +1 -1
- package/dist/esm/transformers/CTEDisabler.js +2 -2
- package/dist/esm/transformers/CTEDisabler.js.map +1 -1
- package/dist/esm/transformers/Formatter.js +92 -15
- package/dist/esm/transformers/Formatter.js.map +1 -1
- package/dist/esm/transformers/QueryBuilder.js +54 -6
- package/dist/esm/transformers/QueryBuilder.js.map +1 -1
- package/dist/esm/transformers/SelectValueCollector.js +4 -4
- package/dist/esm/transformers/UpstreamSelectQueryFinder.js +1 -1
- package/dist/esm/types/models/Clause.d.ts +58 -4
- package/dist/esm/types/models/InsertQuery.d.ts +4 -9
- package/dist/esm/types/models/UpdateQuery.d.ts +31 -0
- package/dist/esm/types/models/ValueComponent.d.ts +7 -4
- package/dist/esm/types/parsers/FullNameParser.d.ts +26 -0
- package/dist/esm/types/parsers/InsertQueryParser.d.ts +0 -1
- package/dist/esm/types/parsers/ReturningClauseParser.d.ts +12 -0
- package/dist/esm/types/parsers/SetClauseParser.d.ts +11 -0
- package/dist/esm/types/parsers/SourceExpressionParser.d.ts +8 -0
- package/dist/esm/types/parsers/SourceParser.d.ts +14 -0
- package/dist/esm/types/parsers/UpdateClauseParser.d.ts +15 -0
- package/dist/esm/types/parsers/UpdateQueryParser.d.ts +16 -0
- package/dist/esm/types/transformers/Formatter.d.ts +6 -0
- package/dist/esm/types/transformers/QueryBuilder.d.ts +9 -0
- package/dist/models/Clause.d.ts +58 -4
- package/dist/models/Clause.js +110 -7
- package/dist/models/Clause.js.map +1 -1
- package/dist/models/CreateTableQuery.js +1 -1
- package/dist/models/CreateTableQuery.js.map +1 -1
- package/dist/models/InsertQuery.d.ts +4 -9
- package/dist/models/InsertQuery.js +2 -9
- package/dist/models/InsertQuery.js.map +1 -1
- package/dist/models/UpdateQuery.d.ts +31 -0
- package/dist/models/UpdateQuery.js +29 -0
- package/dist/models/UpdateQuery.js.map +1 -0
- package/dist/models/ValueComponent.d.ts +7 -4
- package/dist/models/ValueComponent.js +31 -14
- package/dist/models/ValueComponent.js.map +1 -1
- package/dist/parsers/FullNameParser.d.ts +26 -0
- package/dist/parsers/FullNameParser.js +93 -0
- package/dist/parsers/FullNameParser.js.map +1 -0
- package/dist/parsers/FunctionExpressionParser.js +32 -31
- package/dist/parsers/FunctionExpressionParser.js.map +1 -1
- package/dist/parsers/IdentifierParser.js +5 -30
- package/dist/parsers/IdentifierParser.js.map +1 -1
- package/dist/parsers/InsertQueryParser.d.ts +0 -1
- package/dist/parsers/InsertQueryParser.js +6 -27
- package/dist/parsers/InsertQueryParser.js.map +1 -1
- package/dist/parsers/ReturningClauseParser.d.ts +12 -0
- package/dist/parsers/ReturningClauseParser.js +35 -0
- package/dist/parsers/ReturningClauseParser.js.map +1 -0
- package/dist/parsers/SetClauseParser.d.ts +11 -0
- package/dist/parsers/SetClauseParser.js +43 -0
- package/dist/parsers/SetClauseParser.js.map +1 -0
- package/dist/parsers/SourceExpressionParser.d.ts +8 -0
- package/dist/parsers/SourceExpressionParser.js +19 -0
- package/dist/parsers/SourceExpressionParser.js.map +1 -1
- package/dist/parsers/SourceParser.d.ts +14 -0
- package/dist/parsers/SourceParser.js +16 -8
- package/dist/parsers/SourceParser.js.map +1 -1
- package/dist/parsers/UpdateClauseParser.d.ts +15 -0
- package/dist/parsers/UpdateClauseParser.js +21 -0
- package/dist/parsers/UpdateClauseParser.js.map +1 -0
- package/dist/parsers/UpdateQueryParser.d.ts +16 -0
- package/dist/parsers/UpdateQueryParser.js +88 -0
- package/dist/parsers/UpdateQueryParser.js.map +1 -0
- package/dist/parsers/ValueParser.js +13 -11
- package/dist/parsers/ValueParser.js.map +1 -1
- package/dist/transformers/CTEDisabler.js +2 -2
- package/dist/transformers/CTEDisabler.js.map +1 -1
- package/dist/transformers/Formatter.d.ts +6 -0
- package/dist/transformers/Formatter.js +91 -14
- package/dist/transformers/Formatter.js.map +1 -1
- package/dist/transformers/QueryBuilder.d.ts +9 -0
- package/dist/transformers/QueryBuilder.js +53 -5
- package/dist/transformers/QueryBuilder.js.map +1 -1
- package/dist/transformers/SelectValueCollector.js +4 -4
- package/dist/transformers/UpstreamSelectQueryFinder.js +1 -1
- package/package.json +1 -1
- package/dist/esm/types/utils/extractNamespacesAndName.d.ts +0 -5
- package/dist/esm/types/utils/parseEscapedOrDotSeparatedIdentifiers.d.ts +0 -9
- package/dist/esm/utils/extractNamespacesAndName.js +0 -16
- package/dist/esm/utils/extractNamespacesAndName.js.map +0 -1
- package/dist/esm/utils/parseEscapedOrDotSeparatedIdentifiers.js +0 -39
- package/dist/esm/utils/parseEscapedOrDotSeparatedIdentifiers.js.map +0 -1
- package/dist/utils/extractNamespacesAndName.d.ts +0 -5
- package/dist/utils/extractNamespacesAndName.js +0 -18
- package/dist/utils/extractNamespacesAndName.js.map +0 -1
- package/dist/utils/parseEscapedOrDotSeparatedIdentifiers.d.ts +0 -9
- package/dist/utils/parseEscapedOrDotSeparatedIdentifiers.js +0 -42
- package/dist/utils/parseEscapedOrDotSeparatedIdentifiers.js.map +0 -1
@@ -116,13 +116,13 @@ export class SelectValueCollector {
|
|
116
116
|
const wildSourceNames = wildcards.filter(item => item.value instanceof ColumnReference && item.value.namespaces)
|
117
117
|
.map(item => item.value.getNamespace());
|
118
118
|
if (query.fromClause) {
|
119
|
-
const fromSourceName = query.fromClause.
|
119
|
+
const fromSourceName = query.fromClause.getSourceAliasName();
|
120
120
|
if (fromSourceName && wildSourceNames.includes(fromSourceName)) {
|
121
121
|
this.processFromClause(query.fromClause, false);
|
122
122
|
}
|
123
123
|
if (query.fromClause.joins) {
|
124
124
|
for (const join of query.fromClause.joins) {
|
125
|
-
const joinSourceName = join.
|
125
|
+
const joinSourceName = join.getSourceAliasName();
|
126
126
|
if (joinSourceName && wildSourceNames.includes(joinSourceName)) {
|
127
127
|
this.processJoinClause(join);
|
128
128
|
}
|
@@ -135,7 +135,7 @@ export class SelectValueCollector {
|
|
135
135
|
}
|
136
136
|
processFromClause(clause, joinCascade) {
|
137
137
|
if (clause) {
|
138
|
-
const fromSourceName = clause.
|
138
|
+
const fromSourceName = clause.getSourceAliasName();
|
139
139
|
this.processSourceExpression(fromSourceName, clause.source);
|
140
140
|
if (clause.joins && joinCascade) {
|
141
141
|
for (const join of clause.joins) {
|
@@ -146,7 +146,7 @@ export class SelectValueCollector {
|
|
146
146
|
return;
|
147
147
|
}
|
148
148
|
processJoinClause(clause) {
|
149
|
-
const sourceName = clause.
|
149
|
+
const sourceName = clause.getSourceAliasName();
|
150
150
|
this.processSourceExpression(sourceName, clause.source);
|
151
151
|
}
|
152
152
|
processSourceExpression(sourceName, source) {
|
@@ -25,7 +25,7 @@ export class UpstreamSelectQueryFinder {
|
|
25
25
|
const ctes = cteCollector.collect(query);
|
26
26
|
const cteMap = new Map();
|
27
27
|
for (const cte of ctes) {
|
28
|
-
cteMap.set(cte.
|
28
|
+
cteMap.set(cte.getSourceAliasName(), cte);
|
29
29
|
}
|
30
30
|
return this.findUpstream(query, columnNames, cteMap);
|
31
31
|
}
|
@@ -76,7 +76,7 @@ export declare class TableSource extends SqlComponent {
|
|
76
76
|
namespaces: IdentifierString[] | null;
|
77
77
|
table: IdentifierString;
|
78
78
|
identifier: IdentifierString;
|
79
|
-
constructor(namespaces: string[] | null, table: string);
|
79
|
+
constructor(namespaces: string[] | IdentifierString[] | null, table: string | IdentifierString);
|
80
80
|
getSourceName(): string;
|
81
81
|
}
|
82
82
|
export declare class FunctionSource extends SqlComponent {
|
@@ -120,14 +120,14 @@ export declare class JoinClause extends SqlComponent {
|
|
120
120
|
condition: JoinConditionComponent | null;
|
121
121
|
lateral: boolean;
|
122
122
|
constructor(joinType: string, source: SourceExpression, condition: JoinConditionComponent | null, lateral: boolean);
|
123
|
-
|
123
|
+
getSourceAliasName(): string | null;
|
124
124
|
}
|
125
125
|
export declare class FromClause extends SqlComponent {
|
126
126
|
static kind: symbol;
|
127
127
|
source: SourceExpression;
|
128
128
|
joins: JoinClause[] | null;
|
129
129
|
constructor(source: SourceExpression, join: JoinClause[] | null);
|
130
|
-
|
130
|
+
getSourceAliasName(): string | null;
|
131
131
|
/**
|
132
132
|
* Returns all SourceExpression objects in this FROM clause, including main source and all JOIN sources.
|
133
133
|
*/
|
@@ -139,7 +139,7 @@ export declare class CommonTable extends SqlComponent {
|
|
139
139
|
materialized: boolean | null;
|
140
140
|
aliasExpression: SourceAliasExpression;
|
141
141
|
constructor(query: SelectQuery, aliasExpression: SourceAliasExpression | string, materialized: boolean | null);
|
142
|
-
|
142
|
+
getSourceAliasName(): string;
|
143
143
|
}
|
144
144
|
export declare class WithClause extends SqlComponent {
|
145
145
|
static kind: symbol;
|
@@ -186,3 +186,57 @@ export declare class SourceAliasExpression extends SqlComponent {
|
|
186
186
|
columns: IdentifierString[] | null;
|
187
187
|
constructor(alias: string, columnAlias: string[] | null);
|
188
188
|
}
|
189
|
+
export declare class ReturningClause extends SqlComponent {
|
190
|
+
static kind: symbol;
|
191
|
+
columns: IdentifierString[];
|
192
|
+
/**
|
193
|
+
* Constructs a ReturningClause.
|
194
|
+
* @param columns Array of IdentifierString or string representing column names.
|
195
|
+
*/
|
196
|
+
constructor(columns: (IdentifierString | string)[]);
|
197
|
+
}
|
198
|
+
export declare class SetClause extends SqlComponent {
|
199
|
+
static kind: symbol;
|
200
|
+
items: SetClauseItem[];
|
201
|
+
constructor(items: (SetClauseItem | {
|
202
|
+
column: string | IdentifierString;
|
203
|
+
value: ValueComponent;
|
204
|
+
})[]);
|
205
|
+
}
|
206
|
+
/**
|
207
|
+
* Represents a single SET clause item in an UPDATE statement.
|
208
|
+
*/
|
209
|
+
/**
|
210
|
+
* Represents a single SET clause item in an UPDATE statement.
|
211
|
+
* Now supports namespaces for fully qualified column names (e.g. schema.table.column).
|
212
|
+
*/
|
213
|
+
export declare class SetClauseItem extends SqlComponent {
|
214
|
+
static kind: symbol;
|
215
|
+
namespaces: IdentifierString[] | null;
|
216
|
+
column: IdentifierString;
|
217
|
+
value: ValueComponent;
|
218
|
+
constructor(column: string | IdentifierString | {
|
219
|
+
namespaces: string[] | IdentifierString[] | null;
|
220
|
+
column: string | IdentifierString;
|
221
|
+
}, value: ValueComponent);
|
222
|
+
/**
|
223
|
+
* Returns the fully qualified column name as a string.
|
224
|
+
*/
|
225
|
+
getFullName(): string;
|
226
|
+
}
|
227
|
+
export declare class UpdateClause extends SqlComponent {
|
228
|
+
static kind: symbol;
|
229
|
+
source: SourceExpression;
|
230
|
+
constructor(source: SourceExpression);
|
231
|
+
getSourceAliasName(): string | null;
|
232
|
+
}
|
233
|
+
/**
|
234
|
+
* Represents the target table (with optional alias/schema) and columns for an INSERT statement.
|
235
|
+
* @param source The target table as a SourceExpression (can include schema, alias, etc.)
|
236
|
+
* @param columns Array of column names (as strings)
|
237
|
+
*/
|
238
|
+
export declare class InsertClause extends SqlComponent {
|
239
|
+
source: SourceExpression;
|
240
|
+
columns: string[];
|
241
|
+
constructor(source: SourceExpression, columns: string[]);
|
242
|
+
}
|
@@ -1,21 +1,16 @@
|
|
1
1
|
import { SqlComponent } from "./SqlComponent";
|
2
|
-
import { IdentifierString } from "./ValueComponent";
|
3
2
|
import { SelectQuery } from "./SelectQuery";
|
3
|
+
import { InsertClause } from "./Clause";
|
4
4
|
export declare class InsertQuery extends SqlComponent {
|
5
5
|
static kind: symbol;
|
6
|
-
|
7
|
-
table: IdentifierString;
|
8
|
-
columns: IdentifierString[];
|
6
|
+
insertClause: InsertClause;
|
9
7
|
selectQuery: SelectQuery | null;
|
10
8
|
/**
|
11
|
-
* @param params.
|
12
|
-
* @param params.columns Array of column names (string[] or IdentifierString[])
|
9
|
+
* @param params.insertClause InsertClause instance (target table and columns)
|
13
10
|
* @param params.selectQuery SELECT/VALUES query (required)
|
14
11
|
*/
|
15
12
|
constructor(params: {
|
16
|
-
|
17
|
-
table: string | IdentifierString;
|
18
|
-
columns: (string | IdentifierString)[];
|
13
|
+
insertClause: InsertClause;
|
19
14
|
selectQuery?: SelectQuery | null;
|
20
15
|
});
|
21
16
|
}
|
@@ -0,0 +1,31 @@
|
|
1
|
+
import { SqlComponent } from "./SqlComponent";
|
2
|
+
import { IdentifierString, ValueComponent } from "./ValueComponent";
|
3
|
+
import { FromClause, ReturningClause, SetClause, WhereClause, UpdateClause } from "./Clause";
|
4
|
+
import { WithClause } from "./Clause";
|
5
|
+
export declare class UpdateQuery extends SqlComponent {
|
6
|
+
static kind: symbol;
|
7
|
+
withClause: WithClause | null;
|
8
|
+
updateClause: UpdateClause;
|
9
|
+
setClause: SetClause;
|
10
|
+
whereClause: WhereClause | null;
|
11
|
+
fromClause: FromClause | null;
|
12
|
+
returningClause: ReturningClause | null;
|
13
|
+
/**
|
14
|
+
* @param params.source SourceExpression (table or subquery with optional alias)
|
15
|
+
* @param params.setClause SetClause instance or array of {column, value} pairs
|
16
|
+
* @param params.where WHERE clause (optional)
|
17
|
+
* @param params.from FROM clause (optional)
|
18
|
+
* @param params.returning RETURNING clause (optional)
|
19
|
+
*/
|
20
|
+
constructor(params: {
|
21
|
+
withClause?: WithClause | null;
|
22
|
+
updateClause: UpdateClause;
|
23
|
+
setClause: SetClause | {
|
24
|
+
column: string | IdentifierString;
|
25
|
+
value: ValueComponent;
|
26
|
+
}[];
|
27
|
+
whereClause?: WhereClause | null;
|
28
|
+
fromClause?: FromClause | null;
|
29
|
+
returning?: ReturningClause | null;
|
30
|
+
});
|
31
|
+
}
|
@@ -16,16 +16,17 @@ export declare class ColumnReference extends SqlComponent {
|
|
16
16
|
static kind: symbol;
|
17
17
|
namespaces: IdentifierString[] | null;
|
18
18
|
column: IdentifierString;
|
19
|
-
constructor(namespaces: string | string[] | null, column: string);
|
19
|
+
constructor(namespaces: string | string[] | IdentifierString[] | null, column: string | IdentifierString);
|
20
20
|
toString(): string;
|
21
21
|
getNamespace(): string;
|
22
22
|
}
|
23
23
|
export declare class FunctionCall extends SqlComponent {
|
24
24
|
static kind: symbol;
|
25
|
+
namespaces: IdentifierString[] | null;
|
25
26
|
name: RawString;
|
26
27
|
argument: ValueComponent | null;
|
27
28
|
over: OverExpression | null;
|
28
|
-
constructor(name: string, argument: ValueComponent | null, over: OverExpression | null);
|
29
|
+
constructor(namespaces: string[] | IdentifierString[] | null, name: string | RawString, argument: ValueComponent | null, over: OverExpression | null);
|
29
30
|
}
|
30
31
|
export type OverExpression = WindowFrameExpression | IdentifierString;
|
31
32
|
export declare enum WindowFrameType {
|
@@ -153,9 +154,11 @@ export declare class StringSpecifierExpression extends SqlComponent {
|
|
153
154
|
}
|
154
155
|
export declare class TypeValue extends SqlComponent {
|
155
156
|
static kind: symbol;
|
156
|
-
|
157
|
+
namespaces: IdentifierString[] | null;
|
158
|
+
name: RawString;
|
157
159
|
argument: ValueComponent | null;
|
158
|
-
constructor(
|
160
|
+
constructor(namespaces: string[] | IdentifierString[] | null, name: string | RawString, argument?: ValueComponent | null);
|
161
|
+
getTypeName(): string;
|
159
162
|
}
|
160
163
|
export declare class TupleExpression extends SqlComponent {
|
161
164
|
static kind: symbol;
|
@@ -0,0 +1,26 @@
|
|
1
|
+
import { Lexeme } from "../models/Lexeme";
|
2
|
+
import { IdentifierString } from "../models/ValueComponent";
|
3
|
+
/**
|
4
|
+
* Utility class for parsing fully qualified names (e.g. db.schema.table or db.schema.table.column_name)
|
5
|
+
* This can be used for both table and column references.
|
6
|
+
*/
|
7
|
+
export declare class FullNameParser {
|
8
|
+
/**
|
9
|
+
* Parses a fully qualified name from lexemes, returning namespaces, table, and new index.
|
10
|
+
*/
|
11
|
+
static parseFromLexeme(lexemes: Lexeme[], index: number): {
|
12
|
+
namespaces: string[] | null;
|
13
|
+
name: IdentifierString;
|
14
|
+
newIndex: number;
|
15
|
+
};
|
16
|
+
/**
|
17
|
+
* Parses a fully qualified name from a string (e.g. 'db.schema.table')
|
18
|
+
* Returns { namespaces, name }
|
19
|
+
*/
|
20
|
+
static parse(str: string): {
|
21
|
+
namespaces: string[] | null;
|
22
|
+
name: IdentifierString;
|
23
|
+
};
|
24
|
+
private static parseEscapedOrDotSeparatedIdentifiers;
|
25
|
+
private static extractNamespacesAndName;
|
26
|
+
}
|
@@ -0,0 +1,12 @@
|
|
1
|
+
import { Lexeme } from "../models/Lexeme";
|
2
|
+
import { ReturningClause } from "../models/Clause";
|
3
|
+
export declare class ReturningClauseParser {
|
4
|
+
/**
|
5
|
+
* Parse RETURNING clause from lexemes, starting at the given index.
|
6
|
+
* Returns a ReturningClause instance and the new index after parsing.
|
7
|
+
*/
|
8
|
+
static parseFromLexeme(lexemes: Lexeme[], index: number): {
|
9
|
+
value: ReturningClause;
|
10
|
+
newIndex: number;
|
11
|
+
};
|
12
|
+
}
|
@@ -0,0 +1,11 @@
|
|
1
|
+
import { Lexeme } from "../models/Lexeme";
|
2
|
+
import { SetClause } from "../models/Clause";
|
3
|
+
/**
|
4
|
+
* Parse SET clause from lexemes (including 'SET' keyword check).
|
5
|
+
*/
|
6
|
+
export declare class SetClauseParser {
|
7
|
+
static parseFromLexeme(lexemes: Lexeme[], idx: number): {
|
8
|
+
setClause: SetClause;
|
9
|
+
newIndex: number;
|
10
|
+
};
|
11
|
+
}
|
@@ -1,6 +1,14 @@
|
|
1
1
|
import { SourceExpression } from "../models/Clause";
|
2
2
|
import { Lexeme } from "../models/Lexeme";
|
3
3
|
export declare class SourceExpressionParser {
|
4
|
+
/**
|
5
|
+
* Parse SQL string to SourceExpression (e.g. "table", "table as t", "schema.table t")
|
6
|
+
*/
|
7
|
+
static parse(query: string): SourceExpression;
|
8
|
+
static parseTableSourceFromLexemes(lexemes: Lexeme[], index: number): {
|
9
|
+
value: SourceExpression;
|
10
|
+
newIndex: number;
|
11
|
+
};
|
4
12
|
static parseFromLexeme(lexemes: Lexeme[], index: number): {
|
5
13
|
value: SourceExpression;
|
6
14
|
newIndex: number;
|
@@ -2,6 +2,20 @@ import { SourceComponent } from "../models/Clause";
|
|
2
2
|
import { Lexeme } from "../models/Lexeme";
|
3
3
|
export declare class SourceParser {
|
4
4
|
static parse(query: string): SourceComponent;
|
5
|
+
/**
|
6
|
+
* Parses only a TableSource from the given lexemes, regardless of the presence of parentheses after the identifier.
|
7
|
+
* This method is specifically used for cases like INSERT queries (e.g., "insert into table_name (col1, col2)")
|
8
|
+
* where a parenthesis immediately following the table name could otherwise be misinterpreted as a function call.
|
9
|
+
* By using this method, the parser forcibly treats the source as a TableSource.
|
10
|
+
*
|
11
|
+
* @param lexemes The array of lexemes to parse.
|
12
|
+
* @param index The starting index in the lexeme array.
|
13
|
+
* @returns An object containing the parsed TableSource and the new index.
|
14
|
+
*/
|
15
|
+
static parseTableSourceFromLexemes(lexemes: Lexeme[], index: number): {
|
16
|
+
value: SourceComponent;
|
17
|
+
newIndex: number;
|
18
|
+
};
|
5
19
|
static parseFromLexeme(lexemes: Lexeme[], index: number): {
|
6
20
|
value: SourceComponent;
|
7
21
|
newIndex: number;
|
@@ -0,0 +1,15 @@
|
|
1
|
+
import { UpdateClause } from "../models/Clause";
|
2
|
+
import { Lexeme } from "../models/Lexeme";
|
3
|
+
/**
|
4
|
+
* Parses the target of an UPDATE statement (table or source expression with optional alias).
|
5
|
+
*/
|
6
|
+
export declare class UpdateClauseParser {
|
7
|
+
/**
|
8
|
+
* Parse from lexeme array (returns UpdateClause and new index)
|
9
|
+
* This method parses a table or a table with alias using SourceExpressionParser.
|
10
|
+
*/
|
11
|
+
static parseFromLexeme(lexemes: Lexeme[], index: number): {
|
12
|
+
value: UpdateClause;
|
13
|
+
newIndex: number;
|
14
|
+
};
|
15
|
+
}
|
@@ -0,0 +1,16 @@
|
|
1
|
+
import { UpdateQuery } from "../models/UpdateQuery";
|
2
|
+
import { Lexeme } from "../models/Lexeme";
|
3
|
+
export declare class UpdateQueryParser {
|
4
|
+
/**
|
5
|
+
* Parse SQL string to UpdateQuery AST.
|
6
|
+
* @param query SQL string
|
7
|
+
*/
|
8
|
+
static parse(query: string): UpdateQuery;
|
9
|
+
/**
|
10
|
+
* Parse from lexeme array (for internal use and tests)
|
11
|
+
*/
|
12
|
+
static parseFromLexeme(lexemes: Lexeme[], index: number): {
|
13
|
+
value: UpdateQuery;
|
14
|
+
newIndex: number;
|
15
|
+
};
|
16
|
+
}
|
@@ -104,5 +104,11 @@ export declare class Formatter implements SqlComponentVisitor<string> {
|
|
104
104
|
*/
|
105
105
|
private visitCreateTableQuery;
|
106
106
|
private visitInsertQuery;
|
107
|
+
private visitUpdateQuery;
|
108
|
+
private visitUpdateClause;
|
109
|
+
private visitSetClause;
|
110
|
+
private visitSetClauseItem;
|
111
|
+
private visitReturningClause;
|
112
|
+
private visitInsertClause;
|
107
113
|
}
|
108
114
|
export {};
|
@@ -1,3 +1,4 @@
|
|
1
|
+
import { UpdateQuery } from '../models/UpdateQuery';
|
1
2
|
import { BinarySelectQuery, SelectQuery, SimpleSelectQuery } from "../models/SelectQuery";
|
2
3
|
import { CreateTableQuery } from "../models/CreateTableQuery";
|
3
4
|
import { InsertQuery } from "../models/InsertQuery";
|
@@ -51,4 +52,12 @@ export declare class QueryBuilder {
|
|
51
52
|
* @returns An InsertQuery instance
|
52
53
|
*/
|
53
54
|
static buildInsertQuery(selectQuery: SimpleSelectQuery, tableName: string): InsertQuery;
|
55
|
+
/**
|
56
|
+
* Builds an UPDATE query from a SELECT query, table name, and primary key(s).
|
57
|
+
* @param selectQuery The SELECT query providing new values (must select all columns to update and PKs)
|
58
|
+
* @param updateTableExprRaw The table name to update
|
59
|
+
* @param primaryKeys The primary key column name(s)
|
60
|
+
* @returns UpdateQuery instance
|
61
|
+
*/
|
62
|
+
static buildUpdateQuery(selectQuery: SimpleSelectQuery, selectSourceName: string, updateTableExprRaw: string, primaryKeys: string | string[]): UpdateQuery;
|
54
63
|
}
|
package/dist/models/Clause.d.ts
CHANGED
@@ -76,7 +76,7 @@ export declare class TableSource extends SqlComponent {
|
|
76
76
|
namespaces: IdentifierString[] | null;
|
77
77
|
table: IdentifierString;
|
78
78
|
identifier: IdentifierString;
|
79
|
-
constructor(namespaces: string[] | null, table: string);
|
79
|
+
constructor(namespaces: string[] | IdentifierString[] | null, table: string | IdentifierString);
|
80
80
|
getSourceName(): string;
|
81
81
|
}
|
82
82
|
export declare class FunctionSource extends SqlComponent {
|
@@ -120,14 +120,14 @@ export declare class JoinClause extends SqlComponent {
|
|
120
120
|
condition: JoinConditionComponent | null;
|
121
121
|
lateral: boolean;
|
122
122
|
constructor(joinType: string, source: SourceExpression, condition: JoinConditionComponent | null, lateral: boolean);
|
123
|
-
|
123
|
+
getSourceAliasName(): string | null;
|
124
124
|
}
|
125
125
|
export declare class FromClause extends SqlComponent {
|
126
126
|
static kind: symbol;
|
127
127
|
source: SourceExpression;
|
128
128
|
joins: JoinClause[] | null;
|
129
129
|
constructor(source: SourceExpression, join: JoinClause[] | null);
|
130
|
-
|
130
|
+
getSourceAliasName(): string | null;
|
131
131
|
/**
|
132
132
|
* Returns all SourceExpression objects in this FROM clause, including main source and all JOIN sources.
|
133
133
|
*/
|
@@ -139,7 +139,7 @@ export declare class CommonTable extends SqlComponent {
|
|
139
139
|
materialized: boolean | null;
|
140
140
|
aliasExpression: SourceAliasExpression;
|
141
141
|
constructor(query: SelectQuery, aliasExpression: SourceAliasExpression | string, materialized: boolean | null);
|
142
|
-
|
142
|
+
getSourceAliasName(): string;
|
143
143
|
}
|
144
144
|
export declare class WithClause extends SqlComponent {
|
145
145
|
static kind: symbol;
|
@@ -186,3 +186,57 @@ export declare class SourceAliasExpression extends SqlComponent {
|
|
186
186
|
columns: IdentifierString[] | null;
|
187
187
|
constructor(alias: string, columnAlias: string[] | null);
|
188
188
|
}
|
189
|
+
export declare class ReturningClause extends SqlComponent {
|
190
|
+
static kind: symbol;
|
191
|
+
columns: IdentifierString[];
|
192
|
+
/**
|
193
|
+
* Constructs a ReturningClause.
|
194
|
+
* @param columns Array of IdentifierString or string representing column names.
|
195
|
+
*/
|
196
|
+
constructor(columns: (IdentifierString | string)[]);
|
197
|
+
}
|
198
|
+
export declare class SetClause extends SqlComponent {
|
199
|
+
static kind: symbol;
|
200
|
+
items: SetClauseItem[];
|
201
|
+
constructor(items: (SetClauseItem | {
|
202
|
+
column: string | IdentifierString;
|
203
|
+
value: ValueComponent;
|
204
|
+
})[]);
|
205
|
+
}
|
206
|
+
/**
|
207
|
+
* Represents a single SET clause item in an UPDATE statement.
|
208
|
+
*/
|
209
|
+
/**
|
210
|
+
* Represents a single SET clause item in an UPDATE statement.
|
211
|
+
* Now supports namespaces for fully qualified column names (e.g. schema.table.column).
|
212
|
+
*/
|
213
|
+
export declare class SetClauseItem extends SqlComponent {
|
214
|
+
static kind: symbol;
|
215
|
+
namespaces: IdentifierString[] | null;
|
216
|
+
column: IdentifierString;
|
217
|
+
value: ValueComponent;
|
218
|
+
constructor(column: string | IdentifierString | {
|
219
|
+
namespaces: string[] | IdentifierString[] | null;
|
220
|
+
column: string | IdentifierString;
|
221
|
+
}, value: ValueComponent);
|
222
|
+
/**
|
223
|
+
* Returns the fully qualified column name as a string.
|
224
|
+
*/
|
225
|
+
getFullName(): string;
|
226
|
+
}
|
227
|
+
export declare class UpdateClause extends SqlComponent {
|
228
|
+
static kind: symbol;
|
229
|
+
source: SourceExpression;
|
230
|
+
constructor(source: SourceExpression);
|
231
|
+
getSourceAliasName(): string | null;
|
232
|
+
}
|
233
|
+
/**
|
234
|
+
* Represents the target table (with optional alias/schema) and columns for an INSERT statement.
|
235
|
+
* @param source The target table as a SourceExpression (can include schema, alias, etc.)
|
236
|
+
* @param columns Array of column names (as strings)
|
237
|
+
*/
|
238
|
+
export declare class InsertClause extends SqlComponent {
|
239
|
+
source: SourceExpression;
|
240
|
+
columns: string[];
|
241
|
+
constructor(source: SourceExpression, columns: string[]);
|
242
|
+
}
|
package/dist/models/Clause.js
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
"use strict";
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
3
|
-
exports.SourceAliasExpression = exports.ForClause = exports.LockMode = exports.FetchSpecification = exports.FetchUnit = exports.FetchType = exports.LimitClause = exports.WithClause = exports.CommonTable = exports.FromClause = exports.JoinClause = exports.JoinUsingClause = exports.JoinOnClause = exports.SourceExpression = exports.SubQuerySource = exports.ParenSource = exports.FunctionSource = exports.TableSource = exports.HavingClause = exports.GroupByClause = exports.OrderByItem = exports.OrderByClause = exports.NullsSortDirection = exports.SortDirection = exports.WindowFrameClause = exports.PartitionByClause = exports.WhereClause = exports.DistinctOn = exports.Distinct = exports.SelectClause = exports.SelectItem = void 0;
|
3
|
+
exports.InsertClause = exports.UpdateClause = exports.SetClauseItem = exports.SetClause = exports.ReturningClause = exports.SourceAliasExpression = exports.ForClause = exports.LockMode = exports.FetchSpecification = exports.FetchUnit = exports.FetchType = exports.LimitClause = exports.WithClause = exports.CommonTable = exports.FromClause = exports.JoinClause = exports.JoinUsingClause = exports.JoinOnClause = exports.SourceExpression = exports.SubQuerySource = exports.ParenSource = exports.FunctionSource = exports.TableSource = exports.HavingClause = exports.GroupByClause = exports.OrderByItem = exports.OrderByClause = exports.NullsSortDirection = exports.SortDirection = exports.WindowFrameClause = exports.PartitionByClause = exports.WhereClause = exports.DistinctOn = exports.Distinct = exports.SelectClause = exports.SelectItem = void 0;
|
4
4
|
const SqlComponent_1 = require("./SqlComponent");
|
5
5
|
const ValueComponent_1 = require("./ValueComponent");
|
6
6
|
class SelectItem extends SqlComponent_1.SqlComponent {
|
@@ -108,9 +108,18 @@ HavingClause.kind = Symbol("HavingClause");
|
|
108
108
|
class TableSource extends SqlComponent_1.SqlComponent {
|
109
109
|
constructor(namespaces, table) {
|
110
110
|
super();
|
111
|
-
|
112
|
-
|
113
|
-
|
111
|
+
// Accept both string[] and IdentifierString[] for namespaces
|
112
|
+
if (namespaces === null) {
|
113
|
+
this.namespaces = null;
|
114
|
+
}
|
115
|
+
else if (typeof namespaces[0] === "string") {
|
116
|
+
this.namespaces = namespaces.map(ns => new ValueComponent_1.IdentifierString(ns));
|
117
|
+
}
|
118
|
+
else {
|
119
|
+
this.namespaces = namespaces;
|
120
|
+
}
|
121
|
+
// Accept both string and IdentifierString for table
|
122
|
+
this.table = typeof table === "string" ? new ValueComponent_1.IdentifierString(table) : table;
|
114
123
|
this.identifier = this.table;
|
115
124
|
}
|
116
125
|
getSourceName() {
|
@@ -191,7 +200,7 @@ class JoinClause extends SqlComponent_1.SqlComponent {
|
|
191
200
|
this.condition = condition;
|
192
201
|
this.lateral = lateral;
|
193
202
|
}
|
194
|
-
|
203
|
+
getSourceAliasName() {
|
195
204
|
if (this.source.aliasExpression) {
|
196
205
|
return this.source.aliasExpression.table.name;
|
197
206
|
}
|
@@ -209,7 +218,7 @@ class FromClause extends SqlComponent_1.SqlComponent {
|
|
209
218
|
this.source = source;
|
210
219
|
this.joins = join;
|
211
220
|
}
|
212
|
-
|
221
|
+
getSourceAliasName() {
|
213
222
|
if (this.source.aliasExpression) {
|
214
223
|
return this.source.aliasExpression.table.name;
|
215
224
|
}
|
@@ -245,7 +254,7 @@ class CommonTable extends SqlComponent_1.SqlComponent {
|
|
245
254
|
this.aliasExpression = aliasExpression;
|
246
255
|
}
|
247
256
|
}
|
248
|
-
|
257
|
+
getSourceAliasName() {
|
249
258
|
return this.aliasExpression.table.name;
|
250
259
|
}
|
251
260
|
}
|
@@ -315,4 +324,98 @@ class SourceAliasExpression extends SqlComponent_1.SqlComponent {
|
|
315
324
|
}
|
316
325
|
exports.SourceAliasExpression = SourceAliasExpression;
|
317
326
|
SourceAliasExpression.kind = Symbol("SourceAliasExpression");
|
327
|
+
class ReturningClause extends SqlComponent_1.SqlComponent {
|
328
|
+
/**
|
329
|
+
* Constructs a ReturningClause.
|
330
|
+
* @param columns Array of IdentifierString or string representing column names.
|
331
|
+
*/
|
332
|
+
constructor(columns) {
|
333
|
+
super();
|
334
|
+
this.columns = columns.map(col => typeof col === "string" ? new ValueComponent_1.IdentifierString(col) : col);
|
335
|
+
}
|
336
|
+
}
|
337
|
+
exports.ReturningClause = ReturningClause;
|
338
|
+
ReturningClause.kind = Symbol("ReturningClause");
|
339
|
+
class SetClause extends SqlComponent_1.SqlComponent {
|
340
|
+
constructor(items) {
|
341
|
+
super();
|
342
|
+
this.items = items.map(item => item instanceof SetClauseItem ? item : new SetClauseItem(item.column, item.value));
|
343
|
+
}
|
344
|
+
}
|
345
|
+
exports.SetClause = SetClause;
|
346
|
+
SetClause.kind = Symbol("SetClause");
|
347
|
+
/**
|
348
|
+
* Represents a single SET clause item in an UPDATE statement.
|
349
|
+
*/
|
350
|
+
/**
|
351
|
+
* Represents a single SET clause item in an UPDATE statement.
|
352
|
+
* Now supports namespaces for fully qualified column names (e.g. schema.table.column).
|
353
|
+
*/
|
354
|
+
class SetClauseItem extends SqlComponent_1.SqlComponent {
|
355
|
+
constructor(column, value) {
|
356
|
+
super();
|
357
|
+
if (typeof column === "object" && column !== null && "column" in column) {
|
358
|
+
// Accepts { namespaces, column }
|
359
|
+
const colObj = column;
|
360
|
+
if (colObj.namespaces == null) {
|
361
|
+
this.namespaces = null;
|
362
|
+
}
|
363
|
+
else if (typeof colObj.namespaces[0] === "string") {
|
364
|
+
this.namespaces = colObj.namespaces.map(ns => new ValueComponent_1.IdentifierString(ns));
|
365
|
+
}
|
366
|
+
else {
|
367
|
+
this.namespaces = colObj.namespaces;
|
368
|
+
}
|
369
|
+
this.column = typeof colObj.column === "string" ? new ValueComponent_1.IdentifierString(colObj.column) : colObj.column;
|
370
|
+
}
|
371
|
+
else {
|
372
|
+
this.namespaces = null;
|
373
|
+
this.column = typeof column === "string" ? new ValueComponent_1.IdentifierString(column) : column;
|
374
|
+
}
|
375
|
+
this.value = value;
|
376
|
+
}
|
377
|
+
/**
|
378
|
+
* Returns the fully qualified column name as a string.
|
379
|
+
*/
|
380
|
+
getFullName() {
|
381
|
+
if (this.namespaces && this.namespaces.length > 0) {
|
382
|
+
return this.namespaces.map(ns => ns.name).join(".") + "." + this.column.name;
|
383
|
+
}
|
384
|
+
else {
|
385
|
+
return this.column.name;
|
386
|
+
}
|
387
|
+
}
|
388
|
+
}
|
389
|
+
exports.SetClauseItem = SetClauseItem;
|
390
|
+
SetClauseItem.kind = Symbol("SetClauseItem");
|
391
|
+
class UpdateClause extends SqlComponent_1.SqlComponent {
|
392
|
+
constructor(source) {
|
393
|
+
super();
|
394
|
+
this.source = source;
|
395
|
+
}
|
396
|
+
getSourceAliasName() {
|
397
|
+
if (this.source.aliasExpression) {
|
398
|
+
return this.source.aliasExpression.table.name;
|
399
|
+
}
|
400
|
+
else if (this.source.datasource instanceof TableSource) {
|
401
|
+
return this.source.datasource.table.name;
|
402
|
+
}
|
403
|
+
return null;
|
404
|
+
}
|
405
|
+
}
|
406
|
+
exports.UpdateClause = UpdateClause;
|
407
|
+
UpdateClause.kind = Symbol("UpdateClause");
|
408
|
+
/**
|
409
|
+
* Represents the target table (with optional alias/schema) and columns for an INSERT statement.
|
410
|
+
* @param source The target table as a SourceExpression (can include schema, alias, etc.)
|
411
|
+
* @param columns Array of column names (as strings)
|
412
|
+
*/
|
413
|
+
class InsertClause extends SqlComponent_1.SqlComponent {
|
414
|
+
constructor(source, columns) {
|
415
|
+
super();
|
416
|
+
this.source = source;
|
417
|
+
this.columns = columns;
|
418
|
+
}
|
419
|
+
}
|
420
|
+
exports.InsertClause = InsertClause;
|
318
421
|
//# sourceMappingURL=Clause.js.map
|