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.SQLInsertInto;
17 
18 
19 import hunt.collection;
20 
21 import hunt.sql.ast.SQLExpr;
22 import hunt.sql.ast.SQLName;
23 import hunt.sql.ast.SQLObjectImpl;
24 import hunt.sql.ast.statement.SQLInsertStatement;
25 // import hunt.sql.ast.statement.SQLInsertStatement.ValuesClause;
26 import hunt.sql.ast.statement.SQLExprTableSource;
27 import hunt.sql.ast.statement.SQLSelect;
28 import hunt.sql.ast.statement.SQLSelectQuery;
29 
30 public abstract class SQLInsertInto : SQLObjectImpl {
31     protected SQLExprTableSource        tableSource;
32     protected  List!SQLExpr       columns;
33     protected  string          columnsString;
34     protected  long            columnsStringHash;
35     protected SQLSelect                 query;
36     protected  List!(ValuesClause)  valuesList;
37 
38     public this(){
39         columns = new ArrayList!SQLExpr();
40         valuesList = new ArrayList!(ValuesClause)();
41     }
42 
43     public void cloneTo(SQLInsertInto x) {
44         if (tableSource !is null) {
45             x.setTableSource(tableSource.clone());
46         }
47         foreach (SQLExpr column ; columns) {
48             SQLExpr column2 = column.clone();
49             column2.setParent(x);
50             x.columns.add(column2);
51         }
52         if (query !is null) {
53             x.setQuery(query.clone());
54         }
55         foreach (ValuesClause v ; valuesList) {
56             ValuesClause v2 = v.clone();
57             v2.setParent(x);
58             x.valuesList.add(v2);
59         }
60     }
61 
62     override public abstract SQLInsertInto clone();
63 
64     public string getAlias() {
65         return tableSource.getAlias();
66     }
67 
68     public void setAlias(string alias_p) {
69         this.tableSource.setAlias(alias_p);
70     }
71 
72     public SQLExprTableSource getTableSource() {
73         return tableSource;
74     }
75 
76     public void setTableSource(SQLExprTableSource tableSource) {
77         if (tableSource !is null) {
78             tableSource.setParent(this);
79         }
80         this.tableSource = tableSource;
81     }
82 
83     public SQLName getTableName() {
84         return cast(SQLName) tableSource.getExpr();
85     }
86 
87     public void setTableName(SQLName tableName) {
88         this.setTableSource(new SQLExprTableSource(tableName));
89     }
90 
91     public void setTableSource(SQLName tableName) {
92         this.setTableSource(new SQLExprTableSource(tableName));
93     }
94 
95     public SQLSelect getQuery() {
96         return query;
97     }
98 
99     public void setQuery(SQLSelectQuery query) {
100         this.setQuery(new SQLSelect(query));
101     }
102 
103     public void setQuery(SQLSelect query) {
104         if (query !is null) {
105             query.setParent(this);
106         }
107         this.query = query;
108     }
109 
110     public List!SQLExpr getColumns() {
111         return columns;
112     }
113     
114     public void addColumn(SQLExpr column) {
115         if (column !is null) {
116             column.setParent(this);
117         }
118         this.columns.add(column);
119     }
120 
121     public ValuesClause getValues() {
122         if (valuesList.size() == 0) {
123             return null;
124         }
125         return valuesList.get(0);
126     }
127 
128     public void setValues(ValuesClause values) {
129         if (valuesList.size() == 0) {
130             valuesList.add(values);
131         } else {
132             valuesList.set(0, values);
133         }
134     }
135     
136     public List!ValuesClause getValuesList() {
137         return valuesList;
138     }
139 
140     public void addValueCause(ValuesClause valueClause) {
141         if (valueClause !is null) {
142             valueClause.setParent(this);
143         }
144         valuesList.add(valueClause);
145     }
146 
147     public string getColumnsString() {
148         return columnsString;
149     }
150 
151     public long getColumnsStringHash() {
152         return columnsStringHash;
153     }
154 
155     public void setColumnsString(string columnsString, long columnsStringHash) {
156         this.columnsString = columnsString;
157         this.columnsStringHash = columnsStringHash;
158     }
159 }