sql

package
v0.6.0 Latest Latest
Warning

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

Go to latest
Published: Jul 26, 2025 License: MIT Imports: 3 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func DetectSQL

func DetectSQL(input string) bool

DetectSQL detects if the input string is a SQL statement

func IsSelectAll

func IsSelectAll(fields []SelectField) bool

IsSelectAll checks if the SELECT fields represent SELECT *

Types

type Condition

type Condition struct {
	Field    string           // Field name
	Operator string           // "=", ">", "<", ">=", "<=", "!=", "LIKE", "IN", "NOT IN", "IS NULL", "IS NOT NULL"
	Value    any              // Single value
	Values   []any            // Multiple values for IN clause
	Subquery *SelectStatement // Subquery for IN/EXISTS clauses
}

Condition represents a single condition

func NewCondition

func NewCondition(field, operator string, value any) *Condition

NewCondition creates a new simple condition

func NewInCondition

func NewInCondition(field string, values []any) *Condition

NewInCondition creates a new IN condition

type DeleteStatement

type DeleteStatement struct {
	Table string
	Where *WhereClause
}

DeleteStatement represents a DELETE statement

func (*DeleteStatement) GetTableName

func (s *DeleteStatement) GetTableName() string

func (*DeleteStatement) GetType

func (s *DeleteStatement) GetType() StatementType

type InsertStatement

type InsertStatement struct {
	Table  string
	Fields []string
	Values [][]any // Multiple rows of values
}

InsertStatement represents an INSERT statement

func (*InsertStatement) GetTableName

func (s *InsertStatement) GetTableName() string

func (*InsertStatement) GetType

func (s *InsertStatement) GetType() StatementType

type JoinClause

type JoinClause struct {
	Type      JoinType
	Table     TableRef
	Condition *WhereClause
}

JoinClause represents a JOIN clause

type JoinType

type JoinType int

JoinType represents the type of JOIN

const (
	JoinTypeInner JoinType = iota
	JoinTypeLeft
	JoinTypeRight
	JoinTypeFull
)

type Lexer

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

Lexer represents the lexical analyzer

func NewLexer

func NewLexer(input string) *Lexer

NewLexer creates a new lexer instance

func (*Lexer) NextToken

func (l *Lexer) NextToken() Token

NextToken returns the next token

type OrderByClause

type OrderByClause struct {
	Field     string
	Direction OrderDirection
}

OrderByClause represents an ORDER BY clause

type OrderDirection

type OrderDirection int

OrderDirection represents sort direction

const (
	OrderDirectionAsc OrderDirection = iota
	OrderDirectionDesc
)

type Parser

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

Parser represents the SQL parser

func NewParser

func NewParser(input string) *Parser

NewParser creates a new parser instance

func (*Parser) Errors

func (p *Parser) Errors() []string

Errors returns parsing errors

func (*Parser) Parse

func (p *Parser) Parse() (SQLStatement, error)

Parse parses the SQL statement and returns AST

type SQLStatement

type SQLStatement interface {
	GetType() StatementType
	GetTableName() string
}

SQLStatement represents a parsed SQL statement

type SelectField

type SelectField struct {
	Expression string // Field name or expression
	Alias      string // Optional alias
}

SelectField represents a field in SELECT clause

type SelectStatement

type SelectStatement struct {
	Fields  []SelectField
	From    TableRef
	Where   *WhereClause
	OrderBy []*OrderByClause
	GroupBy []string
	Having  *WhereClause
	Limit   *int
	Offset  *int
	Joins   []*JoinClause
}

SelectStatement represents a SELECT statement

func (*SelectStatement) GetTableName

func (s *SelectStatement) GetTableName() string

func (*SelectStatement) GetType

func (s *SelectStatement) GetType() StatementType

type StatementType

type StatementType int

StatementType represents the type of SQL statement

const (
	StatementTypeSelect StatementType = iota
	StatementTypeInsert
	StatementTypeUpdate
	StatementTypeDelete
)

type TableRef

type TableRef struct {
	Table string // Table name
	Alias string // Optional alias
}

TableRef represents a table reference

type Token

type Token struct {
	Type    TokenType
	Literal string
	Line    int
	Column  int
}

Token represents a lexical token

type TokenType

type TokenType int

TokenType represents the type of token

const (
	// Special tokens
	TokenIllegal TokenType = iota
	TokenEOF

	// Literals
	TokenIdent  // table names, column names
	TokenInt    // 123
	TokenFloat  // 123.45
	TokenString // 'string' or "string"

	// Keywords
	TokenSelect
	TokenFrom
	TokenWhere
	TokenInsert
	TokenInto
	TokenValues
	TokenUpdate
	TokenSet
	TokenDelete
	TokenOrder
	TokenBy
	TokenGroup
	TokenHaving
	TokenLimit
	TokenOffset
	TokenJoin
	TokenInner
	TokenLeft
	TokenRight
	TokenFull
	TokenOn
	TokenAs
	TokenAnd
	TokenOr
	TokenNot
	TokenIn
	TokenIs
	TokenNull
	TokenLike
	TokenBetween
	TokenDistinct
	TokenTrue
	TokenFalse

	// Operators
	TokenEqual        // =
	TokenNotEqual     // !=, <>
	TokenLess         // <
	TokenLessEqual    // <=
	TokenGreater      // >
	TokenGreaterEqual // >=

	// Delimiters
	TokenComma     // ,
	TokenSemicolon // ;
	TokenLParen    // (
	TokenRParen    // )
	TokenStar      // *
	TokenQuestion  // ?
	TokenDot       // .
)

func (TokenType) String

func (t TokenType) String() string

TokenTypeString returns string representation of token type

type UpdateStatement

type UpdateStatement struct {
	Table string
	Set   map[string]any
	Where *WhereClause
}

UpdateStatement represents an UPDATE statement

func (*UpdateStatement) GetTableName

func (s *UpdateStatement) GetTableName() string

func (*UpdateStatement) GetType

func (s *UpdateStatement) GetType() StatementType

type WhereClause

type WhereClause struct {
	Operator  string       // "AND", "OR", "NOT"
	Left      *WhereClause // Left operand for logical operators
	Right     *WhereClause // Right operand for logical operators
	Condition *Condition   // Leaf condition
}

WhereClause represents a WHERE condition

func NewWhereClause

func NewWhereClause(condition *Condition) *WhereClause

NewWhereClause creates a new WHERE clause with a single condition

func (*WhereClause) And

func (w *WhereClause) And(other *WhereClause) *WhereClause

And combines two WHERE clauses with AND

func (*WhereClause) Not

func (w *WhereClause) Not() *WhereClause

Not negates a WHERE clause

func (*WhereClause) Or

func (w *WhereClause) Or(other *WhereClause) *WhereClause

Or combines two WHERE clauses with OR

Jump to

Keyboard shortcuts

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