Skip to content

Commit d0b6a97

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 d0b6a97

14 files changed

+2706
-1
lines changed

Diff for: .github/workflows/packing.yml

+15
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,11 @@ 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+
sudo apt install -y tt
105+
tt rocks install crud
106+
102107
- name: Run tests
103108
run: make test-pure-install
104109

@@ -331,6 +336,11 @@ jobs:
331336
- name: Install test requirements
332337
run: pip3 install -r requirements-test.txt
333338

339+
- name: Install the crud module for testing purposes
340+
run: |
341+
sudo apt install -y tt
342+
tt rocks install crud
343+
334344
- name: Run tests
335345
run: make test-pure-install
336346

@@ -492,6 +502,11 @@ jobs:
492502
- name: Install test requirements
493503
run: pip3 install -r requirements-test.txt
494504

505+
- name: Install the crud module for testing purposes
506+
run: |
507+
sudo apt install -y tt
508+
tt rocks install crud
509+
495510
- name: Run tests
496511
run: make test-pure-install
497512

Diff for: .github/workflows/reusable_testing.yml

+5
Original file line numberDiff line numberDiff line change
@@ -39,4 +39,9 @@ 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+
sudo apt install -y tt
45+
tt rocks install crud
46+
4247
- run: make test

Diff for: .github/workflows/testing.yml

+16
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,11 @@ 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+
sudo apt install -y tt
91+
tt rocks install crud
92+
8893
- name: Run tests
8994
run: make test
9095

@@ -143,6 +148,12 @@ jobs:
143148
- name: Install test requirements
144149
run: pip3 install -r requirements-test.txt
145150

151+
- name: Install the crud module for testing purposes
152+
run: |
153+
source tarantool-enterprise/env.sh
154+
sudo apt install -y tt
155+
tt rocks install crud
156+
146157
- name: Run tests
147158
run: |
148159
source tarantool-enterprise/env.sh
@@ -196,6 +207,11 @@ jobs:
196207
- name: Install test requirements
197208
run: pip3 install -r requirements-test.txt
198209

210+
- name: Install the crud module for testing purposes
211+
run: |
212+
sudo apt install -y tt
213+
tt rocks install crud
214+
199215
- name: Run tests
200216
run: make test-pure-install
201217

Diff for: 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

Diff for: 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

Diff for: 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

Diff for: 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.rows # fields like in Lua implementation.
385+
>>> res.rows
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.err
399+
<tarantool.crud.CrudError object at 0x10a276950>
400+
>>> exc_crud.err. # fields like in Lua implementation.
401+
exc_crud.err.class_name exc_crud.err.err exc_crud.err.file exc_crud.err.line exc_crud.err.str
402+
>>> exc_crud.err.class_name
403+
'InsertError'
404+
>>> exc_crud.err.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), CrudModuleError 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 CrudModuleError as e:
413+
... exc_crud = e
414+
...
415+
>>> exc_crud
416+
CrudModuleError(<tarantool.crud.CrudResult object at 0x11a56f310>, [<tarantool.crud.CrudError object at 0x11a56e9e0>, <tarantool.crud.CrudError object at 0x11a56f490>])
417+
>>> exc_crud.res # some of the rows were inserted.
418+
<tarantool.crud.CrudResult object at 0x11a56f310>
419+
>>> exc_crud.res.rows
420+
[[1800, 100, 'Mike'], [3800, 100, 'Bob']]
421+
>>> exc_crud.errs # some of the rows were not inserted.
422+
[<tarantool.crud.CrudError object at 0x11a56e9e0>, <tarantool.crud.CrudError object at 0x11a56f490>]
423+
>>> exc_crud.errs[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.errs[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)