[jboss-cvs] JBossAS SVN: r72345 - branches/Branch_4_2/server/src/main/org/jboss/ejb/plugins/cmp/jdbc.
jboss-cvs-commits at lists.jboss.org
jboss-cvs-commits at lists.jboss.org
Thu Apr 17 08:51:18 EDT 2008
Author: jesper.pedersen
Date: 2008-04-17 08:51:18 -0400 (Thu, 17 Apr 2008)
New Revision: 72345
Modified:
branches/Branch_4_2/server/src/main/org/jboss/ejb/plugins/cmp/jdbc/JDBCStartCommand.java
Log:
[JBAS-4431] Allow FK index creation during startup (CMP2)
[JBAS-5459] JDBCStartCommand fails with index already exists
Modified: branches/Branch_4_2/server/src/main/org/jboss/ejb/plugins/cmp/jdbc/JDBCStartCommand.java
===================================================================
--- branches/Branch_4_2/server/src/main/org/jboss/ejb/plugins/cmp/jdbc/JDBCStartCommand.java 2008-04-17 12:47:59 UTC (rev 72344)
+++ branches/Branch_4_2/server/src/main/org/jboss/ejb/plugins/cmp/jdbc/JDBCStartCommand.java 2008-04-17 12:51:18 UTC (rev 72345)
@@ -249,13 +249,12 @@
{
JDBCAbstractCMRFieldBridge cmrField = cmrFields[i];
JDBCRelationMetaData relationMetaData = cmrField.getMetaData().getRelationMetaData();
+ DataSource dataSource = relationMetaData.getDataSource();
// if the table for the related entity has been created
final EntityBridge relatedEntity = cmrField.getRelatedEntity();
if(relationMetaData.isTableMappingStyle() && createdTables.contains(relatedEntity.getEntityName()))
{
- DataSource dataSource = relationMetaData.getDataSource();
-
boolean relTableExisted = SQLUtil.tableExists(cmrField.getQualifiedTableName(), entity.getDataSource());
if(relTableExisted)
@@ -323,8 +322,6 @@
{
log.debug("Relation table not created as requested: " + cmrField.getQualifiedTableName());
}
- // create Indices if needed
- createCMRIndex(dataSource, cmrField);
if(relationMetaData.getCreateTable())
{
@@ -334,32 +331,32 @@
}
}
}
+ if(createdTables.contains(relatedEntity.getEntityName()) || relationMetaData.getAlterTable())
+ {
+ // create Indices if needed
+ createCMRIndex(dataSource, cmrField);
+ }
}
}
public void addForeignKeyConstraints() throws DeploymentException
{
- // Create table if necessary
Set createdTables = getCreatedTables(manager);
JDBCAbstractCMRFieldBridge[] cmrFields = entity.getCMRFields();
for(int i = 0; i < cmrFields.length; ++i)
{
JDBCAbstractCMRFieldBridge cmrField = cmrFields[i];
+ EntityBridge relatedEntity = cmrField.getRelatedEntity();
JDBCRelationMetaData relationMetaData = cmrField.getMetaData().getRelationMetaData();
- // if the table for the related entity has been created
- final EntityBridge relatedEntity = cmrField.getRelatedEntity();
-
- // Only generate indices on foreign key columns if
- // the table was freshly created. If not, we risk
- // creating an index twice and get an exception from the DB
- if(relationMetaData.isForeignKeyMappingStyle() && createdTables.contains(relatedEntity.getEntityName()))
+ if(relationMetaData.isForeignKeyMappingStyle() && (createdTables.contains(relatedEntity.getEntityName()) ||
+ relationMetaData.getAlterTable()))
{
- createCMRIndex(((JDBCAbstractEntityBridge)relatedEntity).getDataSource(), cmrField);
+ createCMRIndex(((JDBCAbstractEntityBridge)cmrField.getRelatedEntity()).getDataSource(), cmrField);
}
- // Create my fk constraint
+ // Create fk constraint
addForeignKeyConstraint(cmrField);
}
}
@@ -424,58 +421,23 @@
}
return true;
-/*
-previous implementation
- ArrayList idxAscDesc = oldIndexes.getColumnAscDesc();
+ }
- // search for for column in index
- if(idxAscDesc != null)
+ /**
+ * Check whether a required index already exists on a table
+ *
+ * @param oldIndexes list of existing indexes
+ * @param column column we test the existence of an index for
+ * @return True if the column has an index; otherwise false
+ */
+ private boolean hasIndex(SQLUtil.OldIndexes oldIndexes, String column)
+ {
+ ArrayList idxColumns = oldIndexes.getColumnNames();
+ if(columnIndex(idxColumns, column) == -1)
{
- for(int i = 0; i < idxColumns.size(); i++)
- {
- // only match ascending columns
- if("A".equals(idxAscDesc.get(i)))
- {
- String name = columns[0];
- String testCol = (String) idxColumns.get(i);
- if(testCol.charAt(0) == '\"' &&
- testCol.charAt(testCol.charAt(testCol.length() - 1)) == '\"')
- {
- testCol = testCol.substring(1, testCol.length() - 1);
- }
-
- if(testCol.equalsIgnoreCase(name))
- {
- // first column matches, now check the others
- String idxName = (String) idxNames.get(i);
- int j = 1;
- for(; j < columns.length; j++)
- {
- name = columns[j];
- testCol = (String) idxColumns.get(i + j);
- if(testCol.charAt(0) == '\"' &&
- testCol.charAt(testCol.charAt(testCol.length() - 1)) == '\"')
- {
- testCol = testCol.substring(1, testCol.length() - 1);
- }
-
- String testName = (String) idxNames.get(i + j);
- if(!(testName.equals(idxName)
- &&
- testCol.equalsIgnoreCase(name)
- && idxAscDesc.get(i + j).equals("A")))
- {
- break;
- }
- }
- // if they all matched -> found
- if(j == columns.length) return true;
- }
- }
- }
+ return false;
}
- return false;
-*/
+ return true;
}
private int columnIndex(ArrayList idxColumns, String column)
@@ -484,11 +446,14 @@
{
String idxColumn = (String)idxColumns.get(j);
idxColumn = idxColumn.trim();
- while(idxColumn.charAt(0) == '\"' &&
- idxColumn.charAt(idxColumn.charAt(idxColumn.length() - 1)) == '\"')
+ while(idxColumn.startsWith("\""))
{
- idxColumn = idxColumn.substring(1, idxColumn.length() - 1);
+ idxColumn = idxColumn.substring(1);
}
+ while(idxColumn.endsWith("\""))
+ {
+ idxColumn = idxColumn.substring(0, idxColumn.length() - 1);
+ }
if(idxColumn.equalsIgnoreCase(column))
{
@@ -952,6 +917,8 @@
private void createFKIndex(JDBCRelationshipRoleMetaData metadata, DataSource dataSource, String tableName)
throws DeploymentException
{
+ SQLUtil.OldIndexes oldIndexes = SQLUtil.getOldIndexes(tableName, dataSource);
+
Collection kfl = metadata.getKeyFields();
Iterator it = kfl.iterator();
while(it.hasNext())
@@ -959,8 +926,11 @@
JDBCCMPFieldMetaData fi = (JDBCCMPFieldMetaData) it.next();
if(metadata.isIndexed())
{
- createIndex(dataSource, tableName, fi.getFieldName(), createIndexSQL(fi, tableName));
- idxCount++;
+ if(!hasIndex(oldIndexes, fi.getColumnName()))
+ {
+ createIndex(dataSource, tableName, fi.getFieldName(), createIndexSQL(fi, tableName));
+ idxCount++;
+ }
}
}
}
More information about the jboss-cvs-commits
mailing list