Struct mysql::Conn

source ·
pub struct Conn { /* private fields */ }
Expand description

Mysql connection.

Implementations§

Creates new Conn.

Resets MyConn (drops state then reconnects).

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

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

Implements text protocol of mysql server.

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

Performs query and returns first row.

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:

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

You should wrap such parameters to a proper json value. For example if you are using rustc_serialize for Json support:

pool.prep_exec(r#"SELECT JSON_REPLACE('{"a": true}', '$.a', ?)"#, (Json::Boolean(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!(),
}

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

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

Executes statement and returns first row.

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.

Trait Implementations§

Formats the value using the given formatter. Read more
Executes the destructor for this type. Read more

Auto Trait Implementations§

Blanket Implementations§

Gets the TypeId of self. Read more
Immutably borrows from an owned value. Read more
Mutably borrows from an owned value. Read more

Returns the argument unchanged.

Calls U::from(self).

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

Should always be Self
The type returned in the event of a conversion error.
Performs the conversion.
The type returned in the event of a conversion error.
Performs the conversion.