# Chat2DB Data Import/Export Pipeline Architecture: Web to Strategy Pattern Deep Dive

> Explore Chat2DB's data import export pipeline architecture. Learn how the Web Service Factory Strategy pattern handles SQL CSV Excel JSON file operations efficiently.

- Repository: [OtterMind/Chat2DB](https://github.com/OtterMind/Chat2DB)
- Tags: architecture
- Published: 2026-07-26

---

**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-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/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()` and `otherFileImport2param()` transform incoming import requests.
- **Export conversion**: `sqlFileExport2param()` and `otherFileExport2param()` 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-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()` and `importOtherFile()` initiate import workflows.
- **Export methods**: `exportSqlFile()` and `exportOtherFile()` 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-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/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/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 `.sql` files and executes them via the underlying database executor.
- **`CSVImporter`**: Extends `BaseExcelImporter` to parse comma-separated values using Apache Commons CSV.
- **`XLSImporter`**: Handles legacy Excel `.xls` formats using Apache POI HSSF.
- **`XLSXImporter`**: Processes modern Excel `.xlsx` files using Apache POI XSSF.
- **`JSONImporter`**: Parses JSON arrays and maps row objects to `TableColumn` definitions.

Sources: [[`SQLImporter.java`](https://github.com/OtterMind/Chat2DB/blob/main/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/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/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/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/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 via `CsvResultWriter` for `DbDmlExportService` consumption.
- **`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/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/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/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/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/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/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/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/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:

1. **HTTP Layer**: `TaskImportController` or `TaskExportController` receives the request.
2. **Conversion**: `TaskWebConverter` transforms DTOs to domain parameters.
3. **Orchestration**: `TaskTransferServiceImpl` validates and initiates the workflow.
4. **Strategy Resolution**: `ImportFactory` or `ExportFactory` returns the appropriate strategy based on file extension or enum type.
5. **Execution**: `TaskDataImportServiceImpl` or `TaskDataExportServiceImpl` executes the strategy within an async context.
6. **Delivery**: For exports, `DmlExportDeliveryAdapter` opens the output stream; for imports, the strategy writes directly to the database via `IDbMetaData`.

Both paths support cancellation and progress tracking through `ConsoleTaskProgressListener` attached to the async context.

```java
// 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.
- **`TaskImportController`** and **`TaskExportController`** serve as REST endpoints, delegating to **`TaskTransferServiceImpl`** for 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 by `DmlExportDeliveryAdapter`.
- The pipeline is fully asynchronous, utilizing `ImportAsyncContext` and `ExportAsyncContext` with `ConsoleTaskProgressListener` for 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.