sql

package
v2.9.0 Latest Latest
Warning

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

Go to latest
Published: Mar 4, 2021 License: Apache-2.0 Imports: 14 Imported by: 0

Documentation

Index

Constants

View Source
const (
	SQLTypeSelect = "select"
	SQLTypeShow   = "show"
	SQLTypeEmpty  = ""
)
View Source
const (
	SelectPartDistinct selectPart = 1 << iota
	SelectPartID
	SelectPartStar
	SelectPartField
	SelectPartFields
	SelectPartCountStar
	SelectPartCountField
	SelectPartCountDistinctField
	SelectPartMinField
	SelectPartMaxField
	SelectPartSumField
	SelectPartAvgField
)
View Source
const (
	FromPartTable fromPart = 1 << iota
	FromPartTables
	FromPartJoin
)
View Source
const (
	WherePartIDCondition wherePart = 1 << iota
	WherePartFieldCondition
	WherePartMultiFieldCondition
)
View Source
const (
	GroupByPartField groupByPart = 1 << iota
	GroupByPartFields
)
View Source
const (
	OrderByPartField orderByPart = 1 << iota
	OrderByPartFields
)
View Source
const (
	LimitPartLimit limitPart = 1 << iota
	LimitPartOffset
)
View Source
const ColID = "_id"
View Source
const (
	HavingPartCondition havingPart = 1 << iota
)

Variables

View Source
var (
	ErrIncompleteHeaders = errors.New("incomplete header assignment")
	ErrFieldNotInHeaders = errors.New("field not found in source header")
)
View Source
var (
	ErrMultipleSQLStatements = errors.New("statement contains multiple sql queries")
)

System errors.

View Source
var (
	ErrUnsupportedQuery = errors.New("unsupported query")
)

Functions

func All

func All() string

All creates an All query. Returns the set columns with existence true.

func ApplyValCountFunc

func ApplyValCountFunc(rowser pproto.ToRowser, fn FuncName) pproto.ToRowser

ApplyValCountFunc converts a ValCount result to the proper result for Func

func AssignHeaders

func AssignHeaders(rowser pproto.ToRowser, headers ...Column) pproto.ToRowser

AssignHeaders assigns headers to a ToRowser.

func Between

func Between(fieldName string, a interface{}, b interface{}) string

Between creates a between query.

func ConstRow

func ConstRow(ids ...interface{}) string

ConstRow creates a query value that uses a list of columns in place of a Row query.

func ConvertToTime

func ConvertToTime(text string) (time.Time, bool)

func Count

func Count(rowCall string) string

Count creates a Count query. Returns the number of set columns in the ROW_CALL passed in.

func CountRows

func CountRows(rowser pproto.ToRowser) pproto.ToRowser

CountRows counts the rows from the input rowser.

func Distinct

func Distinct(indexName, fieldName string) string

Distinct creates a Distinct query.

func Equals

func Equals(fieldName string, value interface{}) string

Equals creates an equals query.

func Extract

func Extract(rowCall string, fields ...string) string

Extract creates an Extract query. It accepts a bitmap query to select columns and a list of fields to select rows.

func ExtractFieldName

func ExtractFieldName(columName string) (fieldName string, isSpecial bool)

func GT

func GT(fieldName string, value interface{}) string

GT creates a greater than query.

func GTE

func GTE(fieldName string, value interface{}) string

GTE creates a greater than or equal query.

func GroupByBase

func GroupByBase(rows []string, limit int64, filter, aggregate, having string) (string, error)

GroupByBase creates a GroupBy query with the given functional options.

func Intersect

func Intersect(rows ...string) string

Intersect query - see rowOperation

func LT

func LT(fieldName string, value interface{}) string

LT creates a less than query.

func LTE

func LTE(fieldName string, value interface{}) string

LTE creates a less than or equal query.

func Like

func Like(fieldName string, pattern string) string

Like creates a Rows query filtered by a pattern. An underscore ('_') can be used as a placeholder for a single UTF-8 codepoint or a percent sign ('%') can be used as a placeholder for 0 or more codepoints. All other codepoints in the pattern are matched exactly.

func Limit

func Limit(row string, limit uint, offset uint) string

Limit creates a limit query.

func LimitRows

func LimitRows(rowser pproto.ToRowser, limit uint) pproto.ToRowser

LimitRows applies a limit to a ToRowser.

func Max

func Max(fieldName string, row string) string

Max creates a max query.

func Min

func Min(fieldName string, row string) string

Min creates a min query.

func Not

func Not(rows ...string) string

Not query

func NotEquals

func NotEquals(fieldName string, value interface{}) string

NotEquals creates a not equals query.

func NotNull

func NotNull(fieldName string) string

