mirror of
https://github.com/aljazceru/turso.git
synced 2026-02-09 10:14:21 +01:00
Extract create_table() function
This commit is contained in:
106
core/schema.rs
106
core/schema.rs
@@ -2,7 +2,7 @@ use anyhow::Result;
|
||||
use core::fmt;
|
||||
use fallible_iterator::FallibleIterator;
|
||||
use sqlite3_parser::{
|
||||
ast::{Cmd, CreateTableBody, Stmt},
|
||||
ast::{Cmd, CreateTableBody, QualifiedName, Stmt},
|
||||
lexer::sql::Parser,
|
||||
};
|
||||
use std::collections::HashMap;
|
||||
@@ -41,56 +41,7 @@ impl Table {
|
||||
Some(cmd) => match cmd {
|
||||
Cmd::Stmt(stmt) => match stmt {
|
||||
Stmt::CreateTable { tbl_name, body, .. } => {
|
||||
let mut cols = vec![];
|
||||
match body {
|
||||
CreateTableBody::ColumnsAndConstraints { columns, .. } => {
|
||||
for column in columns {
|
||||
let name = column.col_name.0.to_string();
|
||||
let ty = match column.col_type {
|
||||
Some(data_type) => {
|
||||
let type_name = data_type.name.as_str();
|
||||
if type_name.contains("INT") {
|
||||
Type::Integer
|
||||
} else if type_name.contains("CHAR")
|
||||
|| type_name.contains("CLOB")
|
||||
|| type_name.contains("TEXT")
|
||||
{
|
||||
Type::Text
|
||||
} else if type_name.contains("BLOB")
|
||||
|| type_name.is_empty()
|
||||
{
|
||||
Type::Blob
|
||||
} else if type_name.contains("REAL")
|
||||
|| type_name.contains("FLOA")
|
||||
|| type_name.contains("DOUB")
|
||||
{
|
||||
Type::Real
|
||||
} else {
|
||||
Type::Numeric
|
||||
}
|
||||
}
|
||||
None => Type::Null,
|
||||
};
|
||||
let primary_key = column.constraints.iter().any(|c| {
|
||||
matches!(
|
||||
c.constraint,
|
||||
sqlite3_parser::ast::ColumnConstraint::PrimaryKey { .. }
|
||||
)
|
||||
});
|
||||
cols.push(Column {
|
||||
name,
|
||||
ty,
|
||||
primary_key,
|
||||
});
|
||||
}
|
||||
}
|
||||
CreateTableBody::AsSelect(_) => todo!(),
|
||||
};
|
||||
Ok(Table {
|
||||
root_page,
|
||||
name: tbl_name.name.to_string(),
|
||||
columns: cols,
|
||||
})
|
||||
create_table(tbl_name, body, root_page)
|
||||
}
|
||||
_ => {
|
||||
anyhow::bail!("Expected CREATE TABLE statement");
|
||||
@@ -123,6 +74,59 @@ impl Table {
|
||||
}
|
||||
}
|
||||
|
||||
fn create_table(tbl_name: QualifiedName, body: CreateTableBody, root_page: usize) -> Result<Table> {
|
||||
let mut cols = vec![];
|
||||
match body {
|
||||
CreateTableBody::ColumnsAndConstraints { columns, .. } => {
|
||||
for column in columns {
|
||||
let name = column.col_name.0.to_string();
|
||||
let ty = match column.col_type {
|
||||
Some(data_type) => {
|
||||
let type_name = data_type.name.as_str();
|
||||
if type_name.contains("INT") {
|
||||
Type::Integer
|
||||
} else if type_name.contains("CHAR")
|
||||
|| type_name.contains("CLOB")
|
||||
|| type_name.contains("TEXT")
|
||||
{
|
||||
Type::Text
|
||||
} else if type_name.contains("BLOB")
|
||||
|| type_name.is_empty()
|
||||
{
|
||||
Type::Blob
|
||||
} else if type_name.contains("REAL")
|
||||
|| type_name.contains("FLOA")
|
||||
|| type_name.contains("DOUB")
|
||||
{
|
||||
Type::Real
|
||||
} else {
|
||||
Type::Numeric
|
||||
}
|
||||
}
|
||||
None => Type::Null,
|
||||
};
|
||||
let primary_key = column.constraints.iter().any(|c| {
|
||||
matches!(
|
||||
c.constraint,
|
||||
sqlite3_parser::ast::ColumnConstraint::PrimaryKey { .. }
|
||||
)
|
||||
});
|
||||
cols.push(Column {
|
||||
name,
|
||||
ty,
|
||||
primary_key,
|
||||
});
|
||||
}
|
||||
}
|
||||
CreateTableBody::AsSelect(_) => todo!(),
|
||||
};
|
||||
Ok(Table {
|
||||
root_page,
|
||||
name: tbl_name.name.to_string(),
|
||||
columns: cols,
|
||||
})
|
||||
}
|
||||
|
||||
pub struct Column {
|
||||
pub name: String,
|
||||
pub ty: Type,
|
||||
|
||||
Reference in New Issue
Block a user