Trait mysql_async::prelude::LocalInfileHandler[][src]

pub trait LocalInfileHandler: Sync + Send {
    fn handle(&self, file_name: &[u8]) -> InfileHandlerFuture;
}

Trait used to handle local infile requests.

Be aware of security issues with LOAD DATA LOCAL. Using crate::WhiteListFsLocalInfileHandler is advised.

Simple handler example:

/// This example hanlder will return contained bytes in response to a local infile request.
struct ExampleHandler(&'static [u8]);

impl LocalInfileHandler for ExampleHandler {
    fn handle(&self, _: &[u8]) -> mysql_async::InfileHandlerFuture {
        let handler = Box::new(self.0) as Box<_>;
        Box::pin(async move { Ok(handler) })
    }
}


let opts = OptsBuilder::from_opts(database_url)
    .local_infile_handler(Some(ExampleHandler(b"foobar")));

let pool = mysql_async::Pool::new(opts);

let mut conn = pool.get_conn().await?;
conn.query_drop("CREATE TEMPORARY TABLE tmp (a TEXT);").await?;
match conn.query_drop("LOAD DATA LOCAL INFILE 'baz' INTO TABLE tmp;").await {
    Ok(()) => (),
    Err(Error::Server(ref err)) if err.code == 1148 => {
        // The used command is not allowed with this MySQL version
        return Ok(());
    },
    Err(Error::Server(ref err)) if err.code == 3948 => {
        // Loading local data is disabled;
        // this must be enabled on both the client and server sides
        return Ok(());
    }
    e @ Err(_) => e.unwrap(),
};
let result: Vec<String> = conn.exec("SELECT * FROM tmp", ()).await?;

assert_eq!(result.len(), 1);
assert_eq!(result[0], "foobar");

conn; // dropped connection will go to the pool

pool.disconnect().await?;

Required methods

fn handle(&self, file_name: &[u8]) -> InfileHandlerFuture[src]

file_name is the file name in LOAD DATA LOCAL INFILE '<file name>' INTO TABLE ...; query.

Loading content...

Implementors

impl LocalInfileHandler for WhiteListFsLocalInfileHandler[src]

Loading content...