dalgo2sql

package module
v0.19.2 Latest Latest
Warning

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

Go to latest
Published: Sep 23, 2026 License: MIT Imports: 26 Imported by: 2

README

dalgo2sql

SQL adapter for DALgo - a Database Abstraction Layer in Go.

Our approach to development

We build with our own tooling:

  • SpecScore — specify requirements as SpecScore.md artifacts
  • SpecStudio — author & manage specs across their lifecycle
  • inGitDB — store structured data in Git where applicable
  • DALgo — data access layer for Go
  • cover100.dev — drive toward 100% test coverage
  • DataTug — query & explore data

Status

Lint, Vet, Build, Test Go Report Card GoDoc

Usage

go get github.com/dal-go/dalgo2sql

SQLite structured aggregation

With DbOptions.StructuredQueryDialect set to sqlite, structured DALgo queries render GROUP BY, COUNT, SUM, AVG, MIN, MAX, DISTINCT aggregates, HAVING, alias rewrites, result ordering and pagination natively. SUM/AVG normalize numeric inputs to REAL for parity with DALgo's generic float64 fallback. FIRST/LAST are deliberately not advertised until DALgo models aggregate-local ordering; using unspecified SQLite row order would not be deterministic.

End2end - is a separate module

For end-to-end testing a SQLite driver is used. To avoid bringing a dependency to SQLite into the consumers of dalgo2sql, the end2end tests are in a separate module.

This is an unusual approach, as usually you would want to bring dependency to underlying driver with a dalgo adapter. But this is not a case for this adapter as database/sql that is referenced by dalgo2sql is an abstraction layer and consumer is free to choose the underlying driver.

License

Free to use and open source under MIT License.

Documentation

Index

Constants

View Source
const Version = "0.0.8"

Version indicates package version

Variables

This section is empty.

Functions

func NewDatabase

func NewDatabase(db *sql.DB, schema dal.Schema, options DbOptions) dal.DB

NewDatabase creates a new instance of DALgo adapter to SQL database.

The returned dal.DB is sealed by dal.NewDB: every read-write transaction it starts hands the worker a transaction whose writes run the framework's BeforeSave validation and hooks before reaching this adapter's code.

func NewSimpleSchema added in v0.2.1

func NewSimpleSchema(idFieldName string) dal.Schema

Types

type DbOptions added in v0.4.2

type DbOptions struct {
	ID         string
	PrimaryKey []string
	Recordsets map[string]*Recordset
	// Placeholder controls how SQL parameter markers are emitted.
	// The zero value (PlaceholderQuestion) uses "?" — compatible with
	// SQLite, MySQL, and most other drivers.  Set to PlaceholderDollar
	// for PostgreSQL, which requires "$1", "$2", … positional markers.
	Placeholder PlaceholderDialect
	// StructuredQueryDialect opts structured reads into safe dialect-specific
	// compilation. Empty preserves legacy emission; "sqlite" is supported.
	StructuredQueryDialect string
	// NativeJoinHintTranslator optionally translates validated DALgo JOIN
	// algorithm preferences into trusted, dialect-owned SQL fragments. The
	// translator receives the complete relation tree so it can preserve each
	// edge's independent preference order. Nil is the adapter's explicit ignore
	// policy: a custom compiler receives no fragments. It also preserves the
	// existing SQL byte stream, which is how SQLite ignores JOIN hints by default.
	NativeJoinHintTranslator NativeJoinHintTranslator
	// NativeJoinEligibility is the adapter's opt-in semantic proof for native
	// JOIN execution with NativeStructuredQueryCompiler. Both fields are
	// required for a non-SQLite native path; a nil hook preserves SQLite's
	// transaction-local key preflight and declines other dialects.
	NativeJoinEligibility NativeJoinEligibility
	// NativeStructuredQueryCompiler emits a complete structured SQL query for a
	// trusted adapter dialect. It receives translated JOIN hint fragments on the
	// actual read path. dalgo2sql provides no SQL Server or Oracle compiler.
	NativeStructuredQueryCompiler NativeStructuredQueryCompiler

	// IsAlreadyExists reports whether err — the raw error returned by the
	// underlying database/sql driver for a failed INSERT — represents a
	// duplicate-key violation (a unique or primary-key constraint failure).
	// Detection is driver-specific — pgx's *pgconn.PgError with code
	// "23505", go-sql-driver/mysql's *mysql.MySQLError with number 1062,
	// modernc.org/sqlite's *sqlite.Error with an SQLITE_CONSTRAINT_* code —
	// so dalgo2sql cannot recognize it on its own. The wrapping adapter
	// (dalgo2postgres, dalgo2mysql, dalgo2sqlite, …) supplies this hook.
	//
	// The zero value (nil) is backward compatible: it preserves today's
	// behavior exactly, and the raw driver error passes through unwrapped.
	// When set and it reports true for an insert's error, dalgo2sql wraps
	// that error with record.ErrRecordExists (see execInsert) so callers
	// can test it with record.IsAlreadyExists — the driver error itself is
	// preserved in the chain, never replaced, so existing callers matching
	// on error text or type keep working.
	IsAlreadyExists func(err error) bool
}

