KickJava   Java API By Example, From Geeks To Geeks.

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


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.expression.Expression;
10 import org.h2.expression.Parameter;
11 import org.h2.result.LocalResult;
12 import org.h2.util.ObjectArray;
13 import org.h2.value.Value;
14
15 public class CommandContainer extends Command {
16     
17     private Prepared prepared;
18     
19     CommandContainer(Parser parser, Prepared prepared) {
20         super(parser);
21         prepared.setCommand(this);
22         this.prepared = prepared;
23     }
24     
25     public ObjectArray getParameters() {
26         return prepared.getParameters();
27     }
28     
29     public boolean isTransactional() {
30         return prepared.isTransactional();
31     }
32     
33     public boolean isQuery() {
34         return prepared.isQuery();
35     }
36     
37     private void recompileIfRequired() throws SQLException JavaDoc {
38         if(prepared.needRecompile()) {
39             // TODO test with 'always recompile'
40
prepared.setModificationId(0);
41             String JavaDoc sql = prepared.getSQL();
42             ObjectArray oldValues = prepared.getParameters();
43             Parser parser = new Parser(session);
44             prepared = parser.parseOnly(sql);
45             long mod = prepared.getModificationId();
46             prepared.setModificationId(0);
47             ObjectArray newParams = prepared.getParameters();
48             for(int i=0; i<newParams.size(); i++) {
49                 Value v = ((Expression)oldValues.get(i)).getValue(session);
50                 Parameter p = (Parameter) newParams.get(i);
51                 p.setValue(v);
52             }
53             prepared.prepare();
54             prepared.setModificationId(mod);
55         }
56     }
57     
58     public int update() throws SQLException JavaDoc {
59         recompileIfRequired();
60         // TODO query time: should keep lock time separate from running time
61
start();
62         prepared.checkParameters();
63         prepared.trace();
64         return prepared.update();
65     }
66     
67     public LocalResult query(int maxrows) throws SQLException JavaDoc {
68         recompileIfRequired();
69         // TODO query time: should keep lock time separate from running time
70
start();
71         prepared.checkParameters();
72         prepared.trace();
73         return prepared.query(maxrows);
74     }
75
76     public boolean isReadOnly() {
77         return prepared.isReadOnly();
78     }
79     
80 }
81
Popular Tags