/** relationship flags (default to "don't enforce") */
private int _flags = RelationshipImpl.NO_REFERENTIAL_INTEGRITY_FLAG;
- private String _toTable;
private String _fromTable;
- private List<String> _toCols = new ArrayList<String>();
+ private String _toTable;
private List<String> _fromCols = new ArrayList<String>();
+ private List<String> _toCols = new ArrayList<String>();
- public RelationshipBuilder(String toTable, String fromTable)
+ public RelationshipBuilder(String fromTable, String toTable)
{
- _toTable = toTable;
_fromTable = fromTable;
+ _toTable = toTable;
}
/**
* Adds a pair of columns to the relationship.
*/
- public RelationshipBuilder addColumns(String toCol, String fromCol) {
- _toCols.add(toCol);
+ public RelationshipBuilder addColumns(String fromCol, String toCol) {
_fromCols.add(fromCol);
+ _toCols.add(toCol);
return this;
}
/**
* Enables referential integrity enforcement for this relationship.
*
- * Note, this requires the "to" table to have an existing unique index on
+ * Note, this requires the "from" table to have an existing unique index on
* the relevant columns.
*/
public RelationshipBuilder setReferentialIntegrity() {
}
/**
- * Enables deletes to be cascaded from the "to" table to the "from" table.
+ * Enables deletes to be cascaded from the "from" table to the "to" table.
*
* Note, this requires referential integrity to be enforced.
*/
}
/**
- * Enables updates to be cascaded from the "to" table to the "from" table.
+ * Enables updates to be cascaded from the "from" table to the "to" table.
*
* Note, this requires referential integrity to be enforced.
*/
}
/**
- * Enables deletes in the "to" table to be cascaded as "null" to the "from"
+ * Enables deletes in the "from" table to be cascaded as "null" to the "to"
* table.
*
* Note, this requires referential integrity to be enforced.
return _flags;
}
- public String getToTable() {
- return _toTable;
- }
-
public String getFromTable() {
return _fromTable;
}
- public List<String> getToColumns() {
- return _toCols;
+ public String getToTable() {
+ return _toTable;
}
public List<String> getFromColumns() {
return _fromCols;
}
+
+ public List<String> getToColumns() {
+ return _toCols;
+ }
/**
* Creates a new Relationship in the given Database with the currently
public RelationshipImpl createRelationshipImpl(String name) {
RelationshipImpl newRel = new RelationshipImpl(
- name, _secondaryTable, _primaryTable, _flags,
- _secondaryCols, _primaryCols);
+ name, _primaryTable, _secondaryTable, _flags,
+ _primaryCols, _secondaryCols);
return newRel;
}
private void validate() throws IOException {
- _primaryTable = getDatabase().getTable(_relationship.getToTable());
- _secondaryTable = getDatabase().getTable(_relationship.getFromTable());
+ _primaryTable = getDatabase().getTable(_relationship.getFromTable());
+ _secondaryTable = getDatabase().getTable(_relationship.getToTable());
if((_primaryTable == null) || (_secondaryTable == null)) {
throw new IllegalArgumentException(withErrorContext(
"Two valid tables are required in relationship"));
}
- _primaryCols = getColumns(_primaryTable, _relationship.getToColumns());
- _secondaryCols = getColumns(_secondaryTable, _relationship.getFromColumns());
+ _primaryCols = getColumns(_primaryTable, _relationship.getFromColumns());
+ _secondaryCols = getColumns(_secondaryTable, _relationship.getToColumns());
if((_primaryCols == null) || (_primaryCols.isEmpty()) ||
(_secondaryCols == null) || (_secondaryCols.isEmpty())) {
Object[] entryValues = new Object[_secondaryCols.size()];
for(Row row : _secondaryTable.newCursor().toCursor()
.newIterable().addColumns(_secondaryCols)) {
- // grab the from table values
+ // grab the secondary table values
boolean hasValues = false;
for(int i = 0; i < _secondaryCols.size(); ++i) {
entryValues[i] = _secondaryCols.get(i).getRowValue(row);
continue;
}
+ // check that they exist in the primary table
if(!primaryCursor.findFirstRowByEntry(entryValues)) {
throw new ConstraintViolationException(withErrorContext(
"Integrity constraint violation found for relationship"));
private String withErrorContext(String msg) {
return msg + "(Rel=" +
getTableErrorContext(_primaryTable, _primaryCols,
- _relationship.getToTable(),
- _relationship.getToColumns()) + " <- " +
- getTableErrorContext(_secondaryTable, _secondaryCols,
_relationship.getFromTable(),
- _relationship.getFromColumns()) + ")";
+ _relationship.getFromColumns()) + " -> " +
+ getTableErrorContext(_secondaryTable, _secondaryCols,
+ _relationship.getToTable(),
+ _relationship.getToColumns()) + ")";
}
}