sql

package
v1.7.0 Latest Latest
Warning

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

Go to latest
Published: Jan 25, 2026 License: Apache-2.0 Imports: 4 Imported by: 0

Documentation

Overview

Package sql provides SQL lexing and parsing for CommitDB.

The package includes a lexer that tokenizes SQL strings and a parser that produces abstract syntax trees for SQL statements.

Lexer Usage

lexer := sql.NewLexer("SELECT * FROM users")
for {
    token := lexer.NextToken()
    if token.Type == sql.EOF {
        break
    }
    fmt.Printf("Token: %s = %s\n", token.Type, token.Value)
}

Parser Usage

parser := sql.NewParser("SELECT * FROM mydb.users WHERE id = 1")
statement, err := parser.Parse()
if err != nil {
    log.Fatal(err)
}

Supported Statements

The parser supports the following statement types:

  • SelectStatement
  • InsertStatement
  • UpdateStatement
  • DeleteStatement
  • CreateTableStatement
  • DropTableStatement
  • CreateDatabaseStatement
  • DropDatabaseStatement
  • CreateIndexStatement
  • DropIndexStatement
  • AlterTableStatement
  • BeginStatement, CommitStatement, RollbackStatement
  • DescribeStatement
  • ShowDatabasesStatement, ShowTablesStatement, ShowIndexesStatement

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type AbortMergeStatement added in v1.4.0

type AbortMergeStatement struct{}

func (AbortMergeStatement) Type added in v1.4.0

type AddRemoteStatement added in v1.5.0

type AddRemoteStatement struct {
	Name string
	URL  string
}

func (AddRemoteStatement) Type added in v1.5.0

type AggregateExpr

type AggregateExpr struct {
	Function string // COUNT, SUM, AVG, MIN, MAX
	Column   string
	Alias    string
}

type AlterTableStatement

type AlterTableStatement struct {
	Database      string
	Table         string
	Action        string // ADD, DROP, MODIFY, RENAME
	ColumnName    string
	NewColumnName string // for RENAME
	ColumnType    string
}

func (AlterTableStatement) Type

type AuthConfig added in v1.5.0

type AuthConfig struct {
	Token      string // Token-based authentication
	SSHKeyPath string // Path to SSH private key
	Passphrase string // Passphrase for SSH key
	Username   string // Username for basic auth
	Password   string // Password for basic auth
}

AuthConfig represents authentication configuration for remote operations

type BeginStatement

type BeginStatement struct{}

func (BeginStatement) Type

func (s BeginStatement) Type() StatementType

type CheckoutStatement added in v1.3.0

type CheckoutStatement struct {
	Branch string
}

func (CheckoutStatement) Type added in v1.3.0

type CommitMergeStatement added in v1.4.0

type CommitMergeStatement struct{}

func (CommitMergeStatement) Type added in v1.4.0

type CommitStatement

type CommitStatement struct{}

func (CommitStatement) Type

func (s CommitStatement) Type() StatementType

type CopyStatement added in v1.7.0

type CopyStatement struct {
	Direction string // "INTO_TABLE" or "INTO_FILE"
	Database  string
	Table     string
	FilePath  string
	Header    bool   // Include/expect header row
	Delimiter string // Column delimiter (default ",")
}

CopyStatement for bulk data import/export

func (CopyStatement) Type added in v1.7.0

func (s CopyStatement) Type() StatementType

type CreateBranchStatement added in v1.3.0

type CreateBranchStatement struct {
	Name      string
	FromTxnId string // Optional: create from specific transaction
}

Branch statements

func (CreateBranchStatement) Type added in v1.3.0

type CreateDatabaseStatement

type CreateDatabaseStatement struct {
	Database string
}

func (CreateDatabaseStatement) Type

type CreateIndexStatement

type CreateIndexStatement struct {
	Name     string
	Database string
	Table    string
	Column   string
	Unique   bool
}

func (CreateIndexStatement) Type

type CreateTableStatement

type CreateTableStatement struct {
	Database string
	Table    string
	Columns  []core.Column
}

func (CreateTableStatement) Type

type DeleteStatement

type DeleteStatement struct {
	Database string
	Table    string
	Where    WhereClause
}

func (DeleteStatement) Type

func (s DeleteStatement) Type() StatementType

type DescribeStatement

