database

package
v0.9.2 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: May 1, 2026 License: Apache-2.0 Imports: 5 Imported by: 0

Documentation

Overview

Package database provides shared SQLite helpers for schema migration and connection management. All stores that open or migrate SQLite databases should use these helpers for consistency.

Index

Constants

View Source
const SQLiteTimestampLayout = "2006-01-02 15:04:05.999999999-07:00"

SQLiteTimestampLayout is the canonical TEXT shape go-sqlite3 emits when binding a time.Time value: a space-separated date and time with nanosecond precision and an explicit zone offset. This is the on-disk form for any column whose values were inserted via bound parameters (the production write path), and it must be used as-is when building bounds for lexical comparisons against those columns.

Mixing this with time.RFC3339Nano (which uses a T separator) produces silent miscompares: lexically " " (0x20) < "T" (0x54), so for the same instant the space-form is "less than" the T-form, and a `WHERE timestamp >= ?` clause silently excludes the lower edge of the window when the bound and the stored row disagree on shape.

Variables

This section is empty.

Functions

func AddColumn

func AddColumn(db *sql.DB, table, column, typedef string) error

AddColumn idempotently adds a column to an existing table. If the column already exists the call is a no-op. The typedef parameter is the SQL type and optional constraints, for example "TEXT" or "INTEGER DEFAULT 0".

func FormatTimestamp

func FormatTimestamp(t time.Time) string

FormatTimestamp returns t in SQLiteTimestampLayout — the form go-sqlite3 emits when binding time.Time directly. Use this only when you need a literal string (raw SQL composition, migrations, log lines, structured-data emission). For normal parameterized queries, BIND time.Time directly so the driver and any stored rows share the same on-disk shape — that is the canonical write path and avoids the format-mixing trap described on SQLiteTimestampLayout.

func HasColumn

func HasColumn(db *sql.DB, table, column string) bool

HasColumn reports whether the given table contains a column with the specified name. It uses a lightweight SELECT probe that avoids scanning any rows. Identifiers are left unquoted because SQLite treats double-quoted unknown identifiers as string literals, which would make the probe always succeed.

func Open

func Open(path string) (*sql.DB, error)

Open opens a SQLite database at the given path with standard production settings: WAL journal mode and a 5-second busy timeout. Callers must ensure the github.com/mattn/go-sqlite3 driver is registered (typically via a blank import in the binary's main package or test file).

func OpenMemory

func OpenMemory() (*sql.DB, error)

OpenMemory opens an isolated shared-cache in-memory SQLite database suitable for tests. Each call gets a unique database name so parallel test packages cannot contaminate each other. MaxOpenConns and MaxIdleConns are set to 1 to prevent the pool from dropping the last connection and silently losing the in-memory state.

func ParseTimestamp

func ParseTimestamp(s string) (time.Time, error)

ParseTimestamp parses a timestamp string from SQLite, accepting multiple common formats. This provides read-side defense against format mismatches — the write side should bind time.Time directly (driver-native SQLiteTimestampLayout) or use FormatTimestamp when a literal string is required.

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL