1 module hunt.database.query.Expression; 2 3 import hunt.database.query.Common; 4 5 class Expression 6 { 7 string value; 8 } 9 10 class WhereExpression : Expression 11 { 12 string key; 13 string op; 14 string value; 15 this(string key , string op, string value) 16 { 17 this.key = key; 18 this.op = op; 19 this.value = value; 20 } 21 string formatKey(string str) 22 { 23 return str; 24 } 25 string formatValue(string str) 26 { 27 if(str == null)return "null"; 28 return str; 29 } 30 override string toString() 31 { 32 return formatKey(key) ~ " " ~ op ~ " "~ formatValue(value); 33 } 34 } 35 36 class ValueExpression : Expression 37 { 38 string key; 39 string value; 40 this(string key , string value) 41 { 42 this.key = key; 43 this.value = value; 44 } 45 override string toString() 46 { 47 return key ~ " = " ~ value ; 48 } 49 } 50 51 class JoinExpression : Expression 52 { 53 JoinMethod _join; 54 string _table; 55 string _tableAlias; 56 string _on; 57 this(JoinMethod join,string table,string tableAlias,string on) 58 { 59 _join = join; 60 _table = table; 61 _tableAlias = tableAlias; 62 _on = on; 63 } 64 override string toString() 65 { 66 string str = " " ~ _join ~ " " ~ _table ~ " " ~ _tableAlias ~ " "; 67 if(_join != JoinMethod.CrossJoin) str ~= " ON " ~ _on ~ " "; 68 return str; 69 } 70 } 71 72 class MultiWhereExpression : Expression 73 { 74 Relation _relation; 75 MultiWhereExpression[] childs; 76 WhereExpression expr; 77 override string toString() 78 { 79 if(childs.length){ 80 auto len = childs.length; 81 int i = 0; 82 string str; 83 foreach(child;childs) 84 { 85 str ~= child.toString; 86 if( i < len-1 )str ~= (_relation == Relation.And ? " AND " : " OR "); 87 i++; 88 } 89 return "(" ~ str ~ ")"; 90 }else{ 91 return "(" ~ expr.toString ~ ")"; 92 } 93 } 94 MultiWhereExpression eq(string key,string value) 95 { 96 if(value == null) 97 expr = new WhereExpression(key,"is",null); 98 else 99 expr = new WhereExpression(key,"=",value); 100 return this; 101 } 102 MultiWhereExpression ne(string key,string value) 103 { 104 if(value == null) 105 expr = new WhereExpression(key,"is not",null); 106 else 107 expr = new WhereExpression(key,"!=",value); 108 return this; 109 } 110 MultiWhereExpression gt(string key,string value) 111 { 112 expr = new WhereExpression(key,">",value); 113 return this; 114 } 115 MultiWhereExpression lt(string key,string value) 116 { 117 expr = new WhereExpression(key,"<",value); 118 return this; 119 } 120 MultiWhereExpression ge(string key,string value) 121 { 122 expr = new WhereExpression(key,">=",value); 123 return this; 124 } 125 MultiWhereExpression le(string key,string value) 126 { 127 expr = new WhereExpression(key,"<=",value); 128 return this; 129 } 130 MultiWhereExpression like(string key,string value) 131 { 132 expr = new WhereExpression(key,"like",value); 133 return this; 134 } 135 MultiWhereExpression andX(T...)(T args) 136 { 137 _relation = Relation.And; 138 foreach(v;args) 139 { 140 childs ~= v; 141 } 142 return this; 143 } 144 MultiWhereExpression orX(T...)(T args) 145 { 146 _relation = Relation.Or; 147 foreach(v;args) 148 { 149 childs ~= v; 150 } 151 return this; 152 } 153 }