sqx

package
v0.0.0-...-6c293db Latest Latest
Warning

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

Go to latest
Published: Nov 25, 2021 License: BSD-2-Clause Imports: 9 Imported by: 0

README

SQL Query Builder

q, err := sqx.Parse("select * from foo where id = ?")
if err != nil {
    log.Fatal(err)
}

Documentation

Index

Constants

View Source
const EOF_LINE int = -1

Variables

View Source
var ValidFuncs []string

Functions

func NewWriter

func NewWriter(q Query, database, prefix, namespace, driver string) *writer

func Print

func Print(v interface{}) error

func Sprint

func Sprint(v interface{}) (string, error)

func ToSql

func ToSql(q Query, database, prefix, namespace, driver string) (string, []interface{}, error)

ToSql returns the parsed query

func ValidateSelect

func ValidateSelect(q *SelectQuery, options *ValidateOptions) error

Types

type AddColumnQuery

type AddColumnQuery struct {
	Pos      Position
	Database string
	Table    string
	Column   *CreateColumn
}

func (*AddColumnQuery) Parameters

func (q *AddColumnQuery) Parameters() []interface{}

func (*AddColumnQuery) Position

func (q *AddColumnQuery) Position() Position

type AddConstraintQuery

type AddConstraintQuery struct {
	Pos      Position
	Type     string
	Database string
	Table    string
	Name     string
	Columns  []*ColumnNameExpr
}

func (*AddConstraintQuery) Parameters

func (q *AddConstraintQuery) Parameters() []interface{}

func (*AddConstraintQuery) Position

func (q *AddConstraintQuery) Position() Position

type AddFKQuery

type AddFKQuery struct {
	Pos           Position
	Type          string
	Database      string
	Table         string
	Name          string
	Column        string
	RefDatabase   string
	RefTable      string
	RefColumn     string
	DeleteCascade bool
}

func (*AddFKQuery) Parameters

func (q *AddFKQuery) Parameters() []interface{}

func (*AddFKQuery) Position

func (q *AddFKQuery) Position() Position

type AllColumnsExpr

type AllColumnsExpr struct {
	Table string
	Pos   Position
}

func (*AllColumnsExpr) Position

func (q *AllColumnsExpr) Position() Position

type AlterDropQuery

type AlterDropQuery struct {
	Pos      Position
	Database string
	Table    string
	Type     string
	Item     string
}

func (*AlterDropQuery) Parameters

func (q *AlterDropQuery) Parameters() []interface{}

func (*AlterDropQuery) Position

func (q *AlterDropQuery) Position() Position

type BetweenExpr

type BetweenExpr struct {
	LExpr Expr
	RExpr Expr
}

func (*BetweenExpr) Position

func (i *BetweenExpr) Position() Position

type BinaryExpr

type BinaryExpr struct {
	Operator Type
	Left     Expr
	Right    Expr
}

func (*BinaryExpr) Position

func (i *BinaryExpr) Position() Position

type CallExpr

type CallExpr struct {
	Pos  Position
	Name string
	Args []Expr
}

func (*CallExpr) Position

func (i *CallExpr) Position() Position

type Column

type Column struct {
	Table string
	Name  string
}

type ColumnNameExpr

type ColumnNameExpr struct {
	Pos   Position
	Name  string
	Table string
	Alias string
}

func GetFilterColumns

func GetFilterColumns(q Query) []*ColumnNameExpr

func NameExprColumns

func NameExprColumns(e Expr) []*ColumnNameExpr

ExprColumns returns the column names in an expression.

func (*ColumnNameExpr) FullName

func (i *ColumnNameExpr) FullName() string

func (*ColumnNameExpr) Position

func (i *ColumnNameExpr) Position() Position

type ColumnType

type ColumnType byte
const (
	Int ColumnType = iota
	BigInt
	Decimal
	Char
	Varchar
	Text
	MediumText
	Bool
	Blob
	MediumBlob
	DatTime
)

