Documentation
¶
Overview ¶
Package dbconnector provides interfaces and implementations for connecting to databases.
Index ¶
- Constants
- type Connector
- type Database
- type DatabaseErrorDetail
- type Query
- type QueryErrorDetail
- type Result
- type RollbackErrorDetail
- type Row
- type Rows
- type TenantConfig
- type TenantConfigBuilder
- type TenantErrorDetail
- type TenantProvider
- type TenantProviderConfig
- type Transaction
- type TransactionFN
Constants ¶
const ( ModuleCode = "dbconnector" ErrCodeConnectionFailed = ModuleCode + ".002" ErrCodeTenantNotFound = ModuleCode + ".003" ErrCodeCannotBeginTx = ModuleCode + ".005" ErrCodeCannotCommitTx = ModuleCode + ".006" ErrCodeCannotRollbackTx = ModuleCode + ".007" ErrCodeNotSupported = ModuleCode + ".008" ErrCodeQueryFailed = ModuleCode + ".009" ErrCodeCloseFailed = ModuleCode + ".010" ErrCodeGenericDBError = ModuleCode + ".011" )
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Connector ¶
type Connector interface {
// Connect connects to the database.
Connect(ctx context.Context, tenantID string) (Database, error)
// Reload reloads the tenants and reconfigure the database pool.
Reload() error
}
Connector is the database connector.
type Database ¶
type Database interface {
Query
// RunInTransaction executes the given function in a transaction.
RunInTransaction(ctx context.Context, fn TransactionFN) error
// Close closes the database.
Close(ctx context.Context) error
}
Database is the database interface.
type DatabaseErrorDetail ¶
type DatabaseErrorDetail struct {
TenantErrorDetail
DatabaseErrorCode string `json:"databaseErrorCode"`
DatabaseError string `json:"databaseError"`
}
DatabaseErrorDetail is a struct that contains the details of an error returned by ConnectionError.
type Query ¶
type Query interface {
// TenantConfig returns the tenant config.
TenantConfig() TenantConfig
// Query executes a query and returns the result.
Query(ctx context.Context, query string, args ...interface{}) (Rows, error)
// QueryRow executes a query that is expected to return at most one row.
QueryRow(ctx context.Context, query string, args ...interface{}) Row
}
Query is the query interface.
type QueryErrorDetail ¶
type QueryErrorDetail struct {
DatabaseErrorDetail
QueryScript string `json:"queryScript"`
QueryArgs []interface{} `json:"args"`
}
QueryErrorDetail is a struct that contains the details of an error returned by QueryError.
type Result ¶
type Result interface {
// LastInsertId returns the last inserted ID.
LastInsertId() (int64, error)
// RowsAffected returns the number of rows affected.
RowsAffected() (int64, error)
}
Result is the result of a query.
type RollbackErrorDetail ¶
type RollbackErrorDetail struct {
DatabaseError errorex.EX `json:"databaseError"`
OriginalError errorex.EX `json:"originalError"`
}
RollbackErrorDetail is a struct that contains the details of an error returned by RollbackError.
type Row ¶
type Row interface {
// Scan copies the columns in the current row into the values pointed at by dest.
Scan(dest ...interface{}) error
}
Row is a row in the result set.
type Rows ¶
type Rows interface {
Row
// Next prepares the next row for reading.
Next() bool
// Err returns any error that occurred while reading.
Err() error
// Close closes the row.
Close() error
}
Rows is the result set.
type TenantConfig ¶
type TenantConfig interface {
// TenantID is the ID of the tenant.
TenantID() string
// TenantName is the name of the tenant.
TenantName() string
// DatabaseURL is the URL of the database for the tenant.
DatabaseURL() string
}
TenantConfig is the configuration for a tenant.
type TenantConfigBuilder ¶
type TenantConfigBuilder interface {
// WithTenantID sets the tenant ID.
WithTenantID(id string) TenantConfigBuilder
// WithTenantName sets the tenant name.
WithTenantName(name string) TenantConfigBuilder
// WithDatabaseURL sets the database URL.
WithDatabaseURL(url string) TenantConfigBuilder
// Build creates a TenantConfig.
Build() TenantConfig
}
TeanantConfigBuilder is a builder for TenantConfig.
func NewTenantConfigBuilder ¶
func NewTenantConfigBuilder() TenantConfigBuilder
NewTenantConfigBuilder creates a new TenantConfigBuilder.
type TenantErrorDetail ¶
type TenantErrorDetail struct {
TenantID string `json:"tenantId"`
}
TenantErrorDetail is a struct that contains the details of an error returned by TenantError.
type TenantProvider ¶
type TenantProvider interface {
// Configure configures the tenant provider.
Configure() error
// LoadTenants loads the tenants.
LoadTenants() ([]TenantConfig, error)
}
TenantProvider is the tenant provider interface.
type TenantProviderConfig ¶
type TenantProviderConfig struct {
// URL is the URL of the tenant provider.
URL string
// Token is the token for the tenant provider.
Token string
}
TenantProviderConfig is the configuration for the tenant provider.
type Transaction ¶
type Transaction interface {
Query
// Exec executes a query without returning any rows.
Exec(ctx context.Context, query string, args ...interface{}) (Result, error)
}
Transaction is a database transaction.
type TransactionFN ¶
type TransactionFN func(ctx context.Context, tx Transaction) error
TransactionFN is the transaction function.