File size: 4,886 Bytes
2a0bc63
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
# Copyright DataStax, Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

from cassandra.util import OrderedDict

from cassandra.cqlengine import CQLEngineException
from cassandra.cqlengine.columns import Column
from cassandra.cqlengine.connection import get_cluster
from cassandra.cqlengine.models import UsingDescriptor, BaseModel
from cassandra.cqlengine.query import AbstractQueryableColumn, SimpleQuerySet
from cassandra.cqlengine.query import DoesNotExist as _DoesNotExist
from cassandra.cqlengine.query import MultipleObjectsReturned as _MultipleObjectsReturned


class QuerySetDescriptor(object):
    """

    returns a fresh queryset for the given model

    it's declared on everytime it's accessed

    """

    def __get__(self, obj, model):
        """ :rtype: ModelQuerySet """
        if model.__abstract__:
            raise CQLEngineException('cannot execute queries against abstract models')
        return SimpleQuerySet(obj)

    def __call__(self, *args, **kwargs):
        """

        Just a hint to IDEs that it's ok to call this



        :rtype: ModelQuerySet

        """
        raise NotImplementedError


class NamedColumn(AbstractQueryableColumn):
    """

    A column that is not coupled to a model class, or type

    """

    def __init__(self, name):
        self.name = name

    def __unicode__(self):
        return self.name

    def _get_column(self):
        """ :rtype: NamedColumn """
        return self

    @property
    def db_field_name(self):
        return self.name

    @property
    def cql(self):
        return self.get_cql()

    def get_cql(self):
        return '"{0}"'.format(self.name)

    def to_database(self, val):
        return val


class NamedTable(object):
    """

    A Table that is not coupled to a model class

    """

    __abstract__ = False

    objects = QuerySetDescriptor()

    __partition_keys = None

    _partition_key_index = None

    __connection__ = None
    _connection = None

    using = UsingDescriptor()

    _get_connection = BaseModel._get_connection

    class DoesNotExist(_DoesNotExist):
        pass

    class MultipleObjectsReturned(_MultipleObjectsReturned):
        pass

    def __init__(self, keyspace, name):
        self.keyspace = keyspace
        self.name = name
        self._connection = None

    @property
    def _partition_keys(self):
        if not self.__partition_keys:
            self._get_partition_keys()
        return self.__partition_keys

    def _get_partition_keys(self):
        try:
            table_meta = get_cluster(self._get_connection()).metadata.keyspaces[self.keyspace].tables[self.name]
            self.__partition_keys = OrderedDict((pk.name, Column(primary_key=True, partition_key=True, db_field=pk.name)) for pk in table_meta.partition_key)
        except Exception as e:
            raise CQLEngineException("Failed inspecting partition keys for {0}."
                                     "Ensure cqlengine is connected before attempting this with NamedTable.".format(self.column_family_name()))

    def column(self, name):
        return NamedColumn(name)

    def column_family_name(self, include_keyspace=True):
        """

        Returns the column family name if it's been defined

        otherwise, it creates it from the module and class name

        """
        if include_keyspace:
            return '{0}.{1}'.format(self.keyspace, self.name)
        else:
            return self.name

    def _get_column(self, name):
        """

        Returns the column matching the given name



        :rtype: Column

        """
        return self.column(name)

    # def create(self, **kwargs):
    #     return self.objects.create(**kwargs)

    def all(self):
        return self.objects.all()

    def filter(self, *args, **kwargs):
        return self.objects.filter(*args, **kwargs)

    def get(self, *args, **kwargs):
        return self.objects.get(*args, **kwargs)


class NamedKeyspace(object):
    """

    A keyspace

    """

    def __init__(self, name):
        self.name = name

    def table(self, name):
        """

        returns a table descriptor with the given

        name that belongs to this keyspace

        """
        return NamedTable(self.name, name)