File D:\maven-qcr-plugin-testsunitaires\qcr4java\h2-1.3.170-sources\target\site\qc\lecode\command\dml\Insert.java.html

1 /*
2 * Copyright 2004-2011 H2 Group. Multiple-Licensed under the H2 License,
3 * Version 1.0, and under the Eclipse Public License, Version 1.0
4 * (http://h2database.com/html/license.html).
5 * Initial Developer: H2 Group
6 */
7 package org.h2.command.dml;
8
9 import java.util.ArrayList;
10 import org.h2.api.Trigger;
11 import org.h2.command.Command;
12 import org.h2.command.CommandInterface;
13 import org.h2.command.Prepared;
14 import org.h2.constant.ErrorCode;
15 import org.h2.engine.Right;
16 import org.h2.engine.Session;
17 import org.h2.engine.UndoLogRecord;
18 import org.h2.expression.Expression;
19 import org.h2.expression.Parameter;
20 import org.h2.index.Index;
21 import org.h2.message.DbException;
22 import org.h2.result.ResultInterface;
23 import org.h2.result.ResultTarget;
24 import org.h2.result.Row;
25 import org.h2.table.Column;
26 import org.h2.table.Table;
27 import org.h2.util.New;
28 import org.h2.util.StatementBuilder;
29 import org.h2.value.Value;
30
31 /**
32 * This class represents the statement
33 * INSERT
34 */
35 public class Insert extends Prepared implements ResultTarget {
36
37 private Table table;
38 private Column[] columns;
39 private final ArrayList<Expression[]> list = New.arrayList();
40 private Query query;
41 private boolean sortedInsertMode;
42 private int rowNumber;
43 private boolean insertFromSelect;
44
45 public Insert(Session session) {
46 super(session);
47 }
48
49 public void setCommand(Command command) {
50 super.setCommand(command);
51 if (query != null) {
52 query.setCommand(command);
53 }
54 }
55
56 public void setTable(Table table) {
57 this.table = table;
58 }
59
60 public void setColumns(Column[] columns) {
61 this.columns = columns;
62 }
63
64 public void setQuery(Query query) {
65 this.query = query;
66 }
67
68 /**
69 * Add a row to this merge statement.
70 *
71 * @param expr the list of values
72 */
73 public void addRow(Expression[] expr) {
74 list.add(expr);
75 }
76
77 public int update() {
78 Index index = null;
79 if (sortedInsertMode) {
80 index = table.getScanIndex(session);
81 index.setSortedInsertMode(true);
82 }
83 try {
84 return insertRows();
85 } finally {
86 if (index != null) {
87 index.setSortedInsertMode(false);
88 }
89 }
90 }
91
92 private int insertRows() {
93 session.getUser().checkRight(table, Right.INSERT);
94 setCurrentRowNumber(0);
95 table.fire(session, Trigger.INSERT, true);
96 rowNumber = 0;
97 int listSize = list.size();
98 if (listSize > 0) {
99 int columnLen = columns.length;
100 for (int x = 0; x < listSize; x++) {
101 Row newRow = table.getTemplateRow();
102 Expression[] expr = list.get(x);
103 setCurrentRowNumber(x + 1);
104 for (int i = 0; i < columnLen; i++) {
105 Column c = columns[i];
106 int index = c.getColumnId();
107 Expression e = expr[i];
108 if (e != null) {
109 // e can be null (DEFAULT)
110 e = e.optimize(session);
111 try {
112 Value v = c.convert(e.getValue(session));
113 newRow.setValue(index, v);
114 } catch (DbException ex) {
115 throw setRow(ex, x, getSQL(expr));
116 }
117 }
118 }
119 rowNumber++;
120 table.validateConvertUpdateSequence(session, newRow);
121 boolean done = table.fireBeforeRow(session, null, newRow);
122 if (!done) {
123 table.lock(session, true, false);
124 table.addRow(session, newRow);
125 session.log(table, UndoLogRecord.INSERT, newRow);
126 table.fireAfterRow(session, null, newRow, false);
127 }
128 }
129 } else {
130 table.lock(session, true, false);
131 if (insertFromSelect) {
132 query.query(0, this);
133 } else {
134 ResultInterface rows = query.query(0);
135 while (rows.next()) {
136 Value[] r = rows.currentRow();
137 addRow(r);
138 }
139 rows.close();
140 }
141 }
142 table.fire(session, Trigger.INSERT, false);
143 return rowNumber;
144 }
145
146 public void addRow(Value[] values) {
147 Row newRow = table.getTemplateRow();
148 setCurrentRowNumber(++rowNumber);
149 for (int j = 0, len = columns.length; j < len; j++) {
150 Column c = columns[j];
151 int index = c.getColumnId();
152 try {
153 Value v = c.convert(values[j]);
154 newRow.setValue(index, v);
155 } catch (DbException ex) {
156 throw setRow(ex, rowNumber, getSQL(values));
157 }
158 }
159 table.validateConvertUpdateSequence(session, newRow);
160 boolean done = table.fireBeforeRow(session, null, newRow);
161 if (!done) {
162 table.addRow(session, newRow);
163 session.log(table, UndoLogRecord.INSERT, newRow);
164 table.fireAfterRow(session, null, newRow, false);
165 }
166 }
167
168 public int getRowCount() {
169 return rowNumber;
170 }
171
172 public String getPlanSQL() {
173 StatementBuilder buff = new StatementBuilder("INSERT INTO ");
174 buff.append(table.getSQL()).append('(');
175 for (Column c : columns) {
176 buff.appendExceptFirst(", ");
177 buff.append(c.getSQL());
178 }
179 buff.append(")\n");
180 if (insertFromSelect) {
181 buff.append("DIRECT ");
182 }
183 if (sortedInsertMode) {
184 buff.append("SORTED ");
185 }
186 if (list.size() > 0) {
187 buff.append("VALUES ");
188 int row = 0;
189 if (list.size() > 1) {
190 buff.append('\n');
191 }
192 for (Expression[] expr : list) {
193 if (row++ > 0) {
194 buff.append(",\n");
195 }
196 buff.append('(');
197 buff.resetCount();
198 for (Expression e : expr) {
199 buff.appendExceptFirst(", ");
200 if (e == null) {
201 buff.append("DEFAULT");
202 } else {
203 buff.append(e.getSQL());
204 }
205 }
206 buff.append(')');
207 }
208 } else {
209 buff.append(query.getPlanSQL());
210 }
211 return buff.toString();
212 }
213
214 public void prepare() {
215 if (columns == null) {
216 if (list.size() > 0 && list.get(0).length == 0) {
217 // special case where table is used as a sequence
218 columns = new Column[0];
219 } else {
220 columns = table.getColumns();
221 }
222 }
223 if (list.size() > 0) {
224 for (Expression[] expr : list) {
225 if (expr.length != columns.length) {
226 throw DbException.get(ErrorCode.COLUMN_COUNT_DOES_NOT_MATCH);
227 }
228 for (int i = 0, len = expr.length; i < len; i++) {
229 Expression e = expr[i];
230 if (e != null) {
231 e = e.optimize(session);
232 if (e instanceof Parameter) {
233 Parameter p = (Parameter) e;
234 p.setColumn(columns[i]);
235 }
236 expr[i] = e;
237 }
238 }
239 }
240 } else {
241 query.prepare();
242 if (query.getColumnCount() != columns.length) {
243 throw DbException.get(ErrorCode.COLUMN_COUNT_DOES_NOT_MATCH);
244 }
245 }
246 }
247
248 public boolean isTransactional() {
249 return true;
250 }
251
252 public ResultInterface queryMeta() {
253 return null;
254 }
255
256 public void setSortedInsertMode(boolean sortedInsertMode) {
257 this.sortedInsertMode = sortedInsertMode;
258 }
259
260 public int getType() {
261 return CommandInterface.INSERT;
262 }
263
264 public void setInsertFromSelect(boolean value) {
265 this.insertFromSelect = value;
266 }
267
268 public boolean isCacheable() {
269 return true;
270 }
271
272 }
273