1 /* 2 * Copyright 2015-2018 HuntLabs.cn. 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 module hunt.sql.builder.impl.SQLDeleteBuilderImpl; 17 18 import hunt.collection; 19 20 import hunt.sql.SQLUtils; 21 import hunt.sql.ast.SQLExpr; 22 import hunt.sql.ast.SQLStatement; 23 import hunt.sql.ast.expr.SQLBinaryOperator; 24 import hunt.sql.ast.expr.SQLIdentifierExpr; 25 import hunt.sql.ast.statement.SQLDeleteStatement; 26 import hunt.sql.ast.statement.SQLExprTableSource; 27 import hunt.sql.builder.SQLDeleteBuilder; 28 import hunt.sql.dialect.mysql.ast.statement.MySqlDeleteStatement; 29 // import hunt.sql.dialect.oracle.ast.stmt.OracleDeleteStatement; 30 import hunt.sql.dialect.postgresql.ast.stmt.PGDeleteStatement; 31 import hunt.sql.util.DBType; 32 import hunt.sql.builder.SQLBuilder; 33 34 class SQLDeleteBuilderImpl : SQLDeleteBuilder { 35 36 private SQLDeleteStatement stmt; 37 private string dbType; 38 39 this(string dbType){ 40 this.dbType = dbType; 41 } 42 43 this(string sql, string dbType){ 44 List!SQLStatement stmtList = SQLUtils.parseStatements(sql, dbType); 45 46 if (stmtList.size() == 0) { 47 throw new Exception("not support empty-statement :" ~ sql); 48 } 49 50 if (stmtList.size() > 1) { 51 throw new Exception("not support multi-statement :" ~ sql); 52 } 53 54 SQLDeleteStatement stmt = cast(SQLDeleteStatement) stmtList.get(0); 55 this.stmt = stmt; 56 this.dbType = dbType; 57 } 58 59 this(SQLDeleteStatement stmt, string dbType){ 60 this.stmt = stmt; 61 this.dbType = dbType; 62 } 63 64 override 65 SQLBuilder limit(int rowCount) { 66 throw new Exception("not implement"); 67 } 68 69 override 70 SQLBuilder limit(int rowCount, int offset) { 71 throw new Exception("not implement"); 72 } 73 74 override 75 SQLBuilder from(string table) { 76 return from(table, null); 77 } 78 79 override 80 SQLBuilder from(string table, string _alias) { 81 SQLDeleteStatement _delete = getSQLDeleteStatement(); 82 SQLExprTableSource from = new SQLExprTableSource(new SQLIdentifierExpr(table), _alias); 83 _delete.setTableSource(from); 84 return this; 85 } 86 87 override 88 SQLBuilder where(string expr) { 89 SQLDeleteStatement _delete = getSQLDeleteStatement(); 90 91 SQLExpr exprObj = SQLUtils.toSQLExpr(expr, dbType); 92 _delete.setWhere(exprObj); 93 94 return this; 95 } 96 97 override 98 SQLBuilder whereAnd(string expr) { 99 SQLDeleteStatement _delete = getSQLDeleteStatement(); 100 101 SQLExpr exprObj = SQLUtils.toSQLExpr(expr, dbType); 102 SQLExpr newCondition = SQLUtils.buildCondition(SQLBinaryOperator.BooleanAnd, exprObj, false, _delete.getWhere()); 103 _delete.setWhere(newCondition); 104 105 return this; 106 } 107 108 override 109 SQLBuilder whereOr(string expr) { 110 SQLDeleteStatement _delete = getSQLDeleteStatement(); 111 112 SQLExpr exprObj = SQLUtils.toSQLExpr(expr, dbType); 113 SQLExpr newCondition = SQLUtils.buildCondition(SQLBinaryOperator.BooleanOr, exprObj, false, _delete.getWhere()); 114 _delete.setWhere(newCondition); 115 116 return this; 117 } 118 119 SQLDeleteStatement getSQLDeleteStatement() { 120 if (stmt is null) { 121 stmt = createSQLDeleteStatement(); 122 } 123 return stmt; 124 } 125 126 SQLDeleteStatement createSQLDeleteStatement() { 127 // if (DBType.ORACLE.name == dbType) { 128 // return new OracleDeleteStatement(); 129 // } 130 131 if (DBType.MYSQL.name == dbType) { 132 return new MySqlDeleteStatement(); 133 } 134 135 if (DBType.POSTGRESQL.name == dbType) { 136 return new PGDeleteStatement(); 137 } 138 139 return new SQLDeleteStatement(); 140 } 141 142 override string toString() { 143 return SQLUtils.toSQLString(stmt, dbType); 144 } 145 146 string toString(FormatOption option) { 147 return SQLUtils.toSQLString(stmt, dbType, option); 148 } 149 }