Implementation:APIJSON APIJSON Demo DemoSQLConfig
| Knowledge Sources | |
|---|---|
| Domains | Database, ORM, Configuration |
| Last Updated | 2026-10-14 00:00 GMT |
Overview
Concrete tool for generating database-specific SQL configurations including connection metadata, column mapping, raw SQL fragments, and dialect-specific connection settings provided by the APIJSON framework.
Description
The DemoSQLConfig class extends APIJSONSQLConfig<Long> and is the central configuration for SQL generation in the APIJSON Demo. It defines:
- Default database:
DATABASE_MYSQLwith schema"sys" - SimpleCallback factory: Creates
DemoSQLConfiginstances for each parsed table, with custom user ID key mapping - Database connection metadata:
gainDBVersion(),gainDBUri(),gainDBAccount(),gainDBPassword()return dialect-specific values - RAW_MAP: Registered safe SQL fragments that clients can reference via the
@rawkeyword - Column mapping:
ColumnUtil.VERSIONED_TABLE_COLUMN_MAPandVERSIONED_KEY_COLUMN_MAPmap API field names to database column names
The static initializer configures all of the above at class load time, and the SimpleCallback ensures each request gets a properly configured DemoSQLConfig instance.
Usage
- Standard operation: Automatically instantiated by the framework via
SimpleCallback.getSQLConfig()during request parsing. - Changing database type: Modify
DEFAULT_DATABASEin the static block (e.g., toDATABASE_POSTGRESQL). - Adding raw SQL fragments: Add entries to
RAW_MAPin the static block. - Column aliasing: Add entries to
ColumnUtil.VERSIONED_KEY_COLUMN_MAPfor field-to-column mapping.
Code Reference
Source Location
- Repository: APIJSON-Demo
- File: APIJSON-Java-Server/APIJSONBoot-MultiDataSource/src/main/java/apijson/demo/DemoSQLConfig.java
Signature
public class DemoSQLConfig extends APIJSONSQLConfig<Long> {
public DemoSQLConfig();
public DemoSQLConfig(RequestMethod method, String table);
static {
DEFAULT_DATABASE = DATABASE_MYSQL;
DEFAULT_SCHEMA = "sys";
SIMPLE_CALLBACK = new SimpleCallback<Long>() {
@Override
public AbstractSQLConfig<Long, JSONObject, JSONArray> getSQLConfig(
RequestMethod method, String database, String datasource,
String namespace, String catalog, String schema, String table
) {
return new DemoSQLConfig(method, table);
}
@Override
public String getUserIdKey(String database, String datasource,
String namespace, String catalog, String schema, String table) {
return USER_.equals(table) || PRIVACY_.equals(table) ? ID : USER_ID;
}
};
// Raw SQL fragments
RAW_MAP.put("`to`.`id`", "");
RAW_MAP.put("toDate", "");
RAW_MAP.put("sumUserIdIsEven", "sum(if(`userId`%2=0,1,0)) AS sumUserIdIsEven");
// ... many more entries
}
@Override
public String gainDBVersion(); // Returns "8.0.11" for MySQL, etc.
@Override
public String gainDBUri(); // Returns JDBC URL per database type
@Override
public String gainDBAccount(); // Returns "root" for MySQL, etc.
@Override
public String gainDBPassword(); // Returns password per database type
}
Import
import apijson.demo.DemoSQLConfig;
I/O Contract
Inputs
| Name | Type | Required | Description |
|---|---|---|---|
| method | RequestMethod | Yes | HTTP method for SQL type (GET→SELECT, POST→INSERT, etc.) |
| table | String | Yes | Target database table name |
| request | JSONObject | No | Parsed JSON request (columns, conditions, etc.) |
Outputs
| Name | Type | Description |
|---|---|---|
| gainSQL() | String | Generated SQL statement string |
| gainDBVersion() | String | Database version (e.g., "8.0.11") |
| gainDBUri() | String | JDBC connection URI |
| gainDBAccount() | String | Database username |
| gainDBPassword() | String | Database password |
Usage Examples
Changing Default Database to PostgreSQL
static {
DEFAULT_DATABASE = DATABASE_POSTGRESQL;
DEFAULT_SCHEMA = "public";
// ... rest of config
}
Adding a Raw SQL Fragment
static {
// Register a safe SQL fragment clients can use via @raw
RAW_MAP.put("userCount", "COUNT(*) AS userCount");
// Now clients can use: { "User": { "@column": "userCount", "@raw": "@column" } }
}
Adding Column Mapping
static {
Map<String, Map<String, String>> tableKeyColumnMap = new HashMap<>();
Map<String, String> userKeyColumnMap = new HashMap<>();
userKeyColumnMap.put("gender", "sex"); // API "gender" → DB "sex"
userKeyColumnMap.put("createTime", "date"); // API "createTime" → DB "date"
tableKeyColumnMap.put("User", userKeyColumnMap);
ColumnUtil.VERSIONED_KEY_COLUMN_MAP.put(null, tableKeyColumnMap);
ColumnUtil.init();
}
Related Pages
Implements Principle
- Principle:APIJSON_APIJSON_Demo_Dynamic_SQL_Configuration
- Principle:APIJSON_APIJSON_Demo_Role_Based_Access_Control
- Principle:APIJSON_APIJSON_Demo_JSON_To_SQL_Translation