Skip to content

Commit

Permalink
MultiDB DROP
Browse files Browse the repository at this point in the history
Resolves: #113
Resolves: #46
Resolves: #105
  • Loading branch information
KyGost committed Apr 8, 2022
1 parent b201990 commit 51ddaf8
Showing 1 changed file with 24 additions and 31 deletions.
55 changes: 24 additions & 31 deletions src/executor/alter_table/drop.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use {
super::AlterError,
crate::{data::get_name, Glue, Result, StorageInner, ValueDefault},
crate::{ComplexTableName, Glue, Result, StorageInner, ValueDefault},
futures::stream::{self, TryStreamExt},
sqlparser::ast::{ObjectName, ObjectType},
};
Expand All @@ -12,43 +12,36 @@ impl Glue {
names: &[ObjectName],
if_exists: bool,
) -> Result<()> {
let database = &mut **self.get_mut_database(&None)?;
if object_type != &ObjectType::Table {
return Err(AlterError::DropTypeNotSupported(object_type.to_string()).into());
}

stream::iter(names.iter().map(Ok))
.try_fold(database, |database, table_name| async move {
let table_name = get_name(table_name)?;
let schema = database.fetch_schema(table_name).await?;
for name in names.iter() {
let ComplexTableName {
name: table_name,
database,
..
} = name.try_into()?;

if schema.is_none() {
if !if_exists {
return Err(AlterError::TableNotFound(table_name.to_owned()).into());
} else {
return Ok(database);
let database = &mut **self.get_mut_database(&database)?;
let schema = database.fetch_schema(&table_name).await?;

if let Some(schema) = schema {
for column in schema.column_defs {
if matches!(column.default, Some(ValueDefault::AutoIncrement(_))) {
database
.set_increment_value(&table_name, &column.name, 1_i64)
.await?;
}
}
#[cfg(feature = "auto-increment")]
let result: Result<&mut StorageInner> =
stream::iter(schema.unwrap().column_defs.into_iter().map(Ok))
.try_fold(database, |database, column| async move {
if matches!(column.default, Some(ValueDefault::AutoIncrement(_))) {
database
.set_increment_value(table_name, &column.name, 1_i64)
.await?;
}
Ok(database)
})
.await;

#[cfg(feature = "auto-increment")]
let database = result?;

database.delete_schema(table_name).await?;
Ok(database)
})
.await
.map(|_| ())
database.delete_schema(&table_name).await?;
} else {
if !if_exists {
return Err(AlterError::TableNotFound(table_name.to_owned()).into());
}
}
}
Ok(())
}
}

0 comments on commit 51ddaf8

Please sign in to comment.