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.EntityFieldManyToOne;
13 
14 import hunt.entity;
15 import hunt.Nullable;
16 import hunt.logging;
17 
18 import std.variant;
19 
20 class EntityFieldManyToOne(T : Object) : EntityFieldObject!(T,T) {
21 
22     private ManyToOne _mode;
23     private string _findSqlStr;
24 
25     this(EntityManager manager, string fieldName, string columnName, string tableName, T fieldValue, ManyToOne mode) {
26         super(manager, fieldName, columnName, tableName, fieldValue, null);
27         _mode = mode;      
28         _enableJoin = _mode.fetch == FetchType.EAGER;    
29         _joinColumn = columnName;
30         // _columnFieldData = new ColumnFieldData();
31         // _columnFieldData.valueType = typeof(_entityInfo.getPrimaryValue()).stringof;
32 
33         // _columnFieldData.value = new hunt.Nullable.Nullable!(typeof(_entityInfo.getPrimaryValue()))(_entityInfo.getPrimaryValue());
34 
35         // FIXME: Needing refactor or cleanup -@zhangxueping at 2020-08-21T17:10:30+08:00
36         // Why not set it to a object
37         _columnFieldData = Variant(_entityInfo.getPrimaryValue());
38 
39         initJoinData(tableName, columnName);
40     }
41 
42     private void initJoinData(string tableName, string joinColumn) {
43         _foreignKeyData = new ForeignKeyData();
44         _foreignKeyData.columnName = joinColumn;
45         _foreignKeyData.tableName = _entityInfo.getTableName();
46         _foreignKeyData.primaryKey = _entityInfo.getPrimaryKeyString();
47         
48         _joinSqlData = new JoinSqlBuild(); 
49         _joinSqlData.tableName = _entityInfo.getTableName();
50         _joinSqlData.joinWhere = tableName ~ "." ~ joinColumn ~ " = " ~ _entityInfo.getTableName() ~ "." ~ _entityInfo.getPrimaryKeyString();
51         _joinSqlData.joinType = JoinType.LEFT;
52         foreach(value; _entityInfo.getFields()) {
53             _joinSqlData.columnNames ~= value.getSelectColumn();
54         }
55     }
56 
57 
58     public T deSerialize(Row row) {
59         if (_mode.fetch == FetchType.LAZY)
60             return T.init;
61         long count = -1;
62         return _entityInfo.deSerialize([row], count, 0, null, true);        
63     }
64 
65     public void setMode(ManyToOne mode) {
66         _mode = mode;
67         _enableJoin = _mode.fetch == FetchType.EAGER;    
68     }
69     
70     override FetchType fetchType() {
71         return _mode.fetch;
72     }
73 
74     override LazyData getLazyData(Row row) {
75         string name = EntityExpression.getColumnAsName(_joinColumn, getTableName());
76         Variant v = row.getValue(name);
77         if(!v.hasValue()) {
78             warningf("Can't find value for %s", name);
79             return null;
80         }
81         
82         string value = v.toString();
83         version(HUNT_DEBUG) tracef("A column: %s = %s", name, value);
84         LazyData ret = new LazyData(_entityInfo.getPrimaryKeyString(), value);
85         return ret;        
86     }
87 
88 }