sqlitetable

package
v0.1.37 Latest Latest
Warning

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

Go to latest
Published: Sep 15, 2026 License: Apache-2.0 Imports: 9 Imported by: 0

Documentation

Overview

Package sqlitetable writes rows described by query.ColumnDef into SQLite tables that a `sql` profile can read back under the declared column names.

Columns are stored positionally ("c0", "c1", …) and aliased on the way out, so a declared name is never an identifier the table itself has to carry — any string a profile can name a column is a name this package can store.

Index

Constants

View Source
const TimeLayout = "2006-01-02T15:04:05.000000000Z07:00"

TimeLayout is how an instant is stored, and how a bound compared against a stored instant must be bound. SQLite has no time type, so instants are TEXT compared as text, and text only orders as time when every value is written to the same width in the same zone: RFC3339Nano drops trailing zeros, so "…05Z" would sort after "…05.5Z", and the driver's rendering of a bound time.Time is Go's String(), which sorts before every stored value of its day.

Variables

This section is empty.

Functions

func DecodeStructured

func DecodeStructured(columns []query.ColumnDef, row query.Row) error

DecodeStructured parses the JSON text of every structured column in row in place, so a row read back holds the value that was written.

func FormatTime

func FormatTime(at time.Time) string

FormatTime is at in TimeLayout, in UTC.

func IsStructured

func IsStructured(columnType query.ColumnType) bool

IsStructured reports whether a column type is stored as JSON text.

func ProfileColumns

func ProfileColumns(columns []query.ColumnDef) []query.ColumnDef

ProfileColumns are the columns a profile over the table declares: each structured column reads its stored JSON text back as the value it encodes.

func QuoteIdentifier

func QuoteIdentifier(value string) string

QuoteIdentifier quotes a SQLite identifier.

func Type

func Type(columnType query.ColumnType) string

Type is the SQLite column type a declared column type is stored as. A boolean is declared BOOLEAN rather than INTEGER: SQLite stores both as 0/1, but the driver reads a BOOLEAN column back as a Go bool, so a profile over the table returns true/false rather than a number.

func Value

func Value(columnType query.ColumnType, value any) (any, error)

Value converts a row value into one the sqlite driver stores: declared structured values as JSON text, other scalars as themselves, and times in TimeLayout.

func Write

func Write(ctx context.Context, database Execer, table Table, rows []query.Row) error

Write creates the table and inserts rows into it.

Types

type Execer

type Execer interface {
	ExecContext(ctx context.Context, query string, args ...any) (sql.Result, error)
	PrepareContext(ctx context.Context, query string) (*sql.Stmt, error)
}

Execer is what writing a table needs from a connection: a *sql.DB or a *sql.Tx.

type Table

type Table struct {
	Name    string
	Columns []query.ColumnDef

	// PrimaryKey names the declared columns that together identify a row.
	PrimaryKey []string

	// Unique names declared columns that each get their own unique index.
	Unique []string
}

Table is one SQLite table and the declared columns it stores.

func (Table) Create

func (t Table) Create(ctx context.Context, database Execer) error

Create creates the table and its unique indexes. The table must not exist.

func (Table) Insert

func (t Table) Insert(ctx context.Context, database Execer, rows []query.Row) error

Insert appends rows to the existing table. A row key the table does not declare is not stored; a declared column the row lacks is stored as NULL.

func (Table) Physical

func (t Table) Physical(name string) (string, error)

Physical is the quoted column a declared column is stored in, for a clause that has to address the table itself rather than the aliased result.

func (Table) Select

func (t Table) Select() string

Select reads every declared column under its declared name.

Jump to

Keyboard shortcuts

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