uniquedialect

package module
v0.0.2 Latest Latest
Warning

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

Go to latest
Published: Apr 7, 2026 License: MIT Imports: 42 Imported by: 0

README

UniqueDialect

Write SQL once, run on any database

Documentation

Overview

Package uniquedialect provides a virtual SQL driver and GORM dialector that can translate a small cross-database SQL subset before delegating to a real database driver.

Index

Constants

View Source
const (
	// Scheme is the custom DSN scheme for UniqueDialect.
	Scheme = "uniquedialect"
	// DefaultSQLDriverName is the registered database/sql driver name.
	DefaultSQLDriverName = "uniquedialect"
	// DefaultCipherPrefix prefixes encrypted values for downgrade-safe reads.
	DefaultCipherPrefix = "enc:v1:"
	// DefaultTranslateFailureLogPath is the default JSONL log path for translation failures.
	DefaultTranslateFailureLogPath = "uniquedialect-translate-failures.jsonl"
	// DefaultExecutionFailureLogPath is the default JSONL log path for execution failures.
	DefaultExecutionFailureLogPath = "uniquedialect-execution-failures.jsonl"
)

Variables

This section is empty.

Functions

func ErrUnsupportedDialector

func ErrUnsupportedDialector(dialect Dialect) error

ErrUnsupportedDialector reports an unsupported target for GORM execution.

func Open

func Open(dsn string) (*sql.DB, error)

Open opens a database/sql DB from a UniqueDialect DSN.

func OpenGORM

func OpenGORM(dsn string, cfg GORMConfig) (gorm.Dialector, error)

OpenGORM opens a GORM dialector from a UniqueDialect DSN.

func OpenGORMWithOptions

func OpenGORMWithOptions(opts Options, cfg GORMConfig) (gorm.Dialector, error)

OpenGORMWithOptions opens a GORM dialector from canonical options.

func OpenWithOptions

func OpenWithOptions(opts Options) (*sql.DB, error)

OpenWithOptions opens a database/sql DB from canonical options.

Types

type BootstrapArtifact

type BootstrapArtifact struct {
	Name       string
	Kind       BootstrapKind
	SQL        string
	Idempotent bool
}

BootstrapArtifact is an executable compatibility artifact.

type BootstrapKind

type BootstrapKind string

BootstrapKind identifies the artifact type.

const (
	// BootstrapKindFunction is a database function shim.
	BootstrapKindFunction BootstrapKind = "function"
	// BootstrapKindTrigger is a database trigger shim.
	BootstrapKindTrigger BootstrapKind = "trigger"
)

type BootstrapOptions

type BootstrapOptions struct {
	InputDialect  Dialect
	TargetDialect Dialect
	Enabled       bool
}

BootstrapOptions configures bootstrap planning.

type BootstrapPlanner

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

BootstrapPlanner plans compatibility artifacts for translated SQL and DDL.

func NewBootstrapPlanner

func NewBootstrapPlanner(opts BootstrapOptions) *BootstrapPlanner

NewBootstrapPlanner creates a compatibility artifact planner.

func (*BootstrapPlanner) Plan

Plan analyzes SQL/DDL and returns required bootstrap artifacts.

type ConnectionOptions

type ConnectionOptions struct {
	Host       string
	Port       int
	Username   string
	Password   string
	Database   string
	Parameters map[string]string
}

ConnectionOptions is the canonical connection model shared across targets.

type Dialect

type Dialect string

Dialect identifies a SQL dialect.

const (
	// DialectMySQL is the MySQL dialect.
	DialectMySQL Dialect = "mysql"
	// DialectPostgres is the PostgreSQL dialect.
	DialectPostgres Dialect = "postgres"
	// DialectSQLite is the SQLite dialect.
	DialectSQLite Dialect = "sqlite"
	// DialectOracle is the Oracle dialect.
	DialectOracle Dialect = "oracle"
)

type DriverName

type DriverName string

DriverName identifies an underlying database/sql driver family.

