Documentation
¶
Overview ¶
Package dialect is how Baleia writes SQL for each engine kind: quoting, qualification, literals, statement splitting, previews, metadata queries and the SQL-generation primitives of the semantic layer (#14, #33, #91).
Every difference between engines is declared here; no other package spells engine SQL. Dialects describe what SQL looks like; they never authorize anything (that is internal/policy).
Index ¶
- Constants
- Variables
- func Callable(dialectName, name string) bool
- func ParseStatement(d Dialect, sql string) (expressions.Expression, error)
- func SqlglotDialect(d Dialect) (dialects.DialectType, error)
- type AggFunc
- type Dialect
- type LogicalType
- type MetadataQueries
- type Registry
- type Span
- type TimeGrain
Constants ¶
const ( Trino = "trino" PostgreSQL = "postgresql" MySQL = "mysql" MariaDB = "mariadb" TSQL = "tsql" Oracle = "oracle" SQLite = "sqlite" ClickHouse = "clickhouse" FlightSQL = "flightsql-generic" )
Names of the v1 dialects.
Variables ¶
var ( // ErrUnsupported means the dialect cannot express the request; callers // report it rather than falling back to another dialect's SQL. ErrUnsupported = errors.New("not supported by this dialect") // ErrInvalid means the input (identifier, type, value) is malformed. ErrInvalid = errors.New("invalid input") )
var ErrDelimiter = errors.New("DELIMITER is a mysql client command, not SQL; end each statement with ';' instead")
ErrDelimiter is returned for the MySQL client's DELIMITER command.
var Grains = []TimeGrain{GrainSecond, GrainMinute, GrainHour, GrainDay, GrainWeek, GrainMonth, GrainQuarter, GrainYear}
Grains lists every TimeGrain, finest first.
Functions ¶
func Callable ¶
Callable reports whether a function in the dialect takes parentheses; the special forms (current_date) do not. MySQL/MariaDB expose USER(), SESSION_USER() and SYSTEM_USER() as callable functions.
func ParseStatement ¶
func ParseStatement(d Dialect, sql string) (expressions.Expression, error)
ParseStatement parses one statement with a registered Baleia dialect. It is intentionally a package function so Dialect's SQL-generation interface does not force test doubles or external dialect implementations to depend on the parser.
func SqlglotDialect ¶
func SqlglotDialect(d Dialect) (dialects.DialectType, error)
SqlglotDialect returns the sqlglot dialect name for d so external packages (reference extraction, classification) can drive the fork's optimizer and scope passes without depending on the unexported spec type.
Types ¶
type Dialect ¶
type Dialect interface {
Name() string
// QuoteIdent returns s as a delimited identifier. Empty names, NUL bytes
// and names over the engine's identifier limit are errors; nothing is
// silently altered.
QuoteIdent(s string) (string, error)
// QualifiedName quotes each part and joins them with ".".
QualifiedName(parts ...string) (string, error)
// Qualify names an object following the engine's namespace rules:
// catalogs are dropped where one connection sees one catalog, and a
// schema path deeper than the engine supports is an error.
Qualify(catalog string, schema []string, object string) (string, error)
// QuoteString returns s as a string literal.
QuoteString(s string) string
// Literal renders v as a literal of type t (nil is NULL).
Literal(v any, t LogicalType) (string, error)
// SplitStatements splits a script into statements.
SplitStatements(sql string) ([]string, error)
// Statements is SplitStatements with byte offsets into sql.
Statements(sql string) ([]Span, error)
// PreviewSQL selects the first limit rows of an already qualified object.
PreviewSQL(object string, limit int) string
// Limit caps the rows of a query by wrapping it.
Limit(sql string, n int) string
// ExplainSQL returns the statement that explains sql, or false when the
// engine has no single-statement EXPLAIN.
ExplainSQL(sql string) (string, bool)
// Placeholder is the n-th (1-based) bind parameter.
Placeholder(n int) string
MetadataQueries() MetadataQueries
// FunctionQuery returns the engine's function-catalog SQL (#788) without
// executing it. Names are in the first result column; additional columns
// and the representation of overloads vary by dialect. An empty string
// means no query is configured.
FunctionQuery() string
// SQL-generation primitives. Expressions are SQL the caller built from
// quoted identifiers; they are not re-checked.
Aggregate(fn AggFunc, expr string, distinct bool) (string, error)
TimeTrunc(expr string, grain TimeGrain) (string, error)
Cast(expr string, t LogicalType) (string, error)
// SafeDivide divides as floating point and yields NULL for a zero
// denominator.
SafeDivide(num, den string) string
// Editor support.
FormatterLanguage() string // sql-formatter language id
CodeMirrorBase() string // @maltzsama/lang-sql base dialect
}
Dialect is how one engine kind spells SQL.
type LogicalType ¶
type LogicalType string
LogicalType is an engine-independent column type: boolean, int64, decimal(p,s), float64, string, date, timestamp, timestamptz, binary, json.
const ( TypeBoolean LogicalType = "boolean" TypeInt64 LogicalType = "int64" TypeFloat64 LogicalType = "float64" TypeString LogicalType = "string" TypeDate LogicalType = "date" TypeTimestamp LogicalType = "timestamp" TypeTimestampTZ LogicalType = "timestamptz" TypeBinary LogicalType = "binary" TypeJSON LogicalType = "json" )
func Decimal ¶
func Decimal(precision, scale int) LogicalType
Decimal returns the logical type decimal(p,s).
type MetadataQueries ¶
type MetadataQueries struct {
Schemas string // schema_name
Tables string // table_name, table_type ('TABLE' or 'VIEW')
Columns string // column_name, data_type, is_nullable ('YES'/'NO'), ordinal_position
// Catalog reads every table of the session's catalog with its columns in
// one query, for the metadata sync through an engine (#15): table_schema,
// table_name, table_type ('TABLE' or 'VIEW'), column_name, data_type,
// is_nullable ('YES'/'NO'), ordinal_position, ordered by schema, table
// and position; a table without columns has NULL column fields. Empty
// when the engine has no such query.
Catalog string
}
MetadataQueries are the catalog queries of an engine, in the dialect's own bind-parameter syntax (Placeholder). Schemas takes no parameter; Tables takes the schema; Columns takes the schema and the table. Each returns the documented columns in order.
type Registry ¶
type Registry struct {
// contains filtered or unexported fields
}
Registry holds every dialect by name (#364 follow-up): built once by NewRegistry and only read afterwards, so it is safe for concurrent use.
func NewRegistry ¶
func NewRegistry() *Registry
NewRegistry builds the dialects of every engine Baleia supports.
func (*Registry) ForEngineKind ¶
ForEngineKind returns the dialect of a query-engine kind.
type Span ¶
Span is one statement of a script: Text is the statement without its terminator or surrounding whitespace; Start and End are byte offsets of Text in the script.