Skip to content
This repository
Browse code

Makes Squeryl-Record both aware of the dirty_? flag, which is reset a…

…fter values are loaded from the DB, and utilize runSafe during data loading.
  • Loading branch information...
commit 27f5c8b435705e9f90769847753d49dd27985ec9 1 parent 9c53f7e
Dave Whittaker authored May 08, 2012
25  persistence/squeryl-record/src/main/scala/net/liftweb/squerylrecord/RecordMetaDataFactory.scala
@@ -141,15 +141,30 @@ class RecordMetaDataFactory extends FieldMetaDataFactory {
141 141
         fieldScale getOrElse super.scale
142 142
       }
143 143
 
144  
-      private def fieldFor(o: AnyRef) = getter.get.invoke(o).asInstanceOf[TypedField[_ <: AnyRef]]
  144
+      private def fieldFor(o: AnyRef) = getter.get.invoke(o) match {
  145
+        case tf: TypedField[_] => tf
  146
+        case other => org.squeryl.internals.Utils.throwError("Field's used with Squeryl must inherit from net.liftweb.record.TypedField : " + other )
  147
+      }
145 148
 
146  
-      override def set(target: AnyRef, value: AnyRef) = {
147  
-        val typedField: TypedField[_] = fieldFor(target)
148  
-        typedField.setFromAny(Box !! value)
  149
+      /**
  150
+       * Sets the value which was retrieved from the DB into the appropriate Record field
  151
+       */
  152
+      override def set(target: AnyRef, value: AnyRef) = target match {
  153
+          case record: Record[_] =>
  154
+            record.runSafe {
  155
+            	val typedField: TypedField[_] = fieldFor(target)
  156
+            	typedField.setFromAny(Box !! value)
  157
+            	typedField.resetDirty
  158
+            }
  159
+          case other =>
  160
+            org.squeryl.internals.Utils.throwError("RecordMetaDataFactory can not set fields on non Record objects : " + other)
149 161
       }
150 162
 
151 163
       override def setFromResultSet(target: AnyRef, rs: ResultSet, index: Int) = set(target, resultSetHandler(rs, index))
152 164
 
  165
+      /**
  166
+       * Extracts the value from the field referenced by o that will be stored in the DB
  167
+       */
153 168
       override def get(o: AnyRef) = fieldFor(o) match {
154 169
         case enumField: EnumTypedField[_] => enumField.valueBox match {
155 170
           case Full(enum: Enumeration#Value) => enum.id: java.lang.Integer
@@ -161,7 +176,7 @@ class RecordMetaDataFactory extends FieldMetaDataFactory {
161 176
         }
162 177
         case other => other.valueBox match {
163 178
           case Full(c: Calendar) => new Timestamp(c.getTime.getTime)
164  
-          case Full(other) => other
  179
+          case Full(other: AnyRef) => other
165 180
           case _ => null
166 181
         }
167 182
       }
6  persistence/squeryl-record/src/test/scala/net/liftweb/squerylrecord/SquerylRecordSpec.scala
@@ -352,6 +352,12 @@ object SquerylRecordSpec extends Specification("SquerylRecord Specification") {
352 352
       val columnDefinition = new PostgreSqlAdapter().writeColumnDeclaration(fieldMetaData, false, MySchema)
353 353
       columnDefinition.endsWith("numeric(" + Company.employeeSatisfaction.context.getPrecision() +"," + Company.employeeSatisfaction.scale + ")") must_== true
354 354
     }
  355
+    
  356
+    forExample("Properly reset the dirty_? flag after loading entities") >> inTransaction {
  357
+      val company = from(companies)(company =>
  358
+        select(company)).page(0, 1).single
  359
+      company.allFields map { f => f.dirty_? must_== false }
  360
+    }
355 361
 
356 362
   }
357 363
 

0 notes on commit 27f5c8b

Please sign in to comment.
Something went wrong with that request. Please try again.