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

Heuristic:APIJSON APIJSON Operator Suffix Mapping

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

Overview

Key-suffix-to-SQL-operator mapping that lets APIJSON clients express complex WHERE conditions (LIKE, IN, BETWEEN, regex, comparisons) by appending suffix characters to column names in the request JSON.

Description

APIJSON uses a convention where the suffix appended to a key name determines the SQL operator applied to that key's value. This creates a compact, human-readable query DSL embedded in the JSON request structure. The mapping is implemented in AbstractSQLConfig.getWhereItem() via a keyType integer switch.

The suffix-to-operator mapping:

Suffix keyType SQL Operator Example Generated SQL
(none) 0 = "name":"Tom" name = 'Tom
$ 1 LIKE "name$":"%om" name LIKE '%om
~ 2 REGEXP "name~":"^T" name REGEXP '^T
*~ -2 NOT REGEXP "name*~":"^T" name NOT REGEXP '^T
% 3 BETWEEN "date%":"2020,2021" date BETWEEN 2020 AND 2021
{} 4 IN "id{}":[1,2,3] id IN (1, 2, 3)
}{ 5 EXISTS "id}{":">5" EXISTS (SELECT ...)
<> 6 CONTAINS "praiseUserIdList<>":82001 JSON_CONTAINS(...)
>= 7 >= "count>=":10 count >= 10
<= 8 <= "count<=":100 count <= 100
> 9 > "count>":5 count > 5
< 10 < "count<":50 count < 50

Additionally, column-name suffixes control function wrapping:

  • [ suffix → length() (or datalength() for SQL Server)
  • { suffix → json_length()

And the ! suffix on a key negates the operator (e.g., name!:Tom → name != 'Tom').

Usage

Use this heuristic when:

  • Constructing APIJSON requests with complex WHERE conditions
  • Implementing custom operator suffixes
  • Debugging SQL generation for unexpected query results

The Insight (Rule of Thumb)

  • Action: Append the appropriate suffix character(s) to the key name in the request JSON.
  • Value: See the mapping table above. Order matters: *~ (NOT REGEXP) is checked before ~ (REGEXP); {} (IN) before }{ (EXISTS).
  • Trade-off: The suffix DSL is compact but can be confusing for new users; document the mapping clearly.
  • Compatibility: The <> (CONTAINS) operator uses JSON_CONTAINS() on MySQL and may behave differently on other databases.

Reasoning

Embedding operators in key suffixes keeps the request JSON flat and avoids the need for a separate query expression syntax. The endsWith() chain in getWhereItem() checks longer suffixes first implicitly (since {} and }{ are both two characters, but >= is checked before >). This ordering ensures the most specific suffix matches first.

The ! (negation) suffix is stripped from the column name after the operator is determined, so it works with any operator: id{}!: [1,2] becomes id NOT IN (1, 2).

Code Evidence

Suffix detection in `AbstractSQLConfig.java:3965-3996`:

if (key.endsWith("$")) {
    keyType = 1;
}
else if (key.endsWith("~")) {
    keyType = key.charAt(key.length() - 2) == '*' ? -2 : 2;
}
else if (key.endsWith("%")) {
    keyType = 3;
}
else if (key.endsWith("{}")) {
    keyType = 4;
}
else if (key.endsWith("}{")) {
    keyType = 5;
}
else if (key.endsWith("<>")) {
    keyType = 6;
}
else if (key.endsWith(">=")) {
    keyType = 7;
}
else if (key.endsWith("<=")) {
    keyType = 8;
}
else if (key.endsWith(">")) {
    keyType = 9;
}
else if (key.endsWith("<")) {
    keyType = 10;
} else {
    keyType = 0;
}

Operator dispatch in `AbstractSQLConfig.java:4003-4027`:

switch (keyType) {
case 1: return gainSearchString(key, column, value, rawSQL);
case -2:
case 2: return gainRegExpString(key, column, value, keyType < 0, rawSQL);
case 3: return gainBetweenString(key, column, value, rawSQL);
case 4: return gainRangeString(key, column, value, rawSQL);
case 5: return gainExistsString(key, column, value, rawSQL);
case 6: return gainContainString(key, column, value, rawSQL);
case 7: return gainCompareString(key, column, value, ">=", rawSQL);
case 8: return gainCompareString(key, column, value, "<=", rawSQL);
case 9: return gainCompareString(key, column, value, ">", rawSQL);
case 10: return gainCompareString(key, column, value, "<", rawSQL);
default: return gainEqualString(key, column, value, rawSQL);
}

Related Pages

Page Connections

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