Struct mysql::Conn [] [src]

pub struct Conn {
    // some fields omitted
}

Mysql connection.

Methods

impl Conn
[src]

fn new<T: Into<Opts>>(opts: T) -> MyResult<Conn>

Creates new Conn.

fn reset(&mut self) -> MyResult<()>

Resets MyConn (drops state then reconnects).

fn ping(&mut self) -> bool

Executes COM_PING on Conn. Return true on success or false on error.

fn start_transaction<'a>(&'a mut self, consistent_snapshot: bool, isolation_level: Option<IsolationLevel>, readonly: Option<bool>) -> MyResult<Transaction<'a>>

Starts new transaction with provided options. readonly is only available since MySQL 5.6.5.

fn query<'a, T: AsRef<str> + 'a>(&'a mut self, query: T) -> MyResult<QueryResult<'a>>

Implements text protocol of mysql server.

Executes mysql query on Conn. QueryResult will borrow Conn until the end of its scope.

fn first<T: AsRef<str>>(&mut self, query: T) -> MyResult<Option<Row>>

Performs query and returns first row.

fn prepare<'a, T: AsRef<str> + 'a>(&'a mut self, query: T) -> MyResult<Stmt<'a>>

Implements binary protocol of mysql server.

Prepares mysql statement on Conn. Stmt will borrow Conn until the end of its scope.

This call will take statement from cache if has been prepared on this connection.

Named parameters support

prepare supports named parameters in form of :named_param_name. Allowed characters for parameter name is [a-z_]. Named parameters will be converted to positional before actual call to prepare so SELECT :a-:b, :a*:b is actually SELECT ?-?, ?*?.

// Names could be repeated
pool.prep_exec("SELECT :a+:b, :a * :b, ':c'", params!{"a" => 2, "b" => 3}).map(|mut result| {
    let row = result.next().unwrap().unwrap();
    assert_eq!((5, 6, String::from(":c")), from_row(row));
}).unwrap();

// In case of different types of parameter values
// you should convert them into Value explicitly.
pool.prep_exec("SELECT :num, :string", params!{
    "num" => Value::from(42),
    "string" => Value::from("yay"),
}).map(|mut result| {
    let row = result.next().unwrap().unwrap();
    assert_eq!((42, String::from("yay")), from_row(row));
}).unwrap();

// You can call named statement with positional parameters
pool.prep_exec("SELECT :a+:b, :a*:b", (2, 3, 2, 3)).map(|mut result| {
    let row = result.next().unwrap().unwrap();
    assert_eq!((5, 6), from_row(row));
}).unwrap();

// You must pass all named parameters for statement
let err = pool.prep_exec("SELECT :name", params!{"another_name" => 42}).unwrap_err();
match err {
    DriverError(e) => assert_eq!(MissingNamedParameter(String::from("name")), e),
    _ => unreachable!(),
}

// You can't call positional statement with named parameters
let err = pool.prep_exec("SELECT ?", params!{"first" => 42}).unwrap_err();
match err {
    DriverError(e) => assert_eq!(NamedParamsForPositionalQuery, e),
    _ => unreachable!(),
}

// You can't mix named and positional parameters
let err = pool.prepare("SELECT :a, ?").unwrap_err();
match err {
    DriverError(e) => assert_eq!(MixedParams, e),
    _ => unreachable!(),
}

fn prep_exec<'a, A, T>(&'a mut self, query: A, params: T) -> MyResult<QueryResult<'a>> where A: AsRef<str> + 'a, T: Into<Params>

Prepares and executes statement in one call. See 'Conn::prepare'

This call will take statement from cache if has been prepared on this connection.

fn first_exec<Q, P>(&mut self, query: Q, params: P) -> MyResult<Option<Row>> where Q: AsRef<str>, P: Into<Params>

Executs statement and returns first row.

Trait Implementations

impl Debug for Conn
[src]

fn fmt(&self, __arg_0: &mut Formatter) -> Result

Formats the value using the given formatter.

impl Drop for Conn
[src]

fn drop(&mut self)

A method called when the value goes out of scope. Read more