generated from alecodes/base-template
Compare commits
No commits in common. "704be7688793dd4812fc7042aa21f8d0d40e5ba1" and "ccac3478f7f297a9730f787a011721db1a26bfed" have entirely different histories.
704be76887
...
ccac3478f7
5 changed files with 2 additions and 2113 deletions
1
.gitignore
vendored
1
.gitignore
vendored
|
|
@ -7,4 +7,3 @@ secring.*
|
||||||
# Added by cargo
|
# Added by cargo
|
||||||
|
|
||||||
/target
|
/target
|
||||||
.env
|
|
||||||
|
|
|
||||||
|
|
@ -1,4 +0,0 @@
|
||||||
set dotenv-load := true
|
|
||||||
|
|
||||||
dev:
|
|
||||||
cargo run --bin cli -- query "SELECT * FROM sources;"
|
|
||||||
2051
Cargo.lock
generated
2051
Cargo.lock
generated
File diff suppressed because it is too large
Load diff
|
|
@ -4,11 +4,3 @@ version = "0.1.0"
|
||||||
edition = "2021"
|
edition = "2021"
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
clap = { version = "4.5.26", features = ["derive", "env"] }
|
|
||||||
sqlx = { version = "0.8", features = [
|
|
||||||
"runtime-tokio",
|
|
||||||
"tls-native-tls",
|
|
||||||
"postgres",
|
|
||||||
"sqlite",
|
|
||||||
] }
|
|
||||||
tokio = {version = "1.43.0", features = ["full"]}
|
|
||||||
|
|
|
||||||
|
|
@ -1,50 +1,3 @@
|
||||||
use clap::{Parser, Subcommand};
|
fn main() {
|
||||||
use futures::TryStreamExt;
|
println!("Hello from cli bin!");
|
||||||
use sqlx::postgres::PgPool;
|
|
||||||
|
|
||||||
#[derive(Parser)]
|
|
||||||
#[command(version, about, long_about = None)]
|
|
||||||
#[command(propagate_version = true)]
|
|
||||||
struct Cli {
|
|
||||||
/// Name of the person to greet
|
|
||||||
#[command(subcommand)]
|
|
||||||
command: Commands,
|
|
||||||
|
|
||||||
#[arg(short = 'u', long, global = true, env = "CLI_DB_URL")]
|
|
||||||
db_url: Option<String>,
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(Subcommand)]
|
|
||||||
enum Commands {
|
|
||||||
/// Executes a query agains the database, it pass it directly to the underliying driver
|
|
||||||
Query {
|
|
||||||
/// Query to execute
|
|
||||||
sql: String,
|
|
||||||
},
|
|
||||||
}
|
|
||||||
|
|
||||||
#[tokio::main]
|
|
||||||
async fn main() {
|
|
||||||
let cli = Cli::parse();
|
|
||||||
|
|
||||||
let url = cli.db_url.unwrap();
|
|
||||||
|
|
||||||
// You can check for the existence of subcommands, and if found use their
|
|
||||||
// matches just as you would the top level cmd
|
|
||||||
match &cli.command {
|
|
||||||
Commands::Query { sql } => handle_query(url, sql).await.unwrap(),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
async fn handle_query(url: String, query: &String) -> Result<(), sqlx::Error> {
|
|
||||||
let pool = PgPool::connect(url.as_str()).await?;
|
|
||||||
|
|
||||||
let mut rows = sqlx::query("DELETE FROM table").fetch(&pool);
|
|
||||||
|
|
||||||
while let Some(row) = rows.try_next().await? {
|
|
||||||
// map the row into a user-defined domain type
|
|
||||||
let email: &str = row.try_get("email")?;
|
|
||||||
}
|
|
||||||
|
|
||||||
return Ok(());
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue