query

package
v0.4.42 Latest Latest
Warning

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

Go to latest
Published: Aug 18, 2021 License: MIT Imports: 7 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func FieldNameToColumn

func FieldNameToColumn(modelObj models.IModel, fieldName string) (string, error)

func FindFieldNameToStructAndStructFieldNameIfAny

func FindFieldNameToStructAndStructFieldNameIfAny(rel *PredicateRelation) (*string, *string)

hacky...

func GetInnerModelIfValid

func GetInnerModelIfValid(modelObj models.IModel, field string) (models.IModel, error)

func GetModelFieldTypeInModelIfValid

func GetModelFieldTypeInModelIfValid(modelObj models.IModel, field string) (reflect.Type, error)

Never returns the pointer value Since what we want is reflec.New() and it would be a pointer

func IsFieldInModel

func IsFieldInModel(modelObj models.IModel, field string) bool

func JSONKeysToFieldName

func JSONKeysToFieldName(modelObj models.IModel, key string) (string, error)

JSONKeyToColumnName transforms json name to column name if not found, return err By doing this we don't need model check?

Types

type Criteria

type Criteria interface {
	BuildQueryStringAndValues(modelObj models.IModel) (string, []interface{}, error)
}

type Escape

type Escape struct {
	Value string
}

If a predicte value is wrapped within an Escape class Assume it has a Stringer interface, and the result of the string is not wrapped in quotes as Postgres values, and also SQL injection is not checked So this should only be used internally

type FieldNotInModelError

type FieldNotInModelError struct {
	Msg string
}

FieldNotInModelError is for GetModelFieldTypeIfValid. if field doesn't exist in the model, return this error We want to go ahead and skip it since this field may be other options that user can read in hookpoints

func (*FieldNotInModelError) Error

func (r *FieldNotInModelError) Error() string

type IQuery added in v0.4.39

type IQuery interface {
	Q(args ...interface{}) IQuery
	Order(order string) IQuery
	Limit(limit int) IQuery
	Offset(offset int) IQuery
	InnerJoin(modelObj models.IModel, foreignObj models.IModel, args ...interface{}) IQuery
	First(modelObj models.IModel) IQuery
	Find(modelObjs interface{}) IQuery
	Create(modelObj models.IModel) IQuery
	Delete(modelObj models.IModel) IQuery
	Save(modelObj models.IModel) IQuery
	Update(attrs ...interface{}) IQuery
	Reset() IQuery
	Error() error
}

IQuery so we can stubb out the DB

func DB added in v0.4.39

func DB(db *gorm.DB) IQuery

Instead of Q() directly, we can use DB().Q() This is so it's easier to stubb out when testing

func Q

func Q(db *gorm.DB, args ...interface{}) IQuery

It would be Q(db, C(...), C(...)...).First() or Q(db).First() with empty PredicateRelationBuilder Use multiple C() when working on inner fields (one C() per struct field)

type ModelAndBuilder

type ModelAndBuilder struct {
	ModelObj models.IModel // THe model this predicate relation applies to
	Builder  *PredicateRelationBuilder
}

type Predicate

type Predicate struct {
	Field string        // e.g. Age
	Cond  PredicateCond // e.g. <, or IN
	Value interface{}   // e.g. 20 or an array of values
}

Predicate is used to represent something like Age < 20

func NewPredicateFromStringAndVal

func NewPredicateFromStringAndVal(s string, value interface{}) (*Predicate, error)

NewPredicateFromStringAndVal, turn string like "age <" and value into proper predicate This is for convenience I cannot get "age < 20" directly because I'd have to know in advance the type of object (unless of course I just send it as a string, wonder if SQL can take it)

func (*Predicate) BuildQueryStringAndValues

func (p *Predicate) BuildQueryStringAndValues(modelObj models.IModel) (string, []interface{}, error)

BuildQuryStringAndValues output proper query conditionals and the correponding values which field those fields Because this then is given to the database, the output needs to match the column names

type PredicateCond

type PredicateCond string
const (
	// PredicateCondEQ is equals
	PredicateCondEQ PredicateCond = "="
	// PredicateCondLT is less than
	PredicateCondLT PredicateCond = "<"
	// PredicateCondLTEQ is less than or equal to
	PredicateCondLTEQ PredicateCond = "<="
	// PredicateCondGT is equal to
	PredicateCondGT PredicateCond = ">"
	// PredicateCondGTEQ is greater than or equal to
	PredicateCondGTEQ PredicateCond = ">="
	// PredicateCondGTEQ is greater than or equal to
	PredicateCondIN PredicateCond = "IN"
)

