main.rs#[macro_use] extern crate diesel;
use diesel::pg::PgConnection;
use diesel::prelude::*;
mod models; // (1)
use models::Starship;
mod schema; // (2)
use schema::starships;
fn main() {
let conn = create_connection().unwrap();
add_item(&conn, &get_data()).unwrap();
}
fn add_item(conn: &PgConnection, ship: &Starship) -> Result<Starship, diesel::result::Error> {
diesel::insert_into(starships::table)
.values(ship)
.get_result(conn)
}
fn create_connection() -> Result<PgConnection, diesel::result::ConnectionError> {
dotenv::dotenv().ok();
let url = &std::env::var("DATABASE_URL").unwrap();
Ok(PgConnection::establish(url)?)
}
fn get_data() -> Starship {
Starship {
registry: "NCC-1700".to_string(),
name: "USS Constitution".to_string(),
crew: 204,
}
}
#[macro_use] extern crate diesel;
use diesel::pg::PgConnection;
use diesel::prelude::*;
use diesel::result::QueryResult;
mod models;
use models::Starship;
mod schema; // (2)
use schema::starships::dsl::*;
use clap::{Args, Parser, Subcommand};
#[derive(Parser)]
struct Cli {
#[clap(subcommand)]
command: Commands,
}
#[derive(Subcommand)]
enum Commands {
Add(Starship),
Update(StarshipFilter),
Remove(StarshipFilter),
List,
}
#[derive(Args)]
struct StarshipFilter {
#[clap(short, long)]
filter: String
}
fn main() {
let app = Cli::parse();
match app.command {
Commands::Add(s) => add_ship(&s),
Commands::List => list_ships(),
Commands::Remove(s) => remove_ship(&s),
Commands::Update(s) => update_ship(&s),
}
}
fn update_ship(s: &StarshipFilter) {
let s = &s.filter;
let conn = get_connection().unwrap();
}
fn remove_ship(s: &StarshipFilter) {
let s = &s.filter;
let conn = get_connection().unwrap();
let result = diesel::delete(
starships.filter(registry.ilike(s))
).get_result::<Starship>(&conn)
.expect("Record not found!");
println!("Removing {:?}", result);
}
fn add_ship(s: &Starship) {
let conn = get_connection().unwrap();
println!("Adding {:?}", s);
s.insert_into(starships)
.execute(&conn)
.unwrap();
}
fn list_ships() {
println!("{:?}", get_ships().unwrap());
}
fn get_connection() -> ConnectionResult<PgConnection> {
dotenv::dotenv().expect("Couldn't load .env file");
let url = &std::env::var("DATABASE_URL").unwrap();
PgConnection::establish(url)
}
fn get_ships() -> QueryResult<Vec<Starship>> {
let conn = get_connection().unwrap();
starships
.load::<Starship>(&conn)
}
#[macro_use] extern crate diesel;
use diesel::pg::PgConnection;
use diesel::prelude::*;
use diesel::result::QueryResult;
mod models;
use models::Starship;
mod schema;
use schema::starships::dsl::*;
use clap::{Args, Parser, Subcommand};
#[derive(Parser)]
struct Cli {
#[clap(subcommand)]
command: Commands,
}
#[derive(Subcommand)]
enum Commands {
Add(Starship),
Update(StarshipFilter),
Remove(StarshipFilter),
List(OptionalStarshipFilter),
}
#[derive(Args)]
struct StarshipFilter {
#[clap(short, long)]
filter: String
}
#[derive(Args)]
struct OptionalStarshipFilter {
#[clap(short,long)]
filter: Option<String>
}
fn main() {
let app = Cli::parse();
match app.command {
Commands::Add(arg) => add_ship(&arg),
Commands::List(arg) => list_ships(&arg),
Commands::Remove(arg) => remove_ship(&arg),
Commands::Update(arg) => update_ship(&arg),
}
}
fn update_ship(s: &StarshipFilter) {
let s = &s.filter;
let conn = get_connection().unwrap();
}
fn remove_ship(s: &StarshipFilter) {
let s = &s.filter;
let conn = get_connection().unwrap();
let result = diesel::delete(
starships.filter(registry.ilike(s))
).get_result::<Starship>(&conn)
.expect("Record not found!");
println!("Removing {:?}", result);
}
fn add_ship(s: &Starship) {
let conn = get_connection().unwrap();
println!("Adding {:?}", s);
s.insert_into(starships)
.execute(&conn)
.unwrap();
}
fn list_ships(arg: &OptionalStarshipFilter) {
let conn = get_connection().unwrap();
match &arg.filter {
Some(s) => println!("{:?}", starships.filter(registry.ilike(s)).load::<Starship>(&conn).unwrap()),
None => println!("{:?}", starships.load::<Starship>(&conn).unwrap()),
}
}
fn get_connection() -> ConnectionResult<PgConnection> {
dotenv::dotenv().expect("Couldn't load .env file");
let url = &std::env::var("DATABASE_URL").unwrap();
PgConnection::establish(url)
}