001: /**
002: * Sequoia: Database clustering technology.
003: * Copyright (C) 2006 Continuent, Inc.
004: * Contact: sequoia@continuent.org
005: *
006: * Licensed under the Apache License, Version 2.0 (the "License");
007: * you may not use this file except in compliance with the License.
008: * You may obtain a copy of the License at
009: *
010: * http://www.apache.org/licenses/LICENSE-2.0
011: *
012: * Unless required by applicable law or agreed to in writing, software
013: * distributed under the License is distributed on an "AS IS" BASIS,
014: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015: * See the License for the specific language governing permissions and
016: * limitations under the License.
017: *
018: * Initial developer(s): Emmanuel Cecchet.
019: * Contributor(s): ______________________.
020: */package org.continuent.sequoia.controller.backend;
021:
022: import java.sql.ResultSetMetaData;
023: import java.sql.SQLException;
024:
025: import org.continuent.sequoia.common.protocol.Field;
026: import org.continuent.sequoia.controller.cache.metadata.MetadataCache;
027:
028: /**
029: * This class defines a SequoiaResultSetMetaDataFactory
030: *
031: * @author <a href="mailto:emmanuel.cecchet@continuent.com">Emmanuel Cecchet</a>
032: * @version 1.0
033: */
034: public class SequoiaResultSetMetaDataFactory implements
035: ResultSetMetaDataFactory {
036:
037: /**
038: * @see org.continuent.sequoia.controller.backend.ResultSetMetaDataFactory#copyResultSetMetaData(java.sql.ResultSetMetaData,
039: * org.continuent.sequoia.controller.cache.metadata.MetadataCache)
040: */
041: public Field[] copyResultSetMetaData(ResultSetMetaData metaData,
042: MetadataCache metadataCache) throws SQLException {
043: if (metaData == null) {
044: // Nothing to fetch
045: return new Field[0];
046: // At least two drivers (pgjdbc & hsqldb) return a null reference
047: // in this case, so we are not transparent here. See SEQUOIA-262.
048: }
049:
050: int nbColumn = metaData.getColumnCount();
051: Field[] fields = new Field[nbColumn];
052: for (int i = 1; i <= nbColumn; i++) { // 1st column is 1
053: String columnName = metaData.getColumnName(i);
054: String fTableName = null;
055: try {
056: fTableName = metaData.getTableName(i);
057: } catch (Exception ignore) {
058: }
059: String columnLabel = null;
060: try {
061: columnLabel = metaData.getColumnLabel(i);
062: } catch (Exception ignore) {
063: }
064: if (metadataCache != null) { // Check Field cache
065: fields[i - 1] = metadataCache.getField(fTableName + "."
066: + columnName + "." + columnLabel);
067: if (fields[i - 1] != null)
068: continue; // Cache hit
069: }
070: // Field cache miss
071: int fColumnDisplaySize = 0;
072: try {
073: fColumnDisplaySize = metaData.getColumnDisplaySize(i);
074: } catch (Exception ignore) {
075: }
076: int columnType = -1;
077: try {
078: columnType = metaData.getColumnType(i);
079: } catch (Exception ignore) {
080: }
081: String columnTypeName = null;
082: try {
083: columnTypeName = metaData.getColumnTypeName(i);
084: } catch (Exception ignore) {
085: }
086: String fColumnClassName = null;
087: try {
088: fColumnClassName = metaData.getColumnClassName(i);
089: } catch (Exception ignore) {
090: }
091: boolean fIsAutoIncrement = false;
092: try {
093: fIsAutoIncrement = metaData.isAutoIncrement(i);
094: } catch (Exception ignore) {
095: }
096: boolean fIsCaseSensitive = false;
097: try {
098: fIsCaseSensitive = metaData.isCaseSensitive(i);
099: } catch (Exception ignore) {
100: }
101: boolean fIsCurrency = false;
102: try {
103: fIsCurrency = metaData.isCurrency(i);
104: } catch (Exception ignore) {
105: }
106: int fIsNullable = ResultSetMetaData.columnNullableUnknown;
107: try {
108: fIsNullable = metaData.isNullable(i);
109: } catch (Exception ignore) {
110: }
111: boolean fIsReadOnly = false;
112: try {
113: fIsReadOnly = metaData.isReadOnly(i);
114: } catch (Exception ignore) {
115: }
116: boolean fIsWritable = false;
117: try {
118: fIsWritable = metaData.isWritable(i);
119: } catch (Exception ignore) {
120: }
121: boolean fIsDefinitelyWritable = false;
122: try {
123: fIsDefinitelyWritable = metaData
124: .isDefinitelyWritable(i);
125: } catch (Exception ignore) {
126: }
127: boolean fIsSearchable = false;
128: try {
129: fIsSearchable = metaData.isSearchable(i);
130: } catch (Exception ignore) {
131: }
132: boolean fIsSigned = false;
133: try {
134: fIsSigned = metaData.isSigned(i);
135: } catch (Exception ignore) {
136: }
137: int fPrecision = 0;
138: try {
139: fPrecision = metaData.getPrecision(i);
140: } catch (Exception ignore) {
141: }
142: int fScale = 0;
143: try {
144: fScale = metaData.getScale(i);
145: } catch (Exception ignore) {
146: }
147: String encoding = null;
148: // Here we trust the backend's driver not to change behind our back the
149: // metadata Strings it gave to us (this would be a rather weird
150: // interpretation of the JDBC spec)
151: fields[i - 1] = new Field(fTableName, columnName,
152: columnLabel, fColumnDisplaySize, columnType,
153: columnTypeName, fColumnClassName, fIsAutoIncrement,
154: fIsCaseSensitive, fIsCurrency, fIsNullable,
155: fIsReadOnly, fIsWritable, fIsDefinitelyWritable,
156: fIsSearchable, fIsSigned, fPrecision, fScale,
157: encoding);
158:
159: if (metadataCache != null)
160: // Add field to cache
161: metadataCache.addField(fTableName + "." + columnName
162: + "." + columnLabel, fields[i - 1]);
163: } // for
164: return fields;
165: }
166:
167: }
|