Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: TEXT type #108

Merged
merged 13 commits into from
Feb 1, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 2 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ use apllodb_immutable_schema_engine_domain::{
version::active_version::ActiveVersion, vtable::VTable,
};
use apllodb_shared_components::{
ApllodbError, ApllodbErrorKind, ApllodbResult, ColumnDataType, ColumnName, ColumnReference,
SqlType,
ApllodbError, ApllodbErrorKind, ApllodbResult, AstTranslator, ColumnDataType, ColumnName,
ColumnReference,
};
use apllodb_sql_parser::{
apllodb_ast::{self, Command, CreateTableCommand, TableElement},
Expand Down Expand Up @@ -81,19 +81,7 @@ impl ActiveVersionDeserializer {
.column_constraints
.contains(&apllodb_ast::ColumnConstraint::NotNullVariant);

let sql_type = {
match cd.data_type {
apllodb_ast::DataType::IntegerTypeVariant(
apllodb_ast::IntegerType::SmallIntVariant,
) => SqlType::small_int(),
apllodb_ast::DataType::IntegerTypeVariant(
apllodb_ast::IntegerType::IntegerVariant,
) => SqlType::integer(),
apllodb_ast::DataType::IntegerTypeVariant(
apllodb_ast::IntegerType::BigIntVariant,
) => SqlType::big_int(),
}
};
let sql_type = AstTranslator::data_type(cd.data_type.clone());

Ok(ColumnDataType::new(colref, sql_type, !not_null))
})
Expand Down
36 changes: 36 additions & 0 deletions apllodb-server/tests/sql_type.rs
Original file line number Diff line number Diff line change
Expand Up @@ -101,3 +101,39 @@ async fn test_big_int() {
.run()
.await;
}

#[async_std::test]
async fn test_text() {
SqlTest::default()
.add_steps(Steps::BeginTransaction)
.add_step(Step::new(
"CREATE TABLE t (c TEXT, PRIMARY KEY (c))",
StepRes::Ok,
))
.add_step(Step::new(
format!(
r#"INSERT INTO t (c) VALUES ("{}")"#,
r#"abcあいうえお🍺@'\"#
),
StepRes::Ok,
))
.add_step(Step::new(
"SELECT c FROM t",
StepRes::OkQuery(Box::new(|mut records| {
let field = FieldIndex::factory_colref(ColumnReference::factory("t", "c"));

let r = records.next().unwrap();
assert_eq!(
r.get::<i64>(&field).unwrap_err().kind(),
&ApllodbErrorKind::DatatypeMismatch
);
assert_eq!(
r.get::<String>(&field).unwrap().unwrap(),
r#"abcあいうえお🍺@'\"#
);
Ok(())
})),
))
.run()
.await;
}
4 changes: 4 additions & 0 deletions apllodb-shared-components/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ version = "0.1.0"
test-support = ["rand"]

[dependencies]
apllodb-sql-parser = {path = "../apllodb-sql-parser"}

serde = {version = "1.0", features = ["derive"]}

derive-new = "0.5"
Expand All @@ -32,3 +34,5 @@ apllodb-test-support = {path = "../apllodb-test-support"}
ctor = "0.1"

version-sync = "0.9"

pretty_assertions = "0.6"
18 changes: 18 additions & 0 deletions apllodb-shared-components/src/ast_translator.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#![allow(missing_docs)]

//! Module to translate [ApllodbAst](apllodb_sql_parser::ApllodbAst) into [apllodb_shared_components](crate)' data structures.

pub mod column_constraint;
pub mod column_definition;
pub mod column_name;
pub mod column_reference;
pub mod data_type;
pub mod database_name;
pub mod expression;
pub mod table_constraint;
pub mod table_name;
pub mod unary_operator;

/// Holds static translation methods.
#[derive(Clone, Eq, PartialEq, Ord, PartialOrd, Hash, Debug, Default)]
pub struct AstTranslator;
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
use apllodb_shared_components::ColumnConstraintKind;
use crate::ColumnConstraintKind;
use apllodb_sql_parser::apllodb_ast;

use crate::ast_translator::AstTranslator;

