[][src]Struct sprattus::Connection

pub struct Connection { /* fields omitted */ }

Client for Postgres database manipulation.

Methods

impl Connection[src]

pub async fn new<'_>(connection_string: &'_ str) -> Result<Self, Error>[src]

Creates a new connection to the database.

Example:

use sprattus::*;

let conn = Connection::new("postgresql://localhost?user=tg").await?;

pub async fn execute<'_, '_, '_, '_>(
    &'_ self,
    sql: &'_ str,
    args: &'_ [&'_ (dyn ToSqlItem + Sync)]
) -> Result<u64, Error>
[src]

Executes a statement, returning the number of rows modified.

If the statement does not modify any rows (e.g. SELECT), 0 is returned.

Panics

Panics if the number of parameters provided does not match the number expected.

pub async fn batch_execute<'_, '_>(&'_ self, sql: &'_ str) -> Result<(), Error>[src]

Executes a sequence of SQL statements using the simple query protocol.

Statements should be separated by semicolons. If an error occurs, execution of the sequence will stop at that point. This is intended for use when, for example, initializing a database schema.

Warning

Prepared statements should be use for any query which contains user-specified data, as they provided the functionality to safely embed that data in the request. Do not form statements via string concatenation and pass them to this method!

pub async fn query_multiple<'_, '_, '_, '_, T>(
    &'_ self,
    sql: &'_ str,
    args: &'_ [&'_ (dyn ToSqlItem + Sync)]
) -> Result<Vec<T>, Error> where
    T: FromSql
[src]

Query multiple rows of a table.

Example:

let conn = Connection::new("postgresql://localhost?user=tg").await?;
let product_list : Vec<Product> =
   conn.query_multiple("SELECT * FROM Products LIMIT 3", &[]).await?;
assert_eq!(product_list,
    vec!(
   Product {
       prod_id : 1,
       title : String::from("ACADEMY ACADEMY")
   },
   Product {
      prod_id : 2,
      title : String::from("ACADEMY ACE")
   },
   Product {
       prod_id : 3,
       title : String::from("ACADEMY ADAPTATION")
   }));

pub async fn query<'_, '_, '_, '_, T>(
    &'_ self,
    sql: &'_ str,
    args: &'_ [&'_ (dyn ToSqlItem + Sync)]
) -> Result<T, Error> where
    T: FromSql
[src]

Get a single row of a table.

Example:

use sprattus::*;
use tokio::prelude::*;

#[derive(FromSql, Eq, PartialEq, Debug)]
struct Product {
    #[sql(primary_key)]
    prod_id: i32,
    title: String
}

#[tokio::main]
async fn main() -> Result<(), Error> {
    let conn = Connection::new("postgresql://localhost?user=tg").await?;
    let product : Product = conn.query("SELECT * FROM Products LIMIT 1", &[]).await?;
    assert_eq!(product, Product{ prod_id: 1, title: String::from("ACADEMY ACADEMY")});
    Ok(())
}

pub async fn update<'_, '_, T: FromSql + ToSql>(
    &'_ self,
    item: &'_ T
) -> Result<T, Error> where
    <T as ToSql>::PK: ToSql
[src]

Update a single rust value in the database.

Example:

use sprattus::*;
use tokio::prelude::*;

#[derive(FromSql, ToSql, Eq, PartialEq, Debug)]
struct Product {
    #[sql(primary_key)]
    prod_id: i32,
    title: String
}

#[tokio::main]
async fn main() -> Result<(), Error> {
    let conn = Connection::new("postgresql://localhost?user=tg").await?;
    // Change a existing record in the database.
    conn.update(&Product { prod_id : 50, title: String::from("Rust ORM")}).await?;

    let product : Product = conn.query("SELECT * FROM Products where prod_id = 50", &[]).await?;
    assert_eq!(product, Product{ prod_id: 50, title: String::from("Rust ORM")});
    // Change it back to it's original value.
    conn.update(&Product { prod_id : 50, title: String::from("ACADEMY BAKED")}).await?;

    let product : Product = conn.query("SELECT * FROM Products where prod_id = 50", &[]).await?;
    assert_eq!(product, Product{ prod_id: 50, title: String::from("ACADEMY BAKED")});
    Ok(())
}

pub async fn update_multiple<'_, '_, T>(
    &'_ self,
    items: &'_ [T]
) -> Result<Vec<T>, Error> where
    T: Sized + ToSql + FromSql
[src]

Update multiple rust values in the database.

Example:

use sprattus::*;
use tokio::prelude::*;

#[derive(FromSql, ToSql, Eq, PartialEq, Debug)]
struct Product {
    #[sql(primary_key)]
    prod_id: i32,
    title: String
}

#[tokio::main]
async fn main() -> Result<(), Error> {
    let conn = Connection::new("postgresql://localhost?user=tg").await?;
    let new_products = vec!(
            Product{ prod_id: 60, title: String::from("Rust ACADEMY") },
            Product{ prod_id: 61, title: String::from("SQL ACADEMY") },
            Product{ prod_id: 62, title: String::from("Backend development training") },
        );
    // Change a existing record in the database.
    conn.update_multiple(&new_products).await?;
    let sql = "SELECT * FROM Products where prod_id in (60, 61, 62)";
    let products: Vec<Product> = conn.query_multiple(sql, &[]).await?;
    assert_eq!(products, new_products);
    Ok(())
}

pub async fn create<'_, '_, T>(&'_ self, item: &'_ T) -> Result<T, Error> where
    T: Sized + ToSql + FromSql
[src]

Create a new row in the database.

Example:

use sprattus::*;
use tokio::prelude::*;

#[derive(FromSql, ToSql, Eq, PartialEq, Debug)]
struct Product {
    #[sql(primary_key)]
    prod_id: i32,
    title: String
}

#[tokio::main]
async fn main() -> Result<(), Error> {
    let conn = Connection::new("postgresql://localhost?user=tg").await?;
    let new_product = Product {prod_id: 0, title: String::from("Sql insert lesson")};
    let product = conn.create(&new_product).await?;

    assert_eq!(new_product, product);
    Ok(())
}

pub async fn create_multiple<'_, '_, T>(
    &'_ self,
    items: &'_ [T]
) -> Result<Vec<T>, Error> where
    T: Sized + ToSql + FromSql
[src]

Create new rows in the database.

Example:

use sprattus::*;
use tokio::prelude::*;

#[derive(FromSql, ToSql, Eq, PartialEq, Debug)]
struct Product {
    #[sql(primary_key)]
    prod_id: i32,
    title: String
}

#[tokio::main]
async fn main() -> Result<(), Error> {
    let conn = Connection::new("postgresql://localhost?user=tg").await?;
    let new_products = vec!(
        Product {prod_id: 0, title: String::from("Sql insert lesson")},
        Product {prod_id: 0, title: String::from("Rust macro lesson")},
        Product {prod_id: 0, title: String::from("Postgres data types lesson")}
    );
    let products = conn.create_multiple(&new_products).await?;

    assert_eq!(&new_products, &products);

    conn.delete_multiple(&products).await?;
    Ok(())
}

pub async fn delete<'_, '_, T: FromSql + ToSql>(
    &'_ self,
    item: &'_ T
) -> Result<T, Error> where
    <T as ToSql>::PK: ToSql + Sync
[src]

Deletes a item.

Example:

use sprattus::*;
use tokio::prelude::*;

#[derive(FromSql, ToSql, Eq, PartialEq, Debug)]
struct Product {
    #[sql(primary_key)]
    prod_id: i32,
    title: String
}

#[tokio::main]
async fn main() -> Result<(), Error> {
    let conn = Connection::new("postgresql://localhost?user=tg").await?;

    let new_product = Product {prod_id: 0, title: String::from("Sql insert lesson")};
    let product = conn.create(&new_product).await?;
    let deleted_product = conn.delete(&product).await?;

    assert_eq!(&product, &deleted_product);
    Ok(())
}

pub async fn delete_multiple<'_, '_, P, T>(
    &'_ self,
    items: &'_ [T]
) -> Result<Vec<T>, Error> where
    P: ToSql,
    T: FromSql + ToSql<PK = P>,
    <T as ToSql>::PK: Sync
[src]

Deletes a list of items.

Example:

use sprattus::*;
use tokio::prelude::*;

#[derive(FromSql, ToSql, Eq, PartialEq, Debug)]
struct Product {
    #[sql(primary_key)]
    prod_id: i32,
    title: String
}

#[tokio::main]
async fn main() -> Result<(), Error> {
    let conn = Connection::new("postgresql://localhost?user=tg").await?;
    let new_products = vec!(
        Product {prod_id: 0, title: String::from("Sql insert lesson")},
        Product {prod_id: 0, title: String::from("Rust macro lesson")},
        Product {prod_id: 0, title: String::from("Postgres data types lesson")}
    );
    let created_products = conn.create_multiple(&new_products).await?;

    let deleted_products = conn.delete_multiple(&created_products).await?;
    assert_eq!(&created_products, &deleted_products);
    Ok(())
}

Trait Implementations

impl Clone for Connection[src]

Auto Trait Implementations

Blanket Implementations

impl<T, U> Into<U> for T where
    U: From<T>, 
[src]

impl<T> From<T> for T[src]

impl<T> ToOwned for T where
    T: Clone
[src]

type Owned = T

The resulting type after obtaining ownership.

impl<T, U> TryFrom<U> for T where
    U: Into<T>, 
[src]

type Error = Infallible

The type returned in the event of a conversion error.

impl<T, U> TryInto<U> for T where
    U: TryFrom<T>, 
[src]

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.

impl<T> Borrow<T> for T where
    T: ?Sized
[src]

impl<T> BorrowMut<T> for T where
    T: ?Sized
[src]

impl<T> Any for T where
    T: 'static + ?Sized
[src]

impl<T> Same<T> for T

type Output = T

Should always be Self