-
Notifications
You must be signed in to change notification settings - Fork 0
/
RelatieTypeUpdater.py
72 lines (59 loc) · 2.58 KB
/
RelatieTypeUpdater.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
from typing import Iterator
from Helpers import peek_generator, turn_list_of_lists_into_string
class RelatieTypeUpdater:
@staticmethod
def update_objects(object_generator: Iterator[dict], connection, safe_insert: bool = False):
object_generator = peek_generator(object_generator)
if object_generator is None:
return
values_array = []
for relatietype_dict in object_generator:
record_array = [f"'{relatietype_dict['uuid']}'",
f"'{relatietype_dict['naam']}'"]
label = relatietype_dict.get('label', '')
definitie = relatietype_dict.get('definitie', '').replace("'", "''")
uri = relatietype_dict.get('uri', '')
nullables_values = [uri, label, definitie]
for nullable_value in nullables_values:
if nullable_value != '':
record_array.append(f"'{nullable_value}'")
else:
record_array.append("NULL")
record_array.append(f"{relatietype_dict['actief']}")
record_array.append(f"{relatietype_dict['gericht']}")
values_array.append(record_array)
values_string = turn_list_of_lists_into_string(values_array)
insert_query = f"""
WITH s (uuid, naam, uri, label, definitie, actief, gericht)
AS (VALUES {values_string}),
t AS (
SELECT uuid::uuid AS uuid, naam, uri, label, definitie, actief, gericht
FROM s),
to_insert AS (
SELECT t.*
FROM t
LEFT JOIN public.relatietypes ON relatietypes.uuid = t.uuid
WHERE relatietypes.uuid IS NULL)
INSERT INTO public.relatietypes (uuid, naam, uri, label, definitie, actief, gericht)
SELECT to_insert.uuid, to_insert.naam, to_insert.uri, to_insert.label, to_insert.definitie, to_insert.actief, to_insert.gericht
FROM to_insert;"""
update_query = f"""
WITH s (uuid, naam, uri, label, definitie, actief, gericht)
AS (VALUES {values_string}),
t AS (
SELECT uuid::uuid AS uuid, naam, uri, label, definitie, actief, gericht
FROM s),
to_update AS (
SELECT t.*
FROM t
LEFT JOIN public.relatietypes ON relatietypes.uuid = t.uuid
WHERE relatietypes.uuid IS NOT NULL)
UPDATE public.relatietypes
SET naam = to_update.naam, uri = to_update.uri, label = to_update.label, definitie = to_update.definitie, actief = to_update.actief,
gericht = to_update.gericht
FROM to_update
WHERE to_update.uuid = relatietypes.uuid;"""
cursor = connection.cursor()
cursor.execute(insert_query)
cursor = connection.cursor()
cursor.execute(update_query)