-
Notifications
You must be signed in to change notification settings - Fork 159
Expand file tree
/
Copy pathsqltypes.py
More file actions
243 lines (200 loc) · 8.1 KB
/
sqltypes.py
File metadata and controls
243 lines (200 loc) · 8.1 KB
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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
# Copyright (c) 2016 The Johns Hopkins University/Applied Physics Laboratory
# All Rights Reserved.
#
# 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 kmip.core import enums
from sqlalchemy import Column, ForeignKey, Integer, String
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import relationship
import sqlalchemy.types as types
Base = declarative_base()
def attribute_append_factory(index_attribute):
def attribute_append(list_container, list_attribute, initiator):
index = getattr(list_container, index_attribute)
list_attribute.index = index
setattr(list_container, index_attribute, index + 1)
return list_attribute
return attribute_append
class UsageMaskType(types.TypeDecorator):
"""
Converts a list of enums.CryptographicUsageMask Enums in an integer
bitmask. This allows the database to only store an integer instead of a
list of enumbs. This also does the reverse of converting an integer bit
mask into a list enums.CryptographicUsageMask Enums.
"""
impl = types.Integer
def process_bind_param(self, value, dialect):
"""
Returns the integer value of the usage mask bitmask. This value is
stored in the database.
Args:
value(list<enums.CryptographicUsageMask>): list of enums in the
usage mask
dialect(string): SQL dialect
"""
bitmask = 0x00
for e in value:
bitmask = bitmask | e.value
return bitmask
def process_result_value(self, value, dialect):
"""
Returns a new list of enums.CryptographicUsageMask Enums. This converts
the integer value into the list of enums.
Args:
value(int): The integer value stored in the database that is used
to create the list of enums.CryptographicUsageMask Enums.
dialect(string): SQL dialect
"""
masks = list()
if value:
for e in enums.CryptographicUsageMask:
if e.value & value:
masks.append(e)
return masks
class EnumType(types.TypeDecorator):
"""
Converts a Python enum to an integer before storing it in the database.
This also does the reverse of converting an integer into an enum object.
This allows enums to be stored in a database.
"""
impl = types.Integer
def __init__(self, cls):
"""
Create a new EnumType. This new EnumType requires a class object in the
constructor. The class is used to construct new instances of the Enum
when the integer value is retrieved from the database.
Args:
cls(class): An Enum class used to create new instances from integer
values.
"""
super(EnumType, self).__init__()
self._cls = cls
def process_bind_param(self, value, dialect):
"""
Returns the integer value of the Enum. This value is stored in the
database.
Args:
value(Enum): An Enum instance whose integer value is to be stored.
dialect(string): SQL dialect
"""
if value:
return value.value
else:
return -1
def process_result_value(self, value, dialect):
"""
Returns a new Enum representing the value stored in the database. The
Enum class type of the returned object is that of the cls parameter in
the __init__ call.
Args:
value(int): The integer value stored in the database that is used
to create the Enum
dialect(string): SQL dialect
"""
if value == -1:
return None
return self._cls(value)
class ManagedObjectName(Base):
__tablename__ = 'managed_object_names'
id = Column('id', Integer, primary_key=True)
mo_uid = Column('mo_uid', Integer, ForeignKey('managed_objects.uid'))
name = Column('name', String)
index = Column('name_index', Integer)
name_type = Column('name_type', EnumType(enums.NameType))
mo = relationship('ManagedObject', back_populates='_names')
def __init__(self, name, index=0,
name_type=enums.NameType.UNINTERPRETED_TEXT_STRING):
self.name = name
self.index = index
self.name_type = name_type
def __repr__(self):
return ("<ManagedObjectName(name='%s', index='%d', type='%s')>" %
(self.name, self.index, self.name_type))
def __eq__(self, other):
if isinstance(other, ManagedObjectName):
if self.name != other.name:
return False
elif self.index != other.index:
return False
elif self.name_type != other.name_type:
return False
else:
return True
else:
return NotImplemented
def __ne__(self, other):
if isinstance(other, ManagedObjectName):
return not (self == other)
else:
return NotImplemented
class ManagedObjectAlternativeName(Base):
__tablename__ = 'managed_object_alternative_names'
id = Column('id', Integer, primary_key=True)
mo_uid = Column('mo_uid', Integer, ForeignKey('managed_objects.uid'))
alternative_name_value = Column('alternative_name_value', String)
alternative_name_type = Column(
'alternative_name_type',
EnumType(enums.AlternativeNameType)
)
mo = relationship('ManagedObject', back_populates='alternative_names')
def __init__(self, alternative_name_value, alternative_name_type):
self.alternative_name_value = alternative_name_value
self.alternative_name_type = alternative_name_type
def __repr__(self):
return ("<ManagedObjectAlternativeName(name='%s', type='%s')>" %
(self.alternative_name_value, self.alternative_name_type))
def __eq__(self, other):
if isinstance(other, ManagedObjectAlternativeName):
if self.alternative_name_value != other.alternative_name_value:
return False
elif self.alternative_name_type != other.alternative_name_type:
return False
else:
return True
else:
return NotImplemented
def __ne__(self, other):
if isinstance(other, ManagedObjectAlternativeName):
return not (self == other)
else:
return NotImplemented
class ManagedObjectCustomAttribute(Base):
__tablename__ = 'managed_object_custom_attributes'
id = Column('id', Integer, primary_key=True)
mo_uid = Column('mo_uid', Integer, ForeignKey('managed_objects.uid'))
attribute_name = Column('attribute_name', String)
attribute_text = Column('attribute_text', String)
mo = relationship('ManagedObject', back_populates='custom_attributes')
def __init__(self, attribute_name, attribute_text):
self.attribute_name = attribute_name
self.attribute_text = attribute_text
def __repr__(self):
return (
"<ManagedObjectCustomAttribute(alternative_name='%s', \
alternative_name_type='%s')>" % (self.attribute_name, self.attribute_text)
)
def __eq__(self, other):
if isinstance(other, ManagedObjectCustomAttribute):
if self.attribute_name != other.attribute_name:
return False
elif self.attribute_text != other.attribute_text:
return False
else:
return True
else:
return NotImplemented
def __ne__(self, other):
if isinstance(other, ManagedObjectCustomAttribute):
return not (self == other)
else:
return NotImplemented