value exprs
This commit is contained in:
parent
824a5cdb43
commit
d298cb129a
25 changed files with 451 additions and 210 deletions
63
squirrel_core/src/table/datatypes.rs
Normal file
63
squirrel_core/src/table/datatypes.rs
Normal file
|
@ -0,0 +1,63 @@
|
|||
use anyhow::anyhow;
|
||||
|
||||
#[derive(Debug, Eq, PartialEq)]
|
||||
pub enum Datatype {
|
||||
Integer,
|
||||
CharacterVarying,
|
||||
}
|
||||
|
||||
impl Datatype {
|
||||
pub fn as_str(&self) -> &'static str {
|
||||
match self {
|
||||
Datatype::CharacterVarying => "varchar",
|
||||
Datatype::Integer => "integer",
|
||||
}
|
||||
}
|
||||
|
||||
pub fn has_len(&self) -> bool {
|
||||
match self {
|
||||
Datatype::CharacterVarying => true,
|
||||
Datatype::Integer => false,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn to_bytes(&self, data_val: String) -> ::anyhow::Result<Vec<u8>> {
|
||||
match self {
|
||||
Datatype::CharacterVarying => {
|
||||
let mut str_bytes = data_val.as_bytes().to_vec();
|
||||
Ok(str_bytes)
|
||||
}
|
||||
Datatype::Integer => {
|
||||
let val = data_val.parse::<u8>()?;
|
||||
Ok(vec![val])
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub fn from_bytes(&self, data_val: &[u8]) -> ::anyhow::Result<String> {
|
||||
match self {
|
||||
Datatype::CharacterVarying => {
|
||||
let str_val = String::from_utf8(data_val.to_vec())?;
|
||||
Ok(str_val)
|
||||
}
|
||||
Datatype::Integer => {
|
||||
if let Some(val) = data_val.first() {
|
||||
Ok(format!("{}", val))
|
||||
} else {
|
||||
Err(anyhow!("Unable to parse Integer"))
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub fn parse_from_str(string: &str) -> ::anyhow::Result<Datatype> {
|
||||
match string {
|
||||
"varchar" => Ok(Datatype::CharacterVarying),
|
||||
"character varying" => Ok(Datatype::CharacterVarying),
|
||||
"integer" => Ok(Datatype::Integer),
|
||||
"int" => Ok(Datatype::Integer),
|
||||
"int8" => Ok(Datatype::Integer),
|
||||
_ => Err(anyhow!("Undefined data type")),
|
||||
}
|
||||
}
|
||||
}
|
2
squirrel_core/src/table/mod.rs
Normal file
2
squirrel_core/src/table/mod.rs
Normal file
|
@ -0,0 +1,2 @@
|
|||
pub mod datatypes;
|
||||
pub mod table_definition;
|
31
squirrel_core/src/table/table_definition.rs
Normal file
31
squirrel_core/src/table/table_definition.rs
Normal file
|
@ -0,0 +1,31 @@
|
|||
use crate::table::datatypes::Datatype;
|
||||
|
||||
#[derive(Debug, Eq, PartialEq)]
|
||||
pub struct ColumnDefinition {
|
||||
pub name: String,
|
||||
pub data_type: Datatype,
|
||||
pub length: usize, // used for char(n), varchar(n)
|
||||
}
|
||||
|
||||
#[derive(Debug, Eq, PartialEq)]
|
||||
pub struct TableDefinition {
|
||||
pub name: String,
|
||||
pub column_defs: Vec<ColumnDefinition>,
|
||||
}
|
||||
|
||||
impl TableDefinition {
|
||||
pub fn get_byte_size(&self) -> usize {
|
||||
let mut sum: usize = 0;
|
||||
for col_def in self.column_defs.iter() {
|
||||
// TODO HACK FIXME
|
||||
// We should keep track of length
|
||||
// even for built-in datatypes.
|
||||
sum += if col_def.length > 0 {
|
||||
col_def.length
|
||||
} else {
|
||||
1
|
||||
};
|
||||
}
|
||||
sum
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue