Passed
Branch master (17b603)
by P.R.
01:31
created

SqlLoaderWriter.filename()   A

Complexity

Conditions 1

Size

Total Lines 8
Code Lines 3

Duplication

Lines 8
Ratio 100 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 3
nop 1
dl 8
loc 8
ccs 0
cts 3
cp 0
crap 2
rs 10
c 0
b 0
f 0
1
"""
2
ETLT
3
4
Copyright 2016 Set Based IT Consultancy
5
6
Licence MIT
7
"""
8
import abc
9
10
from etlt.writer.Writer import Writer
11
12
13 View Code Duplication
class SqlLoaderWriter(Writer):
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
14
    """
15
    Abstract parent class for loading rows to a table in a database using a SQL statement for loading data from file.
16
    """
17
    handlers = {}
18
    """
19
    The handlers for writing objects as a field to a CSV file.
20
21
    :type: dict[str,callable]
22
    """
23
24
    # ------------------------------------------------------------------------------------------------------------------
25
    def __init__(self, filename, encoding='utf8'):
26
        """
27
        Object constructor.
28
29
        :param str filename: The destination file for the rows.
30
        :param str encoding: The encoding of the text of the destination file.
31
        """
32
        Writer.__init__(self)
33
34
        self._filename = filename
35
        """
36
        The name of the destination file.
37
38
        :type: str
39
        """
40
41
        self._encoding = encoding
42
        """
43
        The encoding of the text in the destination file.
44
45
        :type: str
46
        """
47
48
        self._file = None
49
        """
50
        The underling file object.
51
52
        :type: T
53
        """
54
55
    # ------------------------------------------------------------------------------------------------------------------
56
    def __enter__(self):
57
        self._file = open(self._filename, mode='wt', encoding=self._encoding)
58
59
    # ------------------------------------------------------------------------------------------------------------------
60
    def __exit__(self, exc_type, exc_value, traceback):
61
        self._file.close()
62
63
    # ------------------------------------------------------------------------------------------------------------------
64
    @property
65
    def filename(self):
66
        """
67
        Getter for filename.
68
69
        :rtype: str
70
        """
71
        return self._filename
72
73
    # ------------------------------------------------------------------------------------------------------------------
74
    @property
75
    def encoding(self):
76
        """
77
        Getter for encoding.
78
79
        :rtype: str
80
        """
81
        return self._encoding
82
83
    # ------------------------------------------------------------------------------------------------------------------
84
    @abc.abstractmethod
85
    def get_bulk_load_sql(self, table_name):
86
        """
87
        Returns a SQL statement for bulk loading the data writen to the destination file into a table.
88
89
        :param str table_name: The name of the table.
90
91
        :rtype: str
92
        """
93
        raise NotImplementedError()
94
95
    # ------------------------------------------------------------------------------------------------------------------
96
    @staticmethod
97
    def register_handler(class_name, handler):
98
        """
99
        Registers a handler for writing instances of a class as a field to the destination file.
100
101
        :param str class_name: The name of the class.
102
        :param callable handler: The handler. This handler will be called with two arguments: the object which value
103
                                 must be writen to the destination file, the file handler.
104
        """
105
        SqlLoaderWriter.handlers[class_name] = handler
106
107
    # ------------------------------------------------------------------------------------------------------------------
108
    def _write_field(self, value):
109
        """
110
        Write a single field to the destination file.
111
112
        :param T value: The value of the field.
113
        """
114
        class_name = str(value.__class__)
115
        if class_name not in self.handlers:
116
            raise ValueError('No handler has been registered for class: {0!s}'.format(class_name))
117
        handler = self.handlers[class_name]
118
        handler(value, self._file)
119
120
# ----------------------------------------------------------------------------------------------------------------------
121