-
Notifications
You must be signed in to change notification settings - Fork 0
/
query.py
135 lines (106 loc) · 3.72 KB
/
query.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
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
"""Provide a class to generate basic SQL commands."""
from typing import Union, Tuple
class Query:
"""A query string."""
__slots__ = ["_select", "_from", "_where", "_orderby", "_limit", "_withas"]
def __init__(self, SELECT: str, FROM: str, WHERE: str = None,
ORDERBY: str = None, LIMIT: Union[int, str] = None,
WITHAS: Tuple[str] = None):
"""Create an instance of Query."""
# Initialize attributes
self._select = None
self._from = None
self._where = None
self._orderby = None
self._limit = None
self._withas = None
# set property managed attributes
self.SELECT = SELECT
self.FROM = FROM
self.WHERE = WHERE
self.ORDERBY = ORDERBY
self.LIMIT = LIMIT
self.WITHAS = WITHAS
def __str__(self) -> str:
"""Create the query string."""
q = (f"{self.WITHAS}"
f"{self.SELECT}"
f" {self.FROM}"
f" {self.WHERE}"
f" {self.ORDERBY}"
f" {self.LIMIT}")
return q
# properties -------------------------------------------------------------
@property
def WITHAS(self) -> str:
"""Return the WITHAS attribute."""
return self._withas
@WITHAS.setter
def WITHAS(self, WITHAS: Tuple[str]) -> None:
"""Set value of WITHAS attribute."""
if WITHAS is None:
self._withas = ""
elif not isinstance(WITHAS, Tuple):
raise TypeError(f"Expected tuple, but {type(WITHAS)} was given.")
elif len(WITHAS) > 2:
raise RuntimeError(f"Expected tuple of length 2, but"
f" len(WITHAS)={len(WITHAS)}.")
else:
WITH, AS = WITHAS
self._withas = f"WITH {WITH} AS ({AS}) "
@property
def SELECT(self) -> str:
"""Return the SELECT attribute."""
return self._select
@SELECT.setter
def SELECT(self, SELECT: str) -> None:
if not isinstance(SELECT, str):
raise TypeError(f"Expected str, but {type(SELECT)} was given.")
else:
self._select = f"SELECT {SELECT}"
@property
def FROM(self) -> str:
"""Return the FROM attribute."""
return self._from
@FROM.setter
def FROM(self, FROM: str) -> None:
if not isinstance(FROM, str):
raise TypeError(f"Expected str, but {type(FROM)} was given.")
else:
self._from = f"FROM {FROM}"
@property
def WHERE(self) -> str:
"""Return the WHERE attribute."""
return self._where
@WHERE.setter
def WHERE(self, WHERE: str) -> None:
if WHERE is None:
self._where = ""
elif not isinstance(WHERE, str):
raise TypeError(f"Expected str, but {type(WHERE)} was given.")
else:
self._where = f"WHERE {WHERE}"
@property
def ORDERBY(self) -> str:
"""Return the ORDERBY attribute."""
return self._orderby
@ORDERBY.setter
def ORDERBY(self, ORDERBY: str) -> None:
if ORDERBY is None:
self._orderby = ""
elif not isinstance(ORDERBY, str):
raise TypeError(f"Expected str, but {type(ORDERBY)} was given.")
else:
self._orderby = f"ORDER BY {ORDERBY}"
@property
def LIMIT(self) -> str:
"""Return the LIMIT attribute."""
return self._limit
@LIMIT.setter
def LIMIT(self, LIMIT: str) -> None:
if LIMIT is None:
self._limit = ""
elif not (isinstance(LIMIT, str) or isinstance(LIMIT, int)):
raise TypeError(f"Expected type str but {type(LIMIT)} was given.")
else:
self._limit = f"LIMIT {LIMIT}"