DbOptions provides database sqlOptions for DALgo - // TODO: document why & how to use

func (DbOptions) GetRecordsetByKey added in v0.4.2

func (o DbOptions) GetRecordsetByKey(key *record.Key) *Recordset

func (DbOptions) PrimaryKeyFieldNames added in v0.4.2

func (o DbOptions) PrimaryKeyFieldNames(key *record.Key) (primaryKey []string)

type Field

type Field struct {
	Name string
}

Field defines field

func (Field) String added in v0.0.9

func (v Field) String() string

type NativeJoinEligibility added in v0.19.0

type NativeJoinEligibility func(context.Context, dal.StructuredQuery) error

NativeJoinEligibility lets a concrete adapter validate whether its native compiler can preserve DALgo JOIN semantics for one complete query.

type NativeJoinHintFragments added in v0.19.0

type NativeJoinHintFragments struct {
	AfterSelect   string
	JoinOperators map[string]string
	AfterQuery    string
	HandledPaths  []string
}

NativeJoinHintFragments identifies where a dialect places its trusted SQL JOIN hints. JoinOperators is keyed by DALgo structural paths such as "from.joins[0]" and is emitted between the JOIN type and JOIN keyword, so a SQL Server adapter can return "HASH" for `INNER HASH JOIN`. AfterSelect and AfterQuery support dialects such as Oracle that place optimizer hints after SELECT or at the end of the statement. HandledPaths must acknowledge every hinted edge as applied or intentionally ignored by adapter policy; a rejected preference is returned as an error. This prevents a configured translator from silently dropping a per-edge preference while using only a statement level fragment.

type NativeJoinHintTranslator added in v0.19.0

type NativeJoinHintTranslator interface {
	TranslateNativeJoinHints(dal.FromSource) (NativeJoinHintFragments, error)
}

NativeJoinHintTranslator translates a complete, validated relation tree for one native SQL query. It is implemented by trusted database adapters, never from DTQL input. An error prevents SQL from being emitted.

type NativeStructuredQueryCompiler added in v0.19.0

type NativeStructuredQueryCompiler interface {
	CompileNativeStructuredQuery(dal.StructuredQuery, NativeJoinHintFragments) (string, []any, error)
}

NativeStructuredQueryCompiler emits a complete structured query for a concrete adapter dialect. Its implementation owns parameter syntax, identifier quoting, and all dialect semantics.

type PlaceholderDialect added in v0.9.0

type PlaceholderDialect int

PlaceholderDialect selects how positional SQL parameters are formatted. The zero value (PlaceholderQuestion) is backward-compatible with all drivers that accept "?" — SQLite, MySQL, etc.

const (
	// PlaceholderQuestion emits "?" for every parameter (default, SQLite/MySQL style).
	PlaceholderQuestion PlaceholderDialect = iota
	// PlaceholderDollar emits "$1", "$2", … (PostgreSQL style).
	PlaceholderDollar
)

type Recordset

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

Recordset hold recordset settings

func NewRecordset added in v0.0.9

func NewRecordset(name string, t RecordsetType, primaryKey []dal.FieldRef) *Recordset

func (*Recordset) Name

func (v *Recordset) Name() string

func (*Recordset) PrimaryKey

func (v *Recordset) PrimaryKey() []dal.FieldRef

func (*Recordset) PrimaryKeyFieldNames added in v0.0.9

func (v *Recordset) PrimaryKeyFieldNames() []string

func (*Recordset) Type

func (v *Recordset) Type() RecordsetType

type RecordsetType

type RecordsetType = int

RecordsetType defines type of a database recordset

const (
	// Table identifies a table in a database
	Table RecordsetType = iota
)

Directories

Path Synopsis
end2end module

Jump to

Keyboard shortcuts

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