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