Principle:APIJSON APIJSON Conditional Querying
| Knowledge Sources | |
|---|---|
| Domains | ORM, Query_DSL, SQL_Generation |
| Last Updated | 2026-10-11 00:00 GMT |
Overview
A query DSL pattern that expresses SQL WHERE clause conditions through key-suffix operators and logical prefixes, allowing clients to specify comparisons, ranges, pattern matching, and boolean combinations declaratively.
Description
Conditional Querying in APIJSON uses key suffixes to encode SQL operators and key prefixes to encode logical connectors. This creates a compact, human-readable DSL that maps directly to SQL WHERE clauses:
Operator suffixes:
- key: value → key = value (equality)
- key>: value → key > value (greater than)
- key<: value → key < value (less than)
- key>=: value → key >= value
- key<=: value → key <= value
- key!=: value → key != value
- key{}: [1,2,3] → key IN (1,2,3) (IN clause)
- key}{': [1,2] → key BETWEEN 1 AND 2 (range)
- key~: "Tom" → key LIKE 'Tom' (pattern match)
- key$: "a" → key LIKE '%a%' (contains)
- key?%: "a" → key LIKE 'a%' (starts with)
Logical prefixes:
- key (no prefix) → default AND
- &key → explicit AND
- |key → OR
- !key → NOT
Complex grouping via @combine:
- "@combine": "(name~ | age>) & status" → WHERE (name LIKE ... OR age > ...) AND status = ...
The problem solved is allowing clients to express complex query conditions without a separate query language (like GraphQL or OData). The key-suffix approach keeps the JSON format compact while supporting the full range of SQL WHERE operators.
Usage
Use this principle when designing an API that needs flexible querying capabilities beyond simple equality checks. The suffix-based DSL is particularly effective for REST-like APIs that use JSON request bodies.
Theoretical Basis
# Abstract algorithm (NOT real implementation):
def build_where_clause(conditions, combine):
parts = []
for key, value in conditions.items():
# Extract logical prefix
logic = Logic(key) # & | ! prefix
clean_key = logic.key
# Extract operator suffix
operator = parse_operator_suffix(clean_key) # >, <, {}, ~, etc.
column = clean_key_without_suffix
# Generate SQL condition
if operator == "=":
parts.append(f"{quote(column)} = {param(value)}")
elif operator == ">":
parts.append(f"{quote(column)} > {param(value)}")
elif operator == "IN":
parts.append(f"{quote(column)} IN ({param_list(value)})")
elif operator == "LIKE":
parts.append(f"{quote(column)} LIKE {param(value)}")
# ... etc.
if combine:
return apply_combine_expression(parts, combine)
else:
return " AND ".join(parts)
Related Pages
Implemented By
- Implementation:APIJSON_APIJSON_Logic_Class
- Implementation:APIJSON_APIJSON_AbstractSQLConfig_Where_Clause_Construction