diff --git a/salat-core/src/main/scala/com/novus/salat/dao/DAO.scala b/salat-core/src/main/scala/com/novus/salat/dao/DAO.scala index 712c5cec..55312e87 100644 --- a/salat-core/src/main/scala/com/novus/salat/dao/DAO.scala +++ b/salat-core/src/main/scala/com/novus/salat/dao/DAO.scala @@ -115,13 +115,15 @@ trait BaseDAOMethods[ObjectType <: AnyRef, ID <: Any] { /** Saves an object to this collection. * @param t object to save * @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. * @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) } @@ -149,41 +151,47 @@ trait BaseDAOMethods[ObjectType <: AnyRef, ID <: Any] { /** Remove a matching object 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 a matching object from the collection * @param t object to remove from the collection * @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. * @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) } /** Removes objects from the database collection. * @param q the object that documents to be removed must match * @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. * @param id the ID of the document to be removed * @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. * @param ids the list of IDs identifying the list of documents to be removed * @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. * @param q object for which to search diff --git a/salat-core/src/main/scala/com/novus/salat/dao/ModelCompanion.scala b/salat-core/src/main/scala/com/novus/salat/dao/ModelCompanion.scala index 8991d91d..f6670f12 100644 --- a/salat-core/src/main/scala/com/novus/salat/dao/ModelCompanion.scala +++ b/salat-core/src/main/scala/com/novus/salat/dao/ModelCompanion.scala @@ -233,41 +233,53 @@ trait ModelCompanion[ObjectType <: AnyRef, ID <: Any] extends BaseDAOMethods[Obj /** @param t object to remove from the collection * @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) } /** @param q the object that documents to be removed must match * @param wc write concern * @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) } /** @param id the ID of the document to be removed * @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) } /** @param ids the list of IDs identifying the list of documents to be removed * @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) } /** @param t object to save * @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) } - def update(q: DBObject, o: DBObject, upsert: Boolean, multi: Boolean, wc: WriteConcern = defaultWriteConcern): WriteResult = { + /** @param q search query for old object to update + * @param o object with which to update q + * @param upsert if the database should create the element if it does not exist + * @param multi if the update should be applied to all objects matching + * @param wc write concern + * @return (WriteResult) result of write operation + */ + def update(q: DBObject, o: DBObject, upsert: Boolean, multi: Boolean, wc: WriteConcern = defaultWriteConcern) = { dao.update(q, o, upsert, multi, wc) } diff --git a/salat-core/src/main/scala/com/novus/salat/dao/SalatDAO.scala b/salat-core/src/main/scala/com/novus/salat/dao/SalatDAO.scala index 170d99a0..a3c9520f 100644 --- a/salat-core/src/main/scala/com/novus/salat/dao/SalatDAO.scala +++ b/salat-core/src/main/scala/com/novus/salat/dao/SalatDAO.scala @@ -318,8 +318,9 @@ abstract class SalatDAO[ObjectType <: AnyRef, ID <: Any](val collection: MongoCo /** @param t object to remove from the collection * @param wc write concern + * @return (WriteResult) result of write operation */ - def remove(t: ObjectType, wc: WriteConcern) { + def remove(t: ObjectType, wc: WriteConcern) = { try { val dbo = _grater.asDBObject(t) val wr = collection.remove(dbo, wc) @@ -327,40 +328,46 @@ abstract class SalatDAO[ObjectType <: AnyRef, ID <: Any](val collection: MongoCo if (lastError != null && !lastError.ok()) { throw SalatRemoveError(description, collection, wc, wr, List(dbo)) } + wr } } /** @param q the object that documents to be removed must match * @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 { val wr = collection.remove(q, wc) val lastError = wr.getCachedLastError if (lastError != null && !lastError.ok()) { throw SalatRemoveQueryError(description, collection, q, wc, wr) } + wr } } /** @param id the ID of the document to be removed * @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) } /** @param ids the list of IDs identifying the list of documents to be removed * @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) } /** @param t object to save * @param wc write concern + * @return (WriteResult) result of write operation */ - def save(t: ObjectType, wc: WriteConcern) { + def save(t: ObjectType, wc: WriteConcern) = { try { val dbo = _grater.asDBObject(t) val wr = collection.save(dbo, wc) @@ -368,6 +375,7 @@ abstract class SalatDAO[ObjectType <: AnyRef, ID <: Any](val collection: MongoCo if (lastError != null && !lastError.ok()) { throw SalatSaveError(description, collection, wc, wr, List(dbo)) } + wr } } diff --git a/salat-core/src/test/scala/com/novus/salat/test/dao/SalatDAOSpec.scala b/salat-core/src/test/scala/com/novus/salat/test/dao/SalatDAOSpec.scala index a25eb552..b7a8e45f 100644 --- a/salat-core/src/test/scala/com/novus/salat/test/dao/SalatDAOSpec.scala +++ b/salat-core/src/test/scala/com/novus/salat/test/dao/SalatDAOSpec.scala @@ -137,7 +137,8 @@ class SalatDAOSpec extends SalatSpec { val alpha3_* = alpha3.copy(beta = List[Beta](Gamma("gamma3"))) alpha3_* must_!= alpha3 - val cr = AlphaDAO.save(alpha3_*) + val wr = AlphaDAO.save(alpha3_*) + wr.getN must_== 1L AlphaDAO.collection.count must_== 1L val dbo: MongoDBObject = MongoConnection()(SalatSpecDb)(AlphaColl).findOne().get @@ -151,7 +152,8 @@ class SalatDAOSpec extends SalatSpec { _ids must contain(Some(alpha6.id)) 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.findOne(grater[Alpha].asDBObject(alpha5)) must beNone @@ -165,14 +167,16 @@ class SalatDAOSpec extends SalatSpec { "support removing by ID" in new alphaContext { AlphaDAO.insert(alpha1) AlphaDAO.collection.count must_== 1L - AlphaDAO.removeById(alpha1.id) + val wr = AlphaDAO.removeById(alpha1.id) + wr.getN must_== 1L AlphaDAO.collection.count must_== 0L } "support removing by a list of IDs" in new alphaContext { val _ids = AlphaDAO.insert(alpha4, alpha5, alpha6) AlphaDAO.collection.count must_== 3L - AlphaDAO.removeByIds(_ids.flatten) + val wr = AlphaDAO.removeByIds(_ids.flatten) + wr.getN must_== 3L AlphaDAO.collection.count must_== 0L }