# doug-martin/goqu architecture

> Architecture diagram of the GitHub repository doug-martin/goqu, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-05-20). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/doug-martin/goqu
- Repository: https://github.com/doug-martin/goqu

## Overview

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/sqlgen.go`](https://github.com/doug-martin/goqu/blob/master/sqlgen/sqlgen.go) (sqlgen), [`exec/query_executor.go`](https://github.com/doug-martin/goqu/blob/master/exec/query_executor.go) (Executor), [`exp/exp.go`](https://github.com/doug-martin/goqu/blob/master/exp/exp.go) (exp), [`goqu.go`](https://github.com/doug-martin/goqu/blob/master/goqu.go) (goqu)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_public_api["Public API"]
  node_goqu["goqu<br/>package entry<br/>[goqu.go]"]
  node_database["Database<br/>facade<br/>[database.go]"]
end

subgraph group_datasets["Datasets"]
  node_select_dataset["Select<br/>dataset<br/>[select_dataset.go]"]
  node_insert_dataset["Insert<br/>dataset<br/>[insert_dataset.go]"]
  node_update_dataset["Update<br/>dataset<br/>[update_dataset.go]"]
  node_delete_dataset["Delete<br/>dataset<br/>[delete_dataset.go]"]
  node_truncate_dataset["Truncate<br/>dataset"]
end

subgraph group_expressions["Expressions"]
  node_exp_core["exp<br/>[exp.go]"]
  node_exp_clauses["Clauses<br/>[select_clauses.go]"]
  node_exp_types["Expr types<br/>expression nodes<br/>[ident.go]"]
end

subgraph group_generation["SQL Generation"]
  node_sqlgen_core["sqlgen<br/>generator core<br/>[sqlgen.go]"]
  node_select_gen["Select SQL<br/>generator"]
  node_insert_gen["Insert SQL<br/>generator"]
  node_update_gen["Update SQL<br/>generator"]
  node_delete_gen["Delete SQL<br/>generator"]
  node_truncate_gen["Truncate SQL<br/>generator"]
  node_expr_gen["Expr SQL<br/>generator"]
end

subgraph group_dialects["Dialects"]
  node_sql_dialect["Dialect API<br/>dialect contract<br/>[sql_dialect.go]"]
  node_mysql_dialect["MySQL<br/>dialect<br/>[mysql.go]"]
  node_postgres_dialect["Postgres<br/>dialect<br/>[postgres.go]"]
  node_sqlite_dialect["SQLite<br/>dialect<br/>[sqlite3.go]"]
  node_sqlserver_dialect["SQL Server<br/>dialect<br/>[sqlserver.go]"]
end

subgraph group_execution["Execution"]
  node_query_executor["Executor<br/>[query_executor.go]"]
  node_query_factory["Query factory<br/>[query_factory.go]"]
  node_scanner["Scanner<br/>result mapper<br/>[scanner.go]"]
end

subgraph group_support["Support"]
  node_internal_util["util<br/>reflection helpers<br/>[reflect.go]"]
  node_internal_tag["tags<br/>tag parser<br/>[tags.go]"]
  node_internal_errors["errors<br/>error helpers<br/>[error.go]"]
  node_internal_sb["sb<br/>string builder<br/>[sql_builder.go]"]
  node_prepared["Prepared<br/>output mode<br/>[prepared.go]"]
end

node_goqu -->|"constructs"| node_select_dataset
node_goqu -->|"constructs"| node_insert_dataset
node_goqu -->|"constructs"| node_update_dataset
node_goqu -->|"constructs"| node_delete_dataset
node_goqu -->|"constructs"| node_truncate_dataset
node_database -->|"drives"| node_query_factory
node_database -->|"executes"| node_query_executor
node_select_dataset -->|"composes"| node_exp_core
node_insert_dataset -->|"composes"| node_exp_core
node_update_dataset -->|"composes"| node_exp_core
node_delete_dataset -->|"composes"| node_exp_core
node_truncate_dataset -->|"composes"| node_exp_core
node_exp_core -->|"defines"| node_exp_types
node_exp_core -->|"organizes"| node_exp_clauses
node_select_dataset -->|"renders via"| node_select_gen
node_insert_dataset -->|"renders via"| node_insert_gen
node_update_dataset -->|"renders via"| node_update_gen
node_delete_dataset -->|"renders via"| node_delete_gen
node_truncate_dataset -->|"renders via"| node_truncate_gen
node_select_gen -->|"delegates"| node_expr_gen
node_insert_gen -->|"delegates"| node_expr_gen
node_update_gen -->|"delegates"| node_expr_gen
node_delete_gen -->|"delegates"| node_expr_gen
node_truncate_gen -->|"delegates"| node_expr_gen
node_sqlgen_core -->|"uses"| node_sql_dialect
node_sqlgen_core -->|"coordinates"| node_select_gen
node_sqlgen_core -->|"coordinates"| node_insert_gen
node_sqlgen_core -->|"coordinates"| node_update_gen
node_sqlgen_core -->|"coordinates"| node_delete_gen
node_sqlgen_core -->|"coordinates"| node_truncate_gen
node_mysql_dialect -->|"implements"| node_sql_dialect
node_postgres_dialect -->|"implements"| node_sql_dialect
node_sqlite_dialect -->|"implements"| node_sql_dialect
node_sqlserver_dialect -->|"implements"| node_sql_dialect
node_query_factory -->|"builds"| node_sqlgen_core
node_query_executor -->|"consumes"| node_query_factory
node_scanner -->|"reads results from"| node_query_executor
node_select_dataset -->|"executes through"| node_query_executor
node_insert_dataset -->|"executes through"| node_query_executor
node_update_dataset -->|"executes through"| node_query_executor
node_delete_dataset -->|"executes through"| node_query_executor
node_truncate_dataset -->|"executes through"| node_query_executor
node_internal_util -->|"supports"| node_insert_dataset
node_internal_util -->|"supports"| node_update_dataset
node_internal_util -->|"supports"| node_scanner
node_internal_tag -->|"drives mapping"| node_internal_util
node_internal_errors -->|"shared by"| node_sqlgen_core
node_internal_sb -->|"builds text"| node_sqlgen_core

click node_goqu "https://github.com/doug-martin/goqu/blob/master/goqu.go"
click node_database "https://github.com/doug-martin/goqu/blob/master/database.go"
click node_select_dataset "https://github.com/doug-martin/goqu/blob/master/select_dataset.go"
click node_insert_dataset "https://github.com/doug-martin/goqu/blob/master/insert_dataset.go"
click node_update_dataset "https://github.com/doug-martin/goqu/blob/master/update_dataset.go"
click node_delete_dataset "https://github.com/doug-martin/goqu/blob/master/delete_dataset.go"
click node_truncate_dataset "https://github.com/doug-martin/goqu/blob/master/truncate_dataset.go"
click node_exp_core "https://github.com/doug-martin/goqu/blob/master/exp/exp.go"
click node_exp_clauses "https://github.com/doug-martin/goqu/blob/master/exp/select_clauses.go"
click node_exp_types "https://github.com/doug-martin/goqu/blob/master/exp/ident.go"
click node_sqlgen_core "https://github.com/doug-martin/goqu/blob/master/sqlgen/sqlgen.go"
click node_select_gen "https://github.com/doug-martin/goqu/blob/master/sqlgen/select_sql_generator.go"
click node_insert_gen "https://github.com/doug-martin/goqu/blob/master/sqlgen/insert_sql_generator.go"
click node_update_gen "https://github.com/doug-martin/goqu/blob/master/sqlgen/update_sql_generator.go"
click node_delete_gen "https://github.com/doug-martin/goqu/blob/master/sqlgen/delete_sql_generator.go"
click node_truncate_gen "https://github.com/doug-martin/goqu/blob/master/sqlgen/truncate_sql_generator.go"
click node_expr_gen "https://github.com/doug-martin/goqu/blob/master/sqlgen/expression_sql_generator.go"
click node_sql_dialect "https://github.com/doug-martin/goqu/blob/master/sql_dialect.go"
click node_mysql_dialect "https://github.com/doug-martin/goqu/blob/master/dialect/mysql/mysql.go"
click node_postgres_dialect "https://github.com/doug-martin/goqu/blob/master/dialect/postgres/postgres.go"
click node_sqlite_dialect "https://github.com/doug-martin/goqu/blob/master/dialect/sqlite3/sqlite3.go"
click node_sqlserver_dialect "https://github.com/doug-martin/goqu/blob/master/dialect/sqlserver/sqlserver.go"
click node_query_executor "https://github.com/doug-martin/goqu/blob/master/exec/query_executor.go"
click node_query_factory "https://github.com/doug-martin/goqu/blob/master/exec/query_factory.go"
click node_scanner "https://github.com/doug-martin/goqu/blob/master/exec/scanner.go"
click node_internal_util "https://github.com/doug-martin/goqu/blob/master/internal/util/reflect.go"
click node_internal_tag "https://github.com/doug-martin/goqu/blob/master/internal/tag/tags.go"
click node_internal_errors "https://github.com/doug-martin/goqu/blob/master/internal/errors/error.go"
click node_internal_sb "https://github.com/doug-martin/goqu/blob/master/internal/sb/sql_builder.go"
click node_prepared "https://github.com/doug-martin/goqu/blob/master/prepared.go"

classDef toneNeutral fill:#f8fafc,stroke:#334155,stroke-width:1.5px,color:#0f172a
classDef toneBlue fill:#dbeafe,stroke:#2563eb,stroke-width:1.5px,color:#172554
classDef toneAmber fill:#fef3c7,stroke:#d97706,stroke-width:1.5px,color:#78350f
classDef toneMint fill:#dcfce7,stroke:#16a34a,stroke-width:1.5px,color:#14532d
classDef toneRose fill:#ffe4e6,stroke:#e11d48,stroke-width:1.5px,color:#881337
classDef toneIndigo fill:#e0e7ff,stroke:#4f46e5,stroke-width:1.5px,color:#312e81
classDef toneTeal fill:#ccfbf1,stroke:#0f766e,stroke-width:1.5px,color:#134e4a
class node_goqu,node_database,node_internal_util,node_internal_tag,node_internal_errors,node_internal_sb,node_prepared toneBlue
class node_select_dataset,node_insert_dataset,node_update_dataset,node_delete_dataset,node_truncate_dataset toneAmber
class node_exp_core,node_exp_clauses,node_exp_types toneMint
class node_sqlgen_core,node_select_gen,node_insert_gen,node_update_gen,node_delete_gen,node_truncate_gen,node_expr_gen toneRose
class node_sql_dialect,node_mysql_dialect,node_postgres_dialect,node_sqlite_dialect,node_sqlserver_dialect toneIndigo
class node_query_executor,node_query_factory,node_scanner toneTeal
```

