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: ClientImplementations§
Source§impl DbConnection
impl DbConnection
Sourcepub fn new(config: &Config) -> Result<Self>
pub fn new(config: &Config) -> Result<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();Sourcepub fn get_current_database(&self) -> Option<&str>
pub fn get_current_database(&self) -> Option<&str>
Get current database name.
Sourcepub fn connection_info(self) -> String
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");Sourcepub fn get_user_database_privileges(&mut self) -> Result<Vec<UserDatabaseRole>>
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
Sourcepub fn get_user_schema_privileges(&mut self) -> Result<Vec<UserSchemaRole>>
pub fn get_user_schema_privileges(&mut self) -> Result<Vec<UserSchemaRole>>
Get the user schema privileges for current database
Sourcepub fn get_user_table_privileges(&mut self) -> Result<Vec<UserTableRole>>
pub fn get_user_table_privileges(&mut self) -> Result<Vec<UserTableRole>>
Get the user table privileges for current database
Sourcepub fn query<T>(
&mut self,
query: &T,
params: &[&(dyn ToSql + Sync)],
) -> Result<Vec<Row>>where
T: ?Sized + ToStatement,
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);Sourcepub fn execute(
&mut self,
query: &str,
params: &[&(dyn ToSql + Sync)],
) -> Result<i64>
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
impl FromStr for DbConnection
Source§fn from_str(connection_info: &str) -> Result<Self>
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();