Documentation
¶
Overview ¶
Package sql exposes a SQLite-dialect SQL surface over the PostgreSQL wire protocol. It is the scaffolding for a swytch-backed SQL layer; in this phase, SQLite runs in :memory: per connection with no swytch storage wired in.
Index ¶
- Variables
- func Evaluate(p *Predicate, row *RowWrite) bool
- func Run(args []string) error
- type CmpOp
- type PredKind
- type Predicate
- func PAnd(children ...Predicate) Predicate
- func PCmp(col int, op CmpOp, lit TypedValue) Predicate
- func PColCmp(left int, op CmpOp, right int) Predicate
- func PFalse() Predicate
- func PIsNull(col int) Predicate
- func PNot(child Predicate) Predicate
- func POr(children ...Predicate) Predicate
- func PTrue() Predicate
- type RowWrite
- type Server
- type ServerConfig
- type TypedValue
- type WriteKind
Constants ¶
This section is empty.
Variables ¶
var ( // ErrNoSuchTable is returned from vtab / schema read paths when a // table name is not registered in the replicated table list. ErrNoSuchTable = errors.New("sql: no such table") // ErrTableExists is returned from DDL when CREATE TABLE targets a // name already in the registry. ErrTableExists = errors.New("sql: table already exists") // ErrNoSuchIndex is returned from index load/drop paths when the // index name is not registered. ErrNoSuchIndex = errors.New("sql: no such index") // ErrNoSuchView is returned from view load/drop paths when the // view name is not registered in the replicated view list. ErrNoSuchView = errors.New("sql: no such view") // ErrViewExists is returned from CREATE VIEW when the name is // already taken by another view. Table/view name collisions // surface as "sql: table already exists" via the parent pathway. ErrViewExists = errors.New("sql: view already exists") )
var ErrUniqueViolation = errors.New("sql: unique constraint violation")
ErrUniqueViolation is returned from write paths when an INSERT or UPDATE would put two rows under the same value of a UNIQUE index.
var Version = "dev"
Functions ¶
Types ¶
type CmpOp ¶
type CmpOp uint8
CmpOp is the comparison operator for Cmp / ColCmp terms.
BETWEEN is deliberately not represented here — the planner decomposes `col BETWEEN x AND y` into `And(Cmp(col ≥ x), Cmp(col ≤ y))`.
type PredKind ¶
type PredKind uint8
PredKind discriminates the Predicate struct's contents.
const ( // PredBool is the TRUE/FALSE leaf. Unsupported predicate // fragments conservatively expand to Bool(TRUE) so the // observation widens rather than missing conflicts. PredBool PredKind = iota // PredAnd / PredOr are boolean connectives. Short-circuit // evaluation with three-valued logic propagates NULL/unknown // correctly. PredAnd PredOr // PredNot inverts its child; three-valued with UNKNOWN passing // through. PredNot // PredCmp is `col <op> literal`. PredCmp // PredColCmp is `col <op> col` — both operands come from the // write's tuple. PredColCmp // PredIsNull is `col IS NULL`. `col IS NOT NULL` is expressed // as Not(IsNull(col)). PredIsNull )
type Predicate ¶
type Predicate struct {
Kind PredKind
BoolVal bool
Children []Predicate
Child *Predicate
Col int
Col2 int
Op CmpOp
Literal TypedValue
}
Predicate is a node in a boolean expression tree. Exactly one field-group is meaningful, discriminated by Kind:
Bool -> BoolVal And / Or -> Children Not -> Child Cmp -> Col, Op, Literal ColCmp -> Col, Op, Col2 IsNull -> Col
The struct is intentionally a tagged sum rather than an interface so it round-trips through protobuf without wrapper types and is cheap to evaluate (no type assertions on the hot path).
type RowWrite ¶
type RowWrite struct {
Table string
PK []byte
Kind WriteKind
Cols []TypedValue
}
RowWrite is the structured row payload carried by every INSERT/UPDATE/DELETE, consumed by the predicate evaluator at fork-choice time. The Cols slice is ordinal-aligned with the table's column list at the schema tip the observation saw.
For INSERTs, Cols is the new row's state. For UPDATEs, it's the effective post-commit state (carry-forward already applied). For DELETEs, it's the pre-deletion state so an observation like `role = 'admin'` can be evaluated against the row that was there.
type Server ¶
type Server struct {
// contains filtered or unexported fields
}
Server is a PostgreSQL-wire-compatible SQL server that executes SQLite dialect SQL. Each client connection is assigned its own SQLite connection opened in ":memory:" mode; swytch is the authoritative storage for all tables created through this server.
func NewServer ¶
func NewServer(cfg ServerConfig) (*Server, error)
NewServer builds a Server with the given configuration. It does not begin listening — call Start or StartAsync.
func (*Server) Addr ¶
Addr returns the network address the server is listening on, or nil if it has not started yet.
func (*Server) Done ¶
Done returns a channel that receives the error (if any) from the serve loop when StartAsync was used. Nil when Start was used.
func (*Server) Start ¶
Start listens on the configured address and serves until the listener is closed. It blocks.
func (*Server) StartAsync ¶
StartAsync listens on the configured address and serves in a background goroutine. The bound address is available via Addr once this method returns.
type ServerConfig ¶
type ServerConfig struct {
// Address is the TCP address to listen on (e.g. ":5433").
Address string
// AdvertisedVersion is the string sent as server_version during the
// pg wire startup handshake. Defaults to "17.0 (Swytch)".
AdvertisedVersion string
// Logger is an optional *slog.Logger. When nil, slog.Default() is used.
Logger *slog.Logger
// Engine is the swytch effects engine used as the storage layer.
// When nil, NewServer constructs a standalone single-node engine.
// Shared with other swytch subsystems (cluster, beacon) when
// provided externally.
Engine *effects.Engine
// contains filtered or unexported fields
}
ServerConfig holds configuration for the SQL server.
func DefaultServerConfig ¶
func DefaultServerConfig() ServerConfig
DefaultServerConfig returns a default server configuration.
type TypedValue ¶
TypedValue is a tagged literal value. The Kind tag identifies which of the payload fields is meaningful; the evaluator uses it to pick a typed comparison path and to detect type mismatches that should evaluate to UNKNOWN.
func BlobVal ¶
func BlobVal(v []byte) TypedValue
func FloatVal ¶
func FloatVal(v float64) TypedValue
func IntVal ¶
func IntVal(v int64) TypedValue
func NullVal ¶
func NullVal() TypedValue
func TextVal ¶
func TextVal(v string) TypedValue