1+ use crate :: statement_cache:: CacheSize ;
2+ use crate :: statement_cache:: { MaybeCached , QueryFragmentForCachedStatement , StatementCache } ;
13use crate :: stmt_cache:: { CallbackHelper , QueryFragmentHelper } ;
24use crate :: { AnsiTransactionManager , AsyncConnection , SimpleAsyncConnection } ;
3- use diesel:: connection:: statement_cache:: {
4- MaybeCached , QueryFragmentForCachedStatement , StatementCache ,
5- } ;
5+ use diesel:: connection:: Instrumentation ;
6+ use diesel:: connection:: InstrumentationEvent ;
67use diesel:: connection:: StrQueryHelper ;
7- use diesel:: connection:: { CacheSize , Instrumentation } ;
8- use diesel:: connection:: { DynInstrumentation , InstrumentationEvent } ;
98use diesel:: mysql:: { Mysql , MysqlQueryBuilder , MysqlType } ;
109use diesel:: query_builder:: QueryBuilder ;
1110use diesel:: query_builder:: { bind_collector:: RawBytesBindCollector , QueryFragment , QueryId } ;
@@ -33,7 +32,7 @@ pub struct AsyncMysqlConnection {
3332 conn : mysql_async:: Conn ,
3433 stmt_cache : StatementCache < Mysql , Statement > ,
3534 transaction_manager : AnsiTransactionManager ,
36- instrumentation : DynInstrumentation ,
35+ instrumentation : Option < Box < dyn Instrumentation > > ,
3736}
3837
3938impl SimpleAsyncConnection for AsyncMysqlConnection {
@@ -74,7 +73,7 @@ impl AsyncConnection for AsyncMysqlConnection {
7473 type TransactionManager = AnsiTransactionManager ;
7574
7675 async fn establish ( database_url : & str ) -> diesel:: ConnectionResult < Self > {
77- let mut instrumentation = DynInstrumentation :: default_instrumentation ( ) ;
76+ let mut instrumentation = diesel :: connection :: get_default_instrumentation ( ) ;
7877 instrumentation. on_connection_event ( InstrumentationEvent :: start_establish_connection (
7978 database_url,
8079 ) ) ;
@@ -99,10 +98,6 @@ impl AsyncConnection for AsyncMysqlConnection {
9998 let stmt_for_exec = match stmt {
10099 MaybeCached :: Cached ( ref s) => ( * s) . clone ( ) ,
101100 MaybeCached :: CannotCache ( ref s) => s. clone ( ) ,
102- _ => unreachable ! (
103- "Diesel has only two variants here at the time of writing.\n \
104- If you ever see this error message please open in issue in the diesel-async issue tracker"
105- ) ,
106101 } ;
107102
108103 let ( tx, rx) = futures_channel:: mpsc:: channel ( 0 ) ;
@@ -179,11 +174,11 @@ impl AsyncConnection for AsyncMysqlConnection {
179174 }
180175
181176 fn instrumentation ( & mut self ) -> & mut dyn Instrumentation {
182- & mut * self . instrumentation
177+ & mut self . instrumentation
183178 }
184179
185180 fn set_instrumentation ( & mut self , instrumentation : impl Instrumentation ) {
186- self . instrumentation = instrumentation . into ( ) ;
181+ self . instrumentation = Some ( Box :: new ( instrumentation ) ) ;
187182 }
188183
189184 fn set_prepared_statement_cache_size ( & mut self , size : CacheSize ) {
@@ -211,7 +206,7 @@ fn update_transaction_manager_status<T>(
211206fn prepare_statement_helper < ' a > (
212207 conn : & ' a mut mysql_async:: Conn ,
213208 sql : & str ,
214- _is_for_cache : diesel :: connection :: statement_cache:: PrepareForCache ,
209+ _is_for_cache : crate :: statement_cache:: PrepareForCache ,
215210 _metadata : & [ MysqlType ] ,
216211) -> CallbackHelper < impl Future < Output = QueryResult < ( Statement , & ' a mut mysql_async:: Conn ) > > + Send >
217212{
@@ -239,7 +234,7 @@ impl AsyncMysqlConnection {
239234 conn,
240235 stmt_cache : StatementCache :: new ( ) ,
241236 transaction_manager : AnsiTransactionManager :: default ( ) ,
242- instrumentation : DynInstrumentation :: default_instrumentation ( ) ,
237+ instrumentation : diesel :: connection :: get_default_instrumentation ( ) ,
243238 } ;
244239
245240 for stmt in CONNECTION_SETUP_QUERIES {
@@ -296,21 +291,25 @@ impl AsyncMysqlConnection {
296291 sql,
297292 safe_to_cache : is_safe_to_cache_prepared,
298293 } ;
294+
299295 let inner = async {
300- let ( stmt, conn) = stmt_cache
301- . cached_statement_non_generic (
302- query_id,
303- & helper,
304- & Mysql ,
305- & metadata,
306- conn,
307- prepare_statement_helper,
308- & mut * * instrumentation,
309- )
310- . await ?;
296+ let ( stmt, conn) = {
297+ stmt_cache
298+ . cached_statement_non_generic (
299+ query_id,
300+ & helper,
301+ & Mysql ,
302+ & metadata,
303+ conn,
304+ prepare_statement_helper,
305+ & mut * instrumentation,
306+ )
307+ . await ?
308+ } ;
311309 callback ( conn, stmt, ToSqlHelper { metadata, binds } ) . await
312310 } ;
313311 let r = update_transaction_manager_status ( inner. await , transaction_manager) ;
312+
314313 instrumentation. on_connection_event ( InstrumentationEvent :: finish_query (
315314 & StrQueryHelper :: new ( & helper. sql ) ,
316315 r. as_ref ( ) . err ( ) ,
@@ -371,7 +370,7 @@ impl AsyncMysqlConnection {
371370 conn,
372371 stmt_cache : StatementCache :: new ( ) ,
373372 transaction_manager : AnsiTransactionManager :: default ( ) ,
374- instrumentation : DynInstrumentation :: none ( ) ,
373+ instrumentation : diesel :: connection :: get_default_instrumentation ( ) ,
375374 } )
376375 }
377376}
0 commit comments