Documentation
¶
Overview ¶
Package database resolves a Popcorn Web rdb DSN onto the engine that opens it.
A DSN is scheme://rest, and the scheme names an engine rather than a database/sql driver. The distinction matters: the PostgreSQL package builds its *sql.DB from a connector and registers no driver name at all, so sql.Open could never reach it. The scheme also decides how much of the DSN the engine receives, because a libpq URL is a valid DSN on its own while a SQLite path and a go-sql-driver DSN are not URLs and have to lose the prefix.
An engine registers itself from init, so a project links only the engine its DSN selects:
import _ "github.com/shibukawa/popcornweb/database/postgres"
An engine may also register a native opener, which the request-time query path uses instead of database/sql: the sql.DB pool mutex, the per-conn mutex, and driver.Value boxing all disappear from that path. The *sql.DB opener stays mandatory beside it, because migration and seeding tooling runs on database/sql regardless of how requests are served.
Index ¶
- func Dialect(configured string) (string, error)
- func Register(engine Engine)
- func Scheme(configured string) (scheme, rest string, err error)
- func Schemes() []string
- type Engine
- type NativeDB
- type NativeExecutor
- type NativeTx
- type NativeTxOptions
- type OpenFunc
- type OpenNativeFunc
- type PoolBounds
- type Target
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Register ¶
func Register(engine Engine)
Register adds an engine. Engine packages call it from init; registering the same dialect twice is harmless, so importing an engine along two paths is not an error.
Types ¶
type Engine ¶
type Engine struct {
// Dialect is the canonical name reported by pw.DBDriver, and the one that
// selects savepoint, EXPLAIN, and migration behavior. Every scheme of an
// engine reports the same dialect, so an alias cannot produce a second one.
Dialect string
// Schemes are the DSN prefixes that select this engine.
Schemes []string
// Open opens the pool for the resolved data source.
Open OpenFunc
// OpenNative, when set, opens the pool the request-time query path uses
// instead of database/sql. Open stays required beside it, because
// migration and seeding tooling runs on database/sql regardless.
OpenNative OpenNativeFunc
// KeepScheme hands Open the whole configured DSN instead of the part after
// the scheme, for an engine whose DSN is already a URL.
KeepScheme bool
}
Engine describes one database a project can be configured against.
type NativeDB ¶
type NativeDB interface {
NativeExecutor
BeginTx(ctx context.Context, options NativeTxOptions) (NativeTx, error)
Ping(ctx context.Context) error
Close() error
}
NativeDB is the request-time handle of an engine that bypasses database/sql. It is framework-defined so the registry, and everything above it, never sees a driver type.
type NativeExecutor ¶
type NativeExecutor interface {
sqlbind.SQLExecutor
sqlbind.RowsQuerier
}
NativeExecutor is the statement surface of a native handle. It satisfies sqlbind.SQLExecutor so the framework's executor seam can store it, and adds the driver-agnostic query path sqlbind.Query dispatches to; the QueryContext half of SQLExecutor is expected to be sqlbind.UnimplementedQuerier, because a backend outside database/sql cannot construct a *sql.Rows.
type NativeTx ¶
type NativeTx interface {
NativeExecutor
Commit(ctx context.Context) error
Rollback(ctx context.Context) error
}
NativeTx is one open transaction on a NativeDB. Commit and Rollback take a context because the native protocol sends them on the wire, unlike sql.Tx which bound its context at Begin.
type NativeTxOptions ¶
type NativeTxOptions struct {
ReadOnly bool
}
NativeTxOptions selects how a native transaction begins.
type OpenFunc ¶
OpenFunc opens a pool for one data source. It wraps sql.Open for an engine that registers a driver name, and the engine's own constructor for one that does not.
type OpenNativeFunc ¶
type OpenNativeFunc func(ctx context.Context, dataSource string, bounds PoolBounds) (NativeDB, error)
OpenNativeFunc opens a native pool for one data source with the configured bounds already applied.
type PoolBounds ¶
type PoolBounds struct {
MaxOpenConns int
// MaxIdleConns exists for symmetry with the sql.DB setter but has no
// equivalent on a native pool, which prunes idle connections by
// ConnMaxIdleTime instead. An engine that cannot express it ignores it.
MaxIdleConns int
ConnMaxLifetime time.Duration
ConnMaxIdleTime time.Duration
}
PoolBounds carries the configured pool limits to a native opener. A native pool is configured at construction rather than adjusted afterwards, which is why the bounds travel with the open call instead of being applied to its result the way sql.DB setters are.
type Target ¶
type Target struct {
// Dialect is the canonical engine name.
Dialect string
// DataSource is the part of the DSN the engine expects.
DataSource string
// contains filtered or unexported fields
}
Target is a resolved DSN: which dialect it names, and what to open.
func (Target) OpenNative ¶
OpenNative opens the native pool with bounds applied. The caller still pings, because a native pool connects lazily.
Directories
¶
| Path | Synopsis |
|---|---|
|
Package d1 registers Cloudflare D1 for a d1:// DSN.
|
Package d1 registers Cloudflare D1 for a d1:// DSN. |
|
Package dynamo opens the application's DynamoDB client from configuration and keeps it as process state every operation reaches through Handle.
|
Package dynamo opens the application's DynamoDB client from configuration and keeps it as process state every operation reaches through Handle. |
|
Package firestore opens the application's Firestore client from configuration and keeps it as process state every operation reaches through Handle.
|
Package firestore opens the application's Firestore client from configuration and keeps it as process state every operation reaches through Handle. |
|
Package mysql registers the MySQL and MariaDB engine for a mysql:// DSN.
|
Package mysql registers the MySQL and MariaDB engine for a mysql:// DSN. |
|
Package postgres registers the PostgreSQL engine for a postgres:// DSN.
|
Package postgres registers the PostgreSQL engine for a postgres:// DSN. |
|
Package sqlite registers the SQLite engine for a sqlite:// DSN.
|
Package sqlite registers the SQLite engine for a sqlite:// DSN. |