📄 interbasemodelreader.java
字号:
package org.apache.ddlutils.platform.interbase;
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.sql.Types;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.apache.ddlutils.Platform;
import org.apache.ddlutils.model.Column;
import org.apache.ddlutils.model.ForeignKey;
import org.apache.ddlutils.model.Index;
import org.apache.ddlutils.model.Table;
import org.apache.ddlutils.model.TypeMap;
import org.apache.ddlutils.platform.DatabaseMetaDataWrapper;
import org.apache.ddlutils.platform.JdbcModelReader;
/**
* The Jdbc Model Reader for Interbase.
*
* @version $Revision: $
*/
public class InterbaseModelReader extends JdbcModelReader
{
/**
* Creates a new model reader for Interbase databases.
*
* @param platform The platform that this model reader belongs to
*/
public InterbaseModelReader(Platform platform)
{
super(platform);
setDefaultCatalogPattern(null);
setDefaultSchemaPattern(null);
setDefaultTablePattern("%");
setDefaultColumnPattern("%");
}
/**
* {@inheritDoc}
*/
protected Table readTable(DatabaseMetaDataWrapper metaData, Map values) throws SQLException
{
Table table = super.readTable(metaData, values);
if (table != null)
{
determineExtraColumnInfo(table);
determineAutoIncrementColumns(table);
adjustColumns(table);
}
return table;
}
/**
* {@inheritDoc}
*/
protected Collection readColumns(DatabaseMetaDataWrapper metaData, String tableName) throws SQLException
{
ResultSet columnData = null;
try
{
List columns = new ArrayList();
if (getPlatform().isDelimitedIdentifierModeOn())
{
// Jaybird has a problem when delimited identifiers are used as
// it is not able to find the columns for the table
// So we have to filter manually below
columnData = metaData.getColumns(getDefaultTablePattern(), getDefaultColumnPattern());
while (columnData.next())
{
Map values = readColumns(columnData, getColumnsForColumn());
if (tableName.equals(values.get("TABLE_NAME")))
{
columns.add(readColumn(metaData, values));
}
}
}
else
{
columnData = metaData.getColumns(tableName, getDefaultColumnPattern());
while (columnData.next())
{
Map values = readColumns(columnData, getColumnsForColumn());
columns.add(readColumn(metaData, values));
}
}
return columns;
}
finally
{
if (columnData != null)
{
columnData.close();
}
}
}
/**
* Helper method that determines extra column info from the system tables: default value, precision, scale.
*
* @param table The table
*/
protected void determineExtraColumnInfo(Table table) throws SQLException
{
StringBuffer query = new StringBuffer();
query.append("SELECT a.RDB$FIELD_NAME, a.RDB$DEFAULT_SOURCE, b.RDB$FIELD_PRECISION, b.RDB$FIELD_SCALE,");
query.append(" b.RDB$FIELD_TYPE, b.RDB$FIELD_SUB_TYPE FROM RDB$RELATION_FIELDS a, RDB$FIELDS b");
query.append(" WHERE a.RDB$RELATION_NAME=? AND a.RDB$FIELD_SOURCE=b.RDB$FIELD_NAME");
PreparedStatement prepStmt = getConnection().prepareStatement(query.toString());
try
{
prepStmt.setString(1, getPlatform().isDelimitedIdentifierModeOn() ? table.getName() : table.getName().toUpperCase());
ResultSet rs = prepStmt.executeQuery();
while (rs.next())
{
String columnName = rs.getString(1).trim();
Column column = table.findColumn(columnName, getPlatform().isDelimitedIdentifierModeOn());
if (column != null)
{
String defaultValue = rs.getString(2);
if (!rs.wasNull() && (defaultValue != null))
{
defaultValue = defaultValue.trim();
if (defaultValue.startsWith("DEFAULT "))
{
defaultValue = defaultValue.substring("DEFAULT ".length());
}
column.setDefaultValue(defaultValue);
}
short precision = rs.getShort(3);
boolean precisionSpecified = !rs.wasNull();
short scale = rs.getShort(4);
boolean scaleSpecified = !rs.wasNull();
if (precisionSpecified)
{
// for some reason, Interbase stores the negative scale
column.setSizeAndScale(precision, scaleSpecified ? -scale : 0);
}
short dbType = rs.getShort(5);
short blobSubType = rs.getShort(6);
// CLOBs are returned by the driver as VARCHAR
if (!rs.wasNull() && (dbType == 261) && (blobSubType == 1))
{
column.setTypeCode(Types.CLOB);
}
}
}
rs.close();
}
finally
{
prepStmt.close();
}
}
/**
* Helper method that determines the auto increment status using Interbase's system tables.
*
* @param table The table
*/
protected void determineAutoIncrementColumns(Table table) throws SQLException
{
// Since for long table and column names, the generator name will be shortened
// we have to determine for each column whether there is a generator for it
InterbaseBuilder builder = (InterbaseBuilder)getPlatform().getSqlBuilder();
Column[] columns = table.getColumns();
HashMap names = new HashMap();
String name;
for (int idx = 0; idx < columns.length; idx++)
{
name = builder.getGeneratorName(table, columns[idx]);
if (!getPlatform().isDelimitedIdentifierModeOn())
{
name = name.toUpperCase();
}
names.put(name, columns[idx]);
}
Statement stmt = getConnection().createStatement();
try
{
ResultSet rs = stmt.executeQuery("SELECT RDB$GENERATOR_NAME FROM RDB$GENERATORS");
while (rs.next())
{
String generatorName = rs.getString(1).trim();
Column column = (Column)names.get(generatorName);
if (column != null)
{
column.setAutoIncrement(true);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -