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  
30  import org.modeshape.jdbc.JcrType;
31  import org.modeshape.jdbc.JdbcI18n;
32  import org.modeshape.jdbc.metadata.MetadataProvider;
33  import org.modeshape.jdbc.metadata.ResultsMetadataConstants;
34  
35  /**
36   * 
37   */
38  public class ResultSetMetaDataImpl implements ResultSetMetaData {
39  
40      private MetadataProvider provider;
41      
42      public ResultSetMetaDataImpl(MetadataProvider provider) {
43      	this.provider = provider;
44      }
45      
46      /**
47       * Adjust from 1-based to internal 0-based representation
48       * @param index External 1-based representation
49       * @return Internal 0-based representation
50       */
51      private int adjustColumn(int index) {
52      	return index-1;
53      }
54  
55      public int getColumnCount() throws SQLException {
56          return provider.getColumnCount();
57      }
58  
59      public boolean isAutoIncrement(int index) throws SQLException {
60          return provider.getBooleanValue(adjustColumn(index), ResultsMetadataConstants.AUTO_INCREMENTING);
61      }
62  
63      public boolean isCaseSensitive(int index) throws SQLException {
64          return provider.getBooleanValue(adjustColumn(index), ResultsMetadataConstants.CASE_SENSITIVE);
65      }
66  
67      public boolean isSearchable(int index) throws SQLException {
68          Integer searchable = (Integer) provider.getValue(adjustColumn(index), ResultsMetadataConstants.SEARCHABLE);
69          return !(ResultsMetadataConstants.SEARCH_TYPES.UNSEARCHABLE.equals(searchable));
70      }
71  
72      public boolean isCurrency(int index) throws SQLException {
73          return provider.getBooleanValue(adjustColumn(index), ResultsMetadataConstants.CURRENCY);
74      }
75  
76      public int isNullable(int index) throws SQLException {
77          Object nullable = provider.getValue(adjustColumn(index), ResultsMetadataConstants.NULLABLE);
78          if(nullable.equals(ResultsMetadataConstants.NULL_TYPES.NULLABLE)) {
79              return columnNullable;    
80          } else if(nullable.equals(ResultsMetadataConstants.NULL_TYPES.NOT_NULL)) {
81              return columnNoNulls;
82          } else {
83              return columnNullableUnknown;
84          }
85      }
86                          
87      public boolean isSigned(int index) throws SQLException {
88          return provider.getBooleanValue(adjustColumn(index), ResultsMetadataConstants.SIGNED);
89      }
90  
91      public int getColumnDisplaySize(int index) throws SQLException {
92          return provider.getIntValue(adjustColumn(index), ResultsMetadataConstants.DISPLAY_SIZE);
93      }
94  
95      public String getColumnLabel(int index) throws SQLException {
96          return provider.getStringValue(adjustColumn(index), ResultsMetadataConstants.COLUMN_LABEL);
97      }
98  
99      public String getColumnName(int index) throws SQLException {
100         return provider.getStringValue(adjustColumn(index), ResultsMetadataConstants.COLUMN);
101     }
102 
103     public String getSchemaName(int index) throws SQLException {
104         String name = provider.getStringValue(adjustColumn(index), ResultsMetadataConstants.SCHEMA);
105         if (name != null) {
106         	int dotIndex = name.indexOf('.');
107         	if (dotIndex != -1) {
108         		return name.substring(0, dotIndex);
109         	}
110         }
111         return null;
112     }
113 
114     public int getPrecision(int index) throws SQLException {
115         return provider.getIntValue(adjustColumn(index), ResultsMetadataConstants.PRECISION);
116     }
117 
118     public int getScale(int index) throws SQLException {
119         return provider.getIntValue(adjustColumn(index), ResultsMetadataConstants.SCALE);
120     }
121 
122     public String getTableName(int index) throws SQLException {
123         String name = provider.getStringValue(adjustColumn(index), ResultsMetadataConstants.TABLE);
124         if (name != null) {
125         	int dotIndex = name.indexOf('.');
126         	if (dotIndex != -1) {
127         		return name.substring(dotIndex + 1);
128         	}
129         }
130         return name;
131     }
132 
133     public String getCatalogName(int index) throws SQLException {
134     	return provider.getStringValue(adjustColumn(index), ResultsMetadataConstants.CATALOG);
135     }
136 
137     public int getColumnType(int index) throws SQLException {
138         String runtimeTypeName = provider.getStringValue(adjustColumn(index), ResultsMetadataConstants.DATA_TYPE);
139         
140         JcrType typeInfo = JcrType.typeInfo(runtimeTypeName);
141         return typeInfo != null ? typeInfo.getJdbcType() : Types.VARCHAR;
142     }
143 
144     public String getColumnTypeName(int index) throws SQLException {
145         return provider.getStringValue(adjustColumn(index), ResultsMetadataConstants.DATA_TYPE);
146     }
147 
148     public boolean isReadOnly(int index) throws SQLException {
149         return ! provider.getBooleanValue(adjustColumn(index), ResultsMetadataConstants.WRITABLE);
150     }
151 
152     public boolean isWritable(int index) throws SQLException {
153         return provider.getBooleanValue(adjustColumn(index), ResultsMetadataConstants.WRITABLE);
154     }
155 
156     public boolean isDefinitelyWritable(int index) throws SQLException {
157         return provider.getBooleanValue(adjustColumn(index), ResultsMetadataConstants.WRITABLE);
158     }
159 
160     public String getColumnClassName(int index) throws SQLException {
161        	JcrType typeInfo = JcrType.typeInfo(getColumnTypeName(index));
162         return typeInfo != null ? typeInfo.getRepresentationClass().getName() : String.class.getName();
163     }
164     
165     /**
166      * {@inheritDoc}
167      * 
168      * @see java.sql.Wrapper#isWrapperFor(java.lang.Class)
169      */
170     @Override
171     public boolean isWrapperFor( Class<?> iface ) {
172         return iface.isInstance(this);
173     }
174 
175     /**
176      * {@inheritDoc}
177      * 
178      * @see java.sql.Wrapper#unwrap(java.lang.Class)
179      */
180     @Override
181     public <T> T unwrap( Class<T> iface ) throws SQLException {
182         if (iface.isInstance(this)) {
183             return iface.cast(this);
184         }
185 
186         throw new SQLException(JdbcI18n.classDoesNotImplementInterface.text());
187     }
188 
189 }
190