KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > derby > impl > tools > ij > Session


1 /*
2
3    Derby - Class org.apache.derby.impl.tools.ij.Session
4
5    Licensed to the Apache Software Foundation (ASF) under one or more
6    contributor license agreements. See the NOTICE file distributed with
7    this work for additional information regarding copyright ownership.
8    The ASF licenses this file to You under the Apache License, Version 2.0
9    (the "License"); you may not use this file except in compliance with
10    the License. You may obtain a copy of the License at
11
12       http://www.apache.org/licenses/LICENSE-2.0
13
14    Unless required by applicable law or agreed to in writing, software
15    distributed under the License is distributed on an "AS IS" BASIS,
16    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17    See the License for the specific language governing permissions and
18    limitations under the License.
19
20  */

21
22 package org.apache.derby.impl.tools.ij;
23
24 import org.apache.derby.iapi.tools.i18n.LocalizedOutput;
25
26 import java.sql.Connection JavaDoc;
27 import java.sql.PreparedStatement JavaDoc;
28 import java.sql.Statement JavaDoc;
29 import java.sql.ResultSet JavaDoc;
30 import java.sql.SQLException JavaDoc;
31 import java.util.Hashtable JavaDoc;
32
33 /**
34     Session holds the objects local to a particular database session,
35     which starts with a connection and is all other JDBC
36     stuff used on that connection, along with some ij state
37     that is connection-based as well.
38
39     This is separated out to localize database objects and
40     also group objects by session.
41
42     @author ames
43  */

44 class Session {
45     static final String JavaDoc DEFAULT_NAME="CONNECTION";
46
47     boolean singleSession = true;
48     Connection JavaDoc conn = null;
49     String JavaDoc tag, name;
50     Hashtable JavaDoc prepStmts = new Hashtable JavaDoc();
51     Hashtable JavaDoc cursorStmts = new Hashtable JavaDoc();
52     Hashtable JavaDoc cursors = new Hashtable JavaDoc();
53     Hashtable JavaDoc asyncStmts = new Hashtable JavaDoc();
54     boolean isJCC= false; // Is this the IBM UNIVERSAL DRIVER.
55
boolean isDNC = false; // Is this the Derby Network Client JDBC Driver
56
Session(Connection JavaDoc newConn, String JavaDoc newTag, String JavaDoc newName) {
57         conn = newConn;
58         tag = newTag;
59         name = newName;
60
61         try
62         {
63             isJCC = conn.getMetaData().getDriverName().startsWith("IBM DB2 JDBC Universal Driver");
64             isDNC = conn.getMetaData().getDriverName().startsWith("Apache Derby Network Client");
65
66         }
67         catch (SQLException JavaDoc se)
68         {
69             // if there is a problem getting the driver name we will
70
// assume it is not JCC or DNC.
71
}
72     }
73
74     Connection JavaDoc getConnection() {
75         // CHECK: should never be null
76
return conn;
77     }
78
79     boolean getIsJCC()
80     {
81         return isJCC;
82     }
83
84     boolean getIsDNC()
85     {
86         return isDNC;
87     }
88
89     String JavaDoc getName() {
90         return name;
91     }
92
93     Object JavaDoc addPreparedStatement(String JavaDoc name, PreparedStatement JavaDoc ps) {
94         return prepStmts.put(name,ps);
95     }
96
97     Object JavaDoc addCursorStatement(String JavaDoc name, Statement JavaDoc s) {
98         return cursorStmts.put(name, s);
99     }
100
101     Object JavaDoc addCursor(String JavaDoc name, ResultSet JavaDoc rs) {
102         return cursors.put(name, rs);
103     }
104
105     Object JavaDoc addAsyncStatement(String JavaDoc name, AsyncStatement s) {
106         return asyncStmts.put(name, s);
107     }
108
109     PreparedStatement JavaDoc getPreparedStatement(String JavaDoc name) {
110         return (PreparedStatement JavaDoc) prepStmts.get(name);
111     }
112
113     Statement JavaDoc getCursorStatement(String JavaDoc name) {
114         return (Statement JavaDoc)cursorStmts.get(name);
115     }
116
117     ResultSet JavaDoc getCursor(String JavaDoc name) {
118         return (ResultSet JavaDoc)cursors.get(name);
119     }
120
121     AsyncStatement getAsyncStatement(String JavaDoc name) {
122         return (AsyncStatement)asyncStmts.get(name);
123     }
124
125     boolean removePreparedStatement(String JavaDoc name) {
126         return prepStmts.remove(name)!=null;
127     }
128
129     boolean removeCursorStatement(String JavaDoc name) {
130         return cursorStmts.remove(name)!=null;
131     }
132
133     boolean removeCursor(String JavaDoc name) {
134         return cursors.remove(name)!=null;
135     }
136
137     void doPrompt(boolean newStatement, LocalizedOutput out, boolean multiSessions) {
138         // check if tag should be increased...
139
if (multiSessions && singleSession) {
140             singleSession = false;
141
142             if (tag == null) tag = "("+name+")";
143             else tag = tag.substring(0,tag.length()-1)+":"+name+")";
144         }
145
146         // check if tag should be reduced...
147
if (!multiSessions && !singleSession) {
148             singleSession = true;
149
150             if (tag == null) {}
151             else if (tag.length() == name.length()+2) tag = null;
152             else tag = tag.substring(0,tag.length()-2-name.length())+")";
153         }
154
155         utilMain.doPrompt(newStatement, out, tag);
156     }
157
158     void close() throws SQLException JavaDoc {
159
160         if (!conn.isClosed())
161         {
162             if (!conn.getAutoCommit() && name != null && ! name.startsWith("XA"))
163                 conn.rollback();
164             conn.close();
165         }
166
167         prepStmts.clear(); // should we check & close them individually?
168
cursorStmts.clear();
169         cursors.clear();
170         asyncStmts.clear();
171
172         conn = null;
173     }
174
175 }
176
Popular Tags