_toCursor = toCursor;
}
+ /**
+ * Creates a new Joiner based on the foreign-key relationship between the
+ * given "from"" table and the given "to"" table.
+ *
+ * @param fromTable the "from" side of the relationship
+ * @param toTable the "to" side of the relationship
+ * @throws IllegalArgumentException if there is no relationship between the
+ * given tables
+ */
+ public static Joiner create(Table fromTable, Table toTable)
+ throws IOException
+ {
+ return create(fromTable.getForeignKeyIndex(toTable));
+ }
+
/**
* Creates a new Joiner based on the given index which backs a foreign-key
* relationship. The table of the given index will be the "from" table and
- * the table on the other end of the relationsip is the "to" table.
+ * the table on the other end of the relationship will be the "to" table.
*
* @param fromIndex the index backing one side of a foreign-key relationship
*/
toCursor.setColumnMatcher(CaseInsensitiveColumnMatcher.INSTANCE);
return new Joiner(fromIndex, toCursor);
}
+
+ /**
+ * Creates a new Joiner that is the reverse of this Joiner (the "from" and
+ * "to" tables are swapped).
+ */
+ public Joiner createReverse()
+ throws IOException
+ {
+ return create(getToTable(), getFromTable());
+ }
public Table getFromTable()
{
/**
* @return the index with the given name
+ * @throws IllegalArgumentException if there is no index with the given name
*/
public Index getIndex(String name) {
for(Index index : _indexes) {
throw new IllegalArgumentException("Index with name " + name +
" does not exist on this table");
}
-
+
+ /**
+ * @return the primary key index for this table
+ * @throws IllegalArgumentException if there is no primary key index on this
+ * table
+ */
+ public Index getPrimaryKeyIndex() {
+ for(Index index : _indexes) {
+ if(index.isPrimaryKey()) {
+ return index;
+ }
+ }
+ throw new IllegalArgumentException("Table " + getName() +
+ " does not have a primary key index");
+ }
+
+ /**
+ * @return the foreign key index joining this table to the given other table
+ * @throws IllegalArgumentException if there is no relationship between this
+ * table and the given table
+ */
+ public Index getForeignKeyIndex(Table otherTable) {
+ for(Index index : _indexes) {
+ if(index.isForeignKey() && (index.getReference() != null) &&
+ (index.getReference().getOtherTablePageNumber() ==
+ otherTable.getTableDefPageNumber())) {
+ return index;
+ }
+ }
+ throw new IllegalArgumentException(
+ "Table " + getName() + " does not have a foreign key reference to " +
+ otherTable.getName());
+ }
+
/**
* @return All of the IndexData on this table (unmodifiable List)
*/
for (final TestDB testDB : SUPPORTED_DBS_TEST_FOR_READ) {
Table table = open(testDB).getTable("Table1");
Map<String, Boolean> foundPKs = new HashMap<String, Boolean>();
+ Index pkIndex = null;
for(Index index : table.getIndexes()) {
foundPKs.put(index.getColumns().iterator().next().getName(),
index.isPrimaryKey());
+ if(index.isPrimaryKey()) {
+ pkIndex= index;
+
+ }
}
Map<String, Boolean> expectedPKs = new HashMap<String, Boolean>();
expectedPKs.put("A", Boolean.TRUE);
expectedPKs.put("B", Boolean.FALSE);
assertEquals(expectedPKs, foundPKs);
+ assertSame(pkIndex, table.getPrimaryKeyIndex());
}
}
{
final Set<String> colNames = new HashSet<String>(
Arrays.asList("id", "data"));
-
+
+ Joiner revJoin = join.createReverse();
for(Map<String,Object> row : join.getFromTable()) {
Integer id = (Integer)row.get("id");
if(!expectedRows.isEmpty()) {
assertEquals(expectedRows.get(0), join.findFirstRow(row));
+
+ assertEquals(row, revJoin.findFirstRow(expectedRows.get(0)));
} else {
assertNull(join.findFirstRow(row));
}