@@ -18,6 +18,7 @@ mod sea_query_db;
1818use std:: fmt:: { Display , Formatter , Write } ;
1919use std:: hash:: Hash ;
2020use std:: str:: FromStr ;
21+ use std:: sync:: Arc ;
2122
2223use async_trait:: async_trait;
2324pub use cot_macros:: { model, query} ;
@@ -789,10 +790,9 @@ pub trait SqlxValueRef<'r>: Sized {
789790/// It is used to execute queries and interact with the database. The connection
790791/// is established when the structure is created and closed when
791792/// [`Self::close()`] is called.
792- #[ derive( Debug ) ]
793+ #[ derive( Debug , Clone ) ]
793794pub struct Database {
794- _url : String ,
795- inner : DatabaseImpl ,
795+ inner : Arc < DatabaseImpl > ,
796796}
797797
798798#[ derive( Debug ) ]
@@ -837,26 +837,23 @@ impl Database {
837837 if url. starts_with ( "sqlite:" ) {
838838 let inner = DatabaseSqlite :: new ( & url) . await ?;
839839 return Ok ( Self {
840- _url : url,
841- inner : DatabaseImpl :: Sqlite ( inner) ,
840+ inner : Arc :: new ( DatabaseImpl :: Sqlite ( inner) ) ,
842841 } ) ;
843842 }
844843
845844 #[ cfg( feature = "postgres" ) ]
846845 if url. starts_with ( "postgresql:" ) {
847846 let inner = DatabasePostgres :: new ( & url) . await ?;
848847 return Ok ( Self {
849- _url : url,
850- inner : DatabaseImpl :: Postgres ( inner) ,
848+ inner : Arc :: new ( DatabaseImpl :: Postgres ( inner) ) ,
851849 } ) ;
852850 }
853851
854852 #[ cfg( feature = "mysql" ) ]
855853 if url. starts_with ( "mysql:" ) {
856854 let inner = DatabaseMySql :: new ( & url) . await ?;
857855 return Ok ( Self {
858- _url : url,
859- inner : DatabaseImpl :: MySql ( inner) ,
856+ inner : Arc :: new ( DatabaseImpl :: MySql ( inner) ) ,
860857 } ) ;
861858 }
862859
@@ -886,7 +883,7 @@ impl Database {
886883 /// }
887884 /// ```
888885 pub async fn close ( & self ) -> Result < ( ) > {
889- match & self . inner {
886+ match & * self . inner {
890887 #[ cfg( feature = "sqlite" ) ]
891888 DatabaseImpl :: Sqlite ( inner) => inner. close ( ) . await ,
892889 #[ cfg( feature = "postgres" ) ]
@@ -1124,7 +1121,7 @@ impl Database {
11241121 return Ok ( ( ) ) ;
11251122 }
11261123
1127- let max_params = match self . inner {
1124+ let max_params = match & * self . inner {
11281125 // https://sqlite.org/limits.html#max_variable_number
11291126 // Assuming SQLite > 3.32.0 (2020-05-22)
11301127 #[ cfg( feature = "sqlite" ) ]
@@ -1471,7 +1468,7 @@ impl Database {
14711468 . collect :: < Vec < _ > > ( ) ;
14721469 let values = SqlxValues ( sea_query:: Values ( values) ) ;
14731470
1474- let result = match & self . inner {
1471+ let result = match & * self . inner {
14751472 #[ cfg( feature = "sqlite" ) ]
14761473 DatabaseImpl :: Sqlite ( inner) => inner. raw_with ( query, values) . await ?,
14771474 #[ cfg( feature = "postgres" ) ]
@@ -1487,7 +1484,7 @@ impl Database {
14871484 where
14881485 T : SqlxBinder + Send + Sync ,
14891486 {
1490- let result = match & self . inner {
1487+ let result = match & * self . inner {
14911488 #[ cfg( feature = "sqlite" ) ]
14921489 DatabaseImpl :: Sqlite ( inner) => inner. fetch_option ( statement) . await ?. map ( Row :: Sqlite ) ,
14931490 #[ cfg( feature = "postgres" ) ]
@@ -1502,7 +1499,7 @@ impl Database {
15021499 }
15031500
15041501 fn supports_returning ( & self ) -> bool {
1505- match self . inner {
1502+ match & * self . inner {
15061503 #[ cfg( feature = "sqlite" ) ]
15071504 DatabaseImpl :: Sqlite ( _) => true ,
15081505 #[ cfg( feature = "postgres" ) ]
@@ -1516,7 +1513,7 @@ impl Database {
15161513 where
15171514 T : SqlxBinder + Send + Sync ,
15181515 {
1519- let result = match & self . inner {
1516+ let result = match & * self . inner {
15201517 #[ cfg( feature = "sqlite" ) ]
15211518 DatabaseImpl :: Sqlite ( inner) => inner
15221519 . fetch_all ( statement)
@@ -1547,7 +1544,7 @@ impl Database {
15471544 where
15481545 T : SqlxBinder + Send + Sync ,
15491546 {
1550- let result = match & self . inner {
1547+ let result = match & * self . inner {
15511548 #[ cfg( feature = "sqlite" ) ]
15521549 DatabaseImpl :: Sqlite ( inner) => inner. execute_statement ( statement) . await ?,
15531550 #[ cfg( feature = "postgres" ) ]
@@ -1563,7 +1560,7 @@ impl Database {
15631560 & self ,
15641561 statement : T ,
15651562 ) -> Result < StatementResult > {
1566- let result = match & self . inner {
1563+ let result = match & * self . inner {
15671564 #[ cfg( feature = "sqlite" ) ]
15681565 DatabaseImpl :: Sqlite ( inner) => inner. execute_schema ( statement) . await ?,
15691566 #[ cfg( feature = "postgres" ) ]
@@ -1578,7 +1575,7 @@ impl Database {
15781575
15791576impl ColumnTypeMapper for Database {
15801577 fn sea_query_column_type_for ( & self , column_type : ColumnType ) -> sea_query:: ColumnType {
1581- match & self . inner {
1578+ match & * self . inner {
15821579 #[ cfg( feature = "sqlite" ) ]
15831580 DatabaseImpl :: Sqlite ( inner) => inner. sea_query_column_type_for ( column_type) ,
15841581 #[ cfg( feature = "postgres" ) ]
@@ -1735,45 +1732,6 @@ impl DatabaseBackend for Database {
17351732 }
17361733}
17371734
1738- #[ async_trait]
1739- impl DatabaseBackend for std:: sync:: Arc < Database > {
1740- async fn insert_or_update < T : Model > ( & self , data : & mut T ) -> Result < ( ) > {
1741- Database :: insert_or_update ( self , data) . await
1742- }
1743-
1744- async fn insert < T : Model > ( & self , data : & mut T ) -> Result < ( ) > {
1745- Database :: insert ( self , data) . await
1746- }
1747-
1748- async fn update < T : Model > ( & self , data : & mut T ) -> Result < ( ) > {
1749- Database :: update ( self , data) . await
1750- }
1751-
1752- async fn bulk_insert < T : Model > ( & self , data : & mut [ T ] ) -> Result < ( ) > {
1753- Database :: bulk_insert ( self , data) . await
1754- }
1755-
1756- async fn bulk_insert_or_update < T : Model > ( & self , data : & mut [ T ] ) -> Result < ( ) > {
1757- Database :: bulk_insert_or_update ( self , data) . await
1758- }
1759-
1760- async fn query < T : Model > ( & self , query : & Query < T > ) -> Result < Vec < T > > {
1761- Database :: query ( self , query) . await
1762- }
1763-
1764- async fn get < T : Model > ( & self , query : & Query < T > ) -> Result < Option < T > > {
1765- Database :: get ( self , query) . await
1766- }
1767-
1768- async fn exists < T : Model > ( & self , query : & Query < T > ) -> Result < bool > {
1769- Database :: exists ( self , query) . await
1770- }
1771-
1772- async fn delete < T : Model > ( & self , query : & Query < T > ) -> Result < StatementResult > {
1773- Database :: delete ( self , query) . await
1774- }
1775- }
1776-
17771735/// Result of a statement execution.
17781736#[ derive( Debug , Copy , Clone , PartialEq , Eq , PartialOrd , Ord , Hash ) ]
17791737pub struct StatementResult {
0 commit comments