Skip to content

Commit da98380

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 da98380

14 files changed

+2721
-1
lines changed

Diff for: .github/workflows/packing.yml

+16
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

@@ -162,6 +165,13 @@ jobs:
162165
touch tarantool.pid
163166
echo $TNT_PID > ./tarantool.pid
164167
168+
- name: Install the crud module for testing purposes
169+
shell: wsl-bash_Ubuntu-20.04 {0}
170+
run: |
171+
sudo apt install -y unzip
172+
tarantoolctl rocks install crud
173+
tarantoolctl rocks list
174+
165175
- name: Run tests
166176
env:
167177
REMOTE_TARANTOOL_HOST: localhost
@@ -331,6 +341,9 @@ jobs:
331341
- name: Install test requirements
332342
run: pip3 install -r requirements-test.txt
333343

344+
- name: Install the crud module for testing purposes
345+
run: tarantoolctl rocks install crud
346+
334347
- name: Run tests
335348
run: make test-pure-install
336349

@@ -492,6 +505,9 @@ jobs:
492505
- name: Install test requirements
493506
run: pip3 install -r requirements-test.txt
494507

508+
- name: Install the crud module for testing purposes
509+
run: tarantoolctl rocks install crud
510+
495511
- name: Run tests
496512
run: make test-pure-install
497513

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

+25
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

@@ -263,6 +274,13 @@ jobs:
263274
touch tarantool.pid
264275
echo $TNT_PID > ./tarantool.pid
265276
277+
- name: Install the crud module for testing purposes
278+
shell: wsl-bash_Ubuntu-20.04 {0}
279+
run: |
280+
sudo apt install -y unzip
281+
tarantoolctl rocks install crud
282+
tarantoolctl rocks list
283+
266284
- name: Run tests
267285
env:
268286
REMOTE_TARANTOOL_HOST: localhost
@@ -335,6 +353,13 @@ jobs:
335353
touch tarantool.pid
336354
echo $TNT_PID > ./tarantool.pid
337355
356+
- name: Install the crud module for testing purposes
357+
shell: wsl-bash_Ubuntu-20.04 {0}
358+
run: |
359+
sudo apt install -y unzip
360+
tarantoolctl rocks install crud
361+
tarantoolctl rocks list
362+
338363
- name: Run tests
339364
env:
340365
REMOTE_TARANTOOL_HOST: localhost

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

+137
Original file line numberDiff line numberDiff line change
@@ -347,3 +347,140 @@ 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.
399+
exc_crud.args exc_crud.err exc_crud.res exc_crud.with_traceback(
400+
>>> exc_crud.args
401+
(None, <tarantool.crud.CrudError object at 0x10a276950>)
402+
>>> exc_crud.err
403+
<tarantool.crud.CrudError object at 0x10a276950>
404+
>>> exc_crud.err. # fields like in Lua implementation.
405+
exc_crud.err.class_name exc_crud.err.err exc_crud.err.file exc_crud.err.line exc_crud.err.str
406+
>>> exc_crud.err.class_name
407+
'InsertError'
408+
>>> exc_crud.err.str
409+
'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"]'
410+
>>> exc_crud.res is None # using only in case of *_many.
411+
True
412+
413+
# In case of batch operation (*_many), CrudModuleError contains both result and errors:
414+
>>> try:
415+
... 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})
416+
... except CrudModuleError as e:
417+
... exc_crud = e
418+
...
419+
>>> exc_crud
420+
CrudModuleError(<tarantool.crud.CrudResult object at 0x11a56f310>, [<tarantool.crud.CrudError object at 0x11a56e9e0>, <tarantool.crud.CrudError object at 0x11a56f490>])
421+
>>> exc_crud.
422+
exc_crud.args exc_crud.errs exc_crud.res exc_crud.with_traceback(
423+
>>> exc_crud.res # some of the rows were inserted.
424+
<tarantool.crud.CrudResult object at 0x11a56f310>
425+
>>> exc_crud.res.rows
426+
[[1800, 100, 'Mike'], [3800, 100, 'Bob']]
427+
>>> exc_crud.errs # some of the rows were not inserted.
428+
[<tarantool.crud.CrudError object at 0x11a56e9e0>, <tarantool.crud.CrudError object at 0x11a56f490>]
429+
>>> exc_crud.errs[0].str
430+
'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"]'
431+
>>> exc_crud.errs[1].str
432+
'InsertManyError: Failed to flatten object: FlattenError: Object is specified in bad format: FlattenError: Unknown field "second_name" is specified'
433+
>>> exc_crud.args
434+
(<tarantool.crud.CrudResult object at 0x11a56f310>, [<tarantool.crud.CrudError object at 0x11a56e9e0>, <tarantool.crud.CrudError object at 0x11a56f490>])
435+
436+
If module crud not found on the router:
437+
438+
.. code-block:: python
439+
440+
>>> try:
441+
... res = conn.crud_insert('tester', (22221,300,'Rob'))
442+
... except CrudModuleError as e:
443+
... exc_crud = e
444+
... except DatabaseError as e:
445+
... exc_db = e
446+
...
447+
>>> exc_db
448+
DatabaseError(33, "Procedure 'crud.insert' is not defined. Ensure that you're calling crud.router and user have enough grants")
449+
450+
Using :meth:`~tarantool.Connection.crud_select` and :meth:`~tarantool.Connection.crud_unflatten_rows`:
451+
452+
.. code-block:: python
453+
454+
>>> try:
455+
... res = conn.crud_select('tester', {}, {'first':2})
456+
... except CrudModuleError as e:
457+
... exc_crud = e
458+
...
459+
>>> res
460+
<tarantool.crud.CrudResult object at 0x10a276d10>
461+
>>> res.
462+
res.metadata res.rows
463+
>>> res.rows
464+
[[1, 100, 'Mike'], [2, 100, 'Mike']]
465+
>>> res.metadata
466+
[{'name': 'id', 'type': 'unsigned'}, {'name': 'bucket_id', 'type': 'unsigned'}, {'name': 'name', 'type': 'string'}]
467+
>>> r = conn.crud_unflatten_rows(res.rows, res.metadata)
468+
>>> r
469+
[{'id': 1, 'bucket_id': 100, 'name': 'Mike'}, {'id': 2, 'bucket_id': 100, 'name': 'Mike'}]
470+
471+
Using :meth:`~tarantool.Connection.crud_truncate` and :meth:`~tarantool.Connection.crud_len`:
472+
473+
.. code-block:: python
474+
475+
>>> try:
476+
... res = conn.crud_len('tester')
477+
... except CrudModuleError as e:
478+
... exc_crud = e
479+
>>> res
480+
26
481+
>>> try:
482+
... res = conn.crud_truncate('tester')
483+
... except CrudModuleError as e:
484+
... exc_crud = e
485+
>>> res
486+
True

0 commit comments

Comments
 (0)