Skip to content

Commit fc0398a

Browse files
committed
Merge remote-tracking branch 'origin/main'
2 parents 0048c9f + 0b589b2 commit fc0398a

10 files changed

Lines changed: 269 additions & 91 deletions

File tree

src/ast/dml.rs

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,8 @@ use super::{
3333
display_comma_separated, helpers::attached_token::AttachedToken, query::InputFormatClause,
3434
Assignment, Expr, FromTable, Ident, InsertAliases, MysqlInsertPriority, ObjectName, OnInsert,
3535
OptimizerHint, OrderByExpr, Query, SelectInto, SelectItem, Setting, SqliteOnConflict,
36-
TableFactor, TableObject, TableWithJoins, UpdateTableFromKind, Values,
36+
TableAliasWithoutColumns, TableFactor, TableObject, TableWithJoins, UpdateTableFromKind,
37+
Values,
3738
};
3839

3940
/// INSERT statement.
@@ -56,8 +57,9 @@ pub struct Insert {
5657
pub into: bool,
5758
/// TABLE
5859
pub table: TableObject,
59-
/// table_name as foo (for PostgreSQL)
60-
pub table_alias: Option<Ident>,
60+
/// `table_name as foo` (for PostgreSQL)
61+
/// `table_name foo` (for Oracle)
62+
pub table_alias: Option<TableAliasWithoutColumns>,
6163
/// COLUMNS
6264
pub columns: Vec<Ident>,
6365
/// Overwrite (Hive)
@@ -125,8 +127,13 @@ pub struct Insert {
125127
impl Display for Insert {
126128
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
127129
// SQLite OR conflict has a special format: INSERT OR ... INTO table_name
128-
let table_name = if let Some(alias) = &self.table_alias {
129-
format!("{0} AS {alias}", self.table)
130+
let table_name = if let Some(table_alias) = &self.table_alias {
131+
format!(
132+
"{table} {as_keyword}{alias}",
133+
table = self.table,
134+
as_keyword = if table_alias.explicit { "AS " } else { "" },
135+
alias = table_alias.alias
136+
)
130137
} else {
131138
self.table.to_string()
132139
};

src/ast/mod.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6480,6 +6480,17 @@ pub struct InsertAliases {
64806480
pub col_aliases: Option<Vec<Ident>>,
64816481
}
64826482

6483+
#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
6484+
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
6485+
#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
6486+
/// Optional alias for an `INSERT` table; i.e. the table to be inserted into
6487+
pub struct TableAliasWithoutColumns {
6488+
/// `true` if the aliases was explicitly introduced with the "AS" keyword
6489+
pub explicit: bool,
6490+
/// the alias name itself
6491+
pub alias: Ident,
6492+
}
6493+
64836494
#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
64846495
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
64856496
#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]

src/ast/spans.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1327,7 +1327,7 @@ impl Spanned for Insert {
13271327
union_spans(
13281328
core::iter::once(insert_token.0.span)
13291329
.chain(core::iter::once(table.span()))
1330-
.chain(table_alias.as_ref().map(|i| i.span))
1330+
.chain(table_alias.iter().map(|k| k.alias.span))
13311331
.chain(columns.iter().map(|i| i.span))
13321332
.chain(source.as_ref().map(|q| q.span()))
13331333
.chain(assignments.iter().map(|i| i.span()))

src/dialect/mod.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1240,6 +1240,11 @@ pub trait Dialect: Debug + Any {
12401240
false
12411241
}
12421242

1243+
/// Returns true if this dialect supports `INSERT INTO t [[AS] alias] ...`.
1244+
fn supports_insert_table_alias(&self) -> bool {
1245+
false
1246+
}
1247+
12431248
/// Returns true if this dialect supports `SET` statements without an explicit
12441249
/// assignment operator such as `=`. For example: `SET SHOWPLAN_XML ON`.
12451250
fn supports_set_stmt_without_operator(&self) -> bool {

src/dialect/oracle.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,4 +110,8 @@ impl Dialect for OracleDialect {
110110
fn supports_comment_optimizer_hint(&self) -> bool {
111111
true
112112
}
113+
114+
fn supports_insert_table_alias(&self) -> bool {
115+
true
116+
}
113117
}

src/dialect/postgresql.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -288,4 +288,8 @@ impl Dialect for PostgreSqlDialect {
288288
fn supports_interval_options(&self) -> bool {
289289
true
290290
}
291+
292+
fn supports_insert_table_alias(&self) -> bool {
293+
true
294+
}
291295
}

0 commit comments

Comments
 (0)