Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ jobs:
- uses: actions/checkout@v4
with:
repository: polypheny/Polypheny-DB
ref: ${{ vars.POLYPHENY_DB_REF || 'ebcf8e5b09cbae409526c017f509c7a55d07b0a4' }}
ref: ${{ vars.POLYPHENY_DB_REF || 'master' }}

- uses: actions/setup-java@v4
with:
Expand Down
11 changes: 9 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,12 @@ cd /path/to/polyphney-connector-python-x.y.z/
python setup.py install
```

The connector uses the Prism 2.2 client definitions and connects to servers
The connector uses the Prism 2.4 client definitions and connects to servers
that report a compatible Prism 2.x API. The minor versions do not have to match.
Document collection metadata requires server-side support introduced in Prism 2.2.
PEP 249 error classification uses SQLSTATE details from Prism 2.4 servers.
Errors from older servers raise `DatabaseError` with `sqlstate=None` and
`errorcode=None`.


## Getting Started
Expand Down Expand Up @@ -82,11 +85,15 @@ con.close()
An in-depth and more detailed documentation can be found [here](https://docs.polypheny.com/en/latest/drivers/python/overview).

## Tests
Run the tests with coverage report:
Run the tests with a coverage report against an isolated Polypheny instance.
```
coverage run --source polypheny -m pytest && coverage report -m
```

The constraint tests read the test server's runtime settings through its Web UI
at `http://127.0.0.1:7659`. Set `POLYPHENY_TEST_WEBUI_URL` if the same test server
exposes its Web UI at a different address.

## Roadmap
See the [open issues](https://github.com/polypheny/Polypheny-DB/labels/A-python) for a list of proposed features (and known issues).

Expand Down
48 changes: 40 additions & 8 deletions docs/polypheny.rst
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,8 @@ Obtain a cursor from ``Connection.cursor()``. Each new execution releases the
previous result on that cursor. ``execute()`` runs SQL. ``executeany()`` takes
an explicit language and can select a default namespace for the statement.
Use ``?`` with a list or tuple of parameters, or ``:name`` with a dictionary
for named SQL parameters. Cypher execution uses the direct statement path.
for named SQL parameters. For servers that support prepared Cypher, bind
``$name`` with a dictionary.

.. autoclass:: polypheny.Cursor()

Expand Down Expand Up @@ -89,13 +90,44 @@ at exhaustion.
Exceptions
----------

The connector exports ``Warning`` and ``Error``. Prism server errors use
``Error``. The DB-API subclasses are ``InterfaceError`` and ``DatabaseError``.
The latter has subclasses ``DataError``, ``OperationalError``,
``IntegrityError``, ``InternalError``, ``ProgrammingError``, and
``NotSupportedError``. Closed connection operations such as
``cursor()``, ``commit()``, or ``rollback()`` raise ``ProgrammingError``.
Socket failures can surface as ``OSError`` or ``EOFError``.
The connector exports ``Warning`` and ``Error``. The DB-API subclasses are
``InterfaceError`` and ``DatabaseError``. The latter has subclasses
``DataError``, ``OperationalError``, ``IntegrityError``, ``InternalError``,
``ProgrammingError``, and ``NotSupportedError``.

Prism server errors use the SQLSTATE class to select the exception type.
An unknown class or a response from a server without structured error
details raises ``DatabaseError``. Every ``Error`` exposes ``sqlstate``
and ``errorcode``. Each attribute is ``None`` when unavailable, and an
explicit vendor code of zero is returned as ``0``. ``str(error)``
returns the original server message.

.. list-table::
:header-rows: 1

* - SQLSTATE classes
- Exception
* - ``08``, ``28``, ``40``, ``53``, ``57``, ``58``
- ``OperationalError``
* - ``0A``
- ``NotSupportedError``
* - ``22``
- ``DataError``
* - ``23``
- ``IntegrityError``
* - ``24``, ``25``, ``26``, ``2D``, ``I9``, ``XX``
- ``InternalError``
* - ``3F``, ``42``
- ``ProgrammingError``

Invalid cursor or connection use raises ``ProgrammingError``. A transport
configuration or protocol mismatch raises ``InterfaceError``. A refused or
lost connection raises ``OperationalError``. Unsupported result frames
and value types raise ``NotSupportedError``.

After transport loss, ``rollback()`` also raises ``OperationalError``.
Handle that error if you attempt a rollback during cleanup, and use a new
connection for subsequent operations.

See the `official Python API reference <https://docs.polypheny.com/en/latest/drivers/python/api/polypheny>`_
and `error handling guide <https://docs.polypheny.com/en/latest/drivers/python/error-handling>`_.
31 changes: 19 additions & 12 deletions polypheny/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
from polypheny.exceptions import *

import datetime
import time
from typing import Union

# See https://peps.python.org/pep-0249/#globals
Expand All @@ -24,36 +25,42 @@
paramstyle = 'qmark'


def _make_value(constructor, *args):
try:
return constructor(*args)
except (TypeError, ValueError, OverflowError, OSError) as e:
raise DataError(str(e)) from e


def Date(year, month, day):
return datetime.date(year, month, day)
return _make_value(datetime.date, year, month, day)


def Time(hour, minute, second):
return datetime.time(hour, minute, second)
return _make_value(datetime.time, hour, minute, second)


def Timestamp(year, month, day, hour, minute, second):
return datetime.datetime(year, month, day, hour, minute, second)


# See PEP 249
import time
return _make_value(datetime.datetime, year, month, day, hour, minute, second)


def DateFromTicks(ticks):
return Date(*time.localtime(ticks)[:3]) # TODO: Really local time?
return Date(*_make_value(time.localtime, ticks)[:3])


def TimeFromTicks(ticks):
return Time(*time.localtime(ticks)[3:6])
return Time(*_make_value(time.localtime, ticks)[3:6])


def TimestampFromTicks(ticks):
return Timestamp(*time.localtime(ticks)[:6])
return Timestamp(*_make_value(time.localtime, ticks)[:6])


def Binary(string):
return string.encode('UTF-8')
try:
return _make_value(string.encode, 'UTF-8')
except AttributeError as e:
raise DataError(str(e)) from e


# Intentionally omitted, we always give type_code = None, like sqlite3
Expand All @@ -80,6 +87,6 @@ def connect(address: Union[tuple[str, int], str] = None, *, username: str = None
if address is None and transport is None and username is None and password is None and len(kwargs) == 0:
transport = 'unix'
elif address is None or transport is None:
raise Error("Address and transport must be given")
raise InterfaceError("Address and transport must be given")

return Connection(address, username, password, transport, kwargs)
Loading
Loading