Julio Biason
3 years ago
4 changed files with 1294 additions and 0 deletions
@ -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"] } |
@ -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(()) |
||||
} |
||||
} |
@ -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…
Reference in new issue