Skip to content

Commit 3455edc

Browse files
committed
api: native crud module support
Adds native api support for crud module [1] to use it from a connection object. 1. github.com/tarantool/crud Closes #205
1 parent 1154b3d commit 3455edc

14 files changed

+2725
-1
lines changed

.github/workflows/packing.yml

+18
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,12 @@ jobs:
9999
- name: Install test requirements
100100
run: pip3 install -r requirements-test.txt
101101

102+
- name: Install the crud module for testing purposes
103+
run: |
104+
curl -L https://tarantool.io/release/2/installer.sh | bash
105+
sudo apt install -y tt
106+
tt rocks install crud
107+
102108
- name: Run tests
103109
run: make test-pure-install
104110

@@ -331,6 +337,12 @@ jobs:
331337
- name: Install test requirements
332338
run: pip3 install -r requirements-test.txt
333339

340+
- name: Install the crud module for testing purposes
341+
run: |
342+
curl -L https://tarantool.io/release/2/installer.sh | bash
343+
sudo apt install -y tt
344+
tt rocks install crud
345+
334346
- name: Run tests
335347
run: make test-pure-install
336348

@@ -492,6 +504,12 @@ jobs:
492504
- name: Install test requirements
493505
run: pip3 install -r requirements-test.txt
494506

507+
- name: Install the crud module for testing purposes
508+
run: |
509+
curl -L https://tarantool.io/release/2/installer.sh | bash
510+
sudo apt install -y tt
511+
tt rocks install crud
512+
495513
- name: Run tests
496514
run: make test-pure-install
497515

.github/workflows/reusable_testing.yml

+6
Original file line numberDiff line numberDiff line change
@@ -39,4 +39,10 @@ jobs:
3939
- name: Install test requirements
4040
run: pip3 install -r requirements-test.txt
4141

42+
- name: Install the crud module for testing purposes
43+
run: |
44+
curl -L https://tarantool.io/release/2/installer.sh | bash
45+
sudo apt install -y tt
46+
tt rocks install crud
47+
4248
- run: make test

.github/workflows/testing.yml

+20
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,12 @@ jobs:
8585
- name: Install test requirements
8686
run: pip3 install -r requirements-test.txt
8787

88+
- name: Install the crud module for testing purposes
89+
run: |
90+
curl -L https://tarantool.io/release/2/installer.sh | bash
91+
sudo apt install -y tt
92+
tt rocks install crud
93+
8894
- name: Run tests
8995
run: make test
9096

@@ -143,6 +149,14 @@ jobs:
143149
- name: Install test requirements
144150
run: pip3 install -r requirements-test.txt
145151

152+
- name: Install the crud module for testing purposes
153+
# This is a workaround with TARANTOOL_DIR and should be reworked later.
154+
run: |
155+
source tarantool-enterprise/env.sh
156+
curl -L https://tarantool.io/release/2/installer.sh | bash
157+
sudo apt install -y tt
158+
tt rocks install crud TARANTOOL_DIR=$PWD/tarantool-enterprise
159+
146160
- name: Run tests
147161
run: |
148162
source tarantool-enterprise/env.sh
@@ -196,6 +210,12 @@ jobs:
196210
- name: Install test requirements
197211
run: pip3 install -r requirements-test.txt
198212

213+
- name: Install the crud module for testing purposes
214+
run: |
215+
curl -L https://tarantool.io/release/2/installer.sh | bash
216+
sudo apt install -y tt
217+
tt rocks install crud
218+
199219
- name: Run tests
200220
run: make test-pure-install
201221

CHANGELOG.md