type DescribeStatement struct {
	Database string
	Table    string
}

func (DescribeStatement) Type

type DropDatabaseStatement

type DropDatabaseStatement struct {
	Database string
}

func (DropDatabaseStatement) Type

type DropIndexStatement

type DropIndexStatement struct {
	Name     string
	Database string
	Table    string
}

func (DropIndexStatement) Type

type DropRemoteStatement added in v1.5.0

type DropRemoteStatement struct {
	Name string
}

func (DropRemoteStatement) Type added in v1.5.0

type DropTableStatement

type DropTableStatement struct {
	Database string
	Table    string
}

func (DropTableStatement) Type

type FetchStatement added in v1.5.0

type FetchStatement struct {
	Remote string
	Auth   *AuthConfig
}

func (FetchStatement) Type added in v1.5.0

func (s FetchStatement) Type() StatementType

type FunctionExpr added in v1.7.0

type FunctionExpr struct {
	Function string   // UPPER, LOWER, CONCAT, SUBSTRING, TRIM, LENGTH, REPLACE
	Args     []string // Arguments (column names or literals)
	Alias    string   // Optional AS alias
}

FunctionExpr represents a function call like UPPER(column), CONCAT(a, b)

type InsertStatement

type InsertStatement struct {
	Database  string
	Table     string
	Columns   []string
	ValueRows [][]string // Multiple rows for bulk insert: VALUES (v1), (v2), ...
}

func (InsertStatement) Type

func (s InsertStatement) Type() StatementType

type JoinClause

type JoinClause struct {
	Type       string // INNER, LEFT, RIGHT
	Database   string
	Table      string
	TableAlias string
	LeftCol    string // left table column
	RightCol   string // right table column
}

type Lexer

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

func NewLexer

func NewLexer(sql string) *Lexer

func (*Lexer) NextToken

func (lexer *Lexer) NextToken() Token

func (*Lexer) PeekToken

func (lexer *Lexer) PeekToken() Token

type LogicalOperator

type LogicalOperator int
const (
	LogicalAnd LogicalOperator = iota
	LogicalOr
)

type MergeStatement added in v1.3.0

type MergeStatement struct {
	SourceBranch     string
	ManualResolution bool
}

func (MergeStatement) Type added in v1.3.0

func (s MergeStatement) Type() StatementType

type OrderByClause

type OrderByClause struct {
	Column     string
	Descending bool
}

type Parser

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

func NewParser

func NewParser(sql string) *Parser

func (*Parser) Parse

func (parser *Parser) Parse() (Statement, error)

type PullStatement added in v1.5.0

type PullStatement struct {
	Remote string
	Branch string
	Auth   *AuthConfig
}

func (PullStatement) Type added in v1.5.0

func (s PullStatement) Type() StatementType

type PushStatement added in v1.5.0

type PushStatement struct {
	Remote string
	Branch string
	Auth   *AuthConfig
}

func (PushStatement) Type added in v1.5.0

func (s PushStatement) Type() StatementType

type ResolveConflictStatement added in v1.4.0

type ResolveConflictStatement struct {
	Database   string
	Table      string
	Key        string
	Resolution string // "HEAD", "SOURCE", or a literal value
}

func (ResolveConflictStatement) Type added in v1.4.0

type RollbackStatement

type RollbackStatement struct{}

func (RollbackStatement) Type

type SelectStatement

type SelectStatement struct {
	Database   string
	Table      string
	TableAlias string
	Columns    []string
	Aggregates []AggregateExpr
	Functions  []FunctionExpr // String functions like UPPER, LOWER, etc.
	Joins      []JoinClause
	Distinct   bool
	CountAll   bool
	Where      WhereClause
	GroupBy    []string
	Having     WhereClause
	OrderBy    []OrderByClause
	Limit      int
	Offset     int
}

func (SelectStatement) Type

func (s SelectStatement) Type() StatementType

type SetClause

type SetClause struct {
	Column string
	Value  string
}

type ShowBranchesStatement added in v1.3.0

type ShowBranchesStatement struct{}

func (ShowBranchesStatement) Type added in v1.3.0

type ShowDatabasesStatement

type ShowDatabasesStatement struct {
}

func (ShowDatabasesStatement) Type

type ShowIndexesStatement

type ShowIndexesStatement struct {
	Database string
	Table    string
}

