virtualview

package
v0.0.0-...-08b9edc Latest Latest
Warning

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

Go to latest
Published: Nov 10, 2021 License: Apache-2.0 Imports: 11 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type OrderByField

type OrderByField struct {
	JSONFieldName string `json:"json_field_name"`
	IsDesc        bool   `json:"is_desc"`
}

type VirtualView

type VirtualView struct {
	nocopy.NoCopy
	// contains filtered or unexported fields
}

VirtualView is a helper to construct SQL clauses for view-like models, based on the view definition in the `vexpr` tag in the model.

For a model field like:

AggLastSeen   int  `vexpr:"UNIX_TIMESTAMP(MAX(last_seen))"`

VirtualView can build projections like:

    SELECT UNIX_TIMESTAMP(MAX(last_seen)) AS agg_last_seen ....
												^^^^^^^^^^^^^ This follows the GORM naming strategy and
															  can be controlled by gorm:"column:xxx".

Then, when selecting the result of this projection into the same model, fields will be filled out naturally:

    {
	     AggLastSeen: <the result of `UNIX_TIMESTAMP(MAX(last_seen))`>
    }

If `vexpr` is not specified in the model field, the field can be transparently used. For example:

FieldFoo  int

The projection will be:

SELECT field_foo ...

Callers must specify the fields to be used in clauses. The field can be specified via its JSON name.

VirtualView is safe to be used concurrently.

func MustNew

func MustNew(model interface{}) *VirtualView

func New

func New(model interface{}) (*VirtualView, error)

func (*VirtualView) Clauses

func (vv *VirtualView) Clauses(jsonFieldNames []string) VirtualViewClauses

func (*VirtualView) SetSourceDBColumns

func (vv *VirtualView) SetSourceDBColumns(dbColumnNames []string)

SetSourceDBColumns restricts SelectClause and OrderByClause to only build clauses over these specified db columns or fields calculated from these db columns. The restriction will be removed if nil is given. This is useful when the source table does not fully match the model, e.g. when there are extra fields in the model, this can avoid view to be broken.

This function is concurrent-safe.

type VirtualViewClauses

type VirtualViewClauses struct {
	// contains filtered or unexported fields
}

func (VirtualViewClauses) OrderBy

func (vvc VirtualViewClauses) OrderBy(fields []OrderByField) clause.Expression

OrderBy builds a Order By clause based on the given fields. Order by fields that do not exist when calling Clauses() will be ignored.

This function is concurrent-safe.

func (VirtualViewClauses) Select

func (vvc VirtualViewClauses) Select() clause.Expression

Select builds a Select clause that return all fields specified in Clauses().

This function is concurrent-safe.

Jump to

Keyboard shortcuts

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