cosmos

package
v0.20.0 Latest Latest
Warning

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

Go to latest
Published: Oct 11, 2026 License: MIT Imports: 24 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package cosmos is a database/sql driver for Azure Cosmos DB with the API for NoSQL. It registers the one name "cosmos", and takes a DSN of the form cosmos://x:key@host:port/database/container (D190).

db, err := sql.Open("cosmos", "cosmos://x:"+key+"@account.documents.azure.com/mydb/mycontainer")

A statement is the SQL of Cosmos DB, which the driver sends as a query to the documents of one container. The master key of the account is the password of the DSN, and signs each request. The driver reads only: the SQL of Cosmos DB has no statement that writes, so Exec fails with dbimp.ErrNotSupported (D190).

The columns of a result are the keys of its first row, in the order that they arrive, and a later row that lacks a key has nil for it. A later row with a key that the first row lacks is an error (D18 and D190). A result whose rows are not objects, such as the rows of SELECT VALUE, has one column, named $1. The driver follows the header X-Ms-Continuation to the end of a result, one page at a time, and reads each page one document at a time (D25).

The driver sends a query as it is. It does not plan a query across partitions or merge the answers, so the hosted service answers an aggregate, TOP, ORDER BY, OFFSET LIMIT, DISTINCT or GROUP BY across partitions with an error. The caller names the partition key, with WithPartitionKey or the DSN key partitionkey (D190). docs/COSMOS.md holds what the driver knows about the server.

Index

Constants

View Source
const Name = "cosmos"

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 started with the context applies them.

Types

type Config

type Config struct {
	// Host is the host of the endpoint, such as
	// account.documents.azure.com or 127.0.0.1.
	Host string
	// Port is the port of the endpoint. Zero means the port of the scheme,
	// 443 for HTTPS and 80 for HTTP.
	Port int
	// TLS is true to speak HTTPS. The DSN turns it on by default, and only a
	// test with a fake server turns it off.
	TLS bool
	// Insecure is true to accept any certificate of the server, as the
	// emulator needs, because it makes a certificate for itself (D190). It is
	// false by default.
	Insecure bool
	// Database is the database of a statement, and Container is its
	// container. The path of the DSN holds them, and WithDatabase and
	// WithContainer change them for one statement. They can be empty, and
	// then each statement must name them.
	Database  string
	Container string
	// User is the user of the DSN, which the driver does not read. Key is
	// the master key of the account, as base64 text, and it is the password
	// of the DSN (D94 and D190). It signs each request.
	User string
	Key  string
	// PageSize is the number of documents that the server sends in one
	// page. Zero leaves the choice to the server, and -1 lets it choose the
	// size of each page.
	PageSize int
	// PartitionKey is the value of the partition key that a statement is
	// limited to, as text. It is nil when the statement is not limited to
	// one partition key.
	PartitionKey *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 cosmos://user:key@host:port/database/container (D27, D35 and D190). The user is any text, because the driver does not read it, and the password is the master key of the account, as base64 text. The path holds the database and the container, and it can hold the database only or nothing. The keys are tls, which is true by default, insecure, which is false by default and accepts any certificate of the server, pagesize, which is 0 by default, and partitionkey, which has no default. A DSN with another key, a key that appears twice, a path of more than two names, or no key is refused.

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

type Connector

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

Connector opens connections to one account. 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.

func (*Connector) Connect

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

Connect satisfies driver.Connector. A connection holds nothing on the server, so it sends no request.

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 Azure Cosmos DB.

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
	// SubStatus is the header X-Ms-Substatus, such as 1004 for a query across
	// partitions that the gateway cannot serve. It is 0 when the response has
	// none.
	SubStatus int
	// Code is the code of the first error in the message, such as SC1001, and
	// else the code of the body, such as BadRequest. It is empty when the
	// answer has none.
	Code string
	// Message is the text of the error, or the text of the answer when it
	// has none.
	Message string
	// RetryAfter is the wait that the header X-Ms-Retry-After-Ms names. It
	// is 0 when the response has none. A response of HTTP 429 did not run
	// the request, so a caller can send it again after the wait (recorded:
	// "lead: a burst of reads, number 1").
	RetryAfter time.Duration
	// contains filtered or unexported fields
}

Error is an error that Cosmos DB reported. The body of an error has code and message. On the hosted service the message is text that holds a JSON object and a line ActivityId, such as Message: {"errors":[{"code":"SC1001",...}]}, and the driver reads the object to get the code and the text of a syntax error (recorded: "a syntax error"). The emulator writes the object as the body (recorded). The driver sends no request again after an error, so HTTP 429 reaches the caller with the wait that the server asks for in RetryAfter (D8 and D190).

func (*Error) Error

func (err *Error) Error() string

Error satisfies the error interface.

func (*Error) Is added in v0.18.0

func (err *Error) Is(target error) bool

Is reports whether err matches target. It matches dbimp.ErrAuthentication when the status is HTTP 401 with the code Unauthorized, such as a request with no signature (recorded: "a request that the recorder sends with no signature"). Every other refusal does not match, such as HTTP 403 for a missing permission (D197).

func (*Error) Unwrap

func (err *Error) Unwrap() error

Unwrap returns the *dbimp.StatusError of the response.

type Option

type Option = dbimp.Option[options]

Option sets an option of one statement (D109). An option comes from the DSN, then from the context through WithOptions, and then from an argument of the statement, and a later one wins. The keys tls and insecure of the DSN belong to the connection, so they have no option.

func WithContainer

func WithContainer(name string) Option

WithContainer names the container of one statement, in place of the container of the DSN. The SQL of Cosmos DB has no container name in FROM, because the path of the request holds it (recorded: "a query that selects every document").

func WithDatabase

func WithDatabase(name string) Option

WithDatabase names the database of one statement, in place of the database of the DSN.

func WithPageSize

func WithPageSize(n int) Option

WithPageSize sets the number of documents in a page of the result, which the server takes in the header X-Ms-Max-Item-Count. Zero leaves it to the server, and -1 lets the server choose the size of each page (recorded: "a page of minus one"). A value below -1 fails the statement with dbimp.ErrInvalidValue.

func WithParameter

func WithParameter(name string, value any) Option

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

func WithPartitionKey

func WithPartitionKey(value any) Option

WithPartitionKey limits the statement to the documents whose partition key is value. The value is encoded with json/v2, so a string, a number, a bool and nil, which is null, are the values of a partition key. The server then serves an aggregate, TOP, ORDER BY, OFFSET LIMIT and DISTINCT that it refuses across partitions (recorded: "a query for one partition key"). The DSN key partitionkey sets a string value for every statement.

func WithReadonly

func WithReadonly(readonly bool) Option

WithReadonly asks that the statement write nothing. The driver reads only (D190), so WithReadonly(true) holds for every statement and changes nothing.

func WithTimeout

func WithTimeout(d time.Duration) Option

WithTimeout sets the time that the server gives the statement. The REST API of Cosmos DB has no such setting (not measured, source: Microsoft), so a positive value fails the statement with dbimp.ErrNotSupported. The context of the statement is the way to bound it. Zero asks for nothing.

Jump to

Keyboard shortcuts

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