001: /*
002:
003: Derby - Class org.apache.derby.impl.tools.ij.Session
004:
005: Licensed to the Apache Software Foundation (ASF) under one or more
006: contributor license agreements. See the NOTICE file distributed with
007: this work for additional information regarding copyright ownership.
008: The ASF licenses this file to You under the Apache License, Version 2.0
009: (the "License"); you may not use this file except in compliance with
010: the License. You may obtain a copy of the License at
011:
012: http://www.apache.org/licenses/LICENSE-2.0
013:
014: Unless required by applicable law or agreed to in writing, software
015: distributed under the License is distributed on an "AS IS" BASIS,
016: WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
017: See the License for the specific language governing permissions and
018: limitations under the License.
019:
020: */
021:
022: package org.apache.derby.impl.tools.ij;
023:
024: import org.apache.derby.iapi.tools.i18n.LocalizedOutput;
025:
026: import java.sql.Connection;
027: import java.sql.PreparedStatement;
028: import java.sql.Statement;
029: import java.sql.ResultSet;
030: import java.sql.SQLException;
031: import java.util.Hashtable;
032:
033: /**
034: Session holds the objects local to a particular database session,
035: which starts with a connection and is all other JDBC
036: stuff used on that connection, along with some ij state
037: that is connection-based as well.
038:
039: This is separated out to localize database objects and
040: also group objects by session.
041:
042: @author ames
043: */
044: class Session {
045: static final String DEFAULT_NAME = "CONNECTION";
046:
047: boolean singleSession = true;
048: Connection conn = null;
049: String tag, name;
050: Hashtable prepStmts = new Hashtable();
051: Hashtable cursorStmts = new Hashtable();
052: Hashtable cursors = new Hashtable();
053: Hashtable asyncStmts = new Hashtable();
054: boolean isJCC = false; // Is this the IBM UNIVERSAL DRIVER.
055: boolean isDNC = false; // Is this the Derby Network Client JDBC Driver
056:
057: Session(Connection newConn, String newTag, String newName) {
058: conn = newConn;
059: tag = newTag;
060: name = newName;
061:
062: try {
063: isJCC = conn.getMetaData().getDriverName().startsWith(
064: "IBM DB2 JDBC Universal Driver");
065: isDNC = conn.getMetaData().getDriverName().startsWith(
066: "Apache Derby Network Client");
067:
068: } catch (SQLException se) {
069: // if there is a problem getting the driver name we will
070: // assume it is not JCC or DNC.
071: }
072: }
073:
074: Connection getConnection() {
075: // CHECK: should never be null
076: return conn;
077: }
078:
079: boolean getIsJCC() {
080: return isJCC;
081: }
082:
083: boolean getIsDNC() {
084: return isDNC;
085: }
086:
087: String getName() {
088: return name;
089: }
090:
091: Object addPreparedStatement(String name, PreparedStatement ps) {
092: return prepStmts.put(name, ps);
093: }
094:
095: Object addCursorStatement(String name, Statement s) {
096: return cursorStmts.put(name, s);
097: }
098:
099: Object addCursor(String name, ResultSet rs) {
100: return cursors.put(name, rs);
101: }
102:
103: Object addAsyncStatement(String name, AsyncStatement s) {
104: return asyncStmts.put(name, s);
105: }
106:
107: PreparedStatement getPreparedStatement(String name) {
108: return (PreparedStatement) prepStmts.get(name);
109: }
110:
111: Statement getCursorStatement(String name) {
112: return (Statement) cursorStmts.get(name);
113: }
114:
115: ResultSet getCursor(String name) {
116: return (ResultSet) cursors.get(name);
117: }
118:
119: AsyncStatement getAsyncStatement(String name) {
120: return (AsyncStatement) asyncStmts.get(name);
121: }
122:
123: boolean removePreparedStatement(String name) {
124: return prepStmts.remove(name) != null;
125: }
126:
127: boolean removeCursorStatement(String name) {
128: return cursorStmts.remove(name) != null;
129: }
130:
131: boolean removeCursor(String name) {
132: return cursors.remove(name) != null;
133: }
134:
135: void doPrompt(boolean newStatement, LocalizedOutput out,
136: boolean multiSessions) {
137: // check if tag should be increased...
138: if (multiSessions && singleSession) {
139: singleSession = false;
140:
141: if (tag == null)
142: tag = "(" + name + ")";
143: else
144: tag = tag.substring(0, tag.length() - 1) + ":" + name
145: + ")";
146: }
147:
148: // check if tag should be reduced...
149: if (!multiSessions && !singleSession) {
150: singleSession = true;
151:
152: if (tag == null) {
153: } else if (tag.length() == name.length() + 2)
154: tag = null;
155: else
156: tag = tag
157: .substring(0, tag.length() - 2 - name.length())
158: + ")";
159: }
160:
161: utilMain.doPrompt(newStatement, out, tag);
162: }
163:
164: void close() throws SQLException {
165:
166: if (!conn.isClosed()) {
167: if (!conn.getAutoCommit() && name != null
168: && !name.startsWith("XA"))
169: conn.rollback();
170: conn.close();
171: }
172:
173: prepStmts.clear(); // should we check & close them individually?
174: cursorStmts.clear();
175: cursors.clear();
176: asyncStmts.clear();
177:
178: conn = null;
179: }
180:
181: }
|