diesel::mysql

Struct MysqlConnection

Source
pub struct MysqlConnection { /* private fields */ }
Available on crate features mysql and mysql_backend only.
Expand description

A connection to a MySQL database. Connection URLs should be in the form mysql://[user[:password]@]host/database_name[?unix_socket=socket-path&ssl_mode=SSL_MODE*&ssl_ca=/etc/ssl/certs/ca-certificates.crt&ssl_cert=/etc/ssl/certs/client-cert.crt&ssl_key=/etc/ssl/certs/client-key.crt]

  • host can be an IP address or a hostname. If it is set to localhost, a connection will be attempted through the socket at /tmp/mysql.sock. If you want to connect to a local server via TCP (e.g. docker containers), use 0.0.0.0 or 127.0.0.1 instead.
  • unix_socket expects the path to the unix socket
  • ssl_ca accepts a path to the system’s certificate roots
  • ssl_cert accepts a path to the client’s certificate file
  • ssl_key accepts a path to the client’s private key file
  • ssl_mode expects a value defined for MySQL client command option --ssl-mode See https://dev.mysql.com/doc/refman/5.7/en/connection-options.html#option_general_ssl-mode

§Supported loading model implementations

As MysqlConnection only supports a single loading mode implementation it is not required to explicitly specify a loading mode when calling RunQueryDsl::load_iter() or LoadConnection::load

§DefaultLoadingMode

MysqlConnection only supports a single loading mode, which loads values row by row from the result set.

use diesel::connection::DefaultLoadingMode;
{ // scope to restrict the lifetime of the iterator
    let iter1 = users::table.load_iter::<(i32, String), DefaultLoadingMode>(connection)?;

    for r in iter1 {
        let (id, name) = r?;
        println!("Id: {} Name: {}", id, name);
    }
}

// works without specifying the loading mode
let iter2 = users::table.load_iter::<(i32, String), _>(connection)?;

for r in iter2 {
    let (id, name) = r?;
    println!("Id: {} Name: {}", id, name);
}

This mode does not support creating multiple iterators using the same connection.

use diesel::connection::DefaultLoadingMode;

let iter1 = users::table.load_iter::<(i32, String), DefaultLoadingMode>(connection)?;
let iter2 = users::table.load_iter::<(i32, String), DefaultLoadingMode>(connection)?;

for r in iter1 {
    let (id, name) = r?;
    println!("Id: {} Name: {}", id, name);
}

for r in iter2 {
    let (id, name) = r?;
    println!("Id: {} Name: {}", id, name);
}

Trait Implementations§

Source§

impl Connection for MysqlConnection

Source§

fn establish(database_url: &str) -> ConnectionResult<Self>

Establishes a new connection to the MySQL database database_url may be enhanced by GET parameters mysql://[user[:password]@]host[:port]/database_name[?unix_socket=socket-path&ssl_mode=SSL_MODE*&ssl_ca=/etc/ssl/certs/ca-certificates.crt&ssl_cert=/etc/ssl/certs/client-cert.crt&ssl_key=/etc/ssl/certs/client-key.crt]

  • host can be an IP address or a hostname. If it is set to localhost, a connection will be attempted through the socket at /tmp/mysql.sock. If you want to connect to a local server via TCP (e.g. docker containers), use 0.0.0.0 or 127.0.0.1 instead.
  • unix_socket expects the path to the unix socket
  • ssl_ca accepts a path to the system’s certificate roots
  • ssl_cert accepts a path to the client’s certificate file
  • ssl_key accepts a path to the client’s private key file
  • ssl_mode expects a value defined for MySQL client command option --ssl-mode See https://dev.mysql.com/doc/refman/5.7/en/connection-options.html#option_general_ssl-mode
Source§

type Backend = Mysql

The backend this type connects to
Source§

fn set_instrumentation(&mut self, instrumentation: impl Instrumentation)

Set a specific Instrumentation implementation for this connection
Source§

fn transaction<T, E, F>(&mut self, f: F) -> Result<T, E>
where F: FnOnce(&mut Self) -> Result<T, E>, E: From<Error>,

Executes the given function inside of a database transaction Read more
Source§

fn begin_test_transaction(&mut self) -> QueryResult<()>

Creates a transaction that will never be committed. This is useful for tests. Panics if called while inside of a transaction or if called with a connection containing a broken transaction
Source§

fn test_transaction<T, E, F>(&mut self, f: F) -> T
where F: FnOnce(&mut Self) -> Result<T, E>, E: Debug,

Executes the given function inside a transaction, but does not commit it. Panics if the given function returns an error. Read more
Source§

impl LoadConnection for MysqlConnection

Source§

type Cursor<'conn, 'query> = StatementIterator<'conn>

The cursor type returned by LoadConnection::load Read more
Source§

type Row<'conn, 'query> = MysqlRow

The row type used as Iterator::Item for the iterator implementation of LoadConnection::Cursor
Source§

impl MigrationConnection for MysqlConnection

Source§

fn setup(&mut self) -> QueryResult<usize>

Setup the following table: Read more
Source§

impl R2D2Connection for MysqlConnection

Available on crate feature r2d2 only.
Source§

fn ping(&mut self) -> QueryResult<()>

Check if a connection is still valid
Source§

fn is_broken(&mut self) -> bool

Checks if the connection is broken and should not be reused Read more
Source§

impl SimpleConnection for MysqlConnection

Source§

fn batch_execute(&mut self, query: &str) -> QueryResult<()>

Execute multiple SQL statements within the same string. Read more
Source§

impl<'b, Changes, Output> UpdateAndFetchResults<Changes, Output> for MysqlConnection
where Changes: Copy + Identifiable + AsChangeset<Target = <Changes as HasTable>::Table> + IntoUpdateTarget, Changes::Table: FindDsl<Changes::Id>, Update<Changes, Changes>: ExecuteDsl<MysqlConnection>, Find<Changes::Table, Changes::Id>: LoadQuery<'b, MysqlConnection, Output>, <Changes::Table as Table>::AllColumns: ValidGrouping<()>, <<Changes::Table as Table>::AllColumns as ValidGrouping<()>>::IsAggregate: MixedAggregates<No, Output = No>,

Source§

fn update_and_fetch(&mut self, changeset: Changes) -> QueryResult<Output>

See the traits documentation.
Source§

impl Send for MysqlConnection

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<C> BoxableConnection<<C as Connection>::Backend> for C
where C: Connection + Any,

Source§

fn as_any(&self) -> &(dyn Any + 'static)

Source§

fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)

Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

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

Source§

impl<T> IntoSql for T

Source§

fn into_sql<T>(self) -> AsExprOf<Self, T>

Convert self to an expression for Diesel’s query builder. Read more
Source§

fn as_sql<'a, T>(&'a self) -> AsExprOf<&'a Self, T>
where &'a Self: AsExpression<T>, T: SqlType + TypedExpressionType,

Convert &self to an expression for Diesel’s query builder. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<T> ErasedDestructor for T
where T: 'static,

Source§

impl<T> MaybeSendSync for T