Skip to content
This repository has been archived by the owner on Jan 15, 2022. It is now read-only.

Commit

Permalink
Browse files Browse the repository at this point in the history
Return WriteResult from save, remove, removeById, and removeByIds
  • Loading branch information
varju committed Nov 24, 2012
1 parent 25aac99 commit 8e504be
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 22 deletions.
24 changes: 16 additions & 8 deletions salat-core/src/main/scala/com/novus/salat/dao/DAO.scala
Expand Up @@ -115,13 +115,15 @@ trait BaseDAOMethods[ObjectType <: AnyRef, ID <: Any] {
/** Saves an object to this collection. /** Saves an object to this collection.
* @param t object to save * @param t object to save
* @param wc write concern * @param wc write concern
* @return (WriteResult) result of write operation
*/ */
def save(t: ObjectType, wc: WriteConcern) def save(t: ObjectType, wc: WriteConcern): WriteResult


/** Saves an object to this collection. /** Saves an object to this collection.
* @param t object to save * @param t object to save
* @return (WriteResult) result of write operation
*/ */
def save(t: ObjectType) { def save(t: ObjectType): WriteResult = {
save(t = t, wc = defaultWriteConcern) save(t = t, wc = defaultWriteConcern)
} }


Expand Down Expand Up @@ -149,41 +151,47 @@ trait BaseDAOMethods[ObjectType <: AnyRef, ID <: Any] {


/** Remove a matching object from the collection /** Remove a matching object from the collection
* @param t object to remove from the collection * @param t object to remove from the collection
* @return (WriteResult) result of write operation
*/ */
def remove(t: ObjectType) { def remove(t: ObjectType): WriteResult = {
remove(t = t, wc = defaultWriteConcern) remove(t = t, wc = defaultWriteConcern)
} }


/** Remove a matching object from the collection /** Remove a matching object from the collection
* @param t object to remove from the collection * @param t object to remove from the collection
* @param wc write concern * @param wc write concern
* @return (WriteResult) result of write operation
*/ */
def remove(t: ObjectType, wc: WriteConcern) def remove(t: ObjectType, wc: WriteConcern): WriteResult


/** Removes objects from the database collection. /** Removes objects from the database collection.
* @param q the object that documents to be removed must match * @param q the object that documents to be removed must match
* @return (WriteResult) result of write operation
*/ */
def remove[A <% DBObject](q: A) { def remove[A <% DBObject](q: A): WriteResult = {
remove(q = q, wc = defaultWriteConcern) remove(q = q, wc = defaultWriteConcern)
} }


/** Removes objects from the database collection. /** Removes objects from the database collection.
* @param q the object that documents to be removed must match * @param q the object that documents to be removed must match
* @param wc write concern * @param wc write concern
* @return (WriteResult) result of write operation
*/ */
def remove[A <% DBObject](q: A, wc: WriteConcern) def remove[A <% DBObject](q: A, wc: WriteConcern): WriteResult


/** Remove document identified by this ID. /** Remove document identified by this ID.
* @param id the ID of the document to be removed * @param id the ID of the document to be removed
* @param wc write concern * @param wc write concern
* @return (WriteResult) result of write operation
*/ */
def removeById(id: ID, wc: WriteConcern = defaultWriteConcern) def removeById(id: ID, wc: WriteConcern = defaultWriteConcern): WriteResult


/** Remove documents matching any of the supplied list of IDs. /** Remove documents matching any of the supplied list of IDs.
* @param ids the list of IDs identifying the list of documents to be removed * @param ids the list of IDs identifying the list of documents to be removed
* @param wc wrote concern * @param wc wrote concern
* @return (WriteResult) result of write operation
*/ */
def removeByIds(ids: List[ID], wc: WriteConcern = defaultWriteConcern) def removeByIds(ids: List[ID], wc: WriteConcern = defaultWriteConcern): WriteResult


/** Count the number of documents matching the search criteria. /** Count the number of documents matching the search criteria.
* @param q object for which to search * @param q object for which to search
Expand Down
15 changes: 10 additions & 5 deletions salat-core/src/main/scala/com/novus/salat/dao/ModelCompanion.scala
Expand Up @@ -233,37 +233,42 @@ trait ModelCompanion[ObjectType <: AnyRef, ID <: Any] extends BaseDAOMethods[Obj


/** @param t object to remove from the collection /** @param t object to remove from the collection
* @param wc write concern * @param wc write concern
* @return (WriteResult) result of write operation
*/ */
def remove(t: ObjectType, wc: WriteConcern = defaultWriteConcern) { def remove(t: ObjectType, wc: WriteConcern = defaultWriteConcern) = {
dao.remove(t, wc) dao.remove(t, wc)
} }


/** @param q the object that documents to be removed must match /** @param q the object that documents to be removed must match
* @param wc write concern * @param wc write concern
* @tparam A * @tparam A
* @return (WriteResult) result of write operation
*/ */
def remove[A <% DBObject](q: A, wc: WriteConcern) { def remove[A <% DBObject](q: A, wc: WriteConcern) = {
dao.remove(q, wc) dao.remove(q, wc)
} }


/** @param id the ID of the document to be removed /** @param id the ID of the document to be removed
* @param wc write concern * @param wc write concern
* @return (WriteResult) result of write operation
*/ */
def removeById(id: ID, wc: WriteConcern = defaultWriteConcern) { def removeById(id: ID, wc: WriteConcern = defaultWriteConcern) = {
dao.removeById(id, wc) dao.removeById(id, wc)
} }


/** @param ids the list of IDs identifying the list of documents to be removed /** @param ids the list of IDs identifying the list of documents to be removed
* @param wc wrote concern * @param wc wrote concern
* @return (WriteResult) result of write operation
*/ */
def removeByIds(ids: List[ID], wc: WriteConcern = defaultWriteConcern) { def removeByIds(ids: List[ID], wc: WriteConcern = defaultWriteConcern) = {
dao.removeByIds(ids, wc) dao.removeByIds(ids, wc)
} }


/** @param t object to save /** @param t object to save
* @param wc write concern * @param wc write concern
* @return (WriteResult) result of write operation
*/ */
def save(t: ObjectType, wc: WriteConcern = defaultWriteConcern) { def save(t: ObjectType, wc: WriteConcern = defaultWriteConcern) = {
dao.save(t, wc) dao.save(t, wc)
} }


Expand Down
18 changes: 13 additions & 5 deletions salat-core/src/main/scala/com/novus/salat/dao/SalatDAO.scala
Expand Up @@ -318,56 +318,64 @@ abstract class SalatDAO[ObjectType <: AnyRef, ID <: Any](val collection: MongoCo


/** @param t object to remove from the collection /** @param t object to remove from the collection
* @param wc write concern * @param wc write concern
* @return (WriteResult) result of write operation
*/ */
def remove(t: ObjectType, wc: WriteConcern) { def remove(t: ObjectType, wc: WriteConcern) = {
try { try {
val dbo = _grater.asDBObject(t) val dbo = _grater.asDBObject(t)
val wr = collection.remove(dbo, wc) val wr = collection.remove(dbo, wc)
val lastError = wr.getCachedLastError val lastError = wr.getCachedLastError
if (lastError != null && !lastError.ok()) { if (lastError != null && !lastError.ok()) {
throw SalatRemoveError(description, collection, wc, wr, List(dbo)) throw SalatRemoveError(description, collection, wc, wr, List(dbo))
} }
wr
} }
} }


/** @param q the object that documents to be removed must match /** @param q the object that documents to be removed must match
* @param wc write concern * @param wc write concern
* @return (WriteResult) result of write operation
*/ */
def remove[A <% DBObject](q: A, wc: WriteConcern) { def remove[A <% DBObject](q: A, wc: WriteConcern) = {
try { try {
val wr = collection.remove(q, wc) val wr = collection.remove(q, wc)
val lastError = wr.getCachedLastError val lastError = wr.getCachedLastError
if (lastError != null && !lastError.ok()) { if (lastError != null && !lastError.ok()) {
throw SalatRemoveQueryError(description, collection, q, wc, wr) throw SalatRemoveQueryError(description, collection, q, wc, wr)
} }
wr
} }
} }


/** @param id the ID of the document to be removed /** @param id the ID of the document to be removed
* @param wc write concern * @param wc write concern
* @return (WriteResult) result of write operation
*/ */
def removeById(id: ID, wc: WriteConcern = defaultWriteConcern) { def removeById(id: ID, wc: WriteConcern = defaultWriteConcern) = {
remove(MongoDBObject("_id" -> id), wc) remove(MongoDBObject("_id" -> id), wc)
} }


/** @param ids the list of IDs identifying the list of documents to be removed /** @param ids the list of IDs identifying the list of documents to be removed
* @param wc wrote concern * @param wc wrote concern
* @return (WriteResult) result of write operation
*/ */
def removeByIds(ids: List[ID], wc: WriteConcern) { def removeByIds(ids: List[ID], wc: WriteConcern) = {
remove(MongoDBObject("_id" -> MongoDBObject("$in" -> MongoDBList(ids: _*))), wc) remove(MongoDBObject("_id" -> MongoDBObject("$in" -> MongoDBList(ids: _*))), wc)
} }


/** @param t object to save /** @param t object to save
* @param wc write concern * @param wc write concern
* @return (WriteResult) result of write operation
*/ */
def save(t: ObjectType, wc: WriteConcern) { def save(t: ObjectType, wc: WriteConcern) = {
try { try {
val dbo = _grater.asDBObject(t) val dbo = _grater.asDBObject(t)
val wr = collection.save(dbo, wc) val wr = collection.save(dbo, wc)
val lastError = wr.getCachedLastError val lastError = wr.getCachedLastError
if (lastError != null && !lastError.ok()) { if (lastError != null && !lastError.ok()) {
throw SalatSaveError(description, collection, wc, wr, List(dbo)) throw SalatSaveError(description, collection, wc, wr, List(dbo))
} }
wr
} }
} }


Expand Down
Expand Up @@ -137,7 +137,8 @@ class SalatDAOSpec extends SalatSpec {


val alpha3_* = alpha3.copy(beta = List[Beta](Gamma("gamma3"))) val alpha3_* = alpha3.copy(beta = List[Beta](Gamma("gamma3")))
alpha3_* must_!= alpha3 alpha3_* must_!= alpha3
val cr = AlphaDAO.save(alpha3_*) val wr = AlphaDAO.save(alpha3_*)
wr.getN must_== 1L
AlphaDAO.collection.count must_== 1L AlphaDAO.collection.count must_== 1L


val dbo: MongoDBObject = MongoConnection()(SalatSpecDb)(AlphaColl).findOne().get val dbo: MongoDBObject = MongoConnection()(SalatSpecDb)(AlphaColl).findOne().get
Expand All @@ -151,7 +152,8 @@ class SalatDAOSpec extends SalatSpec {
_ids must contain(Some(alpha6.id)) _ids must contain(Some(alpha6.id))
AlphaDAO.collection.count must_== 3L AlphaDAO.collection.count must_== 3L


val cr = AlphaDAO.remove(alpha5) val wr = AlphaDAO.remove(alpha5)
wr.getN must_== 1L
AlphaDAO.collection.count must_== 2L AlphaDAO.collection.count must_== 2L


AlphaDAO.findOne(grater[Alpha].asDBObject(alpha5)) must beNone AlphaDAO.findOne(grater[Alpha].asDBObject(alpha5)) must beNone
Expand All @@ -165,14 +167,16 @@ class SalatDAOSpec extends SalatSpec {
"support removing by ID" in new alphaContext { "support removing by ID" in new alphaContext {
AlphaDAO.insert(alpha1) AlphaDAO.insert(alpha1)
AlphaDAO.collection.count must_== 1L AlphaDAO.collection.count must_== 1L
AlphaDAO.removeById(alpha1.id) val wr = AlphaDAO.removeById(alpha1.id)
wr.getN must_== 1L
AlphaDAO.collection.count must_== 0L AlphaDAO.collection.count must_== 0L
} }


"support removing by a list of IDs" in new alphaContext { "support removing by a list of IDs" in new alphaContext {
val _ids = AlphaDAO.insert(alpha4, alpha5, alpha6) val _ids = AlphaDAO.insert(alpha4, alpha5, alpha6)
AlphaDAO.collection.count must_== 3L AlphaDAO.collection.count must_== 3L
AlphaDAO.removeByIds(_ids.flatten) val wr = AlphaDAO.removeByIds(_ids.flatten)
wr.getN must_== 3L
AlphaDAO.collection.count must_== 0L AlphaDAO.collection.count must_== 0L
} }


Expand Down

0 comments on commit 8e504be

Please sign in to comment.