KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > h2 > command > dml > Insert


1 /*
2  * Copyright 2004-2006 H2 Group. Licensed under the H2 License, Version 1.0 (http://h2database.com/html/license.html).
3  * Initial Developer: H2 Group
4  */

5 package org.h2.command.dml;
6
7 import java.sql.SQLException JavaDoc;
8
9 import org.h2.command.Command;
10 import org.h2.command.Prepared;
11 import org.h2.engine.Right;
12 import org.h2.engine.Session;
13 import org.h2.expression.Expression;
14 import org.h2.message.Message;
15 import org.h2.result.LocalResult;
16 import org.h2.result.Row;
17 import org.h2.store.UndoLogRecord;
18 import org.h2.table.Column;
19 import org.h2.table.Table;
20 import org.h2.util.ObjectArray;
21 import org.h2.util.StringUtils;
22 import org.h2.value.Value;
23
24 /**
25  * @author Thomas
26  */

27 public class Insert extends Prepared {
28
29     private Table table;
30     private Column[] columns;
31     private ObjectArray list = new ObjectArray();
32     private Query query;
33
34     public Insert(Session session) {
35         super(session);
36     }
37
38     public void setCommand(Command command) {
39         super.setCommand(command);
40         if(query != null) {
41             query.setCommand(command);
42         }
43     }
44
45     public void setTable(Table table) {
46         this.table = table;
47     }
48
49     public void setColumns(Column[] columns) {
50         this.columns = columns;
51     }
52
53     public void setQuery(Query query) {
54         this.query = query;
55     }
56
57     public void addRow(Expression[] expr) {
58         list.add(expr);
59     }
60
61     public int update() throws SQLException JavaDoc {
62         int count;
63         session.getUser().checkRight(table, Right.INSERT);
64         setCurrentRowNumber(0);
65         if(list.size() > 0) {
66             count = 0;
67             for(int x=0; x<list.size(); x++) {
68                 Expression[] expr = (Expression[])list.get(x);
69                 Row newRow = table.getTemplateRow();
70                 setCurrentRowNumber(x+1);
71                 for (int i = 0; i < columns.length; i++) {
72                     Column c = columns[i];
73                     int index = c.getColumnId();
74                     Expression e = expr[i];
75                     if(e != null) {
76                         // e can be null (DEFAULT)
77
e = e.optimize(session);
78                         Value v = e.getValue(session).convertTo(c.getType());
79                         newRow.setValue(index, v);
80                     }
81                 }
82                 // TODO insert: set default values before calling triggers?
83
checkCancelled();
84                 table.fireBefore(session);
85                 table.validateConvertUpdateSequence(session, newRow);
86                 table.fireBeforeRow(session, null, newRow);
87                 table.lock(session, true);
88                 table.addRow(session, newRow);
89                 session.log(new UndoLogRecord(table, UndoLogRecord.INSERT, newRow));
90                 table.fireAfter(session);
91                 table.fireAfterRow(session, null, newRow);
92                 count++;
93             }
94         } else {
95             LocalResult rows = query.query(0);
96             count = 0;
97             table.fireBefore(session);
98             table.lock(session, true);
99             while(rows.next()) {
100                 checkCancelled();
101                 count++;
102                 Value[] r = rows.currentRow();
103                 Row newRow = table.getTemplateRow();
104                 setCurrentRowNumber(count);
105                 for (int j = 0; j < columns.length; j++) {
106                     Column c = columns[j];
107                     int index = c.getColumnId();
108                     Value v = r[j].convertTo(c.getType());
109                     newRow.setValue(index, v);
110                 }
111                 table.validateConvertUpdateSequence(session, newRow);
112                 table.fireBeforeRow(session, null, newRow);
113                 table.addRow(session, newRow);
114                 session.log(new UndoLogRecord(table, UndoLogRecord.INSERT, newRow));
115                 table.fireAfterRow(session, null, newRow);
116             }
117             rows.close();
118             table.fireAfter(session);
119         }
120         return count;
121     }
122
123     public String JavaDoc getPlan() {
124         StringBuffer JavaDoc buff = new StringBuffer JavaDoc();
125         buff.append("INSERT INTO ");
126         buff.append(table.getSQL());
127         buff.append('(');
128         for(int i=0; i<columns.length; i++) {
129             if(i>0) {
130                 buff.append(", ");
131             }
132             buff.append(columns[i].getSQL());
133         }
134         buff.append(")\n");
135         if(list.size() > 0) {
136             buff.append("VALUES ");
137             for(int x=0; x<list.size(); x++) {
138                 Expression[] expr = (Expression[])list.get(x);
139                 if(x > 0) {
140                     buff.append(", ");
141                 }
142                 buff.append("(");
143                 for (int i = 0; i < columns.length; i++) {
144                     if(i>0) {
145                         buff.append(", ");
146                     }
147                     Expression e = expr[i];
148                     if(e == null) {
149                         buff.append("DEFAULT");
150                     } else {
151                         buff.append(StringUtils.unEnclose(e.getSQL()));
152                     }
153                 }
154                 buff.append(')');
155             }
156         } else {
157             buff.append(query.getPlan());
158         }
159         return buff.toString();
160     }
161
162     public void prepare() throws SQLException JavaDoc {
163         if (columns == null) {
164             if(list.size() > 0 && ((Expression[])list.get(0)).length == 0) {
165                 // special case where table is used as a sequence
166
columns = new Column[0];
167             } else {
168                 columns = table.getColumns();
169             }
170         }
171         if(list.size() > 0) {
172             for(int x=0; x<list.size(); x++) {
173                 Expression[] expr = (Expression[])list.get(x);
174                 if(expr.length != columns.length) {
175                     throw Message.getSQLException(Message.COLUMN_COUNT_DOES_NOT_MATCH);
176                 }
177                 for(int i=0; i<expr.length; i++) {
178                     Expression e = expr[i];
179                     if(e != null) {
180                         expr[i] = e.optimize(session);
181                     }
182                 }
183             }
184         } else {
185             query.prepare();
186             if(query.getColumnCount() != columns.length) {
187                 throw Message.getSQLException(Message.COLUMN_COUNT_DOES_NOT_MATCH);
188             }
189         }
190     }
191
192     public boolean isTransactional() {
193         return true;
194     }
195
196 }
197
Popular Tags