📄 tablerelationshipsreader.java
字号:
/*
LoaderGenerator - tool for generated xml, sql and doml file needed for Octopus.
Copyright (C) 2003 Together
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
package org.webdocwf.util.loader.generator;
import java.sql.Connection;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Hashtable;
import java.util.List;
import org.webdocwf.util.loader.LoaderException;
import org.webdocwf.util.loader.logging.Logger;
import org.webdocwf.util.loader.logging.StandardLogger;
/**
*
* TableRelationshipsReader class read the data which describe database relationships
* between the tables.
* @author Radoslav Dutina
* @version 1.0
*/
public class TableRelationshipsReader {
private static List listPrimaryKeys = null;
private static List listIndexVariables = null;
private static List listForeignVariables = null;
private Hashtable htIndexAndReplacement = new Hashtable();
private RelationshipsAttributes relationshipsAttributes = new RelationshipsAttributes();
private Logger logger;
/**
* Construct object TableRelationshipsReader with associated parameters.
* @param tableName is name of the table form which we retrieve data.
* @param conn is the named connection.
* @param catalogName is the name of the current database.
*/
public TableRelationshipsReader(Connection conn, String tableName, String catalogName, InputParameters generatorParameters, int constraintCount) throws LoaderException{
setLogger();
this.logger.write("full", "TableRelationshipsReader is started.");
listPrimaryKeys = new ArrayList();
listIndexVariables = new ArrayList();
listForeignVariables = new ArrayList();
String maxConstraintLenght = generatorParameters.getMaxConstraintLength();
int maxConstraintInt = 0;
if (!maxConstraintLenght.equalsIgnoreCase("") && !maxConstraintLenght.equalsIgnoreCase("-1")) {
maxConstraintInt = Integer.parseInt(maxConstraintLenght);
}
// int constraintCount=0;
try {
boolean unique = false;
boolean app = false;
ResultSet rs2 = conn.getMetaData().getIndexInfo(catalogName, null, tableName, false, true);
ResultSet rs3 = conn.getMetaData().getPrimaryKeys(catalogName, null, tableName);
ResultSet rs5 = conn.getMetaData().getImportedKeys(catalogName, null, tableName);
while (rs3.next()) {
//primaryKeys
String primaryKeyName = rs3.getString(6);
if (maxConstraintInt > 0 && primaryKeyName.length() > maxConstraintInt) {
String newName = primaryKeyName.substring(0, maxConstraintInt - String.valueOf(constraintCount).length());
primaryKeyName = newName + String.valueOf(constraintCount);
constraintCount++;
}
listPrimaryKeys.add(primaryKeyName);
//columnName
listPrimaryKeys.add(rs3.getString(4));
//TODO ADDED logic for making index columns on 2,3 or more columns.
while (rs2.next()) {
if (rs2.getString(6) != null) {
if (!rs2.getString(9).equalsIgnoreCase(rs3.getString(4))) {
//unique
listIndexVariables.add(rs2.getString(4));
//index
String indexName = rs2.getString(6);
//ZK Added next line
String keyIndexName = indexName;
if (maxConstraintInt > 0 && indexName.length() > maxConstraintInt) {
String newName = indexName.substring(0, maxConstraintInt - String.valueOf(constraintCount).length());
indexName = newName + String.valueOf(constraintCount);
//constraintCount++;
//ZK 9.7 2004 Added next block of code.Fixed problem with generation of index on few columns.
if (!htIndexAndReplacement.containsKey(keyIndexName)) {
htIndexAndReplacement.put(keyIndexName, indexName);
constraintCount++;
} else {
indexName = htIndexAndReplacement.get(keyIndexName).toString();
}
//end
}
listIndexVariables.add(indexName);
//columnName
listIndexVariables.add(rs2.getString(9));
}
}
}
}
while (rs5.next()) {
//ForeignKeyTable
listForeignVariables.add(rs5.getString(7));
//foreing key restrictions (lenght)
String foreignKeyName = rs5.getString(12);
if (maxConstraintInt > 0 && foreignKeyName.length() > maxConstraintInt) {
String newName = foreignKeyName.substring(0, maxConstraintInt - String.valueOf(constraintCount).length());
foreignKeyName = newName + String.valueOf(constraintCount);
constraintCount++;
}
//RelationShipName
listForeignVariables.add(foreignKeyName);
//ForeignKeyValue
listForeignVariables.add(rs5.getString(8));
//PrimaryKeyTable
listForeignVariables.add(rs5.getString(3));
//PrimariKeyValue
listForeignVariables.add(rs5.getString(4));
}
relationshipsAttributes.setTableName(tableName);
relationshipsAttributes.setPrimaryKeys(getPrimaryKeys());
relationshipsAttributes.setIndexVariables(getIndexVariables());
relationshipsAttributes.setForeignVariables(getForeignVariables());
rs2.close();
rs3.close();
rs5.close();
} catch (Exception e) {
//e.printStackTrace();
LoaderException le = new LoaderException("Exception:",e);
this.logger.write("full", "Exception in class TableRelationshipsReader."+"\n"+le.getStackTraceAsString());
throw le;
}
this.logger.write("full", "TableRelationshipsReader is finished.");
}
/**
* This method sets the value of listPrimaryKeys parameters.
* @param primary_Keys is value of parameter.
*/
public static void setPrimaryKeys(String[] primary_Keys) {
listPrimaryKeys = Arrays.asList(primary_Keys);
}
/**
* This method read the value of listPrimaryKeys parameters.
* @return value of parameter.
*/
public static String[] getPrimaryKeys() {
String[] ret = new String[listPrimaryKeys.size()];
listPrimaryKeys.toArray(ret);
return ret;
}
/**
* This method sets the value of listIndexVariables parameters.
* @param index_Variables is value of parameter.
*/
public static void setIndexVariables(String[] index_Variables) {
listIndexVariables = Arrays.asList(index_Variables);
}
/**
* This method read the value of listIndexVariables parameters.
* @return value of parameter.
*/
public static String[] getIndexVariables() {
String[] ret = new String[listIndexVariables.size()];
listIndexVariables.toArray(ret);
return ret;
}
/**
* This method sets the value of listForeignVariables parameters.
* @param foreign_Variables is the value of parameter.
*/
public static void setForeignVariables(String[] foreign_Variables) {
listForeignVariables = Arrays.asList(foreign_Variables);
}
/**
* This method read the value of listForeignVariables parameters.
* @return value of parameter.
*/
public static String[] getForeignVariables() {
String[] ret = new String[listForeignVariables.size()];
listForeignVariables.toArray(ret);
return ret;
}
/**
* This method read the value of RelationshipsAttributes parameters.
* @return references to RelationshipsAttributes objects.
*/
public RelationshipsAttributes getRelationshipsAttributes() {
return relationshipsAttributes;
}
/**
* This method will set logger object
* @param logger
*/
private void setLogger() {
this.logger = StandardLogger.getCentralLogger();
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -