@@ -30,8 +30,8 @@ use sqlparser::dialect::{
30
30
use sqlparser:: keywords:: ALL_KEYWORDS ;
31
31
use sqlparser:: parser:: { Parser , ParserError , ParserOptions } ;
32
32
use test_utils:: {
33
- all_dialects, assert_eq_vec, expr_from_projection, join, number, only, table, table_alias ,
34
- TestedDialects ,
33
+ all_dialects, alter_table_op , assert_eq_vec, expr_from_projection, join, number, only, table,
34
+ table_alias , TestedDialects ,
35
35
} ;
36
36
37
37
#[ macro_use]
@@ -2920,48 +2920,37 @@ fn parse_create_external_table_lowercase() {
2920
2920
#[ test]
2921
2921
fn parse_alter_table ( ) {
2922
2922
let add_column = "ALTER TABLE tab ADD COLUMN foo TEXT;" ;
2923
- match one_statement_parses_to ( add_column, "ALTER TABLE tab ADD COLUMN foo TEXT" ) {
2924
- Statement :: AlterTable {
2925
- name,
2926
- operation :
2927
- AlterTableOperation :: AddColumn {
2928
- column_keyword,
2929
- if_not_exists,
2930
- column_def,
2931
- } ,
2923
+ match alter_table_op ( one_statement_parses_to (
2924
+ add_column,
2925
+ "ALTER TABLE tab ADD COLUMN foo TEXT" ,
2926
+ ) ) {
2927
+ AlterTableOperation :: AddColumn {
2928
+ column_keyword,
2929
+ if_not_exists,
2930
+ column_def,
2932
2931
} => {
2933
2932
assert ! ( column_keyword) ;
2934
2933
assert ! ( !if_not_exists) ;
2935
- assert_eq ! ( "tab" , name. to_string( ) ) ;
2936
2934
assert_eq ! ( "foo" , column_def. name. to_string( ) ) ;
2937
2935
assert_eq ! ( "TEXT" , column_def. data_type. to_string( ) ) ;
2938
2936
}
2939
2937
_ => unreachable ! ( ) ,
2940
2938
} ;
2941
2939
2942
2940
let rename_table = "ALTER TABLE tab RENAME TO new_tab" ;
2943
- match verified_stmt ( rename_table) {
2944
- Statement :: AlterTable {
2945
- name,
2946
- operation : AlterTableOperation :: RenameTable { table_name } ,
2947
- } => {
2948
- assert_eq ! ( "tab" , name. to_string( ) ) ;
2949
- assert_eq ! ( "new_tab" , table_name. to_string( ) )
2941
+ match alter_table_op ( verified_stmt ( rename_table) ) {
2942
+ AlterTableOperation :: RenameTable { table_name } => {
2943
+ assert_eq ! ( "new_tab" , table_name. to_string( ) ) ;
2950
2944
}
2951
2945
_ => unreachable ! ( ) ,
2952
2946
} ;
2953
2947
2954
2948
let rename_column = "ALTER TABLE tab RENAME COLUMN foo TO new_foo" ;
2955
- match verified_stmt ( rename_column) {
2956
- Statement :: AlterTable {
2957
- name,
2958
- operation :
2959
- AlterTableOperation :: RenameColumn {
2960
- old_column_name,
2961
- new_column_name,
2962
- } ,
2949
+ match alter_table_op ( verified_stmt ( rename_column) ) {
2950
+ AlterTableOperation :: RenameColumn {
2951
+ old_column_name,
2952
+ new_column_name,
2963
2953
} => {
2964
- assert_eq ! ( "tab" , name. to_string( ) ) ;
2965
2954
assert_eq ! ( old_column_name. to_string( ) , "foo" ) ;
2966
2955
assert_eq ! ( new_column_name. to_string( ) , "new_foo" ) ;
2967
2956
}
@@ -3047,21 +3036,15 @@ fn parse_alter_view_with_columns() {
3047
3036
3048
3037
#[ test]
3049
3038
fn parse_alter_table_add_column ( ) {
3050
- match verified_stmt ( "ALTER TABLE tab ADD foo TEXT" ) {
3051
- Statement :: AlterTable {
3052
- operation : AlterTableOperation :: AddColumn { column_keyword, .. } ,
3053
- ..
3054
- } => {
3039
+ match alter_table_op ( verified_stmt ( "ALTER TABLE tab ADD foo TEXT" ) ) {
3040
+ AlterTableOperation :: AddColumn { column_keyword, .. } => {
3055
3041
assert ! ( !column_keyword) ;
3056
3042
}
3057
3043
_ => unreachable ! ( ) ,
3058
3044
} ;
3059
3045
3060
- match verified_stmt ( "ALTER TABLE tab ADD COLUMN foo TEXT" ) {
3061
- Statement :: AlterTable {
3062
- operation : AlterTableOperation :: AddColumn { column_keyword, .. } ,
3063
- ..
3064
- } => {
3046
+ match alter_table_op ( verified_stmt ( "ALTER TABLE tab ADD COLUMN foo TEXT" ) ) {
3047
+ AlterTableOperation :: AddColumn { column_keyword, .. } => {
3065
3048
assert ! ( column_keyword) ;
3066
3049
}
3067
3050
_ => unreachable ! ( ) ,
@@ -3080,24 +3063,19 @@ fn parse_alter_table_add_column_if_not_exists() {
3080
3063
options : None ,
3081
3064
} ;
3082
3065
3083
- match dialects. verified_stmt ( "ALTER TABLE tab ADD IF NOT EXISTS foo TEXT" ) {
3084
- Statement :: AlterTable {
3085
- operation : AlterTableOperation :: AddColumn { if_not_exists, .. } ,
3086
- ..
3087
- } => {
3066
+ match alter_table_op ( dialects. verified_stmt ( "ALTER TABLE tab ADD IF NOT EXISTS foo TEXT" ) ) {
3067
+ AlterTableOperation :: AddColumn { if_not_exists, .. } => {
3088
3068
assert ! ( if_not_exists) ;
3089
3069
}
3090
3070
_ => unreachable ! ( ) ,
3091
3071
} ;
3092
3072
3093
- match dialects. verified_stmt ( "ALTER TABLE tab ADD COLUMN IF NOT EXISTS foo TEXT" ) {
3094
- Statement :: AlterTable {
3095
- operation :
3096
- AlterTableOperation :: AddColumn {
3097
- column_keyword,
3098
- if_not_exists,
3099
- ..
3100
- } ,
3073
+ match alter_table_op (
3074
+ dialects. verified_stmt ( "ALTER TABLE tab ADD COLUMN IF NOT EXISTS foo TEXT" ) ,
3075
+ ) {
3076
+ AlterTableOperation :: AddColumn {
3077
+ column_keyword,
3078
+ if_not_exists,
3101
3079
..
3102
3080
} => {
3103
3081
assert ! ( column_keyword) ;
@@ -3123,12 +3101,10 @@ fn parse_alter_table_constraints() {
3123
3101
check_one ( "CHECK (end_date > start_date OR end_date IS NULL)" ) ;
3124
3102
3125
3103
fn check_one ( constraint_text : & str ) {
3126
- match verified_stmt ( & format ! ( "ALTER TABLE tab ADD {constraint_text}" ) ) {
3127
- Statement :: AlterTable {
3128
- name,
3129
- operation : AlterTableOperation :: AddConstraint ( constraint) ,
3130
- } => {
3131
- assert_eq ! ( "tab" , name. to_string( ) ) ;
3104
+ match alter_table_op ( verified_stmt ( & format ! (
3105
+ "ALTER TABLE tab ADD {constraint_text}"
3106
+ ) ) ) {
3107
+ AlterTableOperation :: AddConstraint ( constraint) => {
3132
3108
assert_eq ! ( constraint_text, constraint. to_string( ) ) ;
3133
3109
}
3134
3110
_ => unreachable ! ( ) ,
@@ -3150,17 +3126,12 @@ fn parse_alter_table_drop_column() {
3150
3126
) ;
3151
3127
3152
3128
fn check_one ( constraint_text : & str ) {
3153
- match verified_stmt ( & format ! ( "ALTER TABLE tab {constraint_text}" ) ) {
3154
- Statement :: AlterTable {
3155
- name,
3156
- operation :
3157
- AlterTableOperation :: DropColumn {
3158
- column_name,
3159
- if_exists,
3160
- cascade,
3161
- } ,
3129
+ match alter_table_op ( verified_stmt ( & format ! ( "ALTER TABLE tab {constraint_text}" ) ) ) {
3130
+ AlterTableOperation :: DropColumn {
3131
+ column_name,
3132
+ if_exists,
3133
+ cascade,
3162
3134
} => {
3163
- assert_eq ! ( "tab" , name. to_string( ) ) ;
3164
3135
assert_eq ! ( "is_active" , column_name. to_string( ) ) ;
3165
3136
assert ! ( if_exists) ;
3166
3137
assert ! ( cascade) ;
@@ -3173,12 +3144,10 @@ fn parse_alter_table_drop_column() {
3173
3144
#[ test]
3174
3145
fn parse_alter_table_alter_column ( ) {
3175
3146
let alter_stmt = "ALTER TABLE tab" ;
3176
- match verified_stmt ( & format ! ( "{alter_stmt} ALTER COLUMN is_active SET NOT NULL" ) ) {
3177
- Statement :: AlterTable {
3178
- name,
3179
- operation : AlterTableOperation :: AlterColumn { column_name, op } ,
3180
- } => {
3181
- assert_eq ! ( "tab" , name. to_string( ) ) ;
3147
+ match alter_table_op ( verified_stmt ( & format ! (
3148
+ "{alter_stmt} ALTER COLUMN is_active SET NOT NULL"
3149
+ ) ) ) {
3150
+ AlterTableOperation :: AlterColumn { column_name, op } => {
3182
3151
assert_eq ! ( "is_active" , column_name. to_string( ) ) ;
3183
3152
assert_eq ! ( op, AlterColumnOperation :: SetNotNull { } ) ;
3184
3153
}
@@ -3190,14 +3159,10 @@ fn parse_alter_table_alter_column() {
3190
3159
"ALTER TABLE tab ALTER COLUMN is_active DROP NOT NULL" ,
3191
3160
) ;
3192
3161
3193
- match verified_stmt ( & format ! (
3162
+ match alter_table_op ( verified_stmt ( & format ! (
3194
3163
"{alter_stmt} ALTER COLUMN is_active SET DEFAULT false"
3195
- ) ) {
3196
- Statement :: AlterTable {
3197
- name,
3198
- operation : AlterTableOperation :: AlterColumn { column_name, op } ,
3199
- } => {
3200
- assert_eq ! ( "tab" , name. to_string( ) ) ;
3164
+ ) ) ) {
3165
+ AlterTableOperation :: AlterColumn { column_name, op } => {
3201
3166
assert_eq ! ( "is_active" , column_name. to_string( ) ) ;
3202
3167
assert_eq ! (
3203
3168
op,
@@ -3209,12 +3174,10 @@ fn parse_alter_table_alter_column() {
3209
3174
_ => unreachable ! ( ) ,
3210
3175
}
3211
3176
3212
- match verified_stmt ( & format ! ( "{alter_stmt} ALTER COLUMN is_active DROP DEFAULT" ) ) {
3213
- Statement :: AlterTable {
3214
- name,
3215
- operation : AlterTableOperation :: AlterColumn { column_name, op } ,
3216
- } => {
3217
- assert_eq ! ( "tab" , name. to_string( ) ) ;
3177
+ match alter_table_op ( verified_stmt ( & format ! (
3178
+ "{alter_stmt} ALTER COLUMN is_active DROP DEFAULT"
3179
+ ) ) ) {
3180
+ AlterTableOperation :: AlterColumn { column_name, op } => {
3218
3181
assert_eq ! ( "is_active" , column_name. to_string( ) ) ;
3219
3182
assert_eq ! ( op, AlterColumnOperation :: DropDefault { } ) ;
3220
3183
}
@@ -3225,12 +3188,10 @@ fn parse_alter_table_alter_column() {
3225
3188
#[ test]
3226
3189
fn parse_alter_table_alter_column_type ( ) {
3227
3190
let alter_stmt = "ALTER TABLE tab" ;
3228
- match verified_stmt ( "ALTER TABLE tab ALTER COLUMN is_active SET DATA TYPE TEXT" ) {
3229
- Statement :: AlterTable {
3230
- name,
3231
- operation : AlterTableOperation :: AlterColumn { column_name, op } ,
3232
- } => {
3233
- assert_eq ! ( "tab" , name. to_string( ) ) ;
3191
+ match alter_table_op ( verified_stmt (
3192
+ "ALTER TABLE tab ALTER COLUMN is_active SET DATA TYPE TEXT" ,
3193
+ ) ) {
3194
+ AlterTableOperation :: AlterColumn { column_name, op } => {
3234
3195
assert_eq ! ( "is_active" , column_name. to_string( ) ) ;
3235
3196
assert_eq ! (
3236
3197
op,
@@ -3267,34 +3228,28 @@ fn parse_alter_table_alter_column_type() {
3267
3228
#[ test]
3268
3229
fn parse_alter_table_drop_constraint ( ) {
3269
3230
let alter_stmt = "ALTER TABLE tab" ;
3270
- match verified_stmt ( "ALTER TABLE tab DROP CONSTRAINT constraint_name CASCADE" ) {
3271
- Statement :: AlterTable {
3272
- name,
3273
- operation :
3274
- AlterTableOperation :: DropConstraint {
3275
- name : constr_name,
3276
- if_exists,
3277
- cascade,
3278
- } ,
3231
+ match alter_table_op ( verified_stmt (
3232
+ "ALTER TABLE tab DROP CONSTRAINT constraint_name CASCADE" ,
3233
+ ) ) {
3234
+ AlterTableOperation :: DropConstraint {
3235
+ name : constr_name,
3236
+ if_exists,
3237
+ cascade,
3279
3238
} => {
3280
- assert_eq ! ( "tab" , name. to_string( ) ) ;
3281
3239
assert_eq ! ( "constraint_name" , constr_name. to_string( ) ) ;
3282
3240
assert ! ( !if_exists) ;
3283
3241
assert ! ( cascade) ;
3284
3242
}
3285
3243
_ => unreachable ! ( ) ,
3286
3244
}
3287
- match verified_stmt ( "ALTER TABLE tab DROP CONSTRAINT IF EXISTS constraint_name" ) {
3288
- Statement :: AlterTable {
3289
- name,
3290
- operation :
3291
- AlterTableOperation :: DropConstraint {
3292
- name : constr_name,
3293
- if_exists,
3294
- cascade,
3295
- } ,
3245
+ match alter_table_op ( verified_stmt (
3246
+ "ALTER TABLE tab DROP CONSTRAINT IF EXISTS constraint_name" ,
3247
+ ) ) {
3248
+ AlterTableOperation :: DropConstraint {
3249
+ name : constr_name,
3250
+ if_exists,
3251
+ cascade,
3296
3252
} => {
3297
- assert_eq ! ( "tab" , name. to_string( ) ) ;
3298
3253
assert_eq ! ( "constraint_name" , constr_name. to_string( ) ) ;
3299
3254
assert ! ( if_exists) ;
3300
3255
assert ! ( !cascade) ;
0 commit comments