@@ -66,13 +66,7 @@ struct Field {
66
66
impl Field {
67
67
fn walk_attrs ( & self , callback : & mut FnMut ( & Ident ) ) {
68
68
if let Some ( ref field_attrs) = self . attr {
69
- field_attrs. iter ( ) . for_each ( |a| {
70
- let Attribute { path, .. } = a;
71
- let syn:: Path { segments, .. } = path;
72
- let syn:: PathSegment { ident, .. } = & segments[ 0 ] ;
73
-
74
- callback ( ident) ;
75
- } )
69
+ field_attrs. iter ( ) . for_each ( |a| callback ( & a. path . segments [ 0 ] . ident ) )
76
70
}
77
71
}
78
72
@@ -191,6 +185,32 @@ struct Parsed {
191
185
}
192
186
193
187
impl Parsed {
188
+ fn db_connection ( & self ) -> proc_macro2:: TokenStream {
189
+ let mut connection = quote ! ( & self . connection( ) ) ;
190
+ let mut var: Option < & Ident > = None ;
191
+ let mut url: Option < proc_macro2:: Literal > = None ;
192
+
193
+ self . input . attrs . iter ( ) . for_each ( |a| {
194
+ let ident = & a. path . segments [ 0 ] . ident ;
195
+ if ident == "DBVAR" {
196
+ var = Some ( ident) ;
197
+ }
198
+ & a. tts . clone ( ) . into_iter ( ) . for_each ( |token| {
199
+ if let proc_macro2:: TokenTree :: Literal ( lit) = token {
200
+ url = Some ( lit) ;
201
+ }
202
+ } ) ;
203
+ } ) ;
204
+
205
+ if let Some ( env_var) = var {
206
+ if let Some ( db_url) = url {
207
+ connection = quote ! ( & self . connection_string( #db_url) ) ;
208
+ }
209
+ }
210
+
211
+ connection
212
+ }
213
+
194
214
fn model_with_id_fields ( & self ) -> Vec < proc_macro2:: TokenStream > {
195
215
let mut fields = Vec :: new ( ) ;
196
216
fields. push ( quote ! ( pub id: i32 ) ) ;
@@ -339,6 +359,7 @@ impl Parsed {
339
359
let controller = & self . input . ident . append ( "Controller" ) ;
340
360
let table = self . attr . schema . table ( ) ;
341
361
let sql_type = self . attr . schema . sql_type ( ) ;
362
+ let connection = self . db_connection ( ) ;
342
363
343
364
quote ! {
344
365
pub struct #controller;
@@ -365,30 +386,30 @@ impl Parsed {
365
386
fn create( & self , model: & Self :: Model ) -> Result <Self :: ModelWithId , Error > {
366
387
Ok ( insert_into( #table)
367
388
. values( model)
368
- . get_result( & self . connection( ) ) ?)
389
+ . get_result( # connection) ?)
369
390
}
370
391
371
392
fn get_one( & self , by: Expr <#table>) -> Result <Self :: ModelWithId , Error > {
372
393
Ok ( #table
373
394
. filter( by)
374
- . get_result:: <Self :: ModelWithId >( & self . connection( ) ) ?)
395
+ . get_result:: <Self :: ModelWithId >( # connection) ?)
375
396
}
376
397
377
398
fn get_all( & self , by: Expr <#table>) -> Result <Vec <Self :: ModelWithId >, Error > {
378
399
Ok ( #table
379
400
. filter( by)
380
- . get_results:: <Self :: ModelWithId >( & self . connection( ) ) ?)
401
+ . get_results:: <Self :: ModelWithId >( # connection) ?)
381
402
}
382
403
383
404
fn update( & self , model: & Self :: Model , by: Expr <#table>) -> Result <Self :: ModelWithId , Error > {
384
405
Ok ( update( #table)
385
406
. filter( by)
386
407
. set( model)
387
- . get_result:: <Self :: ModelWithId >( & self . connection( ) ) ?)
408
+ . get_result:: <Self :: ModelWithId >( # connection) ?)
388
409
}
389
410
390
411
fn delete( & self , by: Expr <#table>) -> Result <usize , Error > {
391
- Ok ( delete( #table) . filter( by) . execute( & self . connection( ) ) ?)
412
+ Ok ( delete( #table) . filter( by) . execute( # connection) ?)
392
413
}
393
414
}
394
415
}
0 commit comments