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