0

I need to select all agents from couchbase bucket. I am using https://github.com/couchbase/gocb package to work with couchbase.

My current query looks like:

SELECT  a.* FROM `agents` a WHERE a.type=$1

But I feel I need to replace bucket name with some placeholder, or parameter that will be set before sending query to couchbase.

Can anyone tell me how to do that (without Sprintf :) )?


UPD

For now I am using this helper function:

import (
    "strings"

    "gopkg.in/couchbase/gocb.v1"
)

const bucketPlaceholder = "{{bucket}}"

func PrepareQuery(b *gocb.Bucket, q string) string {
    return strings.ReplaceAll(q, bucketPlaceholder, b.Name())
}
J Sorel
  • 343
  • 2
  • 18
  • I'm not a go dev, but: if you are trying to parameterize the bucket name (like you're doing with type=$1), then that's not (currently) possible. You *can* get the bucket name from the bucket object and use that in the query string (I assume that's what you mean by Sprintf) – Matthew Groves Feb 06 '20 at 16:02
  • Yes, is that what I meant. – J Sorel Feb 06 '20 at 18:54
  • I guess you have a problem of concept here. A bucket is NOT something similar to a table in a RDBMS, a bucket is similar to a Schema. So in theory you would never select all documents of your bucket because this is similar to select all rows of your whole database. – deniswsrosa Feb 06 '20 at 20:14
  • Your updated code makes sense to me: you're using the Name() from the bucket object, so in case the bucket name changes at some point, your queries will still work. However, I'm not sure about using a placeholder. Again, I'm not a go dev, but is there a way to do this with string interpolation instead? – Matthew Groves Feb 06 '20 at 21:24
  • @MatthewGroves, Yes, we can use string interpolation via formatting string like `fmt.Sprintf("SELECT a.* FROM %s a", b.Name())` – J Sorel Feb 07 '20 at 07:06

0 Answers0