db

package
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Jul 21, 2026 License: Apache-2.0 Imports: 19 Imported by: 0

Documentation

Overview

Package db provides SQL execution against CockroachDB for the MCP server.

Index

Constants

This section is empty.

Variables

View Source
var ErrInvalidConfig = errors.New("invalid adapter configuration")

ErrInvalidConfig marks operator configuration errors that should surface as a clean stderr message rather than a structured log entry.

Functions

func IsInsufficientPrivilege

func IsInsufficientPrivilege(err error) bool

IsInsufficientPrivilege reports whether err carries SQLSTATE 42501.

func SafeFormat

func SafeFormat(format string, args ...any) (string, error)

SafeFormat substitutes %N placeholders with quoted args: SQLStringer values render themselves, strings become SQL literals, int64 renders as decimal. Each placeholder must be preceded by punctuation, whitespace, or '=' so that `col%1` cannot silently become `col'value'`.

Types

type Adapter

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

Adapter centralizes connection pooling and SQL execution against CockroachDB.

func NewAdapter

func NewAdapter(ctx context.Context, cfg Config) (*Adapter, error)

NewAdapter creates a new pgxpool-backed adapter and verifies connectivity.

func (*Adapter) Close

func (a *Adapter) Close()

Close releases the underlying connection pool.

func (*Adapter) Exec

func (a *Adapter) Exec(ctx context.Context, sql string) (_ int64, err error)

Exec runs a non-result-returning statement (DDL/DML) and returns the number of rows affected.

func (*Adapter) Query

func (a *Adapter) Query(ctx context.Context, sql string) (_ *QueryResult, err error)

Query executes a query and returns columns and rows.

type Config

type Config struct {
	DSN          string
	QueryTimeout time.Duration
	// ReadOnly forces every pool session into read-only mode at SQL layer.
	ReadOnly bool
	// AllowPasswordAuth permits password-based connections when true; rejected
	// by default to keep credentials out of the agent host environment.
	AllowPasswordAuth bool
	MaxConns          int32
	TxnQoS            string
}

Config is the subset of server configuration the adapter needs to open a pool. It is intentionally narrow so the adapter does not depend on the config package.

type Identifier

type Identifier string

Identifier renders as a double-quoted SQL identifier via lexbase.

func (Identifier) SQLString

func (i Identifier) SQLString() string

SQLString implements SQLStringer.

type Manager

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

Manager is the high-level facade used by MCP tool handlers.

func NewManager

func NewManager(ctx context.Context, cfg *config.Config) (*Manager, error)

NewManager opens an adapter using the provided server config.

func (*Manager) Close

func (m *Manager) Close()

Close releases the underlying pool.

func (*Manager) Exec

func (m *Manager) Exec(ctx context.Context, sql string) (int64, error)

Exec runs a non-result-returning statement (DDL/DML) and returns the number of rows affected.

func (*Manager) Query

func (m *Manager) Query(ctx context.Context, sql string) (*QueryResult, error)

Query runs a query against the bootstrap database.

type QualifiedIdentifier

type QualifiedIdentifier []string

QualifiedIdentifier renders as dot-joined quoted identifiers ("db"."schema"."tbl").

func (QualifiedIdentifier) SQLString

func (q QualifiedIdentifier) SQLString() string

SQLString implements SQLStringer.

type QueryResult

type QueryResult struct {
	Columns []string
	Rows    [][]any
}

QueryResult is the projection of a query result returned to tool handlers.

type SQL

type SQL string

SQL is a raw SQL fragment inlined verbatim. The caller is responsible for the safety of its contents.

func (SQL) SQLString

func (s SQL) SQLString() string

SQLString implements SQLStringer.

type SQLStringer

type SQLStringer interface {
	SQLString() string
}

SQLStringer renders itself as a SQL fragment (identifier, literal, or raw SQL).

Jump to

Keyboard shortcuts

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