|
24 | 24 | from abc import ABC, abstractmethod |
25 | 25 | from .exception import * |
26 | 26 |
|
| 27 | +# endregion |
| 28 | + |
| 29 | +# region global variables |
| 30 | +API_NAME = 'nosqlapi' |
| 31 | + |
27 | 32 |
|
28 | 33 | # endregion |
29 | 34 |
|
30 | 35 | # region classes |
31 | 36 | class Connection(ABC): |
32 | 37 | """Server connection abstract class""" |
33 | 38 |
|
| 39 | + def __init__(self): |
| 40 | + self._connected = False |
| 41 | + |
| 42 | + @property |
| 43 | + def connected(self): |
| 44 | + return bool(self._connected) |
| 45 | + |
34 | 46 | @abstractmethod |
35 | | - def close(self): |
| 47 | + def close(self, *args, **kwargs): |
36 | 48 | """Delete this object |
37 | 49 |
|
38 | 50 | :return: None |
39 | 51 | """ |
40 | 52 | pass |
41 | 53 |
|
42 | 54 | @abstractmethod |
43 | | - def connect(self): |
| 55 | + def connect(self, *args, **kwargs): |
44 | 56 | """Connect database server |
45 | 57 |
|
46 | 58 | :return: Session object |
@@ -72,13 +84,37 @@ def delete_database(self, *args, **kwargs): |
72 | 84 | pass |
73 | 85 |
|
74 | 86 | @abstractmethod |
75 | | - def databases(self): |
| 87 | + def databases(self, *args, **kwargs): |
76 | 88 | """Get all databases |
77 | 89 |
|
78 | 90 | :return: Response |
79 | 91 | """ |
80 | 92 | pass |
81 | 93 |
|
| 94 | + @abstractmethod |
| 95 | + def show_database(self, *args, **kwargs): |
| 96 | + """Show a database information |
| 97 | +
|
| 98 | + :return : Response object |
| 99 | + """ |
| 100 | + pass |
| 101 | + |
| 102 | + def __repr__(self): |
| 103 | + return f"<{API_NAME} {self.__class__.__name__} object>" |
| 104 | + |
| 105 | + def __str__(self): |
| 106 | + return f"{repr(self)}, connected={self.connected}" |
| 107 | + |
| 108 | + def __bool__(self): |
| 109 | + if self.connected: |
| 110 | + return True |
| 111 | + |
| 112 | + def __enter__(self): |
| 113 | + return self |
| 114 | + |
| 115 | + def __exit__(self, exc_type, exc_val, exc_tb): |
| 116 | + self.close() |
| 117 | + |
82 | 118 |
|
83 | 119 | class Selector(ABC): |
84 | 120 | """Selector abstract class""" |
@@ -140,13 +176,23 @@ def limit(self, value): |
140 | 176 | self._limit = value |
141 | 177 |
|
142 | 178 | @abstractmethod |
143 | | - def build(self): |
| 179 | + def build(self, *args, **kwargs): |
144 | 180 | """Build string query selector |
145 | 181 |
|
146 | 182 | :return: string |
147 | 183 | """ |
148 | 184 | pass |
149 | 185 |
|
| 186 | + def __repr__(self): |
| 187 | + return f"<{API_NAME} {self.__class__.__name__} object>" |
| 188 | + |
| 189 | + def __str__(self): |
| 190 | + return self.build() |
| 191 | + |
| 192 | + def __bool__(self): |
| 193 | + if self.selector: |
| 194 | + return True |
| 195 | + |
150 | 196 |
|
151 | 197 | class Session(ABC): |
152 | 198 | """Server session abstract class""" |
@@ -222,7 +268,7 @@ def delete(self, *args, **kwargs): |
222 | 268 | pass |
223 | 269 |
|
224 | 270 | @abstractmethod |
225 | | - def close(self): |
| 271 | + def close(self, *args, **kwargs): |
226 | 272 | """Delete session |
227 | 273 |
|
228 | 274 | :return: None |
@@ -253,6 +299,22 @@ def revoke(self, *args, **kwargs): |
253 | 299 | """ |
254 | 300 | pass |
255 | 301 |
|
| 302 | + def __repr__(self): |
| 303 | + return f"<{API_NAME} {self.__class__.__name__} object>" |
| 304 | + |
| 305 | + def __str__(self): |
| 306 | + return f"database={self.database}, description={self.description}" |
| 307 | + |
| 308 | + def __bool__(self): |
| 309 | + if self.description: |
| 310 | + return True |
| 311 | + |
| 312 | + def __enter__(self): |
| 313 | + return self |
| 314 | + |
| 315 | + def __exit__(self, exc_type, exc_val, exc_tb): |
| 316 | + self.close() |
| 317 | + |
256 | 318 |
|
257 | 319 | class Response(ABC): |
258 | 320 | """Server response abstract class""" |
@@ -291,7 +353,7 @@ def __str__(self): |
291 | 353 | return str(self.data) |
292 | 354 |
|
293 | 355 | def __repr__(self): |
294 | | - return f'<class {self.__class__.__name__}: data={type(self.data)}, code={self.code}, error={self.error}>' |
| 356 | + return f"<{API_NAME} {self.__class__.__name__} object>" |
295 | 357 |
|
296 | 358 | def __contains__(self, item): |
297 | 359 | return True if item in self.data else False |
@@ -330,4 +392,14 @@ def execute(self, *args, **kwargs): |
330 | 392 | """ |
331 | 393 | pass |
332 | 394 |
|
| 395 | + def __repr__(self): |
| 396 | + return f"<{API_NAME} {self.__class__.__name__} object>" |
| 397 | + |
| 398 | + def __str__(self): |
| 399 | + return str(self.batch) |
| 400 | + |
| 401 | + def __bool__(self): |
| 402 | + if self.batch: |
| 403 | + return True |
| 404 | + |
333 | 405 | # endregion |
0 commit comments