Code Duplication    Length = 23-23 lines in 2 locations

opcua/server/history_sql.py 2 locations

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