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.criteria.CriteriaBase; 13 14 import hunt.entity; 15 import hunt.logging; 16 17 class CriteriaBase(T : Object, F : Object = T) 18 { 19 20 protected Root!(T,F) _root; 21 protected CriteriaBuilder _criteriaBuilder; 22 protected QueryBuilder _sqlBuidler; 23 24 25 this(CriteriaBuilder criteriaBuilder) { 26 _criteriaBuilder = criteriaBuilder; 27 _sqlBuidler = criteriaBuilder.createQueryBuilder(); 28 } 29 30 Root!(T,F) from(T t = null, F owner = null) { 31 _root = new Root!(T,F)(_criteriaBuilder, t is null ? null : Common.sampleCopy(t), owner); 32 // logDebug("Form table : ",_root.getTableName()); 33 _sqlBuidler.from(_root.getTableName()); 34 _root.autoJoin(); 35 return _root; 36 } 37 38 Root!(T,F) manyToManyFrom(T t = null, F owner = null ,string mapped = string.init) { 39 _root = new Root!(T,F)(_criteriaBuilder, t is null ? null : Common.sampleCopy(t), owner); 40 auto entityInfo = _root.getEntityInfo(); 41 auto filedInfo = entityInfo.getSingleField(mapped); 42 if(filedInfo !is null) 43 filedInfo.setEnableJoin(true); 44 _sqlBuidler.from(_root.getTableName()); 45 _root.autoJoin(); 46 return _root; 47 } 48 49 Root!(T,F) getRoot() {return _root;} 50 51 CriteriaBase!(T,F) where(P...)(P predicates) { 52 string s = " "; 53 foreach(k, v; predicates) { 54 s ~= v.toString(); 55 if (k != predicates.length-1) 56 s ~= " AND "; 57 } 58 _sqlBuidler.where(s); 59 return this; 60 } 61 62 CriteriaBase!(T,F) where(R)(Comparison!R cond) { 63 _sqlBuidler.where(cond); 64 return this; 65 } 66 67 override string toString() { 68 return _sqlBuidler.toString(); 69 } 70 71 void setEnableJoin(bool flg) 72 { 73 _root.setEnableJoin(flg); 74 } 75 }