forked from felipecruz/exemplos
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdomain.py
87 lines (70 loc) · 2.42 KB
/
domain.py
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
import decimal
class Column:
def __init__(self, name, kind, description=""):
self._name = name
self._kind = kind
self._description = description
self._is_pk = False
def __str__(self):
_str = "Col: {} : {} {}".format(self._name,
self._kind,
self._description)
if self._is_pk:
_str = "({}) {}".format("PK", _str)
return _str
@staticmethod
def validate(kind, data):
if kind == 'bigint':
if isinstance(data, int):
return True
return False
elif kind == 'varchar':
if isinstance(data, str):
return True
return False
elif kind == 'numeric':
try:
val = decimal.Decimal(data)
except:
return False
return True
class PrimaryKey(Column):
def __init__(self, table, name, kind, description=None):
super().__init__(name, kind, description=description)
self._is_pk = True
class Relationship:
def __init__(self, name, _from, to, on):
self._name = name
self._from = _from
self._to = to
self._on = on
class DataTable:
def __init__(self, name):
self._name = name
self._columns = []
self._references = []
self._referenced = []
self._data = []
def _get_name(self):
return self._name
def _set_name(self, _name):
self._name = _name
def _del_name(self):
raise AttributeError("Não pode deletar esse atributo")
name = property(_get_name, _set_name, _del_name)
references = property(lambda self: self._references)
referenced = property(lambda self: self._referenced)
def add_column(self, name, kind, description=""):
self._validate_kind(kind)
column = Column(name, kind, description=description)
self._columns.append(column)
return column
def _validate_kind(self, kind):
if not kind in ('bigint', 'numeric', 'varchar'):
raise Exception("Tipo inválido")
def add_references(self, name, to, on):
relationship = Relationship(name, self, to, on)
self._references.append(relationship)
def add_referenced(self, name, by, on):
relationship = Relationship(name, by, self, on)
self._referenced.append(relationship)