コード例 #1
0
ファイル: document.go プロジェクト: yay-couch/couch-go
// Copy.
//
// @param  dest string
// @param  args... bool
// @return map[string]interface{}, error
// @panics
func (this *Document) Copy(dest string, args ...bool) (map[string]interface{}, error) {
	id := this.GetId()
	if id == "" {
		panic("_id field could not be empty!")
	}
	if dest == "" {
		panic("Destination could not be empty!")
	}

	query, headers := util.Map(), util.Map()
	headers["Destination"] = dest

	if args != nil {
		if args[0] == true {
			query["batch"] = "ok"
		}
		if args[1] == true {
			headers["X-Couch-Full-Commit"] = "true"
		}
	}
	data, err := this.Database.Client.Copy(this.Database.Name+"/"+util.UrlEncode(id), query, headers).
		GetBodyData(nil)
	if err != nil {
		return nil, err
	}

	return map[string]interface{}{
		"ok":  util.DigBool("ok", data),
		"id":  util.DigString("id", data),
		"rev": util.DigString("rev", data),
	}, nil
}
コード例 #2
0
ファイル: document.go プロジェクト: yay-couch/couch-go
// Copy to.
//
// @param  dest   string
// @param  destRev string
// @param  args... bool
// @return map[string]interface{}, error
// @panics
func (this *Document) CopyTo(dest, destRev string, args ...bool) (map[string]interface{}, error) {
	id, rev := this.GetId(), this.GetRev()
	if id == "" || rev == "" {
		panic("Both _id & _rev fields could not be empty!")
	}
	if dest == "" || destRev == "" {
		panic("Destination & destination revision could not be empty!")
	}

	query, headers := util.Map(), util.Map()
	headers["If-Match"] = rev
	headers["Destination"] = util.StringFormat("%s?rev=%s", dest, destRev)

	if args != nil {
		if args[0] == true {
			query["batch"] = "ok"
		}
		if args[1] == true {
			headers["X-Couch-Full-Commit"] = "true"
		}
	}

	data, err := this.Database.Client.Copy(this.Database.Name+"/"+util.UrlEncode(id), query, headers).
		GetBodyData(nil)
	if err != nil {
		return nil, err
	}

	return map[string]interface{}{
		"ok":  util.DigBool("ok", data),
		"id":  util.DigString("id", data),
		"rev": util.DigString("rev", data),
	}, nil
}
コード例 #3
0
ファイル: document.go プロジェクト: yay-couch/couch-go
// Remove.
//
// @param  args... bool
// @return map[string]interface{}, error
// @panics
func (this *Document) Remove(args ...bool) (map[string]interface{}, error) {
	id, rev := this.GetId(), this.GetRev()
	if id == "" || rev == "" {
		panic("Both _id & _rev fields could not be empty!")
	}

	query, headers := util.Map(), util.Map()
	headers["If-Match"] = rev

	if args != nil {
		if args[0] == true {
			query["batch"] = "ok"
		}
		if args[1] == true {
			headers["X-Couch-Full-Commit"] = "true"
		}
	}

	data, err := this.Database.Client.Delete(this.Database.Name+"/"+util.UrlEncode(id), query, headers).
		GetBodyData(nil)
	if err != nil {
		return nil, err
	}

	return map[string]interface{}{
		"ok":  util.DigBool("ok", data),
		"id":  util.DigString("id", data),
		"rev": util.DigString("rev", data),
	}, nil
}
コード例 #4
0
ファイル: document.go プロジェクト: yay-couch/couch-go
// Save.
//
// @param  args... bool
// @return map[string]interface{}, error
func (this *Document) Save(args ...bool) (map[string]interface{}, error) {
	query, headers, body := util.Map(), util.Map(), this.GetData()
	if args != nil {
		if args[0] == true {
			query["batch"] = "ok"
		}
		if args[1] == true {
			headers["X-Couch-Full-Commit"] = "true"
		}
	}

	if this.Rev != "" {
		headers["If-Match"] = this.Rev
	}

	if this.Attachments != nil {
		body["_attachments"] = util.Map()
		for name, attachment := range this.Attachments {
			body["_attachments"].(map[string]interface{})[name] = attachment.ToArray(true)
		}
	}

	// make a reusable lambda
	_func := func(data interface{}, err error) (map[string]interface{}, error) {
		if err != nil {
			return nil, err
		}

		id, rev := util.DigString("id", data), util.DigString("rev", data)
		// set id & rev for next save() instant calls
		if id != "" && this.Id == nil {
			this.SetId(id)
		}
		if rev != "" {
			this.SetRev(rev)
		}

		return map[string]interface{}{
			"ok":  util.DigBool("ok", data),
			"id":  id,
			"rev": rev,
		}, nil
	}

	if this.Id == nil {
		return _func( // insert action
			this.Database.Client.Post(this.Database.Name, query, body, headers).
				GetBodyData(nil))
	} else {
		return _func( // update action
			this.Database.Client.Put(this.Database.Name+"/"+this.GetId(), query, body, headers).
				GetBodyData(nil))
	}
}
コード例 #5
0
ファイル: server.go プロジェクト: yay-couch/couch-go
// Get database updates.
//
// @param  query map[string]interface{}
// @return map[string]interface{}, error
func (this *Server) GetDatabaseUpdates(query interface{}) (map[string]interface{}, error) {
	data, err := this.Client.Get("/_db_updates", query, nil).GetBodyData(nil)
	if err != nil {
		return nil, err
	}

	return map[string]interface{}{
		"ok":      util.DigBool("ok", data),
		"type":    util.DigString("type", data),
		"db_name": util.DigString("db_name", data),
	}, nil
}
コード例 #6
0
ファイル: document.go プロジェクト: yay-couch/couch-go
// Find revisions extended.
//
// @return []map[string]interface{}, error
func (this *Document) FindRevisionsExtended() ([]map[string]string, error) {
	data, err := this.Find(util.ParamList("revs_info", true))
	if err != nil {
		return nil, err
	}

	ret := util.MapListString(nil)
	if data["_revs_info"] != nil {
		ret = util.MapListString(data["_revs_info"]) // @overwrite
		for i, info := range data["_revs_info"].([]interface{}) {
			ret[i] = map[string]string{
				"rev":    util.DigString("rev", info),
				"status": util.DigString("status", info),
			}
		}
	}

	return ret, nil
}
コード例 #7
0
ファイル: document.go プロジェクト: yay-couch/couch-go
// Set attachment.
//
// @param  attachment interface{}
// @return void
// @panics
func (this *Document) SetAttachment(attachment interface{}) {
	if _, ok := attachment.(*DocumentAttachment); !ok {
		file := util.DigString("file", attachment)
		fileName := util.DigString("fileName", attachment)
		attachment = NewDocumentAttachment(this, file, fileName)
	}

	// file name must be uniq
	if _, ok := this.Attachments[attachment.(*DocumentAttachment).FileName]; ok {
		panic("Attachment is alredy exists on this document!")
	}

	if this.Attachments == nil {
		this.Attachments = make(map[string]*DocumentAttachment)
	}

	this.Attachments[attachment.(*DocumentAttachment).FileName] =
		attachment.(*DocumentAttachment)
}
コード例 #8
0
// Remove.
//
// @return map[string]interface{}, error
// @panics
func (this *DocumentAttachment) Remove(args ...bool) (map[string]interface{}, error) {
	if this.Document == nil {
		panic("Attachment document is not defined!")
	}

	docId := this.Document.GetId()
	docRev := this.Document.GetRev()
	if docId == "" {
		panic("Attachment document _id is required!")
	}
	if docRev == "" {
		panic("Attachment document _rev is required!")
	}
	if this.FileName == "" {
		panic("Attachment file name is required!")
	}

	query, headers := util.Map(), util.Map()
	if args != nil {
		if args[0] {
			query["batch"] = "ok"
		}
		if args[1] {
			headers["X-Couch-Full-Commit"] = "true"
		}
	}
	headers["If-Match"] = docRev

	data, err := this.Document.Database.Client.Delete(util.StringFormat(
		"%s/%s/%s", this.Document.Database.Name, docId, util.UrlEncode(this.FileName),
	), nil, headers,
	).GetBodyData(nil)
	if err != nil {
		return nil, err
	}

	return map[string]interface{}{
		"ok":  util.DigBool("ok", data),
		"id":  util.DigString("id", data),
		"rev": util.DigString("rev", data),
	}, nil
}
コード例 #9
0
// Save.
//
// @return map[string]interface{}, error
// @panics
func (this *DocumentAttachment) Save() (map[string]interface{}, error) {
	if this.Document == nil {
		panic("Attachment document is not defined!")
	}

	docId := this.Document.GetId()
	docRev := this.Document.GetRev()
	if docId == "" {
		panic("Attachment document _id is required!")
	}
	if docRev == "" {
		panic("Attachment document _rev is required!")
	}
	if this.FileName == "" {
		panic("Attachment file name is required!")
	}

	// read file contents
	this.ReadFile(false)

	headers := util.Map()
	headers["If-Match"] = docRev
	headers["Content-Type"] = this.ContentType

	data, err := this.Document.Database.Client.Put(util.StringFormat(
		"%s/%s/%s", this.Document.Database.Name, docId, util.UrlEncode(this.FileName),
	), nil, this.Data, headers,
	).GetBodyData(nil)
	if err != nil {
		return nil, err
	}

	return map[string]interface{}{
		"ok":  util.DigBool("ok", data),
		"id":  util.DigString("id", data),
		"rev": util.DigString("rev", data),
	}, nil
}