Implementation:APIJSON APIJSON AbstractSQLConfig gainSQL
| Knowledge Sources | |
|---|---|
| Domains | ORM, SQL_Generation, Multi_Database |
| Last Updated | 2026-10-11 00:00 GMT |
Overview
Concrete SQL generator that translates a populated SQLConfig into a dialect-specific SQL string (SELECT, INSERT, UPDATE, DELETE) with prepared-statement support, provided by the APIJSON ORM framework.
Description
The gainSQL method on AbstractSQLConfig is the central SQL generation entry point. It dispatches based on the request method:
- POST → gainUpsertSQL() (INSERT or INSERT ... ON DUPLICATE KEY UPDATE / MERGE)
- PUT → gainUpdateSQL() (UPDATE)
- DELETE → gainDeleteSQL() (DELETE, or fake-delete UPDATE if isFakeDelete() returns true)
- GET/HEAD → gainSelectSQL() (SELECT)
The static overload clears the prepared value list, resolves the table path, and delegates to the appropriate method-specific generator. Each generator assembles dialect-specific SQL by combining column strings, WHERE clauses, JOIN clauses, GROUP BY, HAVING, ORDER BY, and LIMIT/OFFSET.
The method supports prepared statements: when isPrepared == true, values are replaced with ? placeholders and collected into preparedValueList for later binding via PreparedStatement.setX() calls.
Usage
Called internally by AbstractSQLExecutor.execute() to generate the SQL string before execution. Can also be called directly to inspect the generated SQL. Set all relevant config fields (table, method, columns, where, group, having, order, joinList, page, count) before calling.
Code Reference
Source Location
- Repository: APIJSON
- File: src/main/java/apijson/orm/AbstractSQLConfig.java
Signature
// Instance method - generates SQL with current prepared setting
public String gainSQL(boolean prepared) throws Exception
// Static method - generates SQL from any config
public static <T, M extends Map<String, Object>, L extends List<Object>> String gainSQL(
AbstractSQLConfig<T, M, L> config
) throws Exception
Parameters (instance):
- prepared - Whether to generate prepared-statement SQL with ? placeholders
Parameters (static):
- config - Fully populated SQLConfig instance
Returns: String - Complete SQL string
Import
import apijson.orm.AbstractSQLConfig;
I/O Contract
Inputs
| Name | Type | Required | Description |
|---|---|---|---|
| config | AbstractSQLConfig | Yes | Config with table, method, columns, where, group, having, order, joinList |
| prepared | boolean | Yes | Whether to use prepared-statement placeholders |
| table | String | Yes | Table name |
| method | RequestMethod | Yes | HTTP method (GET/POST/PUT/DELETE) |
| column | List<String> | No | Column list from @column directive |
| where | Map<String,Object> | No | WHERE conditions |
| joinList | List<Join> | No │ JOIN configurations | |
| group | String | No | GROUP BY columns |
| having | Map<String,Object> | No | HAVING conditions |
| order | String | No | ORDER BY specification |
| page | int | No | Page number (0-based) |
| count | int | No | Items per page |
Outputs
| Name | Type | Description |
|---|---|---|
| return | String | Complete SQL string (SELECT/INSERT/UPDATE/DELETE) |
| preparedValueList | List<Object> | Ordered parameter values for prepared statement |
Usage Examples
SELECT with Prepared Statement
SQLConfig<Long, JSONObject, JSONArray> config = new SQLConfig<>();
config.setMethod(RequestMethod.GET);
config.setTable("Moment");
config.setColumn(Arrays.asList("id", "content"));
config.putWhere("userId", 82001, true);
config.setPage(0);
config.setCount(10);
String sql = config.gainSQL(true);
// Generated: SELECT `id`,`content` FROM `Moment` WHERE `userId` = ? LIMIT 10 OFFSET 0
// preparedValueList: [82001]
UPDATE with WHERE
config.setMethod(RequestMethod.PUT);
config.setTable("Comment");
config.putWhere("id", 123, true);
config.putWhereSet("content", "Updated text");
String sql = config.gainSQL(false);
// Generated: UPDATE `Comment` SET `content` = 'Updated text' WHERE `id` = 123
DELETE (Fake-Delete)
// If table is configured for fake-delete:
// isFakeDelete() returns true, method is changed to PUT internally
config.setMethod(RequestMethod.DELETE);
config.setTable("Moment");
config.putWhere("id", 456, true);
String sql = config.gainSQL(false);
// Generated: UPDATE `Moment` SET `isDeleted` = 1 WHERE `id` = 456
// (instead of DELETE FROM `Moment` WHERE `id` = 456)
Related Pages
Implements Principle
- Principle:APIJSON_APIJSON_SQL_Generation
- Principle:APIJSON_APIJSON_Fake_Delete
- Principle:APIJSON_APIJSON_Prepared_Statements
- Principle:APIJSON_APIJSON_Multi_Database_Support