qusql-parse 0.4.0

Parser for sql
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

use alloc::vec::Vec;

use crate::{
    Identifier, OptSpanned, QualifiedName, SString, Span, Spanned,
    alter_table::AlterTableOwner,
    data_type::{DataType, DataTypeContext, parse_data_type},
    drop::{CascadeOrRestrict, parse_cascade_or_restrict},
    expression::{Expression, PRIORITY_MAX, parse_expression_unreserved},
    keywords::Keyword,
    lexer::Token,
    parser::{ParseError, Parser},
    qualified_name::parse_qualified_name_unreserved,
};

/// ALTER TYPE actions
#[derive(Clone, Debug)]
pub enum AlterTypeAction<'a> {
    /// OWNER TO new_owner
    OwnerTo {
        owner_to_span: Span,
        new_owner: AlterTableOwner<'a>,
    },
    /// RENAME TO new_name
    RenameTo {
        rename_to_span: Span,
        new_name: Identifier<'a>,
    },
    /// SET SCHEMA new_schema
    SetSchema {
        set_schema_span: Span,
        new_schema: QualifiedName<'a>,
    },
    /// RENAME ATTRIBUTE attribute_name TO new_attribute_name [ CASCADE | RESTRICT ]
    RenameAttribute {
        rename_attribute_span: Span,
        attribute_name: Identifier<'a>,
        to_span: Span,
        new_attribute_name: Identifier<'a>,
        cascade_or_restrict: Option<CascadeOrRestrict>,
    },
    /// ADD VALUE [ IF NOT EXISTS ] new_enum_value [ { BEFORE | AFTER } neighbor_enum_value ]
    AddValue {
        add_value_span: Span,
        if_not_exists_span: Option<Span>, // IF NOT EXISTS
        new_enum_value: SString<'a>,
        placement: Option<(Span, SString<'a>)>, // (BEFORE/AFTER span, neighbor value)
    },
    /// RENAME VALUE existing_enum_value TO new_enum_value
    RenameValue {
        rename_value_span: Span,
        existing_enum_value: SString<'a>,
        to_span: Span,
        new_enum_value: SString<'a>,
    },
    /// ADD ATTRIBUTE attribute_name data_type [ COLLATE collation ] [ CASCADE | RESTRICT ]
    /// DROP ATTRIBUTE [ IF EXISTS ] attribute_name [ CASCADE | RESTRICT ]
    /// ALTER ATTRIBUTE attribute_name [ SET DATA ] TYPE data_type [ COLLATE collation ] [ CASCADE | RESTRICT ]
    Attributes { items: Vec<AttributeAction<'a>> },
    /// SET ( property = value [, ... ] )
    SetProperties {
        set_span: Span,
        properties: Vec<(Identifier<'a>, Expression<'a>)>,
    },
}

impl<'a> Spanned for AlterTypeAction<'a> {
    fn span(&self) -> Span {
        match self {
            AlterTypeAction::OwnerTo {
                owner_to_span,
                new_owner,
            } => owner_to_span.join_span(new_owner),
            AlterTypeAction::RenameTo {
                rename_to_span,
                new_name,
            } => rename_to_span.join_span(new_name),
            AlterTypeAction::SetSchema {
                set_schema_span,
                new_schema,
            } => set_schema_span.join_span(new_schema),
            AlterTypeAction::RenameAttribute {
                rename_attribute_span,
                attribute_name,
                to_span,
                new_attribute_name,
                cascade_or_restrict,
            } => rename_attribute_span
                .join_span(attribute_name)
                .join_span(to_span)
                .join_span(new_attribute_name)
                .join_span(cascade_or_restrict),
            AlterTypeAction::AddValue {
                add_value_span,
                if_not_exists_span,
                new_enum_value,
                placement,
            } => add_value_span
                .join_span(if_not_exists_span)
                .join_span(new_enum_value)
                .join_span(placement),
            AlterTypeAction::RenameValue {
                rename_value_span,
                existing_enum_value,
                to_span,
                new_enum_value,
            } => rename_value_span
                .join_span(existing_enum_value)
                .join_span(to_span)
                .join_span(new_enum_value),
            AlterTypeAction::Attributes { items } => items.opt_span().expect("Empty attributes"),
            AlterTypeAction::SetProperties {
                set_span,
                properties,
            } => set_span.join_span(properties),
        }
    }
}

/// Attribute actions for composite types
#[derive(Clone, Debug)]
pub enum AttributeAction<'a> {
    /// ADD ATTRIBUTE attribute_name data_type [ COLLATE collation ] [ CASCADE | RESTRICT ]
    Add {
        add_attribute_span: Span,
        attribute_name: Identifier<'a>,
        data_type: DataType<'a>,
        collate: Option<(Span, QualifiedName<'a>)>,
        cascade_or_restrict: Option<CascadeOrRestrict>,
    },
    /// DROP ATTRIBUTE [ IF EXISTS ] attribute_name [ CASCADE | RESTRICT ]
    Drop {
        drop_attribute_span: Span,
        if_exists_span: Option<Span>, // IF EXISTS
        attribute_name: Identifier<'a>,
        cascade_or_restrict: Option<CascadeOrRestrict>,
    },
    /// ALTER ATTRIBUTE attribute_name [ SET DATA ] TYPE data_type [ COLLATE collation ] [ CASCADE | RESTRICT ]
    Alter {
        alter_attribute_span: Span,
        attribute_name: Identifier<'a>,
        set_data_span: Option<Span>, // SET DATA
        type_span: Span,
        data_type: DataType<'a>,
        collate: Option<(Span, QualifiedName<'a>)>,
        cascade_or_restrict: Option<CascadeOrRestrict>,
    },
}

impl<'a> Spanned for AttributeAction<'a> {
    fn span(&self) -> Span {
        match self {
            AttributeAction::Add {
                add_attribute_span,
                attribute_name,
                data_type,
                collate,
                cascade_or_restrict,
            } => add_attribute_span
                .join_span(attribute_name)
                .join_span(data_type)
                .join_span(collate)
                .join_span(cascade_or_restrict),
            AttributeAction::Drop {
                drop_attribute_span,
                if_exists_span,
                attribute_name,
                cascade_or_restrict,
            } => drop_attribute_span
                .join_span(if_exists_span)
                .join_span(attribute_name)
                .join_span(cascade_or_restrict),
            AttributeAction::Alter {
                alter_attribute_span,
                attribute_name,
                set_data_span,
                type_span,
                data_type,
                collate,
                cascade_or_restrict,
            } => alter_attribute_span
                .join_span(attribute_name)
                .join_span(set_data_span)
                .join_span(type_span)
                .join_span(data_type)
                .join_span(collate)
                .join_span(cascade_or_restrict),
        }
    }
}

/// ALTER TYPE statement (PostgreSQL)
#[derive(Clone, Debug)]
pub struct AlterType<'a> {
    /// Span of "ALTER TYPE"
    pub alter_type_span: Span,
    /// Name of the type (possibly schema-qualified)
    pub name: QualifiedName<'a>,
    /// Action to perform
    pub action: AlterTypeAction<'a>,
}

impl<'a> Spanned for AlterType<'a> {
    fn span(&self) -> Span {
        self.alter_type_span
            .join_span(&self.name)
            .join_span(&self.action)
    }
}

pub(crate) fn parse_alter_type<'a>(
    parser: &mut Parser<'a, '_>,
    alter_type_span: Span,
) -> Result<AlterType<'a>, ParseError> {
    parser.postgres_only(&alter_type_span);
    let name = parse_qualified_name_unreserved(parser)?;

    let action = match &parser.token {
        Token::Ident(_, Keyword::OWNER) => {
            let owner_to_span = parser.consume_keywords(&[Keyword::OWNER, Keyword::TO])?;
            let new_owner = crate::alter_table::parse_alter_owner(parser)?;
            AlterTypeAction::OwnerTo {
                owner_to_span,
                new_owner,
            }
        }
        Token::Ident(_, Keyword::RENAME) => {
            let rename_span = parser.consume_keyword(Keyword::RENAME)?;
            match &parser.token {
                Token::Ident(_, Keyword::TO) => {
                    // RENAME TO new_name
                    let to_span = parser.consume_keyword(Keyword::TO)?;
                    let rename_to_span = rename_span.join_span(&to_span);
                    let new_name = parser.consume_plain_identifier_unreserved()?;
                    AlterTypeAction::RenameTo {
                        rename_to_span,
                        new_name,
                    }
                }
                Token::Ident(_, Keyword::ATTRIBUTE) => {
                    // RENAME ATTRIBUTE attribute_name TO new_attribute_name
                    let attribute_span = parser.consume_keyword(Keyword::ATTRIBUTE)?;
                    let rename_attribute_span = rename_span.join_span(&attribute_span);
                    let attribute_name = parser.consume_plain_identifier_unreserved()?;
                    let to_span = parser.consume_keyword(Keyword::TO)?;
                    let new_attribute_name = parser.consume_plain_identifier_unreserved()?;
                    let cascade_or_restrict = parse_cascade_or_restrict(parser);
                    AlterTypeAction::RenameAttribute {
                        rename_attribute_span,
                        attribute_name,
                        to_span,
                        new_attribute_name,
                        cascade_or_restrict,
                    }
                }
                Token::Ident(_, Keyword::VALUE) => {
                    // RENAME VALUE existing_enum_value TO new_enum_value
                    let value_span = parser.consume_keyword(Keyword::VALUE)?;
                    let rename_value_span = rename_span.join_span(&value_span);
                    let existing_enum_value = parser.consume_string()?;
                    let to_span = parser.consume_keyword(Keyword::TO)?;
                    let new_enum_value = parser.consume_string()?;
                    AlterTypeAction::RenameValue {
                        rename_value_span,
                        existing_enum_value,
                        to_span,
                        new_enum_value,
                    }
                }
                _ => parser.expected_failure("'TO', 'ATTRIBUTE', or 'VALUE' after 'RENAME'")?,
            }
        }
        Token::Ident(_, Keyword::SET) => {
            let set_span = parser.consume_keyword(Keyword::SET)?;
            match &parser.token {
                Token::Ident(_, Keyword::SCHEMA) => {
                    // SET SCHEMA new_schema
                    let schema_span = parser.consume_keyword(Keyword::SCHEMA)?;
                    let set_schema_span = set_span.join_span(&schema_span);
                    let new_schema = parse_qualified_name_unreserved(parser)?;
                    AlterTypeAction::SetSchema {
                        set_schema_span,
                        new_schema,
                    }
                }
                Token::LParen => {
                    // SET ( property = value [, ... ] )
                    parser.consume_token(Token::LParen)?;
                    let mut properties = Vec::new();
                    loop {
                        let property = parser.consume_plain_identifier_unreserved()?;
                        parser.consume_token(Token::Eq)?;
                        let value = parse_expression_unreserved(parser, PRIORITY_MAX)?;
                        properties.push((property, value));
                        if parser.skip_token(Token::Comma).is_none() {
                            break;
                        }
                    }
                    parser.consume_token(Token::RParen)?;
                    AlterTypeAction::SetProperties {
                        set_span,
                        properties,
                    }
                }
                _ => parser.expected_failure("'SCHEMA' or '(' after 'SET'")?,
            }
        }
        Token::Ident(_, Keyword::ADD) => {
            let add_span = parser.consume_keyword(Keyword::ADD)?;
            match &parser.token {
                Token::Ident(_, Keyword::VALUE) => {
                    // ADD VALUE [ IF NOT EXISTS ] new_enum_value [ { BEFORE | AFTER } neighbor_enum_value ]
                    let value_span = parser.consume_keyword(Keyword::VALUE)?;
                    let add_value_span = add_span.join_span(&value_span);
                    let if_not_exists_span = if let Some(if_span) = parser.skip_keyword(Keyword::IF)
                    {
                        Some(
                            parser
                                .consume_keywords(&[Keyword::NOT, Keyword::EXISTS])?
                                .join_span(&if_span),
                        )
                    } else {
                        None
                    };
                    let new_enum_value = parser.consume_string()?;
                    let placement = if let Some(before_span) = parser.skip_keyword(Keyword::BEFORE)
                    {
                        let neighbor = parser.consume_string()?;
                        Some((before_span, neighbor))
                    } else if let Some(after_span) = parser.skip_keyword(Keyword::AFTER) {
                        let neighbor = parser.consume_string()?;
                        Some((after_span, neighbor))
                    } else {
                        None
                    };
                    AlterTypeAction::AddValue {
                        add_value_span,
                        if_not_exists_span,
                        new_enum_value,
                        placement,
                    }
                }
                Token::Ident(_, Keyword::ATTRIBUTE) => {
                    // ADD ATTRIBUTE or composite type operations with multiple items
                    let mut items = Vec::new();
                    loop {
                        items.push(parse_attribute_action(parser)?);
                        if parser.skip_token(Token::Comma).is_none() {
                            break;
                        }
                    }
                    AlterTypeAction::Attributes { items }
                }
                _ => parser.expected_failure("'VALUE' or 'ATTRIBUTE' after 'ADD'")?,
            }
        }
        Token::Ident(_, Keyword::DROP) => {
            // DROP ATTRIBUTE or multiple operations
            let mut items = Vec::new();
            loop {
                items.push(parse_attribute_action(parser)?);
                if parser.skip_token(Token::Comma).is_none() {
                    break;
                }
            }
            AlterTypeAction::Attributes { items }
        }
        Token::Ident(_, Keyword::ALTER) => {
            // ALTER ATTRIBUTE or multiple operations
            let mut items = Vec::new();
            loop {
                items.push(parse_attribute_action(parser)?);
                if parser.skip_token(Token::Comma).is_none() {
                    break;
                }
            }
            AlterTypeAction::Attributes { items }
        }
        _ => parser.expected_failure(
            "'OWNER', 'RENAME', 'SET', 'ADD', 'DROP', or 'ALTER' after type name",
        )?,
    };

    Ok(AlterType {
        alter_type_span,
        name,
        action,
    })
}

