Added initial elements for SQL.
This commit is contained in:
120
src/morethantext/old-mod2.rs
Normal file
120
src/morethantext/old-mod2.rs
Normal file
@ -0,0 +1,120 @@
|
||||
pub mod error;
|
||||
pub mod fieldtype;
|
||||
|
||||
use async_std::sync::{Arc, RwLock};
|
||||
use error::MTTError;
|
||||
use std::collections::HashMap;
|
||||
|
||||
#[derive(Clone)]
|
||||
pub struct MoreThanText {
|
||||
tables: Arc<RwLock<HashMap<String, Table>>>,
|
||||
}
|
||||
|
||||
impl MoreThanText {
|
||||
pub async fn new() -> Self {
|
||||
Self {
|
||||
tables: 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 get_table(&self, name: &str) -> Option<Table> {
|
||||
let tables = self.tables.read().await;
|
||||
match tables.get(name) {
|
||||
Some(tbl) => Some(tbl.clone()),
|
||||
None => None,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone)]
|
||||
pub struct Table;
|
||||
|
||||
impl Table {
|
||||
pub async fn new() -> Self {
|
||||
Self {}
|
||||
}
|
||||
|
||||
async fn new_column(&self, _name: &str, _type: &str) {}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod database {
|
||||
use super::*;
|
||||
|
||||
#[async_std::test]
|
||||
async fn create_table_with_str() {
|
||||
let db = MoreThanText::new().await;
|
||||
db.new_table("william").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()),
|
||||
Err(err) => {
|
||||
if err.to_string() == msg {
|
||||
Ok(())
|
||||
} else {
|
||||
Err(format!(
|
||||
"Error message is incorrect: 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_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.");
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod table {
|
||||
use super::*;
|
||||
|
||||
#[async_std::test]
|
||||
async fn add_column() {
|
||||
let tbl = Table::new().await;
|
||||
tbl.new_column("fred", "StaticString").await;
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user