query

package
v0.5.11 Latest Latest
Warning

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

Go to latest
Published: Dec 1, 2021 License: MIT Imports: 9 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func CreatePeggedAssocFields added in v0.5.1

func CreatePeggedAssocFields(db *gorm.DB, modelObj models.IModel) (err error)

CreatePeggedAssocFields :-

func DeleteModelFixManyToManyAndPegAndPegAssoc added in v0.5.1

func DeleteModelFixManyToManyAndPegAndPegAssoc(db *gorm.DB, modelObj models.IModel) error

func DesignatorContainsDot added in v0.4.50

func DesignatorContainsDot(rel *PredicateRelation) bool

func FindFieldNameToStructAndStructFieldNameIfAny

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

hacky...

func GetOuterTableName added in v0.4.50

func GetOuterTableName(modelObj models.IModel, fieldNameDesignator string) (string, error)

Types

type BatchCreateData added in v0.5.1

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

type BuilderInfo added in v0.4.50

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

type Criteria

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

	// GetDesignatedModel gets the inner model within this modelObj designated by the criteria
	// If it's on the first-level, modelObj itself is returned
	GetDesignatedModel(modelObj models.IModel) (models.IModel, error)

	// GetDesignatedField gets the name of the current Field designator for the inner model
	// or empty string if it is modelObj itself
	GetDesignatedField(modelObj models.IModel) string

	// GetAllUnqueStructFieldDesignator returns the struct fields designators (used for buliding joins within
	// nested table). For example, A.B.C returns A.B. and A.
	// Returns map because what we really want is a unique set. The value of the map is not important.
	GetAllUnqueStructFieldDesignator() map[string]interface{}

	// GetNestedLevel is the level the criteria designates
	// The top-most level is 1
	GetNestedLevel() int
}

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 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
	BuildQuery(modelObj models.IModel) (*gorm.DB, error)
	First(modelObj models.IModel) IQuery
	Find(modelObjs interface{}) IQuery
	Count(modelObj models.IModel, no *int) IQuery
	Create(modelObj models.IModel) IQuery
	CreateMany(modelObjs []models.IModel) IQuery
	Delete(modelObj models.IModel) IQuery
	DeleteMany(modelObjs []models.IModel) IQuery
	Save(modelObj models.IModel) IQuery
	// Update(modelObjs interface{}, attrs ...interface{}) IQuery
	Update(modelObj models.IModel, p *PredicateRelationBuilder) IQuery
	GetDB() *gorm.DB
	GetDBOri() *gorm.DB
	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 {
	// contains filtered or unexported fields
}

func (*ModelAndBuilder) GetAllPotentialJoinStructDesignators added in v0.4.50

func (mb *ModelAndBuilder) GetAllPotentialJoinStructDesignators() ([]string, error)

func (*ModelAndBuilder) SortBuilderInfosByLevel added in v0.4.50

func (mb *ModelAndBuilder) SortBuilderInfosByLevel()

top level table has to be joined before more-nested table are joined

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

func (*Predicate) GetAllUnqueStructFieldDesignator added in v0.4.50

func (p *Predicate) GetAllUnqueStructFieldDesignator() map[string]interface{}

func (*Predicate) GetDesignatedField added in v0.4.47

func (p *Predicate) GetDesignatedField(modelObj models.IModel) string

func (*Predicate) GetDesignatedModel added in v0.4.47

func (p *Predicate) GetDesignatedModel(modelObj models.IModel) (models.IModel, error)

func (*Predicate) GetNestedLevel added in v0.4.50

func (p *Predicate) GetNestedLevel() int

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"
	// PredicateCondBETWEEN is between two values
	PredicateCondBETWEEN PredicateCond = "BETWEEN"
)

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)

func (*PredicateRelation) GetAllUnqueStructFieldDesignator added in v0.4.50

func (pr *PredicateRelation) GetAllUnqueStructFieldDesignator() map[string]interface{}

GetAllUnqueStructFieldDesignator returns all unique designators which are struct field Example: Dogs.DogToy.Name, Dogs.DogToy.Color, Name, furniture.Type will return Dogs.DogToy, Furniture This function is needed to figure out the join statement we need to issue

func (*PredicateRelation) GetDesignatedField added in v0.4.47

func (pr *PredicateRelation) GetDesignatedField(modelObj models.IModel) string

func (*PredicateRelation) GetDesignatedModel added in v0.4.47

func (pr *PredicateRelation) GetDesignatedModel(modelObj models.IModel) (models.IModel, error)

func (*PredicateRelation) GetNestedLevel added in v0.4.50

func (pr *PredicateRelation) GetNestedLevel() int

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

func NewPredicateRelationBuilder() *PredicateRelationBuilder

func (*PredicateRelationBuilder) And

func (p *PredicateRelationBuilder) And(args ...interface{}) *PredicateRelationBuilder

s is Name =?, v is value

func (*PredicateRelationBuilder) C

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

func (*PredicateRelationBuilder) GetPredicateRelation

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

func (*PredicateRelationBuilder) Or

func (p *PredicateRelationBuilder) Or(args ...interface{}) *PredicateRelationBuilder

s is Name =?, v is value

type Query

type Query struct {

	// args  []interface{}
	Err error
	// 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) BuildQuery added in v0.5.7

func (q *Query) BuildQuery(modelObj models.IModel) (*gorm.DB, error)

This is a passover for building query, we're just building the where clause

func (*Query) Count added in v0.5.7

func (q *Query) Count(modelObj models.IModel, no *int) IQuery

func (*Query) Create added in v0.4.37

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

func (*Query) CreateMany added in v0.5.1

func (q *Query) CreateMany(modelObjs []models.IModel) IQuery

func (*Query) Delete added in v0.4.37

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

Delete can be with criteria, or can just delete the model directly

func (*Query) DeleteMany added in v0.5.1

func (q *Query) DeleteMany(modelObjs []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) GetDB added in v0.4.46

func (q *Query) GetDB() *gorm.DB

func (*Query) GetDBOri added in v0.4.46

func (q *Query) GetDBOri() *gorm.DB

func (*Query) InnerJoin

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

args can be multiple C(), each C() works on one-level of modelObj The args are to select the query of modelObj designated, it could work on nested level inside the 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(modelObj models.IModel, p *PredicateRelationBuilder) IQuery

Update only allow one level of builder

type QueryType

type QueryType int

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

const (
	QueryTypeFirst QueryType = iota
	QueryTypeFind
)

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