1 module dbal.driver.mysql.builder; 2 3 import dbal; 4 5 class MySqlBuilder : SqlBuilder 6 { 7 Method _method; 8 string _tableName; 9 string _tableNameAlias; 10 string[] _selectKeys = ["*"]; 11 string _having; 12 string _groupby; 13 string _orderByKey; 14 string _order; 15 int _offset; 16 int _limit; 17 string _multiWhereStr; 18 WhereExpression[] _whereKeys; 19 WhereExpression[] _whereKeysParameters; 20 ValueExpression[string] _values; 21 ValueExpression[] _valuesParameters; 22 JoinExpression[] _joins; 23 24 string _autoIncreaseKey; 25 26 SqlBuilder from(string tableName,string tableNameAlias = null) 27 { 28 _tableName = tableName; 29 _tableNameAlias = tableNameAlias.length ? tableNameAlias : tableName; 30 return this; 31 } 32 SqlBuilder selectImpl(string[] args) 33 { 34 _selectKeys = null; 35 _selectKeys = args; 36 _method = Method.Select; 37 return this; 38 } 39 SqlBuilder insert(string tableName) 40 { 41 _tableName = tableName; 42 _method = Method.Insert; 43 return this; 44 } 45 SqlBuilder update(string tableName) 46 { 47 _tableName = tableName; 48 _method = Method.Update; 49 return this; 50 } 51 SqlBuilder remove(string tableName) 52 { 53 _tableName = tableName; 54 _method = Method.Delete; 55 return this; 56 } 57 SqlBuilder where(string expression) 58 { 59 if(!expression.length)return this; 60 auto arr = split(strip(expression)," "); 61 if(arr.length != 3){ 62 _multiWhereStr ~= expression; 63 }else{ 64 auto expr = new WhereExpression(arr[0],arr[1],arr[2]); 65 _whereKeys ~= expr; 66 if(arr[2] == "?")_whereKeysParameters ~= expr; 67 } 68 return this; 69 } 70 SqlBuilder whereImpl(string key,CompareType type,string value) 71 { 72 _whereKeys ~= new WhereExpression(key,type,value); 73 return this; 74 } 75 SqlBuilder where(MultiWhereExpression expr) 76 { 77 _multiWhereStr ~= expr.toString; 78 return this; 79 } 80 SqlBuilder having(string expression) 81 { 82 _having = expression; 83 return this; 84 } 85 MultiWhereExpression expr() 86 { 87 return new MultiWhereExpression(); 88 } 89 SqlBuilder join(JoinMethod joinMethod,string table,string tablealias,string joinWhere) 90 { 91 _joins ~= new JoinExpression(joinMethod,table,tablealias,joinWhere); 92 return this; 93 } 94 SqlBuilder join(JoinMethod joinMethod,string table,string joinWhere) 95 { 96 return join(joinMethod,table,table,joinWhere); 97 } 98 SqlBuilder innerJoin(string table,string tablealias,string joinWhere) 99 { 100 return join(JoinMethod.InnerJoin,table,tablealias,joinWhere); 101 } 102 SqlBuilder innerJoin(string table,string joinWhere) 103 { 104 return innerJoin(table,table,joinWhere); 105 } 106 SqlBuilder leftJoin(string table,string tableAlias,string joinWhere) 107 { 108 return join(JoinMethod.LeftJoin,table,tableAlias,joinWhere); 109 } 110 SqlBuilder leftJoin(string table,string joinWhere) 111 { 112 return leftJoin(table,table,joinWhere); 113 } 114 SqlBuilder rightJoin(string table,string tableAlias,string joinWhere) 115 { 116 return join(JoinMethod.RightJoin,table,tableAlias,joinWhere); 117 } 118 SqlBuilder rightJoin(string table,string joinWhere) 119 { 120 return rightJoin(table,table,joinWhere); 121 } 122 SqlBuilder fullJoin(string table,string tableAlias,string joinWhere) 123 { 124 return join(JoinMethod.FullJoin,table,tableAlias,joinWhere); 125 } 126 SqlBuilder fullJoin(string table,string joinWhere) 127 { 128 return fullJoin(table,table,joinWhere); 129 } 130 SqlBuilder crossJoin(string table,string tableAlias) 131 { 132 return join(JoinMethod.CrossJoin,table,tableAlias,null); 133 } 134 SqlBuilder crossJoin(string table) 135 { 136 return crossJoin(table,table); 137 } 138 SqlBuilder groupBy(string expression) 139 { 140 _groupby = expression; 141 return this; 142 } 143 SqlBuilder orderBy (string key,string order = "DESC") 144 { 145 _orderByKey = key; 146 _order = order; 147 return this; 148 } 149 SqlBuilder offset(int offset) 150 { 151 _offset = offset; 152 return this; 153 } 154 SqlBuilder limit(int limit) 155 { 156 _limit = limit; 157 return this; 158 } 159 SqlBuilder values(string[string] arr) 160 { 161 foreach(key,value;arr){ 162 auto expr = new ValueExpression(key,value); 163 _values[key] = expr; 164 if(value == "?")_valuesParameters ~= expr; 165 } 166 return this; 167 } 168 SqlBuilder set(string key,string value) 169 { 170 auto expr = new ValueExpression(key,value); 171 _values[key] = expr; 172 if(value == "?")_valuesParameters ~= expr; 173 return this; 174 } 175 SqlBuilder setParameter(int index,string value) 176 { 177 if(_whereKeysParameters.length){ 178 if(index > _whereKeysParameters.length - 1) 179 throw new DbalException("query builder setParameter range valite"); 180 _whereKeysParameters[index].value = value; 181 }else{ 182 if(index > _valuesParameters.length - 1) 183 throw new DbalException("query builder setParameter range valite"); 184 _valuesParameters[index].value = value; 185 } 186 return this; 187 } 188 189 SqlBuilder setAutoIncrease(string key) 190 { 191 _autoIncreaseKey = key; 192 return this; 193 } 194 195 string getAutoIncrease() 196 { 197 return _autoIncreaseKey; 198 } 199 200 string tableName() 201 { 202 return this._tableName; 203 } 204 205 string tableNameAlias() 206 { 207 return this._tableNameAlias; 208 } 209 210 Method method() 211 { 212 return this._method; 213 } 214 215 string[] selectKeys() 216 { 217 return this._selectKeys; 218 } 219 220 string having() 221 { 222 return this._having; 223 } 224 225 string groupBy() 226 { 227 return this._groupby; 228 } 229 230 string orderBy() 231 { 232 return this._orderByKey; 233 } 234 235 string order() 236 { 237 return this._order; 238 } 239 240 int limit() 241 { 242 return this._limit; 243 } 244 245 int offset() 246 { 247 return this._offset; 248 } 249 250 string multiWhereStr() 251 { 252 return this._multiWhereStr; 253 } 254 255 WhereExpression[] whereKeys() 256 { 257 return this._whereKeys; 258 } 259 260 ValueExpression[string] values() 261 { 262 return this._values; 263 } 264 265 JoinExpression[] joins() 266 { 267 return this._joins; 268 } 269 270 MySqlSyntax build() 271 { 272 return new MySqlSyntax(this); 273 } 274 }