client

package
v0.2.4 Latest Latest
Warning

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

Go to latest
Published: Aug 27, 2026 License: MIT Imports: 9 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Bridge

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

Bridge exposes dynamically typed methods for language runtimes. Every method accepts a context and returns a value plus an error so runtimes can inject the context and surface errors consistently.

func NewBridge

func NewBridge(db *sqlx.DB) *Bridge

NewBridge creates a dynamic-language bridge from an existing connection pool.

func (*Bridge) Begin

func (b *Bridge) Begin(ctx context.Context) (any, error)

Begin starts a transaction.

func (*Bridge) Close

func (b *Bridge) Close(context.Context) (any, error)

Close releases an exclusive connection back to the pool.

func (*Bridge) Commit

func (b *Bridge) Commit(context.Context) (any, error)

Commit commits the active transaction.

func (*Bridge) Connect

func (b *Bridge) Connect(ctx context.Context) (any, error)

Connect reserves an exclusive connection from the pool.

func (*Bridge) Get

func (b *Bridge) Get(ctx context.Context, query string, args ...any) (any, error)

Get returns the first result row.

func (*Bridge) GetAll

func (b *Bridge) GetAll(ctx context.Context, query string, args ...any) (any, error)

GetAll returns all result rows.

func (*Bridge) Insert

func (b *Bridge) Insert(ctx context.Context, table string, value any) (any, error)

Insert inserts a dynamically typed named value.

func (*Bridge) InsertID

func (b *Bridge) InsertID(context.Context) (any, error)

Insert_id returns the ID generated by the last insert.

func (*Bridge) Query

func (b *Bridge) Query(ctx context.Context, query string, args ...any) (any, error)

Query executes a statement with dynamically typed arguments.

func (*Bridge) Replace

func (b *Bridge) Replace(ctx context.Context, table string, value any) (any, error)

Replace replaces a row using a dynamically typed named value.

func (*Bridge) Rollback

func (b *Bridge) Rollback(context.Context) (any, error)

Rollback rolls back the active transaction.

func (*Bridge) RowsAffected

func (b *Bridge) RowsAffected(context.Context) (any, error)

Rows_affected returns the affected row count from the last write.

func (*Bridge) Update

func (b *Bridge) Update(ctx context.Context, table string, value any, keyColumns ...any) (any, error)

Update updates a row using dynamically typed named values and key columns.

func (*Bridge) WithObserver

func (b *Bridge) WithObserver(fn ObserveFunc)

WithObserver sets an observer on the underlying client.

type Client

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

Client wraps an *sqlx.DB and provides a PHP-PDO-like API.

Concurrency: The underlying *sqlx.DB is safe for concurrent use, so multiple PDO instances can share the same database connection pool. A single *Client value is NOT safe for concurrent use: write operations mutate lastInsertID and rowsAffected, and transaction methods mutate transaction state. For stateless HTTP handlers, create a new *Client per request that shares the underlying *sql.DB pool (see OpenDB).

func NewClient

func NewClient(db *sqlx.DB) *Client

NewClient creates a new PDO from an existing *sqlx.DB.

func (*Client) Begin

func (p *Client) Begin(ctx context.Context) error

Begin starts a transaction. Returns an error if a transaction is already in progress (nested transactions are not supported).

func (*Client) Close

func (p *Client) Close() error

Close closes the underlying exclusive connection. Only needed does something if `Connect()` is called.

func (*Client) Commit

func (p *Client) Commit() error

Commit commits the current transaction. Returns nil when no transaction is in progress.

func (*Client) Connect

func (p *Client) Connect(ctx context.Context) error

Connect takes a transaction from the pool for exclusive use by the client.

func (*Client) Exec

func (p *Client) Exec(ctx context.Context, query string, args ...any) error

Exec executes a query with args (positional, map, or struct for named params).

func (*Client) Get

func (p *Client) Get(ctx context.Context, dest any, query string, args ...any) error

Get scans the first result into dest (pointer to struct).

func (*Client) Insert

func (p *Client) Insert(ctx context.Context, table string, value any) error

Insert inserts a struct with `db` tags or a string-keyed map into the table. Uses NamedExecContext to bind fields to :name placeholders.

func (*Client) InsertID

func (p *Client) InsertID() int64

InsertID returns the last insert ID.

func (*Client) Query

func (p *Client) Query(ctx context.Context, query string, args ...any) error

Query is an alias of Exec, added for usage ergonomics.

func (*Client) Replace

func (p *Client) Replace(ctx context.Context, table string, value any) error

Replace performs a REPLACE INTO operation with a struct or string-keyed map.

func (*Client) Rollback

func (p *Client) Rollback() error

Rollback rolls back the current transaction. Returns nil when no transaction is in progress.

func (*Client) RowsAffected

func (p *Client) RowsAffected() int64

RowsAffected returns the number of rows affected by the last write operation.

func (*Client) Select

func (p *Client) Select(ctx context.Context, dest any, query string, args ...any) error

Select scans all results into dest (pointer to slice of structs).

func (*Client) Update

func (p *Client) Update(ctx context.Context, table string, value any, keyCols ...string) error

Update updates rows using a struct or string-keyed map. keyCols specify WHERE columns.

func (*Client) WithObserver

func (p *Client) WithObserver(fn ObserveFunc)

WithObserver sets an observer to the client.

type ObserveFunc

type ObserveFunc func(ctx context.Context, entry QueryLogEntry)

ObserveFunc is the callback signature for query observation.

type Observer

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

Observer collects query log entries.

func (*Observer) Clear

func (o *Observer) Clear()

Clear removes all collected entries.

func (*Observer) Entries

func (o *Observer) Entries() []QueryLogEntry

Entries returns the collected entries.

func (*Observer) Observe

func (o *Observer) Observe(ctx context.Context, entry QueryLogEntry)

Observe records a query log entry.

type QueryLogEntry

type QueryLogEntry struct {
	Query    string
	Args     any
	Started  time.Time
	Duration time.Duration
	Err      error
	TxDepth  int
}

QueryLogEntry records a single query execution for observability.

Jump to

Keyboard shortcuts

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