mirror of
https://github.com/Chaoscaot/schemsearch.git
synced 2025-11-05 05:54:02 +01:00
Finishing
This commit is contained in:
@ -2,12 +2,12 @@
|
||||
name = "schemsearch-sql"
|
||||
version = "0.1.0"
|
||||
edition = "2021"
|
||||
license = "AGPL-3.0-or-later"
|
||||
|
||||
# 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"] }
|
||||
sqlx = { version = "0.6", features = [ "runtime-async-std-native-tls" , "mysql" ] }
|
||||
|
||||
schemsearch-lib = { path = "../schemsearch-lib" }
|
||||
schemsearch-files = { path = "../schemsearch-files" }
|
||||
@ -1,38 +1,43 @@
|
||||
use std::sync::Mutex;
|
||||
use sqlx::{Executor, MySql, Pool, Row};
|
||||
use sqlx::mysql::MySqlPoolOptions;
|
||||
|
||||
mod properties;
|
||||
|
||||
static mut CONN: Mutex<Option<Pool<MySql>>> = Mutex::new(None);
|
||||
|
||||
pub struct SchematicNode {
|
||||
pub id: i32,
|
||||
pub name: String,
|
||||
pub node_owner: i32,
|
||||
|
||||
pub name: String
|
||||
}
|
||||
|
||||
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 unsafe fn get_connection() {
|
||||
let mut conn = CONN.lock().unwrap();
|
||||
if conn.is_none() {
|
||||
let properties = properties::load_mysql_properties();
|
||||
let _ = conn.insert(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> {
|
||||
pub async fn load_all_schematics() -> Vec<SchematicNode> {
|
||||
unsafe { get_connection().await; }
|
||||
let mut schematics = Vec::new();
|
||||
let rows = conn.fetch_all("SELECT id, name, node_owner FROM schematics").await.expect("Failed to fetch schematics");
|
||||
let rows = unsafe { &CONN }.lock().unwrap().as_mut().unwrap().fetch_all("SELECT SN.NodeId, SN.NodeName FROM NodeData ND INNER JOIN SchematicNode SN ON SN.NodeId = ND.NodeId WHERE NodeFormat = true").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),
|
||||
name: row.get(1)
|
||||
});
|
||||
}
|
||||
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");
|
||||
pub async fn load_schemdata(id: i32) -> Vec<u8> {
|
||||
unsafe { get_connection().await; }
|
||||
let rows = unsafe { &CONN }.lock().unwrap().as_mut().unwrap().fetch_one(format!("SELECT SchemData FROM NodeData WHERE NodeId = {}", id).as_str()).await.expect("Failed to fetch schematics");
|
||||
rows.get(0)
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user