pystratum_pgsql.helper.PgSqlDataTypeHelper   A
last analyzed

Complexity

Total Complexity 16

Size/Duplication

Total Lines 94
Duplicated Lines 0 %

Test Coverage

Coverage 94.29%

Importance

Changes 0
Metric Value
eloc 56
dl 0
loc 94
ccs 33
cts 35
cp 0.9429
rs 10
c 0
b 0
f 0
wmc 16

2 Methods

Rating   Name   Duplication   Size   Complexity  
B PgSqlDataTypeHelper.column_type_to_python_type_hint() 0 40 8
B PgSqlDataTypeHelper.column_type_to_python_type() 0 40 8
1 1
from typing import Any, Dict
2
3 1
from pystratum_common.helper.DataTypeHelper import DataTypeHelper
4
5
6 1
class PgSqlDataTypeHelper(DataTypeHelper):
7
    """
8
    Utility class for deriving information based on a PostgreSQL data type.
9
    """
10
11
    # ------------------------------------------------------------------------------------------------------------------
12 1
    def column_type_to_python_type(self, data_type_info: Dict[str, Any]) -> str:
13
        """
14
        Returns the corresponding Python data type of a PostgreSQL data type.
15
16
        :param dict data_type_info: The PostgreSQL data type metadata.
17
18
        :rtype: str
19
        """
20 1
        if data_type_info['data_type'] in ['bit',
21
                                           'bigint',
22
                                           'smallint',
23
                                           'integer']:
24 1
            return 'int'
25
26 1
        if data_type_info['data_type'] in ['boolean']:
27 1
            return 'bool'
28
29 1
        if data_type_info['data_type'] in ['date',
30
                                           'time without time zone',
31
                                           'timestamp without time zone']:
32 1
            return 'str'
33
34 1
        if data_type_info['data_type'] == 'numeric':
35 1
            return 'float|int'
36
37 1
        if data_type_info['data_type'] in ['real',
38
                                           'double',
39
                                           'money']:
40 1
            return 'float'
41
42 1
        if data_type_info['data_type'] in ['character',
43
                                           'character varying',
44
                                           'text']:
45 1
            return 'str'
46
47 1
        if data_type_info['data_type'] in ['bytea',
48
                                           'binary']:
49 1
            return 'bytes'
50
51
        raise RuntimeError('Unknown data type {0}'.format(data_type_info['data_type']))
52
53
    # ------------------------------------------------------------------------------------------------------------------
54 1
    def column_type_to_python_type_hint(self, data_type_info: Dict[str, Any]) -> str:
55
        """
56
        Returns the corresponding Python data type hinting of a PostgreSQL data type.
57
58
        :param dict data_type_info: The PostgreSQL data type metadata.
59
60
        :rtype: str
61
        """
62 1
        if data_type_info['data_type'] in ['bit',
63
                                           'bigint',
64
                                           'smallint',
65
                                           'integer']:
66 1
            return 'Optional[int]'
67
68 1
        if data_type_info['data_type'] in ['boolean']:
69 1
            return 'Optional[bool]'
70
71 1
        if data_type_info['data_type'] in ['date',
72
                                           'time without time zone',
73
                                           'timestamp without time zone']:
74 1
            return 'Optional[str]'
75
76 1
        if data_type_info['data_type'] == 'numeric':
77 1
            return 'Union[float, int, None]'
78
79 1
        if data_type_info['data_type'] in ['real',
80
                                           'double',
81
                                           'money']:
82 1
            return 'Optional[float]'
83
84 1
        if data_type_info['data_type'] in ['character',
85
                                           'character varying',
86
                                           'text']:
87 1
            return 'Optional[str]'
88
89 1
        if data_type_info['data_type'] in ['bytea',
90
                                           'binary']:
91 1
            return 'Optional[bytes]'
92
93
        raise RuntimeError('Unknown data type {0}'.format(data_type_info['data_type']))
94
95
# ----------------------------------------------------------------------------------------------------------------------
96