const (
	// DriverMySQL is the MySQL database/sql driver family.
	DriverMySQL DriverName = "mysql"
	// DriverPostgres is the PostgreSQL database/sql driver family.
	DriverPostgres DriverName = "postgres"
	// DriverSQLite is the SQLite database/sql driver family.
	DriverSQLite DriverName = "sqlite"
	// DriverOracle is the Oracle database/sql driver family.
	DriverOracle DriverName = "oracle"
)

type EncryptionOptions

type EncryptionOptions struct {
	Enabled  bool
	Key      string
	Provider Encryptor
}

EncryptionOptions configures field encryption for GORM.

type Encryptor

type Encryptor interface {
	Encrypt(string) (string, error)
	Decrypt(string) (string, error)
}

Encryptor encrypts and decrypts string values.

func NewDefaultEncryptor

func NewDefaultEncryptor(key string) (Encryptor, error)

NewDefaultEncryptor constructs the built-in AES-GCM encryptor.

type GORMConfig

type GORMConfig struct {
	PreferSimpleProtocol      bool
	SkipInitializeWithVersion bool
}

GORMConfig configures the GORM dialector wrapper.

type LoggingOptions

type LoggingOptions struct {
	Level                   string
	LogFailedSQL            bool
	TranslateFailureLogPath string
	ExecutionFailureLogPath string
}

LoggingOptions configures execution logging.

type Options

type Options struct {
	InputDialect  Dialect
	TargetDialect Dialect
	Driver        DriverName
	Connection    ConnectionOptions
	Translator    TranslatorOptions
	Logging       LoggingOptions
	Encryption    EncryptionOptions
}

Options configures the virtual driver.

func ParseDSN

func ParseDSN(raw string) (Options, error)

ParseDSN decodes a UniqueDialect DSN into canonical options.

func (Options) BuildDriverDSN

func (o Options) BuildDriverDSN() (string, error)

BuildDriverDSN maps the canonical connection options to the target driver DSN.

func (Options) FormatDSN

func (o Options) FormatDSN() string

FormatDSN encodes options into a custom UniqueDialect DSN.

type ParserAdaptationError

type ParserAdaptationError struct {
	SourceDialect  string
	StatementKind  string
	NativeNodeType string
	Status         string
}

ParserAdaptationError indicates the SQL was parsed successfully but is not yet adapted by the translation pipeline.

func (*ParserAdaptationError) Error

func (e *ParserAdaptationError) Error() string

type Translation

type Translation struct {
	SQL  string
	Args []any
}

Translation contains a translated SQL statement and its mapped arguments.

type Translator

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

Translator translates SQL between dialects and caches results.

func NewTranslator

func NewTranslator(opts TranslatorOptions) (*Translator, error)

NewTranslator constructs a translator for a dialect pair.

func (*Translator) Stats

func (t *Translator) Stats() TranslatorStats

Stats returns a snapshot of cache statistics.

func (*Translator) Translate

func (t *Translator) Translate(_ context.Context, sql string, args ...any) (Translation, error)

Translate rewrites SQL text and returns mapped arguments.

type TranslatorOptions

type TranslatorOptions struct {
	InputDialect  Dialect
	TargetDialect Dialect
}

TranslatorOptions configures SQL translation.

type TranslatorStats

type TranslatorStats struct {
	Hits   int
	Misses int
}

TranslatorStats exposes in-memory cache hit statistics.

Directories

Path Synopsis
internal
ir
parser/tidb/ast
Package ast is the abstract syntax tree parsed from a SQL statement by parser.
Package ast is the abstract syntax tree parsed from a SQL statement by parser.
parser/tidb/duration
Package duration provides a customized duration, which supports unit 'd', 'h' and 'm'
Package duration provides a customized duration, which supports unit 'd', 'h' and 'm'
parser/tidb/goyacc command
Goyacc is a version of yacc generating Go parsers.
Goyacc is a version of yacc generating Go parsers.

Jump to

Keyboard shortcuts

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