View Javadoc
1   package edu.internet2.middleware.grouper.ext.org.apache.ddlutils.platform.mckoi;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with 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,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import java.sql.ResultSet;
23  import java.sql.SQLException;
24  import java.sql.Statement;
25  import java.util.HashMap;
26  import java.util.Map;
27  
28  import org.apache.commons.collections.map.ListOrderedMap;
29  import edu.internet2.middleware.grouper.ext.org.apache.ddlutils.Platform;
30  import edu.internet2.middleware.grouper.ext.org.apache.ddlutils.model.Column;
31  import edu.internet2.middleware.grouper.ext.org.apache.ddlutils.model.Table;
32  import edu.internet2.middleware.grouper.ext.org.apache.ddlutils.model.TypeMap;
33  import edu.internet2.middleware.grouper.ext.org.apache.ddlutils.platform.DatabaseMetaDataWrapper;
34  import edu.internet2.middleware.grouper.ext.org.apache.ddlutils.platform.JdbcModelReader;
35  
36  /**
37   * Reads a database model from a Mckoi database.
38   *
39   * @version $Revision: $
40   */
41  public class MckoiModelReader extends JdbcModelReader
42  {
43      /**
44       * Creates a new model reader for Mckoi databases.
45       * 
46       * @param platform The platform that this model reader belongs to
47       */
48      public MckoiModelReader(Platform platform)
49      {
50          super(platform);
51          setDefaultCatalogPattern(null);
52          setDefaultSchemaPattern(null);
53      }
54  
55      /**
56       * {@inheritDoc}
57       */
58      protected Table readTable(DatabaseMetaDataWrapper metaData, Map values) throws SQLException
59      {
60          Table table = super.readTable(metaData, values);
61  
62          if (table != null)
63          {
64              // Mckoi does not currently return unique indices in the metadata so we have to query
65              // internal tables to get this info
66              StringBuffer query = new StringBuffer();
67          
68              query.append("SELECT uniqueColumns.column, uniqueColumns.seq_no, uniqueInfo.name");
69              query.append(" FROM SYS_INFO.sUSRUniqueColumns uniqueColumns, SYS_INFO.sUSRUniqueInfo uniqueInfo");
70              query.append(" WHERE uniqueColumns.un_id = uniqueInfo.id AND uniqueInfo.table = '");
71              query.append(table.getName());
72              if (table.getSchema() != null)
73              {
74                  query.append("' AND uniqueInfo.schema = '");
75                  query.append(table.getSchema());
76              }
77              query.append("'");
78          
79              Statement stmt        = getConnection().createStatement();
80              ResultSet resultSet   = stmt.executeQuery(query.toString());
81              Map       indices     = new ListOrderedMap();
82              Map       indexValues = new HashMap();
83          
84              indexValues.put("NON_UNIQUE", Boolean.FALSE);
85              while (resultSet.next())
86              {
87                  indexValues.put("COLUMN_NAME",      resultSet.getString(1));
88                  indexValues.put("ORDINAL_POSITION", new Short(resultSet.getShort(2)));
89                  indexValues.put("INDEX_NAME",       resultSet.getString(3));
90          
91                  readIndex(metaData, indexValues, indices);
92              }
93              resultSet.close();
94          
95              table.addIndices(indices.values());
96          }
97          
98          return table;
99      }
100 
101     /**
102      * {@inheritDoc}
103      */
104     protected Column readColumn(DatabaseMetaDataWrapper metaData, Map values) throws SQLException
105     {
106         Column column = super.readColumn(metaData, values);
107 
108         if (column.getSize() != null)
109         {
110             if (column.getSizeAsInt() <= 0)
111             {
112                 column.setSize(null);
113             }
114         }
115 
116         String defaultValue = column.getDefaultValue();
117 
118         if (defaultValue != null)
119         {
120             if (defaultValue.toLowerCase().startsWith("nextval('") ||
121                 defaultValue.toLowerCase().startsWith("uniquekey('"))
122             {
123                 column.setDefaultValue(null);
124                 column.setAutoIncrement(true);
125             }
126             else if (TypeMap.isTextType(column.getTypeCode()))
127             {
128                 column.setDefaultValue(unescape(column.getDefaultValue(), "'", "\\'"));
129             }
130         }
131         return column;
132     }
133 }