How doug-martin/goqu works
1. What this repository is
goqu is a Go library for building, generating, and executing SQL across multiple database dialects. It is not an application; its architecture is centered on a fluent API that produces SQL strings and argument lists, plus optional execution/scanning against database/sql.
2. Top-level public API layer
The main entry points are the dataset types in select_dataset.go, insert_dataset.go, update_dataset.go, delete_dataset.go, and truncate_dataset.go, along with database.go and goqu.go. These files expose the user-facing DSL: From, Insert, Update, Delete, Truncate, ToSQL, and execution helpers.
3. Dataset objects as the core composition boundary
Each dataset represents a query state machine for one SQL verb. It holds selected tables, clauses, predicates, ordering, limits, values, and flags like prepared mode. The dataset layer is the main boundary between user code and the SQL generator layer below it.
4. Expression system
The exp/ package is the library’s AST-like expression model. It defines column references, identifiers, literals, boolean logic, functions, joins, CTEs, window functions, casts, conflicts, ordering, locking, and statement-specific clause objects. Datasets are built by composing these expressions rather than concatenating SQL fragments directly.
5. SQL generation layer
The sqlgen/ package turns expressions and datasets into SQL text and bind arguments. It contains separate generators for select, insert, update, delete, truncate, and generic expression rendering. This is where SQL syntax is assembled, with dialect-specific behavior injected through options and dialect implementations.
6. Dialect abstraction
The dialect/ directory contains concrete implementations for mysql, postgres, sqlite3, and sqlserver. These dialects define quoting rules, placeholder style, support for features like locking or conflict clauses, and other SQL differences. The dialect boundary is what lets one dataset model emit vendor-specific SQL without changing the DSL.
7. Execution and scanning
The exec/ package handles running generated SQL and mapping database/sql results back into Go values. It includes query execution helpers, query factory logic, and scanners for rows/values/structs. This layer is separate from SQL generation so the same SQL can be used either as text-only output or as executable queries.
8. Database wrapper
database.go provides a higher-level database facade around an underlying SQL executor. It connects the dialect-aware query building pipeline to actual database execution, and acts as the bridge for callers who want goqu to manage query generation plus execution in one object.
9. Reflection and struct mapping utilities
internal/util/ and internal/tag/ support converting structs into column/value maps and reading db/goqu tags. These helpers are important for insert/update row encoding and for scanning rows into structs. They form the reflective data-mapping subsystem beneath the public API.
10. Prepared vs interpolated SQL
prepared.go and interpolation-related docs/tests indicate two output modes: bind-parameter SQL and fully interpolated SQL. The generator and executor pipeline must preserve argument ordering and dialect-specific placeholders when prepared mode is enabled.
11. Error handling and shared primitives
internal/errors/, internal/sb/, and small shared files like sql_dialect.go, sql_dialect_options.go, and expressions.go provide the common plumbing used across packages. This includes string building, error types, dialect option structs, and interface contracts that decouple datasets, generators, and dialects.
12. Testing and integration environment
The repository has extensive unit and example tests across exp/, sqlgen/, exec/, and dataset files, plus integration-oriented tests that rely on PostgreSQL, MySQL, SQLite, and SQL Server. .github/workflows, docker-compose.yml, and .run/ files show the project is validated against multiple live databases, which is important because dialect behavior is a first-class architectural concern.
13. Documentation as part of the contract
The docs/ directory is organized by concern: selecting, inserting, updating, deleting, expressions, database usage, interpolation, time handling, and dialects. This mirrors the code structure and reinforces the main architectural separation: expression model, dataset composition, SQL generation, dialect specialization, and execution.
Main entry points: sqlgen.go sqlgen · query_executor.go Executor · exp.go exp · goqu.go goqu
Components 30
Public API
User-facing entry points for building datasets and database handles.
Datasets
Verb-specific query state objects and fluent DSL boundaries.
- Select · SELECT query state and fluent composition.
select_dataset.go - Insert · INSERT query state and fluent composition.
insert_dataset.go - Update · UPDATE query state and fluent composition.
update_dataset.go - Delete · DELETE query state and fluent composition.
delete_dataset.go - Truncate · TRUNCATE query state and fluent composition.
truncate_dataset.go
Expressions
AST-like SQL expression model used to compose queries.
- exp · Core expression interfaces and shared AST contracts.
exp/exp.go - Clauses · Statement-specific clause objects for select/insert/update/delete/truncate.
exp/select_clauses.go - Expr types · Identifiers, literals, functions, joins, CTEs, windows, casts, locks, and related nodes.
exp/ident.go
SQL Generation
Renderers that turn datasets and expressions into SQL and bind arguments.
- sqlgen · Shared SQL generation orchestration and options.
sqlgen/sqlgen.go - Select SQL · Renders SELECT datasets to SQL text and arguments.
sqlgen/select_sql_generator.go - Insert SQL · Renders INSERT datasets to SQL text and arguments.
sqlgen/insert_sql_generator.go - Update SQL · Renders UPDATE datasets to SQL text and arguments.
sqlgen/update_sql_generator.go - Delete SQL · Renders DELETE datasets to SQL text and arguments.
sqlgen/delete_sql_generator.go - Truncate SQL · Renders TRUNCATE datasets to SQL text and arguments.
sqlgen/truncate_sql_generator.go - Expr SQL · Renders individual expressions into SQL fragments.
sqlgen/expression_sql_generator.go
Dialects
Vendor-specific SQL behavior and quoting/placeholder rules.
- Dialect API · Dialect interface and shared options for vendor-specific behavior.
sql_dialect.go - MySQL · MySQL-specific quoting and SQL behavior.
dialect/mysql/mysql.go - Postgres · PostgreSQL-specific quoting and SQL behavior.
dialect/postgres/postgres.go - SQLite · SQLite-specific quoting and SQL behavior.
dialect/sqlite3/sqlite3.go - SQL Server · SQL Server-specific quoting and SQL behavior.
dialect/sqlserver/sqlserver.go
Execution
Query execution, result handling, and scanning against database/sql.
- Executor · Runs generated SQL and manages execution helpers.
exec/query_executor.go - Query factory · Builds executable queries from datasets and options.
exec/query_factory.go - Scanner · Maps rows and values back into Go structures and primitives.
exec/scanner.go
Support
Shared plumbing, reflection helpers, and internal utilities.
- util · Struct-to-map and value-slice utilities for inserts, updates, and scanning.
internal/util/reflect.go - tags · Reads db/goqu struct tags for mapping and scanning.
internal/tag/tags.go - errors · Shared error types and construction helpers.
internal/errors/error.go - sb · Internal SQL string builder plumbing.
internal/sb/sql_builder.go - Prepared · Bind-parameter versus interpolated SQL behavior.
prepared.go
Connections 48
- goqu → Select: constructs · Creates SELECT datasets from the public DSL.
- goqu → Insert: constructs · Creates INSERT datasets from the public DSL.
- goqu → Update: constructs · Creates UPDATE datasets from the public DSL.
- goqu → Delete: constructs · Creates DELETE datasets from the public DSL.
- goqu → Truncate: constructs · Creates TRUNCATE datasets from the public DSL.
- Database → Query factory: drives · Database facade routes dataset work into query construction and execution.
- Database → Executor: executes · Runs generated statements against the underlying SQL executor.
- Select → exp: composes · SELECT state is built from expression interfaces and clause objects.
- Insert → exp: composes · INSERT state is built from expression interfaces and clause objects.
- Update → exp: composes · UPDATE state is built from expression interfaces and clause objects.
- Delete → exp: composes · DELETE state is built from expression interfaces and clause objects.
- Truncate → exp: composes · TRUNCATE state is built from expression interfaces and clause objects.
- exp → Expr types: defines · Core expression contracts are implemented by expression nodes.
- exp → Clauses: organizes · Statement-specific clause objects hang off the core expression model.
- Select → Select SQL: renders via · SELECT datasets are converted to SQL by the select generator.
- Insert → Insert SQL: renders via · INSERT datasets are converted to SQL by the insert generator.
- Update → Update SQL: renders via · UPDATE datasets are converted to SQL by the update generator.
- Delete → Delete SQL: renders via · DELETE datasets are converted to SQL by the delete generator.
- Truncate → Truncate SQL: renders via · TRUNCATE datasets are converted to SQL by the truncate generator.
- Select SQL → Expr SQL: delegates · Statement generators invoke expression rendering for nested nodes.
- Insert SQL → Expr SQL: delegates · Statement generators invoke expression rendering for nested nodes.
- Update SQL → Expr SQL: delegates · Statement generators invoke expression rendering for nested nodes.
- Delete SQL → Expr SQL: delegates · Statement generators invoke expression rendering for nested nodes.
- Truncate SQL → Expr SQL: delegates · Statement generators invoke expression rendering for nested nodes.
- sqlgen → Dialect API: uses · SQL generation is parameterized by dialect capabilities and options.
- sqlgen → Select SQL: coordinates · Shared generation layer coordinates verb-specific generators.
- sqlgen → Insert SQL: coordinates · Shared generation layer coordinates verb-specific generators.
- sqlgen → Update SQL: coordinates · Shared generation layer coordinates verb-specific generators.
- sqlgen → Delete SQL: coordinates · Shared generation layer coordinates verb-specific generators.
- sqlgen → Truncate SQL: coordinates · Shared generation layer coordinates verb-specific generators.
- MySQL → Dialect API: implements · MySQL dialect plugs into the shared dialect contract.
- Postgres → Dialect API: implements · Postgres dialect plugs into the shared dialect contract.
- SQLite → Dialect API: implements · SQLite dialect plugs into the shared dialect contract.
- SQL Server → Dialect API: implements · SQL Server dialect plugs into the shared dialect contract.
- Query factory → sqlgen: builds · Turns datasets into executable SQL requests.
- Executor → Query factory: consumes · Executor consumes built query objects for execution.
- Scanner → Executor: reads results from · Scanner maps database rows and values returned by execution.
- Select → Executor: executes through · Datasets can be executed directly via helpers.
- Insert → Executor: executes through · Datasets can be executed directly via helpers.
- Update → Executor: executes through · Datasets can be executed directly via helpers.
- Delete → Executor: executes through · Datasets can be executed directly via helpers.
- Truncate → Executor: executes through · Datasets can be executed directly via helpers.
- util → Insert: supports · Reflection utilities help encode rows and structs for writes.
- util → Update: supports · Reflection utilities help encode rows and structs for writes.
- util → Scanner: supports · Reflection utilities help map results into Go values.
- tags → util: drives mapping · Tags guide struct field to column/value conversion.
- errors → sqlgen: shared by · Common error types and helpers are used across generation.
- sb → sqlgen: builds text · Shared string builder underpins SQL assembly.