Skip to content

Commit 523f086

Browse files
committed
Introduce SQLObjectName struct (4.1/4.4)
(To store "A name of a table, view, custom type, etc., possibly multi-part, i.e. db.schema.obj".) Before this change - some places used `String` for this (these are updated in this commit) - while others (notably SQLStatement::SQLDelete::relation, which is the reason for this series of commits) relied on ASTNode::SQLCompoundIdentifier (which is also backed by a Vec<SQLIdent>, but, as a variant of ASTNode enum, is not convenient to use when you know you need that specific variant).
1 parent 215820e commit 523f086

File tree

6 files changed

+42
-27
lines changed

6 files changed

+42
-27
lines changed

src/sqlast/mod.rs

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -153,15 +153,15 @@ pub enum SQLStatement {
153153
/// INSERT
154154
SQLInsert {
155155
/// TABLE
156-
table_name: String,
156+
table_name: SQLObjectName,
157157
/// COLUMNS
158158
columns: Vec<SQLIdent>,
159159
/// VALUES (vector of rows to insert)
160160
values: Vec<Vec<ASTNode>>,
161161
},
162162
SQLCopy {
163163
/// TABLE
164-
table_name: String,
164+
table_name: SQLObjectName,
165165
/// COLUMNS
166166
columns: Vec<SQLIdent>,
167167
/// VALUES a vector of values to be copied
@@ -170,7 +170,7 @@ pub enum SQLStatement {
170170
/// UPDATE
171171
SQLUpdate {
172172
/// TABLE
173-
table_name: String,
173+
table_name: SQLObjectName,
174174
/// Column assignments
175175
assignments: Vec<SQLAssignment>,
176176
/// WHERE
@@ -186,14 +186,14 @@ pub enum SQLStatement {
186186
/// CREATE TABLE
187187
SQLCreateTable {
188188
/// Table name
189-
name: String,
189+
name: SQLObjectName,
190190
/// Optional schema
191191
columns: Vec<SQLColumnDef>,
192192
},
193193
/// ALTER TABLE
194194
SQLAlterTable {
195195
/// Table name
196-
name: String,
196+
name: SQLObjectName,
197197
operation: AlterOperation,
198198
},
199199
}
@@ -207,7 +207,7 @@ impl ToString for SQLStatement {
207207
columns,
208208
values,
209209
} => {
210-
let mut s = format!("INSERT INTO {}", table_name);
210+
let mut s = format!("INSERT INTO {}", table_name.to_string());
211211
if columns.len() > 0 {
212212
s += &format!(" ({})", columns.join(", "));
213213
}
@@ -232,7 +232,7 @@ impl ToString for SQLStatement {
232232
columns,
233233
values,
234234
} => {
235-
let mut s = format!("COPY {}", table_name);
235+
let mut s = format!("COPY {}", table_name.to_string());
236236
if columns.len() > 0 {
237237
s += &format!(
238238
" ({})",
@@ -262,7 +262,7 @@ impl ToString for SQLStatement {
262262
assignments,
263263
selection,
264264
} => {
265-
let mut s = format!("UPDATE {}", table_name);
265+
let mut s = format!("UPDATE {}", table_name.to_string());
266266
if assignments.len() > 0 {
267267
s += &format!(
268268
"{}",
@@ -293,20 +293,30 @@ impl ToString for SQLStatement {
293293
}
294294
SQLStatement::SQLCreateTable { name, columns } => format!(
295295
"CREATE TABLE {} ({})",
296-
name,
296+
name.to_string(),
297297
columns
298298
.iter()
299299
.map(|c| c.to_string())
300300
.collect::<Vec<String>>()
301301
.join(", ")
302302
),
303303
SQLStatement::SQLAlterTable { name, operation } => {
304-
format!("ALTER TABLE {} {}", name, operation.to_string())
304+
format!("ALTER TABLE {} {}", name.to_string(), operation.to_string())
305305
}
306306
}
307307
}
308308
}
309309

310+
/// A name of a table, view, custom type, etc., possibly multi-part, i.e. db.schema.obj
311+
#[derive(Debug, Clone, PartialEq)]
312+
pub struct SQLObjectName(pub Vec<SQLIdent>);
313+
314+
impl ToString for SQLObjectName {
315+
fn to_string(&self) -> String {
316+
self.0.join(".")
317+
}
318+
}
319+
310320
/// SQL assignment `foo = expr` as used in SQLUpdate
311321
#[derive(Debug, Clone, PartialEq)]
312322
pub struct SQLAssignment {

src/sqlast/sqltype.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
use super::SQLObjectName;
2+
13
/// SQL datatypes for literals in SQL statements
24
#[derive(Debug, Clone, PartialEq)]
35
pub enum SQLType {
@@ -44,7 +46,7 @@ pub enum SQLType {
4446
/// Bytea
4547
Bytea,
4648
/// Custom type such as enums
47-
Custom(String),
49+
Custom(SQLObjectName),
4850
/// Arrays
4951
Array(Box<SQLType>),
5052
}

src/sqlast/table_key.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use super::SQLIdent;
1+
use super::{SQLIdent, SQLObjectName};
22

33
#[derive(Debug, PartialEq, Clone)]
44
pub enum AlterOperation {
@@ -30,7 +30,7 @@ pub enum TableKey {
3030
Key(Key),
3131
ForeignKey {
3232
key: Key,
33-
foreign_table: String,
33+
foreign_table: SQLObjectName,
3434
referred_columns: Vec<SQLIdent>,
3535
},
3636
}
@@ -53,7 +53,7 @@ impl ToString for TableKey {
5353
"{} FOREIGN KEY ({}) REFERENCES {}({})",
5454
key.name,
5555
key.columns.join(", "),
56-
foreign_table,
56+
foreign_table.to_string(),
5757
referred_columns.join(", ")
5858
),
5959
}

src/sqlparser.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1060,11 +1060,11 @@ impl Parser {
10601060
}
10611061
}
10621062

1063-
pub fn parse_tablename(&mut self) -> Result<String, ParserError> {
1063+
pub fn parse_tablename(&mut self) -> Result<SQLObjectName, ParserError> {
10641064
let identifier = self.parse_compound_identifier(&Token::Period)?;
10651065
match identifier {
10661066
// TODO: should store the compound identifier itself
1067-
ASTNode::SQLCompoundIdentifier(idents) => Ok(idents.join(".")),
1067+
ASTNode::SQLCompoundIdentifier(idents) => Ok(SQLObjectName(idents)),
10681068
other => parser_err!(format!("Expecting compound identifier, found: {:?}", other)),
10691069
}
10701070
}

tests/sqlparser_generic.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -364,7 +364,7 @@ fn parse_create_table() {
364364
);
365365
match ast {
366366
SQLStatement::SQLCreateTable { name, columns } => {
367-
assert_eq!("uk_cities", name);
367+
assert_eq!("uk_cities", name.to_string());
368368
assert_eq!(3, columns.len());
369369

370370
let c_name = &columns[0];

tests/sqlparser_postgres.rs

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ fn parse_simple_insert() {
3131
values,
3232
..
3333
} => {
34-
assert_eq!(table_name, "customer");
34+
assert_eq!(table_name.to_string(), "customer");
3535
assert!(columns.is_empty());
3636
assert_eq!(
3737
vec![vec![
@@ -56,7 +56,7 @@ fn parse_common_insert() {
5656
values,
5757
..
5858
} => {
59-
assert_eq!(table_name, "public.customer");
59+
assert_eq!(table_name.to_string(), "public.customer");
6060
assert!(columns.is_empty());
6161
assert_eq!(
6262
vec![vec![
@@ -81,7 +81,7 @@ fn parse_complex_insert() {
8181
values,
8282
..
8383
} => {
84-
assert_eq!(table_name, "db.public.customer");
84+
assert_eq!(table_name.to_string(), "db.public.customer");
8585
assert!(columns.is_empty());
8686
assert_eq!(
8787
vec![vec![
@@ -120,7 +120,7 @@ fn parse_insert_with_columns() {
120120
values,
121121
..
122122
} => {
123-
assert_eq!(table_name, "public.customer");
123+
assert_eq!(table_name.to_string(), "public.customer");
124124
assert_eq!(
125125
columns,
126126
vec!["id".to_string(), "name".to_string(), "active".to_string()]
@@ -164,7 +164,7 @@ fn parse_create_table_with_defaults() {
164164
);
165165
match one_statement_parses_to(&sql, "") {
166166
SQLStatement::SQLCreateTable { name, columns } => {
167-
assert_eq!("public.customer", name);
167+
assert_eq!("public.customer", name.to_string());
168168
assert_eq!(10, columns.len());
169169

170170
let c_name = &columns[0];
@@ -205,7 +205,7 @@ fn parse_create_table_from_pg_dump() {
205205
)");
206206
match one_statement_parses_to(&sql, "") {
207207
SQLStatement::SQLCreateTable { name, columns } => {
208-
assert_eq!("public.customer", name);
208+
assert_eq!("public.customer", name.to_string());
209209

210210
let c_customer_id = &columns[0];
211211
assert_eq!("customer_id", c_customer_id.name);
@@ -238,7 +238,10 @@ fn parse_create_table_from_pg_dump() {
238238

239239
let c_release_year = &columns[10];
240240
assert_eq!(
241-
SQLType::Custom("public.year".to_string()),
241+
SQLType::Custom(SQLObjectName(vec![
242+
"public".to_string(),
243+
"year".to_string()
244+
])),
242245
c_release_year.data_type
243246
);
244247
}
@@ -259,7 +262,7 @@ fn parse_create_table_with_inherit() {
259262
);
260263
match verified_stmt(&sql) {
261264
SQLStatement::SQLCreateTable { name, columns } => {
262-
assert_eq!("bazaar.settings", name);
265+
assert_eq!("bazaar.settings", name.to_string());
263266

264267
let c_name = &columns[0];
265268
assert_eq!("settings_id", c_name.name);
@@ -288,7 +291,7 @@ fn parse_alter_table_constraint_primary_key() {
288291
);
289292
match verified_stmt(&sql) {
290293
SQLStatement::SQLAlterTable { name, .. } => {
291-
assert_eq!(name, "bazaar.address");
294+
assert_eq!(name.to_string(), "bazaar.address");
292295
}
293296
_ => assert!(false),
294297
}
@@ -301,7 +304,7 @@ fn parse_alter_table_constraint_foreign_key() {
301304
ADD CONSTRAINT customer_address_id_fkey FOREIGN KEY (address_id) REFERENCES public.address(address_id)");
302305
match verified_stmt(&sql) {
303306
SQLStatement::SQLAlterTable { name, .. } => {
304-
assert_eq!(name, "public.customer");
307+
assert_eq!(name.to_string(), "public.customer");
305308
}
306309
_ => assert!(false),
307310
}

0 commit comments

Comments
 (0)