summaryrefslogtreecommitdiff
path: root/boto/dynamodb2/table.py
diff options
context:
space:
mode:
Diffstat (limited to 'boto/dynamodb2/table.py')
-rw-r--r--boto/dynamodb2/table.py60
1 files changed, 55 insertions, 5 deletions
diff --git a/boto/dynamodb2/table.py b/boto/dynamodb2/table.py
index 37833dd9..66e6337d 100644
--- a/boto/dynamodb2/table.py
+++ b/boto/dynamodb2/table.py
@@ -648,16 +648,35 @@ class Table(object):
self.connection.update_item(self.table_name, raw_key, item_data, **kwargs)
return True
- def delete_item(self, **kwargs):
+ def delete_item(self, expected=None, conditional_operator=None, **kwargs):
"""
- Deletes an item in DynamoDB.
+ Deletes a single item. You can perform a conditional delete operation
+ that deletes the item if it exists, or if it has an expected attribute
+ value.
+
+ Conditional deletes are useful for only deleting items if specific
+ conditions are met. If those conditions are met, DynamoDB performs
+ the delete. Otherwise, the item is not deleted.
+
+ To specify the expected attribute values of the item, you can pass a
+ dictionary of conditions to ``expected``. Each condition should follow
+ the pattern ``<attributename>__<comparison_operator>=<value_to_expect>``.
**IMPORTANT** - Be careful when using this method, there is no undo.
To specify the key of the item you'd like to get, you can specify the
key attributes as kwargs.
- Returns ``True`` on success.
+ Optionally accepts an ``expected`` parameter which is a dictionary of
+ expected attribute value conditions.
+
+ Optionally accepts a ``conditional_operator`` which applies to the
+ expected attribute value conditions:
+
+ + `AND` - If all of the conditions evaluate to true (default)
+ + `OR` - True if at least one condition evaluates to true
+
+ Returns ``True`` on success, ``False`` on failed conditional delete.
Example::
@@ -676,9 +695,21 @@ class Table(object):
... })
True
+ # Conditional delete
+ >>> users.delete_item(username='johndoe',
+ ... expected={'balance__eq': 0})
+ True
"""
+ expected = self._build_filters(expected, using=FILTER_OPERATORS)
raw_key = self._encode_keys(kwargs)
- self.connection.delete_item(self.table_name, raw_key)
+
+ try:
+ self.connection.delete_item(self.table_name, raw_key,
+ expected=expected,
+ conditional_operator=conditional_operator)
+ except exceptions.ConditionalCheckFailedException:
+ return False
+
return True
def get_key_fields(self):
@@ -969,7 +1000,8 @@ class Table(object):
return results
def query_count(self, index=None, consistent=False, conditional_operator=None,
- query_filter=None, **filter_kwargs):
+ query_filter=None, scan_index_forward=True, limit=None,
+ **filter_kwargs):
"""
Queries the exact count of matching items in a DynamoDB table.
@@ -1003,6 +1035,22 @@ class Table(object):
Returns an integer which represents the exact amount of matched
items.
+ :type scan_index_forward: boolean
+ :param scan_index_forward: Specifies ascending (true) or descending
+ (false) traversal of the index. DynamoDB returns results reflecting
+ the requested order determined by the range key. If the data type
+ is Number, the results are returned in numeric order. For String,
+ the results are returned in order of ASCII character code values.
+ For Binary, DynamoDB treats each byte of the binary data as
+ unsigned when it compares binary values.
+
+ If ScanIndexForward is not specified, the results are returned in
+ ascending order.
+
+ :type limit: integer
+ :param limit: The maximum number of items to evaluate (not necessarily
+ the number of matching items).
+
Example::
# Look for last names equal to "Doe".
@@ -1037,6 +1085,8 @@ class Table(object):
key_conditions=key_conditions,
query_filter=built_query_filter,
conditional_operator=conditional_operator,
+ limit=limit,
+ scan_index_forward=scan_index_forward,
)
return int(raw_results.get('Count', 0))