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