Skip to content

Commit e9fddba

Browse files
committed
turbodbc: Improve demo program and documentation
1 parent 0165156 commit e9fddba

File tree

3 files changed

+53
-53
lines changed

3 files changed

+53
-53
lines changed

by-language/python-turbodbc/README.rst

Lines changed: 22 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -2,23 +2,13 @@
22
Using CrateDB with turbodbc
33
###########################
44

5+
This document and its accompanying code example describes how to connect
6+
to `CrateDB`_ using `turbodbc`_.
57

6-
*****
7-
About
8-
*****
9-
10-
This section of the documentation describes how to connect to `CrateDB`_
11-
with `turbodbc`_, by providing a few example programs.
12-
13-
The examples use the `unixODBC`_ implementation of `ODBC`_, and the `PostgreSQL
8+
The example uses the `unixODBC`_ implementation of `ODBC`_, and the `PostgreSQL
149
ODBC driver`_, for connecting to the `PostgreSQL wire protocol`_ interface of
1510
`CrateDB`_.
1611

17-
This folder also contains ``Dockerfile`` files providing environments to
18-
exercise the examples on different operating systems, like Arch Linux,
19-
Red Hat (CentOS), Debian, and SUSE Linux.
20-
21-
2212
************
2313
Introduction
2414
************
@@ -77,35 +67,28 @@ Setup
7767
Install prerequisites
7868
=====================
7969

80-
Arch Linux::
81-
82-
# See `dockerfiles/archlinux.Dockerfile`.
83-
84-
CentOS Stream::
85-
86-
dnf install --enablerepo=crb -y boost-devel g++ postgresql-odbc python3 python3-devel python3-pip unixODBC-devel
87-
88-
Debian::
89-
90-
apt-get install --yes build-essential libboost-dev odbc-postgresql unixodbc-dev
91-
9270
macOS/Homebrew::
9371

94-
brew install psqlodbc unixodbc
72+
brew install psqlodbc pybind11 simdutf unixodbc
9573

96-
SUSE Linux Enterprise Server::
74+
If you can't install the required software components on your machine,
75+
head over to the `README-OCI`_ document.
9776

98-
# See `dockerfiles/sles.Dockerfile`.
77+
This folder also contains ``Dockerfile`` files providing environments to
78+
exercise the code example on different operating systems, like Arch Linux,
79+
Red Hat (CentOS), Debian, and SUSE Linux.
9980

10081
Install Python sandbox
10182
======================
102-
::
83+
Create Python virtualenv and install dependency packages::
10384

104-
# Create Python virtualenv and install dependency packages.
105-
python3 -m venv .venv
85+
uv venv --python 3.12 --seed
10686
source .venv/bin/activate
107-
pip install --upgrade --requirement=requirements-prereq.txt
108-
pip install --upgrade --requirement=requirements.txt --verbose
87+
uv pip install --upgrade --verbose \
88+
--requirement=requirements-prereq.txt
89+
uv pip install --upgrade --verbose \
90+
--requirement=requirements-dev.txt \
91+
--requirement=requirements.txt
10992

11093
.. note::
11194

@@ -125,21 +108,11 @@ Usage
125108
Run CrateDB::
126109

127110
docker run --rm -it --publish=4200:4200 --publish=5432:5432 crate \
128-
-Cdiscovery.type=single-node -Ccluster.routing.allocation.disk.threshold_enabled=false
129-
130-
Invoke demo program on workstation::
131-
132-
python demo.py
133-
134-
Exercise demo program using Docker, on different operating systems::
135-
136-
docker build --progress=plain --tag local/python-turbodbc-demo --file=dockerfiles/archlinux.Dockerfile .
137-
docker build --progress=plain --tag local/python-turbodbc-demo --file=dockerfiles/centos.Dockerfile .
138-
docker build --progress=plain --tag local/python-turbodbc-demo --file=dockerfiles/debian.Dockerfile .
139-
docker build --progress=plain --tag local/python-turbodbc-demo --file=dockerfiles/sles.Dockerfile .
111+
-Cdiscovery.type=single-node
140112

141-
docker run --rm -it --volume=$(pwd):/src --network=host local/python-turbodbc-demo python3 /src/demo.py
113+
Invoke demo program::
142114

115+
python demo.py
143116

144117
*******
145118
Backlog
@@ -152,10 +125,11 @@ examples to be exercised are tracked within the `backlog`_.
152125

153126
.. _Apache Arrow: https://en.wikipedia.org/wiki/Apache_Arrow
154127
.. _Apache Arrow support: https://turbodbc.readthedocs.io/en/latest/pages/advanced_usage.html#advanced-usage-arrow
155-
.. _backlog: https://github.com/crate/cratedb-examples/blob/main/python-turbodbc/backlog.rst
156-
.. _CrateDB: https://github.com/crate/crate
128+
.. _backlog: https://github.com/crate/cratedb-examples/blob/main/by-language/python-turbodbc/backlog.rst
129+
.. _CrateDB: https://cratedb.com/database
157130
.. _NumPy: https://en.wikipedia.org/wiki/NumPy
158131
.. _NumPy support: https://turbodbc.readthedocs.io/en/latest/pages/advanced_usage.html#advanced-usage-numpy
132+
.. _README-OCI: ./README-OCI.md
159133
.. _ODBC: https://en.wikipedia.org/wiki/Open_Database_Connectivity
160134
.. _Open Database Connectivity (ODBC): https://en.wikipedia.org/wiki/Open_Database_Connectivity
161135
.. _PostgreSQL ODBC driver: https://odbc.postgresql.org/

by-language/python-turbodbc/backlog.rst

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@ Various items how this little code example can be improved.
88
- [x] Insert multiple records using parameters
99
- [x] Docs: Add installation on SUSE
1010
- [x] Provide example(s) for different operating systems (Linux, macOS)
11+
- [x] Using turbodbc 4 is not an option, see https://github.com/blue-yonder/turbodbc/issues/437
12+
- [x] Note turbodbc=5.0.1 should only require numpy>=2 in the build environment but should run also with e.g. numpy=1.26
13+
- [x] Let's use NumPy 2 across the board
1114
- [o] Docs: Drop a note about connecting with driver file vs. connecting via DSN
1215
- [o] Evaluate different ODBC drivers
1316
- [o] Provide an example scenario how to run it on Windows

by-language/python-turbodbc/demo.py

Lines changed: 28 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,24 @@
1+
"""
2+
Example program connecting to CrateDB [1] using turbodbc [2].
3+
4+
[1] https://cratedb.com/database
5+
[2] https://turbodbc.readthedocs.io/
6+
"""
7+
18
import os
29
import sys
310

411
from turbodbc import connect
512

613

7-
def demo_pg():
14+
def main():
815
# Connect to database.
916
# https://turbodbc.readthedocs.io/en/latest/pages/getting_started.html#establish-a-connection-with-your-database
1017

1118
# Either connect per data source name defined within the ODBC configuration,
1219
# connection = connect(dsn="postgresql", server="localhost", database="testdrive", uid="crate", pwd=None)
1320

14-
# or connect per connection string, referencing a driver file directly.
21+
# ... or connect per connection string, referencing a driver file directly.
1522
if sys.platform == "linux":
1623
candidates = [
1724
# archlinux
@@ -29,16 +36,31 @@ def demo_pg():
2936
else:
3037
raise NotImplementedError(f"Platform {sys.platform} not supported yet")
3138

32-
connection_string = f"Driver={driver_file};Server=localhost;Port=5432;Database=testdrive;Uid=crate;Pwd=;"
39+
connection_string = (
40+
f"Driver={driver_file};Server=localhost;Port=5432;Database=testdrive;Uid=crate;Pwd=;"
41+
)
3342
print(f"INFO: Connecting to '{connection_string}'")
3443
connection = connect(connection_string=connection_string)
44+
print()
45+
46+
# Query `sys.summits`.
47+
print("sys.summits")
48+
cursor = connection.cursor()
49+
cursor.execute("SELECT * FROM sys.summits ORDER by height DESC LIMIT 10")
50+
for row in cursor:
51+
print(row)
52+
cursor.close()
53+
print()
3554

3655
# Insert data.
56+
print("doc.testdrive")
3757
cursor = connection.cursor()
3858
cursor.execute("CREATE TABLE IF NOT EXISTS testdrive (id INT PRIMARY KEY, data TEXT);")
3959
cursor.execute("DELETE FROM testdrive;")
4060
cursor.execute("INSERT INTO testdrive VALUES (0, 'zero'), (1, 'one'), (2, 'two');")
41-
cursor.executemany("INSERT INTO testdrive VALUES (?, ?);", [(3, "three"), (4, "four"), (5, "five")])
61+
cursor.executemany(
62+
"INSERT INTO testdrive VALUES (?, ?);", [(3, "three"), (4, "four"), (5, "five")]
63+
)
4264
cursor.execute("REFRESH TABLE testdrive;")
4365
cursor.close()
4466

@@ -54,6 +76,7 @@ def demo_pg():
5476
print(row)
5577

5678
cursor.close()
79+
print()
5780

5881
# Terminate database connection.
5982
connection.close()
@@ -66,4 +89,4 @@ def find_program(candidates):
6689

6790

6891
if __name__ == "__main__":
69-
demo_pg()
92+
main()

0 commit comments

Comments
 (0)