type ColumnValue

type ColumnValue struct {
	Pos   Position
	Table string
	Name  string
	Expr  Expr
}

type ConstantExpr

type ConstantExpr struct {
	Pos   Position
	Kind  Type
	Value string
}

func (*ConstantExpr) Position

func (i *ConstantExpr) Position() Position

type Constraint

type Constraint struct {
	Name    string
	Type    string
	Columns []string
}

type CreateColumn

type CreateColumn struct {
	Name          string
	Type          ColumnType
	Nullable      bool
	Size          string
	Decimals      string
	Unsigned      bool
	Key           bool
	Default       string
	AutoIncrement bool
}

type CreateDatabaseQuery

type CreateDatabaseQuery struct {
	Pos         Position
	Name        string
	IfNotExists bool
}

func (*CreateDatabaseQuery) Parameters

func (q *CreateDatabaseQuery) Parameters() []interface{}

func (*CreateDatabaseQuery) Position

func (q *CreateDatabaseQuery) Position() Position

type CreateTableConstraint

type CreateTableConstraint interface {
	// contains filtered or unexported methods
}

type CreateTableQuery

type CreateTableQuery struct {
	Pos         Position
	Database    string
	Name        string
	Columns     []*CreateColumn
	Constraints []CreateTableConstraint
	IfNotExists bool
}

func (*CreateTableQuery) Parameters

func (q *CreateTableQuery) Parameters() []interface{}

func (*CreateTableQuery) Position

func (q *CreateTableQuery) Position() Position

type DateIntervalExpr

type DateIntervalExpr struct {
	Pos   Position
	Value int
	Type  string
}

func (*DateIntervalExpr) Position

func (i *DateIntervalExpr) Position() Position

type DeleteQuery

type DeleteQuery struct {
	Pos       Position
	Alias     []string
	Table     *Table
	WherePart *WherePart
	LimitPart *Limit
}

func (*DeleteQuery) And

func (q *DeleteQuery) And(code string, params ...interface{}) error

func (*DeleteQuery) AndQuery

func (q *DeleteQuery) AndQuery(filter *SelectQuery)

func (*DeleteQuery) Join

func (q *DeleteQuery) Join(code string, params ...interface{}) error

func (*DeleteQuery) Limit

func (q *DeleteQuery) Limit(rowCount int)

func (*DeleteQuery) LimitOffset

func (q *DeleteQuery) LimitOffset(offset, rowCount int)

func (*DeleteQuery) Or

func (q *DeleteQuery) Or(code string, params ...interface{}) error

func (*DeleteQuery) OrQuery

func (q *DeleteQuery) OrQuery(filter *SelectQuery)

func (*DeleteQuery) Parameters

func (q *DeleteQuery) Parameters() []interface{}

func (*DeleteQuery) Position

func (q *DeleteQuery) Position() Position

func (*DeleteQuery) Where

func (q *DeleteQuery) Where(code string, params ...interface{}) error

type DropDatabaseQuery

type DropDatabaseQuery struct {
	Pos      Position
	Database string
	IfExists bool
}

func (*DropDatabaseQuery) Parameters

func (q *DropDatabaseQuery) Parameters() []interface{}

func (*DropDatabaseQuery) Position

func (q *DropDatabaseQuery) Position() Position

type DropTableQuery

type DropTableQuery struct {
	Pos      Position
	Database string
	Table    string
	IfExists bool
}

func (*DropTableQuery) Parameters

func (q *DropTableQuery) Parameters() []interface{}

func (*DropTableQuery) Position

func (q *DropTableQuery) Position() Position

type Error

type Error struct {
	Pos     Position
	Message string
	Token   string
}

func (*Error) Error

func (e *Error) Error() string

func (*Error) Position

func (e *Error) Position() Position

type Expr

type Expr interface {
	Node
	// contains filtered or unexported methods
}

All expression nodes implement the Expr interface.

type ForeginKey

