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#62 from varju/return-write-result-everywhere
Browse files Browse the repository at this point in the history
Return write result everywhere
  • Loading branch information
rktoomey committed Nov 24, 2012
2 parents 25aac99 + d6c2849 commit 2c2c189
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 23 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.
* @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)
}

Expand Down Expand Up @@ -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
Expand Down
24 changes: 18 additions & 6 deletions salat-core/src/main/scala/com/novus/salat/dao/ModelCompanion.scala
Expand Up @@ -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 <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, o: DBObject, upsert: Boolean, multi: Boolean, wc: WriteConcern = defaultWriteConcern) = {
dao.update(q, o, upsert, multi, 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 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)
val lastError = wr.getCachedLastError
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)
val lastError = wr.getCachedLastError
if (lastError != null && !lastError.ok()) {
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")))
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
Expand All @@ -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
Expand All @@ -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
}

Expand Down

0 comments on commit 2c2c189

Please sign in to comment.