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 CheckConnection(ctx context.Context, cfg *models.Config, cred *models.DriverCredentials) (string, error)
- func EngineIsSQLiteLike(engine string) bool
- func EnsureMySQLDatabase(ctx context.Context, cred *models.DriverCredentials, name string) error
- func ErrCredentialsRequired() error
- func ExplainConnectionError(cred *models.DriverCredentials, err error) error
- func InvokeDDLPostCreateHook(ctx context.Context, cfg *models.Config, project *models.Project, ...) error
- func IsAlreadyExistsErr(err error) bool
- func MySQLCompatDDL(query string) (string, []uint16)
- func OpenBun(cfg *models.Config, cred *models.DriverCredentials) (*bun.DB, error)
- func OpenPostgresSQLDB(cred *models.DriverCredentials) (*sql.DB, error)
- func PostgresSearchPathSQL(schema string) string
- func QuoteMySQLIdent(s string) string
- func QuotePGIdent(s string) string
- func RebindIdentQuotesPG(sql string) string
- func WithCheckTimeout(ctx context.Context) (context.Context, context.CancelFunc)
- 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 CheckConnection ¶ added in v1.0.35
func CheckConnection(ctx context.Context, cfg *models.Config, cred *models.DriverCredentials) (string, error)
CheckConnection opens the same Bun handle the project driver would open, pings it, reads the server version and closes it. It never creates a database, schema or table.
func EngineIsSQLiteLike ¶
EngineIsSQLiteLike reports engines that use SQLite semantics (embedded SQLite, libsql, Turso).
func EnsureMySQLDatabase ¶ added in v1.0.35
EnsureMySQLDatabase creates database `name` on the MySQL/MariaDB server in cred when it does not exist yet. The connection is made without selecting a database, so it works before the project database exists.
func ErrCredentialsRequired ¶
func ErrCredentialsRequired() error
ErrCredentialsRequired is returned when driver credentials are missing.
func ExplainConnectionError ¶ added in v1.0.35
func ExplainConnectionError(cred *models.DriverCredentials, err error) error
ExplainConnectionError turns driver errors into operator text. The result never contains the password.
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 MySQLCompatDDL ¶ added in v1.0.35
MySQLCompatDDL rewrites one statement for MySQL and returns the error numbers that mean "already done" for it (nil when the statement is unchanged).
func OpenPostgresSQLDB ¶ added in v1.0.35
func OpenPostgresSQLDB(cred *models.DriverCredentials) (*sql.DB, error)
OpenPostgresSQLDB opens a database/sql handle through bun's pgdriver.
BuildPostgresDSN emits a libpq-style DSN where schema isolation is `options=-csearch_path=<schema>,public` (what psql/pg_dump understand). pgdriver instead turns every unknown DSN parameter into `SET <name> TO $1` after connecting, and "options" is not a settable parameter, so the server rejects it. Here the options parameter is dropped and the search path is set on each new connection with properly quoted identifiers.
func PostgresSearchPathSQL ¶ added in v1.0.35
PostgresSearchPathSQL returns the SET statement for a schema-isolated project ("" when no schema is configured).
func QuoteMySQLIdent ¶
QuoteMySQLIdent returns a MySQL backtick-quoted identifier for DDL.
func QuotePGIdent ¶
QuotePGIdent returns a PostgreSQL double-quoted identifier for DDL.
func RebindIdentQuotesPG ¶ added in v1.0.35
RebindIdentQuotesPG converts backtick-quoted identifiers (`name`, with “ as an escaped backtick) to PostgreSQL double-quoted identifiers ("name", with "" escaping), leaving single-quoted literals, double-quoted identifiers, dollar-quoted bodies and comments untouched.
The shared SQL driver builds SQL in the SQLite/MySQL quoting style; a backtick is never valid PostgreSQL syntax, so this mapping is unambiguous. It plays the same role for identifier quotes that bun's `?` → `$n` rebinding plays for placeholders.
func WithCheckTimeout ¶ added in v1.0.35
WithCheckTimeout returns ctx with the default check deadline when it has none.
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).