KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > derby > client > am > SectionManager


1 /*
2
3    Derby - Class org.apache.derby.client.am.SectionManager
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.client.am;
23
24 import org.apache.derby.shared.common.reference.JDBC30Translation;
25
26 import org.apache.derby.shared.common.reference.SQLState;
27
28
29 public class SectionManager {
30     String JavaDoc collection_;
31     Agent agent_;
32
33     // Cursor holdability attributes used as package cluster indices.
34
// Not defined in PackageBindOptions because this attribute is part of the
35
// declare cursor [with hold] sql string based on section binds.
36
// By convention, we bind all sections in the same package with
37
// the same holdability.
38
final static int HOLD = 0;
39     final static int NO_HOLD = 1;
40
41     // The following stack of available sections is
42
// for pooling and recycling previously used sections.
43
// For performance, the section objects themselves are pooled,
44
// rather than just keeping track of free section numbers;
45
// this way, we don't have to new-up a section if one is available in the pool.
46
java.util.Stack JavaDoc freeSectionsNonHold_ = null;
47     java.util.Stack JavaDoc freeSectionsHold_ = null;
48
49     int nextAvailableSectionNumber_ = 1;
50
51     // store package consistency token information and initialized in
52
// setPKGNAMCBytes
53
// holdPKGNAMCBytes stores PKGNAMCBytes when holdability is hold
54
// noHoldPKGNAMCBytes stores PKGNAMCBytes when holdability is no hold
55
public static byte[] holdPKGNAMCBytes = null;
56     public static byte[] noHoldPKGNAMCBytes = null;
57
58
59     final static String JavaDoc packageNameWithHold__ = "SYSLH000";
60     final static String JavaDoc packageNameWithNoHold__ = "SYSLN000";
61
62     final static String JavaDoc cursorNamePrefixWithHold__ = "SQL_CURLH000C";
63     final static String JavaDoc cursorNamePrefixWithNoHold__ = "SQL_CURLN000C";
64
65     // Jdbc 1 positioned updates are implemented via
66
// sql scan for "...where current of <users-cursor-name>",
67
// the addition of mappings from cursor names to query sections,
68
// and the subtitution of <users-cursor-name> with <canned-cursor-name> in the pass-thru sql string
69
// "...where current of <canned-cursor-name>" when user-defined cursor names are used.
70
// Both "canned" cursor names (from our jdbc package set) and user-defined cursor names are mapped.
71
// Statement.cursorName_ is initialized to null until the cursor name is requested or set.
72
// When set (s.setCursorName()) with a user-defined name, then it is added to the cursor map at that time;
73
// When requested (rs.getCursorName()), if the cursor name is still null,
74
// then is given the canned cursor name as defined by our jdbc package set and added to the cursor map.
75
// Still need to consider how positioned updates should interact with multiple result sets from a stored.
76
private java.util.Hashtable JavaDoc positionedUpdateCursorNameToQuerySection_ = new java.util.Hashtable JavaDoc();
77
78     // Cursor name to ResultSet mapping is needed for positioned updates to check whether
79
// a ResultSet is scrollable. If so, exception is thrown.
80
private java.util.Hashtable JavaDoc positionedUpdateCursorNameToResultSet_ = new java.util.Hashtable JavaDoc();
81
82     String JavaDoc databaseName;
83
84     int maxNumSections_ = 32768;
85
86     public SectionManager(String JavaDoc collection, Agent agent, String JavaDoc databaseName) {
87         collection_ = collection;
88         agent_ = agent;
89         this.databaseName = databaseName;
90         freeSectionsNonHold_ = new java.util.Stack JavaDoc();
91         freeSectionsHold_ = new java.util.Stack JavaDoc();
92     }
93
94     /**
95      * Store the Packagename and consistency token information This is called from Section.setPKGNAMCBytes
96      *
97      * @param b bytearray that has the PKGNAMC information to be stored
98      * @param resultSetHoldability depending on the holdability store it in the correct byte array packagename and
99      * consistency token information for when holdability is set to HOLD_CURSORS_OVER_COMMIT
100      * is stored in holdPKGNAMCBytes and in noHoldPKGNAMCBytes when holdability is set to
101      * CLOSE_CURSORS_AT_COMMIT
102      */

103     public void setPKGNAMCBytes(byte[] b, int resultSetHoldability) {
104         if (resultSetHoldability == JDBC30Translation.HOLD_CURSORS_OVER_COMMIT) {
105             agent_.sectionManager_.holdPKGNAMCBytes = b;
106         } else if (resultSetHoldability == JDBC30Translation.CLOSE_CURSORS_AT_COMMIT) {
107             agent_.sectionManager_.noHoldPKGNAMCBytes = b;
108         }
109     }
110
111
112     //------------------------entry points----------------------------------------
113

