| 1 |
# Copyright DataStax, Inc. |
| 2 |
# |
| 3 |
# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 |
# you may not use this file except in compliance with the License. |
| 5 |
# You may obtain a copy of the License at |
| 6 |
# |
| 7 |
# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 |
# |
| 9 |
# Unless required by applicable law or agreed to in writing, software |
| 10 |
# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 |
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 |
# See the License for the specific language governing permissions and |
| 13 |
# limitations under the License. |
| 14 |
|
| 15 |
from cassandra.cqlengine import UnicodeMixin |
| 16 |
|
| 17 |
|
| 18 |
class QueryOperatorException(Exception): |
| 19 |
pass |
| 20 |
|
| 21 |
|
| 22 |
class BaseQueryOperator(UnicodeMixin): |
| 23 |
# The symbol that identifies this operator in kwargs |
| 24 |
# ie: colname__<symbol> |
| 25 |
symbol = None |
| 26 |
|
| 27 |
# The comparator symbol this operator uses in cql |
| 28 |
cql_symbol = None |
| 29 |
|
| 30 |
def __unicode__(self): |
| 31 |
if self.cql_symbol is None: |
| 32 |
raise QueryOperatorException("cql symbol is None") |
| 33 |
return self.cql_symbol |
| 34 |
|
| 35 |
|
| 36 |
class OpMapMeta(type): |
| 37 |
|
| 38 |
def __init__(cls, name, bases, dct): |
| 39 |
if not hasattr(cls, 'opmap'): |
| 40 |
cls.opmap = {} |
| 41 |
else: |
| 42 |
cls.opmap[cls.symbol] = cls |
| 43 |
super(OpMapMeta, cls).__init__(name, bases, dct) |
| 44 |
|
| 45 |
|
| 46 |
class BaseWhereOperator(BaseQueryOperator, metaclass=OpMapMeta): |
| 47 |
""" base operator used for where clauses """ |
| 48 |
@classmethod |
| 49 |
def get_operator(cls, symbol): |
| 50 |
try: |
| 51 |
return cls.opmap[symbol.upper()] |
| 52 |
except KeyError: |
| 53 |
raise QueryOperatorException("{0} doesn't map to a QueryOperator".format(symbol)) |
| 54 |
|
| 55 |
|
| 56 |
class EqualsOperator(BaseWhereOperator): |
| 57 |
symbol = 'EQ' |
| 58 |
cql_symbol = '=' |
| 59 |
|
| 60 |
|
| 61 |
class NotEqualsOperator(BaseWhereOperator): |
| 62 |
symbol = 'NE' |
| 63 |
cql_symbol = '!=' |
| 64 |
|
| 65 |
|
| 66 |
class InOperator(EqualsOperator): |
| 67 |
symbol = 'IN' |
| 68 |
cql_symbol = 'IN' |
| 69 |
|
| 70 |
|
| 71 |
class GreaterThanOperator(BaseWhereOperator): |
| 72 |
symbol = "GT" |
| 73 |
cql_symbol = '>' |
| 74 |
|
| 75 |
|
| 76 |
class GreaterThanOrEqualOperator(BaseWhereOperator): |
| 77 |
symbol = "GTE" |
| 78 |
cql_symbol = '>=' |
| 79 |
|
| 80 |
|
| 81 |
class LessThanOperator(BaseWhereOperator): |
| 82 |
symbol = "LT" |
| 83 |
cql_symbol = '<' |
| 84 |
|
| 85 |
|
| 86 |
class LessThanOrEqualOperator(BaseWhereOperator): |
| 87 |
symbol = "LTE" |
| 88 |
cql_symbol = '<=' |
| 89 |
|
| 90 |
|
| 91 |
class ContainsOperator(EqualsOperator): |
| 92 |
symbol = "CONTAINS" |
| 93 |
cql_symbol = 'CONTAINS' |
| 94 |
|
| 95 |
|
| 96 |
class LikeOperator(EqualsOperator): |
| 97 |
symbol = "LIKE" |
| 98 |
cql_symbol = 'LIKE' |
| 99 |
|
| 100 |
|
| 101 |
class IsNotNullOperator(EqualsOperator): |
| 102 |
symbol = "IS NOT NULL" |
| 103 |
cql_symbol = 'IS NOT NULL' |