1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
use {
	crate::{data::Schema, Column, ComplexTableName, CreateError, Error, Glue, Result},
	sqlparser::ast::{ColumnDef, ObjectName},
};

impl Glue {
	pub async fn create_table(
		&mut self,
		name: &ObjectName,
		column_defs: &[ColumnDef],
		if_not_exists: bool,
	) -> Result<()> {
		let ComplexTableName {
			name: table_name,
			database,
			..
		} = name.try_into()?;

		let schema = Schema {
			table_name,
			column_defs: column_defs.iter().cloned().map(Column::from).collect(),
			indexes: vec![],
		};
		self.add_table(database, schema, if_not_exists).await
	}
	pub async fn add_table(
		&mut self,
		database: Option<String>,
		schema: Schema,
		if_not_exists: bool,
	) -> Result<()> {
		let database = &mut **self.get_mut_database(&database)?;
		if database.fetch_schema(&schema.table_name).await?.is_some() {
			if !if_not_exists {
				Err(Error::Create(CreateError::AlreadyExists(
					schema.table_name.to_owned(),
				)))
			} else {
				Ok(())
			}
		} else {
			database.insert_schema(&schema).await
		}
	}
}