## Components

### Public API

User-facing entry points for building datasets and database handles.

- [goqu](https://github.com/doug-martin/goqu/blob/master/goqu.go): package entry · Top-level DSL constructors and shared API surface. · `goqu.go`
- [Database](https://github.com/doug-martin/goqu/blob/master/database.go): facade · Higher-level wrapper combining query building with execution. · `database.go`

### Datasets

Verb-specific query state objects and fluent DSL boundaries.

- [Select](https://github.com/doug-martin/goqu/blob/master/select_dataset.go): dataset · SELECT query state and fluent composition. · `select_dataset.go`
- [Insert](https://github.com/doug-martin/goqu/blob/master/insert_dataset.go): dataset · INSERT query state and fluent composition. · `insert_dataset.go`
- [Update](https://github.com/doug-martin/goqu/blob/master/update_dataset.go): dataset · UPDATE query state and fluent composition. · `update_dataset.go`
- [Delete](https://github.com/doug-martin/goqu/blob/master/delete_dataset.go): dataset · DELETE query state and fluent composition. · `delete_dataset.go`
- [Truncate](https://github.com/doug-martin/goqu/blob/master/truncate_dataset.go): dataset · TRUNCATE query state and fluent composition. · `truncate_dataset.go`

### Expressions

AST-like SQL expression model used to compose queries.

- [exp](https://github.com/doug-martin/goqu/blob/master/exp/exp.go): expression model · Core expression interfaces and shared AST contracts. · `exp/exp.go`
- [Clauses](https://github.com/doug-martin/goqu/blob/master/exp/select_clauses.go): expression clauses · Statement-specific clause objects for select/insert/update/delete/truncate. · `exp/select_clauses.go`
- [Expr types](https://github.com/doug-martin/goqu/blob/master/exp/ident.go): expression nodes · 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](https://github.com/doug-martin/goqu/blob/master/sqlgen/sqlgen.go): generator core · Shared SQL generation orchestration and options. · `sqlgen/sqlgen.go`
- [Select SQL](https://github.com/doug-martin/goqu/blob/master/sqlgen/select_sql_generator.go): generator · Renders SELECT datasets to SQL text and arguments. · `sqlgen/select_sql_generator.go`
- [Insert SQL](https://github.com/doug-martin/goqu/blob/master/sqlgen/insert_sql_generator.go): generator · Renders INSERT datasets to SQL text and arguments. · `sqlgen/insert_sql_generator.go`
- [Update SQL](https://github.com/doug-martin/goqu/blob/master/sqlgen/update_sql_generator.go): generator · Renders UPDATE datasets to SQL text and arguments. · `sqlgen/update_sql_generator.go`
- [Delete SQL](https://github.com/doug-martin/goqu/blob/master/sqlgen/delete_sql_generator.go): generator · Renders DELETE datasets to SQL text and arguments. · `sqlgen/delete_sql_generator.go`
- [Truncate SQL](https://github.com/doug-martin/goqu/blob/master/sqlgen/truncate_sql_generator.go): generator · Renders TRUNCATE datasets to SQL text and arguments. · `sqlgen/truncate_sql_generator.go`
- [Expr SQL](https://github.com/doug-martin/goqu/blob/master/sqlgen/expression_sql_generator.go): generator · Renders individual expressions into SQL fragments. · `sqlgen/expression_sql_generator.go`

### Dialects

Vendor-specific SQL behavior and quoting/placeholder rules.

- [Dialect API](https://github.com/doug-martin/goqu/blob/master/sql_dialect.go): dialect contract · Dialect interface and shared options for vendor-specific behavior. · `sql_dialect.go`
- [MySQL](https://github.com/doug-martin/goqu/blob/master/dialect/mysql/mysql.go): dialect · MySQL-specific quoting and SQL behavior. · `dialect/mysql/mysql.go`
- [Postgres](https://github.com/doug-martin/goqu/blob/master/dialect/postgres/postgres.go): dialect · PostgreSQL-specific quoting and SQL behavior. · `dialect/postgres/postgres.go`
- [SQLite](https://github.com/doug-martin/goqu/blob/master/dialect/sqlite3/sqlite3.go): dialect · SQLite-specific quoting and SQL behavior. · `dialect/sqlite3/sqlite3.go`
- [SQL Server](https://github.com/doug-martin/goqu/blob/master/dialect/sqlserver/sqlserver.go): dialect · SQL Server-specific quoting and SQL behavior. · `dialect/sqlserver/sqlserver.go`

### Execution

Query execution, result handling, and scanning against database/sql.

- [Executor](https://github.com/doug-martin/goqu/blob/master/exec/query_executor.go): query executor · Runs generated SQL and manages execution helpers. · `exec/query_executor.go`
- [Query factory](https://github.com/doug-martin/goqu/blob/master/exec/query_factory.go): query factory · Builds executable queries from datasets and options. · `exec/query_factory.go`
- [Scanner](https://github.com/doug-martin/goqu/blob/master/exec/scanner.go): result mapper · Maps rows and values back into Go structures and primitives. · `exec/scanner.go`

### Support

Shared plumbing, reflection helpers, and internal utilities.

- [util](https://github.com/doug-martin/goqu/blob/master/internal/util/reflect.go): reflection helpers · Struct-to-map and value-slice utilities for inserts, updates, and scanning. · `internal/util/reflect.go`
- [tags](https://github.com/doug-martin/goqu/blob/master/internal/tag/tags.go): tag parser · Reads db/goqu struct tags for mapping and scanning. · `internal/tag/tags.go`
- [errors](https://github.com/doug-martin/goqu/blob/master/internal/errors/error.go): error helpers · Shared error types and construction helpers. · `internal/errors/error.go`
- [sb](https://github.com/doug-martin/goqu/blob/master/internal/sb/sql_builder.go): string builder · Internal SQL string builder plumbing. · `internal/sb/sql_builder.go`
- [Prepared](https://github.com/doug-martin/goqu/blob/master/prepared.go): output mode · Bind-parameter versus interpolated SQL behavior. · `prepared.go`

## Connections

- 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.