NotNull creates a not equal to null query.

func Offset

func Offset(row string, offset uint) string

Offset creates a limit query but only with an offset.

func OffsetRows

func OffsetRows(rowser pproto.ToRowser, offset uint) pproto.ToRowser

OffsetRows applies an offset to a ToRowser.

func OrderBy

func OrderBy(rowser pproto.ToRowser, fields, dirs []string) pproto.ToRowser

OrderBy sorts a rowser.

func Row

func Row(fieldName string, rowIDOrKey interface{}) (string, error)

Row query

func RowDistinct

func RowDistinct(indexName, fieldName string, row string) string

RowDistinct creates a Distinct query with the given row filter.

func RowRange

func RowRange(fieldName string, rowIDOrKey interface{}, start time.Time, end time.Time) (string, error)

RowRange is a Row query with from,to times

func RowTopN

func RowTopN(fieldName string, n uint64, row string) string

RowTopN creates a TopN query with the given item count and row. This variant supports customizing the row query.

func Rows

func Rows(fieldName string) string

Rows creates a Rows query with defaults

func RowsLimit

func RowsLimit(fieldName string, limit int64) (string, error)

RowsLimit creates a Rows query with the given limit

func Sum

func Sum(fieldName string, row string) string

Sum creates a sum query.

func TopN

func TopN(fieldName string, n uint64) string

TopN creates a TopN query with the given item count. Returns the id and count of the top n rows (by count of columns) in the field.

func Union

func Union(rows ...string) string

Union query - see rowOperation

Types

type Attributes

type Attributes map[string]interface{}

type BasicColumn

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

func NewBasicColumn

func NewBasicColumn(s, n, a string) *BasicColumn

func (*BasicColumn) Alias

func (b *BasicColumn) Alias() string

func (*BasicColumn) Name

func (b *BasicColumn) Name() string

func (*BasicColumn) Source

func (b *BasicColumn) Source() string

type Column

type Column interface {
	Source() string
	Name() string
	Alias() string
}

Column is an interface which supports mapping to source headers, and aliasing column names. Alias() should always return a value; either a unique alias, or the same value return by Name(), but never an empty string.

type DDLHandler

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

DDLHandler executes CREATE, ALTER, DROP, RENAME, TRUNCATE or ANALYZE statement.

func NewDDLHandler

func NewDDLHandler(api *pilosa.API) *DDLHandler

NewDDLHandler constructor

func (*DDLHandler) Handle

func (h *DDLHandler) Handle(ctx context.Context, mapped *MappedSQL) (pproto.ToRowser, error)

Handle executes mapped SQL

type FieldColumn

type FieldColumn struct {
	Field *pilosa.Field
	// contains filtered or unexported fields
}

func NewFieldColumn

func NewFieldColumn(field *pilosa.Field, alias string) *FieldColumn

func (*FieldColumn) Alias

func (f *FieldColumn) Alias() string

func (*FieldColumn) Name

func (f *FieldColumn) Name() string

func (*FieldColumn) Source

func (f *FieldColumn) Source() string

type FuncColumn

type FuncColumn struct {
	Field    *pilosa.Field
	FuncName FuncName
	// contains filtered or unexported fields
}

func NewFuncColumn

func NewFuncColumn(funcName FuncName, field *pilosa.Field, alias string) *FuncColumn

func (*FuncColumn) Alias

func (f *FuncColumn) Alias() string

func (*FuncColumn) Name

func (f *FuncColumn) Name() string

func (*FuncColumn) Source

func (f *FuncColumn) Source() string

type FuncName

type FuncName string
const (
	FuncCount FuncName = "count"
	FuncMin   FuncName = "min"
	FuncMax   FuncName = "max"
	FuncSum   FuncName = "sum"
	FuncAvg   FuncName = "avg"
)

type HavingClause

type HavingClause struct {
	Subj string
	Cond pql.Condition
}

type IDIndexColumn

type IDIndexColumn struct {
	Index *pilosa.Index
	// contains filtered or unexported fields
}

func NewIDIndexColumn

func NewIDIndexColumn(index *pilosa.Index, alias string) *IDIndexColumn

func (*IDIndexColumn) Alias

func (i *IDIndexColumn) Alias() string

func (*IDIndexColumn) Name

func (i *IDIndexColumn) Name() string

func (*IDIndexColumn) Source

func (i *IDIndexColumn) Source() string

type KeyIndexColumn

type KeyIndexColumn struct {
	Index *pilosa.Index
	// contains filtered or unexported fields
}

TODO: what the difference between this and IDIndexColumn?

func NewKeyIndexColumn

func NewKeyIndexColumn(index *pilosa.Index, alias string) *KeyIndexColumn

func (*KeyIndexColumn) Alias

