|
1 | | -# Adapter SDK Reference |
| 1 | +# Adapter Interface Reference |
2 | 2 |
|
3 | | -The **Adapter SDK** (`nl2sql-adapter-sdk`) defines the core contract that all datasources must implement. |
| 3 | +The adapter contract lives in the adapter SDK: |
4 | 4 |
|
5 | | -## Interface: `DatasourceAdapter` |
| 5 | +- `nl2sql.datasources.protocols.DatasourceAdapterProtocol` |
| 6 | +- `nl2sql_adapter_sdk.contracts.AdapterRequest` |
| 7 | +- `nl2sql_adapter_sdk.contracts.ResultFrame` |
6 | 8 |
|
7 | | -All adapters must inherit from `nl2sql_adapter_sdk.interfaces.DatasourceAdapter`. |
| 9 | +## Interface: `DatasourceAdapterProtocol` |
| 10 | + |
| 11 | +Adapters expose a capability-driven interface: |
8 | 12 |
|
9 | 13 | ```python |
10 | | -from nl2sql_adapter_sdk import DatasourceAdapter |
| 14 | +from nl2sql.datasources.protocols import DatasourceAdapterProtocol |
| 15 | +from nl2sql_adapter_sdk.contracts import AdapterRequest, ResultFrame |
| 16 | +from nl2sql_adapter_sdk.capabilities import DatasourceCapability |
11 | 17 | ``` |
12 | 18 |
|
13 | 19 | ### Mandatory Properties |
14 | 20 |
|
15 | 21 | | Property | Type | Description | |
16 | 22 | | :--- | :--- | :--- | |
17 | 23 | | `datasource_id` | `str` | Unique identifier (e.g., "production_db"). | |
18 | | -| `row_limit` | `int` | **Safety Breaker**. Must return a safe limit (e.g., 1000) to prevent OOM errors. | |
19 | | -| `max_bytes` | `int` | **Safety Breaker**. Recommended limit for network payloads. | |
| 24 | +| `datasource_engine_type` | `str` | Engine type string (e.g., `postgres`, `rest`). | |
| 25 | +| `row_limit` | `int` | Safety breaker (limit returned rows). | |
| 26 | +| `max_bytes` | `int` | Safety breaker (limit payload size). | |
20 | 27 |
|
21 | 28 | ### Mandatory Methods |
22 | 29 |
|
23 | | -#### `fetch_schema()` |
| 30 | +#### `capabilities() -> set[DatasourceCapability]` |
24 | 31 |
|
25 | | -Returns `SchemaMetadata`. |
| 32 | +Declares supported capabilities (e.g., `supports_sql`, `supports_rest`). |
26 | 33 |
|
27 | | -* **Returns**: `SchemaMetadata` containing tables, columns, PKs, FKs. |
28 | | -* **Requirement**: Must populate `col.statistics` (samples, min/max) for the validation logic to work effectively. |
| 34 | +#### `execute(request: AdapterRequest) -> ResultFrame` |
29 | 35 |
|
30 | | -#### `execute(sql: str)` |
| 36 | +Executes a plan-specific request and returns a normalized `ResultFrame`. |
31 | 37 |
|
32 | | -Executes a query and returns results. |
| 38 | +* **Args**: `AdapterRequest` with `plan_type` and `payload` |
| 39 | +* **Returns**: `ResultFrame` with `columns`, `rows`, `row_count`, and error metadata |
33 | 40 |
|
34 | | -* **Args**: `sql` (str) - The SQL query to run. |
35 | | -* **Returns**: `QueryResult` with `rows` (list of dicts) and `columns` (list of names). |
| 41 | +#### `fetch_schema_snapshot()` |
36 | 42 |
|
37 | | -#### `dry_run(sql: str)` |
| 43 | +Required only if `supports_schema_introspection` is advertised. |
38 | 44 |
|
39 | | -Validates SQL without executing it (or safely rolling back). |
| 45 | +### Optional Methods (SQL adapters) |
40 | 46 |
|
41 | | -* **Args**: `sql` (str) |
42 | | -* **Returns**: `DryRunResult(is_valid=bool, error_message=str)` |
| 47 | +#### `dry_run(sql: str)` |
43 | 48 |
|
44 | | -### Optional Methods |
| 49 | +Validates SQL without executing it (or safely rolling back). |
45 | 50 |
|
46 | 51 | #### `explain(sql: str)` |
47 | 52 |
|
48 | 53 | Returns the execution plan. |
49 | 54 |
|
50 | | -* **Returns**: `QueryPlan(plan_text=str)` |
51 | | - |
52 | 55 | #### `cost_estimate(sql: str)` |
53 | 56 |
|
54 | 57 | Returns cost/row estimates for the Physical Validator. |
55 | 58 |
|
56 | | -* **Returns**: `CostEstimate(estimated_cost=float, estimated_rows=int)` |
57 | | - |
58 | 59 | --- |
59 | 60 |
|
60 | 61 | ## Compliance Testing |
61 | 62 |
|
62 | | -The SDK provides a compliance test suite. **All Adapters MUST pass this suite.** |
63 | | - |
64 | | -It verifies: |
65 | | - |
66 | | -* Schema Introspection (PKs/FKs detected?) |
67 | | -* Type Mapping (Date -> Python Date, Numeric -> Python Float) |
68 | | -* Error Handling (Bad SQL -> AdapterError) |
69 | | - |
70 | | -### Running Tests |
71 | | - |
72 | | -```python |
73 | | -# tests/test_my_adapter.py |
74 | | -from nl2sql_adapter_sdk.testing import BaseAdapterTest |
75 | | -from my_adapter import MyAdapter |
76 | | -import pytest |
77 | | - |
78 | | -class TestMyAdapter(BaseAdapterTest): |
79 | | - @pytest.fixture |
80 | | - def adapter(self): |
81 | | - return MyAdapter(...) |
82 | | -``` |
| 63 | +All adapters should pass the compliance test suite (schema introspection, type mapping, |
| 64 | +error handling, and result contract validation). |
0 commit comments