func (ShowIndexesStatement) Type

type ShowMergeConflictsStatement added in v1.4.0

type ShowMergeConflictsStatement struct{}

func (ShowMergeConflictsStatement) Type added in v1.4.0

type ShowRemotesStatement added in v1.5.0

type ShowRemotesStatement struct{}

func (ShowRemotesStatement) Type added in v1.5.0

type ShowTablesStatement

type ShowTablesStatement struct {
	Database string
}

func (ShowTablesStatement) Type

type Statement

type Statement interface {
	Type() StatementType
}

func ParseAbortMerge added in v1.4.0

func ParseAbortMerge(parser *Parser) (Statement, error)

ParseAbortMerge parses ABORT MERGE statements

func ParseAddRemote added in v1.5.0

func ParseAddRemote(parser *Parser) (Statement, error)

ParseAddRemote parses CREATE REMOTE <name> <url> statements

func ParseAlter

func ParseAlter(parser *Parser) (Statement, error)

ParseAlter parses ALTER TABLE statements

func ParseCheckout added in v1.3.0

func ParseCheckout(parser *Parser) (Statement, error)

ParseCheckout parses CHECKOUT statements Syntax: CHECKOUT branch_name

func ParseCopy added in v1.7.0

func ParseCopy(parser *Parser) (Statement, error)

ParseCopy parses COPY INTO commands for bulk data import/export COPY INTO table FROM 'file.csv' [WITH (HEADER = TRUE, DELIMITER = ',')] COPY INTO 'file.csv' FROM table [WITH (HEADER = TRUE)]

func ParseCreate

func ParseCreate(parser *Parser) (Statement, error)

func ParseCreateBranch added in v1.3.0

func ParseCreateBranch(parser *Parser) (Statement, error)

ParseCreateBranch parses CREATE BRANCH statements Syntax: CREATE BRANCH name [FROM 'transaction_id']

func ParseCreateDatabase

func ParseCreateDatabase(parser *Parser) (Statement, error)

func ParseCreateIndex

func ParseCreateIndex(parser *Parser, unique bool) (Statement, error)

ParseCreateIndex parses: CREATE [UNIQUE] INDEX name ON database.table(column)

func ParseCreateTable

func ParseCreateTable(parser *Parser) (Statement, error)

func ParseDelete

func ParseDelete(parser *Parser) (Statement, error)

func ParseDescribe

func ParseDescribe(parser *Parser) (Statement, error)

ParseDescribe parses DESCRIBE table statements

func ParseDrop

func ParseDrop(parser *Parser) (Statement, error)

func ParseDropDatabase

func ParseDropDatabase(parser *Parser) (Statement, error)

func ParseDropIndex

func ParseDropIndex(parser *Parser) (Statement, error)

ParseDropIndex parses: DROP INDEX name ON database.table

func ParseDropRemote added in v1.5.0

func ParseDropRemote(parser *Parser) (Statement, error)

ParseDropRemote parses DROP REMOTE <name> statements

func ParseDropTable

func ParseDropTable(parser *Parser) (Statement, error)

func ParseFetch added in v1.5.0

func ParseFetch(parser *Parser) (Statement, error)

ParseFetch parses FETCH [FROM <remote>] [WITH TOKEN 'xxx' | WITH SSH KEY 'path' [PASSPHRASE 'xxx']]

func ParseInsert

func ParseInsert(parser *Parser) (Statement, error)

func ParseMerge added in v1.3.0

func ParseMerge(parser *Parser) (Statement, error)

ParseMerge parses MERGE statements Syntax: MERGE branch_name [WITH MANUAL RESOLUTION]

func ParsePull added in v1.5.0

func ParsePull(parser *Parser) (Statement, error)

ParsePull parses PULL [FROM <remote>] [BRANCH <branch>] [WITH TOKEN 'xxx' | WITH SSH KEY 'path' [PASSPHRASE 'xxx']]

func ParsePush added in v1.5.0

func ParsePush(parser *Parser) (Statement, error)

ParsePush parses PUSH [TO <remote>] [BRANCH <branch>] [WITH TOKEN 'xxx' | WITH SSH KEY 'path' [PASSPHRASE 'xxx']]

func ParseResolveConflict added in v1.4.0

func ParseResolveConflict(parser *Parser) (Statement, error)

