--- /dev/null
+/*\r
+\r
+ Derby - Class org.apache.derby.impl.tools.ij.Session\r
+\r
+ Licensed to the Apache Software Foundation (ASF) under one or more\r
+ contributor license agreements. See the NOTICE file distributed with\r
+ this work for additional information regarding copyright ownership.\r
+ The ASF licenses this file to You under the Apache License, Version 2.0\r
+ (the "License"); you may not use this file except in compliance with\r
+ the License. You may obtain a copy of the License at\r
+\r
+ http://www.apache.org/licenses/LICENSE-2.0\r
+\r
+ Unless required by applicable law or agreed to in writing, software\r
+ distributed under the License is distributed on an "AS IS" BASIS,\r
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
+ See the License for the specific language governing permissions and\r
+ limitations under the License.\r
+\r
+ */\r
+\r
+package org.apache.derby.impl.tools.ij;\r
+\r
+import org.apache.derby.iapi.tools.i18n.LocalizedOutput;\r
+\r
+import java.sql.Connection;\r
+import java.sql.PreparedStatement;\r
+import java.sql.Statement;\r
+import java.sql.ResultSet;\r
+import java.sql.SQLException;\r
+import java.util.Hashtable;\r
+\r
+/**\r
+ Session holds the objects local to a particular database session,\r
+ which starts with a connection and is all other JDBC\r
+ stuff used on that connection, along with some ij state\r
+ that is connection-based as well.\r
+\r
+ This is separated out to localize database objects and\r
+ also group objects by session.\r
+\r
+ */\r
+class Session {\r
+ static final String DEFAULT_NAME="CONNECTION";\r
+\r
+ boolean singleSession = true;\r
+ Connection conn = null;\r
+ String tag, name;\r
+ Hashtable prepStmts = new Hashtable();\r
+ Hashtable cursorStmts = new Hashtable();\r
+ Hashtable cursors = new Hashtable();\r
+ Hashtable asyncStmts = new Hashtable();\r
+ boolean isJCC= false; // Is this the IBM UNIVERSAL DRIVER.\r
+ boolean isDNC = false; // Is this the Derby Network Client JDBC Driver\r
+ Session(Connection newConn, String newTag, String newName) {\r
+ conn = newConn;\r
+ tag = newTag;\r
+ name = newName;\r
+\r
+ try\r
+ {\r
+ isJCC = conn.getMetaData().getDriverName().startsWith("IBM DB2 JDBC Universal Driver");\r
+ isDNC = conn.getMetaData().getDriverName().startsWith("Apache Derby Network Client");\r
+\r
+ }\r
+ catch (SQLException se)\r
+ {\r
+ // if there is a problem getting the driver name we will\r
+ // assume it is not JCC or DNC.\r
+ }\r
+ }\r
+\r
+ Connection getConnection() {\r
+ // CHECK: should never be null\r
+ return conn;\r
+ }\r
+\r
+ boolean getIsJCC()\r
+ {\r
+ return isJCC;\r
+ }\r
+\r
+ boolean getIsDNC()\r
+ {\r
+ return isDNC;\r
+ }\r
+\r
+ String getName() {\r
+ return name;\r
+ }\r
+\r
+ Object addPreparedStatement(String name, PreparedStatement ps) {\r
+ return prepStmts.put(name,ps);\r
+ }\r
+\r
+ Object addCursorStatement(String name, Statement s) {\r
+ return cursorStmts.put(name, s);\r
+ }\r
+\r
+ Object addCursor(String name, ResultSet rs) {\r
+ return cursors.put(name, rs);\r
+ }\r
+\r
+ Object addAsyncStatement(String name, AsyncStatement s) {\r
+ return asyncStmts.put(name, s);\r
+ }\r
+\r
+ PreparedStatement getPreparedStatement(String name) {\r
+ return (PreparedStatement) prepStmts.get(name);\r
+ }\r
+\r
+ Statement getCursorStatement(String name) {\r
+ return (Statement)cursorStmts.get(name);\r
+ }\r
+\r
+ ResultSet getCursor(String name) {\r
+ return (ResultSet)cursors.get(name);\r
+ }\r
+\r
+ AsyncStatement getAsyncStatement(String name) {\r
+ return (AsyncStatement)asyncStmts.get(name);\r
+ }\r
+\r
+ boolean removePreparedStatement(String name) {\r
+ return prepStmts.remove(name)!=null;\r
+ }\r
+\r
+ boolean removeCursorStatement(String name) {\r
+ return cursorStmts.remove(name)!=null;\r
+ }\r
+\r
+ boolean removeCursor(String name) {\r
+ return cursors.remove(name)!=null;\r
+ }\r
+\r
+ void doPrompt(boolean newStatement, LocalizedOutput out, boolean multiSessions) {\r
+ // check if tag should be increased...\r
+ if (multiSessions && singleSession) {\r
+ singleSession = false;\r
+\r
+ if (tag == null) tag = "("+name+")";\r
+ else tag = tag.substring(0,tag.length()-1)+":"+name+")";\r
+ }\r
+\r
+ // check if tag should be reduced...\r
+ if (!multiSessions && !singleSession) {\r
+ singleSession = true;\r
+\r
+ if (tag == null) {}\r
+ else if (tag.length() == name.length()+2) tag = null;\r
+ else tag = tag.substring(0,tag.length()-2-name.length())+")";\r
+ }\r
+\r
+ utilMain.doPrompt(newStatement, out, tag);\r
+ }\r
+\r
+ void close() throws SQLException {\r
+\r
+ if (!conn.isClosed())\r
+ {\r
+ if (!conn.getAutoCommit() && name != null && ! name.startsWith("XA"))\r
+ conn.rollback();\r
+ conn.close();\r
+ }\r
+\r
+ prepStmts.clear(); // should we check & close them individually?\r
+ cursorStmts.clear();\r
+ cursors.clear();\r
+ asyncStmts.clear();\r
+\r
+ conn = null;\r
+ }\r
+\r
+}\r