sql

package
v2.13.3 Latest Latest
Warning

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

Go to latest
Published: Feb 10, 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

type AbortMergeStatement struct{}

func (AbortMergeStatement) Type

type AddRemoteStatement

type AddRemoteStatement struct {
	Name string
	URL  string
}

func (AddRemoteStatement) Type

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

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

type CheckoutStatement struct {
	Branch string
}

func (CheckoutStatement) Type

type CommitMergeStatement

type CommitMergeStatement struct{}

func (CommitMergeStatement) Type

type CommitStatement

type CommitStatement struct{}

func (CommitStatement) Type

func (s CommitStatement) Type() StatementType

type CopyStatement

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 ",")
	// S3 configuration (optional)
	S3AccessKey string
	S3SecretKey string
	S3Region    string
}

CopyStatement for bulk data import/export

func (CopyStatement) Type

func (s CopyStatement) Type() StatementType

type CreateBranchStatement

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

CreateBranchStatement represents a CREATE BRANCH statement.

func (CreateBranchStatement) Type

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 CreateShareStatement

type CreateShareStatement struct {
	Name string
	URL  string
	Auth *AuthConfig
}

CreateShareStatement represents a CREATE SHARE statement.

func (CreateShareStatement) Type

type CreateTableStatement

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

func (CreateTableStatement) Type

type CreateViewStatement

type CreateViewStatement struct {
	Database     string
	ViewName     string
	SelectQuery  string // Raw SQL for the view definition
	Materialized bool
}

CreateViewStatement represents a CREATE VIEW statement.

func (CreateViewStatement) 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
	IfExists bool
}

func (DropDatabaseStatement) Type

type DropIndexStatement

type DropIndexStatement struct {
	Name     string
	Database string
	Table    string
}

func (DropIndexStatement) Type

type DropRemoteStatement

type DropRemoteStatement struct {
	Name string
}

func (DropRemoteStatement) Type

type DropShareStatement

type DropShareStatement struct {
	Name string
}

func (DropShareStatement) Type

type DropTableStatement

type DropTableStatement struct {
	Database string
	Table    string
	IfExists bool
}

func (DropTableStatement) Type

type DropViewStatement

type DropViewStatement struct {
	Database string
	ViewName string
	IfExists bool
}

func (DropViewStatement) Type

type FetchStatement

type FetchStatement struct {
	Remote string
	Auth   *AuthConfig
}

func (FetchStatement) Type

func (s FetchStatement) Type() StatementType

type FunctionExpr

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
	Share      string // optional: for 3-level naming (share.database.table)
	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

type MergeStatement struct {
	SourceBranch     string
	ManualResolution bool
}

func (MergeStatement) Type

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

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

func (PullStatement) Type

func (s PullStatement) Type() StatementType

type PushStatement

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

func (PushStatement) Type

func (s PushStatement) Type() StatementType

type RefreshViewStatement

type RefreshViewStatement struct {
	Database string
	ViewName string
}

func (RefreshViewStatement) Type

type ResolveConflictStatement

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

func (ResolveConflictStatement) Type

type RollbackStatement

type RollbackStatement struct{}

func (RollbackStatement) Type

type SelectStatement

type SelectStatement struct {
	Share      string // For 3-level naming: share.database.table
	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
	AsOf       string // Transaction ID for time-travel queries
}

func (SelectStatement) Type

func (s SelectStatement) Type() StatementType

type SetClause

type SetClause struct {
	Column string
	Value  string
}

type ShowBranchesStatement

type ShowBranchesStatement struct{}

func (ShowBranchesStatement) Type

type ShowDatabasesStatement

type ShowDatabasesStatement struct {
}

func (ShowDatabasesStatement) Type

type ShowIndexesStatement

type ShowIndexesStatement struct {
	Database string
	Table    string
}

func (ShowIndexesStatement) Type

type ShowMergeConflictsStatement

type ShowMergeConflictsStatement struct{}

func (ShowMergeConflictsStatement) Type

type ShowRemotesStatement

type ShowRemotesStatement struct{}

func (ShowRemotesStatement) Type

type ShowSharesStatement

type ShowSharesStatement struct{}

func (ShowSharesStatement) Type

type ShowTablesStatement

type ShowTablesStatement struct {
	Database string
}

func (ShowTablesStatement) Type

type ShowTransactionsStatement

type ShowTransactionsStatement struct {
	Limit int
}

func (ShowTransactionsStatement) Type

type ShowViewsStatement

type ShowViewsStatement struct {
	Database string
}

func (ShowViewsStatement) Type

type Statement

type Statement interface {
	Type() StatementType
}

func ParseAbortMerge

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

ParseAbortMerge parses ABORT MERGE statements

func ParseAddRemote

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

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

ParseCheckout parses CHECKOUT statements Syntax: CHECKOUT branch_name

func ParseCopy

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

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 ParseCreateShare

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

ParseCreateShare parses CREATE SHARE <name> FROM '<url>' [WITH TOKEN '<token>']

func ParseCreateTable

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

func ParseCreateView

func ParseCreateView(parser *Parser, materialized bool) (Statement, error)

ParseCreateView parses CREATE [MATERIALIZED] VIEW database.name AS SELECT ...

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

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

ParseDropRemote parses DROP REMOTE <name> statements

func ParseDropShare

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

ParseDropShare parses DROP SHARE <name>

func ParseDropTable

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

func ParseDropView

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

ParseDropView parses DROP [MATERIALIZED] VIEW [IF EXISTS] database.name

func ParseFetch

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

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

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

func ParsePull

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

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

func ParsePush

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

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

func ParseRefreshView

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

ParseRefreshView parses REFRESH VIEW database.name

func ParseResolveConflict

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 ParseSyncShare

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

ParseSyncShare parses SYNC SHARE <name> [WITH TOKEN '<token>']

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
	ShowTransactionsStatementType
	ShowMergeConflictsStatementType
	ResolveConflictStatementType
	CommitMergeStatementType
	AbortMergeStatementType
	AddRemoteStatementType
	ShowRemotesStatementType
	DropRemoteStatementType
	PushStatementType
	PullStatementType
	FetchStatementType
	CopyStatementType
	CreateShareStatementType
	SyncShareStatementType
	DropShareStatementType
	ShowSharesStatementType
	CreateViewStatementType
	DropViewStatementType
	ShowViewsStatementType
	RefreshViewStatementType
)

type SyncShareStatement

type SyncShareStatement struct {
	Name string
	Auth *AuthConfig
}

func (SyncShareStatement) Type

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
	AwsKey
	AwsSecret
	AwsRegion
	Share
	Shares
	Transactions
	Sync
	View
	Views
	Materialized
	Refresh
	If
	Exists
	Of
	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