Skip to content

Commit

Permalink
Issue 1067 - Add support for BinaryField in MongoDB
Browse files Browse the repository at this point in the history
  • Loading branch information
Tim Nelson committed Jul 9, 2011
1 parent 9f0fe6a commit de697c6
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ trait BsonMetaRecord[BaseRecord <: BsonRecord[BaseRecord]] extends MetaRecord[Ba
case x if mongotype_?(x.getClass) => dbo.add(f.name, x)
case x if datetype_?(x.getClass) => dbo.add(f.name, datetype2dbovalue(x))
case x: BsonRecord[_] => dbo.add(f.name, x.asDBObject)
case x: Array[Byte] => dbo.add(f.name, x)
case o => dbo.add(f.name, o.toString)
})
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,10 +68,6 @@ trait HarnessedLifecycleCallbacks extends LifecycleCallbacks {
class FieldTypeTestRecord private () extends MongoRecord[FieldTypeTestRecord] with ObjectIdPk[FieldTypeTestRecord] {
def meta = FieldTypeTestRecord

object mandatoryBinaryField extends BinaryField(this)
object legacyOptionalBinaryField extends BinaryField(this) { override def optional_? = true }
object optionalBinaryField extends OptionalBinaryField(this)

object mandatoryBooleanField extends BooleanField(this)
object legacyOptionalBooleanField extends BooleanField(this) { override def optional_? = true }
object optionalBooleanField extends OptionalBooleanField(this)
Expand Down Expand Up @@ -135,7 +131,6 @@ class FieldTypeTestRecord private () extends MongoRecord[FieldTypeTestRecord] wi
override def equals(other: Any): Boolean = other match {
case that: FieldTypeTestRecord =>
this.id.value == that.id.value &&
//this.mandatoryBinaryField.value == that.mandatoryBinaryField.value &&
this.mandatoryBooleanField.value == that.mandatoryBooleanField.value &&
this.mandatoryCountryField.value == that.mandatoryCountryField.value &&
this.mandatoryDecimalField.value == that.mandatoryDecimalField.value &&
Expand All @@ -153,10 +148,56 @@ class FieldTypeTestRecord private () extends MongoRecord[FieldTypeTestRecord] wi
}
}

case class MongoCaseClassTestObject(intField: Int, stringField: String)

object FieldTypeTestRecord extends FieldTypeTestRecord with MongoMetaRecord[FieldTypeTestRecord]

class BinaryFieldTestRecord extends MongoRecord[BinaryFieldTestRecord] with IntPk[BinaryFieldTestRecord] {
def meta = BinaryFieldTestRecord

object mandatoryBinaryField extends BinaryField(this) {
// compare the elements of the Array
override def equals(other: Any): Boolean = other match {
case that: BinaryField[Any] =>
this.value.zip(that.value).filter(t => t._1 != t._2).length == 0
case _ => false
}
}
object legacyOptionalBinaryField extends BinaryField(this) {
override def optional_? = true
// compare the elements of the Array
override def equals(other: Any): Boolean = other match {
case that: BinaryField[Any] => (this.valueBox, that.valueBox) match {
case (Empty, Empty) => true
case (Full(a), Full(b)) =>
a.zip(b).filter(t => t._1 != t._2).length == 0
case _ => false
}
case _ => false
}
}
object optionalBinaryField extends OptionalBinaryField(this) {
// compare the elements of the Array
override def equals(other: Any): Boolean = other match {
case that: OptionalBinaryField[Any] => (this.valueBox, that.valueBox) match {
case (Empty, Empty) => true
case (Full(a), Full(b)) =>
a.zip(b).filter(t => t._1 != t._2).length == 0
case _ => false
}
case _ => false
}
}

override def equals(other: Any): Boolean = other match {
case that:BinaryFieldTestRecord =>
this.id.value == that.id.value &&
this.mandatoryBinaryField == that.mandatoryBinaryField &&
this.legacyOptionalBinaryField == that.legacyOptionalBinaryField &&
this.optionalBinaryField == that.optionalBinaryField
case _ => false
}
}
object BinaryFieldTestRecord extends BinaryFieldTestRecord with MongoMetaRecord[BinaryFieldTestRecord]


case class TypeTestJsonObject(
intField: Int,
Expand Down Expand Up @@ -221,6 +262,8 @@ class PasswordTestRecord private () extends MongoRecord[PasswordTestRecord] with
}
object PasswordTestRecord extends PasswordTestRecord with MongoMetaRecord[PasswordTestRecord]

case class MongoCaseClassTestObject(intField: Int, stringField: String)

class ListTestRecord private () extends MongoRecord[ListTestRecord] with UUIDPk[ListTestRecord] {
def meta = ListTestRecord

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -167,8 +167,9 @@ object MongoRecordSpec extends Specification("MongoRecord Specification") with M
"MongoRecord" should {
checkMongoIsRunning

val binData: Array[Byte] = Array(18, 19, 20)

val fttr = FieldTypeTestRecord.createRecord
//.mandatoryBinaryField()
.mandatoryBooleanField(false)
.mandatoryCountryField(Countries.USA)
.mandatoryDecimalField(BigDecimal("3.14"))
Expand All @@ -183,6 +184,9 @@ object MongoRecordSpec extends Specification("MongoRecord Specification") with M
.mandatoryTextareaField("string")
.mandatoryTimeZoneField("America/Chicago")

val bftr = BinaryFieldTestRecord.createRecord
.mandatoryBinaryField(binData)

val mfttr = MongoFieldTypeTestRecord.createRecord
.mandatoryDateField(new Date)
.mandatoryJsonObjectField(TypeTestJsonObject(1, "jsonobj1", Map("x" -> "1")))
Expand Down Expand Up @@ -328,6 +332,14 @@ object MongoRecordSpec extends Specification("MongoRecord Specification") with M
fttrFromDb foreach { tr =>
tr mustEqual fttr
}

bftr.save

val bftrFromDb = BinaryFieldTestRecord.find(bftr.id.value)
bftrFromDb must notBeEmpty
bftrFromDb foreach { tr =>
tr mustEqual bftr
}
}

"delete record properly" in {
Expand Down

0 comments on commit de697c6

Please sign in to comment.