schema

package
v0.0.7 Latest Latest
Warning

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

Go to latest
Published: Sep 10, 2026 License: MIT Imports: 1 Imported by: 0

Documentation

Index

Constants

View Source
const TotalSizeColumn = "total_size"

TotalSizeColumn is the column Count() adds to a list query, named after google AIP-158

Variables

This section is empty.

Functions

This section is empty.

Types

type Access added in v0.0.3

type Access string

Access narrows what a contract allows. Empty means both read and write.

const (
	AccessFull  Access = ""
	AccessRead  Access = "read"
	AccessWrite Access = "write"
)

type Aggregate added in v0.0.6

type Aggregate struct {
	Func  AggregateFunc
	Field string
}

Aggregate is one aggregate function over one column.

type AggregateFunc added in v0.0.6

type AggregateFunc string

AggregateFunc tells which sql aggregate folds a column.

const (
	AggregateSum AggregateFunc = "sum"
	AggregateAvg AggregateFunc = "avg"
	AggregateMin AggregateFunc = "min"
	AggregateMax AggregateFunc = "max"
)

type Contract

type Contract struct {
	Type   ContractType
	Access Access
}

type ContractType

type ContractType string
const (
	ContractSQLC  ContractType = "sqlc"
	ContractPROTO ContractType = "proto"
)

type Entity

type Entity struct {
	Name      string
	Comment   string
	Fields    []Field
	Contracts []Contract
	Queries   []Query
	Indexes   []Index
}

func FilterPROTO

func FilterPROTO(entities []Entity) []Entity

func FilterSQLC

func FilterSQLC(entities []Entity) []Entity

func (Entity) CallerSuppliesID added in v0.0.6

func (e Entity) CallerSuppliesID() bool

reports if the id arrives with the create request

func (Entity) CanFieldRead added in v0.0.3

func (e Entity) CanFieldRead(field Field) bool

func (Entity) CanFieldWrite added in v0.0.3

func (e Entity) CanFieldWrite(field Field) bool

func (Entity) CollidableConstraints added in v0.0.5

func (e Entity) CollidableConstraints() [][]string

func (Entity) ContainsUniqueKey added in v0.0.6

func (e Entity) ContainsUniqueKey(columns []string) ([]string, bool)

func (Entity) GetContract added in v0.0.3

func (e Entity) GetContract(contractType ContractType) (Contract, bool)

func (Entity) GetFieldByName

func (e Entity) GetFieldByName(name string) (Field, bool)

func (Entity) GetIdField

func (e Entity) GetIdField() Field

func (Entity) HasAutoGeneratedKey added in v0.0.5

func (e Entity) HasAutoGeneratedKey() bool

HasAutoGeneratedKey reports if the database assigns the id.

func (Entity) HasIdField added in v0.0.5

func (e Entity) HasIdField() bool

func (Entity) HasPROTO

func (e Entity) HasPROTO() bool

func (Entity) HasSQLC

func (e Entity) HasSQLC() bool

func (Entity) HasUniqueConstraint added in v0.0.5

func (e Entity) HasUniqueConstraint(columns []string) bool

HasUniqueConstraint reports if the columns are the primary key, a Unique() field or the columns of a Unique() index. Only such a set can be an upsert conflict target.

func (Entity) InsertReturnsID added in v0.0.6

func (e Entity) InsertReturnsID() bool

InsertReturnsID reports if an insert hands the id back, it does unless the caller sent it.

func (Entity) IsFieldVirtual added in v0.0.3

func (e Entity) IsFieldVirtual(field Field) bool

func (Entity) IsPrimaryKeyField added in v0.0.5

func (e Entity) IsPrimaryKeyField(field Field) bool

func (Entity) OtherCollidableConstraints added in v0.0.5

func (e Entity) OtherCollidableConstraints(target []string) [][]string

func (Entity) PrimaryIndex added in v0.0.5

func (e Entity) PrimaryIndex() (Index, bool)

func (Entity) PrimaryKeyFields added in v0.0.5

func (e Entity) PrimaryKeyFields() []Field

func (Entity) PrimaryKeyGetQuery added in v0.0.5

func (e Entity) PrimaryKeyGetQuery() (Query, bool)

returns the get query keyed by the primary key

func (Entity) ProtoQueries added in v0.0.3

func (e Entity) ProtoQueries() []Query

ProtoQueries returns the queries generated for the proto contract

func (Entity) QueryByType added in v0.0.5

func (e Entity) QueryByType(queryType QueryType) (Query, bool)

returns the entity's first query of the given type

func (Entity) SQLCQueries added in v0.0.3

func (e Entity) SQLCQueries() []Query

SQLCQueries returns the queries generated for the sqlc contract

func (Entity) UpsertSetFields added in v0.0.5

func (e Entity) UpsertSetFields(target []string) []Field

func (Entity) UpsertTarget added in v0.0.5

func (e Entity) UpsertTarget(query Query) []string

UpsertTarget returns the conflict target columns of an upsert query. A query that names no fields targets the primary key.

