{"modulePath":"github.com/epzero/basamento","version":"v0.0.0-20261123040955-ecee8c2d92a7","isLatest":true,"isStandardLibrary":false,"goos":"linux","goarch":"amd64","docs":"# package sql\n\nPackage sql is the SQL port of platform: SQLite in the embedded profile and Postgres in the cloud profile, selected by URL (ADR-0004). Migrations come from an fs.FS (typically embed.FS). Repository code built on this store is expected to read the tenant from context (REQ-SQL-010 through REQ-SQL-012); this package does not enforce tenancy by itself.\n\n### URLs {#hdr-URLs}\n\n[Open](#Open) accepts sqlite://… and postgres://… URLs. The legacy postgresql scheme and any other scheme are invalid\\_argument. SQLite forms include sqlite:///absolute/path, sqlite://relative/path, sqlite://host/path (RFC 3986 host plus path, for example sqlite://data/kit.db), and sqlite://:memory: or sqlite:///:memory:. A relative path is resolved against the working directory. Query and fragment on SQLite URLs are rejected. The path :memory: is never passed through filepath.Abs. Postgres URLs must include a database path; otherwise nothing is created.\n\n### Migrations and locking {#hdr-Migrations_and_locking}\n\nConcurrent opens run migrations under an advisory flock on \\\u003cdbpath\u003e.migrate.lock beside the database for SQLite; schema version is checked again inside that critical section, then goose runs. On Postgres, a session pg\\_advisory\\_lock on one dedicated pool connection is held for the whole migrate and schema version is re-checked under that lock. A schema version newer than the migration set's newest file is refused with unsupported before anything is written.\n\n### SQLite files and DSN {#hdr-SQLite_files_and_DSN}\n\nNew SQLite database files are created mode 0600; missing parent directories are created mode 0700 (existing directories are not chmodded). The SQLite driver DSN sets busy\\_timeout before journal\\_mode=WAL, foreign\\_keys, and \\_txlock=immediate, matching the embedded Temporal store.\n\n### Errors {#hdr-Errors}\n\nOperations return \\*core.Error with fixed messages. Driver and vendor text is not forwarded in messages or details; stable codes such as SQLSTATE or SQLite result codes may appear in details. Connection refused, DNS failure, or timeout while opening map to unavailable with Retryable true. Postgres serialization failure and deadlock (40001, 40P01), connection and resource classes (08xxx, 53xxx, 57P01), and SQLite SQLITE\\_BUSY / SQLITE\\_LOCKED map to unavailable with Retryable true. Unique and foreign-key violations map to conflict; not-null and check violations map to invalid\\_argument. [Store.ParsedURLRedacted](#Store.ParsedURLRedacted) and error details replace the userinfo password and the values of the credential query parameters (password, sslpassword, passfile, sslcert, sslkey, sslrootcert) with the literal \\*\\*\\*. When the URL carried any of them, driver text is replaced by the fixed cause \"driver error withheld\". [MapError](#MapError) applies the same mapping to a driver error taken straight from database/sql row iteration, which bypasses [Execer](#Execer) and [Row](#Row); repository code calls it on [database/sql.Rows.Scan](/database/sql#Rows.Scan) and [database/sql.Rows.Err](/database/sql#Rows.Err) results.\n\n### Transactions and SQL {#hdr-Transactions_and_SQL}\n\n[Store.InTx](#Store.InTx) rejects a nested InTx on the same store when fn uses the ctx InTx provides. [Execer](#Execer) documents the ? placeholder contract on both dialects. [TextOrderClause](#TextOrderClause) takes a simple SQL identifier for the column name.\n\nPostgres ?-to-$n scanning follows the dialect lexer for strings, comments, and dollar quotes, including standard single-quote rules for U\u0026'…' strings; plain single-quoted strings always assume standard\\_conforming\\_strings = on. Jsonb operators ?, ?|, and ?\u0026 are out of contract for SQL passed through the store.\n\n## Functions\n\n```go\nfunc ExcludeMigrationVersion(all fs.FS, ver int) (fs.FS, error)\n```\nExcludeMigrationVersion drops one migration version from the set.\n\n```go\nfunc FilterMigrations(all fs.FS, maxVer int) (fs.FS, error)\n```\nFilterMigrations returns an fs.FS containing only migrations with version \\\u003c= maxVer.\n\n```go\nfunc MapError(err error) error\n```\nMapError maps a driver error to the same core error Execer and Row return. Platform code calls it on errors that come straight from database/sql row iteration (Rows.Scan, Rows.Err): those bypass Execer.Query, so an unmapped driver error would reach callers with the wrong code and driver text.\n\n```go\nfunc TextOrderClause(dialectName, col string) (string, error)\n```\nTextOrderClause returns an ORDER BY column expression with dialect collation (REQ-SQL-017). col must be a simple identifier matching \\[A-Za-z\\_]\\[A-Za-z0-9\\_]\\*; otherwise invalid\\_argument. On Postgres the result appends COLLATE \"C\"; on SQLite it appends COLLATE BINARY.\n\n```go\nfunc TruncateMicrosecond(t time.Time) time.Time\n```\nTruncateMicrosecond converts to UTC and truncates to whole microseconds (REQ-SQL-016).\n\n```go\nfunc ValidateText(s string) error\n```\nValidateText rejects strings that contain a NUL byte (REQ-SQL-018).\n\n## Types\n\n```go\ntype Execer struct {\n\t// contains filtered or unexported fields\n}\n```\nExecer runs SQL with dialect-correct placeholders for either the pool or an active transaction when created by Store.InTx. Repository SQL uses ? on both dialects; on Postgres each ? outside single- and double-quoted text, line and block comments, and dollar-quoted bodies becomes $n in order. Authored $n markers are rejected with invalid\\_argument. Any ? outside those regions is a bind marker, so Postgres jsonb operators ?, ?|, and ?\u0026 cannot appear in SQL; use jsonb\\_exists, jsonb\\_exists\\_any, and jsonb\\_exists\\_all instead. A driver.Valuer bind argument is converted once for this check and once by database/sql, so Valuers must be side-effect free.\n\n```go\nfunc (e *Execer) Exec(ctx context.Context, q string, args ...any) (sql.Result, error)\n```\nExec runs q with args rewritten for the store dialect. String arguments must not contain NUL (REQ-SQL-018). Driver errors are mapped by mapExecErr.\n\n```go\nfunc (e *Execer) Query(ctx context.Context, q string, args ...any) (*sql.Rows, error)\n```\nQuery runs a read query on the pool or transaction, rejecting NUL in string arguments.\n\n```go\nfunc (e *Execer) QueryRow(ctx context.Context, q string, args ...any) *Row\n```\nQueryRow runs a single-row read, rejecting NUL in string arguments before touching the database.\n\n```go\ntype Row struct {\n\t// contains filtered or unexported fields\n}\n```\nRow holds the result of Execer.QueryRow; Scan reads the row and maps driver errors.\n\n```go\nfunc (r *Row) Scan(dest ...any) error\n```\nScan reads one row and maps driver errors.\n\n```go\ntype Store struct {\n\t// contains filtered or unexported fields\n}\n```\nStore is an opened SQL database with migrations applied.\n\n```go\nfunc Open(ctx context.Context, rawURL string, migrations fs.FS) (*Store, error)\n```\nOpen parses rawURL, connects, applies migrations from migrations, and returns a Store. See package documentation for supported URL forms, migration locking, SQLite file modes, and error mapping during open and migrate.\n\n```go\nfunc (s *Store) Close() error\n```\nClose releases the database.\n\n```go\nfunc (s *Store) Dialect() string\n```\nDialect reports sqlite or postgres.\n\n```go\nfunc (s *Store) Execer() *Execer\n```\nExecer returns the query layer for the store.\n\n```go\nfunc (s *Store) InTx(ctx context.Context, fn func(ctx context.Context, exec *Execer) error) (err error)\n```\nInTx runs fn inside one transaction on this store. The transaction rolls back when fn returns an error or panics, and commits only when fn returns nil. A nested InTx on the same store returns invalid\\_argument when fn passes the ctx InTx injects into each InTx on that store; passing a different ctx will not see the marker and can deadlock on SQLite (one connection). Different stores may nest independently.\n\n```go\nfunc (s *Store) ParsedURLRedacted() string\n```\nParsedURLRedacted returns the store URL with userinfo and credential query parameters (password, sslpassword, and other libpq secret keys) replaced for safe logging and errors.\n\n```go\nfunc (s *Store) SchemaVersion(ctx context.Context) (int64, error)\n```\nSchemaVersion returns the newest applied goose migration version, or zero when none apply.\n\n","path":"github.com/epzero/basamento/platform/sql","name":"sql","synopsis":"Package sql is the SQL port of platform: SQLite in the embedded profile and Postgres in the cloud profile, selected by URL (ADR-0004).","isRedistributable":true}
