Code Duplication    Length = 23-23 lines in 2 locations

opcua/server/history_sql.py 2 locations

@@ 142-164 (lines=23) @@
139
            self._datachanges_period[source_id] = period
140
            self._event_fields[source_id] = ev_fields
141
142
            table = self._get_table_name(source_id)
143
            columns = self._get_event_columns(ev_fields)
144
145
            # create a table for the event which will store fields generated by the source object's events
146
            # note that _Timestamp is for SQL query, _EventTypeName is for debugging, be careful not to create event
147
            # properties with these names
148
            try:
149
                _c_new.execute(
150
                    'CREATE TABLE "{tn}" (_Id INTEGER PRIMARY KEY NOT NULL, _Timestamp TIMESTAMP, _EventTypeName TEXT, {co})'
151
                    .format(tn=table, co=columns))
152
153
            except sqlite3.Error as e:
154
                self.logger.info('Historizing SQL Table Creation Error for events from %s: %s', source_id, e)
155
156
            self._conn.commit()
157
158
    def save_event(self, event):
159
        with self._lock:
160
            _c_sub = self._conn.cursor()
161
162
            table = self._get_table_name(event.SourceNode)
163
            columns, placeholders, evtup = self._format_event(event)
164
            event_type = event.EventType  # useful for troubleshooting database
165
166
            # insert the event into the database
167
            try:
@@ 25-47 (lines=23) @@
22
        self.logger = logging.getLogger(__name__)
23
        self._datachanges_period = {}
24
        self._db_file = path
25
        self._lock = Lock()
26
        self._event_fields = {}
27
28
        self._conn = sqlite3.connect(self._db_file, detect_types=sqlite3.PARSE_DECLTYPES, check_same_thread=False)
29
30
    def new_historized_node(self, node_id, period, count=0):
31
        with self._lock:
32
            _c_new = self._conn.cursor()
33
34
            table = self._get_table_name(node_id)
35
36
            self._datachanges_period[node_id] = period, count
37
38
            # create a table for the node which will store attributes of the DataValue object
39
            # note: Value/VariantType TEXT is only for human reading, the actual data is stored in VariantBinary column
40
            try:
41
                _c_new.execute('CREATE TABLE "{tn}" (_Id INTEGER PRIMARY KEY NOT NULL,'
42
                               ' ServerTimestamp TIMESTAMP,'
43
                               ' SourceTimestamp TIMESTAMP,'
44
                               ' StatusCode INTEGER,'
45
                               ' Value TEXT,'
46
                               ' VariantType TEXT,'
47
                               ' VariantBinary BLOB)'.format(tn=table))
48
49
            except sqlite3.Error as e:
50
                self.logger.info('Historizing SQL Table Creation Error for %s: %s', node_id, e)