Documentation
¶
Overview ¶
Package sqlcommon holds shared SQL project driver logic and dialect hooks. Engine packages (sqlite, postgres, mysql, mariadb) provide connection init and Dialect implementations.
Index ¶
- func ApplyLitestreamConnectionPragmas(ctx context.Context, orm *bun.DB, engine string) error
- func ApplySQLiteConnectionPragmas(ctx context.Context, orm *bun.DB, engine, dsn string, opts ...SQLitePragmaOpts) error
- func BuildPostgresDSN(c *models.DriverCredentials) (string, error)
- func EngineIsSQLiteLike(engine string) bool
- func ErrCredentialsRequired() error
- func InvokeDDLPostCreateHook(ctx context.Context, cfg *models.Config, project *models.Project, ...) error
- func IsAlreadyExistsErr(err error) bool
- func OpenBun(cfg *models.Config, cred *models.DriverCredentials) (*bun.DB, error)
- func QuoteMySQLIdent(s string) string
- func QuotePGIdent(s string) string
- type Base
- type BunDriver
- type Dialect
- type Driver
- func (d *Driver) Bun() *bun.DB
- func (d *Driver) Close() error
- func (d *Driver) Engine() string
- func (d *Driver) InsertShadowIDs(ctx context.Context, table string, ids []string) (int, error)
- func (d *Driver) ListDistinctNonEmptyColumn(ctx context.Context, table, column string) ([]string, error)
- func (d *Driver) Ping() error
- func (d *Driver) QueryReadOnly(ctx context.Context, query string, limit int) ([]string, [][]interface{}, error)
- type ProjectDBPeelable
- type SQLDriverCarrier
- type SQLitePragmaOpts
- type SyncLocker
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func ApplyLitestreamConnectionPragmas ¶
ApplyLitestreamConnectionPragmas configures SQLite for embedded Litestream local files.
func ApplySQLiteConnectionPragmas ¶
func ApplySQLiteConnectionPragmas(ctx context.Context, orm *bun.DB, engine, dsn string, opts ...SQLitePragmaOpts) error
ApplySQLiteConnectionPragmas sets FK enforcement and performance-related PRAGMAs for SQLite/libsql. Optional cacheSizeKB / mmapBytes override defaults (20000 KiB cache, 128 MiB mmap) when > 0.
func BuildPostgresDSN ¶
func BuildPostgresDSN(c *models.DriverCredentials) (string, error)
BuildPostgresDSN builds a libpq URL for pgdriver from credentials.
func EngineIsSQLiteLike ¶
EngineIsSQLiteLike reports engines that use SQLite semantics (embedded SQLite, libsql, Turso).
func ErrCredentialsRequired ¶
func ErrCredentialsRequired() error
ErrCredentialsRequired is returned when driver credentials are missing.
func InvokeDDLPostCreateHook ¶
func InvokeDDLPostCreateHook(ctx context.Context, cfg *models.Config, project *models.Project, model *models.ModelType, dbHandle interface{}) error
InvokeDDLPostCreateHook runs Config.DDLPostCreateHook after open-core creates a model table.
func IsAlreadyExistsErr ¶
IsAlreadyExistsErr reports duplicate database/schema errors during bootstrap DDL.
func QuoteMySQLIdent ¶
QuoteMySQLIdent returns a MySQL backtick-quoted identifier for DDL.
func QuotePGIdent ¶
QuotePGIdent returns a PostgreSQL double-quoted identifier for DDL.
Types ¶
type Base ¶
type Base struct {
Conf *models.Config
ORM *bun.DB
DriverCredential *models.DriverCredentials
SyncLock SyncLocker
Dialect Dialect
// contains filtered or unexported fields
}
Base holds shared Bun ORM state for all SQL project drivers.
func (*Base) LockSQLiteWrite ¶
LockSQLiteWrite acquires SQLite-like write serialization when applicable.
func (*Base) RunInTxOrBypass ¶
func (b *Base) RunInTxOrBypass(ctx context.Context, fn func(ctx context.Context, tx bun.IDB) error) error
RunInTxOrBypass runs fn on the bare connection for SQLite-like engines, else inside a transaction.
func (*Base) UnlockSQLiteWrite ¶
func (b *Base) UnlockSQLiteWrite()
UnlockSQLiteWrite releases SQLite-like write serialization.
func (*Base) WithWriteLock ¶
WithWriteLock runs fn while holding SQLite-like write serialization when applicable.
type Dialect ¶
type Dialect interface {
Engine() string
QuoteIdent(name string) string
SupportsTransactionalDDL() bool
IsSQLiteLike() bool
IsRemoteSQLiteLikeTurso(d *Driver) bool
PreferDDLRunInTx(d *Driver) bool
RunDDLBatch(ctx context.Context, d *Driver, fn func(bun.IDB) error) error
RootResolverQueryBuilder(conf *models.Config, param *models.CommonSystemParams, returnCount bool) (string, []interface{}, error)
}
Dialect captures engine-specific SQL behavior for the shared driver.
type Driver ¶
type Driver struct {
Base
}
Driver is the shared SQL project driver implementation used by engine packages.
func OpenDriver ¶
func OpenDriver(cfg *models.Config, cred *models.DriverCredentials, dialect Dialect) (*Driver, error)
OpenDriver opens a Bun handle and returns a Driver with the given dialect.
func (*Driver) InsertShadowIDs ¶
InsertShadowIDs upserts id-only rows into a common-model shadow table.
func (*Driver) ListDistinctNonEmptyColumn ¶
func (d *Driver) ListDistinctNonEmptyColumn(ctx context.Context, table, column string) ([]string, error)
ListDistinctNonEmptyColumn returns unique non-empty values from table.column. Missing tables yield an empty slice (no error) so shadow repair is idempotent.
type ProjectDBPeelable ¶
type ProjectDBPeelable interface {
PeelProjectDB() interfaces.ProjectDBInterface
}
ProjectDBPeelable is implemented by decorators that wrap a project SQL driver.
type SQLDriverCarrier ¶
SQLDriverCarrier is implemented by open-core and pro SQL project drivers for dbexplorer unwrap.
type SQLitePragmaOpts ¶
SQLitePragmaOpts tunes cache/mmap for multi-tenant capacity.
type SyncLocker ¶
type SyncLocker interface {
LockForTx()
UnlockForTx()
}
SyncLocker optionally serializes foreground DB operations (legacy hook).