pystratum_mssql.helper.MsSqlDataTypeHelper   A
last analyzed

Complexity

Total Complexity 14

Size/Duplication

Total Lines 108
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 14
eloc 74
dl 0
loc 108
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
B MsSqlDataTypeHelper.column_type_to_python_type_hint() 0 47 7
B MsSqlDataTypeHelper.column_type_to_python_type() 0 47 7
1
from typing import Any, Dict
2
3
from pystratum_common.helper.DataTypeHelper import DataTypeHelper
4
5
6
class MsSqlDataTypeHelper(DataTypeHelper):
7
    """
8
    Utility class for deriving information based on a SQL Server data type.
9
    """
10
11
    # ------------------------------------------------------------------------------------------------------------------
12
    def column_type_to_python_type(self, data_type_info: Dict[str, Any]) -> str:
13
        """
14
        Returns the corresponding Python data type of a SQL Server data type.
15
16
        :param dict data_type_info: The SQL Server data type metadata.
17
18
        :rtype: str
19
        """
20
        if data_type_info['data_type'] in ['bigint',
21
                                           'bit',
22
                                           'int',
23
                                           'smallint',
24
                                           'tinyint']:
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
                                           'real']:
32
            return 'float'
33
34
        if data_type_info['data_type'] in ['char',
35
                                           'date',
36
                                           'datetime',
37
                                           'datetime2',
38
                                           'datetimeoffset',
39
                                           'money',
40
                                           'nchar',
41
                                           'ntext',
42
                                           'nvarchar',
43
                                           'smalldatetime',
44
                                           'smallmoney',
45
                                           'text',
46
                                           'time',
47
                                           'timestamp',
48
                                           'tinytext',
49
                                           'varchar',
50
                                           'xml']:
51
            return 'str'
52
53
        if data_type_info['data_type'] in ['binary',
54
                                           'image',
55
                                           'varbinary']:
56
            return 'bytes'
57
58
        raise RuntimeError('Unknown data type {0}'.format(data_type_info['data_type']))
59
60
    # ------------------------------------------------------------------------------------------------------------------
61
    def column_type_to_python_type_hint(self, data_type_info: Dict[str, Any]) -> str:
62
        """
63
        Returns the corresponding Python data type hinting of a SQL Server data type.
64
65
        :param dict data_type_info: The PostgreSQL data type metadata.
66
67
        :rtype: str
68
        """
69
        if data_type_info['data_type'] in ['bigint',
70
                                           'bit',
71
                                           'int',
72
                                           'smallint',
73
                                           'tinyint']:
74
            return 'Optional[int]'
75
76
        if data_type_info['data_type'] == 'decimal':
77
            return 'Optional[int]' if data_type_info['numeric_scale'] == 0 else 'Optional[float]'
78
79
        if data_type_info['data_type'] in ['float',
80
                                           'real']:
81
            return 'Optional[float]'
82
83
        if data_type_info['data_type'] in ['char',
84
                                           'date',
85
                                           'datetime',
86
                                           'datetime2',
87
                                           'datetimeoffset',
88
                                           'money',
89
                                           'nchar',
90
                                           'ntext',
91
                                           'nvarchar',
92
                                           'smalldatetime',
93
                                           'smallmoney',
94
                                           'text',
95
                                           'time',
96
                                           'timestamp',
97
                                           'tinytext',
98
                                           'varchar',
99
                                           'xml']:
100
            return 'Optional[str]'
101
102
        if data_type_info['data_type'] in ['binary',
103
                                           'image',
104
                                           'varbinary']:
105
            return 'Optional[bytes]'
106
107
        raise RuntimeError('Unknown data type {0}'.format(data_type_info['data_type']))
108
109
# ----------------------------------------------------------------------------------------------------------------------
110