Skip to content

Commit

Permalink
Makes Squeryl-Record both aware of the dirty_? flag, which is reset a…
Browse files Browse the repository at this point in the history
…fter values are loaded from the DB, and utilize runSafe during data loading.
  • Loading branch information
davewhittaker committed May 8, 2012
1 parent 9c53f7e commit 27f5c8b
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 5 deletions.
Expand Up @@ -141,15 +141,30 @@ class RecordMetaDataFactory extends FieldMetaDataFactory {
fieldScale getOrElse super.scale fieldScale getOrElse super.scale
} }


private def fieldFor(o: AnyRef) = getter.get.invoke(o).asInstanceOf[TypedField[_ <: AnyRef]] private def fieldFor(o: AnyRef) = getter.get.invoke(o) match {
case tf: TypedField[_] => tf
case other => org.squeryl.internals.Utils.throwError("Field's used with Squeryl must inherit from net.liftweb.record.TypedField : " + other )
}


override def set(target: AnyRef, value: AnyRef) = { /**
val typedField: TypedField[_] = fieldFor(target) * Sets the value which was retrieved from the DB into the appropriate Record field
typedField.setFromAny(Box !! value) */
override def set(target: AnyRef, value: AnyRef) = target match {
case record: Record[_] =>
record.runSafe {
val typedField: TypedField[_] = fieldFor(target)
typedField.setFromAny(Box !! value)
typedField.resetDirty
}
case other =>
org.squeryl.internals.Utils.throwError("RecordMetaDataFactory can not set fields on non Record objects : " + other)
} }


override def setFromResultSet(target: AnyRef, rs: ResultSet, index: Int) = set(target, resultSetHandler(rs, index)) override def setFromResultSet(target: AnyRef, rs: ResultSet, index: Int) = set(target, resultSetHandler(rs, index))


/**
* Extracts the value from the field referenced by o that will be stored in the DB
*/
override def get(o: AnyRef) = fieldFor(o) match { override def get(o: AnyRef) = fieldFor(o) match {
case enumField: EnumTypedField[_] => enumField.valueBox match { case enumField: EnumTypedField[_] => enumField.valueBox match {
case Full(enum: Enumeration#Value) => enum.id: java.lang.Integer case Full(enum: Enumeration#Value) => enum.id: java.lang.Integer
Expand All @@ -161,7 +176,7 @@ class RecordMetaDataFactory extends FieldMetaDataFactory {
} }
case other => other.valueBox match { case other => other.valueBox match {
case Full(c: Calendar) => new Timestamp(c.getTime.getTime) case Full(c: Calendar) => new Timestamp(c.getTime.getTime)
case Full(other) => other case Full(other: AnyRef) => other
case _ => null case _ => null
} }
} }
Expand Down
Expand Up @@ -352,6 +352,12 @@ object SquerylRecordSpec extends Specification("SquerylRecord Specification") {
val columnDefinition = new PostgreSqlAdapter().writeColumnDeclaration(fieldMetaData, false, MySchema) val columnDefinition = new PostgreSqlAdapter().writeColumnDeclaration(fieldMetaData, false, MySchema)
columnDefinition.endsWith("numeric(" + Company.employeeSatisfaction.context.getPrecision() +"," + Company.employeeSatisfaction.scale + ")") must_== true columnDefinition.endsWith("numeric(" + Company.employeeSatisfaction.context.getPrecision() +"," + Company.employeeSatisfaction.scale + ")") must_== true
} }

forExample("Properly reset the dirty_? flag after loading entities") >> inTransaction {
val company = from(companies)(company =>
select(company)).page(0, 1).single
company.allFields map { f => f.dirty_? must_== false }
}


} }


Expand Down

0 comments on commit 27f5c8b

Please sign in to comment.