1
1
/*
2
- Copyright IBM Corp. 2016 All Rights Reserved.
2
+ Copyright IBM Corp. 2016, 2017 All Rights Reserved.
3
3
4
4
Licensed under the Apache License, Version 2.0 (the "License");
5
5
you may not use this file except in compliance with the License.
@@ -733,6 +733,55 @@ func (dbclient *CouchDatabase) ReadDocRange(startKey, endKey string, limit, skip
733
733
734
734
}
735
735
736
+ //DeleteDoc method provides function to delete a document from the database by id
737
+ func (dbclient * CouchDatabase ) DeleteDoc (id , rev string ) error {
738
+
739
+ logger .Debugf ("Entering DeleteDoc() id=%s" , id )
740
+
741
+ deleteURL , err := url .Parse (dbclient .couchInstance .conf .URL )
742
+ if err != nil {
743
+ logger .Errorf ("URL parse error: %s" , err .Error ())
744
+ return err
745
+ }
746
+
747
+ deleteURL .Path = dbclient .dbName
748
+ // id can contain a '/', so encode separately
749
+ deleteURL = & url.URL {Opaque : deleteURL .String () + "/" + encodePathElement (id )}
750
+
751
+ if rev == "" {
752
+
753
+ //See if the document already exists, we need the rev for delete
754
+ _ , revdoc , err2 := dbclient .ReadDoc (id )
755
+ if err2 != nil {
756
+ //set the revision to indicate that the document was not found
757
+ rev = ""
758
+ } else {
759
+ //set the revision to the rev returned from the document read
760
+ rev = revdoc
761
+ }
762
+ }
763
+
764
+ logger .Debugf (" rev=%s" , rev )
765
+
766
+ resp , couchDBReturn , err := dbclient .handleRequest (http .MethodDelete , deleteURL .String (), nil , rev , "" )
767
+ if err != nil {
768
+ fmt .Printf ("couchDBReturn=%v" , couchDBReturn )
769
+ if couchDBReturn != nil && couchDBReturn .StatusCode == 404 {
770
+ logger .Debug ("Document not found (404), returning nil value instead of 404 error" )
771
+ // non-existent document should return nil value instead of a 404 error
772
+ // for details see https://github.com/hyperledger-archives/fabric/issues/936
773
+ return nil
774
+ }
775
+ return err
776
+ }
777
+ defer resp .Body .Close ()
778
+
779
+ logger .Debugf ("Exiting DeleteDoc()" )
780
+
781
+ return nil
782
+
783
+ }
784
+
736
785
//QueryDocuments method provides function for processing a query
737
786
func (dbclient * CouchDatabase ) QueryDocuments (query string , limit , skip int ) (* []QueryResult , error ) {
738
787
@@ -796,7 +845,6 @@ func (dbclient *CouchDatabase) QueryDocuments(query string, limit, skip int) (*[
796
845
797
846
logger .Debugf ("Adding row to resultset: %s" , row )
798
847
799
- //TODO Replace the temporary NewHeight version when available
800
848
var addDocument = & QueryResult {jsonDoc .ID , row }
801
849
802
850
results = append (results , * addDocument )
@@ -821,7 +869,7 @@ func (dbclient *CouchDatabase) handleRequest(method, connectURL string, data io.
821
869
}
822
870
823
871
//add content header for PUT
824
- if method == http .MethodPut || method == http .MethodPost {
872
+ if method == http .MethodPut || method == http .MethodPost || method == http . MethodDelete {
825
873
826
874
//If the multipartBoundary is not set, then this is a JSON and content-type should be set
827
875
//to application/json. Else, this is contains an attachment and needs to be multipart
0 commit comments