orm

package
v0.5.0-rc.1 Latest Latest
Warning

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

Go to latest
Published: Mar 27, 2023 License: MIT Imports: 6 Imported by: 0

Documentation

Overview

Example (Bind_receivers)
type B struct {
	F3 string  `orm:"f3"`
	F4 *string `orm:"?f4"`
}

type A struct {
	F1 int     `orm:"f1"`
	F2 string  `orm:"f2"`
	B  B       `orm:"?"`
	F5 string  `orm:"?f5"`
	F6 float32 `orm:"?f6"`
	F7 string  `orm:"f7|f8"`
}

t := Table{
	Name: "t",
	columns: nameset{
		"f1": {},
		"f2": {},
		"f3": {},
		"f5": {},
		"f8": {},
	},
}

v := &A{}

bb, err := t.bind_receivers(v)

if err == nil {
	fmt.Printf("ss: %v\n", bb.selectors)
}
Output:

ss: [f1 f2 f3 f5 f8]

Index

Examples

Constants

This section is empty.

Variables

View Source
var ErrEmptyTableSchema = errors.New("empty table schema")
View Source
var ErrNoBindingsProduced = errors.New("failed to produce any field bindings")
View Source
var ErrTableDoesNotExist = errors.New("table does not exist")

Functions

func GetTables

func GetTables(src Querier, table_names ...string) (tt map[string]*Table, err error)

GetTables queries the set of tables from src.

Prefix table name with ? to make it optional.

func Select

func Select[T any](src Querier, table *Table, opts Options, on_row func(t *T) error) error

Select enumerates table rows mapping its columns to fields in struct T.

T needs to have its fields tagged with orm attributes:

  • use `orm:"fieldname"“ for mandatory fields
  • use `orm:"?fieldname"` for optional fields
  • use `orm:"fieldname|alternative"` to match a field to any of the specified columns
  • use `orm:"!"` for structural child filds to link to their fields
  • use `orm:"?"` for structural child filds to optionally link to their fields

func SelectToSlice

func SelectToSlice[S ~[]*T, T any](src Querier, table *Table, opts Options, dst *S) error

SelectToSlice appends results of Select enumeration to dst

func Selector

func Selector[T any](src Querier, table *Table) (func(opts Options, callback func(t *T) error) error, error)

Selector produces an enumerating callable for struct T.

Types

type Condition

type Condition interface {
	Sql() (sql string, args []any)
}

func Where

func Where(expr string, args ...any) Condition

type ErrMissingColumns

type ErrMissingColumns []string

ErrMissingColumns is a list of column names that can be used as the 'missing columns' error.

func (ErrMissingColumns) Error

func (e ErrMissingColumns) Error() string

type ErrMissingTables

type ErrMissingTables []string

ErrMissingTables is a list of table names that can be used as the 'missing tables' error.

func (ErrMissingTables) Error

func (e ErrMissingTables) Error() string

type Options

type Options interface {
	Sql(tablename string, selectors []string) string
	Args() []any
}

func Enumerate

func Enumerate(conditions ...Condition) Options

type Querier

type Querier interface {
	Query(query string, args ...interface{}) (*sql.Rows, error)
}

Querier is a generic db query runner, typically should be hooked to sql.Tx or sql.DB.

type Table

type Table struct {
	Name string
	// contains filtered or unexported fields
}

func GetTable

func GetTable(src Querier, table_name string) (*Table, error)

GetTable queries table columns from src.

func (*Table) HasColumn

func (t *Table) HasColumn(column_name string) bool

Jump to

Keyboard shortcuts

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