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.dialect.postgresql.ast.stmt.PGInsertStatement;
17 
18 
19 import hunt.collection;
20 
21 import hunt.sql.ast.SQLExpr;
22 import hunt.sql.ast.SQLName;
23 import hunt.sql.ast.statement.SQLInsertStatement;
24 import hunt.sql.ast.statement.SQLUpdateSetItem;
25 import hunt.sql.ast.statement.SQLWithSubqueryClause;
26 import hunt.sql.dialect.postgresql.visitor.PGASTVisitor;
27 import hunt.sql.visitor.SQLASTVisitor;
28 import hunt.sql.util.DBType;
29 import hunt.sql.dialect.postgresql.ast.stmt.PGSQLStatement;
30 import hunt.sql.ast.SQLObjectImpl;
31 import hunt.sql.ast.SQLObject;
32 
33 
34 public class PGInsertStatement : SQLInsertStatement , PGSQLStatement {
35 
36     alias cloneTo = SQLInsertStatement.cloneTo;
37     
38     private List!(ValuesClause)     valuesList;
39     private SQLExpr                returning;
40     private bool			       defaultValues = false;
41 
42     private List!(SQLExpr)          onConflictTarget;
43     private SQLName                onConflictConstraint;
44     private SQLExpr                onConflictWhere;
45     private bool                onConflictDoNothing;
46     private List!(SQLUpdateSetItem) onConflictUpdateSetItems;
47 
48 
49     public this() {
50         valuesList = new ArrayList!(ValuesClause)();
51         dbType = DBType.POSTGRESQL.name;
52     }
53 
54     public void cloneTo(PGInsertStatement x) {
55         super.cloneTo(x);
56         foreach(ValuesClause v ; valuesList) {
57             ValuesClause v2 = v.clone();
58             v2.setParent(x);
59             x.valuesList.add(v2);
60         }
61         if (returning !is null) {
62             x.setReturning(returning.clone());
63         }
64         x.defaultValues = defaultValues;
65     }
66 
67     public SQLExpr getReturning() {
68         return returning;
69     }
70 
71     public void setReturning(SQLExpr returning) {
72         this.returning = returning;
73     }
74 
75 
76     override public ValuesClause getValues() {
77         if (valuesList.size() == 0) {
78             return null;
79         }
80         return valuesList.get(0);
81     }
82 
83     override public void setValues(ValuesClause values) {
84         if (valuesList.size() == 0) {
85             valuesList.add(values);
86         } else {
87             valuesList.set(0, values);
88         }
89     }
90 
91     override public List!(ValuesClause) getValuesList() {
92         return valuesList;
93     }
94 
95     override public void addValueCause(ValuesClause valueClause) {
96         valueClause.setParent(this);
97         valuesList.add(valueClause);
98     }
99 
100     public bool isDefaultValues() {
101 		return defaultValues;
102 	}
103 
104 	public void setDefaultValues(bool defaultValues) {
105 		this.defaultValues = defaultValues;
106 	}
107 
108 	override  protected void accept0(SQLASTVisitor visitor) {
109         // accept0(cast(PGASTVisitor) visitor);
110         if (cast(PGASTVisitor)(visitor) !is null) {
111             accept0(cast(PGASTVisitor) visitor);
112         } else {
113             super.accept0(visitor);
114         }
115     }
116 
117     override
118     public void accept0(PGASTVisitor visitor) {
119         if (visitor.visit(this)) {
120             this.acceptChild(visitor, _with);
121             this.acceptChild(visitor, tableSource);
122             this.acceptChild!SQLExpr(visitor, columns);
123             this.acceptChild!ValuesClause(visitor, valuesList);
124             this.acceptChild(visitor, query);
125             this.acceptChild(visitor, returning);
126         }
127 
128         visitor.endVisit(this);
129     }
130 
131     override public PGInsertStatement clone() {
132         PGInsertStatement x = new PGInsertStatement();
133         cloneTo(x);
134         return x;
135     }
136 
137     public List!(SQLExpr) getOnConflictTarget() {
138         return onConflictTarget;
139     }
140 
141     public void setOnConflictTarget(List!(SQLExpr) onConflictTarget) {
142         this.onConflictTarget = onConflictTarget;
143     }
144 
145     public bool isOnConflictDoNothing() {
146         return onConflictDoNothing;
147     }
148 
149     public void setOnConflictDoNothing(bool onConflictDoNothing) {
150         this.onConflictDoNothing = onConflictDoNothing;
151     }
152 
153     public List!(SQLUpdateSetItem) getOnConflictUpdateSetItems() {
154         return onConflictUpdateSetItems;
155     }
156 
157     public void addConflicUpdateItem(SQLUpdateSetItem item) {
158         if (onConflictUpdateSetItems is null) {
159             onConflictUpdateSetItems = new ArrayList!(SQLUpdateSetItem)();
160         }
161 
162         item.setParent(this);
163         onConflictUpdateSetItems.add(item);
164     }
165 
166     public SQLName getOnConflictConstraint() {
167         return onConflictConstraint;
168     }
169 
170     public void setOnConflictConstraint(SQLName x) {
171         if (x !is null) {
172             x.setParent(this);
173         }
174         this.onConflictConstraint = x;
175     }
176 
177     public SQLExpr getOnConflictWhere() {
178         return onConflictWhere;
179     }
180 
181     public void setOnConflictWhere(SQLExpr x) {
182         if (x !is null) {
183             x.setParent(this);
184         }
185         this.onConflictWhere = x;
186     }
187 }