Mirror von
https://github.com/Chaoscaot/schemsearch
synchronisiert 2024-11-19 10:20:08 +01:00
Add SQL Part
Dieser Commit ist enthalten in:
Ursprung
26d83caa05
Commit
ff03732df5
@ -2,5 +2,6 @@
|
||||
members = [
|
||||
"schemsearch-cli",
|
||||
"schemsearch-lib",
|
||||
"schemsearch-files"
|
||||
"schemsearch-files",
|
||||
"schemsearch-sql"
|
||||
]
|
||||
|
13
schemsearch-sql/Cargo.toml
Normale Datei
13
schemsearch-sql/Cargo.toml
Normale Datei
@ -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
38
schemsearch-sql/src/lib.rs
Normale Datei
@ -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)
|
||||
}
|
||||
|
27
schemsearch-sql/src/properties.rs
Normale Datei
27
schemsearch-sql/src/properties.rs
Normale Datei
@ -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(),
|
||||
}
|
||||
}
|
Laden…
In neuem Issue referenzieren
Einen Benutzer sperren