|
| 1 | +from pynamodb.constants import BINARY_SET_SHORT, LIST_SHORT, NUMBER_SET_SHORT, NUMBER_SHORT, STRING_SET_SHORT |
| 2 | +from pynamodb.expressions.util import get_value_placeholder, substitute_names |
| 3 | + |
| 4 | + |
| 5 | +class Action(object): |
| 6 | + format_string = '' |
| 7 | + |
| 8 | + def __init__(self, path, value=None): |
| 9 | + self.path = path |
| 10 | + self.value = value |
| 11 | + |
| 12 | + def serialize(self, placeholder_names, expression_attribute_values): |
| 13 | + path = substitute_names(self.path, placeholder_names, split=True) |
| 14 | + value = get_value_placeholder(self.value, expression_attribute_values) if self.value else None |
| 15 | + return self.format_string.format(value, path=path) |
| 16 | + |
| 17 | + |
| 18 | +class SetAction(Action): |
| 19 | + """ |
| 20 | + The SET action adds an attribute to an item. |
| 21 | + """ |
| 22 | + format_string = '{path} = {0}' |
| 23 | + |
| 24 | + def __init__(self, path, value): |
| 25 | + super(SetAction, self).__init__(path, value) |
| 26 | + |
| 27 | + |
| 28 | +class IncrementAction(SetAction): |
| 29 | + """ |
| 30 | + A SET action that is used to add to a number attribute. |
| 31 | + """ |
| 32 | + format_string = '{path} = {path} + {0}' |
| 33 | + |
| 34 | + def __init__(self, path, amount): |
| 35 | + (attr_type, value), = amount.items() |
| 36 | + if attr_type != NUMBER_SHORT: |
| 37 | + raise ValueError("{0} must be a number".format(value)) |
| 38 | + super(IncrementAction, self).__init__(path, amount) |
| 39 | + |
| 40 | + |
| 41 | +class DecrementAction(SetAction): |
| 42 | + """ |
| 43 | + A SET action that is used to subtract from a number attribute. |
| 44 | + """ |
| 45 | + format_string = '{path} = {path} - {0}' |
| 46 | + |
| 47 | + def __init__(self, path, amount): |
| 48 | + (attr_type, value), = amount.items() |
| 49 | + if attr_type != NUMBER_SHORT: |
| 50 | + raise ValueError("{0} must be a number".format(value)) |
| 51 | + super(DecrementAction, self).__init__(path, amount) |
| 52 | + |
| 53 | + |
| 54 | +class AppendAction(SetAction): |
| 55 | + """ |
| 56 | + A SET action that appends elements to the end of a list. |
| 57 | + """ |
| 58 | + format_string = '{path} = list_append({path}, {0})' |
| 59 | + |
| 60 | + def __init__(self, path, elements): |
| 61 | + (attr_type, value), = elements.items() |
| 62 | + if attr_type != LIST_SHORT: |
| 63 | + raise ValueError("{0} must be a list".format(value)) |
| 64 | + super(AppendAction, self).__init__(path, elements) |
| 65 | + |
| 66 | + |
| 67 | +class PrependAction(SetAction): |
| 68 | + """ |
| 69 | + A SET action that prepends elements to the beginning of a list. |
| 70 | + """ |
| 71 | + format_string = '{path} = list_append({0}, {path})' |
| 72 | + |
| 73 | + def __init__(self, path, elements): |
| 74 | + (attr_type, value), = elements.items() |
| 75 | + if attr_type != LIST_SHORT: |
| 76 | + raise ValueError("{0} must be a list".format(value)) |
| 77 | + super(PrependAction, self).__init__(path, elements) |
| 78 | + |
| 79 | + |
| 80 | +class SetIfNotExistsAction(SetAction): |
| 81 | + """ |
| 82 | + A SET action that avoids overwriting an existing attribute. |
| 83 | + """ |
| 84 | + format_string = '{path} = if_not_exists({path}, {0})' |
| 85 | + |
| 86 | + |
| 87 | +class RemoveAction(Action): |
| 88 | + """ |
| 89 | + The REMOVE action deletes an attribute from an item. |
| 90 | + """ |
| 91 | + format_string = '{path}' |
| 92 | + |
| 93 | + def __init__(self, path): |
| 94 | + super(RemoveAction, self).__init__(path) |
| 95 | + |
| 96 | + |
| 97 | +class AddAction(Action): |
| 98 | + """ |
| 99 | + The ADD action appends elements to a set or mathematically adds to a number attribute. |
| 100 | + """ |
| 101 | + format_string = '{path} {0}' |
| 102 | + |
| 103 | + def __init__(self, path, subset): |
| 104 | + (attr_type, value), = subset.items() |
| 105 | + if attr_type not in [BINARY_SET_SHORT, NUMBER_SET_SHORT, NUMBER_SHORT, STRING_SET_SHORT]: |
| 106 | + raise ValueError("{0} must be a number or set".format(value)) |
| 107 | + super(AddAction, self).__init__(path, subset) |
| 108 | + |
| 109 | + |
| 110 | +class DeleteAction(Action): |
| 111 | + """ |
| 112 | + The DELETE action removes elements from a set. |
| 113 | + """ |
| 114 | + format_string = '{path} {0}' |
| 115 | + |
| 116 | + def __init__(self, path, subset): |
| 117 | + (attr_type, value), = subset.items() |
| 118 | + if attr_type not in [BINARY_SET_SHORT, NUMBER_SET_SHORT, STRING_SET_SHORT]: |
| 119 | + raise ValueError("{0} must be a set".format(value)) |
| 120 | + super(DeleteAction, self).__init__(path, subset) |
| 121 | + |
| 122 | + |
| 123 | +class Update(object): |
| 124 | + |
| 125 | + def __init__(self): |
| 126 | + self.set_actions = [] |
| 127 | + self.remove_actions = [] |
| 128 | + self.add_actions = [] |
| 129 | + self.delete_actions = [] |
| 130 | + |
| 131 | + def add_action(self, action): |
| 132 | + if isinstance(action, SetAction): |
| 133 | + self.set_actions.append(action) |
| 134 | + elif isinstance(action, RemoveAction): |
| 135 | + self.remove_actions.append(action) |
| 136 | + elif isinstance(action, AddAction): |
| 137 | + self.add_actions.append(action) |
| 138 | + elif isinstance(action, DeleteAction): |
| 139 | + self.delete_actions.append(action) |
| 140 | + else: |
| 141 | + raise ValueError("unsupported action type: '{0}'".format(action.__class__.__name__)) |
| 142 | + |
| 143 | + def serialize(self, placeholder_names, expression_attribute_values): |
| 144 | + expression = None |
| 145 | + expression = self._add_clause(expression, 'SET', self.set_actions, placeholder_names, expression_attribute_values) |
| 146 | + expression = self._add_clause(expression, 'REMOVE', self.remove_actions, placeholder_names, expression_attribute_values) |
| 147 | + expression = self._add_clause(expression, 'ADD', self.add_actions, placeholder_names, expression_attribute_values) |
| 148 | + expression = self._add_clause(expression, 'DELETE', self.delete_actions, placeholder_names, expression_attribute_values) |
| 149 | + return expression |
| 150 | + |
| 151 | + @staticmethod |
| 152 | + def _add_clause(expression, keyword, actions, placeholder_names, expression_attribute_values): |
| 153 | + clause = Update._get_clause(keyword, actions, placeholder_names, expression_attribute_values) |
| 154 | + if clause is None: |
| 155 | + return expression |
| 156 | + return clause if expression is None else expression + " " + clause |
| 157 | + |
| 158 | + @staticmethod |
| 159 | + def _get_clause(keyword, actions, placeholder_names, expression_attribute_values): |
| 160 | + actions = ", ".join([action.serialize(placeholder_names, expression_attribute_values) for action in actions]) |
| 161 | + return keyword + " " + actions if actions else None |
0 commit comments