Exemple #1
0
// Get query data as string.
//
// @return string
func (this *Query) ToString() string {
	if this.DataString != "" {
		return this.DataString
	}

	for key, value := range this.Data {
		if util.TypeReal(value) == "[]string" {
			value = _fmt.Sprintf("[\"%s\"]", _str.Join(value.([]string), "\",\""))
		}
		this.DataString += _fmt.Sprintf(
			"%s=%s&", util.UrlEncode(key), util.UrlEncode(util.String(value)))
	}

	if this.DataString != "" {
		// drop last "&"
		this.DataString = this.DataString[0 : len(this.DataString)-1]
		// purify some encoded stuff
		this.DataString = _str.NewReplacer(
			"%5B", "[",
			"%5D", "]",
			"%2C", ",",
		).Replace(this.DataString)
	}

	return this.DataString
}
Exemple #2
0
// 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
}
Exemple #3
0
// 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
}
// Ping.
//
// @param  statusCodes... uint16
// @return bool
// @panics
func (this *DocumentAttachment) Ping(statusCodes ...uint16) bool {
	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 this.FileName == "" {
		panic("Attachment file name is required!")
	}

	query, headers := util.Map(), util.Map()
	if docRev != "" {
		query["rev"] = docRev
	}
	if this.Digest != "" {
		headers["If-None-Match"] = util.Quote(this.Digest)
	}

	database := this.Document.GetDatabase()
	response := database.Client.Head(util.StringFormat("%s/%s/%s",
		database.Name, docId, util.UrlEncode(this.FileName)), query, headers)

	// try to match given status codes
	for _, statusCode := range statusCodes {
		if response.GetStatusCode() == statusCode {
			return true
		}
	}

	return false
}
Exemple #5
0
// 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
}
Exemple #6
0
// Check is not modified.
//
// @return bool
// @panics
func (this *Document) IsNotModified() bool {
	id, rev := this.GetId(), this.GetRev()
	if id == "" || rev == "" {
		panic("_id & _rev fields are could not be empty!")
	}

	headers := util.Map()
	headers["If-None-Match"] = util.Quote(rev)

	return (304 == this.Database.Client.
		Head(this.Database.Name+"/"+util.UrlEncode(id), nil, headers).GetStatusCode())
}
Exemple #7
0
// Ping.
//
// @param  statusCode uint16
// @return bool
// @panics
func (this *Document) Ping(statusCode uint16) bool {
	id := this.GetId()
	if id == "" {
		panic("_id field is could not be empty!")
	}

	headers := util.Map()
	if this.Rev != "" {
		headers["If-None-Match"] = util.Quote(this.Rev)
	}

	return (statusCode == this.Database.Client.
		Head(this.Database.Name+"/"+util.UrlEncode(id), nil, headers).GetStatusCode())
}
// Find.
//
// @return map[string]interface{}
// @panics
func (this *DocumentAttachment) Find() map[string]interface{} {
	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 this.FileName == "" {
		panic("Attachment file name is required!")
	}

	query, headers := util.Map(), util.Map()
	if docRev != "" {
		query["rev"] = docRev
	}
	if this.Digest != "" {
		headers["If-None-Match"] = util.Quote(this.Digest)
	}
	headers["Accept"] = "*/*"
	headers["Content-Type"] = nil // nil=remove

	database := this.Document.GetDatabase()
	response := database.Client.Get(util.StringFormat("%s/%s/%s",
		database.Name, docId, util.UrlEncode(this.FileName)), query, headers)
	statusCode := response.GetStatusCode()

	ret := util.Map()
	// try to match excepted status code
	if statusCode == 200 || statusCode == 304 {
		ret["content"] = response.GetBody()
		ret["content_type"] = response.GetHeader("Content-Type")
		ret["content_length"] = util.UInt(response.GetHeader("Content-Length"))
		// set digest
		md5 := response.GetHeader("Content-MD5")
		if md5 == nil {
			md5 = response.GetHeader("ETag")
		}
		ret["digest"] = "md5-" + util.Trim(md5.(string), "\"")
	}

	return ret
}
// 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
}
// 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
}
Exemple #11
0
// Find as struct.
//
// @param  data interface{}
// @param  query map[string]interface{}
// @return interface{}, error
func (this *Document) FindStruct(
	data interface{}, query map[string]interface{}) (interface{}, error) {
	id := this.GetId()
	if id == "" {
		panic("_id field is could not be empty!")
	}
	if data == nil {
		panic("You should pass your data struct!")
	}

	query = util.Param(query)
	if query["rev"] == "" && this.Rev != "" {
		query["rev"] = this.Rev
	}

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

	return data, nil
}
Exemple #12
0
// Find.
//
// @param  query map[string]interface{}
// @return map[string]interface{}, error
// @panics
func (this *Document) Find(query map[string]interface{}) (map[string]interface{}, error) {
	id := this.GetId()
	if id == "" {
		panic("_id field is could not be empty!")
	}

	query = util.Param(query)
	if query["rev"] == "" && this.Rev != "" {
		query["rev"] = this.Rev
	}

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

	ret := util.Map()
	for key, value := range data.(map[string]interface{}) {
		ret[key] = value
	}

	return ret, nil
}