Skip to content

Commit

Permalink
Encapsulate CreateIndex
Browse files Browse the repository at this point in the history
  • Loading branch information
philipcristiano committed May 29, 2024
1 parent b4fe194 commit c55fa4b
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 37 deletions.
18 changes: 18 additions & 0 deletions src/ast/dml.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,24 @@ use super::{
SqliteOnConflict, TableWithJoins,
};

/// CREATE TABLE statement.
#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
pub struct CreateIndex {
/// index name
pub name: Option<ObjectName>,
#[cfg_attr(feature = "visitor", visit(with = "visit_relation"))]
pub table_name: ObjectName,
pub using: Option<Ident>,
pub columns: Vec<OrderByExpr>,
pub unique: bool,
pub concurrently: bool,
pub if_not_exists: bool,
pub include: Vec<Ident>,
pub nulls_distinct: Option<bool>,
pub predicate: Option<Expr>,
}
/// CREATE TABLE statement.
#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
Expand Down
21 changes: 4 additions & 17 deletions src/ast/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ pub use self::ddl::{
ReferentialAction, TableConstraint, UserDefinedTypeCompositeAttributeDef,
UserDefinedTypeRepresentation, ViewColumnDef,
};
pub use self::dml::{CreateTable, Delete, Insert};
pub use self::dml::{CreateIndex, CreateTable, Delete, Insert};
pub use self::operator::{BinaryOperator, UnaryOperator};
pub use self::query::{
AfterMatchSkip, ConnectBy, Cte, CteAsMaterialized, Distinct, EmptyMatchesMode,
Expand Down Expand Up @@ -1983,20 +1983,7 @@ pub enum Statement {
/// ```sql
/// `CREATE INDEX`
/// ```
CreateIndex {
/// index name
name: Option<ObjectName>,
#[cfg_attr(feature = "visitor", visit(with = "visit_relation"))]
table_name: ObjectName,
using: Option<Ident>,
columns: Vec<OrderByExpr>,
unique: bool,
concurrently: bool,
if_not_exists: bool,
include: Vec<Ident>,
nulls_distinct: Option<bool>,
predicate: Option<Expr>,
},
CreateIndex(CreateIndex),
/// ```sql
/// CREATE ROLE
/// ```
Expand Down Expand Up @@ -3466,7 +3453,7 @@ impl fmt::Display for Statement {
}
Ok(())
}
Statement::CreateIndex {
Statement::CreateIndex(CreateIndex {
name,
table_name,
using,
Expand All @@ -3477,7 +3464,7 @@ impl fmt::Display for Statement {
include,
nulls_distinct,
predicate,
} => {
}) => {
write!(
f,
"CREATE {unique}INDEX {concurrently}{if_not_exists}",
Expand Down
4 changes: 2 additions & 2 deletions src/parser/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4740,7 +4740,7 @@ impl<'a> Parser<'a> {
None
};

Ok(Statement::CreateIndex {
Ok(Statement::CreateIndex(CreateIndex {
name: index_name,
table_name,
using,
Expand All @@ -4751,7 +4751,7 @@ impl<'a> Parser<'a> {
include,
nulls_distinct,
predicate,
})
}))
}

pub fn parse_create_extension(&mut self) -> Result<Statement, ParserError> {
Expand Down
8 changes: 4 additions & 4 deletions tests/sqlparser_common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7195,14 +7195,14 @@ fn parse_create_index() {
},
];
match verified_stmt(sql) {
Statement::CreateIndex {
Statement::CreateIndex(CreateIndex {
name: Some(name),
table_name,
columns,
unique,
if_not_exists,
..
} => {
}) => {
assert_eq!("idx_name", name.to_string());
assert_eq!("test", table_name.to_string());
assert_eq!(indexed_columns, columns);
Expand All @@ -7229,7 +7229,7 @@ fn test_create_index_with_using_function() {
},
];
match verified_stmt(sql) {
Statement::CreateIndex {
Statement::CreateIndex(CreateIndex {
name: Some(name),
table_name,
using,
Expand All @@ -7240,7 +7240,7 @@ fn test_create_index_with_using_function() {
include,
nulls_distinct: None,
predicate: None,
} => {
}) => {
assert_eq!("idx_name", name.to_string());
assert_eq!("test", table_name.to_string());
assert_eq!("btree", using.unwrap().to_string());
Expand Down
28 changes: 14 additions & 14 deletions tests/sqlparser_postgres.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1952,7 +1952,7 @@ fn parse_array_index_expr() {
fn parse_create_index() {
let sql = "CREATE INDEX IF NOT EXISTS my_index ON my_table(col1,col2)";
match pg().verified_stmt(sql) {
Statement::CreateIndex {
Statement::CreateIndex(CreateIndex {
name: Some(ObjectName(name)),
table_name: ObjectName(table_name),
using,
Expand All @@ -1963,7 +1963,7 @@ fn parse_create_index() {
nulls_distinct: None,
include,
predicate: None,
} => {
}) => {
assert_eq_vec(&["my_index"], &name);
assert_eq_vec(&["my_table"], &table_name);
assert_eq!(None, using);
Expand All @@ -1981,7 +1981,7 @@ fn parse_create_index() {
fn parse_create_anonymous_index() {
let sql = "CREATE INDEX ON my_table(col1,col2)";
match pg().verified_stmt(sql) {
Statement::CreateIndex {
Statement::CreateIndex(CreateIndex {
name,
table_name: ObjectName(table_name),
using,
Expand All @@ -1992,7 +1992,7 @@ fn parse_create_anonymous_index() {
include,
nulls_distinct: None,
predicate: None,
} => {
}) => {
assert_eq!(None, name);
assert_eq_vec(&["my_table"], &table_name);
assert_eq!(None, using);
Expand All @@ -2010,7 +2010,7 @@ fn parse_create_anonymous_index() {
fn parse_create_index_concurrently() {
let sql = "CREATE INDEX CONCURRENTLY IF NOT EXISTS my_index ON my_table(col1,col2)";
match pg().verified_stmt(sql) {
Statement::CreateIndex {
Statement::CreateIndex(CreateIndex {
name: Some(ObjectName(name)),
table_name: ObjectName(table_name),
using,
Expand All @@ -2021,7 +2021,7 @@ fn parse_create_index_concurrently() {
include,
nulls_distinct: None,
predicate: None,
} => {
}) => {
assert_eq_vec(&["my_index"], &name);
assert_eq_vec(&["my_table"], &table_name);
assert_eq!(None, using);
Expand All @@ -2039,7 +2039,7 @@ fn parse_create_index_concurrently() {
fn parse_create_index_with_predicate() {
let sql = "CREATE INDEX IF NOT EXISTS my_index ON my_table(col1,col2) WHERE col3 IS NULL";
match pg().verified_stmt(sql) {
Statement::CreateIndex {
Statement::CreateIndex(CreateIndex {
name: Some(ObjectName(name)),
table_name: ObjectName(table_name),
using,
Expand All @@ -2050,7 +2050,7 @@ fn parse_create_index_with_predicate() {
include,
nulls_distinct: None,
predicate: Some(_),
} => {
}) => {
assert_eq_vec(&["my_index"], &name);
assert_eq_vec(&["my_table"], &table_name);
assert_eq!(None, using);
Expand All @@ -2068,7 +2068,7 @@ fn parse_create_index_with_predicate() {
fn parse_create_index_with_include() {
let sql = "CREATE INDEX IF NOT EXISTS my_index ON my_table(col1,col2) INCLUDE (col3)";
match pg().verified_stmt(sql) {
Statement::CreateIndex {
Statement::CreateIndex(CreateIndex {
name: Some(ObjectName(name)),
table_name: ObjectName(table_name),
using,
Expand All @@ -2079,7 +2079,7 @@ fn parse_create_index_with_include() {
include,
nulls_distinct: None,
predicate: None,
} => {
}) => {
assert_eq_vec(&["my_index"], &name);
assert_eq_vec(&["my_table"], &table_name);
assert_eq!(None, using);
Expand All @@ -2097,7 +2097,7 @@ fn parse_create_index_with_include() {
fn parse_create_index_with_nulls_distinct() {
let sql = "CREATE INDEX IF NOT EXISTS my_index ON my_table(col1,col2) NULLS NOT DISTINCT";
match pg().verified_stmt(sql) {
Statement::CreateIndex {
Statement::CreateIndex(CreateIndex {
name: Some(ObjectName(name)),
table_name: ObjectName(table_name),
using,
Expand All @@ -2108,7 +2108,7 @@ fn parse_create_index_with_nulls_distinct() {
include,
nulls_distinct: Some(nulls_distinct),
predicate: None,
} => {
}) => {
assert_eq_vec(&["my_index"], &name);
assert_eq_vec(&["my_table"], &table_name);
assert_eq!(None, using);
Expand All @@ -2124,7 +2124,7 @@ fn parse_create_index_with_nulls_distinct() {

let sql = "CREATE INDEX IF NOT EXISTS my_index ON my_table(col1,col2) NULLS DISTINCT";
match pg().verified_stmt(sql) {
Statement::CreateIndex {
Statement::CreateIndex(CreateIndex {
name: Some(ObjectName(name)),
table_name: ObjectName(table_name),
using,
Expand All @@ -2135,7 +2135,7 @@ fn parse_create_index_with_nulls_distinct() {
include,
nulls_distinct: Some(nulls_distinct),
predicate: None,
} => {
}) => {
assert_eq_vec(&["my_index"], &name);
assert_eq_vec(&["my_table"], &table_name);
assert_eq!(None, using);
Expand Down

0 comments on commit c55fa4b

Please sign in to comment.