001/*
002 * ModeShape (http://www.modeshape.org)
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *       http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016package org.modeshape.jdbc.metadata;
017
018import java.sql.Connection;
019import java.util.HashMap;
020import java.util.Map;
021import org.modeshape.jdbc.JcrType;
022
023/**
024 */
025public class MetadataProvider {
026
027    // Map of detail maps -- <columnIndex, Map<propertyName, metadataObject>>
028    protected Map<?, Object>[] metadata;
029
030    public MetadataProvider( Map<?, Object>[] metadata ) {
031        this.metadata = metadata;
032    }
033
034    public Object getValue( int columnIndex,
035                            Integer metadataPropertyKey ) {
036        if (columnIndex < 0 || columnIndex >= metadata.length) {
037            assert (columnIndex < 0 || columnIndex >= metadata.length);
038        }
039
040        Map<?, Object> column = this.metadata[columnIndex];
041        return column.get(metadataPropertyKey);
042    }
043
044    public int getColumnCount() {
045        return metadata.length;
046    }
047
048    public String getStringValue( int columnIndex,
049                                  Integer metadataPropertyKey ) {
050        return (String)getValue(columnIndex, metadataPropertyKey);
051    }
052
053    public int getIntValue( int columnIndex,
054                            Integer metadataPropertyKey ) {
055        return ((Integer)getValue(columnIndex, metadataPropertyKey)).intValue();
056    }
057
058    public boolean getBooleanValue( int columnIndex,
059                                    Integer metadataPropertyKey ) {
060        return ((Boolean)getValue(columnIndex, metadataPropertyKey)).booleanValue();
061    }
062
063    public static Map<Integer, Object> getColumnMetadata( String catalogName,
064                                                          String tableName,
065                                                          String columnName,
066                                                          String dataType,
067                                                          Integer nullable,
068                                                          Connection driverConnection ) {
069        return getColumnMetadata(catalogName,
070                                 tableName,
071                                 columnName,
072                                 dataType,
073                                 nullable,
074                                 ResultsMetadataConstants.SEARCH_TYPES.UNSEARCHABLE,
075                                 Boolean.FALSE,
076                                 Boolean.FALSE,
077                                 Boolean.FALSE,
078                                 driverConnection);
079    }
080
081    public static Map<Integer, Object> getColumnMetadata( String catalogName,
082                                                          String tableName,
083                                                          String columnName,
084                                                          String dataType,
085                                                          Integer nullable,
086                                                          Integer searchable,
087                                                          Boolean writable,
088                                                          Boolean signed,
089                                                          Boolean caseSensitive,
090                                                          Connection driverConnection ) {
091
092        // map that would contain metadata details
093        Map<Integer, Object> metadataMap = new HashMap<Integer, Object>();
094
095        /*******************************************************
096         * HardCoding Column metadata details for the given column
097         ********************************************************/
098
099        JcrType type = JcrType.typeInfo(dataType);
100        if (type == null) {
101            throw new RuntimeException("Program error: jcr type " + dataType + " not found");
102        }
103
104        metadataMap.put(ResultsMetadataConstants.CATALOG, catalogName);
105        metadataMap.put(ResultsMetadataConstants.TABLE, tableName);
106        metadataMap.put(ResultsMetadataConstants.COLUMN, columnName);
107        metadataMap.put(ResultsMetadataConstants.DATA_TYPE, dataType);
108        metadataMap.put(ResultsMetadataConstants.PRECISION, type.getDefaultPrecision());
109        metadataMap.put(ResultsMetadataConstants.RADIX, new Integer(10));
110        metadataMap.put(ResultsMetadataConstants.SCALE, new Integer(0));
111        metadataMap.put(ResultsMetadataConstants.AUTO_INCREMENTING, Boolean.FALSE);
112        metadataMap.put(ResultsMetadataConstants.CASE_SENSITIVE, caseSensitive);
113        metadataMap.put(ResultsMetadataConstants.NULLABLE, nullable);
114        metadataMap.put(ResultsMetadataConstants.SEARCHABLE, searchable);
115        metadataMap.put(ResultsMetadataConstants.SIGNED, signed);
116        metadataMap.put(ResultsMetadataConstants.WRITABLE, writable);
117        metadataMap.put(ResultsMetadataConstants.CURRENCY, Boolean.FALSE);
118        metadataMap.put(ResultsMetadataConstants.DISPLAY_SIZE, type.getNominalDisplaySize());
119
120        return metadataMap;
121    }
122
123}