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

Commit

Permalink
Merge pull request salat#46 from varju/return-write-result
Browse files Browse the repository at this point in the history
Expose WriteResult to callers of DAO.update
  • Loading branch information
rktoomey committed Nov 24, 2012
2 parents bfb59a0 + 9c57a44 commit 25aac99
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 5 deletions.
6 changes: 4 additions & 2 deletions salat-core/src/main/scala/com/novus/salat/dao/DAO.scala
Expand Up @@ -131,17 +131,19 @@ trait BaseDAOMethods[ObjectType <: AnyRef, ID <: Any] {
* @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)
def update(q: DBObject, o: DBObject, upsert: Boolean, multi: Boolean, wc: WriteConcern): WriteResult

/** Performs an update operation.
* @param q search query for old object to update
* @param t object with which to update <tt>q</tt>
* @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, t: ObjectType, upsert: Boolean, multi: Boolean, wc: WriteConcern) {
def update(q: DBObject, t: ObjectType, upsert: Boolean, multi: Boolean, wc: WriteConcern): WriteResult = {
update(q = q, o = toDBObject(t), upsert = upsert, multi = multi, wc = wc)
}

Expand Down
Expand Up @@ -267,7 +267,7 @@ trait ModelCompanion[ObjectType <: AnyRef, ID <: Any] extends BaseDAOMethods[Obj
dao.save(t, wc)
}

def update(q: DBObject, o: DBObject, upsert: Boolean, multi: Boolean, wc: WriteConcern = defaultWriteConcern) {
def update(q: DBObject, o: DBObject, upsert: Boolean, multi: Boolean, wc: WriteConcern = defaultWriteConcern): WriteResult = {
dao.update(q, o, upsert, multi, wc)
}

Expand Down
4 changes: 3 additions & 1 deletion salat-core/src/main/scala/com/novus/salat/dao/SalatDAO.scala
Expand Up @@ -376,14 +376,16 @@ abstract class SalatDAO[ObjectType <: AnyRef, ID <: Any](val collection: MongoCo
* @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 = false, multi: Boolean = false, wc: WriteConcern = defaultWriteConcern) {
def update(q: DBObject, o: DBObject, upsert: Boolean = false, multi: Boolean = false, wc: WriteConcern = defaultWriteConcern): WriteResult = {
try {
val wr = collection.update(q, o, upsert, multi, wc)
val lastError = wr.getCachedLastError
if (lastError != null && !lastError.ok()) {
throw SalatDAOUpdateError(description, collection, q, o, wc, wr, upsert, multi)
}
wr
}
}

Expand Down
Expand Up @@ -117,11 +117,12 @@ class SalatDAOSpec extends SalatSpec {
AlphaDAO.collection.count must_== 1L

// need to explicitly specify upsert and multi when updating using an object instead of dbo
val cr = AlphaDAO.update(q = MongoDBObject("_id" -> 3),
val wr = AlphaDAO.update(q = MongoDBObject("_id" -> 3),
t = alpha3.copy(beta = List[Beta](Gamma("gamma3"))),
upsert = false,
multi = false,
wc = new WriteConcern())
wr.getN must_== 1L

AlphaDAO.collection.count must_== 1L

Expand Down

0 comments on commit 25aac99

Please sign in to comment.