Principle:APIJSON APIJSON Demo Dynamic SQL Configuration
| Knowledge Sources | |
|---|---|
| Domains | Database, ORM, Configuration |
| Last Updated | 2026-10-14 00:00 GMT |
Overview
Configuration pattern that generates database-specific SQL statements (SELECT, INSERT, UPDATE, DELETE) from parsed JSON request parameters, with support for multiple database dialects, raw SQL fragments, column mapping, and per-table access policies.
Description
Dynamic SQL Configuration is the bridge between the APIJSON parser and the SQL executor. A SQLConfig instance encapsulates all information needed to generate a SQL statement: the HTTP method, target table, selected columns, WHERE conditions, JOIN relations, ORDER BY, GROUP BY, and pagination. The config also holds database connection metadata (URI, account, password, version) and handles dialect-specific SQL generation.
This solves:
- SQL injection: By generating SQL from structured parameters (not string concatenation), the framework uses prepared statements and parameter binding.
- Database portability: A single config object can generate SQL for MySQL, PostgreSQL, Oracle, and other dialects.
- Column aliasing: Fields in JSON requests can be mapped to different database column names (e.g.,
gender→sex) via static configuration. - Raw SQL fragments: For complex queries beyond the JSON syntax,
RAW_MAPallows registering safe SQL fragments that clients can reference by key.
Usage
Use this principle when:
- Building an ORM-like layer that generates SQL from structured parameters rather than hand-written queries.
- Supporting multiple database dialects from a single configuration object.
- You need column name mapping between API and database schema.
Theoretical Basis
SQLConfig holds:
- method: RequestMethod (GET/POST/PUT/DELETE)
- table: String (target table)
- database: String (database type: MYSQL, POSTGRESQL, etc.)
- datasource: String (datasource name)
- columns: List<String> (selected columns)
- conditions: Map<String, Object> (WHERE clauses)
- joins: List<Join> (JOIN relations)
- orderBy, groupBy, having, limit, offset
SQL Generation:
config.gainSQL() → dialect-specific SQL string
config.gainDBVersion() → "8.0.11" (for JDBC compatibility)
config.gainDBUri() → "jdbc:mysql://localhost:3306"
config.gainDBAccount() → "root"
config.gainDBPassword() → "****"
Column mapping example:
// API field "gender" maps to database column "sex"
Map<String, String> userKeyColumnMap = new HashMap<>();
userKeyColumnMap.put("gender", "sex");
userKeyColumnMap.put("createTime", "date");
tableKeyColumnMap.put("User", userKeyColumnMap);
ColumnUtil.VERSIONED_KEY_COLUMN_MAP.put(null, tableKeyColumnMap);
Raw SQL fragment registration:
// Register safe SQL fragments clients can reference by key
RAW_MAP.put("sumUserIdIsEven", "sum(if(`userId`%2=0,1,0)) AS sumUserIdIsEven");
RAW_MAP.put("to.momentId", "`to`.`momentId`");