Cratepostgres [−][src]
A pure-Rust frontend for the popular PostgreSQL database.
externcratepostgres;usepostgres::{Connection,TlsMode};structPerson {id:i32,name:String,data:Option<Vec<u8>>}fnmain() {letconn=Connection::connect("postgresql://postgres@localhost:5433",TlsMode::None) .unwrap();conn.execute("CREATE TABLE person ( id SERIAL PRIMARY KEY, name VARCHAR NOT NULL, data BYTEA )",&[]).unwrap();letme=Person {id:0,name:"Steven".to_owned(),data:None };conn.execute("INSERT INTO person (name, data) VALUES ($1, $2)",&[&me.name,&me.data]).unwrap();forrowin&conn.query("SELECT id, name, data FROM person",&[]).unwrap() {letperson=Person {id:row.get(0),name:row.get(1),data:row.get(2) };println!("Found person {}",person.name); }}
SSL/TLS
This crate supports TLS secured connections. TheTlsMode
enum is passed to connection methodsand indicates if the connection will not, may, or must be secured by TLS. The TLS implementationis pluggable through theTlsHandshake
trait. Implementations for OpenSSL, Secure Transport,SChannel, and thenative-tls
crate are provided behind thewith-openssl
,with-security-framework
,with-schannel
, andwith-native-tls
feature flags respectively.
Examples
Connecting usingnative-tls
:
externcratepostgres;usepostgres::{Connection,TlsMode};usepostgres::tls::native_tls::NativeTls;fnmain() {letnegotiator=NativeTls::new().unwrap();letconn=Connection::connect("postgres://postgres@localhost:5433",TlsMode::Require(&negotiator)) .unwrap();}
Modules
error | Errors. |
notification | Asynchronous notifications. |
params | Connection parameters |
rows | Query result rows. |
stmt | Prepared statements |
tls | Types and traits for TLS support. |
transaction | Transactions |
types | Types. |
Macros
accepts | Generates a simple implementation of |
to_sql_checked | Generates an implementation of |
Structs
CancelData | Contains information necessary to cancel queries for a session. |
Connection | A connection to a Postgres database. |
Error | An error communicating with the Postgres server. |
LoggingNoticeHandler | A notice handler which logs at the |
Enums
TlsMode | Specifies the TLS support requested for a new connection. |
Traits
GenericConnection | A trait allowing abstraction over connections and transactions |
HandleNotice | A trait implemented by types that can handle Postgres notice messages. |
Functions
cancel_query | Attempts to cancel an in-progress query. |
Type Definitions
Result | A type alias of the result returned by many methods. |