sqlq

package
v0.0.0-...-a82c4c5 Latest Latest
Warning

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

Go to latest
Published: Mar 18, 2020 License: MIT Imports: 4 Imported by: 0

Documentation

Overview

Package sqlq deals with building of sql query.

Index

Constants

View Source
const (
	ASC  = "ASC"
	DESC = "DESC"
)

order operators

View Source
const (
	OpSelect = "select"
	OpInsert = "insert"
	OpUpdate = "update"
	OpDelete = "delete"
)

these flags describe whether query option is allowed for a specific db operation.

View Source
const DefaultSelectLimit = 1000

DefaultSelectLimit sets the default limit for select if no limit specified.

Variables

This section is empty.

Functions

This section is empty.

Types

type JoinConfig

type JoinConfig struct {
	Condition string
	StructPtr interface{}
	Columns   []string
	TableName string
}

JoinConfig describes join config.

type Option

type Option func(q *Query) (query string, queryType int, err error)

Option describes common function for building query.

func AND

func AND(opts ...Option) Option

AND combines multiple where options with AND condition.

func All

func All() Option

All enforses quering all data (like where 1=1) used to prevent unintentional update or delete of all rows in table.

func Columns

func Columns(columns ...string) Option

Columns specifies columns that needs to be fetched. By default all columns are fetched.

func Equal

func Equal(field string, value interface{}) Option

Equal adds where field = value construction to query.

func GreaterOrEqual

func GreaterOrEqual(field string, value interface{}) Option

GreaterOrEqual adds where field >= value construction to query.

func GreaterThan

func GreaterThan(field string, value interface{}) Option

GreaterThan adds where field > value construction to query.

func GroupBy

func GroupBy(columns ...string) Option

GroupBy adds group by construction to query. Example: mw.GroupBy("user_id"), or mw.GroupBy("user_id", "price")

func Having

func Having(opts ...Option) Option

Having adds having clause to select.

func IN

func IN(field string, values ...string) Option

IN adds IN construction to query.

func Join

func Join(structPtr interface{}, condition string, columns ...string) Option

Join adds join to query.

  1. define joined struct
  2. search for field with mw:"join" tag
  3. compare struct type, if same, add field address to fields slice
  4. when scan, check whether model row already exist, if yes, try to join rows
  5. TODO: how to join rows with multiple join case?????????????????????
  6. probably duplicate address to keep raw data in order to easy compare it

func LessOrEqual

func LessOrEqual(field string, value interface{}) Option

LessOrEqual adds where field <= value construction to query.

func LessThan

func LessThan(field string, value interface{}) Option

LessThan adds where field < value construction to query.

func Like

func Like(field string, pattern string) Option

Like adds where field LIKE pattern construction to query.

func Limit

func Limit(limit int) Option

Limit adds limit to query. If multiple limits specified, the last one will be set.

func NotEqual

func NotEqual(field string, value interface{}) Option

NotEqual adds where field != value construction to query.

func OR

func OR(opts ...Option) Option

OR combines multiple where options with OR condition.

func Offset

func Offset(offset int) Option

Offset adds limit offset to query. If multiple offsets specified, the last one will be set.

func Order

func Order(field string, orderBy string) Option

Order adds order to query. If multiple orders specified, each will be added to query. For example mw.Order("id", sqlq.ASC), mw.Order("updated", mwq.DESC) will produce ORDER BY "id" ASC, "updated" DESC.

func Raw

func Raw(query string, args ...interface{}) Option

Raw adds raw where query. Arguments in query expected to be marked as '?'. Example: query = "name = ? or status = ?", args = ["John", "active"]

type Query

type Query struct {
	Args       []interface{}
	Columns    []string
	Query      string
	Having     string
	IsQueryAll bool
	Joins      []JoinConfig
	// contains filtered or unexported fields
}

Query keeps query data during it's building.

func Build

func Build(opts []Option, queryType string, existingArgs ...interface{}) (*Query, error)

Build builds sql query from given query option

Jump to

Keyboard shortcuts

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