impl AstTranslator {
pub(crate) fn column_constraint(
pub fn column_constraint(
ast_column_constraint: apllodb_ast::ColumnConstraint,
) -> Option<ColumnConstraintKind> {
match ast_column_constraint {
Expand All @@ -14,7 +14,7 @@ impl AstTranslator {
}
}

pub(crate) fn nullable(ast_column_constraints: &[apllodb_ast::ColumnConstraint]) -> bool {
pub fn nullable(ast_column_constraints: &[apllodb_ast::ColumnConstraint]) -> bool {
!ast_column_constraints
.iter()
.any(|cc| matches!(cc, apllodb_ast::ColumnConstraint::NotNullVariant))
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use apllodb_shared_components::{
use crate::{
ApllodbResult, ColumnConstraintKind, ColumnConstraints, ColumnDataType, ColumnDefinition,
ColumnReference, TableName,
};
Expand All @@ -7,7 +7,7 @@ use apllodb_sql_parser::apllodb_ast::{self};
use crate::ast_translator::AstTranslator;

impl AstTranslator {
pub(crate) fn column_definition(
pub fn column_definition(
ast_column_definition: apllodb_ast::ColumnDefinition,
table_name: TableName,
) -> ApllodbResult<ColumnDefinition> {
Expand Down
10 changes: 10 additions & 0 deletions apllodb-shared-components/src/ast_translator/column_name.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
use crate::{ApllodbResult, ColumnName};
use apllodb_sql_parser::apllodb_ast;

use crate::ast_translator::AstTranslator;

impl AstTranslator {
pub fn column_name(ast_column_name: apllodb_ast::ColumnName) -> ApllodbResult<ColumnName> {
ColumnName::new(ast_column_name.0 .0)
}
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
use apllodb_shared_components::{ApllodbResult, ColumnReference};
use crate::{ApllodbResult, ColumnReference};
use apllodb_sql_parser::apllodb_ast::{self};

use crate::ast_translator::AstTranslator;

impl AstTranslator {
pub(crate) fn column_reference(
pub fn column_reference(
ast_column_reference: apllodb_ast::ColumnReference,
) -> ApllodbResult<ColumnReference> {
let column_name = Self::column_name(ast_column_reference.column_name)?;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,19 @@
use apllodb_shared_components::SqlType;
use crate::SqlType;
use apllodb_sql_parser::apllodb_ast;

use crate::ast_translator::AstTranslator;

impl AstTranslator {
pub(crate) fn data_type(ast_data_type: apllodb_ast::DataType) -> SqlType {
pub fn data_type(ast_data_type: apllodb_ast::DataType) -> SqlType {
match ast_data_type {
apllodb_ast::DataType::IntegerTypeVariant(i) => match i {
apllodb_ast::IntegerType::SmallIntVariant => SqlType::small_int(),
apllodb_ast::IntegerType::IntegerVariant => SqlType::integer(),
apllodb_ast::IntegerType::BigIntVariant => SqlType::big_int(),
},
apllodb_ast::DataType::CharacterTypeVariant(c) => match c {
apllodb_ast::CharacterType::TextVariant => SqlType::text(),
},
}
}
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
use apllodb_shared_components::{ApllodbResult, DatabaseName};
use crate::{ApllodbResult, DatabaseName};
use apllodb_sql_parser::apllodb_ast;

use crate::ast_translator::AstTranslator;

impl AstTranslator {
pub(crate) fn database_name(
pub fn database_name(
ast_database_name: apllodb_ast::DatabaseName,
) -> ApllodbResult<DatabaseName> {
DatabaseName::new(ast_database_name.0 .0)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
pub(crate) mod constant;
pub mod constant;

use apllodb_shared_components::{ApllodbResult, Expression};
use crate::{ApllodbResult, Expression};
use apllodb_sql_parser::apllodb_ast;

use crate::ast_translator::AstTranslator;

impl AstTranslator {
pub(crate) fn expression(ast_expression: apllodb_ast::Expression) -> ApllodbResult<Expression> {
pub fn expression(ast_expression: apllodb_ast::Expression) -> ApllodbResult<Expression> {
let expression: Expression = match ast_expression {
apllodb_ast::Expression::ConstantVariant(c) => {
let sql_value = Self::constant(c)?;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
pub(crate) mod numeric_constant;
pub(crate) mod string_constant;

use apllodb_shared_components::{ApllodbResult, SqlValue};
use crate::{ApllodbResult, SqlValue};
use apllodb_sql_parser::apllodb_ast;

use crate::ast_translator::AstTranslator;
Expand All @@ -9,6 +10,7 @@ impl AstTranslator {
pub(crate) fn constant(ast_constant: apllodb_ast::Constant) -> ApllodbResult<SqlValue> {
let sql_value: SqlValue = match ast_constant {
apllodb_ast::Constant::NumericConstantVariant(nc) => Self::numeric_constant(nc)?,
apllodb_ast::Constant::StringConstantVariant(sc) => Self::string_constant(sc)?,
};
Ok(sql_value)
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
pub(crate) mod integer_constant;

use apllodb_shared_components::{ApllodbResult, SqlValue};
use crate::{ApllodbResult, SqlValue};
use apllodb_sql_parser::apllodb_ast;

use crate::ast_translator::AstTranslator;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
use apllodb_shared_components::{
ApllodbError, ApllodbErrorKind, ApllodbResult, NNSqlValue, SqlValue,
};
use crate::{ApllodbError, ApllodbErrorKind, ApllodbResult, NNSqlValue, SqlValue};
use apllodb_sql_parser::apllodb_ast;

use crate::ast_translator::AstTranslator;
Expand Down Expand Up @@ -42,9 +40,7 @@ impl AstTranslator {
mod test {
use pretty_assertions::assert_eq;

use apllodb_shared_components::{
ApllodbErrorKind, ApllodbResult, NNSqlValue, SqlType, SqlValue,
};
use crate::{ApllodbErrorKind, ApllodbResult, NNSqlValue, SqlType, SqlValue};
use apllodb_sql_parser::apllodb_ast;

use super::AstTranslator;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
use crate::{ApllodbResult, NNSqlValue, SqlValue};
use apllodb_sql_parser::apllodb_ast;

use crate::ast_translator::AstTranslator;

impl AstTranslator {
pub(crate) fn string_constant(
ast_string_constant: apllodb_ast::StringConstant,
) -> ApllodbResult<SqlValue> {
Ok(SqlValue::NotNull(NNSqlValue::Text(ast_string_constant.0)))
}
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
use apllodb_shared_components::{ApllodbResult, TableConstraintKind};
use crate::{ApllodbResult, TableConstraintKind};
use apllodb_sql_parser::apllodb_ast;

use crate::ast_translator::AstTranslator;

impl AstTranslator {
pub(crate) fn table_constraint(
pub fn table_constraint(
ast_table_constraint: apllodb_ast::TableConstraint,
) -> ApllodbResult<TableConstraintKind> {
match ast_table_constraint {
Expand Down
10 changes: 10 additions & 0 deletions apllodb-shared-components/src/ast_translator/table_name.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
use crate::{ApllodbResult, TableName};
use apllodb_sql_parser::apllodb_ast;

use crate::ast_translator::AstTranslator;

impl AstTranslator {
pub fn table_name(ast_table_name: apllodb_ast::TableName) -> ApllodbResult<TableName> {
TableName::new(ast_table_name.0 .0)
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use apllodb_shared_components::UnaryOperator;
use crate::UnaryOperator;
use apllodb_sql_parser::apllodb_ast;

use crate::ast_translator::AstTranslator;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,8 +76,11 @@ impl Hash for NNSqlValue {

impl Display for NNSqlValue {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
let raw_in_s: String = for_all_loose_types!(self, |i: i64| i.to_string(), |s: String| s);
write!(f, "{}", raw_in_s)
let s: String = for_all_loose_types!(self, |i: i64| i.to_string(), |s: String| format!(
r#""{}""#,
s
));
write!(f, "{}", s)
}
}

Expand Down
2 changes: 2 additions & 0 deletions apllodb-shared-components/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,13 @@
#[macro_use]
extern crate derive_new;

pub(crate) mod ast_translator;
pub(crate) mod data_structure;
pub(crate) mod error;
pub(crate) mod traits;

pub use crate::{
ast_translator::AstTranslator,
data_structure::{
alter_table_action::AlterTableAction,
column::{
Expand Down
18 changes: 18 additions & 0 deletions apllodb-sql-parser/src/apllodb_sql_parser/apllodb_ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ pub struct ApllodbAst(pub Command);
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub enum Constant {
NumericConstantVariant(NumericConstant),
StringConstantVariant(StringConstant),
}

#[derive(Clone, Eq, PartialEq, Hash, Debug)]
Expand All @@ -49,6 +50,10 @@ pub enum NumericConstant {
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub struct IntegerConstant(pub String);

#[derive(Clone, Eq, PartialEq, Hash, Debug)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub struct StringConstant(pub String);

/*
* ----------------------------------------------------------------------------
* Operators
Expand Down Expand Up @@ -114,6 +119,7 @@ pub struct ColumnReference {
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub enum DataType {
IntegerTypeVariant(IntegerType),
CharacterTypeVariant(CharacterType),
}

/*
Expand All @@ -130,6 +136,18 @@ pub enum IntegerType {
BigIntVariant,
}

/*
* ----------------------------------------------------------------------------
* Character Types
* ----------------------------------------------------------------------------
*/

#[derive(Clone, Eq, PartialEq, Hash, Debug)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub enum CharacterType {
TextVariant,
}

/*
* ================================================================================================
* Commands:
Expand Down