@@ -6,8 +6,8 @@ use itertools::Itertools;
6
6
use once_cell:: sync:: Lazy ;
7
7
use regex:: Regex ;
8
8
use sqlparser:: ast:: {
9
- AlterTableOperation , Assignment , BinaryOperator , CloseCursor , ColumnDef , Expr , Ident ,
10
- ObjectName , Query , Select , SelectItem , SetExpr , Statement , TableAlias , TableConstraint ,
9
+ AlterTableOperation , Assignment , BinaryOperator , CloseCursor , ColumnDef , Expr , FunctionArg ,
10
+ Ident , ObjectName , Query , Select , SelectItem , SetExpr , Statement , TableAlias , TableConstraint ,
11
11
TableFactor , UnaryOperator , Value , VisitMut , VisitorMut ,
12
12
} ;
13
13
use sqlparser:: dialect:: { Dialect , GenericDialect } ;
@@ -198,12 +198,41 @@ impl VisitorMut for NormalizeVisitor {
198
198
Self :: simplify_table_alias ( alias) ;
199
199
}
200
200
TableFactor :: Pivot {
201
- table_alias,
202
- pivot_alias,
201
+ value_column,
202
+ alias,
203
+ ..
204
+ } => {
205
+ Self :: simplify_compound_identifier ( value_column) ;
206
+ Self :: simplify_table_alias ( alias) ;
207
+ }
208
+ TableFactor :: Function {
209
+ name, args, alias, ..
210
+ } => {
211
+ Self :: simplify_compound_identifier ( & mut name. 0 ) ;
212
+ for arg in args {
213
+ if let FunctionArg :: Named { name, .. } = arg {
214
+ Self :: scrub_name ( name) ;
215
+ }
216
+ }
217
+ Self :: simplify_table_alias ( alias) ;
218
+ }
219
+ TableFactor :: JsonTable { columns, alias, .. } => {
220
+ for column in columns {
221
+ Self :: scrub_name ( & mut column. name ) ;
222
+ }
223
+ Self :: simplify_table_alias ( alias) ;
224
+ }
225
+ TableFactor :: Unpivot {
226
+ value,
227
+ name,
228
+ columns,
229
+ alias,
203
230
..
204
231
} => {
205
- Self :: simplify_table_alias ( table_alias) ;
206
- Self :: simplify_table_alias ( pivot_alias) ;
232
+ Self :: scrub_name ( value) ;
233
+ Self :: scrub_name ( name) ;
234
+ Self :: simplify_compound_identifier ( columns) ;
235
+ Self :: simplify_table_alias ( alias) ;
207
236
}
208
237
}
209
238
ControlFlow :: Continue ( ( ) )
@@ -311,8 +340,10 @@ impl VisitorMut for NormalizeVisitor {
311
340
columns, source, ..
312
341
} => {
313
342
* columns = vec ! [ Self :: ellipsis( ) ] ;
314
- if let SetExpr :: Values ( v) = & mut * source. body {
315
- v. rows = vec ! [ vec![ Expr :: Value ( Self :: placeholder( ) ) ] ]
343
+ if let Some ( source) = source. as_mut ( ) {
344
+ if let SetExpr :: Values ( v) = & mut * source. body {
345
+ v. rows = vec ! [ vec![ Expr :: Value ( Self :: placeholder( ) ) ] ]
346
+ }
316
347
}
317
348
}
318
349
// Simple lists of col = value assignments are collapsed to `..`.
@@ -350,89 +381,93 @@ impl VisitorMut for NormalizeVisitor {
350
381
Statement :: Close {
351
382
cursor : CloseCursor :: Specific { name } ,
352
383
} => Self :: erase_name ( name) ,
353
- Statement :: AlterTable { name, operation } => {
384
+ Statement :: AlterTable {
385
+ name, operations, ..
386
+ } => {
354
387
Self :: simplify_compound_identifier ( & mut name. 0 ) ;
355
- match operation {
356
- AlterTableOperation :: AddConstraint ( c) => match c {
357
- TableConstraint :: Unique { name, columns, .. } => {
358
- if let Some ( name) = name {
359
- Self :: scrub_name ( name) ;
388
+ for operation in operations {
389
+ match operation {
390
+ AlterTableOperation :: AddConstraint ( c) => match c {
391
+ TableConstraint :: Unique { name, columns, .. } => {
392
+ if let Some ( name) = name {
393
+ Self :: scrub_name ( name) ;
394
+ }
395
+ for column in columns {
396
+ Self :: scrub_name ( column) ;
397
+ }
360
398
}
361
- for column in columns {
362
- Self :: scrub_name ( column) ;
399
+ TableConstraint :: ForeignKey {
400
+ name,
401
+ columns,
402
+ referred_columns,
403
+ ..
404
+ } => {
405
+ if let Some ( name) = name {
406
+ Self :: scrub_name ( name) ;
407
+ }
408
+ for column in columns {
409
+ Self :: scrub_name ( column) ;
410
+ }
411
+ for column in referred_columns {
412
+ Self :: scrub_name ( column) ;
413
+ }
363
414
}
364
- }
365
- TableConstraint :: ForeignKey {
366
- name,
367
- columns,
368
- referred_columns,
369
- ..
370
- } => {
371
- if let Some ( name) = name {
372
- Self :: scrub_name ( name) ;
415
+ TableConstraint :: Check { name, .. } => {
416
+ if let Some ( name) = name {
417
+ Self :: scrub_name ( name) ;
418
+ }
373
419
}
374
- for column in columns {
375
- Self :: scrub_name ( column) ;
420
+ TableConstraint :: Index { name, columns, .. } => {
421
+ if let Some ( name) = name {
422
+ Self :: scrub_name ( name) ;
423
+ }
424
+ for column in columns {
425
+ Self :: scrub_name ( column) ;
426
+ }
376
427
}
377
- for column in referred_columns {
378
- Self :: scrub_name ( column) ;
428
+ TableConstraint :: FulltextOrSpatial {
429
+ opt_index_name,
430
+ columns,
431
+ ..
432
+ } => {
433
+ if let Some ( name) = opt_index_name {
434
+ Self :: scrub_name ( name) ;
435
+ }
436
+ for column in columns {
437
+ Self :: scrub_name ( column) ;
438
+ }
379
439
}
440
+ } ,
441
+ AlterTableOperation :: AddColumn { column_def, .. } => {
442
+ let ColumnDef { name, .. } = column_def;
443
+ Self :: scrub_name ( name) ;
380
444
}
381
- TableConstraint :: Check { name, .. } => {
382
- if let Some ( name) = name {
383
- Self :: scrub_name ( name) ;
384
- }
445
+ AlterTableOperation :: DropConstraint { name, .. } => Self :: scrub_name ( name) ,
446
+ AlterTableOperation :: DropColumn { column_name, .. } => {
447
+ Self :: scrub_name ( column_name)
385
448
}
386
- TableConstraint :: Index { name, columns, .. } => {
387
- if let Some ( name) = name {
388
- Self :: scrub_name ( name) ;
389
- }
390
- for column in columns {
391
- Self :: scrub_name ( column) ;
392
- }
449
+ AlterTableOperation :: RenameColumn {
450
+ old_column_name,
451
+ new_column_name,
452
+ } => {
453
+ Self :: scrub_name ( old_column_name) ;
454
+ Self :: scrub_name ( new_column_name) ;
393
455
}
394
- TableConstraint :: FulltextOrSpatial {
395
- opt_index_name,
396
- columns,
397
- ..
456
+ AlterTableOperation :: ChangeColumn {
457
+ old_name, new_name, ..
398
458
} => {
399
- if let Some ( name) = opt_index_name {
400
- Self :: scrub_name ( name) ;
401
- }
402
- for column in columns {
403
- Self :: scrub_name ( column) ;
404
- }
459
+ Self :: scrub_name ( old_name) ;
460
+ Self :: scrub_name ( new_name) ;
405
461
}
406
- } ,
407
- AlterTableOperation :: AddColumn { column_def, .. } => {
408
- let ColumnDef { name, .. } = column_def;
409
- Self :: scrub_name ( name) ;
410
- }
411
- AlterTableOperation :: DropConstraint { name, .. } => Self :: scrub_name ( name) ,
412
- AlterTableOperation :: DropColumn { column_name, .. } => {
413
- Self :: scrub_name ( column_name)
414
- }
415
- AlterTableOperation :: RenameColumn {
416
- old_column_name,
417
- new_column_name,
418
- } => {
419
- Self :: scrub_name ( old_column_name) ;
420
- Self :: scrub_name ( new_column_name) ;
421
- }
422
- AlterTableOperation :: ChangeColumn {
423
- old_name, new_name, ..
424
- } => {
425
- Self :: scrub_name ( old_name) ;
426
- Self :: scrub_name ( new_name) ;
427
- }
428
- AlterTableOperation :: RenameConstraint { old_name, new_name } => {
429
- Self :: scrub_name ( old_name) ;
430
- Self :: scrub_name ( new_name) ;
431
- }
432
- AlterTableOperation :: AlterColumn { column_name, .. } => {
433
- Self :: scrub_name ( column_name) ;
462
+ AlterTableOperation :: RenameConstraint { old_name, new_name } => {
463
+ Self :: scrub_name ( old_name) ;
464
+ Self :: scrub_name ( new_name) ;
465
+ }
466
+ AlterTableOperation :: AlterColumn { column_name, .. } => {
467
+ Self :: scrub_name ( column_name) ;
468
+ }
469
+ _ => { }
434
470
}
435
- _ => { }
436
471
}
437
472
}
438
473
0 commit comments