Skip to content

Commit bc09831

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 6c54109 commit bc09831

14 files changed

+1779
-1
lines changed

Diff for: .github/workflows/packing.yml

+12
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,9 @@ 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: tarantoolctl rocks install crud
104+
102105
- name: Run tests
103106
run: make test-pure-install
104107

@@ -143,6 +146,9 @@ jobs:
143146
- name: Install test requirements
144147
run: pip3 install -r requirements-test.txt
145148

149+
- name: Install the crud module for testing purposes
150+
run: tarantoolctl rocks install crud
151+
146152
- name: Setup WSL for tarantool
147153
uses: Vampire/setup-wsl@v1
148154
with:
@@ -331,6 +337,9 @@ 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: tarantoolctl rocks install crud
342+
334343
- name: Run tests
335344
run: make test-pure-install
336345

@@ -492,6 +501,9 @@ jobs:
492501
- name: Install test requirements
493502
run: pip3 install -r requirements-test.txt
494503

504+
- name: Install the crud module for testing purposes
505+
run: tarantoolctl rocks install crud
506+
495507
- name: Run tests
496508
run: make test-pure-install
497509

Diff for: .github/workflows/reusable_testing.yml

+3
Original file line numberDiff line numberDiff line change
@@ -39,4 +39,7 @@ 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: tarantoolctl rocks install crud
44+
4245
- run: make test

Diff for: .github/workflows/testing.yml

+17
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,9 @@ 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: tarantoolctl rocks install crud
90+
8891
- name: Run tests
8992
run: make test
9093

@@ -141,6 +144,11 @@ jobs:
141144
- name: Install test requirements
142145
run: pip3 install -r requirements-test.txt
143146

147+
- name: Install the crud module for testing purposes
148+
run: |
149+
source tarantool-enterprise/env.sh
150+
tarantoolctl rocks install crud
151+
144152
- name: Run tests
145153
run: |
146154
source tarantool-enterprise/env.sh
@@ -194,6 +202,9 @@ jobs:
194202
- name: Install test requirements
195203
run: pip3 install -r requirements-test.txt
196204

205+
- name: Install the crud module for testing purposes
206+
run: tarantoolctl rocks install crud
207+
197208
- name: Run tests
198209
run: make test-pure-install
199210

@@ -236,6 +247,9 @@ jobs:
236247
- name: Install test requirements
237248
run: pip3 install -r requirements-test.txt
238249

250+
- name: Install the crud module for testing purposes
251+
run: tarantoolctl rocks install crud
252+
239253
- name: Setup WSL for tarantool
240254
uses: Vampire/setup-wsl@v1
241255
with:
@@ -316,6 +330,9 @@ jobs:
316330
- name: Install test requirements
317331
run: pip3 install -r requirements-test.txt
318332

333+
- name: Install the crud module for testing purposes
334+
run: tarantoolctl rocks install crud
335+
319336
- name: Setup WSL for tarantool
320337
uses: Vampire/setup-wsl@v1
321338
with:

Diff for: CHANGELOG.md

+2
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
88

99
### Added
1010

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

1315
### 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