+2
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
99
### Added
1010
- Support custom packer and unpacker factories (#191).
1111

12+
- Support [crud module](https://github.com/tarantool/crud) native API (#205).
13+
1214
### Changed
1315

1416
### Fixed

docs/source/api/submodule-crud.rst

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
module :py:mod:`tarantool.crud`
2+
================================
3+
4+
.. automodule:: tarantool.crud

docs/source/index.rst

+1
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ API Reference
6363
api/module-tarantool.rst
6464
api/submodule-connection.rst
6565
api/submodule-connection-pool.rst
66+
api/submodule-crud.rst
6667
api/submodule-dbapi.rst
6768
api/submodule-error.rst
6869
api/submodule-mesh-connection.rst

docs/source/quick-start.rst

+115
Original file line numberDiff line numberDiff line change
@@ -347,3 +347,118 @@ with out-of-band message processing:
347347
348348
print(callback_res)
349349
>>> [[[100, 1]], [[200, 1]], [[300, 1]]]
350+
351+
352+
Interaction with the crud module
353+
----------------------------------
354+
355+
Through the :class:`~tarantool.Connection` object, you can access
356+
`crud module <https://github.com/tarantool/crud>`_ methods:
357+
358+
.. code-block:: python
359+
360+
>>> import tarantool
361+
>>> from tarantool.error import CrudModuleError, DatabaseError
362+
>>> conn = tarantool.Connection(host='localhost',port=3301)
363+
364+
>>> conn.crud_
365+
conn.crud_count( conn.crud_insert( conn.crud_insert_object_many(
366+
conn.crud_min( conn.crud_replace_object( conn.crud_stats(
367+
conn.crud_unflatten_rows( conn.crud_upsert_many( conn.crud_delete(
368+
conn.crud_insert_many( conn.crud_len( conn.crud_replace(
369+
conn.crud_replace_object_many( conn.crud_storage_info( conn.crud_update(
370+
conn.crud_upsert_object( conn.crud_get( conn.crud_insert_object(
371+
conn.crud_max( conn.crud_replace_many( conn.crud_select(
372+
conn.crud_truncate( conn.crud_upsert( conn.crud_upsert_object_many(
373+
374+
As an example, consider :meth:`~tarantool.Connection.crud_insert` and :meth:`~tarantool.Connection.crud_insert_object_many`.
375+
It is recommended to enclose calls in the try-except construction as follows:
376+
377+
.. code-block:: python
378+
379+
# Insert without exception:
380+
>>> res = conn.crud_insert('tester', (3500,300,'Rob'))
381+
>>> res
382+
<tarantool.crud.CrudResult object at 0x11a56e320>
383+
>>> res.
384+
res.metadata res.success
385+
>>> res.success
386+
[[3500, 300, 'Rob']]
387+
>>> res.metadata
388+
[{'name': 'id', 'type': 'unsigned'}, {'name': 'bucket_id', 'type': 'unsigned'}, {'name': 'name', 'type': 'string'}]
389+
390+
# Insert with exception (duplicate key exists):
391+
>>> try:
392+
... res = conn.crud_insert('tester', (3500,300,'Rob'))
393+
... except CrudModuleError as e:
394+
... exc_crud = e
395+
...
396+
>>> exc_crud
397+
CrudModuleError(None, <tarantool.crud.CrudError object at 0x10a276950>)
398+
>>> exc_crud.error
399+
<tarantool.crud.CrudError object at 0x10a276950>
400+
>>> exc_crud.error.
401+
exc_crud.error.class_name exc_crud.error.err exc_crud.error.file exc_crud.error.line exc_crud.error.str
402+
>>> exc_crud.error.class_name
403+
'InsertError'
404+
>>> exc_crud.error.str
405+
'InsertError: Failed to insert: Duplicate key exists in unique index "primary_index" in space "tester" with old tuple - [3500, 300, "Rob"] and new tuple - [3500, 300, "Rob"]'
406+
>>> exc_crud.res is None # using only in case of *_many.
407+
True
408+
409+
# In case of batch operation (*_many), CrudModuleManyError contains both result and errors:
410+
>>> try:
411+
... res = conn.crud_insert_object_many('tester', ({'id':1800,'bucket_id':100,'name':'Mike'}, {'id':2800,'bucket_id':100,'second_name':'Bill'}, {'id':3800,'bucket_id':100,'name':'Bob'}, {'id':1,'bucket_id':100,'name':'Rob'}), {'timeout':100, 'rollback_on_error':False})
412+
... except CrudModuleManyError as e:
413+
... exc_crud = e
414+
...
415+
>>> exc_crud
416+
CrudModuleManyError(<tarantool.crud.CrudResult object at 0x11a56f310>, [<tarantool.crud.CrudError object at 0x11a56e9e0>, <tarantool.crud.CrudError object at 0x11a56f490>])
417+
>>> exc_crud.success # some of the rows were inserted.
418+
<tarantool.crud.CrudResult object at 0x11a56f310>
419+
>>> exc_crud.success.rows
420+
[[1800, 100, 'Mike'], [3800, 100, 'Bob']]
421+
>>> exc_crud.errors_list # some of the rows were not inserted.
422+
[<tarantool.crud.CrudError object at 0x11a56e9e0>, <tarantool.crud.CrudError object at 0x11a56f490>]
423+
>>> exc_crud.errors_list[0].str
424+
'CallError: Failed for 56d9c861-a8ac-459c-9715-09094ebb94a7: Function returned an error: Duplicate key exists in unique index "primary_index" in space "tester" with old tuple - [1, 100, "Mike"] and new tuple - [1, 100, "Rob"]'
425+
>>> exc_crud.errors_list[1].str
426+
'InsertManyError: Failed to flatten object: FlattenError: Object is specified in bad format: FlattenError: Unknown field "second_name" is specified'
427+
428+
If module crud not found on the router:
429+
430+
.. code-block:: python
431+
432+
>>> try:
433+
... res = conn.crud_insert('tester', (22221,300,'Rob'))
434+
... except DatabaseError as e:
435+
... exc_db = e
436+
...
437+
>>> exc_db
438+
DatabaseError(33, "Procedure 'crud.insert' is not defined. Ensure that you're calling crud.router and user have enough grants")
439+
440+
Using :meth:`~tarantool.Connection.crud_select` and :meth:`~tarantool.Connection.crud_unflatten_rows`:
441+
442+
.. code-block:: python
443+
444+
>>> res = conn.crud_select('tester', {}, {'first':2})
445+
>>> res
446+
<tarantool.crud.CrudResult object at 0x10a276d10>
447+
>>> res.rows
448+
[[1, 100, 'Mike'], [2, 100, 'Mike']]
449+
>>> res.metadata
450+
[{'name': 'id', 'type': 'unsigned'}, {'name': 'bucket_id', 'type': 'unsigned'}, {'name': 'name', 'type': 'string'}]
451+
>>> r = conn.crud_unflatten_rows(res.rows, res.metadata)
452+
>>> r
453+
[{'id': 1, 'bucket_id': 100, 'name': 'Mike'}, {'id': 2, 'bucket_id': 100, 'name': 'Mike'}]
454+
455+
Using :meth:`~tarantool.Connection.crud_truncate` and :meth:`~tarantool.Connection.crud_len`:
456+
457+
.. code-block:: python
458+
459+
>>> res = conn.crud_len('tester')
460+
>>> res
461+
26
462+
>>> res = conn.crud_truncate('tester')
463+
>>> res
464+
True

0 commit comments

Comments
 (0)