@@ -7,41 +7,45 @@ use crate::mysql::query::{
77 ColumnQueryResult , ForeignKeyQueryResult , IndexQueryResult , SchemaQueryBuilder ,
88 TableQueryResult , VersionQueryResult ,
99} ;
10- use crate :: sqlx_types:: SqlxError ;
11- use futures:: future;
10+ use crate :: {
11+ Connection ,
12+ sqlx_types:: { MySqlPool , SqlxError } ,
13+ } ;
1214use sea_query:: { Alias , DynIden , IntoIden , SeaRc } ;
1315
1416mod executor;
1517pub use executor:: * ;
1618
17- pub struct SchemaDiscovery {
19+ pub struct SchemaDiscovery < C : Connection > {
1820 pub query : SchemaQueryBuilder ,
19- pub executor : Executor ,
2021 pub schema : DynIden ,
22+ conn : C ,
23+ }
24+
25+ impl SchemaDiscovery < Executor > {
26+ /// Discover schema from a SQLx pool
27+ pub fn new ( pool : MySqlPool , schema : & str ) -> Self {
28+ Self :: conn ( pool. into_executor ( ) , schema)
29+ }
2130}
2231
23- impl SchemaDiscovery {
24- pub fn new < E > ( executor : E , schema : & str ) -> Self
25- where
26- E : IntoExecutor ,
27- {
28- Self {
32+ impl < C : Connection > SchemaDiscovery < C > {
33+ /// Discover schema from a generic SQLx connection
34+ pub fn conn ( conn : C , schema : & str ) -> Self {
35+ SchemaDiscovery {
2936 query : SchemaQueryBuilder :: default ( ) ,
30- executor : executor. into_executor ( ) ,
3137 schema : Alias :: new ( schema) . into_iden ( ) ,
38+ conn,
3239 }
3340 }
3441
3542 pub async fn discover ( mut self ) -> Result < Schema , SqlxError > {
3643 self . query = SchemaQueryBuilder :: new ( self . discover_system ( ) . await ?) ;
37- let tables = self . discover_tables ( ) . await ?;
38- let tables = future:: try_join_all (
39- tables
40- . into_iter ( )
41- . map ( |t| ( & self , t) )
42- . map ( Self :: discover_table_static) ,
43- )
44- . await ?;
44+ let mut tables = Vec :: new ( ) ;
45+
46+ for table in self . discover_tables ( ) . await ? {
47+ tables. push ( self . discover_table ( table) . await ?) ;
48+ }
4549
4650 Ok ( Schema {
4751 schema : self . schema . to_string ( ) ,
@@ -51,10 +55,10 @@ impl SchemaDiscovery {
5155 }
5256
5357 pub async fn discover_system ( & mut self ) -> Result < SystemInfo , SqlxError > {
54- let rows = self . executor . fetch_all ( self . query . query_version ( ) ) . await ?;
58+ let rows = self . conn . query_all ( self . query . query_version ( ) ) . await ?;
5559
5660 #[ allow( clippy:: never_loop) ]
57- for row in rows. iter ( ) {
61+ for row in rows {
5862 let result: VersionQueryResult = row. into ( ) ;
5963 debug_print ! ( "{:?}" , result) ;
6064 let version = result. parse ( ) ;
@@ -66,12 +70,12 @@ impl SchemaDiscovery {
6670
6771 pub async fn discover_tables ( & mut self ) -> Result < Vec < TableInfo > , SqlxError > {
6872 let rows = self
69- . executor
70- . fetch_all ( self . query . query_tables ( self . schema . clone ( ) ) )
73+ . conn
74+ . query_all ( self . query . query_tables ( self . schema . clone ( ) ) )
7175 . await ?;
7276
7377 let tables: Vec < TableInfo > = rows
74- . iter ( )
78+ . into_iter ( )
7579 . map ( |row| {
7680 let result: TableQueryResult = row. into ( ) ;
7781 debug_print ! ( "{:?}" , result) ;
@@ -84,12 +88,6 @@ impl SchemaDiscovery {
8488 Ok ( tables)
8589 }
8690
87- async fn discover_table_static ( params : ( & Self , TableInfo ) ) -> Result < TableDef , SqlxError > {
88- let this = params. 0 ;
89- let info = params. 1 ;
90- Self :: discover_table ( this, info) . await
91- }
92-
9391 pub async fn discover_table ( & self , info : TableInfo ) -> Result < TableDef , SqlxError > {
9492 let table = SeaRc :: new ( Alias :: new ( info. name . as_str ( ) ) ) ;
9593 let columns = self
@@ -117,12 +115,12 @@ impl SchemaDiscovery {
117115 system : & SystemInfo ,
118116 ) -> Result < Vec < ColumnInfo > , SqlxError > {
119117 let rows = self
120- . executor
121- . fetch_all ( self . query . query_columns ( schema. clone ( ) , table. clone ( ) ) )
118+ . conn
119+ . query_all ( self . query . query_columns ( schema. clone ( ) , table. clone ( ) ) )
122120 . await ?;
123121
124122 let columns = rows
125- . iter ( )
123+ . into_iter ( )
126124 . map ( |row| {
127125 let result: ColumnQueryResult = row. into ( ) ;
128126 debug_print ! ( "{:?}" , result) ;
@@ -141,12 +139,12 @@ impl SchemaDiscovery {
141139 table : DynIden ,
142140 ) -> Result < Vec < IndexInfo > , SqlxError > {
143141 let rows = self
144- . executor
145- . fetch_all ( self . query . query_indexes ( schema. clone ( ) , table. clone ( ) ) )
142+ . conn
143+ . query_all ( self . query . query_indexes ( schema. clone ( ) , table. clone ( ) ) )
146144 . await ?;
147145
148146 let results = rows. into_iter ( ) . map ( |row| {
149- let result: IndexQueryResult = ( & row) . into ( ) ;
147+ let result: IndexQueryResult = row. into ( ) ;
150148 debug_print ! ( "{:?}" , result) ;
151149 result
152150 } ) ;
@@ -164,12 +162,12 @@ impl SchemaDiscovery {
164162 table : DynIden ,
165163 ) -> Result < Vec < ForeignKeyInfo > , SqlxError > {
166164 let rows = self
167- . executor
168- . fetch_all ( self . query . query_foreign_key ( schema. clone ( ) , table. clone ( ) ) )
165+ . conn
166+ . query_all ( self . query . query_foreign_key ( schema. clone ( ) , table. clone ( ) ) )
169167 . await ?;
170168
171169 let results = rows. into_iter ( ) . map ( |row| {
172- let result: ForeignKeyQueryResult = ( & row) . into ( ) ;
170+ let result: ForeignKeyQueryResult = row. into ( ) ;
173171 debug_print ! ( "{:?}" , result) ;
174172 result
175173 } ) ;
0 commit comments