KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > h2 > command > Prepared


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;
6
7 import java.sql.SQLException JavaDoc;
8
9 import org.h2.engine.Constants;
10 import org.h2.engine.Database;
11 import org.h2.engine.Session;
12 import org.h2.expression.Expression;
13 import org.h2.expression.Parameter;
14 import org.h2.message.Message;
15 import org.h2.result.LocalResult;
16 import org.h2.util.ObjectArray;
17
18 public abstract class Prepared {
19     
20     protected String JavaDoc sql;
21     protected int headPos = -1;
22     protected Session session;
23     protected ObjectArray parameters;
24     private long modificationId;
25     private Command command;
26     private int objectId;
27     private boolean prepareAlways;
28     private int currentRowNumber;
29
30     public boolean needRecompile() throws SQLException JavaDoc {
31         Database db = session.getDatabase();
32         if(db == null) {
33             throw Message.getSQLException(Message.CONNECTION_BROKEN);
34         }
35         // TODO parser: currently, compiling every create/drop/... twice! because needRecompile return true even for the first execution
36
return Constants.RECOMPILE_ALWAYS || prepareAlways || modificationId < db.getModificationMetaId();
37     }
38     
39     public abstract boolean isTransactional();
40     
41     public boolean isReadOnly() {
42         return false;
43     }
44
45     public Prepared(Session session) {
46         this.session = session;
47         modificationId = session.getDatabase().getModificationMetaId();
48     }
49     
50     long getModificationId() {
51         return modificationId;
52     }
53     
54     void setModificationId(long id) {
55         this.modificationId = id;
56     }
57     
58     public void setParameterList(ObjectArray parameters) {
59         this.parameters = parameters;
60     }
61
62     public ObjectArray getParameters() {
63         return parameters;
64     }
65
66     protected void checkParameters() throws SQLException JavaDoc {
67         for (int i = 0; parameters != null && i < parameters.size(); i++) {
68             Parameter param = (Parameter) parameters.get(i);
69             param.checkSet();
70         }
71     }
72     
73     public void setCommand(Command command) {
74         this.command = command;
75     }
76     
77     public boolean isQuery() {
78         return false;
79     }
80     
81     public void prepare() throws SQLException JavaDoc {
82         // nothing to do
83
}
84
85     public int update() throws SQLException JavaDoc {
86         throw Message.getSQLException(Message.METHOD_NOT_ALLOWED_FOR_QUERY);
87     }
88
89     public LocalResult query(int maxrows) throws SQLException JavaDoc {
90         throw Message.getSQLException(Message.METHOD_ONLY_ALLOWED_FOR_QUERY);
91     }
92
93     public void setSQL(String JavaDoc sql) {
94         this.sql = sql;
95     }
96     
97     public String JavaDoc getSQL() {
98         return sql;
99     }
100
101     protected int getObjectId(boolean needFresh, boolean dataFile) {
102         Database db = session.getDatabase();
103         int id = objectId;
104         if(id == 0) {
105             id = db.allocateObjectId(needFresh, dataFile);
106         }
107         objectId = 0;
108         return id;
109     }
110     
111     public String JavaDoc getPlan() {
112         return null;
113     }
114     
115     protected void checkCancelled() throws SQLException JavaDoc {
116         // TODO strange code: probably checkCancelled should always be called on the session. fix & test after release 1.0
117
if(command != null) {
118             command.checkCancelled();
119         } else {
120             session.checkCancelled();
121         }
122     }
123     
124     public void setObjectId(int i) {
125         this.objectId = i;
126     }
127
128     public void setHeadPos(int headPos) {
129         this.headPos = headPos;
130     }
131
132     public void setSession(Session currentSession) {
133         this.session = currentSession;
134     }
135     
136     void trace() throws SQLException JavaDoc {
137         if(session.getTrace().info()) {
138             StringBuffer JavaDoc buff = new StringBuffer JavaDoc();
139             buff.append(sql);
140             if(parameters.size()>0) {
141                 buff.append(" {");
142                 for(int i=0; i<parameters.size(); i++) {
143                     if(i>0) {
144                         buff.append(", ");
145                     }
146                     buff.append(i+1);
147                     buff.append(": ");
148                     Expression e = (Expression) parameters.get(i);
149                     buff.append(e.getValue(session).getSQL());
150                 }
151                 buff.append("};");
152             } else {
153                 buff.append(';');
154             }
155             session.getTrace().infoSQL(buff.toString());
156         }
157     }
158
159     public void setPrepareAlways(boolean prepareAlways) {
160         this.prepareAlways = prepareAlways;
161     }
162     
163     protected void setCurrentRowNumber(int rowNumber) {
164         this.currentRowNumber = rowNumber;
165     }
166
167     public int getCurrentRowNumber() {
168         return currentRowNumber;
169     }
170
171 }
172
Popular Tags