Rename @Index(columns) to @Index(fields) and do the proper translation to column name during table creation / migration.

This commit is contained in:
Par Winzell
2007-08-13 22:57:09 +00:00
parent 632b8684be
commit 3a99decfce
2 changed files with 27 additions and 10 deletions
@@ -548,10 +548,6 @@ public class DepotMarshaller<T extends PersistentRecord>
ctx.invoke(new Modifier() {
public int invoke (Connection conn, DatabaseLiaison liaison) throws SQLException {
// create the table
String[] columns = new String[_columnFields.length];
for (int ii = 0; ii < columns.length; ii ++) {
columns[ii] = _fields.get(_columnFields[ii]).getColumnName();
}
String[] primaryKeyColumns = null;
if (_pkColumns != null) {
primaryKeyColumns = new String[_pkColumns.size()];
@@ -559,18 +555,23 @@ public class DepotMarshaller<T extends PersistentRecord>
primaryKeyColumns[ii] = _pkColumns.get(ii).getColumnName();
}
}
liaison.createTableIfMissing(conn, getTableName(), columns, fDeclarations,
uniqueConCols, primaryKeyColumns);
liaison.createTableIfMissing(
conn, getTableName(), fieldsToColumns(_columnFields),
fDeclarations, uniqueConCols, primaryKeyColumns);
// add its indexen
for (Index idx : indexen) {
liaison.addIndexToTable(
conn, getTableName(), idx.columns(),
conn, getTableName(), fieldsToColumns(idx.fields()),
getTableName() + "_" + idx.name(), idx.unique());
}
// its key generator
if (_keyGenerator != null) {
_keyGenerator.init(conn, liaison);
}
// and its full text search indexes
for (FullTextIndex fti : _fullTextIndexes.values()) {
builder.addFullTextSearch(conn, DepotMarshaller.this, fti);
}
@@ -702,7 +703,8 @@ public class DepotMarshaller<T extends PersistentRecord>
ctx.invoke(new Modifier() {
public int invoke (Connection conn, DatabaseLiaison liaison) throws SQLException {
liaison.addIndexToTable(
conn, getTableName(), index.columns(), ixName, index.unique());
conn, getTableName(), fieldsToColumns(index.fields()),
ixName, index.unique());
return 0;
}
});
@@ -764,6 +766,21 @@ public class DepotMarshaller<T extends PersistentRecord>
});
}
// translate an array of field names to an array of column names
protected String[] fieldsToColumns (String[] fields)
{
String[] columns = new String[fields.length];
for (int ii = 0; ii < columns.length; ii ++) {
FieldMarshaller<?> fm = _fields.get(fields[ii]);
if (fm == null) {
throw new IllegalArgumentException(
"Unknown field on record [field=" + fields[ii] + ", pClass=" + _pclass + "]");
}
columns[ii] = fm.getColumnName();
}
return columns;
}
/**
* Checks that there are no database columns for which we no longer have Java fields.
*/
@@ -37,6 +37,6 @@ public @interface Index
/** Does this index enforce a uniqueness constraint? */
boolean unique () default false;
/** Defines the columns on which the index operates. */
String[] columns () default {};
/** Defines the fields on which the index operates. */
String[] fields () default {};
}