From ff03732df5a2dd717e415140471a826410e6e3fa Mon Sep 17 00:00:00 2001 From: Chaoscaot Date: Sat, 4 Mar 2023 13:31:53 +0100 Subject: [PATCH] Add SQL Part --- Cargo.toml | 3 ++- schemsearch-sql/Cargo.toml | 13 +++++++++++ schemsearch-sql/src/lib.rs | 38 +++++++++++++++++++++++++++++++ schemsearch-sql/src/properties.rs | 27 ++++++++++++++++++++++ 4 files changed, 80 insertions(+), 1 deletion(-) create mode 100644 schemsearch-sql/Cargo.toml create mode 100644 schemsearch-sql/src/lib.rs create mode 100644 schemsearch-sql/src/properties.rs diff --git a/Cargo.toml b/Cargo.toml index 4c984d5..8d20580 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -2,5 +2,6 @@ members = [ "schemsearch-cli", "schemsearch-lib", - "schemsearch-files" + "schemsearch-files", + "schemsearch-sql" ] diff --git a/schemsearch-sql/Cargo.toml b/schemsearch-sql/Cargo.toml new file mode 100644 index 0000000..91129af --- /dev/null +++ b/schemsearch-sql/Cargo.toml @@ -0,0 +1,13 @@ +[package] +name = "schemsearch-sql" +version = "0.1.0" +edition = "2021" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] +sqlx = { version = "0.6", features = [ "runtime-tokio-native-tls" , "mysql" ] } +tokio = { version = "1", features = ["full"] } + +schemsearch-lib = { path = "../schemsearch-lib" } +schemsearch-files = { path = "../schemsearch-files" } \ No newline at end of file diff --git a/schemsearch-sql/src/lib.rs b/schemsearch-sql/src/lib.rs new file mode 100644 index 0000000..ea3bd00 --- /dev/null +++ b/schemsearch-sql/src/lib.rs @@ -0,0 +1,38 @@ +use sqlx::{Executor, MySql, Pool, Row}; +use sqlx::mysql::MySqlPoolOptions; + +mod properties; + +pub struct SchematicNode { + pub id: i32, + pub name: String, + pub node_owner: i32, + +} + +pub async fn get_connection() -> Pool { + let properties = properties::load_mysql_properties(); + MySqlPoolOptions::new() + .max_connections(5) + .connect(&format!("mysql://{}:{}@{}/{}", properties.user, properties.password, properties.host, properties.database)) + .await.expect("Failed to connect to database") +} + +pub async fn load_schematics(conn: &mut Pool) -> Vec { + let mut schematics = Vec::new(); + let rows = conn.fetch_all("SELECT id, name, node_owner FROM schematics").await.expect("Failed to fetch schematics"); + for row in rows { + schematics.push(SchematicNode { + id: row.get(0), + name: row.get(1), + node_owner: row.get(2), + }); + } + schematics +} + +pub async fn load_data(conn: &mut Pool, schematic: &SchematicNode) -> Vec { + let rows = conn.fetch_one(sqlx::query("SELECT SchemData FROM NodeData WHERE NodeId = {}").bind(schematic.id)).await.expect("Failed to fetch schematic data"); + rows.get(0) +} + diff --git a/schemsearch-sql/src/properties.rs b/schemsearch-sql/src/properties.rs new file mode 100644 index 0000000..a152a08 --- /dev/null +++ b/schemsearch-sql/src/properties.rs @@ -0,0 +1,27 @@ +use std::collections::HashMap; +use std::fs::read_to_string; +use std::path::Path; + +pub(crate) struct SqlProperties { + pub(crate) host: String, + pub(crate) user: String, + pub(crate) password: String, + pub(crate) database: String, +} + +pub(crate) fn load_mysql_properties() -> SqlProperties { + let content = read_to_string(Path::new(&std::env::var("HOME").unwrap()).join("mysql.properties")).expect("Failed to read mysql.properties"); + let mut properties: HashMap = HashMap::new(); + + for line in content.lines() { + let split: Vec<&str> = line.split('=').collect(); + properties.insert(split[0].to_string(), split[1].to_string()); + } + + SqlProperties { + host: properties.get("host").unwrap().to_string(), + user: properties.get("user").unwrap().to_string(), + password: properties.get("password").unwrap().to_string(), + database: properties.get("database").unwrap().to_string(), + } +} \ No newline at end of file