Documentation
¶
Index ¶
- Variables
- type Action
- type Clause
- type Compiler
- type Condition
- func Eq(field string, value any) Condition
- func Gt(field string, value any) Condition
- func Gte(field string, value any) Condition
- func In(field string, value any) Condition
- func Like(field string, value any) Condition
- func Lt(field string, value any) Condition
- func Lte(field string, value any) Condition
- func Neq(field string, value any) Condition
- func Or(c Condition) Condition
- type Constraint
- type DB
- func (db *DB) Close() error
- func (db *DB) Create(m Model) error
- func (db *DB) CreateDatabase(name string) error
- func (db *DB) CreateTable(m Model) error
- func (db *DB) Delete(m Model, conds ...Condition) error
- func (db *DB) DropTable(m Model) error
- func (db *DB) Query(m Model) *QB
- func (db *DB) RawExecutor() Executor
- func (db *DB) Tx(fn func(tx *DB) error) error
- func (db *DB) Update(m Model, conds ...Condition) error
- type Executor
- type Field
- type FieldInfo
- type FieldType
- type Model
- type Order
- type OrderClause
- type Ormc
- func (o *Ormc) GenerateForFile(infos []StructInfo, sourceFile string) error
- func (o *Ormc) GenerateForStruct(structName string, goFile string) error
- func (o *Ormc) ParseStruct(structName string, goFile string) (StructInfo, error)
- func (o *Ormc) ResolveRelations(all map[string]StructInfo)
- func (o *Ormc) Run() error
- func (o *Ormc) SetLog(fn func(messages ...any))
- func (o *Ormc) SetRootDir(dir string)
- type Plan
- type QB
- func (qb *QB) GroupBy(columns ...string) *QB
- func (qb *QB) Limit(limit int) *QB
- func (qb *QB) Offset(offset int) *QB
- func (qb *QB) Or() *QB
- func (qb *QB) OrderBy(column string) *OrderClause
- func (qb *QB) ReadAll(new func() Model, onRow func(Model)) error
- func (qb *QB) ReadOne() error
- func (qb *QB) Where(column string) *Clause
- type Query
- type RelationInfo
- type Rows
- type Scanner
- type SliceFieldInfo
- type StructInfo
- type TxBoundExecutor
- type TxExecutor
Constants ¶
This section is empty.
Variables ¶
var ErrEmptyTable = fmt.Err("name", "table", "empty")
ErrEmptyTable is returned when TableName() returns an empty string.
var ErrNoTxSupport = fmt.Err("transaction", "not", "supported")
ErrNoTxSupport is returned by DB.Tx() when the executor does not implement TxExecutor.
var ErrNotFound = fmt.Err("record", "not", "found")
ErrNotFound is returned when ReadOne() finds no matching row.
var ErrValidation = fmt.Err("error", "validation")
ErrValidation is returned when validate() finds a mismatch.
Functions ¶
This section is empty.
Types ¶
type Clause ¶ added in v0.0.8
type Clause struct {
// contains filtered or unexported fields
}
Clause represents an intermediate state for building a query condition.
type Condition ¶
type Condition struct {
// contains filtered or unexported fields
}
Condition represents a filter for a query. It is a sealed value type constructed via helper functions.
type Constraint ¶ added in v0.1.0
type Constraint int
Constraint is a bitmask of column-level constraints. ConstraintNone = 0 is defined separately to avoid shifting iota off-by-one.
const ( ConstraintPK Constraint = 1 << iota // 1: Primary Key (auto-detected via fmt.IDorPrimaryKey) ConstraintUnique // 2: UNIQUE ConstraintNotNull // 4: NOT NULL ConstraintAutoIncrement // 8: SERIAL / AUTOINCREMENT / {autoIncrement: true} )
const ConstraintNone Constraint = 0
type DB ¶
type DB struct {
// contains filtered or unexported fields
}
DB represents a database connection. Consumers instantiate it via New().
func (*DB) CreateDatabase ¶ added in v0.1.0
CreateDatabase creates a new database.
func (*DB) CreateTable ¶ added in v0.1.0
CreateTable creates a new table for the given model.
func (*DB) RawExecutor ¶ added in v0.0.10
RawExecutor returns the underlying executor instance.
type Executor ¶ added in v0.0.7
type Executor interface {
Exec(query string, args ...any) error
QueryRow(query string, args ...any) Scanner
Query(query string, args ...any) (Rows, error)
Close() error
}
Executor represents the database connection abstraction. It must remain compatible with sql.DB, sql.Tx, mocks, and WASM drivers.
type Field ¶ added in v0.1.0
type Field struct {
Name string
Type FieldType
Constraints Constraint
Ref string // FK: target table name. Empty = no FK.
RefColumn string // FK: target column. Empty = auto-detect PK of Ref table.
}
Field describes a single column in a model's schema. Schema() and Values() MUST always be in the same field order. Field.Ref is present in all adapters for API compatibility; adapters that do not support FKs (e.g. IndexedDB) silently ignore it without error.
type FieldType ¶ added in v0.1.0
type FieldType int
FieldType represents the abstract storage type of a model field.
type Order ¶
type Order struct {
// contains filtered or unexported fields
}
Order represents a sort order for a query. It is a sealed value type constructed via QB.OrderBy().
type OrderClause ¶ added in v0.0.8
type OrderClause struct {
// contains filtered or unexported fields
}
OrderClause represents an intermediate state for building an order by clause.
func (*OrderClause) Asc ¶ added in v0.0.8
func (o *OrderClause) Asc() *QB
Asc sets the order direction to ascending.
func (*OrderClause) Desc ¶ added in v0.0.8
func (o *OrderClause) Desc() *QB
Desc sets the order direction to descending.
type Ormc ¶ added in v0.1.3
type Ormc struct {
// contains filtered or unexported fields
}
Ormc is the code generator handler for the ormc tool.
func NewOrmc ¶ added in v0.1.3
func NewOrmc() *Ormc
NewOrmc creates a new Ormc handler with rootDir defaulting to ".".
func (*Ormc) GenerateForFile ¶ added in v0.1.3
func (o *Ormc) GenerateForFile(infos []StructInfo, sourceFile string) error
GenerateForFile writes ORM implementations for all infos into one file.
func (*Ormc) GenerateForStruct ¶ added in v0.1.3
GenerateForStruct reads the Go File and generates the ORM implementations for a given struct name.
func (*Ormc) ParseStruct ¶ added in v0.1.3
func (o *Ormc) ParseStruct(structName string, goFile string) (StructInfo, error)
ParseStruct parses a single struct from a Go file and returns its metadata.
func (*Ormc) ResolveRelations ¶ added in v0.1.4
func (o *Ormc) ResolveRelations(all map[string]StructInfo)
ResolveRelations (exported for testing) scans all parent SliceFields, finds the matching FK in the child struct, and appends RelationInfo to the child's entry in the map.
func (*Ormc) SetLog ¶ added in v0.1.3
SetLog sets the log function for warnings and informational messages. If not set, messages are silently discarded.
func (*Ormc) SetRootDir ¶ added in v0.1.3
SetRootDir sets the root directory that Run() will scan. Defaults to ".". Useful in tests to point to a specific directory without needing os.Chdir.
type QB ¶
type QB struct {
// contains filtered or unexported fields
}
QB represents a query builder. Consumers hold a *QB reference in variables for incremental building.
func (*QB) OrderBy ¶
func (qb *QB) OrderBy(column string) *OrderClause
OrderBy starts a new order clause for the given column.
type Query ¶
type Query struct {
Action Action
Table string
Database string
Columns []string
Values []any
Conditions []Condition
OrderBy []Order
GroupBy []string
Limit int
Offset int
}
Query represents a database query to be executed by an Executor. Planners read these fields to build Plans.
type RelationInfo ¶ added in v0.1.4
type RelationInfo struct {
ChildStruct string // e.g. "Role"
FKField string // e.g. "UserID" (Go field name)
FKColumn string // e.g. "user_id" (column name)
LoaderName string // e.g. "ReadAllRoleByUserID"
FKFieldType string // e.g. "string", "int64"
}
RelationInfo describes a one-to-many relation loader to generate.
type SliceFieldInfo ¶ added in v0.1.4
SliceFieldInfo records a slice-of-struct field found in a parent struct. Not DB-mapped; used only for relation resolution.
type StructInfo ¶ added in v0.0.8
type StructInfo struct {
Name string
TableName string
PackageName string
Fields []FieldInfo
TableNameDeclared bool
SourceFile string
SliceFields []SliceFieldInfo // populated by ParseStruct; used by ResolveRelations
Relations []RelationInfo // populated by ResolveRelations; used by GenerateForFile
}
type TxBoundExecutor ¶ added in v0.0.7
TxBoundExecutor represents an executor bound to a transaction.
type TxExecutor ¶ added in v0.0.7
type TxExecutor interface {
Executor
BeginTx() (TxBoundExecutor, error)
}
TxExecutor represents an executor that supports transactions.