1
0
Mirror von https://github.com/Chaoscaot/schemsearch synchronisiert 2024-10-01 20:20:12 +02:00
Dieser Commit ist enthalten in:
Chaoscaot 2023-03-04 13:31:53 +01:00
Ursprung 26d83caa05
Commit ff03732df5
4 geänderte Dateien mit 80 neuen und 1 gelöschten Zeilen

Datei anzeigen

@ -2,5 +2,6 @@
members = [
"schemsearch-cli",
"schemsearch-lib",
"schemsearch-files"
"schemsearch-files",
"schemsearch-sql"
]

13
schemsearch-sql/Cargo.toml Normale Datei
Datei anzeigen

@ -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" }

38
schemsearch-sql/src/lib.rs Normale Datei
Datei anzeigen

@ -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<MySql> {
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<MySql>) -> Vec<SchematicNode> {
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<MySql>, schematic: &SchematicNode) -> Vec<u8> {
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)
}

Datei anzeigen

@ -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<String, String> = 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(),
}
}