How go-xorm/xorm works
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 Statement · session.go Session · engine.go Engine · rows.go Rows
Components 19
Core API
Public façade and session lifecycle
- Engine · Top-level ORM entrypoint for creating sessions, configuring connectivity, and exposing CRUD/query APIs
engine.go - Session · Chainable query and execution state for one database operation
session.go - Tx control · Begin/commit/rollback lifecycle bound to a session
session_tx.go - Processors · Pluggable pre/post execution processing around database actions
processors.go
SQL Assembly
Statement building and query execution
Mapping
Struct metadata and value conversion
- Tags · Reads struct tags and model metadata for table and field mapping
tag.go - Types · Represents mapped field types and nullability semantics
types.go - Convert · Converts between database values and Go representations
convert.go - Helpers · Shared reflection and mapping utilities used by metadata and session logic
helpers.go
Database Adaptation
Dialect, routing, caching, and schema management
- EngineGroup · Read/write splitting and multi-engine routing boundary
engine_group.go - MySQL dialect · MySQL-specific quoting, placeholders, and paging behavior
dialect_mysql.go - Postgres dialect · PostgreSQL-specific SQL syntax and placeholder behavior
dialect_postgres.go - SQLite dialect · SQLite-specific SQL syntax and paging behavior
dialect_sqlite3.go - MSSQL dialect · SQL Server-specific SQL syntax and paging behavior
dialect_mssql.go - Oracle dialect · Oracle-specific SQL syntax and paging behavior
dialect_oracle.go - Context cache · Session-local and shared cache layer for repeated reads
context_cache.go - Migrate · Schema synchronization and table evolution support
migrate/migrate.go
Other components
- database/sql drivers · Underlying Go driver layer for MySQL, Postgres, SQLite, SQL Server, Oracle, and TiDB
Connections 24
- 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