Added initial elements for SQL.
This commit is contained in:
@ -1,120 +1,183 @@
|
||||
pub mod error;
|
||||
pub mod fieldtype;
|
||||
|
||||
use async_std::sync::{Arc, RwLock};
|
||||
use error::MTTError;
|
||||
use error::DBError;
|
||||
use pest::Parser;
|
||||
use std::collections::HashMap;
|
||||
|
||||
enum Ast {
|
||||
Script,
|
||||
Command,
|
||||
Action,
|
||||
Object,
|
||||
Name,
|
||||
}
|
||||
|
||||
#[derive(Parser)]
|
||||
#[grammar = "morethantext/mttsql.pest"]
|
||||
struct MTTSQL;
|
||||
|
||||
#[derive(Clone)]
|
||||
pub struct MoreThanText {
|
||||
tables: Arc<RwLock<HashMap<String, Table>>>,
|
||||
databases: Arc<RwLock<HashMap<String, Database>>>,
|
||||
}
|
||||
|
||||
impl MoreThanText {
|
||||
pub async fn new() -> Self {
|
||||
Self {
|
||||
tables: Arc::new(RwLock::new(HashMap::new())),
|
||||
databases: Arc::new(RwLock::new(HashMap::new())),
|
||||
}
|
||||
}
|
||||
|
||||
pub async fn new_table<S>(&self, tname: S) -> Result<Table, MTTError>
|
||||
where
|
||||
S: Into<String>,
|
||||
{
|
||||
let mut tables = self.tables.write().await;
|
||||
let name = tname.into();
|
||||
match tables.get(&name) {
|
||||
Some(_) => Err(MTTError::new(format!("table {} already exists", name))),
|
||||
None => {
|
||||
let table = Table::new().await;
|
||||
tables.insert(name, table.clone());
|
||||
Ok(table)
|
||||
pub async fn execute(&self, script: &str) -> Result<(), DBError> {
|
||||
match MTTSQL::parse(Rule::file, script) {
|
||||
Ok(mut commands) => {
|
||||
let pair = commands.next().unwrap();
|
||||
match pair.as_rule() {
|
||||
Rule::script => Ast::Script,
|
||||
Rule::command => Ast::Command,
|
||||
Rule::action => Ast::Action,
|
||||
Rule::object => Ast::Object,
|
||||
Rule::name => Ast::Name,
|
||||
Rule::char | Rule::whitespace | Rule::file | Rule::EOI => unreachable!(),
|
||||
};
|
||||
Ok(())
|
||||
}
|
||||
Err(err) => {
|
||||
let mut error = DBError::new("script parsing error");
|
||||
error.add_source(err);
|
||||
Err(error)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub async fn get_table(&self, name: &str) -> Option<Table> {
|
||||
let tables = self.tables.read().await;
|
||||
match tables.get(name) {
|
||||
Some(tbl) => Some(tbl.clone()),
|
||||
None => None,
|
||||
async fn create_database(&self, name: &str) -> Result<(), DBError> {
|
||||
let mut databases = self.databases.write().await;
|
||||
match databases.get(name) {
|
||||
Some(_) => Err(DBError::new("duplicate database name")),
|
||||
None => {
|
||||
let db = Database::new().await;
|
||||
databases.insert(name.to_string(), db);
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
async fn use_database(&self, name: &str) -> Result<(), DBError> {
|
||||
let databases = self.databases.read().await;
|
||||
match databases.get(name) {
|
||||
Some(_) => Ok(()),
|
||||
None => Err(DBError::new("database name not found")),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone)]
|
||||
pub struct Table;
|
||||
struct Database;
|
||||
|
||||
impl Table {
|
||||
pub async fn new() -> Self {
|
||||
impl Database {
|
||||
async fn new() -> Self {
|
||||
Self {}
|
||||
}
|
||||
|
||||
async fn new_column(&self, _name: &str, _type: &str) {}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod database {
|
||||
mod engine_functions {
|
||||
use super::*;
|
||||
|
||||
#[async_std::test]
|
||||
async fn create_table_with_str() {
|
||||
let db = MoreThanText::new().await;
|
||||
db.new_table("william").await.unwrap();
|
||||
async fn create_database() {
|
||||
let mtt = MoreThanText::new().await;
|
||||
mtt.create_database("smith").await.unwrap();
|
||||
}
|
||||
|
||||
#[async_std::test]
|
||||
async fn create_table_with_string() {
|
||||
let db = MoreThanText::new().await;
|
||||
db.new_table("marvin".to_string()).await.unwrap();
|
||||
}
|
||||
|
||||
#[async_std::test]
|
||||
async fn table_names_are_unique() -> Result<(), String> {
|
||||
let db = MoreThanText::new().await;
|
||||
let name = "alexandar";
|
||||
let msg = format!("table {} already exists", name);
|
||||
db.new_table(name).await.unwrap();
|
||||
match db.new_table(name).await {
|
||||
Ok(_) => Err("Duplicate table names are not allowed.".to_string()),
|
||||
async fn database_names_must_be_unique() -> Result<(), DBError> {
|
||||
let mtt = MoreThanText::new().await;
|
||||
let msg = "duplicate database name";
|
||||
mtt.create_database("john").await.unwrap();
|
||||
match mtt.create_database("john").await {
|
||||
Ok(_) => Err(DBError::new("Duplicate names should cause error")),
|
||||
Err(err) => {
|
||||
if err.to_string() == msg {
|
||||
Ok(())
|
||||
} else {
|
||||
Err(format!(
|
||||
"Error message is incorrect: Got: '{}' Want: '{}'",
|
||||
Err(DBError::new(format!(
|
||||
"incorrect err message: got: '{}' want: '{}'",
|
||||
err.to_string(),
|
||||
msg
|
||||
))
|
||||
)))
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[async_std::test]
|
||||
async fn get_non_existant_table() {
|
||||
let db = MoreThanText::new().await;
|
||||
let table = db.get_table("missing").await;
|
||||
assert!(table.is_none(), "There should be no table.");
|
||||
async fn use_database() -> Result<(), DBError> {
|
||||
let mtt = MoreThanText::new().await;
|
||||
let dbname = "Johnson";
|
||||
mtt.create_database(dbname).await.unwrap();
|
||||
mtt.use_database(dbname).await.unwrap();
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[async_std::test]
|
||||
async fn get_a_table() {
|
||||
let db = MoreThanText::new().await;
|
||||
let name = "here";
|
||||
db.new_table(name).await.unwrap();
|
||||
let table = db.get_table(name).await;
|
||||
assert!(table.is_some(), "Table should be found.");
|
||||
async fn use_missing_database() -> Result<(), DBError> {
|
||||
let error = "database name not found";
|
||||
let mtt = MoreThanText::new().await;
|
||||
match mtt.use_database("ssmith").await {
|
||||
Ok(_) => Err(DBError::new("Should raise database missing error")),
|
||||
Err(err) => {
|
||||
if err.to_string() == error {
|
||||
Ok(())
|
||||
} else {
|
||||
Err(DBError::new(format!(
|
||||
"Incorrect error message: Got '{}' Want '{}'",
|
||||
err.to_string(),
|
||||
error
|
||||
)))
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod table {
|
||||
mod database_functions {
|
||||
use super::*;
|
||||
|
||||
#[async_std::test]
|
||||
async fn add_column() {
|
||||
let tbl = Table::new().await;
|
||||
tbl.new_column("fred", "StaticString").await;
|
||||
async fn new_database() {
|
||||
Database::new().await;
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod mtt_commands {
|
||||
use super::*;
|
||||
|
||||
#[async_std::test]
|
||||
async fn create_database() {
|
||||
let mtt = MoreThanText::new().await;
|
||||
mtt.execute("create database fred;").await.unwrap();
|
||||
}
|
||||
|
||||
#[async_std::test]
|
||||
async fn unsuccessful_parse() -> Result<(), DBError> {
|
||||
let msg = "script parsing error";
|
||||
let mtt = MoreThanText::new().await;
|
||||
match mtt.execute("#$%^&").await {
|
||||
Ok(_) => Err(DBError::new("Should show a parse failure.")),
|
||||
Err(err) => {
|
||||
if err.to_string() == msg {
|
||||
Ok(())
|
||||
} else {
|
||||
Err(DBError::new(format!(
|
||||
"Error message is incorrect: Got: '{}' Want: '{}'",
|
||||
err.to_string(),
|
||||
msg
|
||||
)))
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user