snapshot of https://github.com/sqlc-dev/sqlc.git (main, 2026-09-16; blobs > 131072B omitted)
Some checks failed
kotlin / test (push) Has been skipped
python / test (push) Has been skipped
typescript / test (push) Has been skipped
go / build darwin/amd64 (push) Has been cancelled
go / build linux/amd64 (push) Has been cancelled
go / build windows/amd64 (push) Has been cancelled
go / build darwin/arm64 (push) Has been cancelled
go / build linux/arm64 (push) Has been cancelled
go / build windows/arm64 (push) Has been cancelled
go / test (push) Has been cancelled
go / vuln_check (push) Has been cancelled
Some checks failed
kotlin / test (push) Has been skipped
python / test (push) Has been skipped
typescript / test (push) Has been skipped
go / build darwin/amd64 (push) Has been cancelled
go / build linux/amd64 (push) Has been cancelled
go / build windows/amd64 (push) Has been cancelled
go / build darwin/arm64 (push) Has been cancelled
go / build linux/arm64 (push) Has been cancelled
go / build windows/arm64 (push) Has been cancelled
go / test (push) Has been cancelled
go / vuln_check (push) Has been cancelled
This commit is contained in:
6
internal/core/catalogdef/embed.go
Normal file
6
internal/core/catalogdef/embed.go
Normal file
@@ -0,0 +1,6 @@
|
||||
package catalogdef
|
||||
|
||||
import _ "embed"
|
||||
|
||||
//go:embed schema.sql
|
||||
var Schema string
|
||||
284
internal/core/catalogdef/query.sql
Normal file
284
internal/core/catalogdef/query.sql
Normal file
@@ -0,0 +1,284 @@
|
||||
-- Queries against sqlc's own sql_* catalog tables, compiled by sqlc's
|
||||
-- SQLite engine. Regenerate with `go generate ./internal/core/...`.
|
||||
|
||||
-- ============================ sql_namespace ============================
|
||||
|
||||
-- name: CreateNamespace :execlastid
|
||||
INSERT INTO sql_namespace (name) VALUES (?);
|
||||
|
||||
-- name: NamespaceOID :one
|
||||
SELECT oid FROM sql_namespace WHERE name = ?;
|
||||
|
||||
-- name: ListNamespaces :many
|
||||
SELECT oid, name FROM sql_namespace ORDER BY oid;
|
||||
|
||||
-- ============================== sql_dialect ============================
|
||||
|
||||
-- name: CreateDialect :execlastid
|
||||
INSERT INTO sql_dialect (name) VALUES (?);
|
||||
|
||||
-- name: DialectOID :one
|
||||
SELECT oid FROM sql_dialect WHERE name = ?;
|
||||
|
||||
-- name: SeededDialect :one
|
||||
-- The dialect a catalog was seeded with. A catalog is built for exactly one,
|
||||
-- so a restored catalog finds it without being told its name.
|
||||
SELECT oid, name FROM sql_dialect ORDER BY oid LIMIT 1;
|
||||
|
||||
-- name: SetDialectFlag :exec
|
||||
INSERT INTO sql_dialect_flag (dialect_oid, key, value)
|
||||
VALUES (?, ?, ?)
|
||||
ON CONFLICT(dialect_oid, key) DO UPDATE SET value = excluded.value;
|
||||
|
||||
-- name: DialectFlag :one
|
||||
SELECT value FROM sql_dialect_flag WHERE dialect_oid = ? AND key = ?;
|
||||
|
||||
-- =============================== sql_type ==============================
|
||||
|
||||
-- name: CreateType :execlastid
|
||||
INSERT INTO sql_type
|
||||
(name, expr, typtype, category, preferred, namespace_oid, dialect_oid,
|
||||
family_oid, element_oid, base_oid, canonical_oid, not_null)
|
||||
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?);
|
||||
|
||||
-- name: CreateTypeArg :exec
|
||||
INSERT INTO sql_type_arg
|
||||
(type_oid, ord, label, arg_type_oid, nullable, int_value, bool_value, string_value, ident)
|
||||
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?);
|
||||
|
||||
-- name: TypeArgs :many
|
||||
SELECT ord, label, arg_type_oid, nullable, int_value, bool_value, string_value, ident
|
||||
FROM sql_type_arg
|
||||
WHERE type_oid = ?
|
||||
ORDER BY ord;
|
||||
|
||||
-- name: TypeOIDByExpr :one
|
||||
SELECT t.oid
|
||||
FROM sql_type t
|
||||
JOIN sql_namespace ns ON ns.oid = t.namespace_oid
|
||||
WHERE t.expr = sqlc.arg(expr)
|
||||
ORDER BY
|
||||
CASE ns.name
|
||||
WHEN 'pg_catalog' THEN 0
|
||||
WHEN 'public' THEN 1
|
||||
ELSE 2
|
||||
END,
|
||||
ns.name
|
||||
LIMIT 1;
|
||||
|
||||
-- name: TypeOIDsByNameInNamespaces :many
|
||||
SELECT oid, namespace_oid FROM sql_type
|
||||
WHERE name = sqlc.arg(name) AND family_oid IS NULL
|
||||
AND namespace_oid IN (sqlc.slice(namespace_oids));
|
||||
|
||||
-- name: TypeOIDByNameInNamespace :one
|
||||
SELECT oid FROM sql_type
|
||||
WHERE namespace_oid = ? AND name = ? AND family_oid IS NULL;
|
||||
|
||||
-- name: TypeOIDByExprInNamespace :one
|
||||
SELECT oid FROM sql_type WHERE namespace_oid = ? AND expr = ?;
|
||||
|
||||
-- name: TypeOIDByName :one
|
||||
-- The family spelled name: an instance carries its family's name too, and
|
||||
-- is found by its expression instead.
|
||||
SELECT t.oid
|
||||
FROM sql_type t
|
||||
JOIN sql_namespace ns ON ns.oid = t.namespace_oid
|
||||
WHERE t.name = sqlc.arg(name) AND t.family_oid IS NULL
|
||||
ORDER BY
|
||||
CASE ns.name
|
||||
WHEN 'pg_catalog' THEN 0
|
||||
WHEN 'public' THEN 1
|
||||
ELSE 2
|
||||
END,
|
||||
ns.name
|
||||
LIMIT 1;
|
||||
|
||||
-- name: TypeNameByOID :one
|
||||
SELECT name FROM sql_type WHERE oid = ?;
|
||||
|
||||
-- name: TypeOIDsInCategory :many
|
||||
-- The families of a category: an instance inherits its family's category
|
||||
-- and an alias stands for the row it points at, so neither is listed.
|
||||
SELECT oid FROM sql_type
|
||||
WHERE dialect_oid = ? AND category = ?
|
||||
AND family_oid IS NULL AND canonical_oid IS NULL
|
||||
ORDER BY oid;
|
||||
|
||||
-- name: LookupType :one
|
||||
SELECT oid, namespace_oid, name, expr, category, typtype, preferred,
|
||||
family_oid, element_oid, base_oid, canonical_oid, not_null
|
||||
FROM sql_type
|
||||
WHERE oid = ?;
|
||||
|
||||
-- name: CreateTypeRewrite :exec
|
||||
INSERT INTO sql_type_rewrite (dialect_oid, ord, pattern, template, cond)
|
||||
VALUES (?, ?, ?, ?, ?);
|
||||
|
||||
-- name: ListTypeRewrites :many
|
||||
SELECT pattern, template, cond FROM sql_type_rewrite
|
||||
WHERE dialect_oid = ? ORDER BY ord;
|
||||
|
||||
-- name: CreateTypeAffinity :exec
|
||||
INSERT INTO sql_type_affinity (dialect_oid, ord, words, type_oid)
|
||||
VALUES (?, ?, ?, ?);
|
||||
|
||||
-- name: ListTypeAffinities :many
|
||||
SELECT words, type_oid FROM sql_type_affinity
|
||||
WHERE dialect_oid = ? ORDER BY ord;
|
||||
|
||||
-- =============================== sql_class =============================
|
||||
|
||||
-- name: CreateClass :execlastid
|
||||
INSERT INTO sql_class (namespace_oid, name, kind) VALUES (?, ?, ?);
|
||||
|
||||
-- name: ClassOID :one
|
||||
SELECT oid FROM sql_class WHERE namespace_oid = ? AND name = ?;
|
||||
|
||||
-- name: ClassOIDByName :one
|
||||
SELECT oid FROM sql_class WHERE name = ? LIMIT 1;
|
||||
|
||||
-- name: ListTablesInNamespace :many
|
||||
SELECT oid, name FROM sql_class
|
||||
WHERE namespace_oid = ? AND kind = 'r'
|
||||
ORDER BY oid;
|
||||
|
||||
-- name: DeleteClass :exec
|
||||
DELETE FROM sql_class WHERE oid = ?;
|
||||
|
||||
-- name: RenameClass :exec
|
||||
UPDATE sql_class SET name = sqlc.arg(new_name) WHERE oid = sqlc.arg(oid);
|
||||
|
||||
-- ============================= sql_attribute ===========================
|
||||
|
||||
-- name: CreateAttribute :exec
|
||||
INSERT INTO sql_attribute (
|
||||
class_oid, name, type_oid, not_null, has_default, num,
|
||||
decl_type, auto_increment, is_primary_key, is_unique, hidden
|
||||
) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?);
|
||||
|
||||
-- name: SetAttributePrimaryKey :exec
|
||||
UPDATE sql_attribute SET is_primary_key = 1, not_null = 1
|
||||
WHERE class_oid = ? AND name = ?;
|
||||
|
||||
-- name: SetAttributeUnique :exec
|
||||
UPDATE sql_attribute SET is_unique = 1
|
||||
WHERE class_oid = ? AND name = ?;
|
||||
|
||||
-- name: DeleteAttributesByClass :exec
|
||||
DELETE FROM sql_attribute WHERE class_oid = ?;
|
||||
|
||||
-- name: DeleteAttribute :exec
|
||||
DELETE FROM sql_attribute WHERE class_oid = ? AND name = ?;
|
||||
|
||||
-- name: RenameAttribute :exec
|
||||
UPDATE sql_attribute SET name = sqlc.arg(new_name)
|
||||
WHERE class_oid = sqlc.arg(class_oid) AND name = sqlc.arg(name);
|
||||
|
||||
-- name: SetAttributeType :exec
|
||||
UPDATE sql_attribute SET type_oid = sqlc.arg(type_oid), decl_type = sqlc.arg(decl_type)
|
||||
WHERE class_oid = sqlc.arg(class_oid) AND name = sqlc.arg(name);
|
||||
|
||||
-- name: SetAttributeNotNull :exec
|
||||
UPDATE sql_attribute SET not_null = sqlc.arg(not_null)
|
||||
WHERE class_oid = sqlc.arg(class_oid) AND name = sqlc.arg(name);
|
||||
|
||||
-- name: MaxAttributeNum :one
|
||||
SELECT CAST(COALESCE(MAX(num), 0) AS INTEGER) AS num FROM sql_attribute WHERE class_oid = ?;
|
||||
|
||||
-- name: ResolveColumn :one
|
||||
SELECT a.oid, a.class_oid, a.name, t.name AS type_name, a.type_oid, a.not_null,
|
||||
a.decl_type, a.auto_increment, a.is_primary_key, a.is_unique
|
||||
FROM sql_attribute a
|
||||
JOIN sql_class c ON c.oid = a.class_oid
|
||||
JOIN sql_type t ON t.oid = a.type_oid
|
||||
WHERE c.name = sqlc.arg(table_name) AND a.name = sqlc.arg(column_name);
|
||||
|
||||
-- name: TableColumns :many
|
||||
SELECT a.oid, a.class_oid, a.name, t.name AS type_name, a.type_oid, a.not_null,
|
||||
a.decl_type, a.auto_increment, a.is_primary_key, a.is_unique
|
||||
FROM sql_attribute a
|
||||
JOIN sql_class c ON c.oid = a.class_oid
|
||||
JOIN sql_type t ON t.oid = a.type_oid
|
||||
WHERE c.name = ?
|
||||
ORDER BY a.num;
|
||||
|
||||
-- name: ClassAttributes :many
|
||||
SELECT oid, name, type_oid, not_null, hidden
|
||||
FROM sql_attribute
|
||||
WHERE class_oid = ?
|
||||
ORDER BY num;
|
||||
|
||||
-- name: ListClassColumns :many
|
||||
SELECT a.name AS column_name, a.type_oid, a.not_null
|
||||
FROM sql_attribute a
|
||||
JOIN sql_type t ON t.oid = a.type_oid
|
||||
WHERE a.class_oid = ? AND a.hidden = 0
|
||||
ORDER BY a.num;
|
||||
|
||||
-- name: LookupAttribute :one
|
||||
SELECT ns.name AS schema_name, cls.name AS table_name, a.name AS column_name, a.num,
|
||||
a.decl_type, a.auto_increment, a.is_primary_key, a.is_unique, a.not_null
|
||||
FROM sql_attribute a
|
||||
JOIN sql_class cls ON cls.oid = a.class_oid
|
||||
JOIN sql_namespace ns ON ns.oid = cls.namespace_oid
|
||||
WHERE a.oid = ?;
|
||||
|
||||
-- ============================ sql_constraint ===========================
|
||||
|
||||
-- name: CreateConstraint :exec
|
||||
INSERT INTO sql_constraint (class_oid, name, kind, columns) VALUES (?, ?, ?, ?);
|
||||
|
||||
-- =============================== sql_proc ==============================
|
||||
|
||||
-- name: CreateProc :execlastid
|
||||
INSERT INTO sql_proc
|
||||
(namespace_oid, dialect_oid, name, kind,
|
||||
return_type_oid, return_set, return_nullable, return_template, strict, variadic_kind)
|
||||
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?);
|
||||
|
||||
-- name: CreateProcArg :exec
|
||||
INSERT INTO sql_proc_arg (proc_oid, ord, name, type_oid, mode, has_default)
|
||||
VALUES (?, ?, ?, ?, ?, ?);
|
||||
|
||||
-- name: ProcArgTypes :many
|
||||
SELECT type_oid FROM sql_proc_arg
|
||||
WHERE proc_oid = ? AND mode IN ('i', 'b', 'v')
|
||||
ORDER BY ord;
|
||||
|
||||
-- name: FindProcsAnyNamespace :many
|
||||
SELECT oid, name, kind, return_type_oid, return_nullable, return_template
|
||||
FROM sql_proc
|
||||
WHERE name = ?;
|
||||
|
||||
-- name: FindProcsInNamespaces :many
|
||||
SELECT oid, name, kind, return_type_oid, return_nullable, return_template
|
||||
FROM sql_proc
|
||||
WHERE name = sqlc.arg(name)
|
||||
AND namespace_oid IN (sqlc.slice(namespace_oids));
|
||||
|
||||
-- ============================= sql_operator ============================
|
||||
|
||||
-- name: CreateOperator :execlastid
|
||||
INSERT INTO sql_operator
|
||||
(namespace_oid, dialect_oid, name,
|
||||
left_type_oid, right_type_oid, result_type_oid, proc_oid)
|
||||
VALUES (?, ?, ?, ?, ?, ?, ?);
|
||||
|
||||
-- name: FindOperators :many
|
||||
SELECT oid, name, left_type_oid, right_type_oid, result_type_oid, proc_oid
|
||||
FROM sql_operator
|
||||
WHERE name = sqlc.arg(name)
|
||||
AND (sqlc.arg(left_type_oid) = 0 OR left_type_oid = sqlc.arg(left_type_oid))
|
||||
AND (sqlc.arg(right_type_oid) = 0 OR right_type_oid = sqlc.arg(right_type_oid));
|
||||
|
||||
-- =============================== sql_cast ==============================
|
||||
|
||||
-- name: CreateCast :exec
|
||||
INSERT INTO sql_cast (source_type_oid, target_type_oid, proc_oid, context, dialect_oid)
|
||||
VALUES (?, ?, ?, ?, ?);
|
||||
|
||||
-- name: FindCast :one
|
||||
SELECT source_type_oid, target_type_oid, proc_oid, context, dialect_oid
|
||||
FROM sql_cast
|
||||
WHERE source_type_oid = ? AND target_type_oid = ?;
|
||||
227
internal/core/catalogdef/schema.sql
Normal file
227
internal/core/catalogdef/schema.sql
Normal file
@@ -0,0 +1,227 @@
|
||||
-- sql_namespace: schemas / namespaces
|
||||
CREATE TABLE sql_namespace (
|
||||
oid INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||
name TEXT NOT NULL UNIQUE
|
||||
);
|
||||
|
||||
-- sql_dialect: registered SQL dialects (postgresql, sqlite, mysql, ...).
|
||||
CREATE TABLE sql_dialect (
|
||||
oid INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||
name TEXT NOT NULL UNIQUE
|
||||
);
|
||||
|
||||
-- sql_dialect_flag: per-dialect configuration knobs (case-folding,
|
||||
-- identifier quoting, alias scoping rules, etc.). Values are opaque
|
||||
-- strings; the analyzer interprets them per key.
|
||||
CREATE TABLE sql_dialect_flag (
|
||||
dialect_oid INTEGER NOT NULL REFERENCES sql_dialect(oid),
|
||||
key TEXT NOT NULL,
|
||||
value TEXT NOT NULL,
|
||||
PRIMARY KEY (dialect_oid, key)
|
||||
);
|
||||
|
||||
-- sql_type: data types, one row per type expression. Modeled on pg_type,
|
||||
-- with the arguments PostgreSQL keeps as a typmod on the use site held in
|
||||
-- the row instead.
|
||||
--
|
||||
-- A row is a family — a name the dialect or the schema declares: numeric,
|
||||
-- array, mood — or an instance, a family applied to arguments: numeric(10, 2),
|
||||
-- array(integer). expr is the canonical spelling of the whole expression and
|
||||
-- the row's identity; name is the family's name, on an instance too, so a
|
||||
-- lookup by name finds the family and an instance points at it.
|
||||
--
|
||||
-- typtype: 'b'ase | 'c'omposite | 'd'omain | 'e'num | 'p'seudo | 'r'ange
|
||||
-- category: 'N'umeric | 'S'tring | 'B'oolean | 'D'atetime | 'A'rray |
|
||||
-- 'C'omposite | 'E'num | 'U'serdef | 'X'unknown
|
||||
-- preferred: tie-breaker for implicit cast resolution within a category
|
||||
-- family_oid: NULL on a family; the family on an instance
|
||||
-- element_oid: what the type holds: an array's element, a map's value,
|
||||
-- a range's subtype
|
||||
-- base_oid: what the type stands on: a domain's or alias type's base,
|
||||
-- a wrapper's inner type, a SQLite spelling's affinity
|
||||
-- canonical_oid: the row the engine reports this one as: an alias spelling
|
||||
-- points at the type it names
|
||||
-- not_null: a domain or alias type declared NOT NULL
|
||||
-- dialect_oid: NULL = standard / shared across dialects
|
||||
CREATE TABLE sql_type (
|
||||
oid INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||
namespace_oid INTEGER NOT NULL REFERENCES sql_namespace(oid),
|
||||
dialect_oid INTEGER REFERENCES sql_dialect(oid),
|
||||
name TEXT NOT NULL,
|
||||
expr TEXT NOT NULL,
|
||||
typtype TEXT NOT NULL DEFAULT 'b',
|
||||
category TEXT,
|
||||
preferred INTEGER NOT NULL DEFAULT 0,
|
||||
family_oid INTEGER REFERENCES sql_type(oid),
|
||||
element_oid INTEGER REFERENCES sql_type(oid),
|
||||
base_oid INTEGER REFERENCES sql_type(oid),
|
||||
canonical_oid INTEGER REFERENCES sql_type(oid),
|
||||
not_null INTEGER NOT NULL DEFAULT 0,
|
||||
UNIQUE (namespace_oid, expr)
|
||||
);
|
||||
CREATE INDEX idx_sql_type_name ON sql_type(name);
|
||||
|
||||
-- sql_type_arg: the arguments of an instance, or the fields, labels or
|
||||
-- members of a declared composite, enum or set, in order. Exactly one of
|
||||
-- arg_type_oid, int_value, bool_value, string_value and ident is set.
|
||||
-- label: a struct field, tuple element or enum label
|
||||
-- nullable: the argument type is nullable at this position, as the inner
|
||||
-- type of Array(Nullable(String)) is
|
||||
-- ident: a bare word that is not a type: max, sum, day to second
|
||||
CREATE TABLE sql_type_arg (
|
||||
type_oid INTEGER NOT NULL REFERENCES sql_type(oid),
|
||||
ord INTEGER NOT NULL,
|
||||
label TEXT NOT NULL DEFAULT '',
|
||||
arg_type_oid INTEGER REFERENCES sql_type(oid),
|
||||
nullable INTEGER NOT NULL DEFAULT 0,
|
||||
int_value INTEGER,
|
||||
bool_value INTEGER,
|
||||
string_value TEXT,
|
||||
ident TEXT,
|
||||
PRIMARY KEY (type_oid, ord)
|
||||
);
|
||||
|
||||
-- sql_type_rewrite: the rewrites a dialect applies to a type before it is
|
||||
-- interned, in order, which are how a dialect stores what only it spells:
|
||||
-- SQL Server keeps float(24) as real and a bare decimal as decimal(18,0),
|
||||
-- ClickHouse keeps Decimal32(4) as Decimal(9, 4). pattern is a type
|
||||
-- expression whose arguments may be $1, $2... binding whatever stands there;
|
||||
-- template is the expression the match becomes, with the bindings
|
||||
-- substituted; cond bounds a binding, as "$1 <= 24" does.
|
||||
CREATE TABLE sql_type_rewrite (
|
||||
dialect_oid INTEGER NOT NULL REFERENCES sql_dialect(oid),
|
||||
ord INTEGER NOT NULL,
|
||||
pattern TEXT NOT NULL,
|
||||
template TEXT NOT NULL,
|
||||
cond TEXT NOT NULL DEFAULT '',
|
||||
PRIMARY KEY (dialect_oid, ord)
|
||||
);
|
||||
|
||||
-- sql_type_affinity: the rule a dialect resolves an unseeded type family
|
||||
-- by, in order: the first row one of whose words the family's name
|
||||
-- contains names the type it stands on, and a row with no words is the
|
||||
-- default. SQLite gives every declared spelling one of five affinities
|
||||
-- this way.
|
||||
CREATE TABLE sql_type_affinity (
|
||||
dialect_oid INTEGER NOT NULL REFERENCES sql_dialect(oid),
|
||||
ord INTEGER NOT NULL,
|
||||
words TEXT NOT NULL DEFAULT '', -- comma-separated, upper case
|
||||
type_oid INTEGER NOT NULL REFERENCES sql_type(oid),
|
||||
PRIMARY KEY (dialect_oid, ord)
|
||||
);
|
||||
|
||||
-- sql_class: relations (tables, views, indexes).
|
||||
-- kind: 'r' = table, 'v' = view, 'i' = index, 'c' = composite type, 'f' = foreign
|
||||
CREATE TABLE sql_class (
|
||||
oid INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||
namespace_oid INTEGER NOT NULL REFERENCES sql_namespace(oid),
|
||||
name TEXT NOT NULL,
|
||||
kind TEXT NOT NULL DEFAULT 'r',
|
||||
UNIQUE(namespace_oid, name)
|
||||
);
|
||||
|
||||
-- sql_attribute: columns of a relation.
|
||||
-- decl_type: the type as the schema spelled it, before
|
||||
-- canonicalization (VARCHAR(10), BIGINT UNSIGNED), which
|
||||
-- is what a formatter prints back and what SQLite reports.
|
||||
-- auto_increment: rowid alias (sqlite INTEGER PRIMARY KEY), AUTOINCREMENT,
|
||||
-- pg serial/bigserial/identity, mysql AUTO_INCREMENT.
|
||||
-- is_primary_key: this column participates in the relation's primary key.
|
||||
-- Set both for inline-column PK and for table-level PK.
|
||||
-- is_unique: column has a UNIQUE constraint or a single-column UNIQUE
|
||||
-- table constraint.
|
||||
-- hidden: resolvable by name but absent from a star expansion and
|
||||
-- from the relation's model, like the column an sqlite
|
||||
-- fts5 table names after itself.
|
||||
CREATE TABLE sql_attribute (
|
||||
oid INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||
class_oid INTEGER NOT NULL REFERENCES sql_class(oid),
|
||||
name TEXT NOT NULL,
|
||||
type_oid INTEGER NOT NULL REFERENCES sql_type(oid),
|
||||
not_null INTEGER NOT NULL DEFAULT 0,
|
||||
has_default INTEGER NOT NULL DEFAULT 0,
|
||||
num INTEGER NOT NULL, -- ordinal position (1-based)
|
||||
decl_type TEXT NOT NULL DEFAULT '',
|
||||
auto_increment INTEGER NOT NULL DEFAULT 0,
|
||||
is_primary_key INTEGER NOT NULL DEFAULT 0,
|
||||
is_unique INTEGER NOT NULL DEFAULT 0,
|
||||
hidden INTEGER NOT NULL DEFAULT 0,
|
||||
UNIQUE(class_oid, name),
|
||||
UNIQUE(class_oid, num)
|
||||
);
|
||||
|
||||
-- sql_constraint: constraints on a relation.
|
||||
-- kind: 'p' = primary key, 'f' = foreign key, 'u' = unique, 'c' = check
|
||||
CREATE TABLE sql_constraint (
|
||||
oid INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||
class_oid INTEGER NOT NULL REFERENCES sql_class(oid),
|
||||
name TEXT NOT NULL DEFAULT '',
|
||||
kind TEXT NOT NULL,
|
||||
columns TEXT NOT NULL DEFAULT '' -- comma-separated attribute nums
|
||||
);
|
||||
|
||||
-- sql_proc: functions, aggregates, window functions, procedures.
|
||||
-- Modeled on pg_proc.
|
||||
-- kind: 'f' = function, 'a' = aggregate, 'w' = window, 'p' = procedure
|
||||
-- variadic_kind: 'n' = none, 'a' = array (VARIADIC any[]), 'v' = variadic-any
|
||||
-- return_set: 1 if SETOF / table-returning
|
||||
-- return_template: the result as an expression over the call's arguments,
|
||||
-- when it depends on their values: Decimal(18, $2) for
|
||||
-- toDecimal64(x, s). Empty for a result the type says.
|
||||
CREATE TABLE sql_proc (
|
||||
oid INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||
namespace_oid INTEGER REFERENCES sql_namespace(oid),
|
||||
dialect_oid INTEGER REFERENCES sql_dialect(oid),
|
||||
name TEXT NOT NULL,
|
||||
kind TEXT NOT NULL DEFAULT 'f',
|
||||
return_type_oid INTEGER NOT NULL REFERENCES sql_type(oid),
|
||||
return_set INTEGER NOT NULL DEFAULT 0,
|
||||
return_nullable INTEGER NOT NULL DEFAULT 1,
|
||||
return_template TEXT NOT NULL DEFAULT '',
|
||||
strict INTEGER NOT NULL DEFAULT 0,
|
||||
variadic_kind TEXT NOT NULL DEFAULT 'n'
|
||||
);
|
||||
|
||||
-- sql_proc_arg: ordered argument list for a proc.
|
||||
-- mode: 'i' = in, 'o' = out, 'b' = both, 't' = table, 'v' = variadic
|
||||
CREATE TABLE sql_proc_arg (
|
||||
proc_oid INTEGER NOT NULL REFERENCES sql_proc(oid),
|
||||
ord INTEGER NOT NULL,
|
||||
name TEXT NOT NULL DEFAULT '',
|
||||
type_oid INTEGER NOT NULL REFERENCES sql_type(oid),
|
||||
mode TEXT NOT NULL DEFAULT 'i',
|
||||
has_default INTEGER NOT NULL DEFAULT 0,
|
||||
PRIMARY KEY (proc_oid, ord)
|
||||
);
|
||||
|
||||
-- sql_operator: operator overloads.
|
||||
-- left_type_oid is NULL for prefix unary; right_type_oid is NULL for postfix.
|
||||
CREATE TABLE sql_operator (
|
||||
oid INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||
namespace_oid INTEGER REFERENCES sql_namespace(oid),
|
||||
dialect_oid INTEGER REFERENCES sql_dialect(oid),
|
||||
name TEXT NOT NULL,
|
||||
left_type_oid INTEGER REFERENCES sql_type(oid),
|
||||
right_type_oid INTEGER REFERENCES sql_type(oid),
|
||||
result_type_oid INTEGER NOT NULL REFERENCES sql_type(oid),
|
||||
proc_oid INTEGER REFERENCES sql_proc(oid),
|
||||
commutator_oid INTEGER REFERENCES sql_operator(oid),
|
||||
negator_oid INTEGER REFERENCES sql_operator(oid)
|
||||
);
|
||||
|
||||
-- sql_cast: type coercion rules.
|
||||
-- context: 'i' = implicit, 'a' = assignment-only, 'e' = explicit-only
|
||||
-- proc_oid NULL = binary-coercible (no function needed)
|
||||
CREATE TABLE sql_cast (
|
||||
source_type_oid INTEGER NOT NULL REFERENCES sql_type(oid),
|
||||
target_type_oid INTEGER NOT NULL REFERENCES sql_type(oid),
|
||||
proc_oid INTEGER REFERENCES sql_proc(oid),
|
||||
context TEXT NOT NULL DEFAULT 'e',
|
||||
dialect_oid INTEGER REFERENCES sql_dialect(oid),
|
||||
PRIMARY KEY (source_type_oid, target_type_oid)
|
||||
);
|
||||
|
||||
-- Resolution-speed indexes.
|
||||
CREATE INDEX idx_sql_proc_name ON sql_proc(name, namespace_oid);
|
||||
CREATE INDEX idx_sql_operator_name ON sql_operator(name, left_type_oid, right_type_oid);
|
||||
CREATE INDEX idx_sql_attribute_name ON sql_attribute(name);
|
||||
Reference in New Issue
Block a user