Skip to content

Commit

Permalink
Closes #43: Equality expression not reversible in OneToMany
Browse files Browse the repository at this point in the history
  • Loading branch information
mrico committed Jan 21, 2011
1 parent 9dec46f commit 8235eee
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 19 deletions.
34 changes: 17 additions & 17 deletions src/main/scala/org/squeryl/dsl/QueryDsl.scala
Original file line number Diff line number Diff line change
Expand Up @@ -322,23 +322,10 @@ trait QueryDsl
_.selectElement.origin.asInstanceOf[ViewExpressionNode[_]].view == v
).headOption != None

/**
* returns a (FieldMetaData, FieldMetaData) where ._1 is the id of the KeyedEntity on the left or right side,
* and where ._2 is the foreign key of the association object/table
*/
private def _splitEquality(ee: EqualityExpression) =
if(ee.left._fieldMetaData.parentMetaData.clasz == aClass) {
assert(ee.right._fieldMetaData.isIdFieldOfKeyedEntity)
(ee.right._fieldMetaData, ee.left._fieldMetaData)
}
else {
assert(ee.left._fieldMetaData.isIdFieldOfKeyedEntity)
(ee.left._fieldMetaData, ee.right._fieldMetaData)
}

private val (leftPkFmd, leftFkFmd) = _splitEquality(_leftEqualityExpr)
private val (leftPkFmd, leftFkFmd) = _splitEquality(_leftEqualityExpr, thisTable)

private val (rightPkFmd, rightFkFmd) = _splitEquality(_rightEqualityExpr)
private val (rightPkFmd, rightFkFmd) = _splitEquality(_rightEqualityExpr, thisTable)

val leftForeignKeyDeclaration =
schema._createForeignKeyDeclaration(leftFkFmd.columnName, leftPkFmd.columnName)
Expand Down Expand Up @@ -526,8 +513,7 @@ trait QueryDsl
//that refer to FieldSelectElement, who in turn refer to the FieldMetaData

// now the Tuple with the left and right FieldMetaData
(ee_.left.asInstanceOf[SelectElementReference[_]].selectElement.asInstanceOf[FieldSelectElement].fieldMataData,
ee_.right.asInstanceOf[SelectElementReference[_]].selectElement.asInstanceOf[FieldSelectElement].fieldMataData)
_splitEquality(ee.get, rightTable)
}

val foreignKeyDeclaration =
Expand Down Expand Up @@ -577,6 +563,20 @@ trait QueryDsl
}
}

/**
* returns a (FieldMetaData, FieldMetaData) where ._1 is the id of the KeyedEntity on the left or right side,
* and where ._2 is the foreign key of the association object/table
*/
private def _splitEquality(ee: EqualityExpression, rightTable: Table[_]) =
if(ee.left._fieldMetaData.parentMetaData.clasz == rightTable.classOfT) {
assert(ee.right._fieldMetaData.isIdFieldOfKeyedEntity)
(ee.right._fieldMetaData, ee.left._fieldMetaData)
}
else {
assert(ee.left._fieldMetaData.isIdFieldOfKeyedEntity)
(ee.left._fieldMetaData, ee.right._fieldMetaData)
}

// Composite key syntactic sugar :

def compositeKey[A1,A2](a1: A1, a2: A2) =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,15 +59,15 @@ object SchoolDb2 extends Schema {

val courseAssignments =
manyToManyRelation(professors, courses).
via[CourseAssignment]((p,c,a) => (p.id === a.professorId, a.courseId === c.id))
via[CourseAssignment]((p,c,a) => (a.professorId === p.id, a.courseId === c.id))

val courseSubscriptions =
manyToManyRelation(courses, students).
via[CourseSubscription]((c,s,cs) => (cs.studentId === s.id, c.id === cs.courseId))

val subjectToCourses =
oneToManyRelation(subjects, courses).
via((s,c) => s.id === c.subjectId)
via((s,c) => c.subjectId === s.id)

// the default constraint for all foreign keys in this schema :
override def applyDefaultForeignKeyPolicy(foreignKeyDeclaration: ForeignKeyDeclaration) =
Expand Down

0 comments on commit 8235eee

Please sign in to comment.