|
| 1 | +# Licensed to the Apache Software Foundation (ASF) under one |
| 2 | +# or more contributor license agreements. See the NOTICE file |
| 3 | +# distributed with this work for additional information |
| 4 | +# regarding copyright ownership. The ASF licenses this file |
| 5 | +# to you under the Apache License, Version 2.0 (the |
| 6 | +# "License"); you may not use this file except in compliance |
| 7 | +# with the License. You may obtain a copy of the License at |
| 8 | +# |
| 9 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +# |
| 11 | +# Unless required by applicable law or agreed to in writing, |
| 12 | +# software distributed under the License is distributed on an |
| 13 | +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 14 | +# KIND, either express or implied. See the License for the |
| 15 | +# specific language governing permissions and limitations |
| 16 | +# under the License. |
| 17 | + |
| 18 | +# RECIPE STARTS HERE |
| 19 | +#: After verifying the basic driver functionality, we can use the |
| 20 | +#: ``adbc_driver_manager`` Python package's built-in dbapi implementation |
| 21 | +#: to expose a ready-to-go Pythonic database API. This is also useful for |
| 22 | +#: high-level testing! |
| 23 | +#: |
| 24 | +#: First, we'll import pathlib for a few path calculations and the |
| 25 | +#: ``adbc_driver_manager``'s ``dbapi`` module: |
| 26 | +from pathlib import Path |
| 27 | + |
| 28 | +from adbc_driver_manager import dbapi |
| 29 | + |
| 30 | + |
| 31 | +#: Next, we'll define a ``connect()`` function that wraps ``dbapi.connect()`` |
| 32 | +#: with the location the .toml manifest file which points to the shared library |
| 33 | +#: we built using ``cmake`` in the previous section. |
| 34 | +#: For the purposes of our tutorial, this will be in current directory. |
| 35 | +def connect(uri: str): |
| 36 | + # we can point to the manifest file directly |
| 37 | + manifest_file = Path(".") / "driver_example.toml" |
| 38 | + if manifest_file.exists(): |
| 39 | + return dbapi.connect( |
| 40 | + driver=str(manifest_file.resolve()), db_kwargs={"uri": uri} |
| 41 | + ) |
| 42 | + |
| 43 | + # alternatively, it can look for the manifest file in the user's config |
| 44 | + # directory ($HOME/.config/adbc/driver_example.toml) or the system's |
| 45 | + # config directory (/etc/adbc/driver_example.toml) |
| 46 | + return dbapi.connect(driver="driver_example", db_kwargs={"uri": uri}) |
| 47 | + |
| 48 | + |
| 49 | +#: Next, we can give our driver a go! The two pieces we implemented in the driver |
| 50 | +#: were the "bulk ingest" feature and "select all from", so let's see if it works! |
| 51 | +if __name__ == "__main__": |
| 52 | + import pyarrow |
| 53 | + |
| 54 | + with connect(uri=Path(__file__).parent.as_uri()) as con: |
| 55 | + data = pyarrow.table({"col": [1, 2, 3]}) |
| 56 | + with con.cursor() as cur: |
| 57 | + cur.adbc_ingest("example.arrows", data, mode="create") |
| 58 | + |
| 59 | + with con.cursor() as cur: |
| 60 | + cur.execute("SELECT * FROM example.arrows") |
| 61 | + print(cur.fetchall()) |
| 62 | + # Output: [(1,), (2,), (3,)] |
| 63 | + |
| 64 | + (Path(__file__).parent / "example.arrows").unlink() |
0 commit comments