func StringToPredicateCond

func StringToPredicateCond(s string) (PredicateCond, error)

type PredicateLogic

type PredicateLogic string
const (
	PredicateLogicAND PredicateLogic = "AND"
	PredicateLogicOR  PredicateLogic = "OR"
)

type PredicateRelation

type PredicateRelation struct {
	// PredOrRel contains either be a *Predicate or *PredicateRelation
	// If PredicateRelation than it is nested comparison
	PredOrRels []Criteria
	Logics     []PredicateLogic // AND or OR. The number of Logic operators is one less than the number of predicates
}

PredicateRelation represents things like (age < 20 OR age > 70 OR age = 30) A Criteria is a Predicate or Predicate relation Every nested PredicateRelation is meant to work on one models.IModel. It can also designate criteria for nested class. But it cannot be used for another unrelated Model where there is no nesting relationships.

func NewPredicateRelation

func NewPredicateRelation() *PredicateRelation

func (*PredicateRelation) BuildQueryStringAndValues

func (pr *PredicateRelation) BuildQueryStringAndValues(modelObj models.IModel) (string, []interface{}, error)

type PredicateRelationBuilder

type PredicateRelationBuilder struct {
	Rel   *PredicateRelation
	Error error // This allow us to chain and eventually discover any error by querying for Error
}

func C

func C(args ...interface{}) *PredicateRelationBuilder

args is either two arguments: "Name =" "Christy", or another predicate builder C()

func (*PredicateRelationBuilder) And

func (p *PredicateRelationBuilder) And(s string, v interface{}) *PredicateRelationBuilder

s is Name =?, v is value

func (*PredicateRelationBuilder) BuildQueryStringAndValues

func (p *PredicateRelationBuilder) BuildQueryStringAndValues(model models.IModel) (string, []interface{}, error)

func (*PredicateRelationBuilder) C

s is Name =?, v is value

func (*PredicateRelationBuilder) GetPredicateRelation

func (p *PredicateRelationBuilder) GetPredicateRelation() (*PredicateRelation, error)

func (*PredicateRelationBuilder) Or

func (p *PredicateRelationBuilder) Or(s string, v interface{}) *PredicateRelationBuilder

s is Name =?, v is value

type Query

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

Q is the query struct Q(db).By("Name IN", []strings{name1, name2}, "Age >=", 18).Find(&model).Error This is a wrapper over Gorm's. Query by field name, and prevent SQL injection by making sure that fields are part of the model

func (*Query) Create added in v0.4.37

func (q *Query) Create(modelObj models.IModel) IQuery

func (*Query) Delete added in v0.4.37

func (q *Query) Delete(modelObj models.IModel) IQuery

func (*Query) Error

func (q *Query) Error() error

func (*Query) Find

func (q *Query) Find(modelObjs interface{}) IQuery

func (*Query) First

func (q *Query) First(modelObj models.IModel) IQuery

func (*Query) InnerJoin

func (q *Query) InnerJoin(modelObj models.IModel, foreignObj models.IModel, args ...interface{}) IQuery

args can be multiple C(), but each C() works on one-level of modelObj assuming first is top-level, if given

func (*Query) Limit added in v0.4.37

func (q *Query) Limit(limit int) IQuery

func (*Query) Offset added in v0.4.37

func (q *Query) Offset(offset int) IQuery

func (*Query) Order added in v0.4.37

func (q *Query) Order(order string) IQuery

func (*Query) Q added in v0.4.38

func (q *Query) Q(args ...interface{}) IQuery

func (*Query) Reset added in v0.4.41

func (q *Query) Reset() IQuery

func (*Query) Save added in v0.4.37

func (q *Query) Save(modelObj models.IModel) IQuery

func (*Query) Update added in v0.4.40

func (q *Query) Update(attrs ...interface{}) IQuery

type QueryType

type QueryType int

-----------------------------

const (
	QueryTypeFirst QueryType = iota
	QueryTypeFind  QueryType = iota
)

type TableAndArgs

type TableAndArgs struct {
	TblName string // The table the predicate relation applies to, at this level (non-nested)
	Args    []interface{}
}

Jump to

Keyboard shortcuts

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