Jump to content

Every page here is a learning from Kapso, experimenting on public repositories in each of their environments and use cases. Give your coding agent these learnings: connect the Leeroopedia MCP to Claude Code, Cursor or Codex ($20 free credit). Powered by Leeroo · founders@leeroo.com

Principle:APIJSON APIJSON Prepared Statements

From Leeroopedia
Knowledge Sources
Domains ORM, SQL_Generation, Security
Last Updated 2026-10-11 00:00 GMT

Overview

A SQL injection prevention pattern that separates SQL structure from parameter values by using ? placeholders in the generated SQL and binding values separately via JDBC PreparedStatement, ensuring that user input is always treated as data, not executable code.

Description

Prepared Statements in APIJSON are controlled by the isPrepared flag on SQLConfig. When enabled:

  1. The SQL generator replaces literal values with ? placeholders (e.g., WHERE id = ? instead of WHERE id = 82001)
  2. Parameter values are collected into preparedValueList in the order they appear in the SQL
  3. The SQL executor binds values to a PreparedStatement using setString, setLong, setInt, etc.

This prevents SQL injection because the database engine parses the SQL structure before any parameter values are bound. Even if a parameter contains SQL syntax (e.g., "1; DROP TABLE Users"), it is treated as a literal string value, not as executable SQL.

The gainSQL(boolean prepared) method generates SQL in either prepared or non-prepared mode. When prepared mode is active, the WHERE clause builder, HAVING clause builder, and value generators all produce ? placeholders and collect values into the prepared list.

Usage

Use this principle when:

  • Any user input is incorporated into SQL queries (which is always the case in APIJSON)
  • SQL injection prevention is a security requirement
  • Performance benefits from query plan caching are desired

Theoretical Basis

# Abstract algorithm (NOT real implementation):
def generate_sql_prepared(config):
    sql_parts = []
    prepared_values = []
    
    for key, value in config.where.items():
        column = parse_column(key)
        operator = parse_operator(key)
        
        sql_parts.append(f"{quote(column)} {operator} ?")
        prepared_values.append(value)
    
    sql = f"SELECT ... WHERE {' AND '.join(sql_parts)}"
    return sql, prepared_values

def execute_prepared(sql, prepared_values, connection):
    stmt = connection.prepareStatement(sql)
    for i, value in enumerate(prepared_values):
        if isinstance(value, int):
            stmt.setInt(i + 1, value)
        elif isinstance(value, str):
            stmt.setString(i + 1, value)
        # ... etc.
    return stmt.executeQuery()

Related Pages

Implemented By

Page Connections

Double-click a node to navigate. Hold to expand connections.
Principle
Implementation
Heuristic
Environment