Skip to content

Commit

Permalink
Now i can use config.json file for creating databases and tables
Browse files Browse the repository at this point in the history
  • Loading branch information
0xBLCKLPTN committed Sep 28, 2024
1 parent 8ade270 commit eb0590e
Show file tree
Hide file tree
Showing 7 changed files with 153 additions and 17 deletions.
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,6 @@ bazel-Kingdom-System
bazel-out
bazel-testlogs

LOGDATABASE
LOGDATABASE
DATABASE
*.adb
Empty file.
2 changes: 2 additions & 0 deletions Plugins/AliceDatabase/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
test:
cargo run -- ./config.json
8 changes: 4 additions & 4 deletions Plugins/AliceDatabase/config.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,19 @@
{
"database_name": "users",
"database_description": "description of database",
"database_path": "/path_to_database",
"database_type": "cache",
"database_path": "./DATABASE",
"database_type": "default",
"tables": [
{
"name": "QWE",
"name": "ananas",
"fields": [{"name": "username", "ftype": "string"}]
}
]
},
{
"database_name": "logins",
"database_description": "description of database",
"database_path": "/path_to_database",
"database_path": "./DATABASE",
"database_type": "default",
"tables": [
{
Expand Down
118 changes: 118 additions & 0 deletions Plugins/AliceDatabase/src/adbcore.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
use crate::atypes::BoxedResult;
use crate::pb_to_string;
use crate::create_dir;
use crate::PathBuf;
use crate::list_dir;
use crate::create_file;
use crate::write_into_file;

#[derive(Debug, Clone)]
pub struct Field {
pub name: String,
pub ftype: String,
}

#[derive(Debug, Clone)]
pub struct Table {
pub name: String,
pub database: String,
}

#[derive(Debug, Clone)]
pub struct Database {
pub path: PathBuf,
pub name: String,
pub tables: Vec<Table>,
pub engine: String,

}

pub trait DatabaseController {
fn new(path_to_database: PathBuf, engine: &str) -> Self;
async fn simple_write(&self, data: String) -> BoxedResult<()> {
todo!()
}
async fn create_table(&mut self, table_name: &str) -> BoxedResult<()>;
async fn find_tables(&mut self) -> BoxedResult<()>;
async fn write(&self, table_name: &str, data: &str) -> BoxedResult<()>;
async fn get_table_by_name(&self, name: String) -> Option<&Table>;


}
#[derive(Debug)]
pub struct Databases {
pub databases: Vec<Database>,
}

impl Databases {
pub fn new() -> Databases {
let databases: Vec<Database> = Vec::new();
Databases { databases }
}
pub fn add(&mut self, mut db: Database) {
self.databases.push(db);
}

pub fn get_db_by_name(&self, name: String) -> Option<&Database> {
for k in &self.databases {
if k.name == name {
return Some(&k);
}
}
return None;
}
}

impl DatabaseController for Database {
fn new(path_to_database: PathBuf, engine: &str) -> Database {
let mut tables: Vec<Table> = Vec::new();
create_dir(path_to_database.clone());
let name = pb_to_string(&path_to_database.clone()).as_str().split("/").collect::<Vec<_>>().last().unwrap().to_string();
return Database { path: path_to_database, tables, name, engine: engine.to_string()}
}

async fn create_table(&mut self, table_name: &str) -> BoxedResult<()> {
create_file(pb_to_string(&self.path) + "/" + table_name +"." + &self.engine + ".adb");
Ok(
self.tables.push(
Table {
name: table_name.to_string(),
database: self.name.clone(),
}
)
)
}

async fn find_tables(&mut self) -> BoxedResult<()> {
let mut name = "";
for table in list_dir(&self.path).unwrap() {
self.tables.push(
Table {
name: pb_to_string(&table)
.as_str()
.split("/")
.collect::<Vec<_>>()
.last()
.unwrap()
.to_string(),
database: self.name.clone(),
}
)
}
Ok(())
}

async fn get_table_by_name(&self, name: String) -> Option<&Table> {
for table in &self.tables {
if table.name == name {
return Some(&table);
}
}
return None;
}

async fn write(&self, table_name: &str, data: &str) -> BoxedResult<()> {
write_into_file(pb_to_string(&self.path) + "/" + table_name +"." + &self.engine + ".adb", data.to_string());
Ok(())
}
}
2 changes: 1 addition & 1 deletion Plugins/AliceDatabase/src/atypes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ pub struct Field {
#[derive(Serialize, Deserialize, Debug)]
pub struct Table {
pub name: String,
pub fields: Vec<Field>,
pub fields: Option<Vec<Field>>,
}

#[derive(Serialize, Deserialize, Debug)]
Expand Down
36 changes: 25 additions & 11 deletions plugins/AliceDatabase/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,22 @@ pub mod remore_db;
pub mod log_db;
pub mod queue_db;
pub mod default_db;
pub mod adbcore;

use fs_manager::*;
use atypes::*;
//use atypes::*;
use configurator::*;
use std::env;

//use adbcore;
use std::path::PathBuf;

use crate::adbcore::DatabaseController;
use crate::adbcore::Databases;
/*
use log_db::*;
use remore_db::*;
use queue_db::*;
use default_db::*;
use adbcore;
struct AliceDatabase {
pub log_db_mod: Option<LogDatabase>,
Expand All @@ -33,17 +37,27 @@ impl AliceDatabase {
AliceDatabase { log_db_mod, remore_db_mod, queue_db_mod}
}
}

*/
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
//let mut adb = AliceDatabase::new(true);
let mut db = DefaultDatabase::new(PathBuf::from("./DATABASE"));
let mut fields_vec: Vec<DField> = Vec::new();
fields_vec.push(DField { name: "usernames".to_string(), ftype: "string".to_string()});
db.find_tables().await;
println!("{:#?}", db);
db.get_table_by_name("users").await;
let args: Vec<String> = env::args().collect();
let conf = read_config(&args[1]).unwrap();

let mut dbs = Databases::new();

for db in conf {
let mut k = adbcore::Database::new(PathBuf::from(db.database_path), db.database_type.as_str());
for table in db.tables {
k.create_table(table.name.as_str()).await;
}
dbs.add(k);
}

let k = dbs.get_db_by_name("DATABASE".to_string());
match k {
Some(db) => println!("{:#?}", db.get_table_by_name("ananas".to_string()).await),
_ => println!("I Cant find db"),
}
Ok(())

}

0 comments on commit eb0590e

Please sign in to comment.