dialect

package
v0.0.0-20260608 Latest Latest
Warning

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

Go to latest
Published: Jun 8, 2026 License: MIT Imports: 2 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ValidOperators = map[string]bool{
	"=": true, "==": true, "!=": true, "<>": true,
	">": true, "<": true, ">=": true, "<=": true,
	"LIKE": true, "NOT LIKE": true, "ILIKE": true,
	"IN": true, "NOT IN": true,
	"IS": true, "IS NOT": true,
	"BETWEEN": true, "NOT BETWEEN": true,
}

ValidOperators is the whitelist of allowed SQL comparison operators.

Functions

func ValidateOperator

func ValidateOperator(op string) (string, error)

ValidateOperator checks if an operator is in the whitelist. Returns the operator if valid, or an error message if not.

Types

type AggregateClause

type AggregateClause struct {
	Function string // COUNT, MAX, MIN, SUM, AVG
	Column   string // usually * or a column name
}

AggregateClause represents an aggregate function to apply instead of selecting columns.

type Dialect

type Dialect interface {
	// QuoteIdentifier quotes a table or column name (e.g. `col` for MySQL, "col" for Postgres)
	QuoteIdentifier(name string) string

	// Placeholder returns the bind parameter placeholder for a given index (1-based).
	// e.g. $1 for Postgres, ? for MySQL/SQLite
	Placeholder(index int) string

	// CompileSelect compiles a SELECT statement
	CompileSelect(query SelectQuery) (string, []any)

	// CompileInsert compiles an INSERT statement
	CompileInsert(table string, columns []string, values [][]any) (string, []any)

	// CompileUpdate compiles an UPDATE statement
	CompileUpdate(table string, values map[string]any, wheres []WhereClause) (string, []any)

	// CompileDelete compiles a DELETE statement
	CompileDelete(table string, wheres []WhereClause) (string, []any)

	// CompileUpsert compiles an INSERT ... ON CONFLICT (or equivalent) statement.
	CompileUpsert(table string, columns []string, values [][]any, conflictCols []string, updateCols []string) (string, []any)

	// AutoIncrementSQL returns the SQL fragment for an auto-increment primary key column.
	AutoIncrementSQL() string
}

QuoteIdentifier quotes a table or column name (e.g. `col` for MySQL, "col" for Postgres)

type JoinClause

type JoinClause struct {
	Type     string // INNER, LEFT, RIGHT, CROSS
	Table    string
	First    string
	Operator string
	Second   string
}

JoinClause represents a table JOIN condition.

type MySQLDialect

type MySQLDialect struct{}

MySQLDialect implements the Dialect interface for MySQL.

func (*MySQLDialect) AutoIncrementSQL

func (d *MySQLDialect) AutoIncrementSQL() string

func (*MySQLDialect) CompileDelete

func (d *MySQLDialect) CompileDelete(table string, wheres []WhereClause) (string, []any)

func (*MySQLDialect) CompileInsert

func (d *MySQLDialect) CompileInsert(table string, columns []string, values [][]any) (string, []any)

func (*MySQLDialect) CompileSelect

func (d *MySQLDialect) CompileSelect(query SelectQuery) (string, []any)

func (*MySQLDialect) CompileUpdate

func (d *MySQLDialect) CompileUpdate(table string, values map[string]any, wheres []WhereClause) (string, []any)

func (*MySQLDialect) CompileUpsert

func (d *MySQLDialect) CompileUpsert(table string, columns []string, values [][]any, conflictCols []string, updateCols []string) (string, []any)

func (*MySQLDialect) Placeholder

func (d *MySQLDialect) Placeholder(index int) string

func (*MySQLDialect) QuoteIdentifier

func (d *MySQLDialect) QuoteIdentifier(name string) string

type OrderByClause

type OrderByClause struct {
	Column    string
	Direction string
}

OrderByClause represents an ORDER BY condition.

type PostgresDialect

type PostgresDialect struct{}

PostgresDialect implements the Dialect interface for PostgreSQL.

func (*PostgresDialect) AutoIncrementSQL

func (d *PostgresDialect) AutoIncrementSQL() string

func (*PostgresDialect) CompileDelete

func (d *PostgresDialect) CompileDelete(table string, wheres []WhereClause) (string, []any)

func (*PostgresDialect) CompileInsert

func (d *PostgresDialect) CompileInsert(table string, columns []string, values [][]any) (string, []any)

func (*PostgresDialect) CompileSelect

func (d *PostgresDialect) CompileSelect(query SelectQuery) (string, []any)

func (*PostgresDialect) CompileUpdate

func (d *PostgresDialect) CompileUpdate(table string, values map[string]any, wheres []WhereClause) (string, []any)

func (*PostgresDialect) CompileUpsert

func (d *PostgresDialect) CompileUpsert(table string, columns []string, values [][]any, conflictCols []string, updateCols []string) (string, []any)

func (*PostgresDialect) Placeholder

func (d *PostgresDialect) Placeholder(index int) string

func (*PostgresDialect) QuoteIdentifier

func (d *PostgresDialect) QuoteIdentifier(name string) string

type SQLiteDialect

type SQLiteDialect struct{}

SQLiteDialect implements the Dialect interface for SQLite.

func (*SQLiteDialect) AutoIncrementSQL

func (d *SQLiteDialect) AutoIncrementSQL() string

func (*SQLiteDialect) CompileDelete

func (d *SQLiteDialect) CompileDelete(table string, wheres []WhereClause) (string, []any)

func (*SQLiteDialect) CompileInsert

func (d *SQLiteDialect) CompileInsert(table string, columns []string, values [][]any) (string, []any)

func (*SQLiteDialect) CompileSelect

func (d *SQLiteDialect) CompileSelect(query SelectQuery) (string, []any)

func (*SQLiteDialect) CompileUpdate

func (d *SQLiteDialect) CompileUpdate(table string, values map[string]any, wheres []WhereClause) (string, []any)

func (*SQLiteDialect) CompileUpsert

func (d *SQLiteDialect) CompileUpsert(table string, columns []string, values [][]any, conflictCols []string, updateCols []string) (string, []any)

func (*SQLiteDialect) Placeholder

func (d *SQLiteDialect) Placeholder(index int) string

func (*SQLiteDialect) QuoteIdentifier

func (d *SQLiteDialect) QuoteIdentifier(name string) string

type SelectQuery

type SelectQuery struct {
	Table      string
	Columns    []string
	RawColumns []string // Raw SQL expressions (e.g. "COUNT(*) as total") — not quoted
	Wheres     []WhereClause
	OrderBys   []OrderByClause
	Joins      []JoinClause
	Aggregate  *AggregateClause
	Limit      *int
	Offset     *int
	GroupBys   []string
	Havings    []WhereClause
	Lock       string // e.g. "FOR UPDATE", "FOR SHARE" for pessimistic locking
	Distinct   bool   // SELECT DISTINCT
}

SelectQuery represents the components of a SELECT query for compilation.

type WhereClause

type WhereClause struct {
	Type     string // Basic, In, Null, NotNull, Between, Subquery, Raw
	Column   string
	Operator string
	Value    any
	Values   []any  // For IN, BETWEEN
	Boolean  string // AND / OR
	RawSQL   string // For Raw queries
	RawArgs  []any
}

WhereClause represents a WHERE condition.

Jump to

Keyboard shortcuts

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