1 /*
2  * Entity - Entity is an object-relational mapping tool for the D programming language. Referring to the design idea of JPA.
3  *
4  * Copyright (C) 2015-2018  Shanghai Putao Technology Co., Ltd
5  *
6  * Developer: HuntLabs.cn
7  *
8  * Licensed under the Apache-2.0 License.
9  *
10  */
11  
12 module hunt.entity.eql.ResultDes;
13 
14 import hunt.entity.eql.Common;
15 import hunt.entity;
16 import hunt.entity.EntityDeserializer;
17 
18 
19 import hunt.logging;
20 
21 import hunt.util.ConverterUtils;
22 import hunt.util.Traits;
23 
24 import std.array;
25 import std.conv;
26 import std.string;
27 import std.traits;
28 
29 
30 string getTableName(T)() if(is(T == class) || is(T == struct) ) {
31     static if (hasUDA!(T,Table)) {
32         return getUDAs!(getSymbolsByUDA!(T,Table)[0], Table)[0].name;
33     } else {
34         return T.stringof;
35     }
36 }
37 
38 
39 class ResultDes(T : Object) {
40     
41     private string _tableName;
42     private string _tableNameInLower; // for PostgreSQL, the column's name will be converted to lowercase.
43     private string _tablePrefix;
44     private string _clsName;
45     private EntityManager _manager;
46 
47     private EntityFieldInfo[string] _fields;
48 
49     this(EntityManager em)
50     {
51         _manager = em;
52         if(em !is null)
53             _tablePrefix = em.getPrefix();
54         initEntityData();
55     }
56 
57     public void setFields(EntityFieldInfo[string] fields)
58     {
59         _fields = fields;
60         // tracef("T: %s, fields: %s", T.stringof, _fields);
61     }
62 
63 
64     // pragma(msg, "T = "~T.stringof);
65     // pragma(msg, makeDeserializer!(T));
66     // pragma(msg,makeInitEntityData!(T));
67 
68 
69     mixin(makeImport!(T)());
70     mixin(makeInitEntityData!(T)());
71     mixin(makeDeserializer!(T)());
72 
73     EntityFieldInfo opDispatch(string name)() 
74     {
75         EntityFieldInfo info = _fields.get(name,null);
76         if (info is null) {
77             version(HUNT_DEBUG) tracef("T: %s, fields: %s", T.stringof, _fields);
78             throw new EntityException("Cannot find entityfieldinfo by name : " ~ name);
79         }
80         return info;
81     }    
82 
83     string getTableName()
84     {
85         return _tableName;
86     }
87 
88     string formatSelectItem(string col)
89     {
90         return _tableName ~ "__as__" ~ col;
91     }
92 
93     private string getColumnAsName(string name) {
94         return EntityExpression.getColumnAsName(name, _tableName);
95     }
96 
97     private string getColumnAsName(string name, string tableName) {
98         return EntityExpression.getColumnAsName(name, tableName);
99     }    
100 
101     private string getCountAsName() {
102         if(_manager.getDbOption().isPgsql()) {
103             return EntityExpression.getCountAsName(_tableNameInLower);
104         } else {
105             return EntityExpression.getCountAsName(_tableName);
106         }
107     }    
108 
109     public R deSerialize(R)(string value) {
110         version(HUNT_ENTITY_DEBUG) {
111             tracef("type=%s, value=%s", R.stringof, value);
112         }
113 
114         if (value.length == 0) {
115             return R.init;
116         }
117         if (value.length == 1 && cast(byte)(value[0]) == 0) {
118             return R.init;
119         }
120 
121         R r = R.init;
122         static if (is(R==bool)) {
123             if( value[0] == 1 || value[0] == 't')
124                 r = true;
125             else 
126                 r = false;
127         }
128         else {
129             r = to!R(value);
130         }
131 
132         return r;
133     }
134 }
135 
136 
137 string makeInitEntityData(T)() {
138     string str = `
139     private void initEntityData() {
140         import std.string;
141         _clsName = "`~T.stringof~`";`;
142     // static if (hasUDA!(T,Table)) {
143     //     str ~= `
144     //     _tableName = _tablePrefix ~ "` ~ getUDAs!(getSymbolsByUDA!(T,Table)[0], Table)[0].name ~`";`;
145     // }
146     // else {
147     //     str ~= `
148     //     _tableName = _tablePrefix ~ "` ~ T.stringof ~ `";`;
149     // }
150 
151     str ~= `
152     _tableName = _tablePrefix ~ "` ~ getTableName!(T) ~ `";`;
153     
154 
155     str ~= `
156         _tableNameInLower = _tableName.toLower();
157         }
158     `;
159     return str;
160 }
161