1 | /* |
2 | |
3 | Derby - Class org.apache.derby.impl.tools.ij.Session |
4 | |
5 | Copyright 1998, 2004 The Apache Software Foundation or its licensors, as applicable. |
6 | |
7 | Licensed under the Apache License, Version 2.0 (the "License"); |
8 | you may not use this file except in compliance with the License. |
9 | You may obtain a copy of the License at |
10 | |
11 | http://www.apache.org/licenses/LICENSE-2.0 |
12 | |
13 | Unless required by applicable law or agreed to in writing, software |
14 | distributed under the License is distributed on an "AS IS" BASIS, |
15 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
16 | See the License for the specific language governing permissions and |
17 | limitations under the License. |
18 | |
19 | */ |
20 | |
21 | package org.apache.derby.impl.tools.ij; |
22 | |
23 | import org.apache.derby.iapi.tools.i18n.LocalizedOutput; |
24 | |
25 | import java.sql.Connection; |
26 | import java.sql.PreparedStatement; |
27 | import java.sql.Statement; |
28 | import java.sql.ResultSet; |
29 | import java.sql.SQLException; |
30 | import java.util.Hashtable; |
31 | |
32 | /** |
33 | Session holds the objects local to a particular database session, |
34 | which starts with a connection and is all other JDBC |
35 | stuff used on that connection, along with some ij state |
36 | that is connection-based as well. |
37 | |
38 | This is separated out to localize database objects and |
39 | also group objects by session. |
40 | |
41 | @author ames |
42 | */ |
43 | class Session { |
44 | static final String DEFAULT_NAME="CONNECTION"; |
45 | |
46 | boolean singleSession = true; |
47 | Connection conn = null; |
48 | String tag, name; |
49 | Hashtable prepStmts = new Hashtable(); |
50 | Hashtable cursorStmts = new Hashtable(); |
51 | Hashtable cursors = new Hashtable(); |
52 | Hashtable asyncStmts = new Hashtable(); |
53 | boolean isJCC= false; // Is this the IBM UNIVERSAL DRIVER. |
54 | boolean isDNC = false; // Is this the Derby Network Client JDBC Driver |
55 | Session(Connection newConn, String newTag, String newName) { |
56 | conn = newConn; |
57 | tag = newTag; |
58 | name = newName; |
59 | |
60 | try |
61 | { |
62 | isJCC = conn.getMetaData().getDriverName().startsWith("IBM DB2 JDBC Universal Driver"); |
63 | isDNC = conn.getMetaData().getDriverName().startsWith("Apache Derby Network Client"); |
64 | |
65 | } |
66 | catch (SQLException se) |
67 | { |
68 | // if there is a problem getting the driver name we will |
69 | // assume it is not JCC or DNC. |
70 | } |
71 | } |
72 | |
73 | Connection getConnection() { |
74 | // CHECK: should never be null |
75 | return conn; |
76 | } |
77 | |
78 | boolean getIsJCC() |
79 | { |
80 | return isJCC; |
81 | } |
82 | |
83 | boolean getIsDNC() |
84 | { |
85 | return isDNC; |
86 | } |
87 | |
88 | String getName() { |
89 | return name; |
90 | } |
91 | |
92 | Object addPreparedStatement(String name, PreparedStatement ps) { |
93 | return prepStmts.put(name,ps); |
94 | } |
95 | |
96 | Object addCursorStatement(String name, Statement s) { |
97 | return cursorStmts.put(name, s); |
98 | } |
99 | |
100 | Object addCursor(String name, ResultSet rs) { |
101 | return cursors.put(name, rs); |
102 | } |
103 | |
104 | Object addAsyncStatement(String name, AsyncStatement s) { |
105 | return asyncStmts.put(name, s); |
106 | } |
107 | |
108 | PreparedStatement getPreparedStatement(String name) { |
109 | return (PreparedStatement) prepStmts.get(name); |
110 | } |
111 | |
112 | Statement getCursorStatement(String name) { |
113 | return (Statement)cursorStmts.get(name); |
114 | } |
115 | |
116 | ResultSet getCursor(String name) { |
117 | return (ResultSet)cursors.get(name); |
118 | } |
119 | |
120 | AsyncStatement getAsyncStatement(String name) { |
121 | return (AsyncStatement)asyncStmts.get(name); |
122 | } |
123 | |
124 | boolean removePreparedStatement(String name) { |
125 | return prepStmts.remove(name)!=null; |
126 | } |
127 | |
128 | boolean removeCursorStatement(String name) { |
129 | return cursorStmts.remove(name)!=null; |
130 | } |
131 | |
132 | boolean removeCursor(String name) { |
133 | return cursors.remove(name)!=null; |
134 | } |
135 | |
136 | void doPrompt(boolean newStatement, LocalizedOutput out, boolean multiSessions) { |
137 | // check if tag should be increased... |
138 | if (multiSessions && singleSession) { |
139 | singleSession = false; |
140 | |
141 | if (tag == null) tag = "("+name+")"; |
142 | else tag = tag.substring(0,tag.length()-1)+":"+name+")"; |
143 | } |
144 | |
145 | // check if tag should be reduced... |
146 | if (!multiSessions && !singleSession) { |
147 | singleSession = true; |
148 | |
149 | if (tag == null) {} |
150 | else if (tag.length() == name.length()+2) tag = null; |
151 | else tag = tag.substring(0,tag.length()-2-name.length())+")"; |
152 | } |
153 | |
154 | utilMain.doPrompt(newStatement, out, tag); |
155 | } |
156 | |
157 | void close() throws SQLException { |
158 | |
159 | if (!conn.isClosed()) |
160 | { |
161 | if (!conn.getAutoCommit() && name != null && ! name.startsWith("XA")) |
162 | conn.rollback(); |
163 | conn.close(); |
164 | } |
165 | |
166 | prepStmts.clear(); // should we check & close them individually? |
167 | cursorStmts.clear(); |
168 | cursors.clear(); |
169 | asyncStmts.clear(); |
170 | |
171 | conn = null; |
172 | } |
173 | |
174 | } |