Permalink
Browse files

Fix beforeInsert entity handling

  • Loading branch information...
1 parent 496e051 commit a8d7298815217d48613ebda97044ddc9c805c1a8 @kanischev kanischev committed with davewhittaker Dec 20, 2012
Showing with 13 additions and 1 deletion.
  1. +1 −1 src/main/scala/org/squeryl/Table.scala
  2. +12 −0 src/test/scala/org/squeryl/test/schooldb/SchoolDb.scala
@@ -38,7 +38,7 @@ class Table[T] private [squeryl] (n: String, c: Class[T], val schema: Schema, _p
val o = _callbacks.beforeInsert(t.asInstanceOf[AnyRef])
val sess = Session.currentSession
val sw = new StatementWriter(_dbAdapter)
- _dbAdapter.writeInsert(t, this, sw)
+ _dbAdapter.writeInsert(o.asInstanceOf[T], this, sw)
val st =
(_dbAdapter.supportsAutoIncrementInColumnDeclaration, posoMetaData.primaryKey) match {
@@ -198,7 +198,12 @@ class SchoolDb extends Schema {
super.drop
}
+ def studentTransform(s: Student) = {
+ new Student(s.name, s.lastName, s.age, ((s.gender % 2) + 1), s.addressId, s.isMultilingual)
+ }
+
val beforeInsertsOfPerson = new ArrayBuffer[Person]
+ val transformedStudents = new ArrayBuffer[Student]
val beforeInsertsOfKeyedEntity = new ArrayBuffer[KeyedEntity[_]]
val beforeInsertsOfProfessor = new ArrayBuffer[Professor]
val afterInsertsOfProfessor = new ArrayBuffer[Professor]
@@ -209,6 +214,9 @@ class SchoolDb extends Schema {
val professorsCreatedWithFactory = new ArrayBuffer[Int]
override def callbacks = Seq(
+ // We'll change the gender of z1 z2 student
+ beforeInsert[Student]
+ map(s => {if (s.name == "z1" && s.lastName == "z2"){val s2 = studentTransform(s); transformedStudents.append(s2); s2} else s}),
beforeInsert[Person]
map(p => {beforeInsertsOfPerson.append(p); p}),
@@ -546,10 +554,14 @@ abstract class SchoolDbTestRun extends SchoolDbTestBase {
afterInsertsOfProfessor.clear
beforeDeleteOfSchool.clear
professorsCreatedWithFactory.clear
+ transformedStudents.clear
val s1 = students.insert(new Student("z1", "z2", Some(4), 1, Some(4), Some(true)))
+ val sOpt = from(students)(s => where(s.name === "z1" and s.lastName === "z2") select(s)).headOption
+ assert(sOpt.isDefined && sOpt.map(_.gender == 2).getOrElse(false))
assert(beforeInsertsOfPerson.exists(_ == s1))
+ assert(transformedStudents.exists(_ == s1))
assert(beforeInsertsOfKeyedEntity.exists(_ == s1))
assert(!beforeInsertsOfProfessor.exists(_ == s1))
assert(!afterInsertsOfProfessor.exists(_ == s1))

0 comments on commit a8d7298

Please sign in to comment.