couchbase

package
v0.3.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Sep 27, 2026 License: MIT Imports: 21 Imported by: 0

Documentation

Overview

Package couchbase is a database/sql driver for the query service of Couchbase Server, which runs SQL++ (N1QL) over HTTP. It registers the one name "couchbase" (D30), and takes a DSN of the form couchbase://user:pass@host:8093/?key=value (D38).

db, err := sql.Open("couchbase", "couchbase://user:pass@localhost:8093/")

The driver sends each argument to the server, which binds it: an argument without a name fills $1 or ?, and sql.Named("x", v) fills $x (D40). A transaction is a transaction of the query service (D41), and the key durability_level sets its durability (D43). A string that scans into a []byte is decoded as base64 (D44). docs/COUCHBASE.md holds what the driver knows about the server.

Index

Constants

View Source
const Name = "couchbase"

Name is the name that the driver registers with database/sql, and the scheme of its DSN.

Variables

This section is empty.

Functions

func WithOptions

func WithOptions(ctx context.Context, opts ...Option) context.Context

WithOptions returns a context that carries opts. Each statement and each transaction started with the context applies them after the options of the DSN.

Types

type Config

type Config struct {
	// Host is the host of the query service.
	Host string
	// Port is the port of the query service, 8093 by default, or 18093 with
	// TLS.
	Port int
	// TLS is true to speak HTTPS.
	TLS bool
	// User and Password are the credentials, sent with basic
	// authentication.
	User     string
	Password string

	// QueryContext names the bucket and the scope of a collection that a
	// statement names alone, such as "default:dbmeta._default".
	QueryContext string
	// ScanConsistency is "not_bounded" or "request_plus", or "" for the
	// default of the server.
	ScanConsistency string
	// Timeout is the timeout that the server enforces for each statement, or
	// zero for none.
	Timeout time.Duration
	// Durability is the durability of a transaction, or "" for the default
	// of the server (D43).
	Durability string
	// TxTimeout is how long a transaction lasts before the server ends it, or
	// zero for the default of the server, which is 15 seconds (D46).
	TxTimeout time.Duration
}

Config is the configuration of a connector, which the DSN holds. The caller owns it (D7).

func ParseDSN

func ParseDSN(dsn string) (*Config, error)

ParseDSN parses a DSN of the form couchbase://user:pass@host:port/?key=value (D27, D35 and D38).

func (*Config) FormatDSN

func (cfg *Config) FormatDSN() string

FormatDSN returns the DSN of cfg, which ParseDSN reads back as cfg.

type Connector

type Connector struct {
	// contains filtered or unexported fields
}

Connector opens connections to one query service. It owns its transport, and every connection shares it. A caller can build one from a Config and open it with sql.OpenDB.

func NewConnector

func NewConnector(cfg Config) *Connector

NewConnector returns a Connector for cfg. The connector keeps a copy of cfg.

func (*Connector) Close

func (c *Connector) Close() error

Close closes the idle connections of the transport. database/sql calls it when the database closes.

func (*Connector) Connect

func (c *Connector) Connect(ctx context.Context) (driver.Conn, error)

Connect satisfies driver.Connector. A connection holds no state on the server, except a transaction while one is open.

func (*Connector) Driver

func (c *Connector) Driver() driver.Driver

Driver satisfies driver.Connector.

type Driver

type Driver struct{}

Driver is the database/sql driver for Couchbase.

func (Driver) Open

func (Driver) Open(string) (driver.Conn, error)

Open satisfies driver.Driver. database/sql opens a connection through OpenConnector, and Open returns an error, because opening a connection needs a context.

func (Driver) OpenConnector

func (Driver) OpenConnector(dsn string) (driver.Connector, error)

OpenConnector satisfies driver.DriverContext. It parses the DSN.

type Error

type Error struct {
	// Code is the code of the error.
	Code int `json:"code"`
	// Msg is the message of the error.
	Msg string `json:"msg"`
}

Error is one error that the query service reported, such as code 3000 for a syntax error or 12009 for an insert of a key that exists.

func (Error) Error

func (err Error) Error() string

Error satisfies the error interface.

type Option

type Option func(*options)

Option sets an option of one statement or one transaction (D40). An option comes from the DSN, then from the context through WithOptions, then from an argument of the statement, and a later one wins.

func WithDurability

func WithDurability(v string) Option

WithDurability sets the durability of a transaction, such as "none" or "majority" (D43). It applies to BeginTx and to a statement that runs as a transaction of its own.

func WithParameter

func WithParameter(name string, value any) Option

WithParameter sets any parameter of the request by its name, such as "profile" or "client_context_id", as the SDKs of Couchbase do with their raw options. The value is encoded with json/v2. A parameter named here replaces one that the driver sets itself.

func WithQueryContext

func WithQueryContext(v string) Option

WithQueryContext sets the bucket and the scope of a collection that a statement names alone, such as "default:dbmeta._default".

func WithReadonly

func WithReadonly(v bool) Option

WithReadonly makes the server refuse a statement that writes.

func WithScanConsistency

func WithScanConsistency(v string) Option

WithScanConsistency sets the scan consistency, "not_bounded" or "request_plus". A read after a write sends "request_plus" to see the write, because Couchbase updates an index after a write.

func WithTimeout

func WithTimeout(d time.Duration) Option

WithTimeout sets the timeout that the server enforces for the statement.

func WithTransactionTimeout

func WithTransactionTimeout(d time.Duration) Option

WithTransactionTimeout sets how long a transaction can last before the server ends it. It applies to BeginTx.

type ResponseError

type ResponseError struct {
	// HTTPStatus is the status code of the response.
	HTTPStatus int
	// Status is the status in the body of the response.
	Status string
	// Errs are the errors, in the order that the server sent them.
	Errs []Error
}

ResponseError holds the errors of one response, with its HTTP status and the status that the server reported, such as "fatal" or "stopped". Use errors.As to read an Error of it.

func (*ResponseError) Error

func (err *ResponseError) Error() string

Error satisfies the error interface.

func (*ResponseError) Unwrap

func (err *ResponseError) Unwrap() []error

Unwrap returns each Error, for errors.As and errors.Is.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL