@@ -8,9 +8,13 @@ export interface SchemaQueryParams {
88
99export type TemplateFn = ( params ?: SchemaQueryParams ) => string ;
1010
11+ function normalizeParameter ( param : string ) {
12+ return param . replace ( / \$ / g, '\\$' ) ;
13+ }
14+
1115export const createTableTemplate = ( params ?: SchemaQueryParams ) => {
1216 const tableName = params ?. relativePath
13- ? `\`${ params ? .relativePath } /my_row_table\``
17+ ? `\`${ normalizeParameter ( params . relativePath ) } /my_row_table\``
1418 : '${1:my_row_table}' ;
1519 return `-- docs: https://ydb.tech/en/docs/yql/reference/syntax/create_table
1620CREATE TABLE ${ tableName } (
4650} ;
4751export const createColumnTableTemplate = ( params ?: SchemaQueryParams ) => {
4852 const tableName = params ?. relativePath
49- ? `\`${ params ? .relativePath } /my_column_table\``
53+ ? `\`${ normalizeParameter ( params . relativePath ) } /my_column_table\``
5054 : '${1:my_column_table}' ;
5155 return `-- docs: https://ydb.tech/en/docs/yql/reference/syntax/create_table#olap-tables
5256CREATE TABLE ${ tableName } (
7579);` ;
7680} ;
7781export 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>}' ;
7985
8086 return `-- docs: https://ydb.tech/docs/en/yql/reference/syntax/alter_table/
8187
@@ -86,7 +92,9 @@ ALTER TABLE ${path}
8692} ;
8793
8894export 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>}' ;
9098
9199 return `-- documentation about partitioning https://ydb.tech/docs/en/concepts/datamodel/table#partitioning
92100
@@ -100,26 +108,36 @@ ALTER TABLE ${path} SET
100108)` ;
101109} ;
102110export 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>}' ;
104114 const columns =
105- params ?. schemaData ?. map ( ( column ) => '`' + column . name + '`' ) . join ( ', ' ) || '${1:*}' ;
115+ params ?. schemaData
116+ ?. map ( ( column ) => '`' + normalizeParameter ( column . name ?? '' ) + '`' )
117+ . join ( ', ' ) || '${1:*}' ;
106118 const filters = params ?. relativePath ? '' : 'WHERE ${3:Key1 = 1}\nORDER BY ${4:Key1}\n' ;
107119 return `SELECT ${ columns }
108120FROM ${ path }
109121${ filters } LIMIT \${5:10};`;
110122} ;
111123export 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>}' ;
113127 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}' ;
115131 const values = params ?. schemaData ? '${3: }' : '${3:1, "foo"}' ;
116132 return `UPSERT INTO ${ path }
117133( ${ columns } )
118134VALUES ( ${ values } );` ;
119135} ;
120136
121137export 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}' ;
123141 return `DROP EXTERNAL TABLE ${ path } ;` ;
124142} ;
125143
@@ -128,9 +146,13 @@ export const createExternalTableTemplate = (params?: SchemaQueryParams) => {
128146 // to create table in the same folder with data source
129147 const targetPath = params ?. relativePath . split ( '/' ) . slice ( 0 , - 1 ) . join ( '/' ) ;
130148
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>}' ;
132152
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>}' ;
134156 return `CREATE EXTERNAL TABLE ${ target } (
135157 column1 Int,
136158 column2 Int
@@ -143,7 +165,9 @@ export const createExternalTableTemplate = (params?: SchemaQueryParams) => {
143165} ;
144166
145167export 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}' ;
147171 return `-- docs: https://ydb.tech/docs/en/yql/reference/syntax/create-topic
148172CREATE TOPIC ${ path } (
149173 CONSUMER consumer1,
@@ -165,7 +189,9 @@ CREATE TOPIC ${path} (
165189} ;
166190
167191export 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>}' ;
169195 return `-- docs: https://ydb.tech/en/docs/yql/reference/syntax/alter_topic
170196ALTER TOPIC ${ path }
171197 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}
188214} ;
189215
190216export 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>}' ;
192220 return `DROP TOPIC ${ path } ;` ;
193221} ;
194222
195223export 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}' ;
197227 return `CREATE VIEW ${ path } WITH (security_invoker = TRUE) AS SELECT 1;` ;
198228} ;
199229
200230export 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>}' ;
202234 return `DROP VIEW ${ path } ;` ;
203235} ;
204236export 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>}' ;
206240 return `DROP ASYNC REPLICATION ${ path } ;` ;
207241} ;
208242
209243export 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>}' ;
211247 return `-- docs: https://ydb.tech/docs/en/yql/reference/syntax/alter-async-replication
212248ALTER ASYNC REPLICATION ${ path } SET (STATE = "DONE", FAILOVER_MODE = "FORCE");` ;
213249} ;
214250
215251export 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>}' ;
217255 return `ALTER TABLE ${ path } ADD INDEX \${2:index_name} GLOBAL ON (\${3:<column_name>});` ;
218256} ;
219257
220258export const dropTableIndex = ( params ?: SchemaQueryParams ) => {
221259 const indexName = params ?. relativePath . split ( '/' ) . pop ( ) ;
222260 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>}' } ;` ;
225263} ;
226264
227265export 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>}' ;
229269 return `-- docs: https://ydb.tech/docs/en/yql/reference/syntax/alter_table/changefeed
230270ALTER TABLE ${ path } ADD CHANGEFEED \${2:changefeed_name} WITH (
231271 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'}
260300} ;
261301
262302export 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>}' ;
264306 return `-- docs: https://ydb.tech/docs/en/yql/reference/syntax/delete
265307DELETE FROM ${ path }
266308WHERE \${2:Key1 = 1};` ;
@@ -284,7 +326,7 @@ DROP USER \${1:<user_name>}
284326
285327export const grantPrivilegeTemplate = ( params ?: SchemaQueryParams ) => {
286328 const path = params ?. relativePath
287- ? `\`${ params ?. relativePath } \``
329+ ? `\`${ normalizeParameter ( params ?. relativePath ) } \``
288330 : '${2:<path_to_scheme_object>}' ;
289331 return `GRANT \${1:<permission_name>}
290332ON ${ path }
@@ -301,7 +343,7 @@ TO \${3:<role_name>}
301343
302344export const revokePrivilegeTemplate = ( params ?: SchemaQueryParams ) => {
303345 const path = params ?. relativePath
304- ? `\`${ params ?. relativePath } \``
346+ ? `\`${ normalizeParameter ( params ?. relativePath ) } \``
305347 : '${2:<path_to_scheme_object>}' ;
306348 return `REVOKE \${1:<permission_name>}
307349ON ${ path }
@@ -316,14 +358,18 @@ FROM \${3:<role_name>}
316358} ;
317359
318360export 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>}' ;
320364 return `-- docs: https://ydb.tech/docs/en/yql/reference/syntax/update
321365UPDATE ${ path }
322366SET \${2:Column1 = 'foo', Column2 = 'bar'}
323367WHERE \${3:Key1 = 1};` ;
324368} ;
325369
326370export 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>}' ;
328374 return `DROP TABLE ${ path } ;` ;
329375} ;
0 commit comments