type ForeginKey struct {
	Name          string
	Column        string
	RefTable      string
	RefColumn     string
	DeleteCascade bool
}

type FromAsExpr

type FromAsExpr struct {
	From  *ParenExpr
	Alias string
}

func (*FromAsExpr) Position

func (a *FromAsExpr) Position() Position

type GroupConcatExpr

type GroupConcatExpr struct {
	Pos         Position
	Distinct    bool
	Expressions []Expr
	OrderByPart []*OrderColumn
	Separator   string
}

func (*GroupConcatExpr) Position

func (i *GroupConcatExpr) Position() Position

type IdentExpr

type IdentExpr struct {
	Pos  Position
	Name string
}

func (*IdentExpr) Position

func (i *IdentExpr) Position() Position

type InExpr

type InExpr struct {
	LParen Position
	Values []Expr
	RParen Position
}

func (*InExpr) Position

func (i *InExpr) Position() Position

type InsertQuery

type InsertQuery struct {
	Pos     Position
	Table   *TableName
	Columns []*ColumnNameExpr
	Values  []Expr
	Select  *SelectQuery // in case is a insert from a select
}

func (*InsertQuery) AddColumn

func (q *InsertQuery) AddColumn(name string, value interface{}) error

func (*InsertQuery) Parameters

func (q *InsertQuery) Parameters() []interface{}

func (*InsertQuery) Position

func (q *InsertQuery) Position() Position

type Join

type Join struct {
	Pos       Position
	Type      Type
	TableExpr SqlFrom
	Alias     string
	On        Expr
}

type Limit

type Limit struct {
	Pos      Position
	RowCount Expr
	Offset   Expr
}

type ModifyColumnQuery

type ModifyColumnQuery struct {
	Pos      Position
	Database string
	Table    string
	Column   *CreateColumn
}

func (*ModifyColumnQuery) Parameters

func (q *ModifyColumnQuery) Parameters() []interface{}

func (*ModifyColumnQuery) Position

func (q *ModifyColumnQuery) Position() Position

type Node

type Node interface {
	Position() Position
}

All node types implement the Node interface.

type OrderColumn

type OrderColumn struct {
	Expr Expr
	Type Type
}

type ParameterExpr

type ParameterExpr struct {
	Pos   Position
	Name  string
	Value interface{}
}

func (*ParameterExpr) Position

func (q *ParameterExpr) Position() Position

type ParamsQuery

type ParamsQuery interface {
	GetParams() []interface{}
}

type ParenExpr

type ParenExpr struct {
	LParen Position
	X      Expr
	RParen Position
}

func (*ParenExpr) Position

func (i *ParenExpr) Position() Position

type Parser

type Parser struct {
	// If it replaces values with parameters
	ReplaceParams bool

	Params []interface{}
	// contains filtered or unexported fields
}

func NewParser

func NewParser(r io.Reader) *Parser

func NewStrParser

func NewStrParser(code string) *Parser

func (*Parser) AssertParamsSet

func (p *Parser) AssertParamsSet() error

func (*Parser) Parse

func (p *Parser) Parse() ([]Query, error)

Parse parses a sql script. It can contain one or many queries.

func (*Parser) ParseQuery

func (p *Parser) ParseQuery() (Query, error)

ParseQuery parses a single query.

func (*Parser) SetParams

func (p *Parser) SetParams(args []interface{})

type Position

type Position struct {
	Line   int
	Column int
	Length int
}

func (Position) String

func (p Position) String() string

type Printer

type Printer struct {
	Positions bool
}

A helper to inspect the Module

func (*Printer) Print

func (p *Printer) Print(v interface{}) error

func (*Printer) Printf

func (p *Printer) Printf(w io.Writer, v interface{}) error

type Query

type Query interface {
	Node

	Parameters() []interface{}
	// contains filtered or unexported methods
}

All SQL queries implement the Query interface.

func Parse

func Parse(code string, params ...interface{}) (Query, error)

