rawsql-ts 0.2.0-beta → 0.4.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 +134 -11
- package/dist/esm/index.js +2 -1
- package/dist/esm/index.js.map +1 -1
- package/dist/esm/models/BinarySelectQuery.js +0 -2
- package/dist/esm/models/BinarySelectQuery.js.map +1 -1
- package/dist/esm/models/CreateTableQuery.js +62 -0
- package/dist/esm/models/CreateTableQuery.js.map +1 -0
- package/dist/esm/models/InsertQuery.js +24 -0
- package/dist/esm/models/InsertQuery.js.map +1 -0
- package/dist/esm/models/SelectQuery.js +2 -1
- package/dist/esm/models/SelectQuery.js.map +1 -1
- package/dist/esm/models/SimpleSelectQuery.js +2 -2
- package/dist/esm/models/SimpleSelectQuery.js.map +1 -1
- package/dist/esm/models/ValueComponent.js +9 -1
- package/dist/esm/models/ValueComponent.js.map +1 -1
- package/dist/esm/models/ValuesQuery.js +6 -1
- package/dist/esm/models/ValuesQuery.js.map +1 -1
- package/dist/esm/parsers/IdentifierParser.js +3 -2
- package/dist/esm/parsers/IdentifierParser.js.map +1 -1
- package/dist/esm/parsers/InsertQueryParser.js +104 -0
- package/dist/esm/parsers/InsertQueryParser.js.map +1 -0
- package/dist/esm/parsers/ParameterExpressionParser.js +11 -2
- package/dist/esm/parsers/ParameterExpressionParser.js.map +1 -1
- package/dist/esm/parsers/SelectQueryParser.js +1 -1
- package/dist/esm/parsers/SelectQueryParser.js.map +1 -1
- package/dist/esm/parsers/SourceParser.js +8 -26
- package/dist/esm/parsers/SourceParser.js.map +1 -1
- package/dist/esm/parsers/ValueParser.js +14 -1
- package/dist/esm/parsers/ValueParser.js.map +1 -1
- package/dist/esm/tokenReaders/ParameterTokenReader.js +21 -1
- package/dist/esm/tokenReaders/ParameterTokenReader.js.map +1 -1
- package/dist/esm/transformers/Formatter.js +76 -4
- package/dist/esm/transformers/Formatter.js.map +1 -1
- package/dist/esm/transformers/QueryBuilder.js +170 -0
- package/dist/esm/transformers/QueryBuilder.js.map +1 -0
- package/dist/esm/types/index.d.ts +2 -1
- package/dist/esm/types/models/CreateTableQuery.d.ts +27 -0
- package/dist/esm/types/models/InsertQuery.d.ts +21 -0
- package/dist/esm/types/models/SelectQuery.d.ts +3 -2
- package/dist/esm/types/models/ValueComponent.d.ts +1 -1
- package/dist/esm/types/models/ValuesQuery.d.ts +9 -1
- package/dist/esm/types/parsers/InsertQueryParser.d.ts +17 -0
- package/dist/esm/types/tokenReaders/ParameterTokenReader.d.ts +2 -1
- package/dist/esm/types/transformers/Formatter.d.ts +19 -2
- package/dist/esm/types/transformers/QueryBuilder.d.ts +54 -0
- package/dist/esm/types/utils/extractNamespacesAndName.d.ts +5 -0
- package/dist/esm/types/utils/parseEscapedOrDotSeparatedIdentifiers.d.ts +9 -0
- package/dist/esm/utils/extractNamespacesAndName.js +16 -0
- package/dist/esm/utils/extractNamespacesAndName.js.map +1 -0
- package/dist/esm/utils/parseEscapedOrDotSeparatedIdentifiers.js +39 -0
- package/dist/esm/utils/parseEscapedOrDotSeparatedIdentifiers.js.map +1 -0
- package/dist/index.d.ts +2 -1
- package/dist/index.js +2 -1
- package/dist/index.js.map +1 -1
- package/dist/models/BinarySelectQuery.js +0 -2
- package/dist/models/BinarySelectQuery.js.map +1 -1
- package/dist/models/CreateTableQuery.d.ts +27 -0
- package/dist/models/CreateTableQuery.js +66 -0
- package/dist/models/CreateTableQuery.js.map +1 -0
- package/dist/models/InsertQuery.d.ts +21 -0
- package/dist/models/InsertQuery.js +28 -0
- package/dist/models/InsertQuery.js.map +1 -0
- package/dist/models/SelectQuery.d.ts +3 -2
- package/dist/models/SelectQuery.js +3 -1
- package/dist/models/SelectQuery.js.map +1 -1
- package/dist/models/SimpleSelectQuery.js +2 -2
- package/dist/models/SimpleSelectQuery.js.map +1 -1
- package/dist/models/ValueComponent.d.ts +1 -1
- package/dist/models/ValueComponent.js +9 -1
- package/dist/models/ValueComponent.js.map +1 -1
- package/dist/models/ValuesQuery.d.ts +9 -1
- package/dist/models/ValuesQuery.js +6 -1
- package/dist/models/ValuesQuery.js.map +1 -1
- package/dist/parsers/IdentifierParser.js +3 -2
- package/dist/parsers/IdentifierParser.js.map +1 -1
- package/dist/parsers/InsertQueryParser.d.ts +17 -0
- package/dist/parsers/InsertQueryParser.js +108 -0
- package/dist/parsers/InsertQueryParser.js.map +1 -0
- package/dist/parsers/ParameterExpressionParser.js +11 -2
- package/dist/parsers/ParameterExpressionParser.js.map +1 -1
- package/dist/parsers/SelectQueryParser.js +1 -1
- package/dist/parsers/SelectQueryParser.js.map +1 -1
- package/dist/parsers/SourceParser.js +8 -26
- package/dist/parsers/SourceParser.js.map +1 -1
- package/dist/parsers/ValueParser.js +14 -1
- package/dist/parsers/ValueParser.js.map +1 -1
- package/dist/tokenReaders/ParameterTokenReader.d.ts +2 -1
- package/dist/tokenReaders/ParameterTokenReader.js +21 -1
- package/dist/tokenReaders/ParameterTokenReader.js.map +1 -1
- package/dist/transformers/Formatter.d.ts +19 -2
- package/dist/transformers/Formatter.js +76 -4
- package/dist/transformers/Formatter.js.map +1 -1
- package/dist/transformers/QueryBuilder.d.ts +54 -0
- package/dist/transformers/QueryBuilder.js +174 -0
- package/dist/transformers/QueryBuilder.js.map +1 -0
- package/dist/utils/extractNamespacesAndName.d.ts +5 -0
- package/dist/utils/extractNamespacesAndName.js +18 -0
- package/dist/utils/extractNamespacesAndName.js.map +1 -0
- package/dist/utils/parseEscapedOrDotSeparatedIdentifiers.d.ts +9 -0
- package/dist/utils/parseEscapedOrDotSeparatedIdentifiers.js +42 -0
- package/dist/utils/parseEscapedOrDotSeparatedIdentifiers.js.map +1 -0
- package/package.json +1 -1
- package/dist/esm/transformers/QueryConverter.js +0 -115
- package/dist/esm/transformers/QueryConverter.js.map +0 -1
- package/dist/esm/types/transformers/QueryConverter.d.ts +0 -41
- package/dist/transformers/QueryConverter.d.ts +0 -41
- package/dist/transformers/QueryConverter.js +0 -119
- package/dist/transformers/QueryConverter.js.map +0 -1
@@ -0,0 +1,174 @@
|
|
1
|
+
"use strict";
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
3
|
+
exports.QueryBuilder = void 0;
|
4
|
+
const Clause_1 = require("../models/Clause");
|
5
|
+
const SelectQuery_1 = require("../models/SelectQuery");
|
6
|
+
const ValueComponent_1 = require("../models/ValueComponent");
|
7
|
+
const CTENormalizer_1 = require("./CTENormalizer");
|
8
|
+
const CreateTableQuery_1 = require("../models/CreateTableQuery");
|
9
|
+
const InsertQuery_1 = require("../models/InsertQuery");
|
10
|
+
const SelectValueCollector_1 = require("./SelectValueCollector");
|
11
|
+
/**
|
12
|
+
* QueryBuilder provides static methods to build or convert various SQL query objects.
|
13
|
+
*/
|
14
|
+
class QueryBuilder {
|
15
|
+
/**
|
16
|
+
* Builds a BinarySelectQuery by combining an array of SelectQuery using the specified operator.
|
17
|
+
* Throws if less than two queries are provided.
|
18
|
+
* @param queries Array of SelectQuery to combine
|
19
|
+
* @param operator SQL operator to use (e.g. 'union', 'union all', 'intersect', 'except')
|
20
|
+
* @returns BinarySelectQuery
|
21
|
+
*/
|
22
|
+
static buildBinaryQuery(queries, operator) {
|
23
|
+
if (!queries || queries.length === 0) {
|
24
|
+
throw new Error("No queries provided to combine.");
|
25
|
+
}
|
26
|
+
if (queries.length === 1) {
|
27
|
+
throw new Error("At least two queries are required to create a BinarySelectQuery.");
|
28
|
+
}
|
29
|
+
// Always create a new BinarySelectQuery instance (never mutate input)
|
30
|
+
const wrap = (q) => q instanceof SelectQuery_1.ValuesQuery ? QueryBuilder.buildSimpleQuery(q) : q;
|
31
|
+
let result = new SelectQuery_1.BinarySelectQuery(wrap(queries[0]), operator, wrap(queries[1]));
|
32
|
+
CTENormalizer_1.CTENormalizer.normalize(result);
|
33
|
+
for (let i = 2; i < queries.length; i++) {
|
34
|
+
result.appendSelectQuery(operator, wrap(queries[i]));
|
35
|
+
}
|
36
|
+
return result;
|
37
|
+
}
|
38
|
+
constructor() {
|
39
|
+
// This class is not meant to be instantiated.
|
40
|
+
}
|
41
|
+
/**
|
42
|
+
* Converts a SELECT query to a standard SimpleSelectQuery form.
|
43
|
+
* @param query The query to convert
|
44
|
+
* @returns A SimpleSelectQuery
|
45
|
+
*/
|
46
|
+
static buildSimpleQuery(query) {
|
47
|
+
if (query instanceof SelectQuery_1.SimpleSelectQuery) {
|
48
|
+
return query;
|
49
|
+
}
|
50
|
+
else if (query instanceof SelectQuery_1.BinarySelectQuery) {
|
51
|
+
return QueryBuilder.buildSimpleBinaryQuery(query);
|
52
|
+
}
|
53
|
+
else if (query instanceof SelectQuery_1.ValuesQuery) {
|
54
|
+
return QueryBuilder.buildSimpleValuesQuery(query);
|
55
|
+
}
|
56
|
+
throw new Error("Unsupported query type for buildSimpleQuery");
|
57
|
+
}
|
58
|
+
static buildSimpleBinaryQuery(query) {
|
59
|
+
// Create a subquery source from the binary query
|
60
|
+
const subQuerySource = new Clause_1.SubQuerySource(query);
|
61
|
+
// Create a source expression with alias
|
62
|
+
const sourceExpr = new Clause_1.SourceExpression(subQuerySource, new Clause_1.SourceAliasExpression("bq", null));
|
63
|
+
// Create FROM clause with the source expression
|
64
|
+
const fromClause = new Clause_1.FromClause(sourceExpr, null);
|
65
|
+
// Create SELECT clause with * (all columns)
|
66
|
+
const selectClause = QueryBuilder.createSelectAllClause();
|
67
|
+
// Create the final simple select query
|
68
|
+
const q = new SelectQuery_1.SimpleSelectQuery(null, // No WITH clause
|
69
|
+
selectClause, fromClause, null, // No WHERE
|
70
|
+
null, // No GROUP BY
|
71
|
+
null, // No HAVING
|
72
|
+
null, // No ORDER BY
|
73
|
+
null, // No WINDOW
|
74
|
+
null, // No LIMIT
|
75
|
+
null // No FOR
|
76
|
+
);
|
77
|
+
return CTENormalizer_1.CTENormalizer.normalize(q);
|
78
|
+
}
|
79
|
+
/**
|
80
|
+
* Converts a ValuesQuery to a SimpleSelectQuery with sequentially numbered columns or user-specified columns
|
81
|
+
*
|
82
|
+
* @param query The VALUES query to convert
|
83
|
+
* @param columns Optional: column names
|
84
|
+
* @returns A SimpleSelectQuery
|
85
|
+
*/
|
86
|
+
static buildSimpleValuesQuery(query) {
|
87
|
+
// Figure out how many columns are in the VALUES clause
|
88
|
+
const columnCount = query.tuples.length > 0 ? query.tuples[0].values.length : 0;
|
89
|
+
if (query.tuples.length === 0) {
|
90
|
+
throw new Error("Empty VALUES clause cannot be converted to a SimpleSelectQuery");
|
91
|
+
}
|
92
|
+
if (!query.columnAliases) {
|
93
|
+
throw new Error("Column aliases are required to convert a VALUES clause to SimpleSelectQuery. Please specify column aliases.");
|
94
|
+
}
|
95
|
+
if (query.columnAliases.length !== columnCount) {
|
96
|
+
throw new Error(`The number of column aliases (${query.columnAliases.length}) does not match the number of columns in the first tuple (${columnCount}).`);
|
97
|
+
}
|
98
|
+
// Create a subquery source from the VALUES query
|
99
|
+
const subQuerySource = new Clause_1.SubQuerySource(query);
|
100
|
+
const sourceExpr = new Clause_1.SourceExpression(subQuerySource, new Clause_1.SourceAliasExpression("vq", query.columnAliases));
|
101
|
+
// Create FROM clause with the source expression
|
102
|
+
const fromClause = new Clause_1.FromClause(sourceExpr, null);
|
103
|
+
// Create SELECT clause with all columns
|
104
|
+
const selectItems = query.columnAliases.map(name => new Clause_1.SelectItem(new ValueComponent_1.ColumnReference("vq", name), name));
|
105
|
+
const selectClause = new Clause_1.SelectClause(selectItems, null);
|
106
|
+
// Create the final simple select query
|
107
|
+
return new SelectQuery_1.SimpleSelectQuery(null, // No WITH clause
|
108
|
+
selectClause, fromClause, null, // No WHERE
|
109
|
+
null, // No GROUP BY
|
110
|
+
null, // No HAVING
|
111
|
+
null, // No ORDER BY
|
112
|
+
null, // No WINDOW
|
113
|
+
null, // No LIMIT
|
114
|
+
null // No FOR
|
115
|
+
);
|
116
|
+
}
|
117
|
+
/**
|
118
|
+
* Creates a SELECT clause with a single * (all columns) item
|
119
|
+
*
|
120
|
+
* @returns A SELECT clause with *
|
121
|
+
*/
|
122
|
+
static createSelectAllClause() {
|
123
|
+
// Create a column reference for *
|
124
|
+
const columnRef = new ValueComponent_1.ColumnReference(null, "*");
|
125
|
+
// Create a SelectItem with the column reference
|
126
|
+
const selectItem = new Clause_1.SelectItem(columnRef, "*");
|
127
|
+
// Create and return a SelectClause with the item
|
128
|
+
return new Clause_1.SelectClause([selectItem], null);
|
129
|
+
}
|
130
|
+
/**
|
131
|
+
* Converts a SELECT query to a CREATE TABLE query (CREATE [TEMPORARY] TABLE ... AS SELECT ...)
|
132
|
+
* @param query The SELECT query to use as the source
|
133
|
+
* @param tableName The name of the table to create
|
134
|
+
* @param isTemporary If true, creates a temporary table
|
135
|
+
* @returns A CreateTableQuery instance
|
136
|
+
*/
|
137
|
+
static buildCreateTableQuery(query, tableName, isTemporary = false) {
|
138
|
+
return new CreateTableQuery_1.CreateTableQuery({
|
139
|
+
tableName,
|
140
|
+
isTemporary,
|
141
|
+
asSelectQuery: query
|
142
|
+
});
|
143
|
+
}
|
144
|
+
/**
|
145
|
+
* Converts a SELECT query to an INSERT query (INSERT INTO ... SELECT ...)
|
146
|
+
* @param selectQuery The SELECT query to use as the source
|
147
|
+
* @param tableName The name of the table to insert into
|
148
|
+
* @param columns Optional: array of column names. If omitted, columns are inferred from the selectQuery
|
149
|
+
* @returns An InsertQuery instance
|
150
|
+
*/
|
151
|
+
static buildInsertQuery(selectQuery, tableName) {
|
152
|
+
let cols;
|
153
|
+
const count = selectQuery.selectClause.items.length;
|
154
|
+
// Try to infer columns from the selectQuery
|
155
|
+
const collector = new SelectValueCollector_1.SelectValueCollector();
|
156
|
+
const items = collector.collect(selectQuery);
|
157
|
+
cols = items.map(item => item.name);
|
158
|
+
if (!cols.length || count !== cols.length) {
|
159
|
+
throw new Error(`Columns cannot be inferred from the selectQuery. ` +
|
160
|
+
`Make sure you are not using wildcards or unnamed columns.\n` +
|
161
|
+
`Select clause column count: ${count}, ` +
|
162
|
+
`Columns with valid names: ${cols.length}\n` +
|
163
|
+
`Detected column names: [${cols.join(", ")}]`);
|
164
|
+
}
|
165
|
+
return new InsertQuery_1.InsertQuery({
|
166
|
+
namespaces: null,
|
167
|
+
table: tableName,
|
168
|
+
columns: cols,
|
169
|
+
selectQuery: selectQuery
|
170
|
+
});
|
171
|
+
}
|
172
|
+
}
|
173
|
+
exports.QueryBuilder = QueryBuilder;
|
174
|
+
//# sourceMappingURL=QueryBuilder.js.map
|
@@ -0,0 +1 @@
|
|
1
|
+
{"version":3,"file":"QueryBuilder.js","sourceRoot":"","sources":["../../src/transformers/QueryBuilder.ts"],"names":[],"mappings":";;;AAAA,6CAA6I;AAC7I,uDAAuG;AAEvG,6DAAwF;AAExF,mDAAgD;AAChD,iEAA8D;AAC9D,uDAAoD;AACpD,iEAA8D;AAE9D;;GAEG;AACH,MAAa,YAAY;IACrB;;;;;;OAMG;IACI,MAAM,CAAC,gBAAgB,CAAC,OAAsB,EAAE,QAAgB;QACnE,IAAI,CAAC,OAAO,IAAI,OAAO,CAAC,MAAM,KAAK,CAAC,EAAE,CAAC;YACnC,MAAM,IAAI,KAAK,CAAC,iCAAiC,CAAC,CAAC;QACvD,CAAC;QACD,IAAI,OAAO,CAAC,MAAM,KAAK,CAAC,EAAE,CAAC;YACvB,MAAM,IAAI,KAAK,CAAC,kEAAkE,CAAC,CAAC;QACxF,CAAC;QAED,sEAAsE;QACtE,MAAM,IAAI,GAAG,CAAC,CAAc,EAAE,EAAE,CAAC,CAAC,YAAY,yBAAW,CAAC,CAAC,CAAC,YAAY,CAAC,gBAAgB,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC;QACjG,IAAI,MAAM,GAAsB,IAAI,+BAAiB,CAAC,IAAI,CAAC,OAAO,CAAC,CAAC,CAAC,CAAC,EAAE,QAAQ,EAAE,IAAI,CAAC,OAAO,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC;QACpG,6BAAa,CAAC,SAAS,CAAC,MAAM,CAAC,CAAC;QAEhC,KAAK,IAAI,CAAC,GAAG,CA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G,SAAS,CAAC,OAAO,CAAC,WAAW,CAAC,CAAC;QAC7C,IAAI,GAAG,KAAK,CAAC,GAAG,CAAC,IAAI,CAAC,EAAE,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC;QACpC,IAAI,CAAC,IAAI,CAAC,MAAM,IAAI,KAAK,KAAK,IAAI,CAAC,MAAM,EAAE,CAAC;YACxC,MAAM,IAAI,KAAK,CACX,mDAAmD;gBACnD,6DAA6D;gBAC7D,+BAA+B,KAAK,IAAI;gBACxC,6BAA6B,IAAI,CAAC,MAAM,IAAI;gBAC5C,2BAA2B,IAAI,CAAC,IAAI,CAAC,IAAI,CAAC,GAAG,CAChD,CAAC;QACN,CAAC;QAED,OAAO,IAAI,yBAAW,CAAC;YACnB,UAAU,EAAE,IAAI;YAChB,KAAK,EAAE,SAAS;YAChB,OAAO,EAAE,IAAI;YACb,WAAW,EAAE,WAAW;SAC3B,CAAC,CAAC;IACP,CAAC;CACJ;AApMD,oCAoMC"}
|
@@ -0,0 +1,18 @@
|
|
1
|
+
"use strict";
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
3
|
+
exports.extractNamespacesAndName = extractNamespacesAndName;
|
4
|
+
// Utility to extract namespaces and the final name from an array of identifiers
|
5
|
+
// Example: ["db", "schema", "users"] => { namespaces: ["db", "schema"], name: "users" }
|
6
|
+
function extractNamespacesAndName(identifiers) {
|
7
|
+
if (!identifiers || identifiers.length === 0) {
|
8
|
+
throw new Error("Identifier list is empty");
|
9
|
+
}
|
10
|
+
if (identifiers.length === 1) {
|
11
|
+
return { namespaces: null, name: identifiers[0] };
|
12
|
+
}
|
13
|
+
return {
|
14
|
+
namespaces: identifiers.slice(0, -1),
|
15
|
+
name: identifiers[identifiers.length - 1]
|
16
|
+
};
|
17
|
+
}
|
18
|
+
//# sourceMappingURL=extractNamespacesAndName.js.map
|
@@ -0,0 +1 @@
|
|
1
|
+
{"version":3,"file":"extractNamespacesAndName.js","sourceRoot":"","sources":["../../src/utils/extractNamespacesAndName.ts"],"names":[],"mappings":";;AAeS,4DAAwB;AAfjC,gFAAgF;AAChF,wFAAwF;AACxF,SAAS,wBAAwB,CAAC,WAAqB;IACnD,IAAI,CAAC,WAAW,IAAI,WAAW,CAAC,MAAM,KAAK,CAAC,EAAE,CAAC;QAC3C,MAAM,IAAI,KAAK,CAAC,0BAA0B,CAAC,CAAC;IAChD,CAAC;IACD,IAAI,WAAW,CAAC,MAAM,KAAK,CAAC,EAAE,CAAC;QAC3B,OAAO,EAAE,UAAU,EAAE,IAAI,EAAE,IAAI,EAAE,WAAW,CAAC,CAAC,CAAC,EAAE,CAAC;IACtD,CAAC;IACD,OAAO;QACH,UAAU,EAAE,WAAW,CAAC,KAAK,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC;QACpC,IAAI,EAAE,WAAW,CAAC,WAAW,CAAC,MAAM,GAAG,CAAC,CAAC;KAC5C,CAAC;AACN,CAAC"}
|
@@ -0,0 +1,9 @@
|
|
1
|
+
import { Lexeme } from "../models/Lexeme";
|
2
|
+
/**
|
3
|
+
* Parses SQL Server-style escaped identifiers ([table]) and dot-separated identifiers.
|
4
|
+
* Returns the list of identifiers and the new index after parsing.
|
5
|
+
*/
|
6
|
+
export declare function parseEscapedOrDotSeparatedIdentifiers(lexemes: Lexeme[], index: number): {
|
7
|
+
identifiers: string[];
|
8
|
+
newIndex: number;
|
9
|
+
};
|
@@ -0,0 +1,42 @@
|
|
1
|
+
"use strict";
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
3
|
+
exports.parseEscapedOrDotSeparatedIdentifiers = parseEscapedOrDotSeparatedIdentifiers;
|
4
|
+
const Lexeme_1 = require("../models/Lexeme");
|
5
|
+
/**
|
6
|
+
* Parses SQL Server-style escaped identifiers ([table]) and dot-separated identifiers.
|
7
|
+
* Returns the list of identifiers and the new index after parsing.
|
8
|
+
*/
|
9
|
+
function parseEscapedOrDotSeparatedIdentifiers(lexemes, index) {
|
10
|
+
let idx = index;
|
11
|
+
const identifiers = [];
|
12
|
+
while (idx < lexemes.length) {
|
13
|
+
if (lexemes[idx].type & Lexeme_1.TokenType.OpenBracket) {
|
14
|
+
idx++; // skip [
|
15
|
+
if (idx >= lexemes.length || !(lexemes[idx].type & Lexeme_1.TokenType.Identifier)) {
|
16
|
+
throw new Error(`Expected identifier after '[' at position ${idx}`);
|
17
|
+
}
|
18
|
+
identifiers.push(lexemes[idx].value);
|
19
|
+
idx++;
|
20
|
+
if (idx >= lexemes.length || lexemes[idx].value !== "]") {
|
21
|
+
throw new Error(`Expected closing ']' after identifier at position ${idx}`);
|
22
|
+
}
|
23
|
+
idx++; // skip ]
|
24
|
+
}
|
25
|
+
else if (lexemes[idx].type & Lexeme_1.TokenType.Identifier) {
|
26
|
+
identifiers.push(lexemes[idx].value);
|
27
|
+
idx++;
|
28
|
+
}
|
29
|
+
else {
|
30
|
+
break;
|
31
|
+
}
|
32
|
+
// Handle dot for schema.table or db.schema.table
|
33
|
+
if (idx < lexemes.length && (lexemes[idx].type & Lexeme_1.TokenType.Dot)) {
|
34
|
+
idx++; // skip dot
|
35
|
+
}
|
36
|
+
else {
|
37
|
+
break;
|
38
|
+
}
|
39
|
+
}
|
40
|
+
return { identifiers, newIndex: idx };
|
41
|
+
}
|
42
|
+
//# sourceMappingURL=parseEscapedOrDotSeparatedIdentifiers.js.map
|
@@ -0,0 +1 @@
|
|
1
|
+
{"version":3,"file":"parseEscapedOrDotSeparatedIdentifiers.js","sourceRoot":"","sources":["../../src/utils/parseEscapedOrDotSeparatedIdentifiers.ts"],"names":[],"mappings":";;AAMA,sFA6BC;AAnCD,6CAAqD;AAErD;;;GAGG;AACH,SAAgB,qCAAqC,CAAC,OAAiB,EAAE,KAAa;IAClF,IAAI,GAAG,GAAG,KAAK,CAAC;IAChB,MAAM,WAAW,GAAa,EAAE,CAAC;IACjC,OAAO,GAAG,GAAG,OAAO,CAAC,MAAM,EAAE,CAAC;QAC1B,IAAI,OAAO,CAAC,GAAG,CAAC,CAAC,IAA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|
"name": "rawsql-ts",
|
3
|
-
"version": "0.
|
3
|
+
"version": "0.4.0-beta",
|
4
4
|
"description": "[beta]High-performance SQL parser and AST analyzer written in TypeScript. Provides fast parsing and advanced transformation capabilities.",
|
5
5
|
"main": "dist/index.js",
|
6
6
|
"module": "dist/esm/index.js",
|
@@ -1,115 +0,0 @@
|
|
1
|
-
import { FromClause, SelectClause, SelectItem, SourceAliasExpression, SourceExpression, SubQuerySource } from "../models/Clause";
|
2
|
-
import { BinarySelectQuery, SimpleSelectQuery, ValuesQuery } from "../models/SelectQuery";
|
3
|
-
import { ColumnReference } from "../models/ValueComponent";
|
4
|
-
import { CTENormalizer } from "./CTENormalizer";
|
5
|
-
/**
|
6
|
-
* Converts various SELECT query types to a standard SimpleSelectQuery format.
|
7
|
-
* - SimpleSelectQuery is returned as-is
|
8
|
-
* - BinarySelectQuery (UNION, etc.) is wrapped in a subquery: SELECT * FROM (original) AS bq
|
9
|
-
* - ValuesQuery is wrapped with sequentially numbered columns: SELECT * FROM (original) AS vq(column1, column2, ...)
|
10
|
-
*/
|
11
|
-
export class QueryConverter {
|
12
|
-
/**
|
13
|
-
* Private constructor to prevent instantiation of this utility class.
|
14
|
-
*/
|
15
|
-
constructor() {
|
16
|
-
// This class is not meant to be instantiated.
|
17
|
-
}
|
18
|
-
/**
|
19
|
-
* Converts a SELECT query to a standard SimpleSelectQuery form.
|
20
|
-
*
|
21
|
-
* @param query The query to convert
|
22
|
-
* @returns A SimpleSelectQuery
|
23
|
-
*/
|
24
|
-
static toSimple(query) {
|
25
|
-
if (query instanceof SimpleSelectQuery) {
|
26
|
-
// Already a simple query, just return it
|
27
|
-
return query;
|
28
|
-
}
|
29
|
-
else if (query instanceof BinarySelectQuery) {
|
30
|
-
// Convert binary queries to a simple query
|
31
|
-
return QueryConverter.toSimpleBinaryQuery(query);
|
32
|
-
}
|
33
|
-
else if (query instanceof ValuesQuery) {
|
34
|
-
// Convert VALUES queries to a simple query
|
35
|
-
return QueryConverter.toSimpleValuesQuery(query);
|
36
|
-
}
|
37
|
-
// Should not reach here with current type system
|
38
|
-
throw new Error("Unsupported query type for toSimple");
|
39
|
-
}
|
40
|
-
/**
|
41
|
-
* Converts a BinarySelectQuery (UNION, EXCEPT, etc.) to a SimpleSelectQuery
|
42
|
-
* by wrapping it in SELECT * FROM (original) AS bq
|
43
|
-
*
|
44
|
-
* @param query The binary query to convert
|
45
|
-
* @returns A SimpleSelectQuery
|
46
|
-
*/
|
47
|
-
static toSimpleBinaryQuery(query) {
|
48
|
-
// Create a subquery source from the binary query
|
49
|
-
const subQuerySource = new SubQuerySource(query);
|
50
|
-
// Create a source expression with alias
|
51
|
-
const sourceExpr = new SourceExpression(subQuerySource, new SourceAliasExpression("bq", null));
|
52
|
-
// Create FROM clause with the source expression
|
53
|
-
const fromClause = new FromClause(sourceExpr, null);
|
54
|
-
// Create SELECT clause with * (all columns)
|
55
|
-
const selectClause = QueryConverter.createSelectAllClause();
|
56
|
-
// Create the final simple select query
|
57
|
-
const q = new SimpleSelectQuery(null, // No WITH clause
|
58
|
-
selectClause, fromClause, null, // No WHERE
|
59
|
-
null, // No GROUP BY
|
60
|
-
null, // No HAVING
|
61
|
-
null, // No ORDER BY
|
62
|
-
null, // No WINDOW
|
63
|
-
null, // No LIMIT
|
64
|
-
null // No FOR
|
65
|
-
);
|
66
|
-
return CTENormalizer.normalize(q);
|
67
|
-
}
|
68
|
-
/**
|
69
|
-
* Converts a ValuesQuery to a SimpleSelectQuery with sequentially numbered columns
|
70
|
-
*
|
71
|
-
* @param query The VALUES query to convert
|
72
|
-
* @returns A SimpleSelectQuery
|
73
|
-
*/
|
74
|
-
static toSimpleValuesQuery(query) {
|
75
|
-
// Figure out how many columns are in the VALUES clause
|
76
|
-
// by checking the first tuple (if available)
|
77
|
-
const columnCount = query.tuples.length > 0 ? query.tuples[0].values.length : 0;
|
78
|
-
// Generate column names (column1, column2, ...)
|
79
|
-
const columnNames = [];
|
80
|
-
for (let i = 1; i <= columnCount; i++) {
|
81
|
-
columnNames.push(`column${i}`);
|
82
|
-
}
|
83
|
-
// Create a subquery source from the VALUES query
|
84
|
-
const subQuerySource = new SubQuerySource(query);
|
85
|
-
const sourceExpr = new SourceExpression(subQuerySource, new SourceAliasExpression("vq", columnNames));
|
86
|
-
// Create FROM clause with the source expression
|
87
|
-
const fromClause = new FromClause(sourceExpr, null);
|
88
|
-
// Create SELECT clause with * (all columns)
|
89
|
-
const selectClause = QueryConverter.createSelectAllClause();
|
90
|
-
// Create the final simple select query
|
91
|
-
return new SimpleSelectQuery(null, // No WITH clause
|
92
|
-
selectClause, fromClause, null, // No WHERE
|
93
|
-
null, // No GROUP BY
|
94
|
-
null, // No HAVING
|
95
|
-
null, // No ORDER BY
|
96
|
-
null, // No WINDOW
|
97
|
-
null, // No LIMIT
|
98
|
-
null // No FOR
|
99
|
-
);
|
100
|
-
}
|
101
|
-
/**
|
102
|
-
* Creates a SELECT clause with a single * (all columns) item
|
103
|
-
*
|
104
|
-
* @returns A SELECT clause with *
|
105
|
-
*/
|
106
|
-
static createSelectAllClause() {
|
107
|
-
// Create a column reference for *
|
108
|
-
const columnRef = new ColumnReference(null, "*");
|
109
|
-
// Create a SelectItem with the column reference
|
110
|
-
const selectItem = new SelectItem(columnRef, "*");
|
111
|
-
// Create and return a SelectClause with the item
|
112
|
-
return new SelectClause([selectItem], null);
|
113
|
-
}
|
114
|
-
}
|
115
|
-
//# sourceMappingURL=QueryConverter.js.map
|
@@ -1 +0,0 @@
|
|
1
|
-
{"version":3,"file":"QueryConverter.js","sourceRoot":"","sources":["../../../src/transformers/QueryConverter.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,EAAE,YAAY,EAAE,UAAU,EAAE,qBAAqB,EAAE,gBAAgB,EAAE,cAAc,EAAc,MAAM,kBAAkB,CAAC;AAC7I,OAAO,EAAE,iBAAiB,EAAe,iBAAiB,EAAE,WAAW,EAAE,MAAM,uBAAuB,CAAC;AAEvG,OAAO,EAAE,eAAe,EA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W;QACjB,IAAI,CAAE,SAAS;SAClB,CAAC;IACN,CAAC;IAED;;;;OAIG;IACK,MAAM,CAAC,qBAAqB;QAChC,kCAAkC;QAClC,MAAM,SAAS,GAAG,IAAI,eAAe,CAAC,IAAI,EAAE,GAAG,CAAC,CAAC;QAEjD,gDAAgD;QAChD,MAAM,UAAU,GAAG,IAAI,UAAU,CAAC,SAAS,EAAE,GAAG,CAAC,CAAC;QAElD,iDAAiD;QACjD,OAAO,IAAI,YAAY,CAAC,CAAC,UAAU,CAAC,EAAE,IAAI,CAAC,CAAC;IAChD,CAAC;CACJ"}
|
@@ -1,41 +0,0 @@
|
|
1
|
-
import { SelectQuery, SimpleSelectQuery } from "../models/SelectQuery";
|
2
|
-
/**
|
3
|
-
* Converts various SELECT query types to a standard SimpleSelectQuery format.
|
4
|
-
* - SimpleSelectQuery is returned as-is
|
5
|
-
* - BinarySelectQuery (UNION, etc.) is wrapped in a subquery: SELECT * FROM (original) AS bq
|
6
|
-
* - ValuesQuery is wrapped with sequentially numbered columns: SELECT * FROM (original) AS vq(column1, column2, ...)
|
7
|
-
*/
|
8
|
-
export declare class QueryConverter {
|
9
|
-
/**
|
10
|
-
* Private constructor to prevent instantiation of this utility class.
|
11
|
-
*/
|
12
|
-
private constructor();
|
13
|
-
/**
|
14
|
-
* Converts a SELECT query to a standard SimpleSelectQuery form.
|
15
|
-
*
|
16
|
-
* @param query The query to convert
|
17
|
-
* @returns A SimpleSelectQuery
|
18
|
-
*/
|
19
|
-
static toSimple(query: SelectQuery): SimpleSelectQuery;
|
20
|
-
/**
|
21
|
-
* Converts a BinarySelectQuery (UNION, EXCEPT, etc.) to a SimpleSelectQuery
|
22
|
-
* by wrapping it in SELECT * FROM (original) AS bq
|
23
|
-
*
|
24
|
-
* @param query The binary query to convert
|
25
|
-
* @returns A SimpleSelectQuery
|
26
|
-
*/
|
27
|
-
private static toSimpleBinaryQuery;
|
28
|
-
/**
|
29
|
-
* Converts a ValuesQuery to a SimpleSelectQuery with sequentially numbered columns
|
30
|
-
*
|
31
|
-
* @param query The VALUES query to convert
|
32
|
-
* @returns A SimpleSelectQuery
|
33
|
-
*/
|
34
|
-
private static toSimpleValuesQuery;
|
35
|
-
/**
|
36
|
-
* Creates a SELECT clause with a single * (all columns) item
|
37
|
-
*
|
38
|
-
* @returns A SELECT clause with *
|
39
|
-
*/
|
40
|
-
private static createSelectAllClause;
|
41
|
-
}
|
@@ -1,41 +0,0 @@
|
|
1
|
-
import { SelectQuery, SimpleSelectQuery } from "../models/SelectQuery";
|
2
|
-
/**
|
3
|
-
* Converts various SELECT query types to a standard SimpleSelectQuery format.
|
4
|
-
* - SimpleSelectQuery is returned as-is
|
5
|
-
* - BinarySelectQuery (UNION, etc.) is wrapped in a subquery: SELECT * FROM (original) AS bq
|
6
|
-
* - ValuesQuery is wrapped with sequentially numbered columns: SELECT * FROM (original) AS vq(column1, column2, ...)
|
7
|
-
*/
|
8
|
-
export declare class QueryConverter {
|
9
|
-
/**
|
10
|
-
* Private constructor to prevent instantiation of this utility class.
|
11
|
-
*/
|
12
|
-
private constructor();
|
13
|
-
/**
|
14
|
-
* Converts a SELECT query to a standard SimpleSelectQuery form.
|
15
|
-
*
|
16
|
-
* @param query The query to convert
|
17
|
-
* @returns A SimpleSelectQuery
|
18
|
-
*/
|
19
|
-
static toSimple(query: SelectQuery): SimpleSelectQuery;
|
20
|
-
/**
|
21
|
-
* Converts a BinarySelectQuery (UNION, EXCEPT, etc.) to a SimpleSelectQuery
|
22
|
-
* by wrapping it in SELECT * FROM (original) AS bq
|
23
|
-
*
|
24
|
-
* @param query The binary query to convert
|
25
|
-
* @returns A SimpleSelectQuery
|
26
|
-
*/
|
27
|
-
private static toSimpleBinaryQuery;
|
28
|
-
/**
|
29
|
-
* Converts a ValuesQuery to a SimpleSelectQuery with sequentially numbered columns
|
30
|
-
*
|
31
|
-
* @param query The VALUES query to convert
|
32
|
-
* @returns A SimpleSelectQuery
|
33
|
-
*/
|
34
|
-
private static toSimpleValuesQuery;
|
35
|
-
/**
|
36
|
-
* Creates a SELECT clause with a single * (all columns) item
|
37
|
-
*
|
38
|
-
* @returns A SELECT clause with *
|
39
|
-
*/
|
40
|
-
private static createSelectAllClause;
|
41
|
-
}
|
@@ -1,119 +0,0 @@
|
|
1
|
-
"use strict";
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
3
|
-
exports.QueryConverter = void 0;
|
4
|
-
const Clause_1 = require("../models/Clause");
|
5
|
-
const SelectQuery_1 = require("../models/SelectQuery");
|
6
|
-
const ValueComponent_1 = require("../models/ValueComponent");
|
7
|
-
const CTENormalizer_1 = require("./CTENormalizer");
|
8
|
-
/**
|
9
|
-
* Converts various SELECT query types to a standard SimpleSelectQuery format.
|
10
|
-
* - SimpleSelectQuery is returned as-is
|
11
|
-
* - BinarySelectQuery (UNION, etc.) is wrapped in a subquery: SELECT * FROM (original) AS bq
|
12
|
-
* - ValuesQuery is wrapped with sequentially numbered columns: SELECT * FROM (original) AS vq(column1, column2, ...)
|
13
|
-
*/
|
14
|
-
class QueryConverter {
|
15
|
-
/**
|
16
|
-
* Private constructor to prevent instantiation of this utility class.
|
17
|
-
*/
|
18
|
-
constructor() {
|
19
|
-
// This class is not meant to be instantiated.
|
20
|
-
}
|
21
|
-
/**
|
22
|
-
* Converts a SELECT query to a standard SimpleSelectQuery form.
|
23
|
-
*
|
24
|
-
* @param query The query to convert
|
25
|
-
* @returns A SimpleSelectQuery
|
26
|
-
*/
|
27
|
-
static toSimple(query) {
|
28
|
-
if (query instanceof SelectQuery_1.SimpleSelectQuery) {
|
29
|
-
// Already a simple query, just return it
|
30
|
-
return query;
|
31
|
-
}
|
32
|
-
else if (query instanceof SelectQuery_1.BinarySelectQuery) {
|
33
|
-
// Convert binary queries to a simple query
|
34
|
-
return QueryConverter.toSimpleBinaryQuery(query);
|
35
|
-
}
|
36
|
-
else if (query instanceof SelectQuery_1.ValuesQuery) {
|
37
|
-
// Convert VALUES queries to a simple query
|
38
|
-
return QueryConverter.toSimpleValuesQuery(query);
|
39
|
-
}
|
40
|
-
// Should not reach here with current type system
|
41
|
-
throw new Error("Unsupported query type for toSimple");
|
42
|
-
}
|
43
|
-
/**
|
44
|
-
* Converts a BinarySelectQuery (UNION, EXCEPT, etc.) to a SimpleSelectQuery
|
45
|
-
* by wrapping it in SELECT * FROM (original) AS bq
|
46
|
-
*
|
47
|
-
* @param query The binary query to convert
|
48
|
-
* @returns A SimpleSelectQuery
|
49
|
-
*/
|
50
|
-
static toSimpleBinaryQuery(query) {
|
51
|
-
// Create a subquery source from the binary query
|
52
|
-
const subQuerySource = new Clause_1.SubQuerySource(query);
|
53
|
-
// Create a source expression with alias
|
54
|
-
const sourceExpr = new Clause_1.SourceExpression(subQuerySource, new Clause_1.SourceAliasExpression("bq", null));
|
55
|
-
// Create FROM clause with the source expression
|
56
|
-
const fromClause = new Clause_1.FromClause(sourceExpr, null);
|
57
|
-
// Create SELECT clause with * (all columns)
|
58
|
-
const selectClause = QueryConverter.createSelectAllClause();
|
59
|
-
// Create the final simple select query
|
60
|
-
const q = new SelectQuery_1.SimpleSelectQuery(null, // No WITH clause
|
61
|
-
selectClause, fromClause, null, // No WHERE
|
62
|
-
null, // No GROUP BY
|
63
|
-
null, // No HAVING
|
64
|
-
null, // No ORDER BY
|
65
|
-
null, // No WINDOW
|
66
|
-
null, // No LIMIT
|
67
|
-
null // No FOR
|
68
|
-
);
|
69
|
-
return CTENormalizer_1.CTENormalizer.normalize(q);
|
70
|
-
}
|
71
|
-
/**
|
72
|
-
* Converts a ValuesQuery to a SimpleSelectQuery with sequentially numbered columns
|
73
|
-
*
|
74
|
-
* @param query The VALUES query to convert
|
75
|
-
* @returns A SimpleSelectQuery
|
76
|
-
*/
|
77
|
-
static toSimpleValuesQuery(query) {
|
78
|
-
// Figure out how many columns are in the VALUES clause
|
79
|
-
// by checking the first tuple (if available)
|
80
|
-
const columnCount = query.tuples.length > 0 ? query.tuples[0].values.length : 0;
|
81
|
-
// Generate column names (column1, column2, ...)
|
82
|
-
const columnNames = [];
|
83
|
-
for (let i = 1; i <= columnCount; i++) {
|
84
|
-
columnNames.push(`column${i}`);
|
85
|
-
}
|
86
|
-
// Create a subquery source from the VALUES query
|
87
|
-
const subQuerySource = new Clause_1.SubQuerySource(query);
|
88
|
-
const sourceExpr = new Clause_1.SourceExpression(subQuerySource, new Clause_1.SourceAliasExpression("vq", columnNames));
|
89
|
-
// Create FROM clause with the source expression
|
90
|
-
const fromClause = new Clause_1.FromClause(sourceExpr, null);
|
91
|
-
// Create SELECT clause with * (all columns)
|
92
|
-
const selectClause = QueryConverter.createSelectAllClause();
|
93
|
-
// Create the final simple select query
|
94
|
-
return new SelectQuery_1.SimpleSelectQuery(null, // No WITH clause
|
95
|
-
selectClause, fromClause, null, // No WHERE
|
96
|
-
null, // No GROUP BY
|
97
|
-
null, // No HAVING
|
98
|
-
null, // No ORDER BY
|
99
|
-
null, // No WINDOW
|
100
|
-
null, // No LIMIT
|
101
|
-
null // No FOR
|
102
|
-
);
|
103
|
-
}
|
104
|
-
/**
|
105
|
-
* Creates a SELECT clause with a single * (all columns) item
|
106
|
-
*
|
107
|
-
* @returns A SELECT clause with *
|
108
|
-
*/
|
109
|
-
static createSelectAllClause() {
|
110
|
-
// Create a column reference for *
|
111
|
-
const columnRef = new ValueComponent_1.ColumnReference(null, "*");
|
112
|
-
// Create a SelectItem with the column reference
|
113
|
-
const selectItem = new Clause_1.SelectItem(columnRef, "*");
|
114
|
-
// Create and return a SelectClause with the item
|
115
|
-
return new Clause_1.SelectClause([selectItem], null);
|
116
|
-
}
|
117
|
-
}
|
118
|
-
exports.QueryConverter = QueryConverter;
|
119
|
-
//# sourceMappingURL=QueryConverter.js.map
|
@@ -1 +0,0 @@
|
|
1
|
-
{"version":3,"file":"QueryConverter.js","sourceRoot":"","sources":["../../src/transformers/QueryConverter.ts"],"names":[],"mappings":";;;AAAA,6CAA6I;AAC7I,uDAAuG;AAEvG,6DAAwF;AAExF,mDAAgD;AAEhD;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