database

package
v0.1.1 Latest Latest
Warning

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

Go to latest
Published: Apr 28, 2026 License: MIT Imports: 12 Imported by: 0

Documentation

Overview

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

Index

Constants

View Source
const (
	// Exit is the result kind for quit command actions.
	Exit pgxspecial.SpecialResultKind = 100 + iota
	// ChangeDB is the result kind for database switch command actions.
	ChangeDB
	// Conninfo is the result kind for connection info command actions.
	Conninfo
)

Variables

This section is empty.

Functions

This section is empty.

Types

type ChangeDbAction

type ChangeDbAction struct {
	Name string
}

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

func (ChangeDbAction) ResultKind

ResultKind returns the special result kind for ChangeDbAction.

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) 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) (result.Result, 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 ConnInfoAction

type ConnInfoAction struct{}

ConnInfoAction indicates that connection info should be displayed.

func (ConnInfoAction) ResultKind

ResultKind returns the special result kind for ConnInfoAction.

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.

func (ExitAction) ResultKind

func (e ExitAction) ResultKind() pgxspecial.SpecialResultKind

ResultKind returns the special result kind for ExitAction.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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