Skip to content

Commit c55fa4b

Browse files
Encapsulate CreateIndex
1 parent b4fe194 commit c55fa4b

File tree

5 files changed

+42
-37
lines changed

5 files changed

+42
-37
lines changed

src/ast/dml.rs

+18
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,24 @@ use super::{
2626
SqliteOnConflict, TableWithJoins,
2727
};
2828

29+
/// CREATE TABLE statement.
30+
#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
31+
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
32+
#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
33+
pub struct CreateIndex {
34+
/// index name
35+
pub name: Option<ObjectName>,
36+
#[cfg_attr(feature = "visitor", visit(with = "visit_relation"))]
37+
pub table_name: ObjectName,
38+
pub using: Option<Ident>,
39+
pub columns: Vec<OrderByExpr>,
40+
pub unique: bool,
41+
pub concurrently: bool,
42+
pub if_not_exists: bool,
43+
pub include: Vec<Ident>,
44+
pub nulls_distinct: Option<bool>,
45+
pub predicate: Option<Expr>,
46+
}
2947
/// CREATE TABLE statement.
3048
#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
3149
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]

src/ast/mod.rs

+4-17
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ pub use self::ddl::{
3838
ReferentialAction, TableConstraint, UserDefinedTypeCompositeAttributeDef,
3939
UserDefinedTypeRepresentation, ViewColumnDef,
4040
};
41-
pub use self::dml::{CreateTable, Delete, Insert};
41+
pub use self::dml::{CreateIndex, CreateTable, Delete, Insert};
4242
pub use self::operator::{BinaryOperator, UnaryOperator};
4343
pub use self::query::{
4444
AfterMatchSkip, ConnectBy, Cte, CteAsMaterialized, Distinct, EmptyMatchesMode,
@@ -1983,20 +1983,7 @@ pub enum Statement {
19831983
/// ```sql
19841984
/// `CREATE INDEX`
19851985
/// ```
1986-
CreateIndex {
1987-
/// index name
1988-
name: Option<ObjectName>,
1989-
#[cfg_attr(feature = "visitor", visit(with = "visit_relation"))]
1990-
table_name: ObjectName,
1991-
using: Option<Ident>,
1992-
columns: Vec<OrderByExpr>,
1993-
unique: bool,
1994-
concurrently: bool,
1995-
if_not_exists: bool,
1996-
include: Vec<Ident>,
1997-
nulls_distinct: Option<bool>,
1998-
predicate: Option<Expr>,
1999-
},
1986+
CreateIndex(CreateIndex),
20001987
/// ```sql
20011988
/// CREATE ROLE
20021989
/// ```
@@ -3466,7 +3453,7 @@ impl fmt::Display for Statement {
34663453
}
34673454
Ok(())
34683455
}
3469-
Statement::CreateIndex {
3456+
Statement::CreateIndex(CreateIndex {
34703457
name,
34713458
table_name,
34723459
using,
@@ -3477,7 +3464,7 @@ impl fmt::Display for Statement {
34773464
include,
34783465
nulls_distinct,
34793466
predicate,
3480-
} => {
3467+
}) => {
34813468
write!(
34823469
f,
34833470
"CREATE {unique}INDEX {concurrently}{if_not_exists}",

src/parser/mod.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -4740,7 +4740,7 @@ impl<'a> Parser<'a> {
47404740
None
47414741
};
47424742

4743-
Ok(Statement::CreateIndex {
4743+
Ok(Statement::CreateIndex(CreateIndex {
47444744
name: index_name,
47454745
table_name,
47464746
using,
@@ -4751,7 +4751,7 @@ impl<'a> Parser<'a> {
47514751
include,
47524752
nulls_distinct,
47534753
predicate,
4754-
})
4754+
}))
47554755
}
47564756

47574757
pub fn parse_create_extension(&mut self) -> Result<Statement, ParserError> {

tests/sqlparser_common.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -7195,14 +7195,14 @@ fn parse_create_index() {
71957195
},
71967196
];
71977197
match verified_stmt(sql) {
7198-
Statement::CreateIndex {
7198+
Statement::CreateIndex(CreateIndex {
71997199
name: Some(name),
72007200
table_name,
72017201
columns,
72027202
unique,
72037203
if_not_exists,
72047204
..
7205-
} => {
7205+
}) => {
72067206
assert_eq!("idx_name", name.to_string());
72077207
assert_eq!("test", table_name.to_string());
72087208
assert_eq!(indexed_columns, columns);
@@ -7229,7 +7229,7 @@ fn test_create_index_with_using_function() {
72297229
},
72307230
];
72317231
match verified_stmt(sql) {
7232-
Statement::CreateIndex {
7232+
Statement::CreateIndex(CreateIndex {
72337233
name: Some(name),
72347234
table_name,
72357235
using,
@@ -7240,7 +7240,7 @@ fn test_create_index_with_using_function() {
72407240
include,
72417241
nulls_distinct: None,
72427242
predicate: None,
7243-
} => {
7243+
}) => {
72447244
assert_eq!("idx_name", name.to_string());
72457245
assert_eq!("test", table_name.to_string());
72467246
assert_eq!("btree", using.unwrap().to_string());

tests/sqlparser_postgres.rs

+14-14
Original file line numberDiff line numberDiff line change
@@ -1952,7 +1952,7 @@ fn parse_array_index_expr() {
19521952
fn parse_create_index() {
19531953
let sql = "CREATE INDEX IF NOT EXISTS my_index ON my_table(col1,col2)";
19541954
match pg().verified_stmt(sql) {
1955-
Statement::CreateIndex {
1955+
Statement::CreateIndex(CreateIndex {
19561956
name: Some(ObjectName(name)),
19571957
table_name: ObjectName(table_name),
19581958
using,
@@ -1963,7 +1963,7 @@ fn parse_create_index() {
19631963
nulls_distinct: None,
19641964
include,
19651965
predicate: None,
1966-
} => {
1966+
}) => {
19671967
assert_eq_vec(&["my_index"], &name);
19681968
assert_eq_vec(&["my_table"], &table_name);
19691969
assert_eq!(None, using);
@@ -1981,7 +1981,7 @@ fn parse_create_index() {
19811981
fn parse_create_anonymous_index() {
19821982
let sql = "CREATE INDEX ON my_table(col1,col2)";
19831983
match pg().verified_stmt(sql) {
1984-
Statement::CreateIndex {
1984+
Statement::CreateIndex(CreateIndex {
19851985
name,
19861986
table_name: ObjectName(table_name),
19871987
using,
@@ -1992,7 +1992,7 @@ fn parse_create_anonymous_index() {
19921992
include,
19931993
nulls_distinct: None,
19941994
predicate: None,
1995-
} => {
1995+
}) => {
19961996
assert_eq!(None, name);
19971997
assert_eq_vec(&["my_table"], &table_name);
19981998
assert_eq!(None, using);
@@ -2010,7 +2010,7 @@ fn parse_create_anonymous_index() {
20102010
fn parse_create_index_concurrently() {
20112011
let sql = "CREATE INDEX CONCURRENTLY IF NOT EXISTS my_index ON my_table(col1,col2)";
20122012
match pg().verified_stmt(sql) {
2013-
Statement::CreateIndex {
2013+
Statement::CreateIndex(CreateIndex {
20142014
name: Some(ObjectName(name)),
20152015
table_name: ObjectName(table_name),
20162016
using,
@@ -2021,7 +2021,7 @@ fn parse_create_index_concurrently() {
20212021
include,
20222022
nulls_distinct: None,
20232023
predicate: None,
2024-
} => {
2024+
}) => {
20252025
assert_eq_vec(&["my_index"], &name);
20262026
assert_eq_vec(&["my_table"], &table_name);
20272027
assert_eq!(None, using);
@@ -2039,7 +2039,7 @@ fn parse_create_index_concurrently() {
20392039
fn parse_create_index_with_predicate() {
20402040
let sql = "CREATE INDEX IF NOT EXISTS my_index ON my_table(col1,col2) WHERE col3 IS NULL";
20412041
match pg().verified_stmt(sql) {
2042-
Statement::CreateIndex {
2042+
Statement::CreateIndex(CreateIndex {
20432043
name: Some(ObjectName(name)),
20442044
table_name: ObjectName(table_name),
20452045
using,
@@ -2050,7 +2050,7 @@ fn parse_create_index_with_predicate() {
20502050
include,
20512051
nulls_distinct: None,
20522052
predicate: Some(_),
2053-
} => {
2053+
}) => {
20542054
assert_eq_vec(&["my_index"], &name);
20552055
assert_eq_vec(&["my_table"], &table_name);
20562056
assert_eq!(None, using);
@@ -2068,7 +2068,7 @@ fn parse_create_index_with_predicate() {
20682068
fn parse_create_index_with_include() {
20692069
let sql = "CREATE INDEX IF NOT EXISTS my_index ON my_table(col1,col2) INCLUDE (col3)";
20702070
match pg().verified_stmt(sql) {
2071-
Statement::CreateIndex {
2071+
Statement::CreateIndex(CreateIndex {
20722072
name: Some(ObjectName(name)),
20732073
table_name: ObjectName(table_name),
20742074
using,
@@ -2079,7 +2079,7 @@ fn parse_create_index_with_include() {
20792079
include,
20802080
nulls_distinct: None,
20812081
predicate: None,
2082-
} => {
2082+
}) => {
20832083
assert_eq_vec(&["my_index"], &name);
20842084
assert_eq_vec(&["my_table"], &table_name);
20852085
assert_eq!(None, using);
@@ -2097,7 +2097,7 @@ fn parse_create_index_with_include() {
20972097
fn parse_create_index_with_nulls_distinct() {
20982098
let sql = "CREATE INDEX IF NOT EXISTS my_index ON my_table(col1,col2) NULLS NOT DISTINCT";
20992099
match pg().verified_stmt(sql) {
2100-
Statement::CreateIndex {
2100+
Statement::CreateIndex(CreateIndex {
21012101
name: Some(ObjectName(name)),
21022102
table_name: ObjectName(table_name),
21032103
using,
@@ -2108,7 +2108,7 @@ fn parse_create_index_with_nulls_distinct() {
21082108
include,
21092109
nulls_distinct: Some(nulls_distinct),
21102110
predicate: None,
2111-
} => {
2111+
}) => {
21122112
assert_eq_vec(&["my_index"], &name);
21132113
assert_eq_vec(&["my_table"], &table_name);
21142114
assert_eq!(None, using);
@@ -2124,7 +2124,7 @@ fn parse_create_index_with_nulls_distinct() {
21242124

21252125
let sql = "CREATE INDEX IF NOT EXISTS my_index ON my_table(col1,col2) NULLS DISTINCT";
21262126
match pg().verified_stmt(sql) {
2127-
Statement::CreateIndex {
2127+
Statement::CreateIndex(CreateIndex {
21282128
name: Some(ObjectName(name)),
21292129
table_name: ObjectName(table_name),
21302130
using,
@@ -2135,7 +2135,7 @@ fn parse_create_index_with_nulls_distinct() {
21352135
include,
21362136
nulls_distinct: Some(nulls_distinct),
21372137
predicate: None,
2138-
} => {
2138+
}) => {
21392139
assert_eq_vec(&["my_index"], &name);
21402140
assert_eq_vec(&["my_table"], &table_name);
21412141
assert_eq!(None, using);

0 commit comments

Comments
 (0)