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.ast.statement.SQLInsertStatement; 17 18 19 import hunt.collection; 20 21 import hunt.sql.SQLUtils; 22 import hunt.sql.ast.SQLExpr; 23 import hunt.sql.ast.SQLObject; 24 import hunt.sql.ast.SQLObjectImpl; 25 import hunt.sql.ast.SQLStatement; 26 import hunt.sql.visitor.SQLASTVisitor; 27 import hunt.sql.ast.statement.SQLWithSubqueryClause; 28 import hunt.sql.ast.statement.SQLInsertInto; 29 import hunt.sql.ast.statement.SQLExprTableSource; 30 import hunt.sql.ast.statement.SQLSelect; 31 import hunt.sql.ast.SQLName; 32 import hunt.sql.ast.statement.SQLSelectQuery; 33 34 35 36 public class SQLInsertStatement : SQLInsertInto , SQLStatement { 37 38 alias cloneTo = SQLInsertInto.cloneTo; 39 40 protected SQLWithSubqueryClause _with; 41 42 protected string dbType; 43 44 protected bool upsert = false; // for phoenix 45 46 private bool afterSemi; 47 48 public this(){ 49 50 } 51 52 public void cloneTo(SQLInsertStatement x) { 53 super.cloneTo(x); 54 x.dbType = dbType; 55 x.upsert = upsert; 56 x.afterSemi = afterSemi; 57 58 if (_with !is null) { 59 x.setWith(_with.clone()); 60 } 61 } 62 63 override public SQLInsertStatement clone() { 64 SQLInsertStatement x = new SQLInsertStatement(); 65 cloneTo(x); 66 return x; 67 } 68 69 70 override protected void accept0(SQLASTVisitor visitor) { 71 if (visitor.visit(this)) { 72 this.acceptChild(visitor, tableSource); 73 this.acceptChild!SQLExpr(visitor, columns); 74 this.acceptChild!ValuesClause(visitor, valuesList); 75 this.acceptChild(visitor, query); 76 } 77 78 visitor.endVisit(this); 79 } 80 81 override 82 public List!SQLObject getChildren() { 83 List!SQLObject children = new ArrayList!SQLObject(); 84 85 children.add(tableSource); 86 children.addAll(cast(List!SQLObject)(this.columns)); 87 children.addAll(cast(List!SQLObject)(this.valuesList)); 88 if (query !is null) { 89 children.add(query); 90 } 91 92 return children; 93 } 94 95 public bool isUpsert() { 96 return upsert; 97 } 98 99 public void setUpsert(bool upsert) { 100 this.upsert = upsert; 101 } 102 103 // public static class ValuesClause : SQLObjectImpl { 104 105 // private List!SQLExpr values; 106 // private string originalString; 107 // private int replaceCount; 108 109 // public this(){ 110 // this(new ArrayList!SQLExpr()); 111 // } 112 113 // override public ValuesClause clone() { 114 // ValuesClause x = new ValuesClause(new ArrayList!SQLExpr(this.values.size())); 115 // foreach (SQLExpr v ; values) { 116 // x.addValue(v); 117 // } 118 // return x; 119 // } 120 121 // public this(List!SQLExpr values){ 122 // this.values = values; 123 // for (int i = 0; i < values.size(); ++i) { 124 // values.get(i).setParent(this); 125 // } 126 // } 127 128 // public void addValue(SQLExpr value) { 129 // value.setParent(this); 130 // values.add(value); 131 // } 132 133 // public List!SQLExpr getValues() { 134 // return values; 135 // } 136 137 // override public void output(StringBuilder buf) { 138 // buf.append(" VALUES ("); 139 // for (int i = 0, size = values.size(); i < size; ++i) { 140 // if (i != 0) { 141 // buf.append(", "); 142 // } 143 // values.get(i).output(buf); 144 // } 145 // buf.append(")"); 146 // } 147 148 149 // override protected void accept0(SQLASTVisitor visitor) { 150 // if (visitor.visit(this)) { 151 // this.acceptChild(visitor, values); 152 // } 153 154 // visitor.endVisit(this); 155 // } 156 157 // public string getOriginalString() { 158 // return originalString; 159 // } 160 161 // public void setOriginalString(string originalString) { 162 // this.originalString = originalString; 163 // } 164 165 // public int getReplaceCount() { 166 // return replaceCount; 167 // } 168 169 // public void incrementReplaceCount() { 170 // this.replaceCount++; 171 // } 172 // } 173 174 override 175 public string getDbType() { 176 return dbType; 177 } 178 179 public void setDbType(string dbType) { 180 this.dbType = dbType; 181 } 182 183 override 184 public bool isAfterSemi() { 185 return afterSemi; 186 } 187 188 override 189 public void setAfterSemi(bool afterSemi) { 190 this.afterSemi = afterSemi; 191 } 192 193 194 public SQLWithSubqueryClause getWith() { 195 return _with; 196 } 197 198 public void setWith(SQLWithSubqueryClause _with) { 199 if (_with !is null) { 200 _with.setParent(this); 201 } 202 this._with = _with; 203 } 204 205 override public string toString() { 206 return SQLUtils.toSQLString(this, dbType); 207 } 208 209 public string toLowerCaseString() { 210 return SQLUtils.toSQLString(this, dbType, SQLUtils.DEFAULT_LCASE_FORMAT_OPTION); 211 } 212 }