Implementation:APIJSON APIJSON AbstractSQLExecutor execute
| Knowledge Sources | |
|---|---|
| Domains | ORM, SQL_Execution, JDBC |
| Last Updated | 2026-10-11 00:00 GMT |
Overview
Concrete JDBC executor that takes a populated SQLConfig, generates SQL, executes it against the database, and transforms the ResultSet into JSON maps, provided by the APIJSON ORM framework.
Description
The execute method on AbstractSQLExecutor is the bridge between SQL generation and database execution. It:
- Generates the SQL string via config.gainSQL(false)
- Determines if the operation is a query (SELECT) or update (POST/PUT/DELETE)
- For SELECT: executes a query, iterates the ResultSet, converts each row to a Map<String, Object>, and handles @raw and @json column modifiers
- For UPDATE: executes an update, returns the affected row count
- Executes APP JOINs (executeAppJoin) for application-level batch joins after the main query
- Handles vice-table (JOIN) result merging
- Caches generated/executed SQL counts for diagnostics
The method supports both unknownType execution (generic Statement for unknown database types) and typed execution (PreparedStatement for known types with prepared-statement parameter binding).
Usage
Called internally by ObjectParser.executeSQL() after setSQLConfig() has populated the SQLConfig. Override in a subclass to customize JDBC behavior, such as using a connection pool or adding query timeouts.
Code Reference
Source Location
- Repository: APIJSON
- File: src/main/java/apijson/orm/AbstractSQLExecutor.java
Signature
public M execute(@NotNull SQLConfig<T, M, L> config, boolean unknownType) throws Exception
Parameters:
- config - Fully populated SQLConfig with generated SQL, table, method, database connection info
- unknownType - Whether the database type is unknown (uses generic Statement instead of PreparedStatement)
Returns: M (Map<String, Object>) - For SELECT: query rows with column data; for INSERT/UPDATE/DELETE: affected count and id
Import
import apijson.orm.AbstractSQLExecutor;
I/O Contract
Inputs
| Name | Type | Required | Description |
|---|---|---|---|
| config | SQLConfig<T,M,L> | Yes | Config with SQL string, method, database info |
| unknownType | boolean | Yes | Whether database type is unknown |
Outputs
| Name | Type | Description |
|---|---|---|
| return (SELECT) | M (Map<String,Object>) | Query result with @RAW@LIST and column data |
| return (UPDATE) | M (Map<String,Object>) | Result with count (affected rows) and id |
| generatedSQLCount | int | Incremented for each SQL generated |
| executedSQLCount | int | Incremented for each SQL executed |
Usage Examples
Standard Query Execution
// After ObjectParser sets up SQLConfig:
// config.setMethod(RequestMethod.GET);
// config.setTable("Moment");
// config.putWhere("userId", 82001, true);
M result = executor.execute(config, false);
// result: { "id": 1, "userId": 82001, "content": "Hello", ... }
Update Execution
config.setMethod(RequestMethod.PUT);
config.setTable("Comment");
config.putWhere("id", 123, true);
config.putWhereSet("content", "Updated");
M result = executor.execute(config, false);
// result: { "code": 200, "count": 1 } // 1 row affected
APP JOIN Execution
// After main query, executeAppJoin is called for @-type joins:
// executeAppJoin(config, resultList, childMap, keyMap)
// Executes: SELECT * FROM User WHERE id IN (1, 2, 3)
// Merges child results into main table response maps