databend

package
v0.10.0 Latest Latest
Warning

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

Go to latest
Published: Oct 1, 2026 License: MIT Imports: 26 Imported by: 0

Documentation

Overview

Package databend is a database/sql driver for Databend, which runs SQL over the query API of HTTP. It registers the one name "databend", and takes a DSN of the form databend://user:pass@host:8000/database?key=value (D117).

db, err := sql.Open("databend", "databend://root:pass@localhost:8000/default")

The server binds each argument: a positional argument fills each ? in order, and sql.Named("k", v) fills :k (D120). Each result arrives one page at a time, and when the context ends, or the rows close before their end, the driver kills the query on the server (D123). Every value arrives as text, and the driver decodes it by the type of its column, an array, a map and a tuple too (D118 and D119). A transaction and the settings of SET and USE are carried in the session of the connection (D121 and D122). docs/DATABEND.md holds what the driver knows about the server.

Index

Constants

View Source
const (
	// CancelKill sends GET on the kill URI of the query. It is the default.
	CancelKill = "kill"
	// CancelNone sends nothing, and the query runs on to its end.
	CancelNone = "none"
)

The values of the key cancel, which say how the driver stops a query on the server when its context ends or its rows close early (D123).

View Source
const (
	// AuthBasic sends the user and the password with basic authentication.
	// It is the default.
	AuthBasic = dbimp.AuthBasic
	// AuthBearer sends the password as a Bearer token, such as a JWT.
	AuthBearer = dbimp.AuthBearer
)

The values of the key auth, which say how the driver sends the password (D94).

View Source
const Name = "databend"

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 server.
	Host string
	// Port is the port of the server, 8000 by default.
	Port int
	// TLS is true to speak HTTPS.
	TLS bool
	// User and Password are the credentials. The password can be a token
	// (D94).
	User     string
	Password string
	// Auth is AuthBasic or AuthBearer (D94).
	Auth string
	// Database is the database, default by default.
	Database string
	// Cancel is CancelKill or CancelNone (D123).
	Cancel string
	// Timezone is the timezone of the session, such as UTC, or "" for the
	// setting of the server.
	Timezone string
}

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 databend://user:pass@host:port/database?key=value (D27, D35 and D117).

func (*Config) FormatDSN

func (cfg *Config) FormatDSN() string

FormatDSN returns the DSN of cfg. ParseDSN reads it back as cfg, for a Config that ParseDSN or NewConnector filled.

type Connector

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

Connector opens connections to one database. 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, and fills each field that is zero with its default.

func (*Connector) Close

func (c *Connector) Close() error

Close closes the idle connections of the transport.

func (*Connector) Connect

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

Connect satisfies driver.Connector. A connection holds its session, which starts as the session of the DSN (D122).

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 Databend.

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 {
	// HTTPStatus is the status code of the response.
	HTTPStatus int
	// Code is the code of the server, or the HTTP status when the body held
	// none.
	Code int
	// Message is the message of the server.
	Message string
	// contains filtered or unexported fields
}

Error is an error that Databend reported, with its HTTP status and its code, such as 1005 for a statement that does not parse (measured). A statement that fails has HTTP 200 and the error in its body.

func (*Error) Error

func (err *Error) Error() string

Error satisfies the error interface.

func (*Error) Unwrap

func (err *Error) Unwrap() error

Unwrap returns the *dbimp.StatusError of a response whose status is not 2xx, or nil.

type Option

type Option = dbimp.Option[options]

Option sets an option of one statement or one transaction (D109). An option comes from the DSN, then from the context through WithOptions, then from an argument of the statement, and a later one wins. BeginTx takes no argument, so an option of a transaction comes from the DSN or through WithOptions only.

func WithCancel

func WithCancel(how string) Option

WithCancel sets how the driver stops the statement on the server, CancelKill or CancelNone, as the key cancel of the DSN does (D123).

func WithDatabase

func WithDatabase(name string) Option

WithDatabase sets the database of the statement, which the session of the request names, as the path of the DSN does. The next statement of the connection returns to the database of its session.

func WithParameter

func WithParameter(name string, value any) Option

WithParameter sets any key of the body of POST /v1/query by its name, such as "pagination". The value is encoded with json/v2. A key named here replaces one that the driver sets itself, as in Couchbase.

func WithReadonly

func WithReadonly(v bool) Option

WithReadonly would make the server refuse a write. The server has no read-only mode for one statement, so a statement with it fails with dbimp.ErrNotSupported (D117).

func WithTimeout

func WithTimeout(d time.Duration) Option

WithTimeout sets the time that the server gives the statement, as the setting max_execute_time_in_seconds, which counts whole seconds, so the driver rounds d up to the next second. The server stops a statement that runs longer with the code 1043 (measured).

func WithTimezone

func WithTimezone(name string) Option

WithTimezone sets the timezone of the statement, as the key timezone of the DSN does.

Jump to

Keyboard shortcuts

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