Struct grant::connection::DbConnection

source ·
pub struct DbConnection {
    pub connection_info: String,
    pub client: Client,
    /* private fields */
}
Expand description

Connection to the database, currently only Postgres and Redshift is supported TODO: support multiple adapters

Fields§

§connection_info: String§client: Client

Implementations§

source§

impl DbConnection

source

pub fn new(config: &Config) -> Self

A convenience function which store the connection string into connection_info and then connects to the database.

Refer to https://rust-lang-nursery.github.io/rust-cookbook/database/postgres.html for more information.

use grant::{config::Config, connection::DbConnection};
use std::str::FromStr;

let config = Config::from_str(
    r#"
      connection:
        type: postgres
        url: "postgresql://postgres:postgres@localhost:5432/postgres"
      roles: []
      users: []
    "#,
   )
   .unwrap();
   let mut db = DbConnection::new(&config);
   db.query("SELECT 1", &[]).unwrap();
source

pub fn get_current_database(&self) -> Option<&str>

Get current database name.

source

pub fn connection_info(self) -> String

Returns the connection_info

use grant::connection::DbConnection;
use std::str::FromStr;

let connection_info = "postgres://postgres:postgres@localhost:5432/postgres";
let mut client = DbConnection::from_str(connection_info).unwrap();
assert_eq!(client.connection_info(), "postgres://postgres:postgres@localhost:5432/postgres");
source

pub fn get_users(&mut self) -> Result<Vec<User>>

Get the list of users

source

pub fn get_user_database_privileges(&mut self) -> Result<Vec<UserDatabaseRole>>

Get the current database roles for user user_name in current database Returns a list of RoleDatabaseLevel

source

pub fn get_user_schema_privileges(&mut self) -> Result<Vec<UserSchemaRole>>

Get the user schema privileges for current database

source

pub fn get_user_table_privileges(&mut self) -> Result<Vec<UserTableRole>>

Get the user table privileges for current database

source

pub fn query<T>( &mut self, query: &T, params: &[&(dyn ToSql + Sync)], ) -> Result<Vec<Row>>
where T: ?Sized + ToStatement,

Executes a statement, returning the resulting rows A statement may contain parameters, specified by $n where n is the index of the parameter in the list provided, 1-indexed.

use grant::connection::DbConnection;
use std::str::FromStr;

let url = "postgresql://postgres:postgres@localhost:5432/postgres";
let mut db = DbConnection::from_str(url).unwrap();
let rows = db.query("SELECT 1 as t", &[]).unwrap();
println!("test_query: {:?}", rows);

assert_eq!(rows.len(), 1);
assert_eq!(rows.get(0).unwrap().len(), 1);

let t: i32 = rows.get(0).unwrap().get("t");
assert_eq!(t, 1);
source

pub fn execute( &mut self, query: &str, params: &[&(dyn ToSql + Sync)], ) -> Result<i64>

Executes a statement, returning the number of rows modified.

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

use grant::connection::DbConnection;
use std::str::FromStr;

let url = "postgresql://postgres:postgres@localhost:5432/postgres";
let mut db = DbConnection::from_str(url).unwrap();
let nrows = db.execute("SELECT 1 as t", &[]).unwrap();

println!("test_execute: {:?}", nrows);
assert_eq!(nrows, 1);

Trait Implementations§

source§

impl FromStr for DbConnection

source§

fn from_str(connection_info: &str) -> Result<Self>

Connection by a connection string.

use grant::connection::DbConnection;
use std::str::FromStr;

let connection_info = "postgres://postgres:postgres@localhost:5432/postgres";
let mut client = DbConnection::from_str(connection_info).unwrap();
client.query("SELECT 1", &[]).unwrap();
§

type Err = Error

The associated error which can be returned from parsing.

Auto Trait Implementations§

Blanket Implementations§

source§

impl<T> Any for T
where T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for T
where T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T, U> Into<U> for T
where U: From<T>,

source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

source§

impl<T> Same for T

§

type Output = T

Should always be Self
source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

§

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

The type returned in the event of a conversion error.
source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

§

fn vzip(self) -> V