pystratum_mysql.helper.MySqlDataTypeHelper   A
last analyzed

Complexity

Total Complexity 14

Size/Duplication

Total Lines 104
Duplicated Lines 0 %

Test Coverage

Coverage 20%

Importance

Changes 0
Metric Value
wmc 14
eloc 74
dl 0
loc 104
ccs 5
cts 25
cp 0.2
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
B MySqlDataTypeHelper.column_type_to_python_type() 0 45 7
B MySqlDataTypeHelper.column_type_to_python_type_hint() 0 45 7
1 1
from typing import Any, Dict
2
3 1
from pystratum_common.helper.DataTypeHelper import DataTypeHelper
4
5
6 1
class MySqlDataTypeHelper(DataTypeHelper):
7
    """
8
    Utility class for deriving information based on a MySQL 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 MySQL data type.
15
16
        :param data_type_info: The MySQL data type metadata.
17
        """
18
        if data_type_info['data_type'] in ['tinyint',
19
                                           'smallint',
20
                                           'mediumint',
21
                                           'int',
22
                                           'bigint',
23
                                           'year',
24
                                           'bit']:
25
            return 'int'
26
27
        if data_type_info['data_type'] == 'decimal':
28
            return 'int' if data_type_info['numeric_scale'] == 0 else 'float'
29
30
        if data_type_info['data_type'] in ['float',
31
                                           'double']:
32
            return 'float'
33
34
        if data_type_info['data_type'] in ['char',
35
                                           'varchar',
36
                                           'time',
37
                                           'timestamp',
38
                                           'date',
39
                                           'datetime',
40
                                           'enum',
41
                                           'set',
42
                                           'tinytext',
43
                                           'text',
44
                                           'mediumtext',
45
                                           'longtext']:
46
            return 'str'
47
48
        if data_type_info['data_type'] in ['varbinary',
49
                                           'binary',
50
                                           'tinyblob',
51
                                           'blob',
52
                                           'mediumblob',
53
                                           'longblob', ]:
54
            return 'bytes'
55
56
        raise RuntimeError('Unknown data type {0}'.format(data_type_info['data_type']))
57
58
    # ------------------------------------------------------------------------------------------------------------------
59
    def column_type_to_python_type_hint(self, data_type_info: Dict[str, Any]) -> str:
60
        """
61 1
        Returns the corresponding Python data type hinting of a MySQL data type.
62
63
        :param data_type_info: The MySQL data type metadata.
64
        """
65
        if data_type_info['data_type'] in ['tinyint',
66
                                           'smallint',
67
                                           'mediumint',
68
                                           'int',
69
                                           'bigint',
70
                                           'year',
71
                                           'bit']:
72
            return 'Optional[int]'
73
74
        if data_type_info['data_type'] == 'decimal':
75
            return 'Optional[int]' if data_type_info['numeric_scale'] == 0 else 'Optional[float]'
76
77
        if data_type_info['data_type'] in ['float',
78
                                           'double']:
79
            return 'Optional[float]'
80
81
        if data_type_info['data_type'] in ['char',
82
                                           'varchar',
83
                                           'time',
84
                                           'timestamp',
85
                                           'date',
86
                                           'datetime',
87
                                           'enum',
88
                                           'set',
89
                                           'tinytext',
90
                                           'text',
91
                                           'mediumtext',
92
                                           'longtext']:
93
            return 'Optional[str]'
94
95
        if data_type_info['data_type'] in ['varbinary',
96
                                           'binary',
97
                                           'tinyblob',
98
                                           'blob',
99
                                           'mediumblob',
100
                                           'longblob', ]:
101
            return 'Optional[bytes]'
102
103
        raise RuntimeError('Unknown data type {0}'.format(data_type_info['data_type']))
104
105
# ----------------------------------------------------------------------------------------------------------------------
106