From 81f003d29fb25932bfb0fa06ff7d5657937d113d Mon Sep 17 00:00:00 2001 From: Pekka Enberg Date: Wed, 27 Mar 2024 20:41:02 +0200 Subject: [PATCH] Extract create_table() function --- core/schema.rs | 106 +++++++++++++++++++++++++------------------------ 1 file changed, 55 insertions(+), 51 deletions(-) diff --git a/core/schema.rs b/core/schema.rs index 4ac009b0c..8b1c8ae1f 100644 --- a/core/schema.rs +++ b/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 { + 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,