Parse parses a single query.

type QueryTable

type QueryTable struct {
	Name     string
	Database string
	Alias    string
	LeftJoin bool
}

func GetSelectMainTable

func GetSelectMainTable(q Query) *QueryTable

Return the table in a basic SELECT FROM table

func GetTables

func GetTables(q Query) []*QueryTable

type RenameColumnQuery

type RenameColumnQuery struct {
	Pos      Position
	Database string
	Table    string
	Name     string
	Column   *CreateColumn
}

func (*RenameColumnQuery) Parameters

func (q *RenameColumnQuery) Parameters() []interface{}

func (*RenameColumnQuery) Position

func (q *RenameColumnQuery) Position() Position

type SelectColumnExpr

type SelectColumnExpr struct {
	Expr  Expr
	Alias string
}

func (*SelectColumnExpr) Position

func (q *SelectColumnExpr) Position() Position

type SelectQuery

type SelectQuery struct {
	Pos         Position
	Distinct    bool
	ForUpdate   bool
	Columns     []Expr
	From        []SqlFrom
	WherePart   *WherePart
	GroupByPart []Expr
	HavingPart  *WherePart
	OrderByPart []*OrderColumn
	LimitPart   *Limit
	UnionPart   []*SelectQuery
}

func Select

func Select(code string, params ...interface{}) (*SelectQuery, error)

func Where

func Where(code string, params ...interface{}) (*SelectQuery, error)

Where parses a select query filter

func (*SelectQuery) AddColumns

func (q *SelectQuery) AddColumns(code string) error

func (*SelectQuery) And

func (q *SelectQuery) And(code string, params ...interface{}) error

func (*SelectQuery) AndQuery

func (q *SelectQuery) AndQuery(filter *SelectQuery)

func (*SelectQuery) GroupBy

func (q *SelectQuery) GroupBy(code string) error

func (*SelectQuery) Having

func (q *SelectQuery) Having(code string, params ...interface{}) error

func (*SelectQuery) Join

func (q *SelectQuery) Join(code string, params ...interface{}) error

func (*SelectQuery) Limit

func (q *SelectQuery) Limit(rowCount int)

func (*SelectQuery) LimitOffset

func (q *SelectQuery) LimitOffset(offset, rowCount int)

func (*SelectQuery) Or

func (q *SelectQuery) Or(code string, params ...interface{}) error

func (*SelectQuery) OrQuery

func (q *SelectQuery) OrQuery(filter *SelectQuery)

func (*SelectQuery) OrderBy

func (q *SelectQuery) OrderBy(code string) error

func (*SelectQuery) Parameters

func (q *SelectQuery) Parameters() []interface{}

func (*SelectQuery) Position

func (q *SelectQuery) Position() Position

func (*SelectQuery) SetColumns

func (q *SelectQuery) SetColumns(code string) error

func (*SelectQuery) SetFrom

func (q *SelectQuery) SetFrom(code string) error

func (*SelectQuery) Where

func (q *SelectQuery) Where(code string, params ...interface{}) error

type ShowQuery

type ShowQuery struct {
	Pos      Position
	Type     string
	Database string
	Table    string
}

func (*ShowQuery) Parameters

func (q *ShowQuery) Parameters() []interface{}

func (*ShowQuery) Position

func (q *ShowQuery) Position() Position

type SqlFrom

type SqlFrom interface {
	Node
	// contains filtered or unexported methods
}

type Table

type Table struct {
	Pos      Position
	Name     string
	Database string
	Alias    string
	Joins    []*Join
}

func (*Table) FullName

func (q *Table) FullName() string

func (*Table) Position

func (q *Table) Position() Position

func (*Table) RefName

func (q *Table) RefName() string

RefName returns the name as how it should be referenced. If it has an alias, the alias, and if not, the name.

type TableName

type TableName struct {
	Pos      Position
	Database string
	Name     string
}

type Token

