Principle:APIJSON APIJSON Multi Database Support
| Knowledge Sources | |
|---|---|
| Domains | ORM, Multi_Database, SQL_Generation |
| Last Updated | 2026-10-11 00:00 GMT |
Overview
A database abstraction pattern that supports 30+ database engines by detecting the target dialect from a request directive and generating SQL with the correct quoting, pagination, and upsert syntax for each engine.
Description
Multi-Database Support allows a single APIJSON instance to serve requests targeting different database engines. The client specifies the target database via the @database directive (e.g., "@database": "POSTGRESQL"). The SQL generator then uses dialect-specific syntax for:
- Column and table quoting: MySQL uses backticks, PostgreSQL/Oracle use double quotes
- Pagination: MySQL/PostgreSQL use LIMIT/OFFSET, SQL Server/Oracle use OFFSET ... ROWS FETCH NEXT ... ROWS ONLY
- UPSERT: MySQL uses ON DUPLICATE KEY UPDATE, PostgreSQL uses ON CONFLICT DO UPDATE, Oracle/SQL Server/DB2 use MERGE INTO
- Update/delete syntax: ClickHouse uses ALTER TABLE ... UPDATE/DELETE instead of standard UPDATE/DELETE
- Dialect families: Databases are grouped into MySQL-family (isMSQL()), PostgreSQL-family (isPSQL()), and T-SQL-family (isTSQL()) based on SQL syntax compatibility
Supported databases include: MySQL, PostgreSQL, Oracle, SQL Server, DB2, ClickHouse, MongoDB, Redis, Kafka, TiDB, MariaDB, SQLite, TDengine, CockroachDB, openGauss, InfluxDB, TimescaleDB, QuestDB, DuckDB, Dameng, KingbaseES, Milvus, and more.
The problem solved is avoiding database lock-in. Applications can switch databases by changing the @database directive without modifying code, and a single deployment can serve multiple databases.
Usage
Use this principle when:
- An application needs to support multiple database backends
- Different clients or tenants use different databases
- Database portability is a requirement
Theoretical Basis
# Abstract algorithm (NOT real implementation):
def get_dialect(database_name):
if database_name in MYSQL_FAMILY:
return MySQLDialect(quote="`", limit="LIMIT %d OFFSET %d")
elif database_name in POSTGRESQL_FAMILY:
return PostgreSQLDialect(quote='"', limit="LIMIT %d OFFSET %d")
elif database_name in TSQL_FAMILY:
return TSQLDialect(quote='"', limit="OFFSET %d ROWS FETCH NEXT %d ROWS ONLY")
def generate_upsert(table, columns, values, dialect):
if dialect.is_mysql():
return f"INSERT INTO {table} ... ON DUPLICATE KEY UPDATE ..."
elif dialect.is_postgresql():
return f"INSERT INTO {table} ... ON CONFLICT DO UPDATE SET ..."
elif dialect.is_tsql():
return f"MERGE INTO {table} ... WHEN MATCHED THEN UPDATE ..."
Related Pages
Implemented By
- Implementation:APIJSON_APIJSON_AbstractSQLConfig_Dialect_Selection
- Implementation:APIJSON_APIJSON_AbstractSQLConfig_gainSQL