How Chat2DB Manages Different Database Types: Architecture and Implementation
Chat2DB manages different database types through a pluggable architecture built on the DatabaseTypeEnum enumeration and the ISqlSyntaxPlugin interface, enabling support for MySQL, PostgreSQL, Oracle, SQL Server, and 20+ other engines through dedicated syntax plugins.
Supporting heterogeneous database environments requires flexible abstraction layers that decouple dialect-specific logic from core operations. The open-source Chat2DB project (OtterMind/Chat2DB) achieves this through a modular design centered on runtime plugin resolution. This article explores the technical implementation of how Chat2DB manages different database types using its registry-based architecture and specialized connection handling.
Central Database Type Registry
DatabaseTypeEnum Definition
All supported database identifiers are centralized in the DatabaseTypeEnum enumeration. This enum serves as the canonical reference for database types across the application, preventing string-based errors and providing type safety for database operations.
ISqlSyntaxPlugin Interface
Each database type implements the ISqlSyntaxPlugin interface to provide dialect-specific parsing capabilities. Concrete implementations include:
- MySQL –
MySQLSyntaxPlugin(default) - PostgreSQL –
PgsqlSyntaxPluginlocated atchat2db-community-plugins/chat2db-community-postgresql/src/main/java/ai/chat2db/plugin/postgresql/PgsqlSyntaxPlugin.java - Oracle –
OracleSyntaxPluginlocated atchat2db-community-plugins/chat2db-community-oracle/src/main/java/ai/chat2db/plugin/oracle/OracleSyntaxPlugin.java - SQL Server –
SqlServerSyntaxPluginlocated atchat2db-community-plugins/chat2db-community-sqlserver/src/main/java/ai/chat2db/plugin/sqlserver/SqlServerSyntaxPlugin.java - SQLite –
SqliteSyntaxPluginlocated atchat2db-community-plugins/chat2db-community-sqlite/src/main/java/ai/chat2db/plugin/sqlite/SqliteSyntaxPlugin.java
Each plugin implements getDatabaseType() to return the corresponding enum constant, enabling the system to match implementations to database types at runtime.
Runtime Plugin Resolution
The DefaultSqlSyntaxHandler class acts as the central dispatcher for database-specific syntax operations. Located at chat2db-community-spi/src/main/java/ai/chat2db/spi/DefaultSqlSyntaxHandler.java, it implements resolution logic through the getSQLParser(DatabaseTypeEnum) method.
Resolution workflow:
-
Alias Normalization – The handler normalizes input strings via
resolvePluginKey(), mapping aliases likeOSCARto canonical types such asORACLE. -
Non-SQL Fallback – For engines like Redis and MongoDB, the method falls back to the MySQL parser to provide basic compatibility while delegating actual operations to specialized handlers.
-
Plugin Retrieval – The resolved key retrieves the concrete implementation from
sqlSyntaxPluginMap, ensuring the correct dialect rules apply to SQL parsing and generation.
Database-Specific Connection Management
The ConnectionPool class at chat2db-community-spi/src/main/java/ai/chat2db/spi/sql/ConnectionPool.java manages physical connections with database-specific semantics. When creating connections, it inspects ConnectInfo.dbType to apply specialized handling for engines requiring different drivers or connection patterns, including Hive, Presto, SUNDB, MongoDB, and Redis.
SQL Dialect Utilities
Helper classes SqlStringUtil and SqlUtils utilize the DatabaseTypeEnum to apply database-specific quoting and escaping rules. This ensures generated SQL statements use correct identifier delimiters and literal escaping for the target engine, preventing syntax errors across different implementations.
Extending Support for New Database Types
Adding support for additional database engines follows a consistent implementation pattern:
- Define the Type – Add a new constant to
DatabaseTypeEnumin the API module. - Implement the Plugin – Create a class implementing
ISqlSyntaxPluginwithgetDatabaseType()returning the new enum value. - Register the Component – Ensure the plugin is registered in
sqlSyntaxPluginMapaccessed byDefaultSqlSyntaxHandler. - Configure Connections – Update
ConnectionPoollogic if the database requires specialized driver handling or custom connection semantics.
Practical Code Examples
The following example demonstrates parsing SQL statements for a specific database type:
String sql = "SELECT * FROM users";
DatabaseTypeEnum dbType = DatabaseTypeEnum.POSTGRESQL;
SqlParserResponse response = DefaultSqlSyntaxHandler.parserStatements(sql, dbType);
System.out.println(response.getStatements().size());
Creating connections with database-specific driver selection:
ConnectInfo connectInfo = new ConnectInfo();
connectInfo.setDbType(DatabaseTypeEnum.MYSQL.name());
connectInfo.setHost("localhost");
connectInfo.setPort(3306);
connectInfo.setUsername("root");
connectInfo.setPassword("password");
// ConnectionPool automatically selects the MySQL driver based on dbType
Connection connection = ConnectionPool.getConnection(connectInfo);
Summary
- Chat2DB uses
DatabaseTypeEnumas the central registry for supported database types atchat2db-community-domain/chat2db-community-domain-api/src/main/java/ai/chat2db/community/domain/api/enums/parser/DatabaseTypeEnum.java. - The
ISqlSyntaxPlugininterface enables pluggable syntax handling, with implementations located in thechat2db-community-pluginsmodule. DefaultSqlSyntaxHandlerresolves plugins at runtime viagetSQLParser(), supporting alias mapping and non-SQL fallbacks.ConnectionPoolapplies database-specific driver logic based on theConnectInfo.dbTypeenum value.- New database support requires implementing the plugin interface and registering the component in the handler's plugin map.
Frequently Asked Questions
What databases does Chat2DB currently support?
Chat2DB supports MySQL, PostgreSQL, Oracle, SQL Server, SQLite, TiDB, TDengine, Redshift, Presto, Hive, MongoDB, Redis, and others. Each database has a dedicated plugin implementing ISqlSyntaxPlugin located in the chat2db-community-plugins directory, with the specific path following the pattern chat2db-community-[databasename]/src/main/java/ai/chat2db/plugin/[databasename]/[Name]SyntaxPlugin.java.
How does Chat2DB handle database-specific SQL syntax differences?
The DefaultSqlSyntaxHandler class resolves the appropriate ISqlSyntaxPlugin implementation based on the DatabaseTypeEnum value passed to getSQLParser(). Each plugin contains dialect-specific parsing logic, while utility classes like SqlStringUtil handle quoting and escaping rules specific to each database type.
Can developers add support for custom or proprietary databases?
Yes. Developers must extend DatabaseTypeEnum with their database identifier, create a class implementing ISqlSyntaxPlugin with custom parsing logic in the getDatabaseType() method, and register the plugin in the sqlSyntaxPluginMap used by DefaultSqlSyntaxHandler. If the database requires specialized JDBC drivers or connection semantics, the ConnectionPool class at chat2db-community-spi/src/main/java/ai/chat2db/spi/sql/ConnectionPool.java may also require updates.
Why does Chat2DB use the MySQL parser for Redis and MongoDB?
Non-SQL engines like Redis and MongoDB lack traditional SQL parsing requirements. The getSQLParser() method in DefaultSqlSyntaxHandler defaults to the MySQL parser for these types to provide basic statement structure compatibility, while actual data operations are delegated to database-specific connection handlers that understand the respective wire protocols.
Have a question about this repo?
These articles cover the highlights, but your codebase questions are specific. Give your agent direct access to the source. Share this with your agent to get started:
curl -s "https://instagit.com/install.md" Maintain an open-source project? Get it listed too →