1 /**
2 * Copyright © DiamondMVC 2018
3 * License: MIT (https://github.com/DiamondMVC/Diamond/blob/master/LICENSE)
4 * Author: Jacob Jensen (bausshf)
5 */
6 module diamond.data.mapping.engines.mysql.generators.readgenerator;
7 
8 import std..string : format;
9 import std.traits : hasUDA, FieldNameTuple;
10 
11 import diamond.core.traits;
12 import diamond.data.mapping.engines.mysql.model : IMySqlModel;
13 
14 package(diamond.data):
15 /**
16 * Generates the read function for a database model.
17 * Returns:
18 *   The read function string to use with mixin.
19 */
20 string generateRead(T : IMySqlModel)()
21 {
22   string s = q{
23     {
24       %s
25     }
26   };
27 
28   mixin HandleFields!(T, q{{
29     enum hasNoMap = hasUDA!({{fullName}}, DbNoMap);
30 
31     static if (!hasNoMap)
32     {
33       enum hasNull = hasUDA!({{fullName}}, DbNull);
34       enum hasEnum = hasUDA!({{fullName}}, DbEnum);
35       enum typeName = typeof({{fullName}}).stringof;
36 
37       static if (hasNull && hasEnum)
38       {
39         mixin(readNullEnumFomat.format("{{fieldName}}", typeName));
40       }
41       else static if (hasNull)
42       {
43         mixin(readNullFomat.format("{{fieldName}}", typeName));
44       }
45       else static if (hasEnum)
46       {
47         mixin(readEnumFomat.format("{{fieldName}}", typeName));
48       }
49       else static if (is(typeof({{fullName}}) == bool))
50       {
51         mixin(readBoolFormat.format("{{fieldName}}", typeName));
52       }
53       else
54       {
55         mixin(readFomat.format("{{fieldName}}", typeName));
56       }
57     }
58   }});
59 
60   return s.format(handleThem());
61 }