# go-xorm/xorm architecture

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

- Interactive diagram: https://gitdiagram.com/go-xorm/xorm
- Repository: https://github.com/go-xorm/xorm

## Overview

1. **This repository is a Go ORM library, not an application.**  
   The central package exposes an `Engine` façade that applications create with a DB driver name and DSN. From there, almost all work flows through a stateful `Session`, which is the unit of query building and execution.

2. **`Engine` is the top-level integration point.**  
   It owns configuration, database connectivity, dialect selection, and session creation. It also exposes the high-level CRUD/query APIs seen in the README: `Get`, `Find`, `Insert`, `Update`, `Delete`, `Query`, `Exec`, `Transaction`, and schema sync methods.

3. **`Session` is the main execution boundary.**  
   The chainable API is implemented by accumulating state on a session: table selection, filters, column lists, ordering, limits, and raw SQL overrides. A session then turns that state into a concrete statement, runs it, and scans results back into structs or basic containers.

4. **SQL generation is separated into a dedicated statement layer.**  
   Files like `statement.go`, `statement_args.go`, `statement_quote.go`, and `statement_exprparam.go` indicate a distinct SQL assembly subsystem. Its job is to translate session state and mapped metadata into dialect-correct SQL text plus argument lists.

5. **Struct-to-table mapping is handled by reflection and tags.**  
   The files around `tag.go`, `types.go`, `convert.go`, `helpers.go`, `json.go`, and the tag tests show a metadata layer that reads Go struct definitions, field tags, primary keys, version/created/updated semantics, and null/type conversions. This layer defines how beans map to tables and columns.

6. **Database-specific behavior is isolated behind dialect implementations.**  
   `dialect_mysql.go`, `dialect_postgres.go`, `dialect_sqlite3.go`, `dialect_mssql.go`, and `dialect_oracle.go` encapsulate placeholder style, quoting, reserved words, paging, schema quirks, and other SQL differences. This boundary is what lets the same ORM API work across multiple databases.

7. **Result scanning and row iteration form the read-path back into Go values.**  
   `rows.go` and the various `session_find/get/query/iterate` files represent the fetch pipeline: database rows are read, converted, and assigned into structs, slices, maps, or scalar values. The repository supports both bulk loading and streaming/row-by-row access.

8. **Caching is a first-class subsystem with both global and session-scoped layers.**  
   `cache_lru.go`, `cache_memory_store.go`, and `context_cache.go` implement query caching. The README’s “Context Cache” feature is session-local: repeated identical statements within the same session can be served without hitting the DB. This sits on the read path as an optional optimization layer.

9. **`EngineGroup` provides read/write splitting and connection routing.**  
   The `engine_group.go` and `engine_group_policy.go` files indicate a higher-level abstraction that fronts multiple engines, typically one master and several slaves. This is the architecture boundary for automatic read/write separation and routing policy decisions.

10. **Transactions are session-scoped and explicit.**  
   `transaction.go` and `session_tx.go` show that transaction control lives inside the session lifecycle. The design expects a single goroutine to own the transaction session, with `Begin`, `Commit`, and rollback-on-error semantics.

11. **Schema management and migration are separate from runtime CRUD.**  
   The `migrate/` package plus `session_schema.go` support schema synchronization and database evolution. This is a management path distinct from normal application queries, aimed at creating or aligning tables based on mapped structs.

12. **There is a processing/hook layer around statement execution.**  
   `processors.go` suggests pluggable processing before or after core DB actions. In architecture terms, this is the extension point for lifecycle behavior such as logging, mutation, or post-processing without embedding that logic in the core CRUD paths.

13. **External dependency boundaries are standard Go `database/sql` drivers.**  
   The ORM does not talk to databases directly; it sits above Go SQL drivers such as MySQL, PostgreSQL, SQLite, SQL Server, Oracle, and TiDB. That means the runtime boundary is: application → xorm Engine/Session → SQL generation/dialect → `database/sql` driver → database.

14. **Tests and examples are extensive and map directly to the subsystems.**  
   The `examples/` directory and per-feature tests mirror the architecture: dialect behavior, session CRUD, transactions, cache, tag parsing, and engine grouping. They are useful as executable documentation for how the main subsystems interact.

15. **Overall data flow for an operation is:**
   application code configures an `Engine` or `EngineGroup` → creates a `Session` → applies chainable conditions / table metadata / context cache → statement builder emits dialect-specific SQL and args → driver executes against the database → rows are scanned and converted back into Go values, optionally cached or streamed.

