Add Filters to SQL
Dieser Commit ist enthalten in:
Ursprung
5c12ce1202
Commit
a2a750f20e
@ -19,7 +19,7 @@ mod types;
|
|||||||
|
|
||||||
use std::fs::File;
|
use std::fs::File;
|
||||||
use std::io::{BufWriter, StdoutLock, Write};
|
use std::io::{BufWriter, StdoutLock, Write};
|
||||||
use clap::{command, Arg, ArgAction, Command};
|
use clap::{command, Arg, ArgAction, Command, ValueHint};
|
||||||
use schemsearch_files::Schematic;
|
use schemsearch_files::Schematic;
|
||||||
use std::path::Path;
|
use std::path::Path;
|
||||||
use clap::error::ErrorKind;
|
use clap::error::ErrorKind;
|
||||||
@ -27,6 +27,7 @@ use schemsearch_lib::{search, SearchBehavior};
|
|||||||
use crate::types::{PathSchematicSupplier, SchematicSupplierType};
|
use crate::types::{PathSchematicSupplier, SchematicSupplierType};
|
||||||
#[cfg(feature = "sql")]
|
#[cfg(feature = "sql")]
|
||||||
use futures::executor::block_on;
|
use futures::executor::block_on;
|
||||||
|
use schemsearch_sql::filter::SchematicFilter;
|
||||||
#[cfg(feature = "sql")]
|
#[cfg(feature = "sql")]
|
||||||
use schemsearch_sql::load_all_schematics;
|
use schemsearch_sql::load_all_schematics;
|
||||||
#[cfg(feature = "sql")]
|
#[cfg(feature = "sql")]
|
||||||
@ -39,11 +40,13 @@ fn main() {
|
|||||||
Arg::new("pattern")
|
Arg::new("pattern")
|
||||||
.help("The pattern to search for")
|
.help("The pattern to search for")
|
||||||
.required(true)
|
.required(true)
|
||||||
|
.value_hint(ValueHint::FilePath)
|
||||||
.action(ArgAction::Set),
|
.action(ArgAction::Set),
|
||||||
)
|
)
|
||||||
.arg(
|
.arg(
|
||||||
Arg::new("schematic")
|
Arg::new("schematic")
|
||||||
.help("The schematics to search in")
|
.help("The schematics to search in")
|
||||||
|
.value_hint(ValueHint::AnyPath)
|
||||||
.action(ArgAction::Append),
|
.action(ArgAction::Append),
|
||||||
)
|
)
|
||||||
.arg(
|
.arg(
|
||||||
@ -95,6 +98,7 @@ fn main() {
|
|||||||
.help("The output file")
|
.help("The output file")
|
||||||
.short('O')
|
.short('O')
|
||||||
.long("output-file")
|
.long("output-file")
|
||||||
|
.value_hint(ValueHint::FilePath)
|
||||||
.action(ArgAction::Append)
|
.action(ArgAction::Append)
|
||||||
)
|
)
|
||||||
.arg(
|
.arg(
|
||||||
@ -113,10 +117,27 @@ fn main() {
|
|||||||
let mut cmd = cmd
|
let mut cmd = cmd
|
||||||
.arg(
|
.arg(
|
||||||
Arg::new("sql")
|
Arg::new("sql")
|
||||||
.help("The sql file to write to")
|
.help("Use the SteamWar SQL Database")
|
||||||
.short('s')
|
.short('s')
|
||||||
.long("sql")
|
.long("sql")
|
||||||
.action(ArgAction::SetTrue),
|
.action(ArgAction::SetTrue),
|
||||||
|
)
|
||||||
|
.arg(
|
||||||
|
Arg::new("sql-filter-user")
|
||||||
|
.help("Filter the schematics by the owners userid")
|
||||||
|
.short('u')
|
||||||
|
.long("sql-filter-user")
|
||||||
|
.action(ArgAction::Append)
|
||||||
|
.value_parser(|s: &str| s.parse::<u32>().map_err(|e| e.to_string()))
|
||||||
|
.requires("sql"),
|
||||||
|
)
|
||||||
|
.arg(
|
||||||
|
Arg::new("sql-filter-name")
|
||||||
|
.help("Filter the schematics by the schematic name")
|
||||||
|
.short('n')
|
||||||
|
.long("sql-filter-name")
|
||||||
|
.action(ArgAction::Append)
|
||||||
|
.requires("sql"),
|
||||||
);
|
);
|
||||||
|
|
||||||
let matches = cmd.get_matches_mut();
|
let matches = cmd.get_matches_mut();
|
||||||
@ -148,12 +169,20 @@ fn main() {
|
|||||||
|
|
||||||
#[cfg(feature = "sql")]
|
#[cfg(feature = "sql")]
|
||||||
if matches.get_flag("sql") {
|
if matches.get_flag("sql") {
|
||||||
for schem in block_on(load_all_schematics()) {
|
let mut filter = SchematicFilter::default();
|
||||||
|
if let Some(x) = matches.get_many::<u32>("sql-filter-user") {
|
||||||
|
filter = filter.user_id(x.collect());
|
||||||
|
}
|
||||||
|
if let Some(x) = matches.get_many::<String>("sql-filter-name") {
|
||||||
|
filter = filter.name(x.collect());
|
||||||
|
}
|
||||||
|
for schem in block_on(load_all_schematics(filter)) {
|
||||||
schematics.push(SchematicSupplierType::SQL(SqlSchematicSupplier{
|
schematics.push(SchematicSupplierType::SQL(SqlSchematicSupplier{
|
||||||
node: schem
|
node: schem
|
||||||
}))
|
}))
|
||||||
};
|
};
|
||||||
} else if schematics.is_empty() {
|
}
|
||||||
|
if schematics.is_empty() {
|
||||||
cmd.error(ErrorKind::MissingRequiredArgument, "No schematics specified").exit();
|
cmd.error(ErrorKind::MissingRequiredArgument, "No schematics specified").exit();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
35
schemsearch-sql/src/filter.rs
Normale Datei
35
schemsearch-sql/src/filter.rs
Normale Datei
@ -0,0 +1,35 @@
|
|||||||
|
#[derive(Default, Debug, Clone)]
|
||||||
|
pub struct SchematicFilter {
|
||||||
|
pub user_id: Option<Vec<u32>>,
|
||||||
|
pub name: Option<Vec<String>>,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl SchematicFilter {
|
||||||
|
pub fn new() -> SchematicFilter {
|
||||||
|
SchematicFilter {
|
||||||
|
user_id: None,
|
||||||
|
name: None,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn user_id(mut self, user_id: Vec<&u32>) -> SchematicFilter {
|
||||||
|
self.user_id = Some(user_id.into_iter().map(|id| *id).collect());
|
||||||
|
self
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn name(mut self, name: Vec<&String>) -> SchematicFilter {
|
||||||
|
self.name = Some(name.into_iter().map(|name| name.to_string()).collect());
|
||||||
|
self
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn build(self) -> String {
|
||||||
|
let mut query = Vec::new();
|
||||||
|
if let Some(user_id) = self.user_id {
|
||||||
|
query.push(user_id.into_iter().map(|id| format!("ND.NodeOwner = {}", id)).collect::<Vec<String>>().join(" OR "));
|
||||||
|
}
|
||||||
|
if let Some(name) = self.name {
|
||||||
|
query.push(name.into_iter().map(|name| format!("SN.NodeName LIKE '%{}%'", name)).collect::<Vec<String>>().join(" OR "));
|
||||||
|
}
|
||||||
|
format!("AND ({})", query.join(") AND ("))
|
||||||
|
}
|
||||||
|
}
|
@ -18,8 +18,10 @@
|
|||||||
use std::sync::Mutex;
|
use std::sync::Mutex;
|
||||||
use sqlx::{Executor, MySql, MySqlPool, Pool, Row};
|
use sqlx::{Executor, MySql, MySqlPool, Pool, Row};
|
||||||
use sqlx::mysql::MySqlConnectOptions;
|
use sqlx::mysql::MySqlConnectOptions;
|
||||||
|
use crate::filter::SchematicFilter;
|
||||||
|
|
||||||
mod properties;
|
mod properties;
|
||||||
|
pub mod filter;
|
||||||
|
|
||||||
static mut CONN: Mutex<Option<Pool<MySql>>> = Mutex::new(None);
|
static mut CONN: Mutex<Option<Pool<MySql>>> = Mutex::new(None);
|
||||||
|
|
||||||
@ -44,10 +46,10 @@ pub async unsafe fn get_connection() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub async fn load_all_schematics() -> Vec<SchematicNode> {
|
pub async fn load_all_schematics(filter: SchematicFilter) -> Vec<SchematicNode> {
|
||||||
unsafe { get_connection().await; }
|
unsafe { get_connection().await; }
|
||||||
let mut schematics = Vec::new();
|
let mut schematics = Vec::new();
|
||||||
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");
|
let rows = unsafe { &CONN }.lock().unwrap().as_mut().unwrap().fetch_all(format!("SELECT SN.NodeId, SN.NodeName FROM NodeData ND INNER JOIN SchematicNode SN ON SN.NodeId = ND.NodeId WHERE NodeFormat = true {}", filter.build()).as_str()).await.expect("Failed to fetch schematics");
|
||||||
for row in rows {
|
for row in rows {
|
||||||
schematics.push(SchematicNode {
|
schematics.push(SchematicNode {
|
||||||
id: row.get(0),
|
id: row.get(0),
|
||||||
|
In neuem Issue referenzieren
Einen Benutzer sperren