@@ -24,23 +24,6 @@ mod runtime;
24
24
25
25
use query_macros:: * ;
26
26
27
- #[ cfg( feature = "runtime-tokio" ) ]
28
- lazy_static:: lazy_static! {
29
- static ref BASIC_RUNTIME : tokio:: runtime:: Runtime = {
30
- tokio:: runtime:: Builder :: new( )
31
- . threaded_scheduler( )
32
- . enable_io( )
33
- . enable_time( )
34
- . build( )
35
- . expect( "failed to build tokio runtime" )
36
- } ;
37
- }
38
-
39
- #[ cfg( feature = "runtime-tokio" ) ]
40
- fn block_on < F : std:: future:: Future > ( future : F ) -> F :: Output {
41
- BASIC_RUNTIME . enter ( || futures:: executor:: block_on ( future) )
42
- }
43
-
44
27
fn macro_result ( tokens : proc_macro2:: TokenStream ) -> TokenStream {
45
28
quote ! (
46
29
macro_rules! macro_result {
@@ -50,117 +33,21 @@ fn macro_result(tokens: proc_macro2::TokenStream) -> TokenStream {
50
33
. into ( )
51
34
}
52
35
53
- macro_rules! async_macro (
54
- ( $db: ident, $input: ident: $ty: ty => $expr: expr) => { {
55
- let $input = match syn:: parse:: <$ty>( $input) {
56
- Ok ( input) => input,
57
- Err ( e) => return macro_result( e. to_compile_error( ) ) ,
58
- } ;
59
-
60
- let res: Result <proc_macro2:: TokenStream > = block_on( async {
61
- use sqlx:: connection:: Connect ;
62
-
63
- let db_url = Url :: parse( & dotenv:: var( "DATABASE_URL" ) . map_err( |_| "DATABASE_URL not set" ) ?) ?;
64
-
65
- match db_url. scheme( ) {
66
- #[ cfg( feature = "sqlite" ) ]
67
- "sqlite" => {
68
- let $db = sqlx:: sqlite:: SqliteConnection :: connect( db_url. as_str( ) )
69
- . await
70
- . map_err( |e| format!( "failed to connect to database: {}" , e) ) ?;
71
-
72
- $expr. await
73
- }
74
- #[ cfg( not( feature = "sqlite" ) ) ]
75
- "sqlite" => Err ( format!(
76
- "DATABASE_URL {} has the scheme of a SQLite database but the `sqlite` \
77
- feature of sqlx was not enabled",
78
- db_url
79
- ) . into( ) ) ,
80
- #[ cfg( feature = "postgres" ) ]
81
- "postgresql" | "postgres" => {
82
- let $db = sqlx:: postgres:: PgConnection :: connect( db_url. as_str( ) )
83
- . await
84
- . map_err( |e| format!( "failed to connect to database: {}" , e) ) ?;
85
-
86
- $expr. await
87
- }
88
- #[ cfg( not( feature = "postgres" ) ) ]
89
- "postgresql" | "postgres" => Err ( format!(
90
- "DATABASE_URL {} has the scheme of a Postgres database but the `postgres` \
91
- feature of sqlx was not enabled",
92
- db_url
93
- ) . into( ) ) ,
94
- #[ cfg( feature = "mysql" ) ]
95
- "mysql" | "mariadb" => {
96
- let $db = sqlx:: mysql:: MySqlConnection :: connect( db_url. as_str( ) )
97
- . await
98
- . map_err( |e| format!( "failed to connect to database: {}" , e) ) ?;
99
-
100
- $expr. await
101
- }
102
- #[ cfg( not( feature = "mysql" ) ) ]
103
- "mysql" | "mariadb" => Err ( format!(
104
- "DATABASE_URL {} has the scheme of a MySQL/MariaDB database but the `mysql` \
105
- feature of sqlx was not enabled",
106
- db_url
107
- ) . into( ) ) ,
108
- scheme => Err ( format!( "unexpected scheme {:?} in DATABASE_URL {}" , scheme, db_url) . into( ) ) ,
109
- }
110
- } ) ;
111
-
112
- match res {
113
- Ok ( ts) => ts. into( ) ,
114
- Err ( e) => {
115
- if let Some ( parse_err) = e. downcast_ref:: <syn:: Error >( ) {
116
- macro_result( parse_err. to_compile_error( ) )
117
- } else {
118
- let msg = e. to_string( ) ;
119
- macro_result( quote!( compile_error!( #msg) ) )
120
- }
121
- }
122
- }
123
- } }
124
- ) ;
125
-
126
- #[ proc_macro]
127
- #[ allow( unused_variables) ]
128
- pub fn query ( input : TokenStream ) -> TokenStream {
129
- #[ allow( unused_variables) ]
130
- async_macro ! ( db, input: QueryMacroInput => expand_query( input, db) )
131
- }
132
-
133
- #[ proc_macro]
134
- #[ allow( unused_variables) ]
135
- pub fn query_file ( input : TokenStream ) -> TokenStream {
136
- #[ allow( unused_variables) ]
137
- async_macro ! ( db, input: QueryMacroInput => expand_query_file( input, db) )
138
- }
139
-
140
36
#[ proc_macro]
141
- #[ allow( unused_variables) ]
142
- pub fn query_as ( input : TokenStream ) -> TokenStream {
143
- #[ allow( unused_variables) ]
144
- async_macro ! ( db, input: QueryAsMacroInput => expand_query_as( input, db, true ) )
145
- }
37
+ pub fn expand_query ( input : TokenStream ) -> TokenStream {
38
+ let input = syn:: parse_macro_input!( input as QueryMacroInput ) ;
146
39
147
- #[ proc_macro]
148
- #[ allow( unused_variables) ]
149
- pub fn query_file_as ( input : TokenStream ) -> TokenStream {
150
- async_macro ! ( db, input: QueryAsMacroInput => expand_query_file_as( input, db, true ) )
151
- }
152
-
153
- #[ proc_macro]
154
- #[ allow( unused_variables) ]
155
- pub fn query_as_unchecked ( input : TokenStream ) -> TokenStream {
156
- #[ allow( unused_variables) ]
157
- async_macro ! ( db, input: QueryAsMacroInput => expand_query_as( input, db, false ) )
158
- }
159
-
160
- #[ proc_macro]
161
- #[ allow( unused_variables) ]
162
- pub fn query_file_as_unchecked ( input : TokenStream ) -> TokenStream {
163
- async_macro ! ( db, input: QueryAsMacroInput => expand_query_file_as( input, db, false ) )
40
+ match query_macros:: expand_input ( input) {
41
+ Ok ( ts) => ts. into ( ) ,
42
+ Err ( e) => {
43
+ if let Some ( parse_err) = e. downcast_ref :: < syn:: Error > ( ) {
44
+ macro_result ( parse_err. to_compile_error ( ) )
45
+ } else {
46
+ let msg = e. to_string ( ) ;
47
+ macro_result ( quote ! ( compile_error!( #msg) ) )
48
+ }
49
+ }
50
+ }
164
51
}
165
52
166
53
#[ proc_macro_derive( Encode , attributes( sqlx) ) ]
0 commit comments