type Token struct {
	Type Type
	Str  string
	Pos  Position
}

type Type

type Type byte
const (
	NOTSET Type = iota
	ERROR
	EOF
	COMMENT // --

	// Keywords
	CREATE
	SHOW
	DROP
	ALTER
	TABLE
	DATABASE
	NOT
	EXISTS
	CONSTRAINT
	INTEGER
	BIGINT
	DECIMAL
	CHAR
	VARCHAR
	TEXT
	MEDIUMTEXT
	BOOL
	BLOB
	MEDIUMBLOB
	DATETIME
	DEFAULT
	UNSIGNED

	SELECT
	DISTINCT
	INSERT
	INTO
	VALUES
	UPDATE
	SET
	DELETE
	FROM
	WHERE
	GROUP
	HAVING
	JOIN
	LEFT
	RIGHT
	INNER
	OUTER
	CROSS
	ON
	AS
	IN
	NOTIN
	BETWEEN
	LIKE
	IS
	ISNOT
	NOTLIKE
	ORDER
	BY
	ASC
	DESC
	RANDOM
	LIMIT
	UNION
	AND
	OR
	NULL
	TRUE
	FALSE

	FOR

	IDENT  //  fields, tables...
	INT    // 12345
	FLOAT  // 123.45
	STRING // "abc"

	// Operators and delimiters
	ADD // +
	SUB // -
	MUL // *
	DIV // /
	MOD // %

	LSF // << left shift
	RSF // >> right shift
	ANB // &  binary AND

	EQL // =
	LSS // <
	GTR // >
	NT  // !

	NEQ // !=
	LEQ // <=
	GEQ // >=

	LPAREN // (
	LBRACK // [
	LBRACE // {
	COMMA  // ,
	PERIOD // .

	RPAREN    // )
	COLON     // ;
	SEMICOLON // ;
	QUESTION  // ?
)

func (Type) String

func (i Type) String() string

type UnaryExpr

type UnaryExpr struct {
	Pos      Position
	Operator Type
	Operand  Expr
}

func (*UnaryExpr) Position

func (i *UnaryExpr) Position() Position

type UpdateQuery

type UpdateQuery struct {
	Pos       Position
	Table     *Table
	Columns   []ColumnValue
	WherePart *WherePart
	LimitPart *Limit
}

func (*UpdateQuery) AddColumns

func (q *UpdateQuery) AddColumns(code string, params ...interface{}) error

func (*UpdateQuery) And

func (q *UpdateQuery) And(code string, params ...interface{}) error

func (*UpdateQuery) AndQuery

func (q *UpdateQuery) AndQuery(filter *SelectQuery)

func (*UpdateQuery) Join

func (q *UpdateQuery) Join(code string, params ...interface{}) error

func (*UpdateQuery) Limit

func (q *UpdateQuery) Limit(rowCount int)

func (*UpdateQuery) LimitOffset

func (q *UpdateQuery) LimitOffset(offset, rowCount int)

func (*UpdateQuery) Or

func (q *UpdateQuery) Or(code string, params ...interface{}) error

func (*UpdateQuery) OrQuery

func (q *UpdateQuery) OrQuery(filter *SelectQuery)

func (*UpdateQuery) Parameters

func (q *UpdateQuery) Parameters() []interface{}

func (*UpdateQuery) Position

func (q *UpdateQuery) Position() Position

func (*UpdateQuery) SetColumns

func (q *UpdateQuery) SetColumns(code string, params ...interface{}) error

func (*UpdateQuery) Where

func (q *UpdateQuery) Where(code string, params ...interface{}) error

type ValidateOptions

type ValidateOptions struct {
	Tables []*ValidateTable
	// contains filtered or unexported fields
}

type ValidateTable

type ValidateTable struct {
	Name     string
	Database string
	Columns  []string
}

type WherePart

type WherePart struct {
	Pos  Position
	Expr Expr
}

Jump to

Keyboard shortcuts

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