database

package
v0.1.0-rc.1 Latest Latest
Warning

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

Go to latest
Published: Nov 11, 2025 License: GPL-3.0 Imports: 17 Imported by: 0

Documentation

Index

Constants

View Source
const (
	// ErrCodeUniqueViolation is the PostgreSQL error code for unique constraint violations
	ErrCodeUniqueViolation = "23505"
	// ErrCodeForeignKeyViolation is the PostgreSQL error code for foreign key violations
	ErrCodeForeignKeyViolation = "23503"
	// ErrCodeCheckViolation is the PostgreSQL error code for check constraint violations
	ErrCodeCheckViolation = "23514"
)

PostgreSQL error codes

Variables

This section is empty.

Functions

func GetConstraintName

func GetConstraintName(err error) string

GetConstraintName returns the constraint name from a PostgreSQL error

func IsCheckViolation

func IsCheckViolation(err error) bool

IsCheckViolation checks if an error is a check constraint violation

func IsForeignKeyViolation

func IsForeignKeyViolation(err error) bool

IsForeignKeyViolation checks if an error is a foreign key violation

func IsUniqueViolation

func IsUniqueViolation(err error) bool

IsUniqueViolation checks if an error is a unique constraint violation

Types

type ColumnInfo

type ColumnInfo struct {
	Name         string  `json:"name"`
	DataType     string  `json:"data_type"`
	IsNullable   bool    `json:"is_nullable"`
	DefaultValue *string `json:"default_value"`
	IsPrimaryKey bool    `json:"is_primary_key"`
	IsForeignKey bool    `json:"is_foreign_key"`
	IsUnique     bool    `json:"is_unique"`
	MaxLength    *int    `json:"max_length"`
	Position     int     `json:"position"`
}

ColumnInfo represents metadata about a table column

type Connection

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

Connection represents a database connection pool

func NewConnection

func NewConnection(cfg config.DatabaseConfig) (*Connection, error)

NewConnection creates a new database connection pool

func (*Connection) BeginTx

func (c *Connection) BeginTx(ctx context.Context) (pgx.Tx, error)

BeginTx starts a new transaction

func (*Connection) Close

func (c *Connection) Close()

Close closes the database connection pool

func (*Connection) Exec

func (c *Connection) Exec(ctx context.Context, sql string, args ...interface{}) (pgconn.CommandTag, error)

Exec executes a query that doesn't return rows

func (*Connection) Health

func (c *Connection) Health(ctx context.Context) error

Health checks the health of the database connection

func (*Connection) Inspector

func (c *Connection) Inspector() *SchemaInspector

Inspector returns the schema inspector

func (*Connection) Migrate

func (c *Connection) Migrate() error

Migrate runs database migrations from both system and user sources

func (*Connection) Pool

func (c *Connection) Pool() *pgxpool.Pool

Pool returns the underlying connection pool

func (*Connection) Query

func (c *Connection) Query(ctx context.Context, sql string, args ...interface{}) (pgx.Rows, error)

Query executes a query that returns rows

func (*Connection) QueryRow

func (c *Connection) QueryRow(ctx context.Context, sql string, args ...interface{}) pgx.Row

QueryRow executes a query that returns a single row

func (*Connection) Stats

func (c *Connection) Stats() *pgxpool.Stat

Stats returns database connection pool statistics

type ForeignKey

type ForeignKey struct {
	Name             string `json:"name"`
	ColumnName       string `json:"column_name"`
	ReferencedTable  string `json:"referenced_table"`
	ReferencedColumn string `json:"referenced_column"`
	OnDelete         string `json:"on_delete"`
	OnUpdate         string `json:"on_update"`
}

ForeignKey represents a foreign key relationship

type FunctionInfo

type FunctionInfo struct {
	Schema      string          `json:"schema"`
	Name        string          `json:"name"`
	Description string          `json:"description"`
	Parameters  []FunctionParam `json:"parameters"`
	ReturnType  string          `json:"return_type"`
	IsSetOf     bool            `json:"is_set_of"`
	Volatility  string          `json:"volatility"` // VOLATILE, STABLE, IMMUTABLE
	Language    string          `json:"language"`
}

FunctionInfo represents metadata about a database function

type FunctionParam

type FunctionParam struct {
	Name       string `json:"name"`
	Type       string `json:"type"`
	Mode       string `json:"mode"` // IN, OUT, INOUT
	HasDefault bool   `json:"has_default"`
	Position   int    `json:"position"`
}

FunctionParam represents a function parameter

type IndexInfo

type IndexInfo struct {
	Name      string   `json:"name"`
	Columns   []string `json:"columns"`
	IsUnique  bool     `json:"is_unique"`
	IsPrimary bool     `json:"is_primary"`
}

IndexInfo represents an index on a table

type SchemaInspector

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

SchemaInspector provides PostgreSQL schema introspection capabilities

func NewSchemaInspector

func NewSchemaInspector(conn *Connection) *SchemaInspector

NewSchemaInspector creates a new schema inspector

func (*SchemaInspector) BuildRESTPath

func (si *SchemaInspector) BuildRESTPath(table TableInfo) string

BuildRESTPath builds a REST API path for a table

func (*SchemaInspector) GetAllFunctions

func (si *SchemaInspector) GetAllFunctions(ctx context.Context, schemas ...string) ([]FunctionInfo, error)

GetAllFunctions retrieves information about all functions in the specified schemas

func (*SchemaInspector) GetAllTables

func (si *SchemaInspector) GetAllTables(ctx context.Context, schemas ...string) ([]TableInfo, error)

GetAllTables retrieves information about all tables in the specified schemas

func (*SchemaInspector) GetAllViews

func (si *SchemaInspector) GetAllViews(ctx context.Context, schemas ...string) ([]TableInfo, error)

GetAllViews retrieves information about all views in the specified schemas

func (*SchemaInspector) GetSchemas

func (si *SchemaInspector) GetSchemas(ctx context.Context) ([]string, error)

GetSchemas retrieves all available schemas

func (*SchemaInspector) GetTableInfo

func (si *SchemaInspector) GetTableInfo(ctx context.Context, schema, table string) (*TableInfo, error)

GetTableInfo retrieves detailed information about a specific table

type TableInfo

type TableInfo struct {
	Schema      string       `json:"schema"`
	Name        string       `json:"name"`
	RESTPath    string       `json:"rest_path,omitempty"` // The REST API path for this table (e.g., "/auth/users")
	Columns     []ColumnInfo `json:"columns"`
	PrimaryKey  []string     `json:"primary_key"`
	ForeignKeys []ForeignKey `json:"foreign_keys"`
	Indexes     []IndexInfo  `json:"indexes"`
	RLSEnabled  bool         `json:"rls_enabled"`
}

TableInfo represents metadata about a database table

Jump to

Keyboard shortcuts

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