wip: add sqlx setup

This commit is contained in:
Alexander Navarro 2025-01-14 20:24:25 -03:00
parent 57074a89ff
commit 704be76887
5 changed files with 1856 additions and 14 deletions

1
.gitignore vendored
View file

@ -7,3 +7,4 @@ secring.*
# Added by cargo # Added by cargo
/target /target
.env

4
.justfile Normal file
View file

@ -0,0 +1,4 @@
set dotenv-load := true
dev:
cargo run --bin cli -- query "SELECT * FROM sources;"

1834
Cargo.lock generated

File diff suppressed because it is too large Load diff

View file

@ -5,3 +5,10 @@ edition = "2021"
[dependencies] [dependencies]
clap = { version = "4.5.26", features = ["derive", "env"] } 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"]}

View file

@ -1,4 +1,6 @@
use clap::{Parser, Subcommand}; use clap::{Parser, Subcommand};
use futures::TryStreamExt;
use sqlx::postgres::PgPool;
#[derive(Parser)] #[derive(Parser)]
#[command(version, about, long_about = None)] #[command(version, about, long_about = None)]
@ -21,14 +23,28 @@ enum Commands {
}, },
} }
fn main() { #[tokio::main]
async fn main() {
let cli = Cli::parse(); let cli = Cli::parse();
let url = cli.db_url.unwrap();
// You can check for the existence of subcommands, and if found use their // You can check for the existence of subcommands, and if found use their
// matches just as you would the top level cmd // matches just as you would the top level cmd
match &cli.command { match &cli.command {
Commands::Query { sql } => { Commands::Query { sql } => handle_query(url, sql).await.unwrap(),
println!("Provided query: {sql:?}");
}
} }
} }
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(());
}