Chat2DB Data Import/Export Pipeline Architecture: Web to Strategy Pattern Deep Dive
Chat2DB implements a clean Web → Service → Factory → Strategy pipeline that separates HTTP handling, request conversion, business orchestration, and concrete format-specific logic to import and export SQL, CSV, Excel, and JSON files.
The open-source Chat2DB project (OtterMind/Chat2DB) provides a universal database client that requires robust, extensible data mobility across disparate formats. Its Chat2DB data import export pipeline architecture follows strict layered architecture principles, isolating web concerns from domain logic through a sophisticated factory-strategy pattern implementation that supports asynchronous execution and progress tracking.
Web Layer Controllers
The pipeline exposes two primary entry points in chat2db-community-web that handle HTTP multipart requests and delegate to the transfer service.
Import Operations are handled by TaskImportController at endpoints POST /api/task/import/sqlFile and POST /api/task/import/otherFile, accepting SqlFileImportRequest and OtherFileImportRequest DTOs respectively.
Export Operations are handled by TaskExportController at endpoints POST /api/task/export/sqlFile and POST /api/task/export/otherFile, accepting SqlFileExportRequest and OtherFileExportRequest.
Source: [chat2db-community-web/src/main/java/ai/chat2db/community/web/api/controller/TaskImportController.java](https://github.com/OtterMind/Chat2DB/blob/main/chat2db-community-server/chat2db-community-web/src/main/java/ai/chat2db/community/web/api/controller/TaskImportController.java) and [TaskExportController.java](https://github.com/OtterMind/Chat2DB/blob/main/chat2db-community-server/chat2db-community-web/src/main/java/ai/chat2db/community/web/api/controller/TaskExportController.java).
Request-to-Domain Conversion
Before reaching the core domain, TaskWebConverter maps web-layer DTOs to internal domain request models. This anti-corruption layer ensures the service tier remains agnostic of HTTP-specific constructs.
- Import conversion:
sqlFileImport2param()andotherFileImport2param()transform incoming import requests. - Export conversion:
sqlFileExport2param()andotherFileExport2param()handle export request mapping.
Source: [chat2db-community-web/src/main/java/ai/chat2db/community/web/api/converter/task/TaskWebConverter.java](https://github.com/OtterMind/Chat2DB/blob/main/chat2db-community-server/chat2db-community-web/src/main/java/ai/chat2db/community/web/api/converter/task/TaskWebConverter.java).
Transfer Service Orchestration
The ITaskTransferService interface, implemented by TaskTransferServiceImpl, serves as the central orchestrator for all data movement operations. This service coordinates between the conversion layer, factory selection, and execution strategies.
- Import methods:
importSqlFile()andimportOtherFile()initiate import workflows. - Export methods:
exportSqlFile()andexportOtherFile()handle export workflows.
The service obtains the appropriate strategy instance from the factories and delegates execution while managing ImportAsyncContext or ExportAsyncContext for progress reporting through ConsoleTaskProgressListener.
Source: [chat2db-community-domain-core/src/main/java/ai/chat2db/community/domain/core/impl/task/TaskTransferServiceImpl.java](https://github.com/OtterMind/Chat2DB/blob/main/chat2db-community-server/chat2db-community-domain/chat2db-community-domain-core/src/main/java/ai/chat2db/community/domain/core/impl/task/TaskTransferServiceImpl.java).
Factory Layer for Strategy Selection
Chat2DB employs the Factory Pattern to decouple strategy instantiation from business logic, mapping file extensions and enum types to concrete implementations.
ImportFactory resides in chat2db-community-domain-core and provides get(String type) to resolve file extensions (xls, xlsx, csv, json, sql) into specific IImportStrategy implementations.
ExportFactory provides getExporter(ExportTypeEnum type) to resolve ExportTypeEnum values (CSV, EXCEL, SQL, OTHER) into IExportStrategy instances.
Sources: [ImportFactory.java](https://github.com/OtterMind/Chat2DB/blob/main/chat2db-community-server/chat2db-community-domain/chat2db-community-domain-core/src/main/java/ai/chat2db/community/domain/core/impl/task/imports/ImportFactory.java) and [ExportFactory.java](https://github.com/OtterMind/Chat2DB/blob/main/chat2db-community-server/chat2db-community-domain/chat2db-community-domain-core/src/main/java/ai/chat2db/community/domain/core/impl/task/export/ExportFactory.java).
Concrete Import Strategies
All import implementations extend BaseImporter or BaseExcelImporter, inheriting common progress-reporting infrastructure and async handling via ImportAsyncContext.
SQLImporter: Parses DDL/DML statements from.sqlfiles and executes them via the underlying database executor.CSVImporter: ExtendsBaseExcelImporterto parse comma-separated values using Apache Commons CSV.XLSImporter: Handles legacy Excel.xlsformats using Apache POI HSSF.XLSXImporter: Processes modern Excel.xlsxfiles using Apache POI XSSF.JSONImporter: Parses JSON arrays and maps row objects toTableColumndefinitions.
Sources: [SQLImporter.java](https://github.com/OtterMind/Chat2DB/blob/main/chat2db-community-server/chat2db-community-domain/chat2db-community-domain-core/src/main/java/ai/chat2db/community/domain/core/impl/task/imports/sql/SQLImporter.java), [CSVImporter.java](https://github.com/OtterMind/Chat2DB/blob/main/chat2db-community-server/chat2db-community-domain/chat2db-community-domain-core/src/main/java/ai/chat2db/community/domain/core/impl/task/imports/excel/CSVImporter.java), [XLSImporter.java](https://github.com/OtterMind/Chat2DB/blob/main/chat2db-community-server/chat2db-community-domain/chat2db-community-domain-core/src/main/java/ai/chat2db/community/domain/core/impl/task/imports/excel/XLSImporter.java), [XLSXImporter.java](https://github.com/OtterMind/Chat2DB/blob/main/chat2db-community-server/chat2db-community-domain/chat2db-community-domain-core/src/main/java/ai/chat2db/community/domain/core/impl/task/imports/excel/XLSXImporter.java), and [JSONImporter.java](https://github.com/OtterMind/Chat2DB/blob/main/chat2db-community-server/chat2db-community-domain/chat2db-community-domain-core/src/main/java/ai/chat2db/community/domain/core/impl/task/imports/json/JSONImporter.java).
Concrete Export Strategies
Export strategies implement IExportStrategy and write directly to output streams provided by the delivery adapter.
CSVExportStrategy: Generates CSV output viaCsvResultWriterforDbDmlExportServiceconsumption.ExcelExportStrategy: Constructs XLSX workbooks using Apache POI for spreadsheet export.SqlFileExportStrategy: Generates DDL/DML script files containing table structures and data.OtherFileExportStrategy: Handles generic binary blobs or archive formats.
Sources: [CSVExportStrategy.java](https://github.com/OtterMind/Chat2DB/blob/main/chat2db-community-server/chat2db-community-domain/chat2db-community-domain-core/src/main/java/ai/chat2db/community/domain/core/impl/task/export/csv/CSVExportStrategy.java), [ExcelExportStrategy.java](https://github.com/OtterMind/Chat2DB/blob/main/chat2db-community-server/chat2db-community-domain/chat2db-community-domain-core/src/main/java/ai/chat2db/community/domain/core/impl/task/export/excel/ExcelExportStrategy.java), [SqlFileExportStrategy.java](https://github.com/OtterMind/Chat2DB/blob/main/chat2db-community-server/chat2db-community-domain/chat2db-community-domain-core/src/main/java/ai/chat2db/community/domain/core/impl/task/export/sql/SqlFileExportStrategy.java), and [OtherFileExportStrategy.java](https://github.com/OtterMind/Chat2DB/blob/main/chat2db-community-server/chat2db-community-domain/chat2db-community-domain-core/src/main/java/ai/chat2db/community/domain/core/impl/task/export/other/OtherFileExportStrategy.java).
Lower-Level Services and SPI Integration
Beneath the strategy layer, Chat2DB relies on service provider interfaces (SPI) and adapter patterns for database-specific operations.
IDbMetaData provides table and column metadata required for schema-aware imports and exports.
ITaskDataImportService (TaskDataImportServiceImpl) orchestrates the actual IImportStrategy.run() execution, while ITaskDataExportService (TaskDataExportServiceImpl) prepares DbDmlExportPlan instances before handing them to the delivery layer.
IDbDmlExportDeliveryService creates physical output streams, with DmlExportDeliveryAdapter resolving file suffixes and MIME types for HTTP responses or filesystem writes.
Sources: [IDbMetaData.java](https://github.com/OtterMind/Chat2DB/blob/main/chat2db-community-server/chat2db-community-spi/src/main/java/ai/chat2db/spi/IDbMetaData.java), [TaskDataImportServiceImpl.java](https://github.com/OtterMind/Chat2DB/blob/main/chat2db-community-server/chat2db-community-domain/chat2db-community-domain-core/src/main/java/ai/chat2db/community/domain/core/impl/task/TaskDataImportServiceImpl.java), [TaskDataExportServiceImpl.java](https://github.com/OtterMind/Chat2DB/blob/main/chat2db-community-server/chat2db-community-domain/chat2db-community-domain-core/src/main/java/ai/chat2db/community/domain/core/impl/task/TaskDataExportServiceImpl.java), and [DmlExportDeliveryAdapter.java](https://github.com/OtterMind/Chat2DB/blob/main/chat2db-community-server/chat2db-community-web/src/main/java/ai/chat2db/community/web/api/adapter/db/DmlExportDeliveryAdapter.java).
End-to-End Data Flow
The complete Chat2DB data import export pipeline architecture follows this execution path:
- HTTP Layer:
TaskImportControllerorTaskExportControllerreceives the request. - Conversion:
TaskWebConvertertransforms DTOs to domain parameters. - Orchestration:
TaskTransferServiceImplvalidates and initiates the workflow. - Strategy Resolution:
ImportFactoryorExportFactoryreturns the appropriate strategy based on file extension or enum type. - Execution:
TaskDataImportServiceImplorTaskDataExportServiceImplexecutes the strategy within an async context. - Delivery: For exports,
DmlExportDeliveryAdapteropens the output stream; for imports, the strategy writes directly to the database viaIDbMetaData.
Both paths support cancellation and progress tracking through ConsoleTaskProgressListener attached to the async context.
// Example: Importing a CSV file via REST
RestTemplate rest = new RestTemplate();
CsvFileImportRequest req = new CsvFileImportRequest();
req.setFileId(csvFileId);
req.setImportType("csv");
DataResult<Long> result = rest.postForObject(
"http://localhost:10825/api/task/import/otherFile",
req,
DataResult.class);
// Example: Exporting a table to Excel programmatically
DbDmlExportRequest exportReq = new DbDmlExportRequest();
exportReq.setExportType(ExportTypeEnum.EXCEL);
exportReq.setExportPath("/tmp/export.xlsx");
exportReq.setTableName("users");
exportReq.setSchemaName("public");
String fileName = taskTransferService.exportSqlFile(
taskWebConverter.sqlFileExport2param(exportReq));
Summary
- Chat2DB data import export pipeline architecture employs a layered Web → Service → Factory → Strategy pattern that ensures separation of concerns between HTTP handling and business logic.
TaskImportControllerandTaskExportControllerserve as REST endpoints, delegating toTaskTransferServiceImplfor workflow orchestration.- Factory classes (
ImportFactory,ExportFactory) map file extensions and enums to concrete strategies, enabling easy extension for new formats. - Import strategies (
SQLImporter,CSVImporter,XLSImporter,XLSXImporter,JSONImporter) extend base classes that provide async progress reporting. - Export strategies (
CSVExportStrategy,ExcelExportStrategy,SqlFileExportStrategy,OtherFileExportStrategy) write to streams managed byDmlExportDeliveryAdapter. - The pipeline is fully asynchronous, utilizing
ImportAsyncContextandExportAsyncContextwithConsoleTaskProgressListenerfor real-time progress updates.
Frequently Asked Questions
What design pattern does Chat2DB use for data import and export?
Chat2DB uses a combination of the Factory Pattern and Strategy Pattern. The ImportFactory and ExportFactory classes create appropriate strategy instances based on file types, while concrete implementations of IImportStrategy and IExportStrategy encapsulate format-specific logic for SQL, CSV, Excel, and JSON files.
How does Chat2DB handle different file formats like Excel and CSV?
Chat2DB handles format variations through dedicated strategy classes. XLSImporter uses Apache POI HSSF for legacy .xls files, while XLSXImporter uses POI XSSF for modern .xlsx files. CSVImporter extends BaseExcelImporter and utilizes Apache Commons CSV. The ImportFactory maps file extensions to these strategies automatically.
Is the Chat2DB import/export pipeline asynchronous?
Yes, the pipeline is asynchronous-aware. Both TaskDataImportServiceImpl and TaskDataExportServiceImpl execute strategies within ImportAsyncContext or ExportAsyncContext, enabling non-blocking operations. The ConsoleTaskProgressListener attached to these contexts provides real-time progress updates and supports cancellation during long-running imports or exports.
Where is the file format mapping logic located in Chat2DB?
The mapping logic resides in the factory layer within chat2db-community-domain-core. ImportFactory.get(String type) maps file extensions (sql, csv, json, xls, xlsx) to importer instances, while ExportFactory.getExporter(ExportTypeEnum type) maps enum values to exporter instances. This centralization allows new formats to be added without modifying controller or service code.
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 →