|
21 | 21 | use pretty_assertions::assert_eq; |
22 | 22 |
|
23 | 23 | use sqlparser::{ |
24 | | - ast::{BinaryOperator, Expr, Ident, QuoteDelimitedString, Value, ValueWithSpan}, |
| 24 | + ast::{ |
| 25 | + BinaryOperator, Expr, Ident, Insert, ObjectName, Query, QuoteDelimitedString, SetExpr, |
| 26 | + Statement, TableAliasWithoutColumns, TableObject, Value, ValueWithSpan, |
| 27 | + }, |
25 | 28 | dialect::OracleDialect, |
26 | 29 | parser::ParserError, |
27 | 30 | tokenizer::Span, |
@@ -421,3 +424,106 @@ fn test_connect_by() { |
421 | 424 | ORDER BY \"Employee\", \"Manager\", \"Pathlen\", \"Path\"", |
422 | 425 | ); |
423 | 426 | } |
| 427 | + |
| 428 | +#[test] |
| 429 | +fn test_insert_with_table_alias() { |
| 430 | + let oracle_dialect = oracle(); |
| 431 | + |
| 432 | + fn verify_table_name_with_alias(stmt: &Statement, exp_table_name: &str, exp_table_alias: &str) { |
| 433 | + assert!(matches!(stmt, |
| 434 | + Statement::Insert(Insert { |
| 435 | + table: TableObject::TableName(table_name), |
| 436 | + table_alias: Some(TableAliasWithoutColumns { |
| 437 | + explicit: false, |
| 438 | + alias: Ident { |
| 439 | + value: table_alias, |
| 440 | + quote_style: None, |
| 441 | + span: _ |
| 442 | + } |
| 443 | + }), |
| 444 | + .. |
| 445 | + }) |
| 446 | + if table_alias == exp_table_alias |
| 447 | + && table_name == &ObjectName::from(vec![Ident { |
| 448 | + value: exp_table_name.into(), |
| 449 | + quote_style: None, |
| 450 | + span: Span::empty(), |
| 451 | + }]) |
| 452 | + )); |
| 453 | + } |
| 454 | + |
| 455 | + let stmt = oracle_dialect.verified_stmt( |
| 456 | + "INSERT INTO foo_t t \ |
| 457 | + SELECT 1, 2, 3 FROM dual", |
| 458 | + ); |
| 459 | + verify_table_name_with_alias(&stmt, "foo_t", "t"); |
| 460 | + |
| 461 | + let stmt = oracle_dialect.verified_stmt( |
| 462 | + "INSERT INTO foo_t asdf (a, b, c) \ |
| 463 | + SELECT 1, 2, 3 FROM dual", |
| 464 | + ); |
| 465 | + verify_table_name_with_alias(&stmt, "foo_t", "asdf"); |
| 466 | + |
| 467 | + let stmt = oracle_dialect.verified_stmt( |
| 468 | + "INSERT INTO foo_t t (a, b, c) \ |
| 469 | + VALUES (1, 2, 3)", |
| 470 | + ); |
| 471 | + verify_table_name_with_alias(&stmt, "foo_t", "t"); |
| 472 | + |
| 473 | + let stmt = oracle_dialect.verified_stmt( |
| 474 | + "INSERT INTO foo_t t \ |
| 475 | + VALUES (1, 2, 3)", |
| 476 | + ); |
| 477 | + verify_table_name_with_alias(&stmt, "foo_t", "t"); |
| 478 | +} |
| 479 | + |
| 480 | +#[test] |
| 481 | +fn test_insert_without_alias() { |
| 482 | + let oracle_dialect = oracle(); |
| 483 | + |
| 484 | + // check DEFAULT |
| 485 | + let sql = "INSERT INTO t default SELECT 'a' FROM dual"; |
| 486 | + assert_eq!( |
| 487 | + oracle_dialect.parse_sql_statements(sql), |
| 488 | + Err(ParserError::ParserError( |
| 489 | + "Expected: SELECT, VALUES, or a subquery in the query body, found: default".into() |
| 490 | + )) |
| 491 | + ); |
| 492 | + |
| 493 | + // check SELECT |
| 494 | + let sql = "INSERT INTO t SELECT 'a' FROM dual"; |
| 495 | + let stmt = oracle_dialect.verified_stmt(sql); |
| 496 | + assert!(matches!( |
| 497 | + &stmt, |
| 498 | + Statement::Insert(Insert { |
| 499 | + table_alias: None, |
| 500 | + source: Some(source), |
| 501 | + .. |
| 502 | + }) |
| 503 | + if matches!(&**source, Query { body, .. } if matches!(&**body, SetExpr::Select(_))))); |
| 504 | + |
| 505 | + // check WITH |
| 506 | + let sql = "INSERT INTO dual WITH w AS (SELECT 1 AS y FROM dual) SELECT y FROM w"; |
| 507 | + let stmt = oracle_dialect.verified_stmt(sql); |
| 508 | + assert!(matches!( |
| 509 | + &stmt, |
| 510 | + Statement::Insert(Insert { |
| 511 | + table_alias: None, |
| 512 | + source: Some(source), |
| 513 | + .. |
| 514 | + }) |
| 515 | + if matches!(&**source, Query { body, .. } if matches!(&**body, SetExpr::Select(_))))); |
| 516 | + |
| 517 | + // check VALUES |
| 518 | + let sql = "INSERT INTO t VALUES (1)"; |
| 519 | + let stmt = oracle_dialect.verified_stmt(sql); |
| 520 | + assert!(matches!( |
| 521 | + stmt, |
| 522 | + Statement::Insert(Insert { |
| 523 | + table_alias: None, |
| 524 | + source: Some(source), |
| 525 | + .. |
| 526 | + }) |
| 527 | + if matches!(&*source, Query { body, .. } if matches!(&**body, SetExpr::Values(_))) |
| 528 | + )); |
| 529 | +} |
0 commit comments