[][src]Struct mysql::Conn

pub struct Conn { /* fields omitted */ }

Mysql connection.

Methods

impl Conn[src]

pub fn new<T: Into<Opts>>(opts: T) -> MyResult<Conn>[src]

Creates new Conn.

pub fn reset(&mut self) -> MyResult<()>[src]

Resets MyConn (drops state then reconnects).

pub fn ping(&mut self) -> bool[src]

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

pub fn select_db(&mut self, schema: &str) -> bool[src]

Executes COM_INIT_DB on Conn.

pub fn start_transaction(
    &mut self,
    consistent_snapshot: bool,
    isolation_level: Option<IsolationLevel>,
    readonly: Option<bool>
) -> MyResult<Transaction>
[src]

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

pub fn query<T: AsRef<str>>(&mut self, query: T) -> MyResult<QueryResult>[src]

Implements text protocol of mysql server.

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

pub fn first<T: AsRef<str>, U: FromRow>(
    &mut self,
    query: T
) -> MyResult<Option<U>>
[src]

Performs query and returns first row.

pub fn prepare<T: AsRef<str>>(&mut self, query: T) -> MyResult<Stmt>[src]

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.

JSON caveats

For the following statement you will get somewhat unexpected result {"a": 0}, because booleans in mysql binary protocol is TINYINT(1) and will be interpreted as 0:

This example is not tested
pool.prep_exec(r#"SELECT JSON_REPLACE('{"a": true}', '$.a', ?)"#, (false,));

You should wrap such parameters to a proper json value. For example:

This example is not tested
pool.prep_exec(r#"SELECT JSON_REPLACE('{"a": true}', '$.a', ?)"#, (Value::Bool(false),));

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();

// 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("name".into()), 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!(),
}

pub fn prep_exec<A, T>(&mut self, query: A, params: T) -> MyResult<QueryResult> where
    A: AsRef<str>,
    T: Into<Params>, 
[src]

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

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

pub fn first_exec<Q, P, T>(
    &mut self,
    query: Q,
    params: P
) -> MyResult<Option<T>> where
    Q: AsRef<str>,
    P: Into<Params>,
    T: FromRow
[src]

Executes statement and returns first row.

pub fn set_local_infile_handler(&mut self, handler: Option<LocalInfileHandler>)[src]

Sets a callback to handle requests for local files. These are caused by using LOAD DATA LOCAL INFILE queries. The callback is passed the filename, and a Writeable object to receive the contents of that file. Specifying None will reset the handler to the one specified in the Opts for this connection.

pub fn no_backslash_escape(&self) -> bool[src]

Trait Implementations

impl GenericConnection for Conn[src]

impl Drop for Conn[src]

impl Debug for Conn[src]

Auto Trait Implementations

impl Send for Conn

impl Sync for Conn

impl Unpin for Conn

impl UnwindSafe for Conn

impl RefUnwindSafe for Conn

Blanket Implementations

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

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

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<V, T> VZip<V> for T where
    V: MultiLane<T>, 

impl<T> Same<T> for T

type Output = T

Should always be Self