KickJava   Java API By Example, From Geeks To Geeks.

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


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.message.Message;
13 import org.h2.message.Trace;
14 import org.h2.result.LocalResult;
15 import org.h2.result.ResultInterface;
16 import org.h2.util.ObjectArray;
17
18 /**
19  * @author Thomas
20  */

21 public abstract class Command implements CommandInterface {
22     protected Session session;
23     protected long startTime;
24     protected Trace trace;
25     private volatile boolean cancel;
26
27     public abstract boolean isTransactional();
28     public abstract boolean isQuery();
29     public abstract ObjectArray getParameters();
30     public abstract boolean isReadOnly();
31
32     public Command(Parser parser) {
33         this.session = parser.getSession();
34         trace = session.getDatabase().getTrace(Trace.COMMAND);
35     }
36
37     public int update() throws SQLException JavaDoc {
38         throw Message.getSQLException(Message.METHOD_NOT_ALLOWED_FOR_QUERY);
39     }
40
41     public LocalResult query(int maxrows) throws SQLException JavaDoc {
42         throw Message.getSQLException(Message.METHOD_ONLY_ALLOWED_FOR_QUERY);
43     }
44
45     // TODO insert parameters into the original query, or allow this syntax
46
// if(parameters != null && parameters.size() > 0) {
47
// buff.append(" /* ");
48
// for(int i=0; i<parameters.size(); i++) {
49
// if(i>0) {
50
// buff.append(", ");
51
// }
52
// Parameter param = (Parameter) parameters.get(i);
53
// buff.append(i+1);
54
// buff.append(" = ");
55
// buff.append(param.getSQL());
56
// }
57
// buff.append(" */");
58
// }
59

60     public ResultInterface executeQuery(int maxrows, boolean scrollable) throws SQLException JavaDoc {
61         return executeQueryLocal(maxrows);
62     }
63
64     public LocalResult executeQueryLocal(int maxrows) throws SQLException JavaDoc {
65         startTime = System.currentTimeMillis();
66         Database database = session.getDatabase();
67         Object JavaDoc sync = Constants.MULTI_THREADED_KERNEL ? (Object JavaDoc)session : (Object JavaDoc)database;
68         synchronized (sync) {
69             try {
70                 database.checkPowerOff();
71                 session.setCurrentCommand(this);
72                 LocalResult result = query(maxrows);
73                 return result;
74             } catch(Throwable JavaDoc e) {
75                 SQLException JavaDoc s = Message.convert(e);
76                 database.exceptionThrown(s);
77                 throw s;
78             } finally {
79                 stop();
80             }
81         }
82     }
83
84     protected void start() {
85         startTime = System.currentTimeMillis();
86     }
87
88     public void checkCancelled() throws SQLException JavaDoc {
89         if (cancel) {
90             throw Message.getSQLException(Message.STATEMENT_WAS_CANCELLED);
91         }
92         session.throttle();
93     }
94
95     private void stop() throws SQLException JavaDoc {
96         session.setCurrentCommand(null);
97         if (!isTransactional()) {
98             // meta data changes need to commit in any case
99
session.commit();
100         } else if (session.getAutoCommit()) {
101             session.commit();
102         }
103         if (trace.info()) {
104             long time = System.currentTimeMillis() - startTime;
105             if (time > Constants.LONG_QUERY_LIMIT_MS) {
106                 trace.info("long query: " + time);
107             }
108         }
109     }
110
111     public int executeUpdate() throws SQLException JavaDoc {
112         startTime = System.currentTimeMillis();
113         Database database = session.getDatabase();
114         Object JavaDoc sync = Constants.MULTI_THREADED_KERNEL ? (Object JavaDoc)session : (Object JavaDoc)database;
115         synchronized (sync) {
116             int rollback = session.getLogId();
117             session.setCurrentCommand(this);
118             try {
119                 database.checkPowerOff();
120                 int result = update();
121                 return result;
122             } catch (SQLException JavaDoc e) {
123                 database.exceptionThrown(e);
124                 database.checkPowerOff();
125                 session.rollbackTo(rollback);
126                 throw e;
127             } finally {
128                 stop();
129             }
130         }
131     }
132
133     public void close() {
134         // nothing to do
135
}
136
137     public void cancel() {
138         this.cancel = true;
139     }
140     
141 }
142
Popular Tags