func (i *KeyIndexColumn) Alias() string

func (*KeyIndexColumn) Name

func (i *KeyIndexColumn) Name() string

func (*KeyIndexColumn) Source

func (i *KeyIndexColumn) Source() string

type MappedSQL

type MappedSQL struct {
	SQLType   string
	Statement sqlparser.Statement
	Mask      QueryMask
	Tables    []string
	SQL       string
}

type Mapper

type Mapper struct {
	Logger logger.Logger
}

Mapper is responsible for mapping a SQL query to structure representation

func NewMapper

func NewMapper() *Mapper

func (*Mapper) MapSQL

func (m *Mapper) MapSQL(sql string) (*MappedSQL, error)

MapSQL converts a sql string into a MappedSQL object, which includes the parsed query and the query mask, among other information about the query.

func (*Mapper) Parse

func (m *Mapper) Parse(sql string) (sqlparser.Statement, QueryMask, error)

Parse parses SQL query

type MappingResult

type MappingResult struct {
	IndexName    string
	ColumnIDs    []uint64
	ColumnKeys   []string
	FieldFilters []string
	Limit        uint64
	Offset       uint64
	Query        string
	Header       []Column
	Reducers     []func(pproto.ToRowser) pproto.ToRowser
}

type QueryMask

type QueryMask struct {
	SelectMask  selectPart
	FromMask    fromPart
	WhereMask   wherePart
	GroupByMask groupByPart
	HavingMask  havingPart
	OrderByMask orderByPart
	LimitMask   limitPart
}

func GenerateMask

func GenerateMask(parsed sqlparser.Statement) QueryMask

func MustGenerateMask

func MustGenerateMask(sql string) QueryMask

func NewQueryMask

func NewQueryMask(sp selectPart, fp fromPart, wp wherePart, gp groupByPart, hp havingPart) QueryMask

func (*QueryMask) ApplyFilter

func (m *QueryMask) ApplyFilter(f QueryMask) bool

ApplyFilter returns true if m passes the filter f. Note: only certain query parts are included; namely, the orderBy and limit masks are not applied to the filter.

func (*QueryMask) HasFrom

func (m *QueryMask) HasFrom() bool

HasFrom returns true if the mask contains a supported from clause.

func (*QueryMask) HasGroupBy

func (m *QueryMask) HasGroupBy() bool

HasGroupBy returns true if the mask contains a supported group by clause.

func (*QueryMask) HasHaving

func (m *QueryMask) HasHaving() bool

HasHaving returns true if the mask contains a supported having clause.

func (*QueryMask) HasLimit

func (m *QueryMask) HasLimit() bool

HasLimit returns true if the mask contains a supported limit clause.

func (*QueryMask) HasOrderBy

func (m *QueryMask) HasOrderBy() bool

HasOrderBy returns true if the mask contains a supported order by clause.

func (*QueryMask) HasSelect

func (m *QueryMask) HasSelect() bool

HasSelect returns true if the mask contains a supported select clause.

func (*QueryMask) HasSelectPart

func (m *QueryMask) HasSelectPart(p selectPart) bool

HasSelectPart returns true if the mask contains the provided select part.

func (*QueryMask) HasWhere

func (m *QueryMask) HasWhere() bool

HasWhere returns true if the mask contains a supported where clause.

type SelectHandler

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

SelectHandler executes SQL select statements

func NewSelectHandler

func NewSelectHandler(api *pilosa.API) *SelectHandler

NewSelectHandler constructor

func (*SelectHandler) Handle

func (s *SelectHandler) Handle(ctx context.Context, mapped *MappedSQL) (pproto.ToRowser, error)

Handle executes mapped SQL

type SelectProperties

type SelectProperties struct {
	Index             *pilosa.Index
	Fields            []Column
	Features          selectFeatures
	WherePQL          string
	WhereIDs          []uint64
	WhereKeys         []string
	Offset            uint
	Limit             uint
	GroupByFieldNames []string
	Having            *HavingClause
}

type ShowHandler

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

ShowHandler executes SQL show table/field statements

func NewShowHandler

func NewShowHandler(api *pilosa.API) *ShowHandler

NewShowHandler constructor

func (*ShowHandler) Handle

func (s *ShowHandler) Handle(ctx context.Context, mapped *MappedSQL) (pproto.ToRowser, error)

Handle executes mapped SQL

type StarColumn

type StarColumn struct{}

func NewStarColumn

func NewStarColumn() *StarColumn

func (*StarColumn) Alias

func (s *StarColumn) Alias() string

func (*StarColumn) Name

func (s *StarColumn) Name() string

func (*StarColumn) Source

func (s *StarColumn) Source() string

Jump to

Keyboard shortcuts

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