ParseResolveConflict parses RESOLVE CONFLICT statements Syntax: RESOLVE CONFLICT db.table.key USING HEAD|SOURCE|'value'

func ParseSelect

func ParseSelect(parser *Parser) (Statement, error)

func ParseShow

func ParseShow(parser *Parser) (Statement, error)

func ParseUpdate

func ParseUpdate(parser *Parser) (Statement, error)

type StatementType

type StatementType int
const (
	SelectStatementType StatementType = iota
	InsertStatementType
	UpdateStatementType
	DeleteStatementType
	CreateTableStatementType
	DropTableStatementType
	CreateDatabaseStatementType
	DropDatabaseStatementType
	CreateIndexStatementType
	DropIndexStatementType
	AlterTableStatementType
	BeginStatementType
	CommitStatementType
	RollbackStatementType
	DescribeStatementType
	ShowDatabasesStatementType
	ShowTablesStatementType
	ShowIndexesStatementType
	CreateBranchStatementType
	CheckoutStatementType
	MergeStatementType
	ShowBranchesStatementType
	ShowMergeConflictsStatementType
	ResolveConflictStatementType
	CommitMergeStatementType
	AbortMergeStatementType
	AddRemoteStatementType
	ShowRemotesStatementType
	DropRemoteStatementType
	PushStatementType
	PullStatementType
	FetchStatementType
	CopyStatementType
)

type Token

type Token struct {
	Type  TokenType
	Value string
}

func (Token) String

func (token Token) String() string

type TokenType

type TokenType int
const (
	Identifier TokenType = iota
	DatabaseIdentifier
	DatabasesIdentifier
	TableIdentifier
	TablesIdentifier
	IndexIdentifier
	Show
	In
	On
	Wildcard
	String
	Int
	Float
	Bool
	PrimaryKey
	Unique
	Comma
	Quote
	ParenOpen
	ParenClose
	Equals
	NotEquals
	LessThan
	GreaterThan
	LessThanOrEqual
	GreaterThanOrEqual
	And
	Or
	Not
	Is
	Null
	Like
	True
	False
	Select
	From
	Where
	Limit
	Offset
	Order
	By
	Asc
	Desc
	Count
	Sum
	Avg
	Min
	Max
	Distinct
	Group
	Having
	Create
	Drop
	Alter
	Add
	Modify
	Column
	Insert
	Update
	Delete
	Set
	Into
	Values
	Begin
	Commit
	Rollback
	Join
	Inner
	Left
	Right
	Outer
	Describe
	As
	Branch
	Branches
	Checkout
	Merge
	Manual
	Resolution
	With
	Resolve
	Conflict
	Conflicts
	Using
	Abort
	Head
	Source
	Remote
	Remotes
	Push
	Pull
	Fetch
	TokenKeyword
	Key
	Passphrase
	Ssh
	To
	Rename
	Upper
	Lower
	Concat
	Substring
	Trim
	Length
	Replace
	LeftFunc
	RightFunc
	Now
	DateAdd
	DateSub
	DateDiff
	DateFunc
	Year
	Month
	Day
	Hour
	Minute
	Second
	DateFormat
	JsonExtract
	JsonSet
	JsonRemove
	JsonContains
	JsonKeys
	JsonLength
	JsonType
	Copy
	Header
	Delimiter
	EOF
	Unknown
)

type UpdateStatement

type UpdateStatement struct {
	Database string
	Table    string
	Updates  []SetClause
	Where    WhereClause
}

func (UpdateStatement) Type

func (s UpdateStatement) Type() StatementType

type WhereClause

type WhereClause struct {
	Conditions []WhereCondition
	LogicalOps []LogicalOperator // AND/OR between conditions
}

func ParseWhere

func ParseWhere(parser *Parser) (WhereClause, error)

type WhereCondition

type WhereCondition struct {
	Left     string
	Operator WhereOperator
	Right    string
	InValues []string // for IN operator
	Negated  bool     // for NOT
}

type WhereOperator

type WhereOperator int
const (
	EqualsOperator WhereOperator = iota
	NotEqualsOperator
	LessThanOperator
	GreaterThanOperator
	LessThanOrEqualOperator
	GreaterThanOrEqualOperator
	LikeOperator
	IsNullOperator
	IsNotNullOperator
	InOperator
)

Jump to

Keyboard shortcuts

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