Browse Source

Trying Any pool

master
Julio Biason 3 years ago
parent
commit
0cce5c4ced
  1. 1237
      sqlxany/Cargo.lock
  2. 10
      sqlxany/Cargo.toml
  3. 34
      sqlxany/src/entity.rs
  4. 13
      sqlxany/src/main.rs

1237
sqlxany/Cargo.lock generated

File diff suppressed because it is too large Load Diff

10
sqlxany/Cargo.toml

@ -0,0 +1,10 @@
[package]
name = "sqlxany"
version = "0.1.0"
edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
sqlx = { version = "0.5.11", features = ["runtime-tokio-rustls", "postgres", "sqlite", "any", "macros", "migrate"] }
tokio = { version = "1.17.0", features = ["rt", "rt-multi-thread", "macros", "time"] }

34
sqlxany/src/entity.rs

@ -0,0 +1,34 @@
//! Uma entidade
use sqlx::{Any, Pool};
const INSERT: &str = r#"
INSERT INTO entity
(id, description)
VALUES
($1, $2);
"#;
#[derive(sqlx::FromRow)]
pub struct Entity {
id: i64,
description: String,
}
impl Entity {
pub fn new(id: i64, description: &str) -> Self {
Self {
id,
description: description.into(),
}
}
pub async fn save(&self, db: &Pool<Any>) -> Result<(), sqlx::Error> {
sqlx::query(INSERT)
.bind(&self.id)
.bind(&self.description)
.execute(db)
.await?;
Ok(())
}
}

13
sqlxany/src/main.rs

@ -0,0 +1,13 @@
use sqlx::{Any, Pool};
mod entity;
#[tokio::main]
async fn main() {
let record = entity::Entity::new(1, "Something");
let pool =
Pool::<Any>::connect(&std::env::var("DATABASE_URL").expect("I need DATABASE_URL set!"))
.await
.expect("Failed to connect to the database");
record.save(&pool).await.unwrap();
}
Loading…
Cancel
Save