type Field

type Field struct {
	Name         string
	Type         FieldType
	Primary      bool
	Unique       bool
	DefaultValue any
	DefaultFunc  func() any
	ProtoField   int
	Comment      string
	Contracts    []Contract
	Immutable    bool
	Optional     bool
	Validate     func() any
}

func (Field) CanApiRead added in v0.0.3

func (f Field) CanApiRead() bool

func (Field) CanApiWrite added in v0.0.3

func (f Field) CanApiWrite() bool

func (Field) CanDbRead added in v0.0.3

func (f Field) CanDbRead() bool

func (Field) CanDbWrite added in v0.0.3

func (f Field) CanDbWrite() bool

func (Field) GetContract added in v0.0.3

func (f Field) GetContract(contractType ContractType) (Contract, bool)

GetContract returns the field contract of the given type

func (Field) IsID

func (f Field) IsID() bool

func (Field) IsVirtual

func (f Field) IsVirtual() bool

IsVirtual reports a field that lives only in proto and not in sqlc.

type FieldType

type FieldType string
const (
	FieldTypeString FieldType = "string"
	FieldTypeInt    FieldType = "int32"
	FieldTypeInt64  FieldType = "int64"
	FieldTypeFloat  FieldType = "float64"
	FieldTypeBool   FieldType = "bool"
	FieldTypeTime   FieldType = "time"
	FieldTypeByte   FieldType = "[]byte"
	FieldTypeJSON   FieldType = "json"
)

func AggregateResultType added in v0.0.6

func AggregateResultType(fn AggregateFunc, fieldType FieldType) FieldType

type Index

type Index struct {
	Type    IndexType
	Columns []IndexColumn
	Unique  bool
	Name    string
}

func (Index) FieldNames

func (i Index) FieldNames() []string

type IndexColumn

type IndexColumn struct {
	Name string
	Desc bool // false = ASC (default), true = DESC
}

type IndexType

type IndexType string
const (
	IndexPrimary IndexType = "primary"
	IndexRegular IndexType = "index"
)

type OrderColumn added in v0.0.5

type OrderColumn struct {
	Name string
	Desc bool // false = ASC (default), true = DESC
}

type Query

type Query struct {
	Type         QueryType
	Fields       []string
	Filters      []QueryFilter
	Count        bool        // Count() asks for the number of matching rows
	Distinct     []string    // Distinct() selects only these columns, deduplicated
	GroupBy      []string    // GroupBy() folds the rows into one row per column combination
	Aggregates   []Aggregate // Sum(), Avg(), Min() and Max() fold a column, in chain order
	OrderBy      []OrderColumn
	HasLimit     bool
	Limit        int // fixed row count, 0 means the caller sets it
	HasOffset    bool
	Name         string
	Comment      string
	Contracts    []Contract
	PrimaryKey   bool
	Upsert       bool     // Upsert() updates the row the insert collides with
	UpsertFields []string // the conflict target, empty means the primary key
	UpsertIgnore bool     // Ignore() keeps the existing row instead of updating it
}

func (Query) DistinctField added in v0.0.6

func (q Query) DistinctField() (string, bool)

func (Query) HasAggregates added in v0.0.6

func (q Query) HasAggregates() bool

func (Query) HasContract added in v0.0.3

func (q Query) HasContract(contractType ContractType) bool

func (Query) HasDistinct added in v0.0.6

func (q Query) HasDistinct() bool

func (Query) HasGroupBy added in v0.0.6

func (q Query) HasGroupBy() bool

func (Query) IsCreate added in v0.0.5

func (q Query) IsCreate() bool

func (Query) IsList added in v0.0.5

func (q Query) IsList() bool

func (Query) IsWrite added in v0.0.3

func (q Query) IsWrite() bool

func (Query) LimitFromRequest added in v0.0.5

func (q Query) LimitFromRequest() bool

type QueryFilter

type QueryFilter struct {
	Type     QueryFilterType
	Field    string
	Optional bool
}

type QueryFilterType

type QueryFilterType string
const (
	QueryFilterRange  QueryFilterType = "range"
	QueryFilterSearch QueryFilterType = "search"
	QueryFilterEq     QueryFilterType = "eq"
)

type QueryType

type QueryType string
const (
	QueryCreate     QueryType = "create"
	QueryCreateBulk QueryType = "create_bulk"
	QueryUpdate     QueryType = "update"
	QueryDelete     QueryType = "delete"
	QueryDeleteAll  QueryType = "delete_all"
	QueryGetBy      QueryType = "get_by"
	QueryListBy     QueryType = "list_by"
	QueryListAll    QueryType = "list_all"
)

type SQLDialect

type SQLDialect string
const (
	MySQL      SQLDialect = "mysql"
	SQLite     SQLDialect = "sqlite"
	PostgreSQL SQLDialect = "postgresql"
)

type Schema

type Schema struct {
	Entities []Entity
}

Jump to

Keyboard shortcuts

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