| 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.util import OrderedDict |
| 16 |
|
| 17 |
from cassandra.cqlengine import CQLEngineException |
| 18 |
from cassandra.cqlengine.columns import Column |
| 19 |
from cassandra.cqlengine.connection import get_cluster |
| 20 |
from cassandra.cqlengine.models import UsingDescriptor, BaseModel |
| 21 |
from cassandra.cqlengine.query import AbstractQueryableColumn, SimpleQuerySet |
| 22 |
from cassandra.cqlengine.query import DoesNotExist as _DoesNotExist |
| 23 |
from cassandra.cqlengine.query import MultipleObjectsReturned as _MultipleObjectsReturned |
| 24 |
|
| 25 |
|
| 26 |
class QuerySetDescriptor(object): |
| 27 |
""" |
| 28 |
returns a fresh queryset for the given model |
| 29 |
it's declared on everytime it's accessed |
| 30 |
""" |
| 31 |
|
| 32 |
def __get__(self, obj, model): |
| 33 |
""" :rtype: ModelQuerySet """ |
| 34 |
if model.__abstract__: |
| 35 |
raise CQLEngineException('cannot execute queries against abstract models') |
| 36 |
return SimpleQuerySet(obj) |
| 37 |
|
| 38 |
def __call__(self, *args, **kwargs): |
| 39 |
""" |
| 40 |
Just a hint to IDEs that it's ok to call this |
| 41 |
|
| 42 |
:rtype: ModelQuerySet |
| 43 |
""" |
| 44 |
raise NotImplementedError |
| 45 |
|
| 46 |
|
| 47 |
class NamedColumn(AbstractQueryableColumn): |
| 48 |
""" |
| 49 |
A column that is not coupled to a model class, or type |
| 50 |
""" |
| 51 |
|
| 52 |
def __init__(self, name): |
| 53 |
self.name = name |
| 54 |
|
| 55 |
def __unicode__(self): |
| 56 |
return self.name |
| 57 |
|
| 58 |
def _get_column(self): |
| 59 |
""" :rtype: NamedColumn """ |
| 60 |
return self |
| 61 |
|
| 62 |
@property |
| 63 |
def db_field_name(self): |
| 64 |
return self.name |
| 65 |
|
| 66 |
@property |
| 67 |
def cql(self): |
| 68 |
return self.get_cql() |
| 69 |
|
| 70 |
def get_cql(self): |
| 71 |
return '"{0}"'.format(self.name) |
| 72 |
|
| 73 |
def to_database(self, val): |
| 74 |
return val |
| 75 |
|
| 76 |
|
| 77 |
class NamedTable(object): |
| 78 |
""" |
| 79 |
A Table that is not coupled to a model class |
| 80 |
""" |
| 81 |
|
| 82 |
__abstract__ = False |
| 83 |
|
| 84 |
objects = QuerySetDescriptor() |
| 85 |
|
| 86 |
__partition_keys = None |
| 87 |
|
| 88 |
_partition_key_index = None |
| 89 |
|
| 90 |
__connection__ = None |
| 91 |
_connection = None |
| 92 |
|
| 93 |
using = UsingDescriptor() |
| 94 |
|
| 95 |
_get_connection = BaseModel._get_connection |
| 96 |
|
| 97 |
class DoesNotExist(_DoesNotExist): |
| 98 |
pass |
| 99 |
|
| 100 |
class MultipleObjectsReturned(_MultipleObjectsReturned): |
| 101 |
pass |
| 102 |
|
| 103 |
def __init__(self, keyspace, name): |
| 104 |
self.keyspace = keyspace |
| 105 |
self.name = name |
| 106 |
self._connection = None |
| 107 |
|
| 108 |
@property |
| 109 |
def _partition_keys(self): |
| 110 |
if not self.__partition_keys: |
| 111 |
self._get_partition_keys() |
| 112 |
return self.__partition_keys |
| 113 |
|
| 114 |
def _get_partition_keys(self): |
| 115 |
try: |
| 116 |
table_meta = get_cluster(self._get_connection()).metadata.keyspaces[self.keyspace].tables[self.name] |
| 117 |
self.__partition_keys = OrderedDict((pk.name, Column(primary_key=True, partition_key=True, db_field=pk.name)) for pk in table_meta.partition_key) |
| 118 |
except Exception as e: |
| 119 |
raise CQLEngineException("Failed inspecting partition keys for {0}." |
| 120 |
"Ensure cqlengine is connected before attempting this with NamedTable.".format(self.column_family_name())) |
| 121 |
|
| 122 |
def column(self, name): |
| 123 |
return NamedColumn(name) |
| 124 |
|
| 125 |
def column_family_name(self, include_keyspace=True): |
| 126 |
""" |
| 127 |
Returns the column family name if it's been defined |
| 128 |
otherwise, it creates it from the module and class name |
| 129 |
""" |
| 130 |
if include_keyspace: |
| 131 |
return '{0}.{1}'.format(self.keyspace, self.name) |
| 132 |
else: |
| 133 |
return self.name |
| 134 |
|
| 135 |
def _get_column(self, name): |
| 136 |
""" |
| 137 |
Returns the column matching the given name |
| 138 |
|
| 139 |
:rtype: Column |
| 140 |
""" |
| 141 |
return self.column(name) |
| 142 |
|
| 143 |
# def create(self, **kwargs): |
| 144 |
# return self.objects.create(**kwargs) |
| 145 |
|
| 146 |
def all(self): |
| 147 |
return self.objects.all() |
| 148 |
|
| 149 |
def filter(self, *args, **kwargs): |
| 150 |
return self.objects.filter(*args, **kwargs) |
| 151 |
|
| 152 |
def get(self, *args, **kwargs): |
| 153 |
return self.objects.get(*args, **kwargs) |
| 154 |
|
| 155 |
|
| 156 |
class NamedKeyspace(object): |
| 157 |
""" |
| 158 |
A keyspace |
| 159 |
""" |
| 160 |
|
| 161 |
def __init__(self, name): |
| 162 |
self.name = name |
| 163 |
|
| 164 |
def table(self, name): |
| 165 |
""" |
| 166 |
returns a table descriptor with the given |
| 167 |
name that belongs to this keyspace |
| 168 |
""" |
| 169 |
return NamedTable(self.name, name) |