Skip to content

PEP 249 Database API

Alexander Turenko edited this page Jul 22, 2021 · 2 revisions

How to use

Simple example:

#!/usr/bin/env python

# In tarantool console:
#
# tarantool> box.cfg{listen = 'localhost:3301'}
# tarantool> box.schema.user.create('me', {password = 'secret'})
# tarantool> box.schema.user.grant('me','read,write,execute,create,drop,alter','universe')

from pprint import pprint
from tarantool.dbapi import connect

connection = connect(host='localhost', port=3301, user='me', password='secret')
connection.autocommit = True  # see Caveats below
cursor = connection.cursor()
cursor.execute('SELECT :foo, :bar', {'foo': 5, 'bar': 6}) 
res = cursor.fetchall()
pprint(res)

Read PEP 249 for the full API description.

Caveats

The module enables autocommit mode by default, which is reflected in the <connection object>.autocommit property. This default will be changed when interactive transactions support will land into tarantool and will implemented in the module (it is PEP 249 requirement). Code that rely on the autocommit mode should either verify the property or explicitly set it to True.

The standard obligates an implementation to make database changes made using a connection immediately visible by all cursors of the same connection. The module does not invalidate data already fetched by a cursor in case of DML / DDL operation issued using the same connection. This should be fixed in the future.

The standard marked as deprecated usage of <cursor object>.execute(operation[, parameters]) as alias for <cursor object>.executemany(operation, seq_of_parameters) with parameters passed as a list of tuples. The module does not allow this usage of execute(). Use executemany() instead.

Support status

Core features

Unsupported

  • No type objects are defined yet (#175).
  • <cursor>.description (result set metadata) is not supported yet (#176).
  • Transactions are not supported (not supported in tarantool).
  • Optional <cursor>.callproc() and <cursor>.nextset() are no supported (no SQL/PMS support in tarantool) (#171).

Emulated

  • Cursors are emulated (not supported in tarantool). A whole result set is fetched on execute*().
  • Prepared statements are emulated (supported in tarantool since 2.3.0-334-g0e1b20c36).

Supported

All remaining.

paramstyle is defined as 'named', but 'qmark' is also supported. When <cursor object>.execute(operation[, parameters]) is called with mapping parameters, 'named' convention is used, but if parameter is a sequence, 'qmark' is in effect. The same for <cursor object>.executemany(operation, seq_of_parameters).

TBD: A behaviour of rowcount and fetch*() after executemany().

Example:

cursor.execute('SELECT :foo, :bar', {'foo': 5, 'bar': 6})
cursor.execute('SELECT ?, ?', [5, 6])

Extensions (defined in PEP 249)

The standard warning messages are not issued yet (#169).

Supported

  • "DB-API extension connection.<exception> used"
  • "DB-API extension cursor.lastrowid used"

Unsupported

TBD

Extensions (non-standard)

Both 'named' and 'qmark' parameters style are supported. See paramstyle above for details.

<connection object>.autocommit property. See 'Caveats' above for details.

Further reading