OLD | NEW |
(Empty) | |
| 1 # Copyright (c) 2012 Mitch Garnaat http://garnaat.org/ |
| 2 # Copyright (c) 2012 Amazon.com, Inc. or its affiliates. All Rights Reserved |
| 3 # |
| 4 # Permission is hereby granted, free of charge, to any person obtaining a |
| 5 # copy of this software and associated documentation files (the |
| 6 # "Software"), to deal in the Software without restriction, including |
| 7 # without limitation the rights to use, copy, modify, merge, publish, dis- |
| 8 # tribute, sublicense, and/or sell copies of the Software, and to permit |
| 9 # persons to whom the Software is furnished to do so, subject to the fol- |
| 10 # lowing conditions: |
| 11 # |
| 12 # The above copyright notice and this permission notice shall be included |
| 13 # in all copies or substantial portions of the Software. |
| 14 # |
| 15 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS |
| 16 # OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABIL- |
| 17 # ITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT |
| 18 # SHALL THE AUTHOR BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, |
| 19 # WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 20 # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS |
| 21 # IN THE SOFTWARE. |
| 22 # |
| 23 |
| 24 from boto.dynamodb.types import dynamize_value |
| 25 |
| 26 |
| 27 class Condition(object): |
| 28 """ |
| 29 Base class for conditions. Doesn't do a darn thing but allows |
| 30 is to test if something is a Condition instance or not. |
| 31 """ |
| 32 |
| 33 def __eq__(self, other): |
| 34 if isinstance(other, Condition): |
| 35 return self.to_dict() == other.to_dict() |
| 36 |
| 37 class ConditionNoArgs(Condition): |
| 38 """ |
| 39 Abstract class for Conditions that require no arguments, such |
| 40 as NULL or NOT_NULL. |
| 41 """ |
| 42 |
| 43 def __repr__(self): |
| 44 return '%s' % self.__class__.__name__ |
| 45 |
| 46 def to_dict(self): |
| 47 return {'ComparisonOperator': self.__class__.__name__} |
| 48 |
| 49 |
| 50 class ConditionOneArg(Condition): |
| 51 """ |
| 52 Abstract class for Conditions that require a single argument |
| 53 such as EQ or NE. |
| 54 """ |
| 55 |
| 56 def __init__(self, v1): |
| 57 self.v1 = v1 |
| 58 |
| 59 def __repr__(self): |
| 60 return '%s:%s' % (self.__class__.__name__, self.v1) |
| 61 |
| 62 def to_dict(self): |
| 63 return {'AttributeValueList': [dynamize_value(self.v1)], |
| 64 'ComparisonOperator': self.__class__.__name__} |
| 65 |
| 66 |
| 67 class ConditionTwoArgs(Condition): |
| 68 """ |
| 69 Abstract class for Conditions that require two arguments. |
| 70 The only example of this currently is BETWEEN. |
| 71 """ |
| 72 |
| 73 def __init__(self, v1, v2): |
| 74 self.v1 = v1 |
| 75 self.v2 = v2 |
| 76 |
| 77 def __repr__(self): |
| 78 return '%s(%s, %s)' % (self.__class__.__name__, self.v1, self.v2) |
| 79 |
| 80 def to_dict(self): |
| 81 values = (self.v1, self.v2) |
| 82 return {'AttributeValueList': [dynamize_value(v) for v in values], |
| 83 'ComparisonOperator': self.__class__.__name__} |
| 84 |
| 85 |
| 86 class ConditionSeveralArgs(Condition): |
| 87 """ |
| 88 Abstract class for conditions that require several argument (ex: IN). |
| 89 """ |
| 90 |
| 91 def __init__(self, values): |
| 92 self.values = values |
| 93 |
| 94 def __repr__(self): |
| 95 return '{}({})'.format(self.__class__.__name__, |
| 96 ', '.join(self.values)) |
| 97 |
| 98 def to_dict(self): |
| 99 return {'AttributeValueList': [dynamize_value(v) for v in self.values], |
| 100 'ComparisonOperator': self.__class__.__name__} |
| 101 |
| 102 |
| 103 class EQ(ConditionOneArg): |
| 104 |
| 105 pass |
| 106 |
| 107 |
| 108 class NE(ConditionOneArg): |
| 109 |
| 110 pass |
| 111 |
| 112 |
| 113 class LE(ConditionOneArg): |
| 114 |
| 115 pass |
| 116 |
| 117 |
| 118 class LT(ConditionOneArg): |
| 119 |
| 120 pass |
| 121 |
| 122 |
| 123 class GE(ConditionOneArg): |
| 124 |
| 125 pass |
| 126 |
| 127 |
| 128 class GT(ConditionOneArg): |
| 129 |
| 130 pass |
| 131 |
| 132 |
| 133 class NULL(ConditionNoArgs): |
| 134 |
| 135 pass |
| 136 |
| 137 |
| 138 class NOT_NULL(ConditionNoArgs): |
| 139 |
| 140 pass |
| 141 |
| 142 |
| 143 class CONTAINS(ConditionOneArg): |
| 144 |
| 145 pass |
| 146 |
| 147 |
| 148 class NOT_CONTAINS(ConditionOneArg): |
| 149 |
| 150 pass |
| 151 |
| 152 |
| 153 class BEGINS_WITH(ConditionOneArg): |
| 154 |
| 155 pass |
| 156 |
| 157 |
| 158 class IN(ConditionSeveralArgs): |
| 159 |
| 160 pass |
| 161 |
| 162 |
| 163 class BEGINS_WITH(ConditionOneArg): |
| 164 |
| 165 pass |
| 166 |
| 167 |
| 168 class BETWEEN(ConditionTwoArgs): |
| 169 |
| 170 pass |
OLD | NEW |