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.EntityFieldNormal;
13 
14 import hunt.entity;
15 
16 import hunt.logging;
17 
18 import std.conv;
19 import std.math;
20 import std.variant;
21 
22 class EntityFieldNormal(T) : EntityFieldInfo {
23 
24     public this(EntityManager manager ,string fieldName, string columnName, string tableName, T value) {
25         super(fieldName, columnName, tableName);
26 
27         _columnFieldData = Variant(value);
28         _typeInfo = typeid(T);
29 
30         // _columnFieldData = new ColumnFieldData();
31         // _columnFieldData.valueType = typeof(value).stringof;
32         // _columnFieldData.value = new hunt.Nullable.Nullable!(T)(value);
33         // static if (isSomeString!T) {
34         //     if( manager !is null)
35         //         _columnFieldData.value = /*manager.getDatabase().escapeLiteral*/(value);
36         //     else
37         //         _columnFieldData.value = value;
38         // }
39         // else static if (is(T == double)) {
40         //     if (isNaN(value))
41         //         _columnFieldData.value = "0";
42         //     else 
43         //         _columnFieldData.value = "%s".format(value);
44         // }
45         // else static if (is(T == bool)) {
46         //     if(manager.getDbOption().isPgsql())
47         //     {
48         //         _columnFieldData.value = value ? "'1'":"'0'";
49         //     }
50         //     else
51         //     {
52         //         _columnFieldData.value = value ? "1" : "0";
53         //     }
54         // }
55         // else {
56         //     _columnFieldData.value = "%s".format(value);
57         // }
58     }
59 
60     override bool isAggregateType() {
61         return false;
62     }
63 
64     R deSerialize(R)(string value, ref bool flag) {
65         if (value.length == 0) {
66             return R.init;
67         }
68         if (value.length == 1 && cast(byte)(value[0]) == 0) {
69             return R.init;
70         }
71 
72         R r;
73         static if (is(R==bool)) {
74             if( value[0] == 1 || value[0] == 't')
75                 r = true;
76             else 
77                 r = false;
78             flag = true;
79         } else {
80             try {
81                 r = to!R(value);
82                 flag = true;
83             } catch(Exception ex) {
84                 warning(ex.msg);
85                 version(HUNT_DEBUG) {
86                     warning(ex);
87                 }
88             }
89         }
90 
91         return r;
92     }
93 
94 }