Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Return WriteResult from save, remove, removeById, and removeByIds

  • Loading branch information...
commit 8e504be44f8e4a38eb2a2cd7f2e40b0ae9f7394d 1 parent 25aac99
@varju varju authored
View
24 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
View
15 salat-core/src/main/scala/com/novus/salat/dao/ModelCompanion.scala
@@ -233,37 +233,42 @@ 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)
}
View
18 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
}
}
View
12 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
}
Please sign in to comment.
Something went wrong with that request. Please try again.