+151
Original file line numberDiff line numberDiff line change
@@ -347,3 +347,154 @@ 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 :class:`~tarantool.Connection.crud_insert` and :class:`~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+
# Try without exception:
380+
>>> try:
381+
... res = conn.crud_insert('tester', (3500,300,'Rob'))
382+
... except CrudModuleError as e:
383+
... exc_crud = e
384+
...
385+
>>> res
386+
<tarantool.crud.CrudResult object at 0x11a56e320>
387+
>>> res.
388+
res.metadata res.rows # fields like in lua implementation.
389+
>>> res.rows
390+
[[3500, 300, 'Rob']]
391+
>>> res.metadata
392+
[{'name': 'id', 'type': 'unsigned'}, {'name': 'bucket_id', 'type': 'unsigned'}, {'name': 'name', 'type': 'string'}]
393+
394+
# Try with exception:
395+
>>> try:
396+
... res = conn.crud_insert('tester', (3500,300,'Rob'))
397+
... except CrudModuleError as e:
398+
... exc_crud = e
399+
...
400+
>>> exc_crud
401+
CrudModuleError(None, <tarantool.crud.CrudError object at 0x10a276950>)
402+
>>> exc_crud.
403+
exc_crud.args exc_crud.err exc_crud.res exc_crud.with_traceback(
404+
>>> exc_crud.args
405+
(None, <tarantool.crud.CrudError object at 0x10a276950>)
406+
>>> exc_crud.err
407+
<tarantool.crud.CrudError object at 0x10a276950>
408+
>>> exc_crud.err. # fields like in lua implementation.
409+
exc_crud.err.class_name exc_crud.err.err exc_crud.err.file exc_crud.err.line exc_crud.err.str
410+
>>> exc_crud.err.class_name
411+
'InsertError'
412+
>>> exc_crud.err.str
413+
'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"]'
414+
>>> exc_crud.res is None # using only in case of *_many.
415+
True
416+
417+
# In case of batch operation (*_many), CrudModuleError contains both result and errors:
418+
>>> try:
419+
... 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})
420+
... except CrudModuleError as e:
421+
... exc_crud = e
422+
...
423+
>>> exc_crud
424+
CrudModuleError(<tarantool.crud.CrudResult object at 0x11a56f310>, [<tarantool.crud.CrudError object at 0x11a56e9e0>, <tarantool.crud.CrudError object at 0x11a56f490>])
425+
>>> exc_crud.
426+
exc_crud.args exc_crud.errs exc_crud.res exc_crud.with_traceback(
427+
>>> exc_crud.res # some of the lines were inserted.
428+
<tarantool.crud.CrudResult object at 0x11a56f310>
429+
>>> exc_crud.res.rows
430+
[[1800, 100, 'Mike'], [3800, 100, 'Bob']]
431+
>>> exc_crud.errs # some of the lines were not inserted.
432+
[<tarantool.crud.CrudError object at 0x11a56e9e0>, <tarantool.crud.CrudError object at 0x11a56f490>]
433+
>>> exc_crud.errs[0].str
434+
'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"]'
435+
>>> exc_crud.errs[1].str
436+
'InsertManyError: Failed to flatten object: FlattenError: Object is specified in bad format: FlattenError: Unknown field "second_name" is specified'
437+
>>> exc_crud.args
438+
(<tarantool.crud.CrudResult object at 0x11a56f310>, [<tarantool.crud.CrudError object at 0x11a56e9e0>, <tarantool.crud.CrudError object at 0x11a56f490>])
439+
440+
If module crud not found on the router:
441+
442+
.. code-block:: python
443+
444+
>>> try:
445+
... res = conn.crud_insert('tester', (22221,300,'Rob'))
446+
... except CrudModuleError as e:
447+
... exc_crud = e
448+
... except DatabaseError as e:
449+
... exc_db = e
450+
...
451+
>>> exc_db
452+
DatabaseError(33, "Procedure 'crud.insert' is not defined. Ensure that you're calling crud.router and user have enough grants")
453+
>>> exc_db.
454+
exc_db.args exc_db.code exc_db.extra_info exc_db.message exc_db.with_traceback(
455+
>>> exc_db.args
456+
(33, "Procedure 'crud.insert' is not defined. Ensure that you're calling crud.router and user have enough grants")
457+
>>> exc_db.code
458+
33
459+
>>> exc_db.message
460+
"Procedure 'crud.insert' is not defined. Ensure that you're calling crud.router and user have enough grants"
461+
>>> exc_db.extra_info
462+
BoxError(type='ClientError', file='/tmp/tarantool-20221003-6335-edruh3/tarantool-2.10.3/src/box/lua/call.c', line=112, message="Procedure 'crud.insert' is not defined. Ensure that you're calling crud.router and user have enough grants", errno=0, errcode=33, fields=None, prev=None)
463+
464+
Using :class:`~tarantool.Connection.crud_select` and :class:`~tarantool.Connection.crud_unflatten_rows`:
465+
466+
.. code-block:: python
467+
468+
>>> try:
469+
... res = conn.crud_select('tester', {}, {'first':2})
470+
... except CrudModuleError as e:
471+
... exc_crud = e
472+
...
473+
>>> res
474+
<tarantool.crud.CrudResult object at 0x10a276d10>
475+
>>> res.
476+
res.metadata res.rows
477+
>>> res.rows
478+
[[1, 100, 'Mike'], [2, 100, 'Mike']]
479+
>>> res.metadata
480+
[{'name': 'id', 'type': 'unsigned'}, {'name': 'bucket_id', 'type': 'unsigned'}, {'name': 'name', 'type': 'string'}]
481+
>>> r = conn.crud_unflatten_rows(res.rows, res.metadata)
482+
>>> r
483+
[{'id': 1, 'bucket_id': 100, 'name': 'Mike'}, {'id': 2, 'bucket_id': 100, 'name': 'Mike'}]
484+
485+
Using :class:`~tarantool.Connection.crud_truncate` and :class:`~tarantool.Connection.crud_len`:
486+
487+
.. code-block:: python
488+
489+
>>> try:
490+
... res = conn.crud_len('tester')
491+
... except CrudModuleError as e:
492+
... exc_crud = e
493+
>>> res
494+
26
495+
>>> try:
496+
... res = conn.crud_truncate('tester')
497+
... except CrudModuleError as e:
498+
... exc_crud = e
499+
>>> res
500+
True

0 commit comments

Comments
 (0)