database

package
v0.3.1 Latest Latest
Warning

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

Go to latest
Published: Jun 27, 2026 License: MIT Imports: 19 Imported by: 0

Documentation

Overview

Package database manages PostgreSQL connections, execution, and special commands.

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrConnectionClosed         = errors.New("connection closed unexpectedly")
	ErrConnectionNotEstablished = errors.New("connection not established")
)

Functions

This section is empty.

Types

type ChangeDbAction

type ChangeDbAction struct {
	Name string
}

ChangeDbAction carries target database name for \c / \connect commands.

type Client

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

Client provides high-level connection management and SQL execution operations.

func New

func New(logger *slog.Logger) *Client

New creates a database client with logger-backed connection lifecycle reporting.

func (*Client) Cache added in v0.3.0

func (c *Client) Cache(worker *compDB.Worker) error

Cache populates the completion worker's cache to provide context aware completions.

func (*Client) Cancel

func (c *Client) Cancel(ctx context.Context) error

func (*Client) ChangeDatabase

func (c *Client) ChangeDatabase(ctx context.Context, dbName string) error

ChangeDatabase reconnects to the same server with a different database name.

func (*Client) Close

func (c *Client) Close(ctx context.Context) error

Close closes the current database connection if one exists.

func (*Client) Connect

func (c *Client) Connect(ctx context.Context, connector Connector) error

Connect opens a database connection using the provided connector.

func (*Client) ExecuteQuery

func (c *Client) ExecuteQuery(ctx context.Context, query string, isMulti bool) (Rows, bool, error)

ExecuteQuery runs SQL through the underlying executor and returns typed results.

func (*Client) ExecuteSpecial

func (c *Client) ExecuteSpecial(ctx context.Context, command string) (pgxspecial.SpecialCommandResult, bool, error)

ExecuteSpecial executes a pgxspecial command (for example: \q, \c, \conninfo).

func (*Client) GetDatabase

func (c *Client) GetDatabase() string

GetDatabase returns the current database name.

func (*Client) GetHost

func (c *Client) GetHost() string

GetHost returns the current connection host.

func (*Client) GetPort

func (c *Client) GetPort() uint16

GetPort returns the current connection port.

func (*Client) GetUser

func (c *Client) GetUser() string

GetUser returns the current connection user name.

func (*Client) IsConnected

func (c *Client) IsConnected() bool

IsConnected reports whether the client currently has an active connection.

func (*Client) ParsePrompt

func (c *Client) ParsePrompt(str string) string

ParsePrompt resolves prompt placeholders using current connection metadata.

func (*Client) Ping

func (c *Client) Ping(ctx context.Context) error

Ping verifies connectivity to the current database.

type CommandTag added in v0.2.1

type CommandTag interface {
	RowsAffected() int64
	String() string
}

CommandTag represents the result of a SQL command.

type ConnInfoAction

type ConnInfoAction struct{}

ConnInfoAction indicates that connection info should be displayed.

type Connector

type Connector interface {
	Connect(ctx context.Context) (*pgx.Conn, error)
	UpdatePassword(password string)
	Password() string
}

Connector describes how the client obtains and updates a database connection.

func NewPGConnectorFromConnString

func NewPGConnectorFromConnString(connString string) (Connector, error)

NewPGConnectorFromConnString builds a connector from a PostgreSQL connection string.

func NewPGConnectorFromFields

func NewPGConnectorFromFields(host, database, user, password string, port uint16) (Connector, error)

NewPGConnectorFromFields builds a connector from individual connection fields.

type ExitAction

type ExitAction struct{}

ExitAction indicates that the REPL should terminate.

type QueryFn added in v0.2.1

type QueryFn func(ctx context.Context, query string, isMulti bool) (Rows, bool, error)

type Rows added in v0.2.1

type Rows interface {
	driver.Rows

	// The caller must call Close() when done with the
	// result and check the error.
	Close() error

	// Columns returns the column labels of the current result set.
	// The implementation of this method should cache the result so that the
	// result does not need to be constructed on each invocation.
	Columns() []string

	// ColumnTypeDatabaseTypeName returns the database type name
	// of the column at the given column index.
	ColumnTypeDatabaseTypeName(index int) string

	// Tag retrieves the statement tag for the current result set.
	Tag() (CommandTag, error)

	// Next populates values with the next row of results. []byte values are copied
	// so that subsequent calls to Next and Close do not mutate values. This
	// makes it slower than theoretically possible but the safety concerns
	// (since this is unobvious and unexpected behavior) outweigh.
	Next(values []driver.Value) error

	// NextResultSet prepares the next result set for reading.
	// Returns false if there is no more result set to read.
	NextResultSet() (bool, error)
}

Rows describes a result set.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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