Main entry points: [`statement.go`](https://github.com/go-xorm/xorm/blob/master/statement.go) (Statement), [`session.go`](https://github.com/go-xorm/xorm/blob/master/session.go) (Session), [`engine.go`](https://github.com/go-xorm/xorm/blob/master/engine.go) (Engine), [`rows.go`](https://github.com/go-xorm/xorm/blob/master/rows.go) (Rows)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_core["Core API"]
  node_engine["Engine<br/>façade<br/>[engine.go]"]
  node_session["Session<br/>stateful unit<br/>[session.go]"]
  node_session_tx["Tx control<br/>transaction scope<br/>[session_tx.go]"]
  node_processors["Processors<br/>hook pipeline<br/>[processors.go]"]
end

subgraph group_sql["SQL Assembly"]
  node_statement["Statement<br/>SQL builder<br/>[statement.go]"]
  node_rows["Rows<br/>scan path<br/>[rows.go]"]
end

subgraph group_mapping["Mapping"]
  node_tag["Tags<br/>metadata parser<br/>[tag.go]"]
  node_types["Types<br/>conversion model<br/>[types.go]"]
  node_convert["Convert<br/>value conversion<br/>[convert.go]"]
  node_helpers["Helpers<br/>[helpers.go]"]
end

subgraph group_db["Database Adaptation"]
  node_engine_group["EngineGroup<br/>router<br/>[engine_group.go]"]
  node_dialect_mysql["MySQL dialect<br/>[dialect_mysql.go]"]
  node_dialect_postgres["Postgres dialect"]
  node_dialect_sqlite3["SQLite dialect<br/>[dialect_sqlite3.go]"]
  node_dialect_mssql["MSSQL dialect<br/>[dialect_mssql.go]"]
  node_dialect_oracle["Oracle dialect<br/>[dialect_oracle.go]"]
  node_cache["Context cache<br/>query cache<br/>[context_cache.go]"]
  node_migrate["Migrate<br/>schema sync<br/>[migrate.go]"]
end

node_database_sql[("database/sql drivers<br/>external boundary")]

node_engine -->|"creates"| node_session
node_engine -->|"routes via"| node_engine_group
node_engine -->|"selects"| node_dialect_mysql
node_engine -->|"syncs schema"| node_migrate
node_session -->|"builds"| node_statement
node_session -->|"scans"| node_rows
node_session -->|"consults"| node_cache
node_session -->|"passes through"| node_processors
node_session -->|"owns"| node_session_tx
node_statement -->|"uses metadata"| node_tag
node_statement -->|"formats"| node_types
node_tag -->|"interprets"| node_helpers
node_types -->|"converts through"| node_convert
node_convert -->|"decodes"| node_rows
node_statement -.->|"adapts to"| node_dialect_mysql
node_statement -.->|"adapts to"| node_dialect_postgres
node_statement -.->|"adapts to"| node_dialect_sqlite3
node_statement -.->|"adapts to"| node_dialect_mssql
node_statement -.->|"adapts to"| node_dialect_oracle
node_engine_group -->|"delegates"| node_database_sql
node_engine -->|"delegates"| node_database_sql
node_rows -->|"reads from"| node_database_sql
node_migrate -->|"emits"| node_statement
node_cache -.->|"short-circuits"| node_rows

click node_engine "https://github.com/go-xorm/xorm/blob/master/engine.go"
click node_engine_group "https://github.com/go-xorm/xorm/blob/master/engine_group.go"
click node_session "https://github.com/go-xorm/xorm/blob/master/session.go"
click node_session_tx "https://github.com/go-xorm/xorm/blob/master/session_tx.go"
click node_statement "https://github.com/go-xorm/xorm/blob/master/statement.go"
click node_rows "https://github.com/go-xorm/xorm/blob/master/rows.go"
click node_processors "https://github.com/go-xorm/xorm/blob/master/processors.go"
click node_tag "https://github.com/go-xorm/xorm/blob/master/tag.go"
click node_types "https://github.com/go-xorm/xorm/blob/master/types.go"
click node_convert "https://github.com/go-xorm/xorm/blob/master/convert.go"
click node_helpers "https://github.com/go-xorm/xorm/blob/master/helpers.go"
click node_dialect_mysql "https://github.com/go-xorm/xorm/blob/master/dialect_mysql.go"
click node_dialect_postgres "https://github.com/go-xorm/xorm/blob/master/dialect_postgres.go"
click node_dialect_sqlite3 "https://github.com/go-xorm/xorm/blob/master/dialect_sqlite3.go"
click node_dialect_mssql "https://github.com/go-xorm/xorm/blob/master/dialect_mssql.go"
click node_dialect_oracle "https://github.com/go-xorm/xorm/blob/master/dialect_oracle.go"
click node_cache "https://github.com/go-xorm/xorm/blob/master/context_cache.go"
click node_migrate "https://github.com/go-xorm/xorm/blob/master/migrate/migrate.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_engine,node_session,node_session_tx,node_processors toneBlue
class node_statement,node_rows toneAmber
class node_tag,node_types,node_convert,node_helpers toneMint
class node_engine_group,node_dialect_mysql,node_dialect_postgres,node_dialect_sqlite3,node_dialect_mssql,node_dialect_oracle,node_cache,node_migrate toneRose
class node_database_sql toneNeutral
```

## Components

### Core API

Public façade and session lifecycle

- [Engine](https://github.com/go-xorm/xorm/blob/master/engine.go): façade · Top-level ORM entrypoint for creating sessions, configuring connectivity, and exposing CRUD/query APIs · `engine.go`
- [Session](https://github.com/go-xorm/xorm/blob/master/session.go): stateful unit · Chainable query and execution state for one database operation · `session.go`
- [Tx control](https://github.com/go-xorm/xorm/blob/master/session_tx.go): transaction scope · Begin/commit/rollback lifecycle bound to a session · `session_tx.go`
- [Processors](https://github.com/go-xorm/xorm/blob/master/processors.go): hook pipeline · Pluggable pre/post execution processing around database actions · `processors.go`

### SQL Assembly

Statement building and query execution

- [Statement](https://github.com/go-xorm/xorm/blob/master/statement.go): SQL builder · Assembles dialect-correct SQL text and argument lists from session state · `statement.go`
- [Rows](https://github.com/go-xorm/xorm/blob/master/rows.go): scan path · Reads database rows and scans them back into Go values · `rows.go`

### Mapping

Struct metadata and value conversion

- [Tags](https://github.com/go-xorm/xorm/blob/master/tag.go): metadata parser · Reads struct tags and model metadata for table and field mapping · `tag.go`
- [Types](https://github.com/go-xorm/xorm/blob/master/types.go): conversion model · Represents mapped field types and nullability semantics · `types.go`
- [Convert](https://github.com/go-xorm/xorm/blob/master/convert.go): value conversion · Converts between database values and Go representations · `convert.go`
- [Helpers](https://github.com/go-xorm/xorm/blob/master/helpers.go): reflection helpers · Shared reflection and mapping utilities used by metadata and session logic · `helpers.go`

### Database Adaptation

Dialect, routing, caching, and schema management

- [EngineGroup](https://github.com/go-xorm/xorm/blob/master/engine_group.go): router · Read/write splitting and multi-engine routing boundary · `engine_group.go`
- [MySQL dialect](https://github.com/go-xorm/xorm/blob/master/dialect_mysql.go): dialect · MySQL-specific quoting, placeholders, and paging behavior · `dialect_mysql.go`
- [Postgres dialect](https://github.com/go-xorm/xorm/blob/master/dialect_postgres.go): dialect · PostgreSQL-specific SQL syntax and placeholder behavior · `dialect_postgres.go`
- [SQLite dialect](https://github.com/go-xorm/xorm/blob/master/dialect_sqlite3.go): dialect · SQLite-specific SQL syntax and paging behavior · `dialect_sqlite3.go`
- [MSSQL dialect](https://github.com/go-xorm/xorm/blob/master/dialect_mssql.go): dialect · SQL Server-specific SQL syntax and paging behavior · `dialect_mssql.go`
- [Oracle dialect](https://github.com/go-xorm/xorm/blob/master/dialect_oracle.go): dialect · Oracle-specific SQL syntax and paging behavior · `dialect_oracle.go`
- [Context cache](https://github.com/go-xorm/xorm/blob/master/context_cache.go): query cache · Session-local and shared cache layer for repeated reads · `context_cache.go`
- [Migrate](https://github.com/go-xorm/xorm/blob/master/migrate/migrate.go): schema sync · Schema synchronization and table evolution support · `migrate/migrate.go`

### Other components

- **database/sql drivers**: external boundary · Underlying Go driver layer for MySQL, Postgres, SQLite, SQL Server, Oracle, and TiDB

## Connections

- Engine → Session: creates · Engine creates configured sessions for operations
- Engine → EngineGroup: routes via · EngineGroup fronts multiple engines for read/write splitting
- Engine → MySQL dialect: selects · Engine binds a concrete dialect based on driver and configuration
- Engine → Migrate: syncs schema · Schema management is exposed from the engine layer
- Session → Statement: builds · Session state is translated into SQL statements
- Session → Rows: scans · Session query paths scan rows into Go destinations
- Session → Context cache: consults · Read paths can be served from cache when enabled
- Session → Processors: passes through · Execution hooks can run around core session actions
- Session → Tx control: owns · Transaction control is scoped to the session lifecycle
- Statement → Tags: uses metadata · Statement generation depends on mapped struct/table metadata
- Statement → Types: formats · Statement assembly respects mapped types and null semantics
- Tags → Helpers: interprets · Tag parsing relies on reflection and mapping helpers
- Types → Convert: converts through · Type metadata feeds value conversion logic
- Convert → Rows: decodes · Converted values are assigned during row scanning
- Statement → MySQL dialect: adapts to · Dialect implementations supply database-specific SQL behavior
- Statement → Postgres dialect: adapts to · Dialect implementations supply database-specific SQL behavior
- Statement → SQLite dialect: adapts to · Dialect implementations supply database-specific SQL behavior
- Statement → MSSQL dialect: adapts to · Dialect implementations supply database-specific SQL behavior
- Statement → Oracle dialect: adapts to · Dialect implementations supply database-specific SQL behavior
- EngineGroup → database/sql drivers: delegates · Routed engines still execute through standard database/sql drivers
- Engine → database/sql drivers: delegates · All connectivity ultimately reaches database/sql drivers
- Rows → database/sql drivers: reads from · Row iteration consumes driver-provided result sets
- Migrate → Statement: emits · Schema sync uses SQL generation infrastructure
- Context cache → Rows: short-circuits · Cached query results bypass repeated database reads
