Skip to content

Commit 1683bf8

Browse files
committed
Add flags support. Split operation replace, store, insert from one insert
1 parent aeb3162 commit 1683bf8

File tree

3 files changed

+47
-16
lines changed

3 files changed

+47
-16
lines changed

src/tarantool/connection.py

Lines changed: 42 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -218,30 +218,60 @@ def call(self, func_name, *args, **kwargs):
218218
default_type = default_type)
219219
return response
220220

221+
def _insert(self, space_name, values, flags):
222+
assert isinstance(values, tuple)
223+
assert (flags & (BOX_RETURN_TUPLE | BOX_ADD | BOX_REPLACE)) == flags
221224

222-
def insert(self, space_name, values, return_tuple=False, not_presented=False, presented=False):
223-
'''\
224-
Execute INSERT request.
225-
Insert single record into a space `space_name`.
225+
request = RequestInsert(self, space_name, values, flags)
226+
return self._send_request(request, space_name)
227+
228+
def replace(self, space_name, values, return_tuple):
229+
'''
230+
Execute REPLACE request.
231+
It will throw error if there's no tuple with this PK exists
226232
227233
:param int space_name: space id to insert a record
228234
:type space_name: int or str
229235
:param values: record to be inserted. The tuple must contain only scalar (integer or strings) values
230236
:type values: tuple
237+
:param return_tuple: True indicates that it is required to return the inserted tuple back
238+
:type return_tuple: bool
231239
240+
:rtype: `Response` instance
241+
'''
242+
self._insert(space_name, values, (BOX_REPLACE_TUPLE if return_tuple else 0) | BOX_REPLACE)
243+
244+
def store(self, space_name, values, return_tuple):
245+
'''
246+
Execute STORE request.
247+
It will overwrite tuple with the same PK, if it exists, or inserts if not
248+
249+
:param int space_name: space id to insert a record
250+
:type space_name: int or str
251+
:param values: record to be inserted. The tuple must contain only scalar (integer or strings) values
252+
:type values: tuple
232253
:param return_tuple: True indicates that it is required to return the inserted tuple back
233254
:type return_tuple: bool
234-
:param not_presented: True indicates that there's must be no tuple with same primary key
235-
:type not_presented: bool
236-
:param presented: True indicates that there's must be tuple with same primary key
237-
:type presented: bool
238-
255+
239256
:rtype: `Response` instance
240257
'''
241-
assert isinstance(values, tuple)
258+
self._insert(space_name, values, (BOX_REPLACE_TUPLE if return_tuple else 0))
242259

243-
request = RequestInsert(self, space_name, values, return_tuple, not_presented, presented)
244-
return self._send_request(request, space_name)
260+
def insert(self, space_name, values, return_tuple):
261+
'''
262+
Execute INSERT request.
263+
It will throw error if there's tuple with same PK exists.
264+
265+
:param int space_name: space id to insert a record
266+
:type space_name: int or str
267+
:param values: record to be inserted. The tuple must contain only scalar (integer or strings) values
268+
:type values: tuple
269+
:param return_tuple: True indicates that it is required to return the inserted tuple back
270+
:type return_tuple: bool
271+
272+
:rtype: `Response` instance
273+
'''
274+
self._insert(space_name, values, (BOX_REPLACE_TUPLE if return_tuple else 0) | BOX_ADD)
245275

246276

247277
def delete(self, space_name, key, return_tuple=False):

src/tarantool/const.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,10 @@
2828
REQUEST_TYPE_CALL = 22
2929
REQUEST_TYPE_PING = 65280
3030

31+
BOX_RETURN_TUPLE = 1
32+
BOX_ADD = 2
33+
BOX_REPLACE = 4
34+
3135

3236
UPDATE_OPERATION_CODE = {'=': 0, '+': 1, '&': 2, '^': 3, '|': 4, 'splice': 5}
3337

src/tarantool/request.py

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -144,15 +144,12 @@ class RequestInsert(Request):
144144
'''
145145
request_type = REQUEST_TYPE_INSERT
146146

147-
def __init__(self, conn, space_name, values, return_tuple, not_presented, presented): # pylint: disable=W0231
147+
def __init__(self, conn, space_name, values, flags): # pylint: disable=W0231
148148
'''\
149149
'''
150150
super(RequestInsert, self).__init__(conn)
151151

152152
assert isinstance(values, (tuple, list))
153-
flags = 1 if return_tuple else 0
154-
flags += 2 if not_presented else 0
155-
flags += 4 if presented else 0
156153

157154
space_no = self.conn.schema.space_no(space_name)
158155
request_body = \

0 commit comments

Comments
 (0)