orm

package
v0.6.0 Latest Latest
Warning

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

Go to latest
Published: Apr 28, 2024 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 added in v0.2.0

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 added in v0.2.0

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

Special cases:

  • use `orm:"$fieldname"` or `orm:"$?fieldname"` to bind nullable string fields to non-pointer string receivers, this generates conditional sql binding in the form: `(case when fieldname notnull then fieldname else "" end)`

func SelectToSlice added in v0.2.0

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 added in v0.2.0

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 added in v0.2.0

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

func Where added in v0.2.0

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

type ErrMissingColumns added in v0.2.0

type ErrMissingColumns []string

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

func (ErrMissingColumns) Error added in v0.2.0

func (e ErrMissingColumns) Error() string

type ErrMissingTables added in v0.2.0

type ErrMissingTables []string

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

func (ErrMissingTables) Error added in v0.2.0

func (e ErrMissingTables) Error() string

type Options added in v0.2.0

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

func Enumerate added in v0.2.0

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 added in v0.2.0

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

func GetTable added in v0.2.0

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

GetTable queries table columns from src.

func (*Table) HasColumn added in v0.2.0

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