|
1 |
| -from typing import Set, List, Dict |
| 1 | +from typing import List |
2 | 2 |
|
3 | 3 | from flashdb.core.query.exceptions.query_exception import ParseException, EmptyValueError, ValidationError
|
| 4 | +from flashdb.core.query.functions import VALID_FUNCTIONS |
4 | 5 | from flashdb.core.query.keywords.base import Keyword
|
5 | 6 |
|
6 | 7 |
|
| 8 | +class Column(object): |
| 9 | + |
| 10 | + def __init__(self, name: str, alias: str = None, functions: List = None): |
| 11 | + self.name = name |
| 12 | + self.alias = alias |
| 13 | + self.functions = functions |
| 14 | + |
| 15 | + def __eq__(self, other): |
| 16 | + if isinstance(other, self.__class__): |
| 17 | + print(other.__dict__) |
| 18 | + return other.__dict__ == self.__dict__ |
| 19 | + return False |
| 20 | + |
| 21 | + |
7 | 22 | class SelectKeyword(Keyword):
|
8 | 23 |
|
| 24 | + NAME = "name" |
| 25 | + AS = "as" |
| 26 | + FUNCTION = "apply_function" |
| 27 | + FUNCTION_DELIMITER = '>' |
| 28 | + |
9 | 29 | def __init__(self, s_query: List):
|
10 | 30 | self.data = s_query
|
11 | 31 | self.columns = []
|
12 | 32 |
|
13 | 33 | def validate(self) -> "Keyword":
|
14 | 34 | for item in self.data:
|
15 | 35 | keys = item.keys()
|
16 |
| - if 'name' not in keys: |
17 |
| - raise ValidationError("[SELECT] `name` is mandatory") |
18 |
| - if not item['name']: |
19 |
| - raise EmptyValueError(f"[SELECT] Empty `name` is not allowed") |
20 |
| - if 'as' in keys and not item['as']: |
21 |
| - raise EmptyValueError(f"[SELECT] Empty `as` is not allowed") |
22 |
| - # check if function exists |
| 36 | + if self.NAME not in keys: |
| 37 | + raise ValidationError(f"[SELECT] `{self.NAME}` is mandatory") |
| 38 | + if not item[self.NAME]: |
| 39 | + raise EmptyValueError(f"[SELECT] Empty `{self.NAME}` is not allowed") |
| 40 | + for keyword in [self.AS, self.FUNCTION]: |
| 41 | + if keyword in keys and not item[keyword]: |
| 42 | + raise EmptyValueError(f"[SELECT] Empty `{keyword}` is not allowed") |
23 | 43 | return self
|
24 | 44 |
|
25 | 45 | def parse(self):
|
26 | 46 | if not self.data:
|
27 | 47 | return
|
28 | 48 | for item in self.data:
|
29 |
| - self.columns.append({ |
30 |
| - 'name': item['name'], |
31 |
| - 'as': item.get('as', None), |
32 |
| - 'functions': None # create function with a split |
33 |
| - }) |
| 49 | + self.columns.append(Column( |
| 50 | + item[self.NAME], |
| 51 | + item.get(self.AS, None), |
| 52 | + self.parse_functions(item.get(self.FUNCTION, None)) |
| 53 | + )) |
34 | 54 | return self
|
| 55 | + |
| 56 | + def parse_functions(self, raw_functions: str): |
| 57 | + if raw_functions is None: |
| 58 | + return None |
| 59 | + split_functions = raw_functions.split(self.FUNCTION_DELIMITER) |
| 60 | + if any(f not in VALID_FUNCTIONS for f in split_functions): |
| 61 | + raise ParseException(f"[SELECT] {raw_functions} is invalid") |
| 62 | + functions = list() |
| 63 | + for f_name in split_functions: |
| 64 | + functions.append(VALID_FUNCTIONS[f_name]()) |
| 65 | + return functions |
0 commit comments