From 9afae6ed37af57636bce1d78dec63924787e7989 Mon Sep 17 00:00:00 2001 From: Your Name Date: Sun, 15 Sep 2024 14:31:10 -0400 Subject: [PATCH] {SCAFFOLD} --- omnidb/main.rs | 156 ++++++++++++++++++++++++++++++++++++++++++ omnidb/structs/mod.rs | 0 omnidb/types.rs | 22 ++++++ 3 files changed, 178 insertions(+) create mode 100644 omnidb/main.rs create mode 100644 omnidb/structs/mod.rs create mode 100644 omnidb/types.rs diff --git a/omnidb/main.rs b/omnidb/main.rs new file mode 100644 index 0000000..4d9523f --- /dev/null +++ b/omnidb/main.rs @@ -0,0 +1,156 @@ +use std::collections::HashMap; +use serde::{Serialize, Deserialize}; +use serde_json::Value; + +// Define a type alias for NoSQL-like flexible JSON data +type Document = Value; + +// A Record struct for SQL-like tables with typed data +#[derive(Serialize, Deserialize, Clone, Debug)] +pub struct Record { + id: u32, + data: Document, // Can hold any type of data, stored as JSON +} + +// A table structure that holds records for SQL-like storage +pub struct Table { + pub name: String, + pub columns: Vec, + pub records: HashMap, +} + +impl Table { + // Create a new table with the given name and columns + pub fn new(name: &str, columns: Vec) -> Self { + Self { + name: name.to_string(), + columns, + records: HashMap::new(), + } + } + + // Insert a record into the table + pub fn insert(&mut self, id: u32, data: Document) { + let record = Record { id, data }; + self.records.insert(id, record); + } + + // Read a record by ID + pub fn read(&self, id: u32) -> Option<&Record> { + self.records.get(&id) + } + + // Update a record by ID + pub fn update(&mut self, id: u32, data: Document) -> Result<(), String> { + if let Some(record) = self.records.get_mut(&id) { + record.data = data; + Ok(()) + } else { + Err(format!("Record with id {} not found", id)) + } + } + + // Delete a record by ID + pub fn delete(&mut self, id: u32) -> bool { + self.records.remove(&id).is_some() + } +} + +// Database struct to hold tables (SQL-like) and documents (NoSQL-like) +pub struct Database { + pub tables: HashMap, + pub documents: HashMap, // NoSQL-like document store +} + +impl Database { + // Create a new empty database + pub fn new() -> Self { + Self { + tables: HashMap::new(), + documents: HashMap::new(), + } + } + + // Create a new table in the database + pub fn create_table(&mut self, name: &str, columns: Vec) { + let table = Table::new(name, columns); + self.tables.insert(name.to_string(), table); + } + + // Insert a record into a table + pub fn insert_into_table(&mut self, table_name: &str, id: u32, data: Document) -> Result<(), String> { + if let Some(table) = self.tables.get_mut(table_name) { + table.insert(id, data); + Ok(()) + } else { + Err(format!("Table {} not found", table_name)) + } + } + + // Create a new document in the NoSQL-like document store + pub fn create_document(&mut self, name: &str, doc: Document) { + self.documents.insert(name.to_string(), doc); + } + + // Read a document from the NoSQL-like document store + pub fn read_document(&self, name: &str) -> Option<&Document> { + self.documents.get(name) + } + + // Update a document in the NoSQL-like document store + pub fn update_document(&mut self, name: &str, doc: Document) -> Result<(), String> { + if self.documents.contains_key(name) { + self.documents.insert(name.to_string(), doc); + Ok(()) + } else { + Err(format!("Document {} not found", name)) + } + } + + // Delete a document from the NoSQL-like document store + pub fn delete_document(&mut self, name: &str) -> bool { + self.documents.remove(name).is_some() + } +} + +fn main() { + // Create a new database instance + let mut db = Database::new(); + + // Create a SQL-like table + db.create_table("users", vec!["id".to_string(), "name".to_string(), "email".to_string()]); + + // Insert a record into the users table (SQL-like) + let user_data = serde_json::json!({ + "name": "John Doe", + "email": "john.doe@example.com" + }); + db.insert_into_table("users", 1, user_data).expect("Failed to insert record"); + + // Read the record + if let Some(record) = db.tables["users"].read(1) { + println!("User: {:?}", record); + } + + // Update the record + let updated_user_data = serde_json::json!({ + "name": "John Smith", + "email": "john.smith@example.com" + }); + db.tables.get_mut("users").unwrap().update(1, updated_user_data).expect("Failed to update record"); + + // Delete the record + db.tables.get_mut("users").unwrap().delete(1); + + // Insert a NoSQL-like document + let doc_data = serde_json::json!({ + "title": "NoSQL Example", + "content": "This is a NoSQL document." + }); + db.create_document("doc1", doc_data); + + // Read the NoSQL document + if let Some(document) = db.read_document("doc1") { + println!("Document: {:?}", document); + } +} diff --git a/omnidb/structs/mod.rs b/omnidb/structs/mod.rs new file mode 100644 index 0000000..e69de29 diff --git a/omnidb/types.rs b/omnidb/types.rs new file mode 100644 index 0000000..a8220fc --- /dev/null +++ b/omnidb/types.rs @@ -0,0 +1,22 @@ +use chrono::NaiveDateTime; +use serde::{Deserialize, Serialize}; +use serde_json::Value; +use std::path::PathBuf; + +// Define an enum for all supported data types in OmniDb +#[derive(Serialize, Deserialize, Clone, Debug)] +pub enum DataType { + Integer(i64), // Supports i32, i64 types + UnsignedInteger(u64), // Supports u32, u64 types + Float(f64), // Supports f32, f64 types + Boolean(bool), // Boolean type + Text(String), // Textual data + Json(Value), // Flexible JSON-like data + Binary(Vec), // Binary or blob data + Vector(Vec), // Vector of any DataType + Timestamp(NaiveDateTime), // Date and time values + File(PathBuf), // File paths for file storage + Null, + Undefined, + None, +} \ No newline at end of file