Skip to content

Commit

Permalink
Set "PRIMARY" as a reserved keyword for index names (#960)
Browse files Browse the repository at this point in the history
Set "PRIMARY" as a reserved keyword for index names.
Block use of "PRIMARY" name in create or drop index queries.
  • Loading branch information
devgony authored Oct 20, 2022
1 parent 3557d48 commit 29e9bd2
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 2 deletions.
6 changes: 6 additions & 0 deletions core/src/translate/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -122,4 +122,10 @@ pub enum TranslateError {

#[error("unimplemented - compound object is supported: {0}")]
CompoundObjectNotSupported(String),

#[error("cannot create index with reserved name: {0}")]
ReservedIndexName(String),

#[error("cannot drop primary index")]
CannotDropPrimary,
}
12 changes: 11 additions & 1 deletion core/src/translate/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -114,8 +114,14 @@ pub fn translate(sql_statement: &SqlStatement) -> Result<Statement> {
return Err(TranslateError::CompositeIndexNotSupported.into());
}

let name = translate_object_name(name)?;

if name.to_uppercase() == "PRIMARY" {
return Err(TranslateError::ReservedIndexName(name).into());
};

Ok(Statement::CreateIndex {
name: translate_object_name(name)?,
name,
table_name: translate_object_name(table_name)?,
column: translate_order_by_expr(&columns[0])?,
})
Expand All @@ -138,6 +144,10 @@ pub fn translate(sql_statement: &SqlStatement) -> Result<Statement> {
let table_name = object_name[0].value.to_owned();
let name = object_name[1].value.to_owned();

if name.to_uppercase() == "PRIMARY" {
return Err(TranslateError::CannotDropPrimary.into());
};

Ok(Statement::DropIndex { name, table_name })
}
#[cfg(feature = "transaction")]
Expand Down
19 changes: 18 additions & 1 deletion test-suite/src/dictionary_index.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
use {crate::*, gluesql_core::prelude::Value::*};
use {
crate::*,
gluesql_core::{prelude::Value::*, translate::TranslateError},
};

test_case!(ditionary_index, async move {
run!("CREATE TABLE Foo (id INT, name TEXT);");
Expand Down Expand Up @@ -27,4 +30,18 @@ test_case!(ditionary_index, async move {
"Foo".to_owned() "Foo_id_2".to_owned() "BOTH".to_owned() "id + 2".to_owned() false
))
);

let test_cases = [
(
"DROP INDEX Bar.PRIMARY",
Err(TranslateError::CannotDropPrimary.into()),
),
(
"CREATE INDEX Primary ON Foo (id)",
Err(TranslateError::ReservedIndexName("Primary".to_owned()).into()),
),
];
for (sql, expected) in test_cases {
test!(sql, expected);
}
});

0 comments on commit 29e9bd2

Please sign in to comment.