fn parse_attribute_action<'a>(
    parser: &mut Parser<'a, '_>,
) -> Result<AttributeAction<'a>, ParseError> {
    match &parser.token {
        Token::Ident(_, Keyword::ADD) => {
            // ADD ATTRIBUTE attribute_name data_type [ COLLATE collation ] [ CASCADE | RESTRICT ]
            let add_attribute_span =
                parser.consume_keywords(&[Keyword::ADD, Keyword::ATTRIBUTE])?;
            let attribute_name = parser.consume_plain_identifier_unreserved()?;
            let data_type = parse_data_type(parser, DataTypeContext::Column)?;
            let collate = if let Some(collate_span) = parser.skip_keyword(Keyword::COLLATE) {
                let collation = parse_qualified_name_unreserved(parser)?;
                Some((collate_span, collation))
            } else {
                None
            };
            let cascade_or_restrict = parse_cascade_or_restrict(parser);
            Ok(AttributeAction::Add {
                add_attribute_span,
                attribute_name,
                data_type,
                collate,
                cascade_or_restrict,
            })
        }
        Token::Ident(_, Keyword::DROP) => {
            // DROP ATTRIBUTE [ IF EXISTS ] attribute_name [ CASCADE | RESTRICT ]
            let drop_attribute_span =
                parser.consume_keywords(&[Keyword::DROP, Keyword::ATTRIBUTE])?;
            let if_exists_span = if let Some(if_span) = parser.skip_keyword(Keyword::IF) {
                Some(parser.consume_keyword(Keyword::EXISTS)?.join_span(&if_span))
            } else {
                None
            };
            let attribute_name = parser.consume_plain_identifier_unreserved()?;
            let cascade_or_restrict = parse_cascade_or_restrict(parser);
            Ok(AttributeAction::Drop {
                drop_attribute_span,
                if_exists_span,
                attribute_name,
                cascade_or_restrict,
            })
        }
        Token::Ident(_, Keyword::ALTER) => {
            // ALTER ATTRIBUTE attribute_name [ SET DATA ] TYPE data_type [ COLLATE collation ] [ CASCADE | RESTRICT ]
            let alter_attribute_span =
                parser.consume_keywords(&[Keyword::ALTER, Keyword::ATTRIBUTE])?;
            let attribute_name = parser.consume_plain_identifier_unreserved()?;
            let set_data_span = if let Some(set_span) = parser.skip_keyword(Keyword::SET) {
                Some(parser.consume_keyword(Keyword::DATA)?.join_span(&set_span))
            } else {
                None
            };
            let type_span = parser.consume_keyword(Keyword::TYPE)?;
            let data_type = parse_data_type(parser, DataTypeContext::Column)?;
            let collate = if let Some(collate_span) = parser.skip_keyword(Keyword::COLLATE) {
                let collation = parse_qualified_name_unreserved(parser)?;
                Some((collate_span, collation))
            } else {
                None
            };
            let cascade_or_restrict = parse_cascade_or_restrict(parser);
            Ok(AttributeAction::Alter {
                alter_attribute_span,
                attribute_name,
                set_data_span,
                type_span,
                data_type,
                collate,
                cascade_or_restrict,
            })
        }
        _ => parser.expected_failure("'ADD', 'DROP', or 'ALTER' for attribute action"),
    }
}