114     // Get a section for either a jdbc update or query statement.
115
public Section getDynamicSection(int resultSetHoldability) throws SqlException {
116         int cursorHoldIndex;
117         if (resultSetHoldability == JDBC30Translation.HOLD_CURSORS_OVER_COMMIT) {
118             return getSection(freeSectionsHold_, packageNameWithHold__, cursorNamePrefixWithHold__, resultSetHoldability);
119         } else if (resultSetHoldability == JDBC30Translation.CLOSE_CURSORS_AT_COMMIT) {
120             return getSection(freeSectionsNonHold_, packageNameWithNoHold__, cursorNamePrefixWithNoHold__, resultSetHoldability);
121         } else {
122             throw new SqlException(agent_.logWriter_,
123                 new ClientMessageId(SQLState.UNSUPPORTED_HOLDABILITY_PROPERTY),
124                 new Integer JavaDoc(resultSetHoldability));
125         }
126     }
127
128     protected Section getSection(java.util.Stack JavaDoc freeSections, String JavaDoc packageName, String JavaDoc cursorNamePrefix, int resultSetHoldability) throws SqlException {
129         if (!freeSections.empty()) {
130             return (Section) freeSections.pop();
131         } else if (nextAvailableSectionNumber_ < (maxNumSections_ - 1)) {
132             String JavaDoc cursorName = cursorNamePrefix + nextAvailableSectionNumber_;
133             Section section = new Section(agent_, packageName, nextAvailableSectionNumber_, cursorName, resultSetHoldability);
134             nextAvailableSectionNumber_++;
135             return section;
136         } else
137         // unfortunately we have run out of sections
138
{
139             throw new SqlException(agent_.logWriter_,
140                 new ClientMessageId(SQLState.EXCEEDED_MAX_SECTIONS),
141                 "32k");
142         }
143     }
144
145     public void freeSection(Section section, int resultSetHoldability) {
146         if (resultSetHoldability == JDBC30Translation.HOLD_CURSORS_OVER_COMMIT) {
147             this.freeSectionsHold_.push(section);
148         } else if (resultSetHoldability == JDBC30Translation.CLOSE_CURSORS_AT_COMMIT) {
149             this.freeSectionsNonHold_.push(section);
150         }
151     }
152
153     // Get a section for a jdbc 2 positioned update/delete for the corresponding query.
154
// A positioned update section must come from the same package as its query section.
155
Section getPositionedUpdateSection(Section querySection) throws SqlException {
156         Connection connection = agent_.connection_;
157         return getDynamicSection(connection.holdability());
158     }
159
160     // Get a section for a jdbc 1 positioned update/delete for the corresponding query.
161
// A positioned update section must come from the same package as its query section.
162
Section getPositionedUpdateSection(String JavaDoc cursorName, boolean useExecuteImmediateSection) throws SqlException {
163         Section querySection = (Section) positionedUpdateCursorNameToQuerySection_.get(cursorName);
164
165         // If querySection is null, then the user must have provided a bad cursor name.
166
// Otherwise, get a new section and save the client's cursor name and the server's
167
// cursor name to the new section.
168
if (querySection != null) {
169             Section section = getPositionedUpdateSection(querySection);
170             // getPositionedUpdateSection gets the next available section from the query
171
// package, and it has a different cursor name associated with the section.
172
// We need to save the client's cursor name and server's cursor name to the
173
// new section.
174
section.setClientCursorName(querySection.getClientCursorName());
175             section.serverCursorNameForPositionedUpdate_ = querySection.getServerCursorName();
176             return section;
177         } else {
178             return null;
179         }
180     }
181
182     void mapCursorNameToQuerySection(String JavaDoc cursorName, Section section) {
183         positionedUpdateCursorNameToQuerySection_.put(cursorName, section);
184     }
185
186     void mapCursorNameToResultSet(String JavaDoc cursorName, ResultSet resultSet) {
187         positionedUpdateCursorNameToResultSet_.put(cursorName, resultSet);
188     }
189
190     ResultSet getPositionedUpdateResultSet(String JavaDoc cursorName) throws SqlException {
191         ResultSet rs = (ResultSet) positionedUpdateCursorNameToResultSet_.get(cursorName);
192         if (rs == null) {
193             throw new SqlException(agent_.logWriter_,
194                 new ClientMessageId(SQLState.CLIENT_RESULT_SET_NOT_OPEN));
195         }
196         return (rs.resultSetType_ == java.sql.ResultSet.TYPE_FORWARD_ONLY) ? null : rs;
197     }
198
199     void removeCursorNameToResultSetMapping(String JavaDoc clientCursorName,
200                                             String JavaDoc serverCursorName) {
201         if (clientCursorName != null) {
202             positionedUpdateCursorNameToResultSet_.remove(clientCursorName);
203         }
204         if (serverCursorName != null) {
205             positionedUpdateCursorNameToResultSet_.remove(serverCursorName);
206         }
207     }
208
209     void removeCursorNameToQuerySectionMapping(String JavaDoc clientCursorName,
210                                                String JavaDoc serverCursorName) {
211         if (clientCursorName != null) {
212             positionedUpdateCursorNameToQuerySection_.remove(clientCursorName);
213         }
214         if (serverCursorName != null) {
215             positionedUpdateCursorNameToQuerySection_.remove(serverCursorName);
216         }
217     }
218
219 }
220
221
Popular Tags