tmetautil

package
v0.2.1 Latest Latest
Warning

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

Go to latest
Published: Aug 28, 2024 License: MIT Imports: 6 Imported by: 0

Documentation

Overview

Utility stuff that goes along with tmeta but is not core functionality.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Criteria

type Criteria []Criterion

Criteria is a list of Criterion. If used as an Or (see Criterion struct definition) the various Criterions are ORed together, otherwise they are ANDed.

func (Criteria) CheckFieldNames

func (ca Criteria) CheckFieldNames(fields ...string) error

CheckFieldNames returns an error if it encounters any field which is not in the list provided.

func (Criteria) ContainsMatch

func (ca Criteria) ContainsMatch(likePrefixLen int, fields ...string) bool

ContainsMatch calls ContainsMatch on each Criterion.

func (Criteria) SQL

func (ca Criteria) SQL() (stmt string, args []interface{}, err error)

SQL converts to a SQL where clause and the corresponding arguments for it.

type Criterion

type Criterion struct {
	Not   bool        `json:"not"`
	Field string      `json:"field"`
	Op    Op          `json:"op"`
	Value interface{} `json:"value"`
	Or    Criteria    `json:"or"`
}

Criterion is an individual expression that has a field, an op(erator) and a value. It also supports Not for inverting the criterion, and Or can be used to provide a list of other expressions to be ORed together.

func (Criterion) CheckFieldNames

func (c Criterion) CheckFieldNames(fields ...string) error

CheckFieldNames returns an error if it encounters any field which is not in the list provided.

func (Criterion) ContainsMatch

func (c Criterion) ContainsMatch(likePrefixLen int, fields ...string) bool

ContainsMatch will look for any field in the given set which is "matched", meaning it's operator is any of the valid ones except LikeOp, which requires at least likePrefixLen characters at the start without a wildcard character ('%” or '_'). The idea is to restrict queries to specific (usually indexed) fields to avoid excessive database load.

func (Criterion) SQL

func (ca Criterion) SQL() (stmt string, args []interface{}, err error)

SQL converts to a SQL where clause and the corresponding arguments for it.

type DBTime

type DBTime struct {
	time.Time
}

DBTime is a time value that should work with SQLite3, MySQL and Postgres. It supports subsecond precision, works with the database's underlying DATETIME field type (TEXT for SQLite3), won't get confused about time zones (stores in UTC), scans and JSON encodes/decodes properly.

func NewDBTime

func NewDBTime() DBTime

NewDBTime returns a DBTime with the time set to now.

func NewDBTimeFor

func NewDBTimeFor(t time.Time) DBTime

NewDBTimeFor returns a DBTime corresponding to the time you provide.

func (*DBTime) Scan

func (t *DBTime) Scan(value interface{}) error

func (DBTime) Value

func (t DBTime) Value() (driver.Value, error)

type Op

type Op string

Op is one of the supported SQL where operators used with Criteria and Criterion.

const (
	EqOp   Op = "="
	NeOp   Op = "<>"
	LtOp   Op = "<"
	LteOp  Op = "<="
	GtOp   Op = ">"
	GteOp  Op = ">="
	LikeOp Op = "like"
	InOp   Op = "in"
)

type OrderBy

type OrderBy struct {
	Field string `json:"field"`
	Desc  bool   `json:"desc,omitempty"`
}

OrderBy corresponds to a single field in an ORDER BY SQL clause including it's direction (ascending by default).

func (OrderBy) CheckFieldNames

func (o OrderBy) CheckFieldNames(fields ...string) error

CheckFieldNames returns true if the field for this OrderBy is not in the list provided.

func (*OrderBy) UnmarshalJSON

func (o *OrderBy) UnmarshalJSON(b []byte) error

UnmarshalJSON supports normal JSON unmarshaling plus a shorthand of just providing a string to mean the field name sorted ascending.

type OrderByList

type OrderByList []OrderBy

OrderByList is a list of OrderBys.

func (OrderByList) CheckFieldNames

func (ol OrderByList) CheckFieldNames(fields ...string) error

CheckFieldNames returns true if any of the fields for this OrderByList are not in the list provided.

func (*OrderByList) UnmarshalJSON

func (ol *OrderByList) UnmarshalJSON(b []byte) error

UnmarshalJSON supports normal JSON unmarshaling plus a shorthand of just providing either a single object instead of an array, or a string to mean the field name sorted ascending.

Jump to

Keyboard shortcuts

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