@@ -8,9 +8,13 @@ export interface SchemaQueryParams {
8
8
9
9
export type TemplateFn = ( params ?: SchemaQueryParams ) => string ;
10
10
11
+ function normalizeParameter ( param : string ) {
12
+ return param . replace ( / \$ / g, '\\$' ) ;
13
+ }
14
+
11
15
export const createTableTemplate = ( params ?: SchemaQueryParams ) => {
12
16
const tableName = params ?. relativePath
13
- ? `\`${ params ? .relativePath } /my_row_table\``
17
+ ? `\`${ normalizeParameter ( params . relativePath ) } /my_row_table\``
14
18
: '${1:my_row_table}' ;
15
19
return `-- docs: https://ydb.tech/en/docs/yql/reference/syntax/create_table
16
20
CREATE TABLE ${ tableName } (
46
50
} ;
47
51
export const createColumnTableTemplate = ( params ?: SchemaQueryParams ) => {
48
52
const tableName = params ?. relativePath
49
- ? `\`${ params ? .relativePath } /my_column_table\``
53
+ ? `\`${ normalizeParameter ( params . relativePath ) } /my_column_table\``
50
54
: '${1:my_column_table}' ;
51
55
return `-- docs: https://ydb.tech/en/docs/yql/reference/syntax/create_table#olap-tables
52
56
CREATE TABLE ${ tableName } (
75
79
);` ;
76
80
} ;
77
81
export const alterTableTemplate = ( params ?: SchemaQueryParams ) => {
78
- const path = params ?. relativePath ? `\`${ params ?. relativePath } \`` : '${1:<my_table>}' ;
82
+ const path = params ?. relativePath
83
+ ? `\`${ normalizeParameter ( params . relativePath ) } \``
84
+ : '${1:<my_table>}' ;
79
85
80
86
return `-- docs: https://ydb.tech/docs/en/yql/reference/syntax/alter_table/
81
87
@@ -86,7 +92,9 @@ ALTER TABLE ${path}
86
92
} ;
87
93
88
94
export const manageAutoPartitioningTemplate = ( params ?: SchemaQueryParams ) => {
89
- const path = params ?. relativePath ? `\`${ params ?. relativePath } \`` : '${1:<my_table>}' ;
95
+ const path = params ?. relativePath
96
+ ? `\`${ normalizeParameter ( params . relativePath ) } \``
97
+ : '${1:<my_table>}' ;
90
98
91
99
return `-- documentation about partitioning https://ydb.tech/docs/en/concepts/datamodel/table#partitioning
92
100
@@ -100,26 +108,36 @@ ALTER TABLE ${path} SET
100
108
)` ;
101
109
} ;
102
110
export const selectQueryTemplate = ( params ?: SchemaQueryParams ) => {
103
- const path = params ?. relativePath ? `\`${ params ?. relativePath } \`` : '${2:<my_table>}' ;
111
+ const path = params ?. relativePath
112
+ ? `\`${ normalizeParameter ( params . relativePath ) } \``
113
+ : '${2:<my_table>}' ;
104
114
const columns =
105
- params ?. schemaData ?. map ( ( column ) => '`' + column . name + '`' ) . join ( ', ' ) || '${1:*}' ;
115
+ params ?. schemaData
116
+ ?. map ( ( column ) => '`' + normalizeParameter ( column . name ?? '' ) + '`' )
117
+ . join ( ', ' ) || '${1:*}' ;
106
118
const filters = params ?. relativePath ? '' : 'WHERE ${3:Key1 = 1}\nORDER BY ${4:Key1}\n' ;
107
119
return `SELECT ${ columns }
108
120
FROM ${ path }
109
121
${ filters } LIMIT \${5:10};`;
110
122
} ;
111
123
export const upsertQueryTemplate = ( params ?: SchemaQueryParams ) => {
112
- const path = params ?. relativePath ? `\`${ params ?. relativePath } \`` : '${1:<my_table>}' ;
124
+ const path = params ?. relativePath
125
+ ? `\`${ normalizeParameter ( params . relativePath ) } \``
126
+ : '${1:<my_table>}' ;
113
127
const columns =
114
- params ?. schemaData ?. map ( ( column ) => `\`${ column . name } \`` ) . join ( ', ' ) || '${2:id, name}' ;
128
+ params ?. schemaData
129
+ ?. map ( ( column ) => `\`${ normalizeParameter ( column . name ?? '' ) } \`` )
130
+ . join ( ', ' ) || '${2:id, name}' ;
115
131
const values = params ?. schemaData ? '${3: }' : '${3:1, "foo"}' ;
116
132
return `UPSERT INTO ${ path }
117
133
( ${ columns } )
118
134
VALUES ( ${ values } );` ;
119
135
} ;
120
136
121
137
export const dropExternalTableTemplate = ( params ?: SchemaQueryParams ) => {
122
- const path = params ?. relativePath ? `\`${ params ?. relativePath } \`` : '${1:my_table}' ;
138
+ const path = params ?. relativePath
139
+ ? `\`${ normalizeParameter ( params . relativePath ) } \``
140
+ : '${1:my_table}' ;
123
141
return `DROP EXTERNAL TABLE ${ path } ;` ;
124
142
} ;
125
143
@@ -128,9 +146,13 @@ export const createExternalTableTemplate = (params?: SchemaQueryParams) => {
128
146
// to create table in the same folder with data source
129
147
const targetPath = params ?. relativePath . split ( '/' ) . slice ( 0 , - 1 ) . join ( '/' ) ;
130
148
131
- const target = targetPath ? `\`${ targetPath } /my_external_table\`` : '${1:<my_external_table>}' ;
149
+ const target = targetPath
150
+ ? `\`${ normalizeParameter ( targetPath ) } /my_external_table\``
151
+ : '${1:<my_external_table>}' ;
132
152
133
- const source = params ?. relativePath ? `${ params . relativePath } ` : '${2:<path_to_data_source>}' ;
153
+ const source = params ?. relativePath
154
+ ? `${ normalizeParameter ( params . relativePath ) } `
155
+ : '${2:<path_to_data_source>}' ;
134
156
return `CREATE EXTERNAL TABLE ${ target } (
135
157
column1 Int,
136
158
column2 Int
@@ -143,7 +165,9 @@ export const createExternalTableTemplate = (params?: SchemaQueryParams) => {
143
165
} ;
144
166
145
167
export const createTopicTemplate = ( params ?: SchemaQueryParams ) => {
146
- const path = params ?. relativePath ? `\`${ params ?. relativePath } /my_topic\`` : '${1:my_topic}' ;
168
+ const path = params ?. relativePath
169
+ ? `\`${ normalizeParameter ( params . relativePath ) } /my_topic\``
170
+ : '${1:my_topic}' ;
147
171
return `-- docs: https://ydb.tech/docs/en/yql/reference/syntax/create-topic
148
172
CREATE TOPIC ${ path } (
149
173
CONSUMER consumer1,
@@ -165,7 +189,9 @@ CREATE TOPIC ${path} (
165
189
} ;
166
190
167
191
export const alterTopicTemplate = ( params ?: SchemaQueryParams ) => {
168
- const path = params ?. relativePath ? `\`${ params ?. relativePath } \`` : '${1:<my_topic>}' ;
192
+ const path = params ?. relativePath
193
+ ? `\`${ normalizeParameter ( params . relativePath ) } \``
194
+ : '${1:<my_topic>}' ;
169
195
return `-- docs: https://ydb.tech/en/docs/yql/reference/syntax/alter_topic
170
196
ALTER TOPIC ${ path }
171
197
ADD CONSUMER new_consumer WITH (read_from = Datetime('1970-01-01T00:00:00Z')), -- Sets up the message write time starting from which the consumer will receive data.
@@ -188,44 +214,58 @@ ALTER TOPIC ${path}
188
214
} ;
189
215
190
216
export const dropTopicTemplate = ( params ?: SchemaQueryParams ) => {
191
- const path = params ?. relativePath ? `\`${ params ?. relativePath } \`` : '${1:<my_topic>}' ;
217
+ const path = params ?. relativePath
218
+ ? `\`${ normalizeParameter ( params . relativePath ) } \``
219
+ : '${1:<my_topic>}' ;
192
220
return `DROP TOPIC ${ path } ;` ;
193
221
} ;
194
222
195
223
export const createViewTemplate = ( params ?: SchemaQueryParams ) => {
196
- const path = params ?. relativePath ? `\`${ params ?. relativePath } /my_view\`` : '${1:my_view}' ;
224
+ const path = params ?. relativePath
225
+ ? `\`${ normalizeParameter ( params . relativePath ) } /my_view\``
226
+ : '${1:my_view}' ;
197
227
return `CREATE VIEW ${ path } WITH (security_invoker = TRUE) AS SELECT 1;` ;
198
228
} ;
199
229
200
230
export const dropViewTemplate = ( params ?: SchemaQueryParams ) => {
201
- const path = params ?. relativePath ? `\`${ params ?. relativePath } \`` : '${1:<my_view>}' ;
231
+ const path = params ?. relativePath
232
+ ? `\`${ normalizeParameter ( params . relativePath ) } \``
233
+ : '${1:<my_view>}' ;
202
234
return `DROP VIEW ${ path } ;` ;
203
235
} ;
204
236
export const dropAsyncReplicationTemplate = ( params ?: SchemaQueryParams ) => {
205
- const path = params ?. relativePath ? `\`${ params ?. relativePath } \`` : '${1:<my_replication>}' ;
237
+ const path = params ?. relativePath
238
+ ? `\`${ normalizeParameter ( params . relativePath ) } \``
239
+ : '${1:<my_replication>}' ;
206
240
return `DROP ASYNC REPLICATION ${ path } ;` ;
207
241
} ;
208
242
209
243
export const alterAsyncReplicationTemplate = ( params ?: SchemaQueryParams ) => {
210
- const path = params ?. relativePath ? `\`${ params ?. relativePath } \`` : '${1:<my_replication>}' ;
244
+ const path = params ?. relativePath
245
+ ? `\`${ normalizeParameter ( params . relativePath ) } \``
246
+ : '${1:<my_replication>}' ;
211
247
return `-- docs: https://ydb.tech/docs/en/yql/reference/syntax/alter-async-replication
212
248
ALTER ASYNC REPLICATION ${ path } SET (STATE = "DONE", FAILOVER_MODE = "FORCE");` ;
213
249
} ;
214
250
215
251
export const addTableIndex = ( params ?: SchemaQueryParams ) => {
216
- const path = params ?. relativePath ? `\`${ params ?. relativePath } \`` : '${1:<my_table>}' ;
252
+ const path = params ?. relativePath
253
+ ? `\`${ normalizeParameter ( params . relativePath ) } \``
254
+ : '${1:<my_table>}' ;
217
255
return `ALTER TABLE ${ path } ADD INDEX \${2:index_name} GLOBAL ON (\${3:<column_name>});` ;
218
256
} ;
219
257
220
258
export const dropTableIndex = ( params ?: SchemaQueryParams ) => {
221
259
const indexName = params ?. relativePath . split ( '/' ) . pop ( ) ;
222
260
const path = params ?. relativePath . split ( '/' ) . slice ( 0 , - 1 ) . join ( '/' ) ;
223
- const pathSnippet = path ? `\`${ path } \`` : '${1:<my_table>}' ;
224
- return `ALTER TABLE ${ pathSnippet } DROP INDEX ${ indexName || '${2:<index_name>}' } ;` ;
261
+ const pathSnippet = path ? `\`${ normalizeParameter ( path ) } \`` : '${1:<my_table>}' ;
262
+ return `ALTER TABLE ${ pathSnippet } DROP INDEX ${ normalizeParameter ( indexName ?? '' ) || '${2:<index_name>}' } ;` ;
225
263
} ;
226
264
227
265
export const createCdcStreamTemplate = ( params ?: SchemaQueryParams ) => {
228
- const path = params ?. relativePath ? `\`${ params ?. relativePath } \`` : '${1:<my_table>}' ;
266
+ const path = params ?. relativePath
267
+ ? `\`${ normalizeParameter ( params . relativePath ) } \``
268
+ : '${1:<my_table>}' ;
229
269
return `-- docs: https://ydb.tech/docs/en/yql/reference/syntax/alter_table/changefeed
230
270
ALTER TABLE ${ path } ADD CHANGEFEED \${2:changefeed_name} WITH (
231
271
MODE = \${3:'UPDATES'}, -- KEYS_ONLY, UPDATES, NEW_IMAGE, OLD_IMAGE, or NEW_AND_OLD_IMAGES
@@ -260,7 +300,9 @@ CREATE USER \${1:user_name} PASSWORD \${2:'password'}
260
300
} ;
261
301
262
302
export const deleteRowsTemplate = ( params ?: SchemaQueryParams ) => {
263
- const path = params ?. relativePath ? `\`${ params ?. relativePath } \`` : '${1:<my_table>}' ;
303
+ const path = params ?. relativePath
304
+ ? `\`${ normalizeParameter ( params . relativePath ) } \``
305
+ : '${1:<my_table>}' ;
264
306
return `-- docs: https://ydb.tech/docs/en/yql/reference/syntax/delete
265
307
DELETE FROM ${ path }
266
308
WHERE \${2:Key1 = 1};` ;
@@ -284,7 +326,7 @@ DROP USER \${1:<user_name>}
284
326
285
327
export const grantPrivilegeTemplate = ( params ?: SchemaQueryParams ) => {
286
328
const path = params ?. relativePath
287
- ? `\`${ params ?. relativePath } \``
329
+ ? `\`${ normalizeParameter ( params ?. relativePath ) } \``
288
330
: '${2:<path_to_scheme_object>}' ;
289
331
return `GRANT \${1:<permission_name>}
290
332
ON ${ path }
@@ -301,7 +343,7 @@ TO \${3:<role_name>}
301
343
302
344
export const revokePrivilegeTemplate = ( params ?: SchemaQueryParams ) => {
303
345
const path = params ?. relativePath
304
- ? `\`${ params ?. relativePath } \``
346
+ ? `\`${ normalizeParameter ( params ?. relativePath ) } \``
305
347
: '${2:<path_to_scheme_object>}' ;
306
348
return `REVOKE \${1:<permission_name>}
307
349
ON ${ path }
@@ -316,14 +358,18 @@ FROM \${3:<role_name>}
316
358
} ;
317
359
318
360
export const updateTableTemplate = ( params ?: SchemaQueryParams ) => {
319
- const path = params ?. relativePath ? `\`${ params ?. relativePath } \`` : '${1:<my_table>}' ;
361
+ const path = params ?. relativePath
362
+ ? `\`${ normalizeParameter ( params . relativePath ) } \``
363
+ : '${1:<my_table>}' ;
320
364
return `-- docs: https://ydb.tech/docs/en/yql/reference/syntax/update
321
365
UPDATE ${ path }
322
366
SET \${2:Column1 = 'foo', Column2 = 'bar'}
323
367
WHERE \${3:Key1 = 1};` ;
324
368
} ;
325
369
326
370
export const dropTableTemplate = ( params ?: SchemaQueryParams ) => {
327
- const path = params ?. relativePath ? `\`${ params ?. relativePath } \`` : '${1:<my_table>}' ;
371
+ const path = params ?. relativePath
372
+ ? `\`${ normalizeParameter ( params . relativePath ) } \``
373
+ : '${1:<my_table>}' ;
328
374
return `DROP TABLE ${ path } ;` ;
329
375
} ;
0 commit comments