feat: 🎉 postgresql broker first commit of forked prototype from my original code
This commit is contained in:
77
pkg/broker/adapter/database.go
Normal file
77
pkg/broker/adapter/database.go
Normal file
@@ -0,0 +1,77 @@
|
||||
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)
|
||||
Reference in New Issue
Block a user