atlasariga.io/atlas/sql/sqlclient Index | Files

package sqlclient

import "ariga.io/atlas/sql/sqlclient"

Index

Variables

var ErrUnsupported = errors.New("sql/sqlclient: driver does not support changing connected schema")

ErrUnsupported is returned if a registered driver does not support changing the schema.

Functions

func HasDriver

func HasDriver(scheme string) bool

HasDriver reports if there is any driver registered with the given scheme.

func ParseURL

func ParseURL(s string) (*url.URL, error)

ParseURL is similar to url.Parse but returns errors without the raw URL attached to avoid printing userinfo in errors.

func Register

func Register(name string, opener Opener, opts ...RegisterOption)

Register registers a client Opener (i.e. creator) with the given name.

Types

type Client

type Client struct {
	// Name used when creating the client.
	Name string

	// DB used for creating the client.
	DB *sql.DB
	// URL holds an enriched url.URL.
	URL *URL

	// A migration driver for the attached dialect.
	migrate.Driver

	// Marshal and Evaluator functions for decoding
	// and encoding the schema documents.
	schemahcl.Marshaler
	schemahcl.Evaluator

	// Ephemeral indicates that the database we connect to is "ephemeral"
	// (e.g., a temporary running container). This can be set by the driver
	// that opens the client to signal to its consumers that there is no need
	// to guard against race conditions with other Atlas clients.
	Ephemeral bool
	// contains filtered or unexported fields
}

Client provides the common functionalities for working with Atlas from different applications (e.g. CLI and TF). Note, the Client is dialect specific and should be instantiated using a call to Open.

func Open

func Open(ctx context.Context, s string, opts ...OpenOption) (*Client, error)

Open opens an Atlas client by its provided url string.

func OpenURL

func OpenURL(ctx context.Context, u *url.URL, opts ...OpenOption) (*Client, error)

OpenURL opens an Atlas client by its provided url.URL.

func (*Client) AddClosers

func (c *Client) AddClosers(closers ...io.Closer)

AddClosers adds list of closers to close at the end of the client lifetime.

func (*Client) Close

func (c *Client) Close() error

Close closes the underlying database connection and the migration driver in case it implements the io.Closer interface.

func (*Client) Tx

func (c *Client) Tx(ctx context.Context, opts *sql.TxOptions) (*TxClient, error)

Tx returns a transactional client.

type Hook

type Hook struct {
	Conn struct {
		AfterOpen   func(context.Context, *Client) error
		BeforeClose func(*Client) error
	}
	Tx struct {
		AfterBegin func(context.Context, *TxClient) error
		BeforeCommit,
		BeforeRollback func(*TxClient) error
	}
}

Hook groups all possible hooks in connection and transaction lifecycle.

type OpenOption

type OpenOption func(*openOptions) error

OpenOption allows to configure a openOptions using functional arguments.

func OpenSchema

func OpenSchema(s string) OpenOption

OpenSchema opens the connection to the given schema. If the registered driver does not support this, ErrUnsupported is returned instead.

func OpenWithHooks

func OpenWithHooks(hks ...*Hook) OpenOption

OpenWithHooks returns an OpenOption that sets the hooks for the client after opening.

type Opener

type Opener interface {
	Open(ctx context.Context, u *url.URL) (*Client, error)
}

Opener opens a migration driver by the given URL.

func DriverOpener

func DriverOpener(open func(schema.ExecQuerier) (migrate.Driver, error)) Opener

DriverOpener is a helper Opener creator for sharing between all drivers.

type OpenerFunc

type OpenerFunc func(context.Context, *url.URL) (*Client, error)

OpenerFunc allows using a function as an Opener.

func (OpenerFunc) Open

func (f OpenerFunc) Open(ctx context.Context, u *url.URL) (*Client, error)

Open calls f(ctx, u).

type RegisterOption

type RegisterOption func(*registerOptions)

RegisterOption allows configuring the Opener registration using functional options.

func RegisterCodec

RegisterCodec registers static codec for attaching into the client after it is opened.

func RegisterDriverOpener

func RegisterDriverOpener(open func(schema.ExecQuerier) (migrate.Driver, error)) RegisterOption

RegisterDriverOpener registers a func to create a migrate.Driver from a schema.ExecQuerier. Registering this function is implicitly done when using DriverOpener. The passed opener is used when creating a TxClient.

func RegisterFlavours

func RegisterFlavours(flavours ...string) RegisterOption

RegisterFlavours allows registering additional flavours (i.e. names), accepted by Atlas to open clients.

func RegisterTxOpener

func RegisterTxOpener(open TxOpener) RegisterOption

RegisterTxOpener allows registering a custom transaction opener with an optional close function.

func RegisterURLParser

func RegisterURLParser(p URLParser) RegisterOption

RegisterURLParser allows registering a function for parsing the url.URL and attach additional info to the extended URL.

type SchemaChanger

type SchemaChanger interface {
	ChangeSchema(*url.URL, string) *url.URL
}

SchemaChanger is implemented by a driver if it how to change the connection URL to represent another schema.

type Tx

type Tx struct {
	*sql.Tx
	CommitFn   func() error // override default commit behavior
	RollbackFn func() error // override default rollback behavior
}

Tx wraps sql.Tx with optional custom Commit and Rollback functions.

func (*Tx) Commit

func (tx *Tx) Commit() error

Commit the transaction.

func (*Tx) Rollback

func (tx *Tx) Rollback() error

Rollback the transaction.

type TxClient

type TxClient struct {
	*Client

	// The transaction this Client wraps.
	Tx *Tx
	// contains filtered or unexported fields
}

TxClient is returned by calling Client.Tx. It behaves the same as Client, but wraps all operations within a transaction.

func (*TxClient) Commit

func (c *TxClient) Commit() error

Commit the transaction.

func (*TxClient) Rollback

func (c *TxClient) Rollback() error

Rollback the transaction.

type TxOpener

type TxOpener func(context.Context, *sql.DB, *sql.TxOptions) (*Tx, error)

TxOpener opens a transaction with optional closer.

type URL

type URL struct {
	*url.URL

	// The DSN used for opening the connection.
	DSN string `json:"-"`

	// The Schema this client is connected to.
	Schema string
}

URL extends the standard url.URL with additional connection information attached by the Opener (if any).

type URLParser

type URLParser interface {
	ParseURL(*url.URL) *URL
}

URLParser parses an url.URL into an enriched URL and attaches additional info to it.

type URLParserFunc

type URLParserFunc func(*url.URL) *URL

URLParserFunc allows using a function as an URLParser.

func (URLParserFunc) ParseURL

func (f URLParserFunc) ParseURL(u *url.URL) *URL

ParseURL calls f(u).

Source Files

client.go

Version
v0.32.0 (latest)
Published
Mar 10, 2025
Platform
linux/amd64
Imports
10 packages
Last checked
1 month ago

Tools for package owners.