Metadata-Version: 2.4
Name: adbc-driver-ibm-db2
Version: 1.0.0
Summary: ADBC driver for IBM Db2
License-Expression: Apache-2.0
Keywords: adbc,db2,ibm,arrow,database
Classifier: Development Status :: 3 - Alpha
Classifier: Intended Audience :: Developers
Classifier: Programming Language :: Python :: 3
Classifier: Programming Language :: Python :: 3.11
Classifier: Programming Language :: Python :: 3.12
Classifier: Programming Language :: Python :: 3.13
Classifier: Programming Language :: Python :: 3.14
Classifier: Programming Language :: Python :: 3.15
Classifier: Topic :: Database
Classifier: Topic :: Software Development :: Libraries :: Python Modules
Requires-Python: >=3.11
Description-Content-Type: text/markdown
Requires-Dist: adbc-driver-manager>=1.0.0
Requires-Dist: ibm_db>=3.0.0
Provides-Extra: dbapi
Requires-Dist: pandas; extra == "dbapi"
Requires-Dist: pyarrow>=14.0.1; extra == "dbapi"
Provides-Extra: test
Requires-Dist: pandas; extra == "test"
Requires-Dist: polars; extra == "test"
Requires-Dist: pyarrow>=14.0.1; extra == "test"
Requires-Dist: pytest; extra == "test"
Provides-Extra: all
Requires-Dist: pandas; extra == "all"
Requires-Dist: polars; extra == "all"
Requires-Dist: pyarrow>=14.0.1; extra == "all"
Requires-Dist: pytest; extra == "all"

# adbc-driver-ibm-db2

[ADBC](https://arrow.apache.org/adbc/) (Arrow Database Connectivity) driver for
**IBM Db2**. It lets you run SQL against Db2 from Python and get the results
back as Apache Arrow data, without converting row by row.

- Query results come back as Arrow tables, so they work directly with pandas,
  Polars, DuckDB and other Arrow-aware tools.
- Bulk-load Arrow data into Db2 tables with `adbc_ingest`.
- Standard Python DB-API 2.0 interface (`connect`, `cursor`, `execute`, ...).
- No separate Db2 client install is needed. The Db2 CLI libraries come from the
  `ibm_db` package, which is installed automatically.

## Installation

```bash
pip install "adbc-driver-ibm-db2[dbapi]"
```

The `[dbapi]` extra installs `pyarrow` and `pandas`, which you need to fetch
results as Arrow tables or DataFrames. If you only need the driver itself:

```bash
pip install adbc-driver-ibm-db2
```

**Requirements**

- Python 3.11 or newer
- Linux (x86_64), macOS 15 or newer (Apple Silicon), or Windows (x64).
  Prebuilt wheels are published for each.

## Quick start

```python
import adbc_driver_ibm_db2

conn = adbc_driver_ibm_db2.connect(
    "DATABASE=testdb;UID=db2user;PWD=secret;HOSTNAME=db2host;PORT=50000;PROTOCOL=TCPIP"
)

with conn.cursor() as cur:
    cur.execute("SELECT 1 FROM SYSIBM.SYSDUMMY1")
    print(cur.fetchone())

conn.close()
```

The connection string uses the standard Db2 CLI format (`KEY=value;` pairs).
Avoid hard-coding passwords. Read them from an environment variable or a secrets
manager instead.

## Working with Arrow and pandas

These calls need the `[dbapi]` extra (`pyarrow`, and `pandas` for `fetch_df`).

```python
import adbc_driver_ibm_db2

with adbc_driver_ibm_db2.connect(conn_str) as conn:
    with conn.cursor() as cur:
        cur.execute("SELECT * FROM MYSCHEMA.SALES")

        table = cur.fetch_arrow_table()  # pyarrow.Table
        # df = cur.fetch_df()             # pandas.DataFrame instead
```

Because the result is a standard Arrow table, you can pass it on to any
Arrow-aware library, for example `polars.from_arrow(table)`.

### Loading data into Db2

```python
import pyarrow as pa

data = pa.table({"id": [1, 2, 3], "name": ["a", "b", "c"]})

with adbc_driver_ibm_db2.connect(conn_str) as conn:
    with conn.cursor() as cur:
        cur.adbc_ingest("MY_TABLE", data, mode="create")
    conn.commit()
```

`mode` can be `"create"`, `"append"`, `"replace"` or `"create_append"`.

## Encrypted (SSL/TLS) connections

Pass the SSL settings through `db_kwargs` using `DatabaseOptions`:

```python
from adbc_driver_ibm_db2 import DatabaseOptions, connect

conn = connect(
    "DATABASE=testdb;UID=db2user;PWD=secret;HOSTNAME=db2host;PORT=50001;PROTOCOL=TCPIP",
    db_kwargs={
        DatabaseOptions.SSL_SECURITY: "SSL",
        DatabaseOptions.SSL_SERVER_CERT: "/path/to/server.arm",
    },
)
```

| Option | Meaning |
|---|---|
| `DatabaseOptions.SSL_SECURITY` | Set to `"SSL"` to turn on an encrypted connection. |
| `DatabaseOptions.SSL_SERVER_CERT` | Path to the server certificate (`.arm`). Needed for a self-signed server certificate. |
| `DatabaseOptions.SSL_CLIENT_KEYSTOREDB` | Path to the client key database (`.kdb`). |
| `DatabaseOptions.SSL_CLIENT_KEYSTASH` | Path to the keystore stash file (`.sth`). |

## Type mapping notes

Some Db2 types map to Arrow types in ways you might not expect:

| Db2 type | Arrow type |
|---|---|
| `DECIMAL(p, s)` | `string` (not `decimal128`) |
| `XML` | `binary` |
| `TIMESTAMP(p)` | `timestamp` without a timezone |

When loading data with `adbc_ingest`, Arrow `bool` columns are created as
`SMALLINT`, and `decimal128` columns are created as `DECIMAL(31, 8)`.

## Environment variables

| Variable | Purpose |
|---|---|
| `IBM_DB_HOME` | *(Optional)* Path to a Db2 `clidriver`. Found automatically from `ibm_db` if not set. |
| `ADBC_DB2_DRIVER` | *(Optional)* Path to a different `libadbc_driver_db2` shared library to use instead of the bundled one. |

## Troubleshooting

- **`Cannot locate libadbc_driver_db2...`**: you are not using a binary wheel.
  Install from PyPI (`pip install adbc-driver-ibm-db2`), or set `ADBC_DB2_DRIVER`
  to the library path.
- **Db2 CLI library not found at connect time**: make sure `ibm_db` installed
  correctly (`pip show ibm_db`), or set `IBM_DB_HOME` to your `clidriver` folder.
- **`pyarrow` import errors on `fetch_arrow_table()`**: install the extra with
  `pip install "adbc-driver-ibm-db2[dbapi]"`.

## Building from source

```bash
# Build the native driver first (see the repository README), then:
pip wheel python/ -w dist/
pip install dist/adbc_driver_ibm_db2-*.whl
```

## Links

- Source and issue tracker: <https://github.com/adbc-drivers/db2>
- ADBC documentation: <https://arrow.apache.org/adbc/>

## License

Apache License 2.0.
