parse

package
v0.0.0-...-31c2986 Latest Latest
Warning

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

Go to latest
Published: Nov 6, 2022 License: MIT Imports: 6 Imported by: 0

Documentation

Overview

Package parse is a simple sql lexer/parser with limited functionality it can handle queries like :

select fields from table where "field" = 'string' and (another_field=100 or boolean_field) order by field_1 desc , field_2 asc limit 10, 100

the result is some sort of abstract source tree :) the package is based on net/html and rob pike talk about writing lexer in go.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func GetTokenString

func GetTokenString(t Item) string

GetTokenString is a simple function to handle the quoted strings, it remove the single or double quote from the string and remove escape sequence

Types

type Field

type Field struct {
	Table      string // table name if it is a table field
	Alias      string // alias if this has an alias (not supported yet)
	Item       Item   // Token Item, it can be alpha, string literal, number or bool
	Parameters Fields // Parameters is only valid for function fields
}

Field is the fields inside select and functions, for functions the parameters is valid

type Fields

type Fields []Field

Fields is the collection of fields with order

type FuncItem

type FuncItem interface {
	Item
	Parameters() Fields
}

FuncItem is an item with its parameter

type Item

type Item interface {
	fmt.Stringer
	Type() ItemType
	Pos() int
	Value() string
	Data() int
}

Item is an interface to handle the item, any item in the query

type ItemType

type ItemType int

ItemType is each lexeme type, also error and eof. any new keyword should be here first

const (
	// ItemEOF eof is zero, so any data from closed channel (with zero value) is eof
	ItemEOF ItemType = iota
	// ItemError when there is an error
	ItemError
	// ItemWhiteSpace any whitespace sequence
	ItemWhiteSpace
	// ItemSelect sql select stmt
	ItemSelect
	// ItemFrom sql from stmt
	ItemFrom
	// ItemWhere sql where stmt
	ItemWhere
	// ItemOrder sql order stmt
	ItemOrder
	// ItemBy sql by stmt
	ItemBy
	// ItemOr sql or stmt
	ItemOr
	// ItemAnd sql and stmt
	ItemAnd
	// ItemIs sql is stmt
	ItemIs
	// ItemNull sql null stmt
	ItemNull
	// ItemNot sql not stmt
	ItemNot
	// ItemLimit sql limit stmt
	ItemLimit
	// ItemAsc sql asc stmt
	ItemAsc
	// ItemDesc sql desc stmt
	ItemDesc
	// ItemLike sql like stmt
	ItemLike
	// ItemAlpha is a string
	ItemAlpha
	// ItemNumber is a number
	ItemNumber
	// ItemFalse is the bool expression (false)
	ItemFalse
	// ItemTrue is the true
	ItemTrue
	// ItemEqual is =
	ItemEqual
	// ItemGreater is >
	ItemGreater
	// ItemLesser is <
	ItemLesser
	// ItemGreaterEqual is >=
	ItemGreaterEqual
	// ItemLesserEqual is <=
	ItemLesserEqual
	// ItemNotEqual is <>
	ItemNotEqual
	// ItemParenOpen is (
	ItemParenOpen
	// ItemParenClose is )
	ItemParenClose
	// ItemComma is ,
	ItemComma
	// ItemWildCard is *
	ItemWildCard
	// ItemLiteral1 is 'string in single quote'
	ItemLiteral1
	// ItemLiteral2 is "string in double quote"
	ItemLiteral2
	// ItemSemicolon is ;
	ItemSemicolon
	// ItemDot is .
	ItemDot
	// ItemDollarSign is the $ followed by the number
	ItemQuestionMark
	// ItemFunc is the function
	ItemFunc
)

type Order

type Order struct {
	Field string // Field name
	Index int    // Index in the actual table
	DESC  bool   // order asc or desc
}

Order is one order in the order array

type Orders

type Orders []Order

Orders group of orders, the first order is more important than the next

type Query

type Query struct {
	Statement Statement
}

Query is the single query

func AST

func AST(q string) (*Query, error)

AST return the abstract source tree for given query, currently only select is supported

type SelectStmt

type SelectStmt struct {
	Table  string // Which table tos elect from
	Fields Fields // Which field are requested

	Where Stack  // Where stack (need more work on where stack )
	Order Orders // Orders in order part, if any
	Start int    // the start column , -1 means no start specified
	Count int    // the count to show, -1 means no count specified
	// contains filtered or unexported fields
}

SelectStmt is the select query

func (*SelectStmt) ParamCount

func (ss *SelectStmt) ParamCount() int

ParamCount return the number of parameters required for this

type Stack

type Stack interface {
	Pop() (Item, error)
	Push(...Item)
	Peek() (Item, error)
}

Stack is very silly implementation of stack, just for simplify things in where part

func NewStack

func NewStack(capacity int) Stack

NewStack create a new stack with initial capacity

type Statement

type Statement interface {
	// ParamCount return how many parameter this statement needs.
	ParamCount() int
	// contains filtered or unexported methods
}

Statement is the statement interface, only select is supported currently

Jump to

Keyboard shortcuts

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