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

Commit

Permalink
Improvements to CouchQueryRow
Browse files Browse the repository at this point in the history
CouchQueryRow.document now preloads the CouchDocument with its revision ID. This prevents errors when trying to immediately update or delete that document.
  • Loading branch information
snej committed Jul 28, 2011
1 parent c8054c7 commit 5091e94
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 9 deletions.
17 changes: 10 additions & 7 deletions Couch/CouchDocument.m
Expand Up @@ -83,16 +83,19 @@ - (CouchRevision*) currentRevision {
}


- (void) loadCurrentRevisionFrom: (NSDictionary*)properties {
NSString* rev = $castIf(NSString, [properties objectForKey: @"_rev"]);
- (void) loadCurrentRevisionFrom: (CouchQueryRow*)row {
NSString* rev = row.documentRevision;
if (rev) {
if (!_currentRevisionID || [_currentRevisionID isEqualToString: rev]) {
// OK, I can set the current revisions contents from the given dictionary:
[self setCurrentRevisionID: rev];
if (!_currentRevision)
_currentRevision = [[CouchRevision alloc] initWithDocument: self
revisionID: rev];
[_currentRevision setProperties:properties];

NSDictionary* properties = row.documentProperties;
if (properties) {
if (!_currentRevision)
_currentRevision = [[CouchRevision alloc] initWithDocument: self
revisionID: rev];
[_currentRevision setProperties:properties];
}
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion Couch/CouchInternal.h
Expand Up @@ -35,7 +35,7 @@

@interface CouchDocument ()
@property (readwrite, copy) NSString* currentRevisionID;
- (void) loadCurrentRevisionFrom: (NSDictionary*)contents;
- (void) loadCurrentRevisionFrom: (CouchQueryRow*)row;
- (void) bulkSaveCompleted: (NSDictionary*) result;
- (BOOL) notifyChanged: (NSDictionary*)change;
@end
Expand Down
3 changes: 3 additions & 0 deletions Couch/CouchQuery.h
Expand Up @@ -121,6 +121,9 @@
This will be nil if a grouping was enabled in the query, because then the result rows don't correspond to individual documents. */
@property (readonly) CouchDocument* document;

/** The revision ID of the document this row was mapped from. */
@property (readonly) NSString* documentRevision;

/** The properties of the document this row was mapped from.
To get this, you must have set the -prefetch property on the query; else this will be nil. */
@property (readonly) NSDictionary* documentProperties;
Expand Down
11 changes: 10 additions & 1 deletion Couch/CouchQuery.m
Expand Up @@ -240,6 +240,15 @@ - (id) value {return [_result objectForKey: @"value"];}
- (NSString*) documentID {return [_result objectForKey: @"id"];}
- (NSDictionary*) documentProperties {return [_result objectForKey: @"doc"];}

- (NSString*) documentRevision {
// Get the revision id from either the embedded document contents,
// or the 'rev' value key:
NSString* rev = [[_result objectForKey: @"doc"] objectForKey: @"_rev"];
if (!rev)
rev = [$castIf(NSDictionary, self.value) objectForKey: @"rev"];
return rev;
}


- (id) keyAtIndex: (NSUInteger)index {
id key = [_result objectForKey: @"key"];
Expand All @@ -260,7 +269,7 @@ - (CouchDocument*) document {
if (!docID)
return nil;
CouchDocument* doc = [_query.database documentWithID: docID];
[doc loadCurrentRevisionFrom: self.documentProperties];
[doc loadCurrentRevisionFrom: self];
return doc;
}

Expand Down

0 comments on commit 5091e94

Please sign in to comment.