Files
pgsql-broker/pkg/broker/adapter/database.go

78 lines
2.2 KiB
Go

package adapter
import (
"context"
"database/sql"
)
// DBAdapter defines the interface for database operations
type DBAdapter interface {
// Connect establishes a connection to the database
Connect(ctx context.Context) error
// Close closes the database connection
Close() error
// Ping checks if the database is reachable
Ping(ctx context.Context) error
// Begin starts a new transaction
Begin(ctx context.Context) (DBTransaction, error)
// Exec executes a query without returning rows
Exec(ctx context.Context, query string, args ...interface{}) (sql.Result, error)
// QueryRow executes a query that returns at most one row
QueryRow(ctx context.Context, query string, args ...interface{}) DBRow
// Query executes a query that returns rows
Query(ctx context.Context, query string, args ...interface{}) (DBRows, error)
// Listen starts listening on a PostgreSQL notification channel
Listen(ctx context.Context, channel string, handler NotificationHandler) error
// Unlisten stops listening on a channel
Unlisten(ctx context.Context, channel string) error
}
// DBTransaction defines the interface for database transactions
type DBTransaction interface {
// Exec executes a query within the transaction
Exec(ctx context.Context, query string, args ...interface{}) (sql.Result, error)
// QueryRow executes a query that returns at most one row within the transaction
QueryRow(ctx context.Context, query string, args ...interface{}) DBRow
// Query executes a query that returns rows within the transaction
Query(ctx context.Context, query string, args ...interface{}) (DBRows, error)
// Commit commits the transaction
Commit() error
// Rollback rolls back the transaction
Rollback() error
}
// DBRow defines the interface for scanning a single row
type DBRow interface {
Scan(dest ...interface{}) error
}
// DBRows defines the interface for scanning multiple rows
type DBRows interface {
Next() bool
Scan(dest ...interface{}) error
Close() error
Err() error
}
// Notification represents a PostgreSQL NOTIFY event
type Notification struct {
Channel string
Payload string
PID int
}
// NotificationHandler is called when a notification is received
type NotificationHandler func(notification *Notification)