View Javadoc

1   /*
2    * ModeShape (http://www.modeshape.org)
3    * See the COPYRIGHT.txt file distributed with this work for information
4    * regarding copyright ownership.  Some portions may be licensed
5    * to Red Hat, Inc. under one or more contributor license agreements.
6    * See the AUTHORS.txt file in the distribution for a full listing of 
7    * individual contributors.
8    *
9    * ModeShape is free software. Unless otherwise indicated, all code in ModeShape
10   * is licensed to you under the terms of the GNU Lesser General Public License as
11   * published by the Free Software Foundation; either version 2.1 of
12   * the License, or (at your option) any later version.
13   * 
14   * ModeShape is distributed in the hope that it will be useful,
15   * but WITHOUT ANY WARRANTY; without even the implied warranty of
16   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17   * Lesser General Public License for more details.
18   *
19   * You should have received a copy of the GNU Lesser General Public
20   * License along with this software; if not, write to the Free
21   * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
22   * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
23   */
24  package org.modeshape.jdbc.metadata;
25  
26  import java.sql.ResultSetMetaData;
27  import java.sql.SQLException;
28  import java.sql.Types;
29  import org.modeshape.jdbc.JcrType;
30  import org.modeshape.jdbc.JdbcI18n;
31  
32  /**
33   * 
34   */
35  public class ResultSetMetaDataImpl implements ResultSetMetaData {
36  
37      private MetadataProvider provider;
38  
39      public ResultSetMetaDataImpl( MetadataProvider provider ) {
40          this.provider = provider;
41      }
42  
43      /**
44       * Adjust from 1-based to internal 0-based representation
45       * 
46       * @param index External 1-based representation
47       * @return Internal 0-based representation
48       */
49      private int adjustColumn( int index ) {
50          return index - 1;
51      }
52  
53      public int getColumnCount() {
54          return provider.getColumnCount();
55      }
56  
57      public boolean isAutoIncrement( int index ) {
58          return provider.getBooleanValue(adjustColumn(index), ResultsMetadataConstants.AUTO_INCREMENTING);
59      }
60  
61      public boolean isCaseSensitive( int index ) {
62          return provider.getBooleanValue(adjustColumn(index), ResultsMetadataConstants.CASE_SENSITIVE);
63      }
64  
65      public boolean isSearchable( int index ) {
66          Integer searchable = (Integer)provider.getValue(adjustColumn(index), ResultsMetadataConstants.SEARCHABLE);
67          return !(ResultsMetadataConstants.SEARCH_TYPES.UNSEARCHABLE.equals(searchable));
68      }
69  
70      public boolean isCurrency( int index ) {
71          return provider.getBooleanValue(adjustColumn(index), ResultsMetadataConstants.CURRENCY);
72      }
73  
74      public int isNullable( int index ) {
75          Object nullable = provider.getValue(adjustColumn(index), ResultsMetadataConstants.NULLABLE);
76          if (nullable.equals(ResultsMetadataConstants.NULL_TYPES.NULLABLE)) {
77              return columnNullable;
78          } else if (nullable.equals(ResultsMetadataConstants.NULL_TYPES.NOT_NULL)) {
79              return columnNoNulls;
80          } else {
81              return columnNullableUnknown;
82          }
83      }
84  
85      public boolean isSigned( int index ) {
86          return provider.getBooleanValue(adjustColumn(index), ResultsMetadataConstants.SIGNED);
87      }
88  
89      public int getColumnDisplaySize( int index ) {
90          return provider.getIntValue(adjustColumn(index), ResultsMetadataConstants.DISPLAY_SIZE);
91      }
92  
93      public String getColumnLabel( int index ) {
94          return provider.getStringValue(adjustColumn(index), ResultsMetadataConstants.COLUMN_LABEL);
95      }
96  
97      public String getColumnName( int index ) {
98          return provider.getStringValue(adjustColumn(index), ResultsMetadataConstants.COLUMN);
99      }
100 
101     public String getSchemaName( int index ) {
102         String name = provider.getStringValue(adjustColumn(index), ResultsMetadataConstants.SCHEMA);
103         if (name != null) {
104             int dotIndex = name.indexOf('.');
105             if (dotIndex != -1) {
106                 return name.substring(0, dotIndex);
107             }
108         }
109         return null;
110     }
111 
112     public int getPrecision( int index ) {
113         return provider.getIntValue(adjustColumn(index), ResultsMetadataConstants.PRECISION);
114     }
115 
116     public int getScale( int index ) {
117         return provider.getIntValue(adjustColumn(index), ResultsMetadataConstants.SCALE);
118     }
119 
120     public String getTableName( int index ) {
121         String name = provider.getStringValue(adjustColumn(index), ResultsMetadataConstants.TABLE);
122         if (name != null) {
123             int dotIndex = name.indexOf('.');
124             if (dotIndex != -1) {
125                 return name.substring(dotIndex + 1);
126             }
127         }
128         return name;
129     }
130 
131     public String getCatalogName( int index ) {
132         return provider.getStringValue(adjustColumn(index), ResultsMetadataConstants.CATALOG);
133     }
134 
135     public int getColumnType( int index ) {
136         String runtimeTypeName = provider.getStringValue(adjustColumn(index), ResultsMetadataConstants.DATA_TYPE);
137 
138         JcrType typeInfo = JcrType.typeInfo(runtimeTypeName);
139         return typeInfo != null ? typeInfo.getJdbcType() : Types.VARCHAR;
140     }
141 
142     public String getColumnTypeName( int index ) {
143         return provider.getStringValue(adjustColumn(index), ResultsMetadataConstants.DATA_TYPE);
144     }
145 
146     public boolean isReadOnly( int index ) {
147         return !provider.getBooleanValue(adjustColumn(index), ResultsMetadataConstants.WRITABLE);
148     }
149 
150     public boolean isWritable( int index ) {
151         return provider.getBooleanValue(adjustColumn(index), ResultsMetadataConstants.WRITABLE);
152     }
153 
154     public boolean isDefinitelyWritable( int index ) {
155         return provider.getBooleanValue(adjustColumn(index), ResultsMetadataConstants.WRITABLE);
156     }
157 
158     public String getColumnClassName( int index ) {
159         JcrType typeInfo = JcrType.typeInfo(getColumnTypeName(index));
160         return typeInfo != null ? typeInfo.getRepresentationClass().getName() : String.class.getName();
161     }
162 
163     /**
164      * {@inheritDoc}
165      * 
166      * @see java.sql.Wrapper#isWrapperFor(java.lang.Class)
167      */
168     @Override
169     public boolean isWrapperFor( Class<?> iface ) {
170         return iface.isInstance(this);
171     }
172 
173     /**
174      * {@inheritDoc}
175      * 
176      * @see java.sql.Wrapper#unwrap(java.lang.Class)
177      */
178     @Override
179     public <T> T unwrap( Class<T> iface ) throws SQLException {
180         if (iface.isInstance(this)) {
181             return iface.cast(this);
182         }
183 
184         throw new SQLException(JdbcI18n.classDoesNotImplementInterface.text());
185     }
186 
187 }