@@ -256,10 +256,17 @@ pub trait MysqlShim<W: Write> {
256256 }
257257}
258258
259+ /// The options which passed to MysqlIntermediary struct
260+ #[ derive( Debug , Clone , PartialEq , Eq , Default ) ]
261+ pub struct MysqlIntermediaryOptions {
262+ process_use_statement_on_query : bool ,
263+ }
264+
259265/// A server that speaks the MySQL/MariaDB protocol, and can delegate client commands to a backend
260266/// that implements [`MysqlShim`](trait.MysqlShim.html).
261267pub struct MysqlIntermediary < B , R : Read , W : Write > {
262268 pub ( crate ) client_capabilities : CapabilityFlags ,
269+ process_use_statement_on_query : bool ,
263270 shim : B ,
264271 reader : packet:: PacketReader < R > ,
265272 writer : packet:: PacketWriter < W > ,
@@ -273,6 +280,17 @@ impl<B: MysqlShim<net::TcpStream>> MysqlIntermediary<B, net::TcpStream, net::Tcp
273280 let w = stream. try_clone ( ) ?;
274281 MysqlIntermediary :: run_on ( shim, stream, w)
275282 }
283+
284+ /// Create a new server over a TCP stream and process client commands until the client
285+ /// disconnects or an error occurs. See also
286+ pub fn run_on_tcp_with_options (
287+ shim : B ,
288+ stream : net:: TcpStream ,
289+ opts : & MysqlIntermediaryOptions ,
290+ ) -> Result < ( ) , B :: Error > {
291+ let w = stream. try_clone ( ) ?;
292+ MysqlIntermediary :: run_with_options ( shim, stream, w, opts)
293+ }
276294}
277295
278296impl < B : MysqlShim < S > , S : Read + Write + Clone > MysqlIntermediary < B , S , S > {
@@ -282,6 +300,16 @@ impl<B: MysqlShim<S>, S: Read + Write + Clone> MysqlIntermediary<B, S, S> {
282300 pub fn run_on_stream ( shim : B , stream : S ) -> Result < ( ) , B :: Error > {
283301 MysqlIntermediary :: run_on ( shim, stream. clone ( ) , stream)
284302 }
303+
304+ /// Create a new server over a two-way stream and process client commands until the client
305+ /// disconnects or an error ocurrs.
306+ pub fn run_on_stream_with_options (
307+ shim : B ,
308+ stream : S ,
309+ opts : & MysqlIntermediaryOptions ,
310+ ) -> Result < ( ) , B :: Error > {
311+ MysqlIntermediary :: run_with_options ( shim, stream. clone ( ) , stream, opts)
312+ }
285313}
286314
287315#[ derive( Default ) ]
@@ -296,10 +324,22 @@ impl<B: MysqlShim<W>, R: Read, W: Write> MysqlIntermediary<B, R, W> {
296324 /// Create a new server over two one-way channels and process client commands until the client
297325 /// disconnects or an error occurs.
298326 pub fn run_on ( shim : B , reader : R , writer : W ) -> Result < ( ) , B :: Error > {
327+ Self :: run_with_options ( shim, reader, writer, & Default :: default ( ) )
328+ }
329+
330+ /// Create a new server over two one-way channels and process client commands until the client
331+ /// disconnects or an error occurs, with config options.
332+ pub fn run_with_options (
333+ shim : B ,
334+ reader : R ,
335+ writer : W ,
336+ opts : & MysqlIntermediaryOptions ,
337+ ) -> Result < ( ) , B :: Error > {
299338 let r = packet:: PacketReader :: new ( reader) ;
300339 let w = packet:: PacketWriter :: new ( writer) ;
301340 let mut mi = MysqlIntermediary {
302341 client_capabilities : CapabilityFlags :: from_bits_truncate ( 0 ) ,
342+ process_use_statement_on_query : opts. process_use_statement_on_query ,
303343 shim,
304344 reader : r,
305345 writer : w,
@@ -510,7 +550,9 @@ impl<B: MysqlShim<W>, R: Read, W: Write> MysqlIntermediary<B, R, W> {
510550 w. finish ( ) ?;
511551 }
512552 }
513- } else if q. starts_with ( b"USE " ) || q. starts_with ( b"use " ) {
553+ } else if !self . process_use_statement_on_query
554+ && ( q. starts_with ( b"USE " ) || q. starts_with ( b"use " ) )
555+ {
514556 let w = InitWriter {
515557 client_capabilities : self . client_capabilities ,
516558 writer : & mut self . writer ,
0 commit comments