diff --git a/.docker/docker-compose.yml b/.docker/docker-compose.yml
index 61d455f43ec92974207ba0ae8aadf528e15dfcaa..e83592047b58f440ef531a891ac8cfe6636122bd 100644
--- a/.docker/docker-compose.yml
+++ b/.docker/docker-compose.yml
@@ -27,6 +27,8 @@ services:
       - "10080:10080"
     environment:
       DEBUG: 1
+      CAOSDB_CONFIG__CAOSDB_INTEGRATION_TEST_SUITE_KEY: "_CAOSDB_ADV_TEST_SUITE"
+      CAOSDB_CONFIG_TIMEZONE: "Cuba"
 
 networks:
   caosnet:
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 404424ded153081a1b16d7e5b0923d9284695949..2d6b41c8d47d3e89bc3f08a959ef0e74187dca04 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -8,6 +8,13 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
 
 ### Added ###
 
+- Added `table_json_conversion.export_import_xlsx` with a public function
+  `export_container_to_xlsx`, which exports the data of a given Entity
+  Container to a XLSX file.
+- Added parameters to the JsonSchemaExporter to
+  - add `id` property to all RecordTypes
+  - guess when a reference property probably should be treated like an enum.
+
 ### Changed ###
 
 ### Deprecated ###
diff --git a/README_SETUP.md b/README_SETUP.md
index e52885ff5d4f176cf13a79bbf37dadeb5dcea720..5dbc2341742100adaac2c53d483144e692f64974 100644
--- a/README_SETUP.md
+++ b/README_SETUP.md
@@ -33,16 +33,11 @@ entries `install_requires` and `extras_require`.
 
 ## Run Integration Tests Locally
 
-1. Change directory to `integrationtests/`.
-2. Mount `extroot` to the folder that will be used as extroot. E.g. `sudo mount
-   -o bind extroot ../../caosdb-deploy/profiles/debug/paths/extroot` (or
-   whatever path the extroot of the empty profile to be used is located at).
-3. Start (or restart) an empty (!) CaosDB instance (with the mounted
-   extroot). The database will be cleared during testing, so it's important to
-   use an empty instance. Make sure your configuration for the python caosdb
-   module is correct and allows to connect to the server.
-4. Run `test.sh`. Note that this may modify content of the
+1. Start LinkAhead using the profile in `integrationtests/test_profile/profile.yaml`
+2. Change directory to `integrationtests/`.
+3. Run `test.sh`. Note that this may modify content of the
    `integrationtest/extroot/` directory.
+4. Alternatively, run single tests: `pyest integrationtest/test_foo.py`
 
 ## Code Formatting
 
diff --git a/integrationtests/data/lists_template.xlsx b/integrationtests/data/lists_template.xlsx
new file mode 100644
index 0000000000000000000000000000000000000000..baac7cfe8a9f5b1cf00008cbbe0ef49f01d04436
Binary files /dev/null and b/integrationtests/data/lists_template.xlsx differ
diff --git a/integrationtests/data/multiple_refs_data.json b/integrationtests/data/multiple_refs_data.json
new file mode 100644
index 0000000000000000000000000000000000000000..8ddc56e24eff3d3bf1a2be3598d63d42024e0f7f
--- /dev/null
+++ b/integrationtests/data/multiple_refs_data.json
@@ -0,0 +1,55 @@
+{
+  "Training": [
+    {
+      "name": "Example training with multiple organizations.",
+      "participant": [
+        {
+          "name": null,
+          "full_name": "Petra Participant",
+          "email": "petra@indiscale.com"
+        },
+        {
+          "name": null,
+          "full_name": "Peter",
+          "email": "peter@getlinkahead.com"
+        }
+      ],
+      "date": "2024-03-21 14:12:00+00:00",
+      "url": "www.indiscale.com",
+      "Organisation": [
+        {
+          "name": "World Training Organization",
+          "Country": "US",
+          "Person": [
+            {
+              "name": null,
+              "full_name": "Henry Henderson",
+              "email": "henry@organization.org"
+            },
+            {
+              "name": null,
+              "full_name": "Harry Hamburg",
+              "email": "harry@organization.org"
+            }
+          ]
+        },
+        {
+          "name": "European Training Organisation",
+          "Country": "UK",
+          "Person": [
+            {
+              "name": null,
+              "full_name": "Hermione Harvard",
+              "email": "hermione@organisation.org.uk"
+            },
+            {
+              "name": null,
+              "full_name": "Hazel Harper",
+              "email": "hazel@organisation.org.uk"
+            }
+          ]
+        }
+      ]
+    }
+  ]
+}
diff --git a/integrationtests/data/multiple_refs_template.xlsx b/integrationtests/data/multiple_refs_template.xlsx
new file mode 100644
index 0000000000000000000000000000000000000000..70a60534144af8f115f6e1030eb6066d168aabd1
Binary files /dev/null and b/integrationtests/data/multiple_refs_template.xlsx differ
diff --git a/integrationtests/test.sh b/integrationtests/test.sh
index 952a4dd20c4944c0106cb3450ca2ef3e609d79be..4d611609904d55be3d2acf680be64d1914c4326b 100755
--- a/integrationtests/test.sh
+++ b/integrationtests/test.sh
@@ -99,5 +99,8 @@ python3 -m pytest test_yaml_parser.py
 echo "Testing json-schema exporter"
 python3 -m pytest test_json_schema_exporter.py
 
+echo "Testing XLSX export/import"
+python3 -m pytest test_ex_import_xlsx.py
+
 # Obsolete due to teardown in the above test.
 # echo "/n/n/n YOU NEED TO RESTART THE SERVER TO REDO TESTS!!!"
diff --git a/integrationtests/test_ex_import_xlsx.py b/integrationtests/test_ex_import_xlsx.py
new file mode 100755
index 0000000000000000000000000000000000000000..ef8261f2a9f83a161e4b72a4c53f1c93362818c1
--- /dev/null
+++ b/integrationtests/test_ex_import_xlsx.py
@@ -0,0 +1,479 @@
+# encoding: utf-8
+#
+# This file is a part of the LinkAhead Project.
+#
+# Copyright (C) 2025 Indiscale GmbH <info@indiscale.com>
+# Copyright (C) 2025 Daniel Hornung <d.hornung@indiscale.com>
+#
+# This program is free software: you can redistribute it and/or modify
+# it under the terms of the GNU Affero General Public License as
+# published by the Free Software Foundation, either version 3 of the
+# License, or (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU Affero General Public License for more details.
+#
+# You should have received a copy of the GNU Affero General Public License
+# along with this program. If not, see <https://www.gnu.org/licenses/>.
+
+"""Test export and import to and from XLSX sheets, using the table json conversion package.
+
+Data is partly reused from the unit tests.
+"""
+
+import json
+import os
+import sys
+import pytest
+from datetime import datetime
+from pathlib import Path
+
+from linkahead.utils.register_tests import clear_database, set_test_key
+
+from openpyxl import load_workbook
+
+import linkahead as db
+from caosadvancedtools.models import parser
+from caosadvancedtools.table_json_conversion import export_import_xlsx
+
+sys.path.append(os.path.join(os.path.dirname(__file__), "..", "unittests", "table_json_conversion"))
+from utils import (  # noqa: E402, pylint: disable=wrong-import-position
+    assert_equal_jsons,
+    compare_workbooks,
+    purge_from_json,
+    )  # noqa: E402
+
+set_test_key("_CAOSDB_ADV_TEST_SUITE")
+
+
+def rfp(*pathcomponents):
+    """
+    Return full path.
+    Shorthand convenience function.
+    """
+    return os.path.join(os.path.dirname(__file__), *pathcomponents)
+
+
+def rfp_unittest_data(*pathcomponents):
+    """
+    Return full path, from unittest's `table_json_conversion` data directory.
+    Shorthand convenience function.
+    """
+    parts = Path(__file__).parts
+    inttest_index = list(reversed(parts)).index("integrationtests")
+    if inttest_index >= len(parts):
+        raise ValueError("Implausible path")
+    data_path = Path(*parts[:-inttest_index-1]) / "unittests" / "table_json_conversion" / "data"
+
+    return os.path.join(str(data_path), *pathcomponents)
+
+
+def _create_datamodel(modelfile: str):
+    """Create a data model from a yaml file.
+
+    Parameters
+    ----------
+    modelfile : str
+      File name of the yaml file withb the data model.
+    """
+    model = parser.parse_model_from_yaml(modelfile)
+    model.sync_data_model(noquestion=True)
+
+
+def _insert_multiple_choice_data():
+    """Insert the data from `multiple_choice_data.json`."""
+    # 1. Insert enums.
+    enums = db.Container()
+    for skillname in ["Planning", "Communication", "Evaluation"]:
+        rec = db.Record(name=skillname).add_parent(db.RecordType("Skill"))
+        enums.append(rec)
+    for examname in ["Oral", "Written"]:
+        rec = db.Record(name=examname).add_parent(db.RecordType("ExamType"))
+        enums.append(rec)
+    enums.insert()
+
+    # 2. Insert data from JSON
+    json_data_file = rfp_unittest_data("multiple_choice_data.json")
+    with open(json_data_file, encoding="utf-8") as myfile:
+        json_data = json.load(myfile)
+
+    skills = []
+    for skillname in ["Planning", "Evaluation"]:
+        skills.append(db.Record(skillname).retrieve())
+
+    records = db.Container()
+    training_data = json_data["Training"][0]
+
+    rec_training = db.Record(name=training_data["name"]).add_parent(db.RecordType("Training"))
+    rec_training.add_property("date", datetime.fromisoformat(training_data["date"]))
+    rec_training.add_property("skills", skills)
+    rec_training.add_property("exam_types", [])
+
+    records.append(rec_training)
+    records.insert()
+
+
+def _insert_simple_data():
+    """Insert the data from `simple_data.json`."""
+
+    # 1. Insert enums.
+    enums = db.Container()
+    for orgname in ["ECB", "IMF"]:
+        rec_org = db.Record(name=orgname).add_parent(db.RecordType("Organisation"))
+        enums.append(rec_org)
+    enums.insert()
+
+    # 2. Insert data from JSON
+    json_data_file = rfp_unittest_data("simple_data.json")
+    with open(json_data_file, encoding="utf-8") as myfile:
+        json_data = json.load(myfile)
+
+    training_data = json_data["Training"][0]
+    coaches = []
+    for coach_data in training_data["coach"]:
+        rec_coach = db.Record().add_parent(db.RecordType("Person"))
+        for propname, value in coach_data.items():
+            rec_coach.add_property(propname, value=value)
+        coaches.append(rec_coach)
+    rec_supervisor = db.Record().add_parent(db.RecordType("Person"))
+    for propname, value in training_data["supervisor"].items():
+        rec_supervisor.add_property(propname, value=value)
+    persons = []
+    for person_data in json_data["Person"]:
+        rec_person = db.Record().add_parent(db.RecordType("Person"))
+        for propname, value in person_data.items():
+            rec_person.add_property(propname, value=value)
+        persons.append(rec_person)
+    rec_training = db.Record().add_parent(db.RecordType("Training"))
+    rec_training.add_property("date", datetime.fromisoformat(training_data["date"]))
+    rec_training.add_property("url", training_data["url"])
+    rec_training.add_property("coach", coaches)
+    rec_training.add_property("supervisor", rec_supervisor)
+    rec_training.add_property("duration", training_data["duration"])
+    rec_training.add_property("participants", training_data["participants"])
+    rec_training.add_property("subjects", training_data["subjects"])
+    rec_training.add_property("remote", training_data["remote"])
+
+    cont = db.Container()
+    cont.append(rec_training)
+    cont.append(rec_supervisor)
+    cont.extend(coaches)
+    cont.extend(persons)
+
+    cont.insert()
+
+
+def _insert_multiple_refs_data():
+    """Insert the data from `multiple_refs_data`.
+    """
+    json_data_file = rfp_unittest_data("multiple_refs_data.json")
+    with open(json_data_file, encoding="utf-8") as myfile:
+        json_data = json.load(myfile)
+
+    persons = []
+    organizations = []
+    for organization_data in json_data["Training"][0]["Organisation"]:
+        rec_org = db.Record(name=organization_data["name"]).add_parent(
+            db.RecordType("Organisation"))
+        rec_org.add_property("Country", organization_data["Country"])
+        org_persons = []
+        for person_data in organization_data["Person"]:
+            rec_person = db.Record().add_parent(db.RecordType("Person"))
+            rec_person.add_property("full_name", person_data["full_name"])
+            rec_person.add_property("email", person_data["email"])
+            persons.append(rec_person)
+            org_persons.append(rec_person)
+        rec_org.add_property("Person", org_persons, datatype="LIST<Person>")
+        organizations.append(rec_org)
+
+    participants = []
+    for participant_data in json_data["Training"][0]["participant"]:
+        rec_participant = db.Record().add_parent(db.RecordType("Person"))
+        rec_participant.add_property("full_name", participant_data["full_name"])
+        rec_participant.add_property("email", participant_data["email"])
+        persons.append(rec_participant)
+        participants.append(rec_participant)
+
+    rec_training = db.Record(name=json_data["Training"][0]["name"]
+                             ).add_parent(db.RecordType("Training"))
+    rec_training.add_property("participant", participants)
+    rec_training.add_property("date", datetime.fromisoformat(json_data["Training"][0]["date"]))
+    rec_training.add_property("url", json_data["Training"][0]["url"])
+
+    rec_training.add_property("Organisation", organizations, datatype="LIST<Organisation>")
+
+    cont = db.Container()
+    cont.extend(organizations + persons + [rec_training])
+
+    cont.insert()
+
+    # Check datetime consistency
+    training_retrieved = db.execute_query("FIND Training", unique=True)
+    dt_value = datetime.fromisoformat(training_retrieved.get_property("date").value)
+    assert dt_value == datetime.fromisoformat(json_data["Training"][0]["date"])
+
+
+@pytest.fixture(autouse=True)
+def setup(clear_database):
+    "Create needed test data"
+    # Setup data structure
+    test_rt_0 = db.RecordType(name="Person", description="An observant human.")
+    test_prop_0 = db.Property(name="full_name", datatype=db.TEXT)
+    test_rt_0.add_property(test_prop_0)
+    test_rt_1 = db.RecordType(name="ObservationRecord")
+    test_prop_1 = db.Property(name="date", datatype=db.DATETIME)
+    test_prop_2 = db.Property(name="amount", datatype=db.INTEGER)
+    test_prop_3 = db.Property(name="observer", datatype=test_rt_0)
+    test_rt_1.add_property(test_prop_1).add_property(test_prop_2).add_property(test_prop_3)
+    test_rt_2 = db.RecordType(name="Conference")
+    test_prop_4 = db.Property(name="attendees", datatype=db.LIST(test_rt_0))
+    test_rt_2.add_property(test_prop_4)
+
+    # Setup data
+    test_person_0 = db.Record(name="Person 0").add_parent(test_rt_0).add_property(
+        test_prop_0, value="Their Name")
+    test_person_1 = db.Record(name="Person 1").add_parent(test_rt_0).add_property(
+        test_prop_0, value="Also Name")
+    test_person_2 = db.Record(name="Person 2").add_parent(test_rt_0).add_property(
+        test_prop_0, value="Third Name")
+    test_observation_0 = (db.Record().add_parent(test_rt_1)
+                            .add_property(test_prop_1, value="2025-01-01")
+                            .add_property(test_prop_2, value=5)
+                            .add_property(test_prop_3, value=test_person_1))
+    test_observation_1 = (db.Record().add_parent(test_rt_1)
+                            .add_property(test_prop_1, value="2025-02-02")
+                            .add_property(test_prop_2, value=3)
+                            .add_property(test_prop_3, value=test_person_0))
+    test_observation_2 = (db.Record().add_parent(test_rt_1)
+                            .add_property(test_prop_1, value="2025-03-03")
+                            .add_property(test_prop_2, value=12)
+                            .add_property(test_prop_3, value=test_person_0))
+    test_observation_3 = (db.Record().add_parent(test_rt_1)
+                            .add_property(test_prop_1, value="2025-04-04")
+                            .add_property(test_prop_2, value=0)
+                            .add_property(test_prop_3, value=test_person_2))
+    test_conference_0 = (db.Record(description="Only for Also").add_parent(test_rt_2)
+                         .add_property(test_prop_4, value=[test_person_1]))
+    test_conference_1 = (db.Record(name="Official Conf", description="For everyone")
+                         .add_parent(test_rt_2)
+                         .add_property(test_prop_4,
+                                       value=[test_person_0, test_person_1, test_person_2]))
+    testdata = [test_rt_0, test_rt_1, test_rt_2,
+                test_prop_0, test_prop_1, test_prop_2,
+                test_prop_3, test_prop_4,
+                test_person_0, test_person_1, test_person_2,
+                test_observation_0, test_observation_1, test_observation_2, test_observation_3,
+                test_conference_0, test_conference_1]
+
+    # Insert
+    c = db.Container()
+    c.extend(testdata)
+    c.insert()
+
+
+def test_successful_export():
+    records = next(db.execute_query("FIND Record", page_length=50))
+    tmp_path = Path('temp_test_successful_export.xlsx')
+    assert not tmp_path.exists()
+    try:
+        export_import_xlsx.export_container_to_xlsx(records=records,
+                                                    xlsx_data_filepath=tmp_path)
+        assert tmp_path.is_file()
+    finally:
+        if tmp_path.exists():
+            tmp_path.unlink()
+
+
+def test_export_lists(tmpdir):
+    """Properties of datatype LIST<TEXT/INTEGER/...>."""
+    tmpdir = Path(tmpdir)
+    _create_datamodel(rfp_unittest_data("simple_model.yml"))
+    _insert_simple_data()
+
+    query_result = db.execute_query("FIND Training")
+    export_import_xlsx.export_container_to_xlsx(records=query_result,
+                                                include_referenced_entities=True,
+                                                xlsx_data_filepath=tmpdir / "result.xlsx",
+                                                jsonschema_filepath=tmpdir / "schema.json",
+                                                jsondata_filepath=tmpdir / "data.json",
+                                                xlsx_template_filepath=tmpdir / "template.xlsx",
+                                                )
+
+    # Check: schema
+    with open(tmpdir/"schema.json", encoding="utf-8") as schema_f:
+        schema_generated = json.load(schema_f)
+
+    try:
+        assert len(schema_generated["properties"]) == 1  # Only 'Training' should be top level
+        training_props = schema_generated["properties"]["Training"]["properties"]
+        assert_equal_jsons(training_props["subjects"]["items"],
+                           {"type": ["string", "null"]})
+        if "oneOf" in training_props["coach"]["items"]:
+            raise ValueError("'coach' should be handled as 'do_not_retrieve', no records should "
+                             "have been chosen.")
+        assert_equal_jsons(training_props["coach"]["items"]["properties"]["Organisation"],
+                           {"enum": ["ECB", "IMF"]})
+        assert_equal_jsons(training_props["supervisor"]["properties"]["Organisation"],
+                           {"enum": ["ECB", "IMF"]})
+    except KeyError:
+        print("It seems the generated JSON schema does not have the expected structure!")
+        raise
+
+    # Check: XLSX template
+    template_known_good = load_workbook(rfp("data", "lists_template.xlsx"))
+    template_generated = load_workbook(tmpdir / "template.xlsx")
+    compare_workbooks(template_generated, template_known_good)
+
+    # Check: Data json content
+    with open(rfp_unittest_data("simple_data.json"), encoding="utf-8") as myfile:
+        json_known_good = json.load(myfile)
+        json_known_good.pop("Person")
+    with open(tmpdir / "data.json", encoding="utf-8") as myfile:
+        json_generated = purge_from_json(json.load(myfile), remove_keys=["id"])
+    assert_equal_jsons(json_generated, json_known_good, allow_name_dict=True, ignore_datetime=True)
+
+    # Check: Filled XLSX
+    filled_generated = load_workbook(tmpdir / "result.xlsx")
+    # For the moment: just check a few samples
+    assert filled_generated.sheetnames == ['Training',
+                                           'Training.coach',
+                                           ]
+    sheet_training = filled_generated["Training"]
+    assert sheet_training["K7"].value == "IMF"
+    sheet_coach = filled_generated["Training.coach"]
+    assert sheet_coach["G7"].value == "ECB"
+    assert sheet_coach["G8"].value == "ECB"
+
+
+def test_multiple_choice(tmpdir):
+    """List properties of enum references."""
+    tmpdir = Path(tmpdir)
+    _create_datamodel(rfp_unittest_data("multiple_choice_model.yaml"))
+    _insert_multiple_choice_data()
+
+    query_result = db.execute_query("FIND Training")
+    export_import_xlsx.export_container_to_xlsx(records=query_result,
+                                                include_referenced_entities=True,
+                                                xlsx_data_filepath=tmpdir / "result.xlsx",
+                                                jsonschema_filepath=tmpdir / "schema.json",
+                                                jsondata_filepath=tmpdir / "data.json",
+                                                xlsx_template_filepath=tmpdir / "template.xlsx",
+                                                )
+    # Check: schema
+    with open(tmpdir/"schema.json", encoding="utf-8") as schema_f:
+        schema_generated = json.load(schema_f)
+
+    try:
+        assert len(schema_generated["properties"]) == 1  # Only 'Training' should be top level
+        training = schema_generated["properties"]["Training"]
+        props = training["properties"]
+        assert len(props["skills"]["items"]["enum"]) == 3
+        assert len(props["exam_types"]["items"]["enum"]) == 2
+    except KeyError:
+        print("It seems the generated JSON schema does not have the expected structure!")
+        raise
+
+    # Check: XLSX template
+    template_known_good = load_workbook(rfp_unittest_data("multiple_choice_id_template.xlsx"))
+    template_generated = load_workbook(tmpdir / "template.xlsx")
+    compare_workbooks(template_generated, template_known_good)
+
+    # Check: Data json content
+    with open(rfp_unittest_data("multiple_choice_retrieved_data.json"), encoding="utf-8") as myfile:
+        json_known_good = json.load(myfile)
+    with open(tmpdir / "data.json", encoding="utf-8") as myfile:
+        json_generated = purge_from_json(json.load(myfile), remove_keys=["id"])
+    assert_equal_jsons(json_generated, json_known_good)
+
+    # Check: Filled XLSX
+    filled_generated = load_workbook(tmpdir / "result.xlsx")
+    # For the moment: just check a few samples
+    assert filled_generated.sheetnames == ['Training']
+    sheet_training = filled_generated["Training"]
+    assert sheet_training.max_row == 7
+    assert sheet_training.max_column == 9
+    assert sheet_training["E7"].value == "x"
+    assert sheet_training["F7"].value is None
+    assert sheet_training["G7"].value == "x"
+    assert sheet_training["H7"].value is None
+    assert sheet_training["I7"].value is None
+
+
+def test_export_list_refs(tmpdir):
+    """Test the export to XLSX of list-valued references.
+
+    We retrieve all "Training" Records from LinkAhead and run `export_container_to_xlsx` on the
+    result. This shall create an XLSX template, a JSON schema, a filled JSON, and a filled XLSX.
+    All are checked against our expectation.
+    """
+    tmpdir = Path(tmpdir)
+    # Setup database
+    _create_datamodel(rfp_unittest_data("multiple_refs_model.yml"))
+
+    # Create initial data, from json
+    _insert_multiple_refs_data()
+
+    # Retrieve and export all Training entities
+    query_result = db.execute_query("Find Training")
+    export_import_xlsx.export_container_to_xlsx(records=query_result,
+                                                include_referenced_entities=True,
+                                                xlsx_data_filepath=tmpdir / "result.xlsx",
+                                                jsonschema_filepath=tmpdir / "schema.json",
+                                                jsondata_filepath=tmpdir / "data.json",
+                                                xlsx_template_filepath=tmpdir / "template.xlsx",
+                                                )
+
+    # Check: schema
+    with open(tmpdir/"schema.json", encoding="utf-8") as schema_f:
+        schema_generated = json.load(schema_f)
+
+    try:
+        assert len(schema_generated["properties"]) == 1  # Only 'Training' should be top level
+        training = schema_generated["properties"]["Training"]
+        for props in (training["properties"],
+                      training["properties"]["trainer"]["items"]["properties"],
+                      training["properties"]["participant"]["items"]["properties"],
+                      training["properties"]["Organisation"]["items"]["properties"],
+                      ):
+            assert "id" in props.keys()
+    except KeyError:
+        print("It seems the generated JSON schema does not have the expected structure!")
+        raise
+
+    # Check: XLSX template
+    template_known_good = load_workbook(rfp("data", "multiple_refs_template.xlsx"))
+    template_generated = load_workbook(tmpdir / "template.xlsx")
+    compare_workbooks(template_generated, template_known_good)
+
+    # Check: Data json content
+    with open(rfp("data", "multiple_refs_data.json"), encoding="utf-8") as myfile:
+        json_known_good = json.load(myfile)
+    with open(tmpdir / "data.json", encoding="utf-8") as myfile:
+        json_generated = purge_from_json(json.load(myfile), remove_keys=["id"])
+    assert_equal_jsons(json_generated, json_known_good)
+
+    # Check: Filled XLSX
+    filled_generated = load_workbook(tmpdir / "result.xlsx")
+    # For the moment: just check a few samples
+    assert filled_generated.sheetnames == ['Training',
+                                           'Training.Organisation',
+                                           'Training.Organisation.Person',
+                                           'Training.participant',
+                                           'Training.trainer',
+                                           ]
+    sheet_training = filled_generated["Training"]
+    assert sheet_training.max_row == 7
+    assert sheet_training.max_column == 19
+    # Use same transformation as fill_xlsx for datetime comparison.
+    date = sheet_training["D7"].value
+    assert date == datetime.fromisoformat("2024-03-21 14:12:00+00:00"
+                                          ).astimezone().replace(tzinfo=None)
+
+    sheet_top = filled_generated["Training.Organisation.Person"]
+    assert sheet_top.max_row == 11
+    assert sheet_top.max_column == 7
+    assert sheet_top["B8"].value == sheet_training["B7"].value  # Check foreign key component
+    assert sheet_top["B8"].value == sheet_top["B11"].value
+    assert sheet_top["G10"].value == "hermione@organisation.org.uk"
diff --git a/integrationtests/test_profile/custom/other/restore/caosroot.example.tar.gz b/integrationtests/test_profile/custom/other/restore/caosroot.example.tar.gz
new file mode 100644
index 0000000000000000000000000000000000000000..5e02a693960c64d3c82401e2de4abd72f7fd5fd1
Binary files /dev/null and b/integrationtests/test_profile/custom/other/restore/caosroot.example.tar.gz differ
diff --git a/integrationtests/test_profile/custom/other/restore/restore.dump.sql b/integrationtests/test_profile/custom/other/restore/restore.dump.sql
new file mode 100644
index 0000000000000000000000000000000000000000..d14b75b5a1b8d3af46d7338d1e5e997d6d2f515d
--- /dev/null
+++ b/integrationtests/test_profile/custom/other/restore/restore.dump.sql
@@ -0,0 +1,5821 @@
+-- MariaDB dump 10.19  Distrib 10.11.6-MariaDB, for debian-linux-gnu (x86_64)
+--
+-- Host: sqldb    Database: caosdb
+-- ------------------------------------------------------
+-- Server version	10.5.25-MariaDB-ubu2004
+
+/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;
+/*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */;
+/*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */;
+/*!40101 SET NAMES utf8 */;
+/*!40103 SET @OLD_TIME_ZONE=@@TIME_ZONE */;
+/*!40103 SET TIME_ZONE='+00:00' */;
+/*!40014 SET @OLD_UNIQUE_CHECKS=@@UNIQUE_CHECKS, UNIQUE_CHECKS=0 */;
+/*!40014 SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0 */;
+/*!40101 SET @OLD_SQL_MODE=@@SQL_MODE, SQL_MODE='NO_AUTO_VALUE_ON_ZERO' */;
+/*!40111 SET @OLD_SQL_NOTES=@@SQL_NOTES, SQL_NOTES=0 */;
+
+--
+-- Table structure for table `archive_collection_type`
+--
+
+DROP TABLE IF EXISTS `archive_collection_type`;
+/*!40101 SET @saved_cs_client     = @@character_set_client */;
+/*!40101 SET character_set_client = utf8 */;
+CREATE TABLE `archive_collection_type` (
+  `domain_id` int(10) unsigned NOT NULL,
+  `entity_id` int(10) unsigned NOT NULL,
+  `property_id` int(10) unsigned NOT NULL,
+  `collection` varchar(255) NOT NULL,
+  `_iversion` int(10) unsigned NOT NULL,
+  UNIQUE KEY `archive_collection_type-d-e-p-v` (`domain_id`,`entity_id`,`property_id`,`_iversion`),
+  KEY `domain_id` (`domain_id`,`entity_id`,`_iversion`),
+  KEY `domain_id_2` (`domain_id`,`_iversion`),
+  KEY `entity_id` (`entity_id`),
+  KEY `property_id` (`property_id`),
+  CONSTRAINT `archive_collection_type_ibfk_1` FOREIGN KEY (`domain_id`) REFERENCES `entities` (`id`) ON DELETE CASCADE,
+  CONSTRAINT `archive_collection_type_ibfk_2` FOREIGN KEY (`entity_id`) REFERENCES `entities` (`id`) ON DELETE CASCADE,
+  CONSTRAINT `archive_collection_type_ibfk_3` FOREIGN KEY (`property_id`) REFERENCES `entities` (`id`) ON DELETE CASCADE
+) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
+/*!40101 SET character_set_client = @saved_cs_client */;
+
+--
+-- Dumping data for table `archive_collection_type`
+--
+
+LOCK TABLES `archive_collection_type` WRITE;
+/*!40000 ALTER TABLE `archive_collection_type` DISABLE KEYS */;
+/*!40000 ALTER TABLE `archive_collection_type` ENABLE KEYS */;
+UNLOCK TABLES;
+
+--
+-- Table structure for table `archive_data_type`
+--
+
+DROP TABLE IF EXISTS `archive_data_type`;
+/*!40101 SET @saved_cs_client     = @@character_set_client */;
+/*!40101 SET character_set_client = utf8 */;
+CREATE TABLE `archive_data_type` (
+  `domain_id` int(10) unsigned NOT NULL,
+  `entity_id` int(10) unsigned NOT NULL,
+  `property_id` int(10) unsigned NOT NULL,
+  `datatype` int(10) unsigned NOT NULL,
+  `_iversion` int(10) unsigned NOT NULL,
+  UNIQUE KEY `archive_data_type-d-e-p-v` (`domain_id`,`entity_id`,`property_id`,`_iversion`),
+  KEY `domain_id` (`domain_id`,`entity_id`,`_iversion`),
+  KEY `domain_id_2` (`domain_id`,`_iversion`),
+  KEY `entity_id` (`entity_id`),
+  KEY `property_id` (`property_id`),
+  KEY `datatype` (`datatype`),
+  CONSTRAINT `archive_data_type_ibfk_1` FOREIGN KEY (`domain_id`) REFERENCES `entities` (`id`) ON DELETE CASCADE,
+  CONSTRAINT `archive_data_type_ibfk_2` FOREIGN KEY (`entity_id`) REFERENCES `entities` (`id`) ON DELETE CASCADE,
+  CONSTRAINT `archive_data_type_ibfk_3` FOREIGN KEY (`property_id`) REFERENCES `entities` (`id`) ON DELETE CASCADE,
+  CONSTRAINT `archive_data_type_ibfk_4` FOREIGN KEY (`datatype`) REFERENCES `entities` (`id`) ON DELETE CASCADE
+) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
+/*!40101 SET character_set_client = @saved_cs_client */;
+
+--
+-- Dumping data for table `archive_data_type`
+--
+
+LOCK TABLES `archive_data_type` WRITE;
+/*!40000 ALTER TABLE `archive_data_type` DISABLE KEYS */;
+/*!40000 ALTER TABLE `archive_data_type` ENABLE KEYS */;
+UNLOCK TABLES;
+
+--
+-- Table structure for table `archive_date_data`
+--
+
+DROP TABLE IF EXISTS `archive_date_data`;
+/*!40101 SET @saved_cs_client     = @@character_set_client */;
+/*!40101 SET character_set_client = utf8 */;
+CREATE TABLE `archive_date_data` (
+  `domain_id` int(10) unsigned NOT NULL,
+  `entity_id` int(10) unsigned NOT NULL,
+  `property_id` int(10) unsigned NOT NULL,
+  `value` int(11) NOT NULL,
+  `status` enum('OBLIGATORY','RECOMMENDED','SUGGESTED','FIX') NOT NULL,
+  `pidx` int(10) unsigned NOT NULL,
+  `_iversion` int(10) unsigned NOT NULL,
+  KEY `domain_id` (`domain_id`,`entity_id`,`_iversion`),
+  KEY `domain_id_2` (`domain_id`,`_iversion`),
+  KEY `entity_id` (`entity_id`),
+  KEY `property_id` (`property_id`),
+  CONSTRAINT `archive_date_data_ibfk_1` FOREIGN KEY (`domain_id`) REFERENCES `entities` (`id`) ON DELETE CASCADE,
+  CONSTRAINT `archive_date_data_ibfk_2` FOREIGN KEY (`entity_id`) REFERENCES `entities` (`id`) ON DELETE CASCADE,
+  CONSTRAINT `archive_date_data_ibfk_3` FOREIGN KEY (`property_id`) REFERENCES `entities` (`id`) ON DELETE CASCADE
+) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
+/*!40101 SET character_set_client = @saved_cs_client */;
+
+--
+-- Dumping data for table `archive_date_data`
+--
+
+LOCK TABLES `archive_date_data` WRITE;
+/*!40000 ALTER TABLE `archive_date_data` DISABLE KEYS */;
+/*!40000 ALTER TABLE `archive_date_data` ENABLE KEYS */;
+UNLOCK TABLES;
+
+--
+-- Table structure for table `archive_datetime_data`
+--
+
+DROP TABLE IF EXISTS `archive_datetime_data`;
+/*!40101 SET @saved_cs_client     = @@character_set_client */;
+/*!40101 SET character_set_client = utf8 */;
+CREATE TABLE `archive_datetime_data` (
+  `domain_id` int(10) unsigned NOT NULL,
+  `entity_id` int(10) unsigned NOT NULL,
+  `property_id` int(10) unsigned NOT NULL,
+  `value` bigint(20) NOT NULL,
+  `value_ns` int(10) unsigned DEFAULT NULL,
+  `status` enum('OBLIGATORY','RECOMMENDED','SUGGESTED','FIX') NOT NULL,
+  `pidx` int(10) unsigned NOT NULL,
+  `_iversion` int(10) unsigned NOT NULL,
+  KEY `domain_id` (`domain_id`,`entity_id`,`_iversion`),
+  KEY `domain_id_2` (`domain_id`,`_iversion`),
+  KEY `entity_id` (`entity_id`),
+  KEY `property_id` (`property_id`),
+  CONSTRAINT `archive_datetime_data_ibfk_1` FOREIGN KEY (`domain_id`) REFERENCES `entities` (`id`) ON DELETE CASCADE,
+  CONSTRAINT `archive_datetime_data_ibfk_2` FOREIGN KEY (`entity_id`) REFERENCES `entities` (`id`) ON DELETE CASCADE,
+  CONSTRAINT `archive_datetime_data_ibfk_3` FOREIGN KEY (`property_id`) REFERENCES `entities` (`id`) ON DELETE CASCADE
+) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
+/*!40101 SET character_set_client = @saved_cs_client */;
+
+--
+-- Dumping data for table `archive_datetime_data`
+--
+
+LOCK TABLES `archive_datetime_data` WRITE;
+/*!40000 ALTER TABLE `archive_datetime_data` DISABLE KEYS */;
+/*!40000 ALTER TABLE `archive_datetime_data` ENABLE KEYS */;
+UNLOCK TABLES;
+
+--
+-- Table structure for table `archive_desc_overrides`
+--
+
+DROP TABLE IF EXISTS `archive_desc_overrides`;
+/*!40101 SET @saved_cs_client     = @@character_set_client */;
+/*!40101 SET character_set_client = utf8 */;
+CREATE TABLE `archive_desc_overrides` (
+  `domain_id` int(10) unsigned NOT NULL,
+  `entity_id` int(10) unsigned NOT NULL,
+  `property_id` int(10) unsigned NOT NULL,
+  `description` text NOT NULL,
+  `_iversion` int(10) unsigned NOT NULL,
+  UNIQUE KEY `archive_desc_overrides-d-e-p-v` (`domain_id`,`entity_id`,`property_id`,`_iversion`),
+  KEY `domain_id` (`domain_id`,`entity_id`,`_iversion`),
+  KEY `domain_id_2` (`domain_id`,`_iversion`),
+  KEY `entity_id` (`entity_id`),
+  KEY `property_id` (`property_id`),
+  CONSTRAINT `archive_desc_overrides_ibfk_1` FOREIGN KEY (`domain_id`) REFERENCES `entities` (`id`) ON DELETE CASCADE,
+  CONSTRAINT `archive_desc_overrides_ibfk_2` FOREIGN KEY (`entity_id`) REFERENCES `entities` (`id`) ON DELETE CASCADE,
+  CONSTRAINT `archive_desc_overrides_ibfk_3` FOREIGN KEY (`property_id`) REFERENCES `entities` (`id`) ON DELETE CASCADE
+) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
+/*!40101 SET character_set_client = @saved_cs_client */;
+
+--
+-- Dumping data for table `archive_desc_overrides`
+--
+
+LOCK TABLES `archive_desc_overrides` WRITE;
+/*!40000 ALTER TABLE `archive_desc_overrides` DISABLE KEYS */;
+/*!40000 ALTER TABLE `archive_desc_overrides` ENABLE KEYS */;
+UNLOCK TABLES;
+
+--
+-- Table structure for table `archive_double_data`
+--
+
+DROP TABLE IF EXISTS `archive_double_data`;
+/*!40101 SET @saved_cs_client     = @@character_set_client */;
+/*!40101 SET character_set_client = utf8 */;
+CREATE TABLE `archive_double_data` (
+  `domain_id` int(10) unsigned NOT NULL,
+  `entity_id` int(10) unsigned NOT NULL,
+  `property_id` int(10) unsigned NOT NULL,
+  `value` double NOT NULL,
+  `status` enum('OBLIGATORY','RECOMMENDED','SUGGESTED','FIX') NOT NULL,
+  `pidx` int(10) unsigned NOT NULL,
+  `_iversion` int(10) unsigned NOT NULL,
+  `unit_sig` bigint(20) DEFAULT NULL,
+  KEY `domain_id` (`domain_id`,`entity_id`,`_iversion`),
+  KEY `domain_id_2` (`domain_id`,`_iversion`),
+  KEY `entity_id` (`entity_id`),
+  KEY `property_id` (`property_id`),
+  CONSTRAINT `archive_double_data_ibfk_1` FOREIGN KEY (`domain_id`) REFERENCES `entities` (`id`) ON DELETE CASCADE,
+  CONSTRAINT `archive_double_data_ibfk_2` FOREIGN KEY (`entity_id`) REFERENCES `entities` (`id`) ON DELETE CASCADE,
+  CONSTRAINT `archive_double_data_ibfk_3` FOREIGN KEY (`property_id`) REFERENCES `entities` (`id`) ON DELETE CASCADE
+) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
+/*!40101 SET character_set_client = @saved_cs_client */;
+
+--
+-- Dumping data for table `archive_double_data`
+--
+
+LOCK TABLES `archive_double_data` WRITE;
+/*!40000 ALTER TABLE `archive_double_data` DISABLE KEYS */;
+/*!40000 ALTER TABLE `archive_double_data` ENABLE KEYS */;
+UNLOCK TABLES;
+
+--
+-- Table structure for table `archive_entities`
+--
+
+DROP TABLE IF EXISTS `archive_entities`;
+/*!40101 SET @saved_cs_client     = @@character_set_client */;
+/*!40101 SET character_set_client = utf8 */;
+CREATE TABLE `archive_entities` (
+  `id` int(10) unsigned NOT NULL,
+  `description` text DEFAULT NULL,
+  `role` enum('RECORDTYPE','RECORD','FILE','_REPLACEMENT','PROPERTY','DATATYPE','ROLE','QUERYTEMPLATE') NOT NULL,
+  `acl` int(10) unsigned DEFAULT NULL,
+  `_iversion` int(10) unsigned NOT NULL,
+  PRIMARY KEY (`id`,`_iversion`),
+  KEY `acl` (`acl`),
+  CONSTRAINT `archive_entities_ibfk_1` FOREIGN KEY (`id`, `_iversion`) REFERENCES `entity_version` (`entity_id`, `_iversion`) ON DELETE CASCADE,
+  CONSTRAINT `archive_entities_ibfk_2` FOREIGN KEY (`acl`) REFERENCES `entity_acl` (`id`)
+) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
+/*!40101 SET character_set_client = @saved_cs_client */;
+
+--
+-- Dumping data for table `archive_entities`
+--
+
+LOCK TABLES `archive_entities` WRITE;
+/*!40000 ALTER TABLE `archive_entities` DISABLE KEYS */;
+/*!40000 ALTER TABLE `archive_entities` ENABLE KEYS */;
+UNLOCK TABLES;
+
+--
+-- Table structure for table `archive_enum_data`
+--
+
+DROP TABLE IF EXISTS `archive_enum_data`;
+/*!40101 SET @saved_cs_client     = @@character_set_client */;
+/*!40101 SET character_set_client = utf8 */;
+CREATE TABLE `archive_enum_data` (
+  `domain_id` int(10) unsigned NOT NULL,
+  `entity_id` int(10) unsigned NOT NULL,
+  `property_id` int(10) unsigned NOT NULL,
+  `value` varbinary(255) NOT NULL,
+  `status` enum('OBLIGATORY','RECOMMENDED','SUGGESTED','FIX') NOT NULL,
+  `pidx` int(10) unsigned NOT NULL,
+  `_iversion` int(10) unsigned NOT NULL,
+  KEY `domain_id` (`domain_id`,`entity_id`,`_iversion`),
+  KEY `domain_id_2` (`domain_id`,`_iversion`),
+  KEY `entity_id` (`entity_id`),
+  KEY `property_id` (`property_id`),
+  CONSTRAINT `archive_enum_data_ibfk_1` FOREIGN KEY (`domain_id`) REFERENCES `entities` (`id`) ON DELETE CASCADE,
+  CONSTRAINT `archive_enum_data_ibfk_2` FOREIGN KEY (`entity_id`) REFERENCES `entities` (`id`) ON DELETE CASCADE,
+  CONSTRAINT `archive_enum_data_ibfk_3` FOREIGN KEY (`property_id`) REFERENCES `entities` (`id`) ON DELETE CASCADE
+) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
+/*!40101 SET character_set_client = @saved_cs_client */;
+
+--
+-- Dumping data for table `archive_enum_data`
+--
+
+LOCK TABLES `archive_enum_data` WRITE;
+/*!40000 ALTER TABLE `archive_enum_data` DISABLE KEYS */;
+/*!40000 ALTER TABLE `archive_enum_data` ENABLE KEYS */;
+UNLOCK TABLES;
+
+--
+-- Table structure for table `archive_files`
+--
+
+DROP TABLE IF EXISTS `archive_files`;
+/*!40101 SET @saved_cs_client     = @@character_set_client */;
+/*!40101 SET character_set_client = utf8 */;
+CREATE TABLE `archive_files` (
+  `file_id` int(10) unsigned NOT NULL,
+  `path` text NOT NULL,
+  `size` bigint(20) unsigned NOT NULL,
+  `hash` binary(64) DEFAULT NULL,
+  `_iversion` int(10) unsigned NOT NULL,
+  PRIMARY KEY (`file_id`,`_iversion`),
+  CONSTRAINT `archive_files_ibfk_1` FOREIGN KEY (`file_id`, `_iversion`) REFERENCES `entity_version` (`entity_id`, `_iversion`) ON DELETE CASCADE
+) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
+/*!40101 SET character_set_client = @saved_cs_client */;
+
+--
+-- Dumping data for table `archive_files`
+--
+
+LOCK TABLES `archive_files` WRITE;
+/*!40000 ALTER TABLE `archive_files` DISABLE KEYS */;
+/*!40000 ALTER TABLE `archive_files` ENABLE KEYS */;
+UNLOCK TABLES;
+
+--
+-- Table structure for table `archive_integer_data`
+--
+
+DROP TABLE IF EXISTS `archive_integer_data`;
+/*!40101 SET @saved_cs_client     = @@character_set_client */;
+/*!40101 SET character_set_client = utf8 */;
+CREATE TABLE `archive_integer_data` (
+  `domain_id` int(10) unsigned NOT NULL,
+  `entity_id` int(10) unsigned NOT NULL,
+  `property_id` int(10) unsigned NOT NULL,
+  `value` bigint(20) NOT NULL,
+  `status` enum('OBLIGATORY','RECOMMENDED','SUGGESTED','FIX') NOT NULL,
+  `pidx` int(10) unsigned NOT NULL,
+  `_iversion` int(10) unsigned NOT NULL,
+  `unit_sig` bigint(20) DEFAULT NULL,
+  KEY `domain_id` (`domain_id`,`entity_id`,`_iversion`),
+  KEY `domain_id_2` (`domain_id`,`_iversion`),
+  KEY `entity_id` (`entity_id`),
+  KEY `property_id` (`property_id`),
+  CONSTRAINT `archive_integer_data_ibfk_1` FOREIGN KEY (`domain_id`) REFERENCES `entities` (`id`) ON DELETE CASCADE,
+  CONSTRAINT `archive_integer_data_ibfk_2` FOREIGN KEY (`entity_id`) REFERENCES `entities` (`id`) ON DELETE CASCADE,
+  CONSTRAINT `archive_integer_data_ibfk_3` FOREIGN KEY (`property_id`) REFERENCES `entities` (`id`) ON DELETE CASCADE
+) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
+/*!40101 SET character_set_client = @saved_cs_client */;
+
+--
+-- Dumping data for table `archive_integer_data`
+--
+
+LOCK TABLES `archive_integer_data` WRITE;
+/*!40000 ALTER TABLE `archive_integer_data` DISABLE KEYS */;
+/*!40000 ALTER TABLE `archive_integer_data` ENABLE KEYS */;
+UNLOCK TABLES;
+
+--
+-- Table structure for table `archive_isa`
+--
+
+DROP TABLE IF EXISTS `archive_isa`;
+/*!40101 SET @saved_cs_client     = @@character_set_client */;
+/*!40101 SET character_set_client = utf8 */;
+CREATE TABLE `archive_isa` (
+  `child` int(10) unsigned NOT NULL,
+  `child_iversion` int(10) unsigned NOT NULL,
+  `parent` int(10) unsigned NOT NULL,
+  `direct` tinyint(1) DEFAULT 1,
+  KEY `parent` (`parent`),
+  KEY `child` (`child`,`child_iversion`),
+  CONSTRAINT `archive_isa_ibfk_1` FOREIGN KEY (`parent`) REFERENCES `entities` (`id`) ON DELETE CASCADE,
+  CONSTRAINT `archive_isa_ibfk_2` FOREIGN KEY (`child`, `child_iversion`) REFERENCES `entity_version` (`entity_id`, `_iversion`) ON DELETE CASCADE
+) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
+/*!40101 SET character_set_client = @saved_cs_client */;
+
+--
+-- Dumping data for table `archive_isa`
+--
+
+LOCK TABLES `archive_isa` WRITE;
+/*!40000 ALTER TABLE `archive_isa` DISABLE KEYS */;
+/*!40000 ALTER TABLE `archive_isa` ENABLE KEYS */;
+UNLOCK TABLES;
+
+--
+-- Table structure for table `archive_name_data`
+--
+
+DROP TABLE IF EXISTS `archive_name_data`;
+/*!40101 SET @saved_cs_client     = @@character_set_client */;
+/*!40101 SET character_set_client = utf8 */;
+CREATE TABLE `archive_name_data` (
+  `domain_id` int(10) unsigned NOT NULL,
+  `entity_id` int(10) unsigned NOT NULL,
+  `property_id` int(10) unsigned NOT NULL,
+  `value` varchar(255) NOT NULL,
+  `status` enum('OBLIGATORY','RECOMMENDED','SUGGESTED','FIX') NOT NULL,
+  `pidx` int(10) unsigned NOT NULL,
+  `_iversion` int(10) unsigned NOT NULL,
+  KEY `domain_id` (`domain_id`,`entity_id`,`_iversion`),
+  KEY `domain_id_2` (`domain_id`,`_iversion`),
+  KEY `value` (`value`),
+  KEY `entity_id` (`entity_id`),
+  KEY `property_id` (`property_id`),
+  CONSTRAINT `archive_name_data_ibfk_1` FOREIGN KEY (`domain_id`) REFERENCES `entities` (`id`) ON DELETE CASCADE,
+  CONSTRAINT `archive_name_data_ibfk_2` FOREIGN KEY (`entity_id`) REFERENCES `entities` (`id`) ON DELETE CASCADE,
+  CONSTRAINT `archive_name_data_ibfk_3` FOREIGN KEY (`property_id`) REFERENCES `entities` (`id`) ON DELETE CASCADE
+) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
+/*!40101 SET character_set_client = @saved_cs_client */;
+
+--
+-- Dumping data for table `archive_name_data`
+--
+
+LOCK TABLES `archive_name_data` WRITE;
+/*!40000 ALTER TABLE `archive_name_data` DISABLE KEYS */;
+/*!40000 ALTER TABLE `archive_name_data` ENABLE KEYS */;
+UNLOCK TABLES;
+
+--
+-- Table structure for table `archive_name_overrides`
+--
+
+DROP TABLE IF EXISTS `archive_name_overrides`;
+/*!40101 SET @saved_cs_client     = @@character_set_client */;
+/*!40101 SET character_set_client = utf8 */;
+CREATE TABLE `archive_name_overrides` (
+  `domain_id` int(10) unsigned NOT NULL,
+  `entity_id` int(10) unsigned NOT NULL,
+  `property_id` int(10) unsigned NOT NULL,
+  `name` varchar(255) NOT NULL,
+  `_iversion` int(10) unsigned NOT NULL,
+  UNIQUE KEY `archive_name_overrides-d-e-p-v` (`domain_id`,`entity_id`,`property_id`,`_iversion`),
+  KEY `domain_id` (`domain_id`,`entity_id`,`_iversion`),
+  KEY `domain_id_2` (`domain_id`,`_iversion`),
+  KEY `entity_id` (`entity_id`),
+  KEY `property_id` (`property_id`),
+  CONSTRAINT `archive_name_overrides_ibfk_1` FOREIGN KEY (`domain_id`) REFERENCES `entities` (`id`) ON DELETE CASCADE,
+  CONSTRAINT `archive_name_overrides_ibfk_2` FOREIGN KEY (`entity_id`) REFERENCES `entities` (`id`) ON DELETE CASCADE,
+  CONSTRAINT `archive_name_overrides_ibfk_3` FOREIGN KEY (`property_id`) REFERENCES `entities` (`id`) ON DELETE CASCADE
+) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
+/*!40101 SET character_set_client = @saved_cs_client */;
+
+--
+-- Dumping data for table `archive_name_overrides`
+--
+
+LOCK TABLES `archive_name_overrides` WRITE;
+/*!40000 ALTER TABLE `archive_name_overrides` DISABLE KEYS */;
+/*!40000 ALTER TABLE `archive_name_overrides` ENABLE KEYS */;
+UNLOCK TABLES;
+
+--
+-- Table structure for table `archive_null_data`
+--
+
+DROP TABLE IF EXISTS `archive_null_data`;
+/*!40101 SET @saved_cs_client     = @@character_set_client */;
+/*!40101 SET character_set_client = utf8 */;
+CREATE TABLE `archive_null_data` (
+  `domain_id` int(10) unsigned NOT NULL,
+  `entity_id` int(10) unsigned NOT NULL,
+  `property_id` int(10) unsigned NOT NULL,
+  `status` enum('OBLIGATORY','RECOMMENDED','SUGGESTED','FIX') NOT NULL,
+  `pidx` int(10) unsigned NOT NULL,
+  `_iversion` int(10) unsigned NOT NULL,
+  KEY `domain_id` (`domain_id`,`entity_id`,`_iversion`),
+  KEY `domain_id_2` (`domain_id`,`_iversion`),
+  KEY `entity_id` (`entity_id`),
+  KEY `property_id` (`property_id`),
+  CONSTRAINT `archive_null_data_ibfk_1` FOREIGN KEY (`domain_id`) REFERENCES `entities` (`id`) ON DELETE CASCADE,
+  CONSTRAINT `archive_null_data_ibfk_2` FOREIGN KEY (`entity_id`) REFERENCES `entities` (`id`) ON DELETE CASCADE,
+  CONSTRAINT `archive_null_data_ibfk_3` FOREIGN KEY (`property_id`) REFERENCES `entities` (`id`) ON DELETE CASCADE
+) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
+/*!40101 SET character_set_client = @saved_cs_client */;
+
+--
+-- Dumping data for table `archive_null_data`
+--
+
+LOCK TABLES `archive_null_data` WRITE;
+/*!40000 ALTER TABLE `archive_null_data` DISABLE KEYS */;
+/*!40000 ALTER TABLE `archive_null_data` ENABLE KEYS */;
+UNLOCK TABLES;
+
+--
+-- Table structure for table `archive_query_template_def`
+--
+
+DROP TABLE IF EXISTS `archive_query_template_def`;
+/*!40101 SET @saved_cs_client     = @@character_set_client */;
+/*!40101 SET character_set_client = utf8 */;
+CREATE TABLE `archive_query_template_def` (
+  `id` int(10) unsigned NOT NULL,
+  `definition` mediumtext NOT NULL,
+  `_iversion` int(10) unsigned NOT NULL,
+  PRIMARY KEY (`id`,`_iversion`),
+  CONSTRAINT `archive_query_template_def_ibfk_1` FOREIGN KEY (`id`, `_iversion`) REFERENCES `entity_version` (`entity_id`, `_iversion`) ON DELETE CASCADE
+) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
+/*!40101 SET character_set_client = @saved_cs_client */;
+
+--
+-- Dumping data for table `archive_query_template_def`
+--
+
+LOCK TABLES `archive_query_template_def` WRITE;
+/*!40000 ALTER TABLE `archive_query_template_def` DISABLE KEYS */;
+/*!40000 ALTER TABLE `archive_query_template_def` ENABLE KEYS */;
+UNLOCK TABLES;
+
+--
+-- Table structure for table `archive_reference_data`
+--
+
+DROP TABLE IF EXISTS `archive_reference_data`;
+/*!40101 SET @saved_cs_client     = @@character_set_client */;
+/*!40101 SET character_set_client = utf8 */;
+CREATE TABLE `archive_reference_data` (
+  `domain_id` int(10) unsigned NOT NULL,
+  `entity_id` int(10) unsigned NOT NULL,
+  `property_id` int(10) unsigned NOT NULL,
+  `value` int(10) unsigned NOT NULL,
+  `value_iversion` int(10) unsigned DEFAULT NULL,
+  `status` enum('OBLIGATORY','RECOMMENDED','SUGGESTED','FIX','REPLACEMENT') NOT NULL,
+  `pidx` int(10) unsigned NOT NULL,
+  `_iversion` int(10) unsigned NOT NULL,
+  KEY `domain_id` (`domain_id`,`entity_id`,`_iversion`),
+  KEY `domain_id_2` (`domain_id`,`_iversion`),
+  KEY `entity_id` (`entity_id`),
+  KEY `property_id` (`property_id`),
+  KEY `value` (`value`),
+  CONSTRAINT `archive_reference_data_ibfk_1` FOREIGN KEY (`domain_id`) REFERENCES `entities` (`id`) ON DELETE CASCADE,
+  CONSTRAINT `archive_reference_data_ibfk_2` FOREIGN KEY (`entity_id`) REFERENCES `entities` (`id`) ON DELETE CASCADE,
+  CONSTRAINT `archive_reference_data_ibfk_3` FOREIGN KEY (`property_id`) REFERENCES `entities` (`id`) ON DELETE CASCADE,
+  CONSTRAINT `archive_reference_data_ibfk_4` FOREIGN KEY (`value`) REFERENCES `entities` (`id`) ON DELETE CASCADE
+) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
+/*!40101 SET character_set_client = @saved_cs_client */;
+
+--
+-- Dumping data for table `archive_reference_data`
+--
+
+LOCK TABLES `archive_reference_data` WRITE;
+/*!40000 ALTER TABLE `archive_reference_data` DISABLE KEYS */;
+/*!40000 ALTER TABLE `archive_reference_data` ENABLE KEYS */;
+UNLOCK TABLES;
+
+--
+-- Table structure for table `archive_text_data`
+--
+
+DROP TABLE IF EXISTS `archive_text_data`;
+/*!40101 SET @saved_cs_client     = @@character_set_client */;
+/*!40101 SET character_set_client = utf8 */;
+CREATE TABLE `archive_text_data` (
+  `domain_id` int(10) unsigned NOT NULL,
+  `entity_id` int(10) unsigned NOT NULL,
+  `property_id` int(10) unsigned NOT NULL,
+  `value` text NOT NULL,
+  `status` enum('OBLIGATORY','RECOMMENDED','SUGGESTED','FIX') NOT NULL,
+  `pidx` int(10) unsigned NOT NULL,
+  `_iversion` int(10) unsigned NOT NULL,
+  KEY `domain_id` (`domain_id`,`entity_id`,`_iversion`),
+  KEY `domain_id_2` (`domain_id`,`_iversion`),
+  KEY `entity_id` (`entity_id`),
+  KEY `property_id` (`property_id`),
+  CONSTRAINT `archive_text_data_ibfk_1` FOREIGN KEY (`domain_id`) REFERENCES `entities` (`id`) ON DELETE CASCADE,
+  CONSTRAINT `archive_text_data_ibfk_2` FOREIGN KEY (`entity_id`) REFERENCES `entities` (`id`) ON DELETE CASCADE,
+  CONSTRAINT `archive_text_data_ibfk_3` FOREIGN KEY (`property_id`) REFERENCES `entities` (`id`) ON DELETE CASCADE
+) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
+/*!40101 SET character_set_client = @saved_cs_client */;
+
+--
+-- Dumping data for table `archive_text_data`
+--
+
+LOCK TABLES `archive_text_data` WRITE;
+/*!40000 ALTER TABLE `archive_text_data` DISABLE KEYS */;
+/*!40000 ALTER TABLE `archive_text_data` ENABLE KEYS */;
+UNLOCK TABLES;
+
+--
+-- Table structure for table `collection_type`
+--
+
+DROP TABLE IF EXISTS `collection_type`;
+/*!40101 SET @saved_cs_client     = @@character_set_client */;
+/*!40101 SET character_set_client = utf8 */;
+CREATE TABLE `collection_type` (
+  `domain_id` int(10) unsigned NOT NULL,
+  `entity_id` int(10) unsigned NOT NULL,
+  `property_id` int(10) unsigned NOT NULL,
+  `collection` varchar(255) NOT NULL,
+  UNIQUE KEY `collection_type-d-e-p` (`domain_id`,`entity_id`,`property_id`),
+  KEY `domain_id` (`domain_id`,`entity_id`),
+  KEY `entity_id` (`entity_id`),
+  KEY `property_id` (`property_id`),
+  CONSTRAINT `collection_type_domain_id_entity` FOREIGN KEY (`domain_id`) REFERENCES `entities` (`id`),
+  CONSTRAINT `collection_type_entity_id_entity` FOREIGN KEY (`entity_id`) REFERENCES `entities` (`id`),
+  CONSTRAINT `collection_type_property_id_entity` FOREIGN KEY (`property_id`) REFERENCES `entities` (`id`)
+) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
+/*!40101 SET character_set_client = @saved_cs_client */;
+
+--
+-- Dumping data for table `collection_type`
+--
+
+LOCK TABLES `collection_type` WRITE;
+/*!40000 ALTER TABLE `collection_type` DISABLE KEYS */;
+/*!40000 ALTER TABLE `collection_type` ENABLE KEYS */;
+UNLOCK TABLES;
+
+--
+-- Table structure for table `data_type`
+--
+
+DROP TABLE IF EXISTS `data_type`;
+/*!40101 SET @saved_cs_client     = @@character_set_client */;
+/*!40101 SET character_set_client = utf8 */;
+CREATE TABLE `data_type` (
+  `domain_id` int(10) unsigned NOT NULL,
+  `entity_id` int(10) unsigned NOT NULL,
+  `property_id` int(10) unsigned NOT NULL,
+  `datatype` int(10) unsigned NOT NULL,
+  UNIQUE KEY `datatype_ukey` (`domain_id`,`entity_id`,`property_id`),
+  KEY `name_ov_dom_ent_idx` (`domain_id`,`entity_id`),
+  KEY `datatype_forkey_ent` (`entity_id`),
+  KEY `datatype_forkey_pro` (`property_id`),
+  KEY `datatype_forkey_type` (`datatype`),
+  CONSTRAINT `datatype_forkey_dom` FOREIGN KEY (`domain_id`) REFERENCES `entities` (`id`),
+  CONSTRAINT `datatype_forkey_ent` FOREIGN KEY (`entity_id`) REFERENCES `entities` (`id`),
+  CONSTRAINT `datatype_forkey_pro` FOREIGN KEY (`property_id`) REFERENCES `entities` (`id`),
+  CONSTRAINT `datatype_forkey_type` FOREIGN KEY (`datatype`) REFERENCES `entities` (`id`)
+) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
+/*!40101 SET character_set_client = @saved_cs_client */;
+
+--
+-- Dumping data for table `data_type`
+--
+
+LOCK TABLES `data_type` WRITE;
+/*!40000 ALTER TABLE `data_type` DISABLE KEYS */;
+INSERT INTO `data_type` VALUES
+(0,0,20,14),
+(0,0,21,14),
+(0,0,24,14);
+/*!40000 ALTER TABLE `data_type` ENABLE KEYS */;
+UNLOCK TABLES;
+
+--
+-- Table structure for table `date_data`
+--
+
+DROP TABLE IF EXISTS `date_data`;
+/*!40101 SET @saved_cs_client     = @@character_set_client */;
+/*!40101 SET character_set_client = utf8 */;
+CREATE TABLE `date_data` (
+  `domain_id` int(10) unsigned DEFAULT NULL,
+  `entity_id` int(10) unsigned DEFAULT NULL,
+  `property_id` int(10) unsigned DEFAULT NULL,
+  `value` int(11) NOT NULL,
+  `status` enum('OBLIGATORY','RECOMMENDED','SUGGESTED','FIX') DEFAULT NULL,
+  `pidx` int(10) unsigned NOT NULL DEFAULT 0,
+  KEY `date_data_dom_ent_idx` (`domain_id`,`entity_id`),
+  KEY `date_ov_forkey_ent` (`entity_id`),
+  KEY `date_ov_forkey_pro` (`property_id`),
+  CONSTRAINT `date_ov_forkey_dom` FOREIGN KEY (`domain_id`) REFERENCES `entities` (`id`),
+  CONSTRAINT `date_ov_forkey_ent` FOREIGN KEY (`entity_id`) REFERENCES `entities` (`id`),
+  CONSTRAINT `date_ov_forkey_pro` FOREIGN KEY (`property_id`) REFERENCES `entities` (`id`)
+) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
+/*!40101 SET character_set_client = @saved_cs_client */;
+
+--
+-- Dumping data for table `date_data`
+--
+
+LOCK TABLES `date_data` WRITE;
+/*!40000 ALTER TABLE `date_data` DISABLE KEYS */;
+/*!40000 ALTER TABLE `date_data` ENABLE KEYS */;
+UNLOCK TABLES;
+
+--
+-- Table structure for table `datetime_data`
+--
+
+DROP TABLE IF EXISTS `datetime_data`;
+/*!40101 SET @saved_cs_client     = @@character_set_client */;
+/*!40101 SET character_set_client = utf8 */;
+CREATE TABLE `datetime_data` (
+  `domain_id` int(10) unsigned NOT NULL COMMENT 'Domain.',
+  `entity_id` int(10) unsigned NOT NULL COMMENT 'Entity.',
+  `property_id` int(10) unsigned NOT NULL COMMENT 'Property.',
+  `status` enum('OBLIGATORY','RECOMMENDED','SUGGESTED','FIX','REPLACEMENT') NOT NULL COMMENT 'Status of this statement.',
+  `pidx` int(10) unsigned NOT NULL DEFAULT 0,
+  `value_ns` int(10) unsigned DEFAULT NULL,
+  `value` bigint(20) NOT NULL,
+  KEY `domain_id` (`domain_id`,`entity_id`),
+  KEY `dat_entity_id_entity` (`entity_id`),
+  KEY `dat_property_id_entity` (`property_id`),
+  CONSTRAINT `dat_domain_id_entity` FOREIGN KEY (`domain_id`) REFERENCES `entities` (`id`),
+  CONSTRAINT `dat_entity_id_entity` FOREIGN KEY (`entity_id`) REFERENCES `entities` (`id`),
+  CONSTRAINT `dat_property_id_entity` FOREIGN KEY (`property_id`) REFERENCES `entities` (`id`)
+) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
+/*!40101 SET character_set_client = @saved_cs_client */;
+
+--
+-- Dumping data for table `datetime_data`
+--
+
+LOCK TABLES `datetime_data` WRITE;
+/*!40000 ALTER TABLE `datetime_data` DISABLE KEYS */;
+/*!40000 ALTER TABLE `datetime_data` ENABLE KEYS */;
+UNLOCK TABLES;
+
+--
+-- Table structure for table `desc_overrides`
+--
+
+DROP TABLE IF EXISTS `desc_overrides`;
+/*!40101 SET @saved_cs_client     = @@character_set_client */;
+/*!40101 SET character_set_client = utf8 */;
+CREATE TABLE `desc_overrides` (
+  `domain_id` int(10) unsigned DEFAULT NULL,
+  `entity_id` int(10) unsigned DEFAULT NULL,
+  `property_id` int(10) unsigned DEFAULT NULL,
+  `description` text DEFAULT NULL,
+  UNIQUE KEY `desc_ov_ukey` (`domain_id`,`entity_id`,`property_id`),
+  KEY `desc_ov_dom_ent_idx` (`domain_id`,`entity_id`),
+  KEY `desc_ov_forkey_ent` (`entity_id`),
+  KEY `desc_ov_forkey_pro` (`property_id`),
+  CONSTRAINT `desc_ov_forkey_dom` FOREIGN KEY (`domain_id`) REFERENCES `entities` (`id`),
+  CONSTRAINT `desc_ov_forkey_ent` FOREIGN KEY (`entity_id`) REFERENCES `entities` (`id`),
+  CONSTRAINT `desc_ov_forkey_pro` FOREIGN KEY (`property_id`) REFERENCES `entities` (`id`)
+) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
+/*!40101 SET character_set_client = @saved_cs_client */;
+
+--
+-- Dumping data for table `desc_overrides`
+--
+
+LOCK TABLES `desc_overrides` WRITE;
+/*!40000 ALTER TABLE `desc_overrides` DISABLE KEYS */;
+/*!40000 ALTER TABLE `desc_overrides` ENABLE KEYS */;
+UNLOCK TABLES;
+
+--
+-- Table structure for table `double_data`
+--
+
+DROP TABLE IF EXISTS `double_data`;
+/*!40101 SET @saved_cs_client     = @@character_set_client */;
+/*!40101 SET character_set_client = utf8 */;
+CREATE TABLE `double_data` (
+  `domain_id` int(10) unsigned NOT NULL COMMENT 'Domain.',
+  `entity_id` int(10) unsigned NOT NULL COMMENT 'Entity.',
+  `property_id` int(10) unsigned NOT NULL COMMENT 'Property.',
+  `value` double NOT NULL,
+  `status` enum('OBLIGATORY','RECOMMENDED','SUGGESTED','FIX','REPLACEMENT') NOT NULL COMMENT 'Status of this statement.',
+  `pidx` int(10) unsigned NOT NULL DEFAULT 0,
+  `unit_sig` bigint(20) DEFAULT NULL,
+  KEY `domain_id` (`domain_id`,`entity_id`),
+  KEY `dou_entity_id_entity` (`entity_id`),
+  KEY `dou_property_id_entity` (`property_id`),
+  CONSTRAINT `dou_domain_id_entity` FOREIGN KEY (`domain_id`) REFERENCES `entities` (`id`),
+  CONSTRAINT `dou_entity_id_entity` FOREIGN KEY (`entity_id`) REFERENCES `entities` (`id`),
+  CONSTRAINT `dou_property_id_entity` FOREIGN KEY (`property_id`) REFERENCES `entities` (`id`)
+) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
+/*!40101 SET character_set_client = @saved_cs_client */;
+
+--
+-- Dumping data for table `double_data`
+--
+
+LOCK TABLES `double_data` WRITE;
+/*!40000 ALTER TABLE `double_data` DISABLE KEYS */;
+/*!40000 ALTER TABLE `double_data` ENABLE KEYS */;
+UNLOCK TABLES;
+
+--
+-- Table structure for table `entities`
+--
+
+DROP TABLE IF EXISTS `entities`;
+/*!40101 SET @saved_cs_client     = @@character_set_client */;
+/*!40101 SET character_set_client = utf8 */;
+CREATE TABLE `entities` (
+  `id` int(10) unsigned NOT NULL AUTO_INCREMENT COMMENT 'Unique identifier.',
+  `description` text DEFAULT NULL,
+  `role` enum('RECORDTYPE','RECORD','FILE','_REPLACEMENT','PROPERTY','DATATYPE','ROLE','QUERYTEMPLATE') NOT NULL,
+  `acl` int(10) unsigned DEFAULT NULL COMMENT 'Access Control List for the entity.',
+  PRIMARY KEY (`id`),
+  KEY `entity_entity_acl` (`acl`),
+  CONSTRAINT `entity_entity_acl` FOREIGN KEY (`acl`) REFERENCES `entity_acl` (`id`)
+) ENGINE=InnoDB AUTO_INCREMENT=100 DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
+/*!40101 SET character_set_client = @saved_cs_client */;
+
+--
+-- Dumping data for table `entities`
+--
+
+LOCK TABLES `entities` WRITE;
+/*!40000 ALTER TABLE `entities` DISABLE KEYS */;
+INSERT INTO `entities` VALUES
+(0,'The default domain.','ROLE',0),
+(1,'The default recordtype.','ROLE',0),
+(2,'The default record.','ROLE',0),
+(3,'The default file.','ROLE',0),
+(4,'The default property.','ROLE',0),
+(7,'The default datatype.','ROLE',0),
+(8,'The QueryTemplate role.','ROLE',0),
+(11,'The default reference data type.','DATATYPE',0),
+(12,'The default integer data type.','DATATYPE',0),
+(13,'The default double data type.','DATATYPE',0),
+(14,'The default text data type.','DATATYPE',0),
+(15,'The default datetime data type.','DATATYPE',0),
+(16,'The default timespan data type.','DATATYPE',0),
+(17,'The default file reference data type.','DATATYPE',0),
+(18,'The defaulf boolean data type','DATATYPE',0),
+(20,'Name of an entity','PROPERTY',0),
+(21,'Unit of an entity.','PROPERTY',0),
+(24,'Description of an entity.','PROPERTY',0);
+/*!40000 ALTER TABLE `entities` ENABLE KEYS */;
+UNLOCK TABLES;
+
+--
+-- Table structure for table `entity_acl`
+--
+
+DROP TABLE IF EXISTS `entity_acl`;
+/*!40101 SET @saved_cs_client     = @@character_set_client */;
+/*!40101 SET character_set_client = utf8 */;
+CREATE TABLE `entity_acl` (
+  `id` int(10) unsigned NOT NULL AUTO_INCREMENT,
+  `acl` varbinary(65525) NOT NULL,
+  PRIMARY KEY (`id`),
+  KEY `entity_acl_acl` (`acl`(3072))
+) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
+/*!40101 SET character_set_client = @saved_cs_client */;
+
+--
+-- Dumping data for table `entity_acl`
+--
+
+LOCK TABLES `entity_acl` WRITE;
+/*!40000 ALTER TABLE `entity_acl` DISABLE KEYS */;
+INSERT INTO `entity_acl` VALUES
+(0,'');
+/*!40000 ALTER TABLE `entity_acl` ENABLE KEYS */;
+UNLOCK TABLES;
+
+--
+-- Table structure for table `entity_ids`
+--
+
+DROP TABLE IF EXISTS `entity_ids`;
+/*!40101 SET @saved_cs_client     = @@character_set_client */;
+/*!40101 SET character_set_client = utf8 */;
+CREATE TABLE `entity_ids` (
+  `id` varchar(255) NOT NULL,
+  `internal_id` int(10) unsigned NOT NULL COMMENT 'Internal ID of an entity. This id is used internally in the *_data tables and elsewhere. This ID is never exposed via the CaosDB API.',
+  PRIMARY KEY (`id`),
+  KEY `entity_ids_internal_id` (`internal_id`),
+  CONSTRAINT `entity_ids_internal_id` FOREIGN KEY (`internal_id`) REFERENCES `entities` (`id`)
+) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin;
+/*!40101 SET character_set_client = @saved_cs_client */;
+
+--
+-- Dumping data for table `entity_ids`
+--
+
+LOCK TABLES `entity_ids` WRITE;
+/*!40000 ALTER TABLE `entity_ids` DISABLE KEYS */;
+INSERT INTO `entity_ids` VALUES
+('1',1),
+('2',2),
+('3',3),
+('4',4),
+('7',7),
+('8',8),
+('11',11),
+('12',12),
+('13',13),
+('14',14),
+('15',15),
+('16',16),
+('17',17),
+('18',18),
+('20',20),
+('21',21),
+('24',24);
+/*!40000 ALTER TABLE `entity_ids` ENABLE KEYS */;
+UNLOCK TABLES;
+
+--
+-- Table structure for table `entity_version`
+--
+
+DROP TABLE IF EXISTS `entity_version`;
+/*!40101 SET @saved_cs_client     = @@character_set_client */;
+/*!40101 SET character_set_client = utf8 */;
+CREATE TABLE `entity_version` (
+  `entity_id` int(10) unsigned NOT NULL,
+  `hash` varbinary(255) DEFAULT NULL,
+  `version` varbinary(255) NOT NULL,
+  `_iversion` int(10) unsigned NOT NULL,
+  `_ipparent` int(10) unsigned DEFAULT NULL,
+  `srid` varbinary(255) NOT NULL,
+  PRIMARY KEY (`entity_id`,`_iversion`),
+  UNIQUE KEY `entity_version-e-v` (`entity_id`,`version`),
+  KEY `srid` (`srid`),
+  CONSTRAINT `entity_version_ibfk_1` FOREIGN KEY (`entity_id`) REFERENCES `entities` (`id`) ON DELETE CASCADE,
+  CONSTRAINT `entity_version_ibfk_2` FOREIGN KEY (`srid`) REFERENCES `transactions` (`srid`)
+) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
+/*!40101 SET character_set_client = @saved_cs_client */;
+
+--
+-- Dumping data for table `entity_version`
+--
+
+LOCK TABLES `entity_version` WRITE;
+/*!40000 ALTER TABLE `entity_version` DISABLE KEYS */;
+INSERT INTO `entity_version` VALUES
+(0,NULL,'a22e13cc0a0d6abc63a520b038ffb11f937f2fe5',1,NULL,'cf83e1357eefb8bdf1542850d66d8007d620e4050b5715dc83f4a921d36ce9ce47d0d13c5d85f2b0ff8318d2877eec2f63b931bd47417a81a538327af927da3e'),
+(1,NULL,'8206d5a37ccedf6e2101274cacabf613c464963e',1,NULL,'cf83e1357eefb8bdf1542850d66d8007d620e4050b5715dc83f4a921d36ce9ce47d0d13c5d85f2b0ff8318d2877eec2f63b931bd47417a81a538327af927da3e'),
+(2,NULL,'5ff5012d83e097bd915bde08d50ef1570b868bba',1,NULL,'cf83e1357eefb8bdf1542850d66d8007d620e4050b5715dc83f4a921d36ce9ce47d0d13c5d85f2b0ff8318d2877eec2f63b931bd47417a81a538327af927da3e'),
+(3,NULL,'03f4ee27316f17c90e421cd474cc0004defddf47',1,NULL,'cf83e1357eefb8bdf1542850d66d8007d620e4050b5715dc83f4a921d36ce9ce47d0d13c5d85f2b0ff8318d2877eec2f63b931bd47417a81a538327af927da3e'),
+(4,NULL,'8dfef42117275fbc475b607f95b19810f4de2de5',1,NULL,'cf83e1357eefb8bdf1542850d66d8007d620e4050b5715dc83f4a921d36ce9ce47d0d13c5d85f2b0ff8318d2877eec2f63b931bd47417a81a538327af927da3e'),
+(7,NULL,'0a283af5f5b162a36744b2043dc6caeabb881e25',1,NULL,'cf83e1357eefb8bdf1542850d66d8007d620e4050b5715dc83f4a921d36ce9ce47d0d13c5d85f2b0ff8318d2877eec2f63b931bd47417a81a538327af927da3e'),
+(8,NULL,'8acd25b0ce6fbdb9841bb214f60a06f5dc31bc3f',1,NULL,'cf83e1357eefb8bdf1542850d66d8007d620e4050b5715dc83f4a921d36ce9ce47d0d13c5d85f2b0ff8318d2877eec2f63b931bd47417a81a538327af927da3e'),
+(11,NULL,'be63da8ec865803eabcdb6bba4d2156cbaee524a',1,NULL,'cf83e1357eefb8bdf1542850d66d8007d620e4050b5715dc83f4a921d36ce9ce47d0d13c5d85f2b0ff8318d2877eec2f63b931bd47417a81a538327af927da3e'),
+(12,NULL,'87f06890501ab9a4c767c2c662e240e8b9749d01',1,NULL,'cf83e1357eefb8bdf1542850d66d8007d620e4050b5715dc83f4a921d36ce9ce47d0d13c5d85f2b0ff8318d2877eec2f63b931bd47417a81a538327af927da3e'),
+(13,NULL,'3c8c0e26528d4f38defcfdcf4bb767327eb4f295',1,NULL,'cf83e1357eefb8bdf1542850d66d8007d620e4050b5715dc83f4a921d36ce9ce47d0d13c5d85f2b0ff8318d2877eec2f63b931bd47417a81a538327af927da3e'),
+(14,NULL,'d41b564762eda4fc3405cd45dd6f1e74727b27dc',1,NULL,'cf83e1357eefb8bdf1542850d66d8007d620e4050b5715dc83f4a921d36ce9ce47d0d13c5d85f2b0ff8318d2877eec2f63b931bd47417a81a538327af927da3e'),
+(15,NULL,'b060268f9575814bffbeeaec3af4eac32cbc32b3',1,NULL,'cf83e1357eefb8bdf1542850d66d8007d620e4050b5715dc83f4a921d36ce9ce47d0d13c5d85f2b0ff8318d2877eec2f63b931bd47417a81a538327af927da3e'),
+(16,NULL,'3639e7d0691aad743b283c340c183e6bbd46ec68',1,NULL,'cf83e1357eefb8bdf1542850d66d8007d620e4050b5715dc83f4a921d36ce9ce47d0d13c5d85f2b0ff8318d2877eec2f63b931bd47417a81a538327af927da3e'),
+(17,NULL,'e3e837ac410cd07ede3ca7a7e6072488c4313b96',1,NULL,'cf83e1357eefb8bdf1542850d66d8007d620e4050b5715dc83f4a921d36ce9ce47d0d13c5d85f2b0ff8318d2877eec2f63b931bd47417a81a538327af927da3e'),
+(18,NULL,'14b349a86c04ec27ec0035c1cbeb92f700342236',1,NULL,'cf83e1357eefb8bdf1542850d66d8007d620e4050b5715dc83f4a921d36ce9ce47d0d13c5d85f2b0ff8318d2877eec2f63b931bd47417a81a538327af927da3e'),
+(20,NULL,'9030ad4b0f91c832ddb4a7c9d9b090c1f035b1d3',1,NULL,'cf83e1357eefb8bdf1542850d66d8007d620e4050b5715dc83f4a921d36ce9ce47d0d13c5d85f2b0ff8318d2877eec2f63b931bd47417a81a538327af927da3e'),
+(21,NULL,'6a693e36a2afae1cbc571cce52c1676065c1ed24',1,NULL,'cf83e1357eefb8bdf1542850d66d8007d620e4050b5715dc83f4a921d36ce9ce47d0d13c5d85f2b0ff8318d2877eec2f63b931bd47417a81a538327af927da3e'),
+(24,NULL,'d78e2b50954d74d6946d2c113cb7393b3b6146f3',1,NULL,'cf83e1357eefb8bdf1542850d66d8007d620e4050b5715dc83f4a921d36ce9ce47d0d13c5d85f2b0ff8318d2877eec2f63b931bd47417a81a538327af927da3e');
+/*!40000 ALTER TABLE `entity_version` ENABLE KEYS */;
+UNLOCK TABLES;
+
+--
+-- Table structure for table `enum_data`
+--
+
+DROP TABLE IF EXISTS `enum_data`;
+/*!40101 SET @saved_cs_client     = @@character_set_client */;
+/*!40101 SET character_set_client = utf8 */;
+CREATE TABLE `enum_data` (
+  `domain_id` int(10) unsigned DEFAULT NULL,
+  `entity_id` int(10) unsigned DEFAULT NULL,
+  `property_id` int(10) unsigned DEFAULT NULL,
+  `value` varbinary(255) NOT NULL,
+  `status` enum('OBLIGATORY','RECOMMENDED','SUGGESTED','FIX') DEFAULT NULL,
+  `pidx` int(10) unsigned NOT NULL DEFAULT 0,
+  KEY `enum_ov_dom_ent_idx` (`domain_id`,`entity_id`),
+  KEY `enum_ov_forkey_ent` (`entity_id`),
+  KEY `enum_ov_forkey_pro` (`property_id`),
+  CONSTRAINT `enum_ov_forkey_dom` FOREIGN KEY (`domain_id`) REFERENCES `entities` (`id`),
+  CONSTRAINT `enum_ov_forkey_ent` FOREIGN KEY (`entity_id`) REFERENCES `entities` (`id`),
+  CONSTRAINT `enum_ov_forkey_pro` FOREIGN KEY (`property_id`) REFERENCES `entities` (`id`)
+) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
+/*!40101 SET character_set_client = @saved_cs_client */;
+
+--
+-- Dumping data for table `enum_data`
+--
+
+LOCK TABLES `enum_data` WRITE;
+/*!40000 ALTER TABLE `enum_data` DISABLE KEYS */;
+/*!40000 ALTER TABLE `enum_data` ENABLE KEYS */;
+UNLOCK TABLES;
+
+--
+-- Table structure for table `feature_config`
+--
+
+DROP TABLE IF EXISTS `feature_config`;
+/*!40101 SET @saved_cs_client     = @@character_set_client */;
+/*!40101 SET character_set_client = utf8 */;
+CREATE TABLE `feature_config` (
+  `_key` varchar(255) NOT NULL,
+  `_value` varchar(255) DEFAULT NULL,
+  PRIMARY KEY (`_key`)
+) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
+/*!40101 SET character_set_client = @saved_cs_client */;
+
+--
+-- Dumping data for table `feature_config`
+--
+
+LOCK TABLES `feature_config` WRITE;
+/*!40000 ALTER TABLE `feature_config` DISABLE KEYS */;
+INSERT INTO `feature_config` VALUES
+('ENTITY_VERSIONING','ENABLED');
+/*!40000 ALTER TABLE `feature_config` ENABLE KEYS */;
+UNLOCK TABLES;
+
+--
+-- Table structure for table `files`
+--
+
+DROP TABLE IF EXISTS `files`;
+/*!40101 SET @saved_cs_client     = @@character_set_client */;
+/*!40101 SET character_set_client = utf8 */;
+CREATE TABLE `files` (
+  `file_id` int(10) unsigned NOT NULL COMMENT 'The file''s ID.',
+  `path` varchar(255) NOT NULL COMMENT 'Directory of the file.',
+  `size` bigint(20) unsigned NOT NULL COMMENT 'Size in kB (oktet bytes).',
+  `hash` binary(64) DEFAULT NULL,
+  `checked_timestamp` bigint(20) NOT NULL DEFAULT 0,
+  PRIMARY KEY (`file_id`),
+  CONSTRAINT `fil_file_id_entity` FOREIGN KEY (`file_id`) REFERENCES `entities` (`id`)
+) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
+/*!40101 SET character_set_client = @saved_cs_client */;
+
+--
+-- Dumping data for table `files`
+--
+
+LOCK TABLES `files` WRITE;
+/*!40000 ALTER TABLE `files` DISABLE KEYS */;
+/*!40000 ALTER TABLE `files` ENABLE KEYS */;
+UNLOCK TABLES;
+
+--
+-- Table structure for table `integer_data`
+--
+
+DROP TABLE IF EXISTS `integer_data`;
+/*!40101 SET @saved_cs_client     = @@character_set_client */;
+/*!40101 SET character_set_client = utf8 */;
+CREATE TABLE `integer_data` (
+  `domain_id` int(10) unsigned NOT NULL COMMENT 'Domain.',
+  `entity_id` int(10) unsigned NOT NULL COMMENT 'Entity.',
+  `property_id` int(10) unsigned NOT NULL COMMENT 'Property.',
+  `value` bigint(20) NOT NULL,
+  `status` enum('OBLIGATORY','RECOMMENDED','SUGGESTED','FIX','REPLACEMENT') NOT NULL COMMENT 'Status of this statement.',
+  `pidx` int(10) unsigned NOT NULL DEFAULT 0,
+  `unit_sig` bigint(20) DEFAULT NULL,
+  KEY `domain_id` (`domain_id`,`entity_id`),
+  KEY `int_entity_id_entity` (`entity_id`),
+  KEY `int_property_id_entity` (`property_id`),
+  CONSTRAINT `int_domain_id_entity` FOREIGN KEY (`domain_id`) REFERENCES `entities` (`id`),
+  CONSTRAINT `int_entity_id_entity` FOREIGN KEY (`entity_id`) REFERENCES `entities` (`id`),
+  CONSTRAINT `int_property_id_entity` FOREIGN KEY (`property_id`) REFERENCES `entities` (`id`)
+) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
+/*!40101 SET character_set_client = @saved_cs_client */;
+
+--
+-- Dumping data for table `integer_data`
+--
+
+LOCK TABLES `integer_data` WRITE;
+/*!40000 ALTER TABLE `integer_data` DISABLE KEYS */;
+/*!40000 ALTER TABLE `integer_data` ENABLE KEYS */;
+UNLOCK TABLES;
+
+--
+-- Table structure for table `isa_cache`
+--
+
+DROP TABLE IF EXISTS `isa_cache`;
+/*!40101 SET @saved_cs_client     = @@character_set_client */;
+/*!40101 SET character_set_client = utf8 */;
+CREATE TABLE `isa_cache` (
+  `child` int(10) unsigned NOT NULL,
+  `parent` int(10) unsigned NOT NULL,
+  `rpath` varchar(255) NOT NULL,
+  PRIMARY KEY (`child`,`parent`,`rpath`),
+  KEY `isa_cache_parent_entity` (`parent`),
+  CONSTRAINT `isa_cache_child_entity` FOREIGN KEY (`child`) REFERENCES `entities` (`id`),
+  CONSTRAINT `isa_cache_parent_entity` FOREIGN KEY (`parent`) REFERENCES `entities` (`id`)
+) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
+/*!40101 SET character_set_client = @saved_cs_client */;
+
+--
+-- Dumping data for table `isa_cache`
+--
+
+LOCK TABLES `isa_cache` WRITE;
+/*!40000 ALTER TABLE `isa_cache` DISABLE KEYS */;
+/*!40000 ALTER TABLE `isa_cache` ENABLE KEYS */;
+UNLOCK TABLES;
+
+--
+-- Table structure for table `name_data`
+--
+
+DROP TABLE IF EXISTS `name_data`;
+/*!40101 SET @saved_cs_client     = @@character_set_client */;
+/*!40101 SET character_set_client = utf8 */;
+CREATE TABLE `name_data` (
+  `domain_id` int(10) unsigned NOT NULL,
+  `entity_id` int(10) unsigned NOT NULL,
+  `property_id` int(10) unsigned NOT NULL,
+  `value` varchar(255) NOT NULL,
+  `status` enum('OBLIGATORY','RECOMMENDED','SUGGESTED','FIX','REPLACEMENT') NOT NULL,
+  `pidx` int(10) unsigned NOT NULL DEFAULT 0,
+  UNIQUE KEY `domain_id_2` (`domain_id`,`entity_id`,`property_id`),
+  KEY `domain_id` (`domain_id`,`entity_id`),
+  KEY `entity_id` (`entity_id`),
+  KEY `property_id` (`property_id`),
+  KEY `value` (`value`),
+  CONSTRAINT `name_data_domain_id_entity` FOREIGN KEY (`domain_id`) REFERENCES `entities` (`id`),
+  CONSTRAINT `name_data_entity_id_entity` FOREIGN KEY (`entity_id`) REFERENCES `entities` (`id`),
+  CONSTRAINT `name_data_property_id_entity` FOREIGN KEY (`property_id`) REFERENCES `entities` (`id`)
+) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
+/*!40101 SET character_set_client = @saved_cs_client */;
+
+--
+-- Dumping data for table `name_data`
+--
+
+LOCK TABLES `name_data` WRITE;
+/*!40000 ALTER TABLE `name_data` DISABLE KEYS */;
+INSERT INTO `name_data` VALUES
+(0,0,20,'DOMAIN','FIX',0),
+(0,1,20,'RECORDTYPE','FIX',0),
+(0,2,20,'RECORD','FIX',0),
+(0,3,20,'FILE','FIX',0),
+(0,4,20,'PROPERTY','FIX',0),
+(0,7,20,'DATATYPE','FIX',0),
+(0,8,20,'QUERYTEMPLATE','FIX',0),
+(0,11,20,'REFERENCE','FIX',0),
+(0,12,20,'INTEGER','FIX',0),
+(0,13,20,'DOUBLE','FIX',0),
+(0,14,20,'TEXT','FIX',0),
+(0,15,20,'DATETIME','FIX',0),
+(0,16,20,'TIMESPAN','FIX',0),
+(0,17,20,'FILE','FIX',0),
+(0,18,20,'BOOLEAN','FIX',0),
+(0,20,20,'name','FIX',0),
+(0,21,20,'unit','FIX',0),
+(0,24,20,'description','FIX',0);
+/*!40000 ALTER TABLE `name_data` ENABLE KEYS */;
+UNLOCK TABLES;
+
+--
+-- Table structure for table `name_overrides`
+--
+
+DROP TABLE IF EXISTS `name_overrides`;
+/*!40101 SET @saved_cs_client     = @@character_set_client */;
+/*!40101 SET character_set_client = utf8 */;
+CREATE TABLE `name_overrides` (
+  `domain_id` int(10) unsigned DEFAULT NULL,
+  `entity_id` int(10) unsigned DEFAULT NULL,
+  `property_id` int(10) unsigned DEFAULT NULL,
+  `name` varchar(255) DEFAULT NULL,
+  UNIQUE KEY `name_ov_ukey` (`domain_id`,`entity_id`,`property_id`),
+  KEY `name_ov_dom_ent_idx` (`domain_id`,`entity_id`),
+  KEY `name_ov_forkey_ent` (`entity_id`),
+  KEY `name_ov_forkey_pro` (`property_id`),
+  CONSTRAINT `name_ov_forkey_dom` FOREIGN KEY (`domain_id`) REFERENCES `entities` (`id`),
+  CONSTRAINT `name_ov_forkey_ent` FOREIGN KEY (`entity_id`) REFERENCES `entities` (`id`),
+  CONSTRAINT `name_ov_forkey_pro` FOREIGN KEY (`property_id`) REFERENCES `entities` (`id`)
+) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
+/*!40101 SET character_set_client = @saved_cs_client */;
+
+--
+-- Dumping data for table `name_overrides`
+--
+
+LOCK TABLES `name_overrides` WRITE;
+/*!40000 ALTER TABLE `name_overrides` DISABLE KEYS */;
+/*!40000 ALTER TABLE `name_overrides` ENABLE KEYS */;
+UNLOCK TABLES;
+
+--
+-- Table structure for table `null_data`
+--
+
+DROP TABLE IF EXISTS `null_data`;
+/*!40101 SET @saved_cs_client     = @@character_set_client */;
+/*!40101 SET character_set_client = utf8 */;
+CREATE TABLE `null_data` (
+  `domain_id` int(10) unsigned DEFAULT NULL,
+  `entity_id` int(10) unsigned DEFAULT NULL,
+  `property_id` int(10) unsigned DEFAULT NULL,
+  `status` enum('OBLIGATORY','RECOMMENDED','SUGGESTED','FIX') DEFAULT NULL,
+  `pidx` int(10) unsigned NOT NULL DEFAULT 0,
+  KEY `null_data_dom_ent_idx` (`domain_id`,`entity_id`),
+  KEY `null_forkey_ent` (`entity_id`),
+  KEY `null_forkey_pro` (`property_id`),
+  CONSTRAINT `null_forkey_dom` FOREIGN KEY (`domain_id`) REFERENCES `entities` (`id`),
+  CONSTRAINT `null_forkey_ent` FOREIGN KEY (`entity_id`) REFERENCES `entities` (`id`),
+  CONSTRAINT `null_forkey_pro` FOREIGN KEY (`property_id`) REFERENCES `entities` (`id`)
+) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
+/*!40101 SET character_set_client = @saved_cs_client */;
+
+--
+-- Dumping data for table `null_data`
+--
+
+LOCK TABLES `null_data` WRITE;
+/*!40000 ALTER TABLE `null_data` DISABLE KEYS */;
+/*!40000 ALTER TABLE `null_data` ENABLE KEYS */;
+UNLOCK TABLES;
+
+--
+-- Table structure for table `passwd`
+--
+
+DROP TABLE IF EXISTS `passwd`;
+/*!40101 SET @saved_cs_client     = @@character_set_client */;
+/*!40101 SET character_set_client = utf8 */;
+CREATE TABLE `passwd` (
+  `principal` varbinary(255) NOT NULL,
+  `hash` varbinary(255) NOT NULL,
+  `alg` varchar(255) DEFAULT 'SHA-512',
+  `it` int(10) unsigned DEFAULT 5000,
+  `salt` varbinary(255) NOT NULL,
+  PRIMARY KEY (`principal`)
+) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
+/*!40101 SET character_set_client = @saved_cs_client */;
+
+--
+-- Dumping data for table `passwd`
+--
+
+LOCK TABLES `passwd` WRITE;
+/*!40000 ALTER TABLE `passwd` DISABLE KEYS */;
+/*!40000 ALTER TABLE `passwd` ENABLE KEYS */;
+UNLOCK TABLES;
+
+--
+-- Table structure for table `permissions`
+--
+
+DROP TABLE IF EXISTS `permissions`;
+/*!40101 SET @saved_cs_client     = @@character_set_client */;
+/*!40101 SET character_set_client = utf8 */;
+CREATE TABLE `permissions` (
+  `role` varbinary(255) NOT NULL,
+  `permissions` mediumtext NOT NULL,
+  PRIMARY KEY (`role`),
+  CONSTRAINT `perm_name_roles` FOREIGN KEY (`role`) REFERENCES `roles` (`name`)
+) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
+/*!40101 SET character_set_client = @saved_cs_client */;
+
+--
+-- Dumping data for table `permissions`
+--
+
+LOCK TABLES `permissions` WRITE;
+/*!40000 ALTER TABLE `permissions` DISABLE KEYS */;
+INSERT INTO `permissions` VALUES
+('administration','[{\"grant\":\"true\",\"priority\":\"true\",\"permission\":\"*\"}]');
+/*!40000 ALTER TABLE `permissions` ENABLE KEYS */;
+UNLOCK TABLES;
+
+--
+-- Table structure for table `query_template_def`
+--
+
+DROP TABLE IF EXISTS `query_template_def`;
+/*!40101 SET @saved_cs_client     = @@character_set_client */;
+/*!40101 SET character_set_client = utf8 */;
+CREATE TABLE `query_template_def` (
+  `id` int(10) unsigned NOT NULL,
+  `definition` mediumtext NOT NULL,
+  PRIMARY KEY (`id`),
+  CONSTRAINT `query_template_def_ibfk_1` FOREIGN KEY (`id`) REFERENCES `entities` (`id`)
+) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
+/*!40101 SET character_set_client = @saved_cs_client */;
+
+--
+-- Dumping data for table `query_template_def`
+--
+
+LOCK TABLES `query_template_def` WRITE;
+/*!40000 ALTER TABLE `query_template_def` DISABLE KEYS */;
+/*!40000 ALTER TABLE `query_template_def` ENABLE KEYS */;
+UNLOCK TABLES;
+
+--
+-- Table structure for table `reference_data`
+--
+
+DROP TABLE IF EXISTS `reference_data`;
+/*!40101 SET @saved_cs_client     = @@character_set_client */;
+/*!40101 SET character_set_client = utf8 */;
+CREATE TABLE `reference_data` (
+  `domain_id` int(10) unsigned NOT NULL COMMENT 'Domain.',
+  `entity_id` int(10) unsigned NOT NULL COMMENT 'Entity.',
+  `property_id` int(10) unsigned NOT NULL COMMENT 'Property.',
+  `value` int(10) unsigned NOT NULL,
+  `status` enum('OBLIGATORY','RECOMMENDED','SUGGESTED','FIX','REPLACEMENT') NOT NULL COMMENT 'Status of this statement.',
+  `pidx` int(10) unsigned NOT NULL DEFAULT 0,
+  `value_iversion` int(10) unsigned DEFAULT NULL,
+  KEY `entity_id` (`entity_id`,`property_id`),
+  KEY `ref_domain_id_entity` (`domain_id`),
+  KEY `ref_property_id_entity` (`property_id`),
+  KEY `ref_value_entity` (`value`),
+  KEY `value` (`value`,`value_iversion`),
+  CONSTRAINT `ref_domain_id_entity` FOREIGN KEY (`domain_id`) REFERENCES `entities` (`id`),
+  CONSTRAINT `ref_entity_id_entity` FOREIGN KEY (`entity_id`) REFERENCES `entities` (`id`),
+  CONSTRAINT `ref_property_id_entity` FOREIGN KEY (`property_id`) REFERENCES `entities` (`id`),
+  CONSTRAINT `ref_value_entity` FOREIGN KEY (`value`) REFERENCES `entities` (`id`),
+  CONSTRAINT `reference_data_ibfk_1` FOREIGN KEY (`value`, `value_iversion`) REFERENCES `entity_version` (`entity_id`, `_iversion`)
+) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
+/*!40101 SET character_set_client = @saved_cs_client */;
+
+--
+-- Dumping data for table `reference_data`
+--
+
+LOCK TABLES `reference_data` WRITE;
+/*!40000 ALTER TABLE `reference_data` DISABLE KEYS */;
+/*!40000 ALTER TABLE `reference_data` ENABLE KEYS */;
+UNLOCK TABLES;
+
+--
+-- Table structure for table `roles`
+--
+
+DROP TABLE IF EXISTS `roles`;
+/*!40101 SET @saved_cs_client     = @@character_set_client */;
+/*!40101 SET character_set_client = utf8 */;
+CREATE TABLE `roles` (
+  `name` varbinary(255) NOT NULL,
+  `description` mediumtext DEFAULT NULL,
+  PRIMARY KEY (`name`)
+) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
+/*!40101 SET character_set_client = @saved_cs_client */;
+
+--
+-- Dumping data for table `roles`
+--
+
+LOCK TABLES `roles` WRITE;
+/*!40000 ALTER TABLE `roles` DISABLE KEYS */;
+INSERT INTO `roles` VALUES
+('administration','Users with this role have unrestricted permissions.'),
+('anonymous','Users who did not authenticate themselves.');
+/*!40000 ALTER TABLE `roles` ENABLE KEYS */;
+UNLOCK TABLES;
+
+--
+-- Table structure for table `stats`
+--
+
+DROP TABLE IF EXISTS `stats`;
+/*!40101 SET @saved_cs_client     = @@character_set_client */;
+/*!40101 SET character_set_client = utf8 */;
+CREATE TABLE `stats` (
+  `name` varchar(255) NOT NULL,
+  `value` blob DEFAULT NULL,
+  PRIMARY KEY (`name`)
+) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
+/*!40101 SET character_set_client = @saved_cs_client */;
+
+--
+-- Dumping data for table `stats`
+--
+
+LOCK TABLES `stats` WRITE;
+/*!40000 ALTER TABLE `stats` DISABLE KEYS */;
+INSERT INTO `stats` VALUES
+('RootBenchmark','��\0sr\0-org.caosdb.server.database.misc.RootBenchmark����Qk]\0\0xr\04org.caosdb.server.database.misc.TransactionBenchmark����Qk]\0J\0sinceL\0measurementst\0Ljava/util/Map;[\0stackTraceElementst\0[Ljava/lang/StackTraceElement;L\0\rsubBenchmarksq\0~\0xp\0\0�L�	�sr\0java.util.HashMap���`�\0F\0\nloadFactorI\0	thresholdxp?@\0\0\0\0\0\0w\0\0\0\0\0\0\0xur\0[Ljava.lang.StackTraceElement;F*<<�\"9\0\0xp\0\0\0sr\0java.lang.StackTraceElementa	Ś&6݅\0B\0formatI\0\nlineNumberL\0classLoaderNamet\0Ljava/lang/String;L\0declaringClassq\0~\0\nL\0fileNameq\0~\0\nL\0\nmethodNameq\0~\0\nL\0\nmoduleNameq\0~\0\nL\0\rmoduleVersionq\0~\0\nxp\0\0Spt\0java.lang.Threadt\0Thread.javat\0\rgetStackTracet\0	java.baset\017.0.12sq\0~\0	\0\0!t\0appt\04org.caosdb.server.database.misc.TransactionBenchmarkt\0TransactionBenchmark.javat\0<init>ppsq\0~\0	\0\0\0�q\0~\0t\0-org.caosdb.server.database.misc.RootBenchmarkq\0~\0q\0~\0ppsq\0~\0	\0\0q\0~\0q\0~\0q\0~\0t\0<clinit>ppsq\0~\0	\0\0fq\0~\0t\0org.caosdb.server.CaosDBServert\0CaosDBServer.javat\0initBackendppsq\0~\0	\0\0\0�q\0~\0q\0~\0q\0~\0t\0mainppsq\0~\0?@\0\0\0\0\0w\0\0\0\0\0\0t\0Infosr\0,org.caosdb.server.database.misc.SubBenchmark����Qk]\0L\0nameq\0~\0\nxq\0~\0\0\0�L�	�sq\0~\0?@\0\0\0\0\0\0w\0\0\0\0\0\0\0xuq\0~\0\0\0\0\nsq\0~\0	\0\0Spq\0~\0q\0~\0\rq\0~\0q\0~\0q\0~\0sq\0~\0	\0\0!q\0~\0q\0~\0q\0~\0q\0~\0ppsq\0~\0	\0\0\0�q\0~\0t\0,org.caosdb.server.database.misc.SubBenchmarkq\0~\0q\0~\0ppsq\0~\0	\0\0�q\0~\0q\0~\0q\0~\0t\0getBenchmarkppsq\0~\0	\0\0�q\0~\0q\0~\0q\0~\0q\0~\0+ppsq\0~\0	\0\0\0$q\0~\0t\02org.caosdb.server.transaction.TransactionInterfacet\0TransactionInterface.javat\0getTransactionBenchmarkppsq\0~\0	\0\0\00q\0~\0q\0~\0.q\0~\0/t\0executeppsq\0~\0	\0\0\0�q\0~\0t\0org.caosdb.server.utils.Infot\0	Info.javat\0syncDatabaseppsq\0~\0	\0\0\0�q\0~\0t\0/org.caosdb.server.database.misc.RootBenchmark$1q\0~\0t\0runppsq\0~\0	\0\0Hpq\0~\0q\0~\0\rq\0~\09q\0~\0q\0~\0sq\0~\0?@\0\0\0\0\0w\0\0\0\0\0\0t\0	SyncStatssq\0~\0\"\0\0�L�	�sq\0~\0?@\0\0\0\0\0\0w\0\0\0\0\0\0\0xuq\0~\0\0\0\0	sq\0~\0	\0\0Spq\0~\0q\0~\0\rq\0~\0q\0~\0q\0~\0sq\0~\0	\0\0!q\0~\0q\0~\0q\0~\0q\0~\0ppsq\0~\0	\0\0\0�q\0~\0q\0~\0)q\0~\0q\0~\0ppsq\0~\0	\0\0�q\0~\0q\0~\0q\0~\0q\0~\0+ppsq\0~\0	\0\0�q\0~\0q\0~\0q\0~\0q\0~\0+ppsq\0~\0	\0\0\00q\0~\0q\0~\0.q\0~\0/q\0~\02ppsq\0~\0	\0\0\0�q\0~\0q\0~\04q\0~\05q\0~\06ppsq\0~\0	\0\0\0�q\0~\0q\0~\08q\0~\0q\0~\09ppsq\0~\0	\0\0Hpq\0~\0q\0~\0\rq\0~\09q\0~\0q\0~\0sq\0~\0?@\0\0\0\0\0w\0\0\0\0\0\0t\0MySQLSyncStatssq\0~\0\"\0\0�L�	�sq\0~\0?@\0\0\0\0\0\0w\0\0\0\0\0\0\0xuq\0~\0\0\0\0sq\0~\0	\0\0Spq\0~\0q\0~\0\rq\0~\0q\0~\0q\0~\0sq\0~\0	\0\0!q\0~\0q\0~\0q\0~\0q\0~\0ppsq\0~\0	\0\0\0�q\0~\0q\0~\0)q\0~\0q\0~\0ppsq\0~\0	\0\0�q\0~\0q\0~\0q\0~\0q\0~\0+ppsq\0~\0	\0\0�q\0~\0q\0~\0q\0~\0q\0~\0+ppsq\0~\0	\0\0q\0~\0t\0-org.caosdb.server.database.BackendTransactiont\0BackendTransaction.javat\0getImplementationppsq\0~\0	\0\0\0+q\0~\0t\08org.caosdb.server.database.backend.transaction.SyncStatst\0SyncStats.javaq\0~\02ppsq\0~\0	\0\0\0�q\0~\0q\0~\0Tq\0~\0Ut\0executeTransactionppsq\0~\0	\0\0\01q\0~\0q\0~\0.q\0~\0/q\0~\02ppsq\0~\0	\0\0\0�q\0~\0q\0~\04q\0~\05q\0~\06ppsq\0~\0	\0\0\0�q\0~\0q\0~\08q\0~\0q\0~\09ppsq\0~\0	\0\0Hpq\0~\0q\0~\0\rq\0~\09q\0~\0q\0~\0sq\0~\0?@\0\0\0\0\0\0w\0\0\0\0\0\0\0xq\0~\0Jxq\0~\0<xq\0~\0!x'),
+('TransactionBenchmark','��\0sr\00caosdb.server.database.misc.TransactionBenchmark�Cl=���E\0J\0sinceL\0acct\0Ljava/util/HashMap;L\0countsq\0~\0xp\0\0l���Wsr\0java.util.HashMap���`�\0F\0\nloadFactorI\0	thresholdxp?@\0\0\0\0\0w\0\0\0\0\0\0t\0	SyncStatssr\0java.lang.Long;��̏#�\0J\0valuexr\0java.lang.Number������\0\0xp\0\0\0\0\0\0\0t\0GetInfosq\0~\0\0\0\0\0\0\0 xsq\0~\0?@\0\0\0\0\0w\0\0\0\0\0\0q\0~\0sr\0java.lang.Integer⠤���8\0I\0valuexq\0~\0\0\0\0q\0~\0	sq\0~\0\0\0\0x');
+/*!40000 ALTER TABLE `stats` ENABLE KEYS */;
+UNLOCK TABLES;
+
+--
+-- Table structure for table `text_data`
+--
+
+DROP TABLE IF EXISTS `text_data`;
+/*!40101 SET @saved_cs_client     = @@character_set_client */;
+/*!40101 SET character_set_client = utf8 */;
+CREATE TABLE `text_data` (
+  `domain_id` int(10) unsigned NOT NULL COMMENT 'Domain.',
+  `entity_id` int(10) unsigned NOT NULL COMMENT 'Entity.',
+  `property_id` int(10) unsigned NOT NULL COMMENT 'Property.',
+  `value` text NOT NULL,
+  `status` enum('OBLIGATORY','RECOMMENDED','SUGGESTED','FIX','REPLACEMENT') NOT NULL COMMENT 'Status of this statement.',
+  `pidx` int(10) unsigned NOT NULL DEFAULT 0,
+  KEY `domain_id` (`domain_id`,`entity_id`),
+  KEY `str_entity_id_entity` (`entity_id`),
+  KEY `str_property_id_entity` (`property_id`),
+  CONSTRAINT `str_domain_id_entity` FOREIGN KEY (`domain_id`) REFERENCES `entities` (`id`),
+  CONSTRAINT `str_entity_id_entity` FOREIGN KEY (`entity_id`) REFERENCES `entities` (`id`),
+  CONSTRAINT `str_property_id_entity` FOREIGN KEY (`property_id`) REFERENCES `entities` (`id`)
+) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
+/*!40101 SET character_set_client = @saved_cs_client */;
+
+--
+-- Dumping data for table `text_data`
+--
+
+LOCK TABLES `text_data` WRITE;
+/*!40000 ALTER TABLE `text_data` DISABLE KEYS */;
+/*!40000 ALTER TABLE `text_data` ENABLE KEYS */;
+UNLOCK TABLES;
+
+--
+-- Table structure for table `transaction_log`
+--
+
+DROP TABLE IF EXISTS `transaction_log`;
+/*!40101 SET @saved_cs_client     = @@character_set_client */;
+/*!40101 SET character_set_client = utf8 */;
+CREATE TABLE `transaction_log` (
+  `transaction` varchar(255) NOT NULL COMMENT 'Transaction.',
+  `entity_id` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_bin NOT NULL,
+  `username` varbinary(255) NOT NULL,
+  `seconds` bigint(20) unsigned NOT NULL DEFAULT 0,
+  `nanos` int(10) unsigned NOT NULL DEFAULT 0,
+  `realm` varbinary(255) NOT NULL,
+  KEY `entity_id` (`entity_id`)
+) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
+/*!40101 SET character_set_client = @saved_cs_client */;
+
+--
+-- Dumping data for table `transaction_log`
+--
+
+LOCK TABLES `transaction_log` WRITE;
+/*!40000 ALTER TABLE `transaction_log` DISABLE KEYS */;
+/*!40000 ALTER TABLE `transaction_log` ENABLE KEYS */;
+UNLOCK TABLES;
+
+--
+-- Table structure for table `transactions`
+--
+
+DROP TABLE IF EXISTS `transactions`;
+/*!40101 SET @saved_cs_client     = @@character_set_client */;
+/*!40101 SET character_set_client = utf8 */;
+CREATE TABLE `transactions` (
+  `srid` varbinary(255) NOT NULL,
+  `username` varbinary(255) NOT NULL,
+  `realm` varbinary(255) NOT NULL,
+  `seconds` bigint(20) unsigned NOT NULL,
+  `nanos` int(10) unsigned NOT NULL,
+  PRIMARY KEY (`srid`)
+) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
+/*!40101 SET character_set_client = @saved_cs_client */;
+
+--
+-- Dumping data for table `transactions`
+--
+
+LOCK TABLES `transactions` WRITE;
+/*!40000 ALTER TABLE `transactions` DISABLE KEYS */;
+INSERT INTO `transactions` VALUES
+('cf83e1357eefb8bdf1542850d66d8007d620e4050b5715dc83f4a921d36ce9ce47d0d13c5d85f2b0ff8318d2877eec2f63b931bd47417a81a538327af927da3e','administration','CaosDB',0,0);
+/*!40000 ALTER TABLE `transactions` ENABLE KEYS */;
+UNLOCK TABLES;
+
+--
+-- Table structure for table `units_lin_con`
+--
+
+DROP TABLE IF EXISTS `units_lin_con`;
+/*!40101 SET @saved_cs_client     = @@character_set_client */;
+/*!40101 SET character_set_client = utf8 */;
+CREATE TABLE `units_lin_con` (
+  `signature_from` bigint(20) NOT NULL,
+  `signature_to` bigint(20) NOT NULL,
+  `a` decimal(65,30) NOT NULL,
+  `b_dividend` int(11) NOT NULL,
+  `b_divisor` int(11) NOT NULL,
+  `c` decimal(65,30) NOT NULL,
+  PRIMARY KEY (`signature_from`)
+) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
+/*!40101 SET character_set_client = @saved_cs_client */;
+
+--
+-- Dumping data for table `units_lin_con`
+--
+
+LOCK TABLES `units_lin_con` WRITE;
+/*!40000 ALTER TABLE `units_lin_con` DISABLE KEYS */;
+/*!40000 ALTER TABLE `units_lin_con` ENABLE KEYS */;
+UNLOCK TABLES;
+
+--
+-- Table structure for table `user_info`
+--
+
+DROP TABLE IF EXISTS `user_info`;
+/*!40101 SET @saved_cs_client     = @@character_set_client */;
+/*!40101 SET character_set_client = utf8 */;
+CREATE TABLE `user_info` (
+  `realm` varbinary(255) NOT NULL,
+  `name` varbinary(255) NOT NULL,
+  `email` varbinary(255) DEFAULT NULL,
+  `status` enum('ACTIVE','INACTIVE') NOT NULL DEFAULT 'INACTIVE',
+  `entity` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_bin DEFAULT NULL,
+  PRIMARY KEY (`realm`,`name`),
+  KEY `subject_entity` (`entity`),
+  CONSTRAINT `subjects_ibfk_2` FOREIGN KEY (`entity`) REFERENCES `entity_ids` (`id`)
+) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
+/*!40101 SET character_set_client = @saved_cs_client */;
+
+--
+-- Dumping data for table `user_info`
+--
+
+LOCK TABLES `user_info` WRITE;
+/*!40000 ALTER TABLE `user_info` DISABLE KEYS */;
+/*!40000 ALTER TABLE `user_info` ENABLE KEYS */;
+UNLOCK TABLES;
+
+--
+-- Table structure for table `user_roles`
+--
+
+DROP TABLE IF EXISTS `user_roles`;
+/*!40101 SET @saved_cs_client     = @@character_set_client */;
+/*!40101 SET character_set_client = utf8 */;
+CREATE TABLE `user_roles` (
+  `realm` varbinary(255) NOT NULL,
+  `user` varbinary(255) NOT NULL,
+  `role` varbinary(255) NOT NULL,
+  PRIMARY KEY (`realm`,`user`,`role`),
+  KEY `user_roles_ibfk_1` (`role`),
+  CONSTRAINT `user_roles_ibfk_1` FOREIGN KEY (`role`) REFERENCES `roles` (`name`)
+) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
+/*!40101 SET character_set_client = @saved_cs_client */;
+
+--
+-- Dumping data for table `user_roles`
+--
+
+LOCK TABLES `user_roles` WRITE;
+/*!40000 ALTER TABLE `user_roles` DISABLE KEYS */;
+/*!40000 ALTER TABLE `user_roles` ENABLE KEYS */;
+UNLOCK TABLES;
+
+--
+-- Dumping routines for database 'caosdb'
+--
+/*!50003 SET @saved_sql_mode       = @@sql_mode */ ;
+/*!50003 SET sql_mode              = 'STRICT_TRANS_TABLES,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION' */ ;
+/*!50003 DROP FUNCTION IF EXISTS `CaosDBVersion` */;
+/*!50003 SET @saved_cs_client      = @@character_set_client */ ;
+/*!50003 SET @saved_cs_results     = @@character_set_results */ ;
+/*!50003 SET @saved_col_connection = @@collation_connection */ ;
+/*!50003 SET character_set_client  = utf8 */ ;
+/*!50003 SET character_set_results = utf8 */ ;
+/*!50003 SET collation_connection  = utf8_general_ci */ ;
+DELIMITER ;;
+CREATE DEFINER=`root`@`%` FUNCTION `CaosDBVersion`() RETURNS varchar(255) CHARSET utf8 COLLATE utf8_unicode_ci
+    DETERMINISTIC
+RETURN 'v7.0.2' ;;
+DELIMITER ;
+/*!50003 SET sql_mode              = @saved_sql_mode */ ;
+/*!50003 SET character_set_client  = @saved_cs_client */ ;
+/*!50003 SET character_set_results = @saved_cs_results */ ;
+/*!50003 SET collation_connection  = @saved_col_connection */ ;
+/*!50003 SET @saved_sql_mode       = @@sql_mode */ ;
+/*!50003 SET sql_mode              = 'STRICT_TRANS_TABLES,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION' */ ;
+/*!50003 DROP FUNCTION IF EXISTS `constructDateTimeWhereClauseForColumn` */;
+/*!50003 SET @saved_cs_client      = @@character_set_client */ ;
+/*!50003 SET @saved_cs_results     = @@character_set_results */ ;
+/*!50003 SET @saved_col_connection = @@collation_connection */ ;
+/*!50003 SET character_set_client  = utf8 */ ;
+/*!50003 SET character_set_results = utf8 */ ;
+/*!50003 SET collation_connection  = utf8_general_ci */ ;
+DELIMITER ;;
+CREATE DEFINER=`root`@`%` FUNCTION `constructDateTimeWhereClauseForColumn`(seconds_col VARCHAR(255), nanos_col VARCHAR(255), vDateTimeSecLow VARCHAR(255), vDateTimeNSLow VARCHAR(255), vDateTimeSecUpp VARCHAR(255), vDateTimeNSUpp VARCHAR(255), operator CHAR(4)) RETURNS varchar(20000) CHARSET utf8 COLLATE utf8_unicode_ci
+    DETERMINISTIC
+BEGIN
+
+    DECLARE isInterval BOOLEAN DEFAULT vDateTimeSecUpp IS NOT NULL or vDateTimeNSUpp IS NOT NULL;
+    DECLARE operator_prefix CHAR(1) DEFAULT LEFT(operator,1);
+
+    IF isInterval THEN
+        IF operator = '=' THEN
+            RETURN " 0=1";
+        ELSEIF operator = '!=' THEN
+            RETURN " 0=1";
+        ELSEIF operator = '>' or operator = '<=' THEN
+            RETURN CONCAT(" ", seconds_col, operator_prefix, vDateTimeSecUpp);
+        ELSEIF operator = '<' or operator = '>=' THEN
+            RETURN CONCAT(" ", seconds_col, operator_prefix, vDateTimeSecLow);
+        ELSEIF operator = "(" THEN
+            RETURN CONCAT(" ", seconds_col, ">=", vDateTimeSecLow, " AND ",seconds_col, "<", vDateTimeSecUpp);
+        ELSEIF operator = "!(" THEN
+            RETURN CONCAT(" ", seconds_col, "<", vDateTimeSecLow, " OR ", seconds_col, ">=", vDateTimeSecUpp);
+        END IF;
+    ELSE
+        IF operator = '=' THEN
+            RETURN CONCAT(" ",
+                seconds_col,
+                "=", vDateTimeSecLow, IF(vDateTimeNSLow IS NULL, CONCAT(' AND ', nanos_col, ' IS NULL'), CONCAT(' AND ',
+                    nanos_col,
+                '=', vDateTimeNSLow)));
+        ELSEIF operator = '!=' THEN
+            RETURN CONCAT(" ",
+                seconds_col,
+                "!=", vDateTimeSecLow, IF(vDateTimeNSLow IS NULL, '', CONCAT(' OR ',
+                        nanos_col,
+                        '!=', vDateTimeNSLow)));
+        ELSEIF operator = '>' or operator = '<' THEN
+            RETURN CONCAT(" ",
+                seconds_col, operator, vDateTimeSecLow, IF(vDateTimeNSLow IS NULL, '', CONCAT(' OR (',seconds_col,'=', vDateTimeSecLow, ' AND ',nanos_col, operator, vDateTimeNSLow, ')')));
+        ELSEIF operator = '>=' or operator = '<=' THEN
+            RETURN CONCAT(
+                " ", seconds_col, operator, vDateTimeSecLow,
+                IF(vDateTimeNSLow IS NULL,
+                    '',
+                    CONCAT(
+                        ' AND (', seconds_col, operator_prefix, vDateTimeSecLow,
+                        ' OR ', nanos_col, operator, vDateTimeNSLow,
+                        ' OR ', nanos_col, ' IS NULL)')));
+        ELSEIF operator = "(" THEN
+            RETURN IF(vDateTimeNSLow IS NULL,CONCAT(" ",seconds_col,"=", vDateTimeSecLow),CONCAT(" ",seconds_col,"=",vDateTimeSecLow," AND ",nanos_col,"=",vDateTimeNSLow));
+        ELSEIF operator = "!(" THEN
+            RETURN IF(vDateTimeNSLow IS NULL,CONCAT(" ",seconds_col,"!=",vDateTimeSecLow, ""),CONCAT(" ",seconds_col,"!=",vDateTimeSecLow," OR ",nanos_col, " IS NULL OR ", nanos_col, "!=",vDateTimeNSLow));
+        END IF;
+    END IF;
+    return ' 0=1';
+END ;;
+DELIMITER ;
+/*!50003 SET sql_mode              = @saved_sql_mode */ ;
+/*!50003 SET character_set_client  = @saved_cs_client */ ;
+/*!50003 SET character_set_results = @saved_cs_results */ ;
+/*!50003 SET collation_connection  = @saved_col_connection */ ;
+/*!50003 SET @saved_sql_mode       = @@sql_mode */ ;
+/*!50003 SET sql_mode              = 'STRICT_TRANS_TABLES,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION' */ ;
+/*!50003 DROP FUNCTION IF EXISTS `convert_unit` */;
+/*!50003 SET @saved_cs_client      = @@character_set_client */ ;
+/*!50003 SET @saved_cs_results     = @@character_set_results */ ;
+/*!50003 SET @saved_col_connection = @@collation_connection */ ;
+/*!50003 SET character_set_client  = utf8 */ ;
+/*!50003 SET character_set_results = utf8 */ ;
+/*!50003 SET collation_connection  = utf8_general_ci */ ;
+DELIMITER ;;
+CREATE DEFINER=`root`@`%` FUNCTION `convert_unit`(unit_sig BIGINT, value DECIMAL(65,30)) RETURNS decimal(65,30)
+    DETERMINISTIC
+BEGIN
+    DECLARE ret DECIMAL(65,30) DEFAULT value;
+
+    SELECT (((value+a)*b_dividend)/b_divisor+c) INTO ret FROM units_lin_con WHERE signature_from=unit_sig;
+    RETURN ret;
+END ;;
+DELIMITER ;
+/*!50003 SET sql_mode              = @saved_sql_mode */ ;
+/*!50003 SET character_set_client  = @saved_cs_client */ ;
+/*!50003 SET character_set_results = @saved_cs_results */ ;
+/*!50003 SET collation_connection  = @saved_col_connection */ ;
+/*!50003 SET @saved_sql_mode       = @@sql_mode */ ;
+/*!50003 SET sql_mode              = 'STRICT_TRANS_TABLES,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION' */ ;
+/*!50003 DROP FUNCTION IF EXISTS `getAggValueWhereClause` */;
+/*!50003 SET @saved_cs_client      = @@character_set_client */ ;
+/*!50003 SET @saved_cs_results     = @@character_set_results */ ;
+/*!50003 SET @saved_col_connection = @@collation_connection */ ;
+/*!50003 SET character_set_client  = utf8 */ ;
+/*!50003 SET character_set_results = utf8 */ ;
+/*!50003 SET collation_connection  = utf8_general_ci */ ;
+DELIMITER ;;
+CREATE DEFINER=`root`@`%` FUNCTION `getAggValueWhereClause`(entities VARCHAR(255), properties VARCHAR(255)) RETURNS varchar(20000) CHARSET utf8 COLLATE utf8_unicode_ci
+    DETERMINISTIC
+BEGIN
+        RETURN CONCAT(" EXISTS (SELECT 1 FROM `", entities, "` AS ent WHERE ent.id = subdata.entity_id LIMIT 1)", IF(properties IS NOT NULL AND properties != '', CONCAT(" AND EXISTS (SELECT 1 FROM `", properties, "` as props WHERE props.id = subdata.property_id LIMIT 1)"),''));
+END ;;
+DELIMITER ;
+/*!50003 SET sql_mode              = @saved_sql_mode */ ;
+/*!50003 SET character_set_client  = @saved_cs_client */ ;
+/*!50003 SET character_set_results = @saved_cs_results */ ;
+/*!50003 SET collation_connection  = @saved_col_connection */ ;
+/*!50003 SET @saved_sql_mode       = @@sql_mode */ ;
+/*!50003 SET sql_mode              = 'STRICT_TRANS_TABLES,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION' */ ;
+/*!50003 DROP FUNCTION IF EXISTS `getDateTimeWhereClause` */;
+/*!50003 SET @saved_cs_client      = @@character_set_client */ ;
+/*!50003 SET @saved_cs_results     = @@character_set_results */ ;
+/*!50003 SET @saved_col_connection = @@collation_connection */ ;
+/*!50003 SET character_set_client  = utf8 */ ;
+/*!50003 SET character_set_results = utf8 */ ;
+/*!50003 SET collation_connection  = utf8_general_ci */ ;
+DELIMITER ;;
+CREATE DEFINER=`root`@`%` FUNCTION `getDateTimeWhereClause`(vDateTime VARCHAR(255), operator CHAR(4)) RETURNS varchar(20000) CHARSET utf8 COLLATE utf8_unicode_ci
+    DETERMINISTIC
+BEGIN
+    DECLARE sep_loc INTEGER DEFAULT LOCATE('--',vDateTime);
+    DECLARE vDateTimeLow VARCHAR(255) DEFAULT IF(sep_loc != 0, SUBSTRING_INDEX(vDateTime, '--',1), vDateTime);
+    DECLARE vDateTimeUpp VARCHAR(255) DEFAULT IF(sep_loc != 0, SUBSTRING_INDEX(vDateTime, '--',-1), NULL);
+
+    DECLARE vDateTimeSecLow VARCHAR(255) DEFAULT SUBSTRING_INDEX(vDateTimeLow, 'UTC', 1);
+    DECLARE vDateTimeNSLow VARCHAR(255) DEFAULT IF(SUBSTRING_INDEX(vDateTimeLow, 'UTC', -1)='',NULL,SUBSTRING_INDEX(vDateTimeLow, 'UTC', -1));
+
+    DECLARE vDateTimeSecUpp VARCHAR(255) DEFAULT IF(sep_loc != 0, SUBSTRING_INDEX(vDateTimeUpp, 'UTC', 1), NULL);
+    DECLARE vDateTimeNSUpp VARCHAR(255) DEFAULT IF(sep_loc != 0 AND SUBSTRING_INDEX(vDateTimeUpp, 'UTC', -1)!='',SUBSTRING_INDEX(vDateTimeUpp, 'UTC', -1),NULL);
+
+
+    RETURN constructDateTimeWhereClauseForColumn("subdata.value", "subdata.value_ns", vDateTimeSecLow, vDateTimeNSLow, vDateTimeSecUpp, vDateTimeNSUpp, operator);
+END ;;
+DELIMITER ;
+/*!50003 SET sql_mode              = @saved_sql_mode */ ;
+/*!50003 SET character_set_client  = @saved_cs_client */ ;
+/*!50003 SET character_set_results = @saved_cs_results */ ;
+/*!50003 SET collation_connection  = @saved_col_connection */ ;
+/*!50003 SET @saved_sql_mode       = @@sql_mode */ ;
+/*!50003 SET sql_mode              = 'STRICT_TRANS_TABLES,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION' */ ;
+/*!50003 DROP FUNCTION IF EXISTS `getDateWhereClause` */;
+/*!50003 SET @saved_cs_client      = @@character_set_client */ ;
+/*!50003 SET @saved_cs_results     = @@character_set_results */ ;
+/*!50003 SET @saved_col_connection = @@collation_connection */ ;
+/*!50003 SET character_set_client  = utf8 */ ;
+/*!50003 SET character_set_results = utf8 */ ;
+/*!50003 SET collation_connection  = utf8_general_ci */ ;
+DELIMITER ;;
+CREATE DEFINER=`root`@`%` FUNCTION `getDateWhereClause`(vDateTimeDotNotation VARCHAR(255), operator CHAR(4)) RETURNS varchar(20000) CHARSET utf8 COLLATE utf8_unicode_ci
+    DETERMINISTIC
+BEGIN
+    DECLARE isInterval INTEGER DEFAULT LOCATE('--',vDateTimeDotNotation);
+    
+    DECLARE vILB VARCHAR(255) DEFAULT IF(isInterval != 0, SUBSTRING_INDEX(vDateTimeDotNotation, '--', 1), vDateTimeDotNotation);
+    
+    DECLARE vEUB VARCHAR(255) DEFAULT IF(isInterval != 0, SUBSTRING_INDEX(vDateTimeDotNotation, '--', -1), NULL);
+    DECLARE vILB_Date INTEGER DEFAULT SUBSTRING_INDEX(vILB, '.', 1);
+    DECLARE vEUB_Date INTEGER DEFAULT SUBSTRING_INDEX(vEUB, '.', 1);
+    
+    DECLARE hasTime INTEGER DEFAULT LOCATE('.NULL.NULL',vILB);
+    
+    DECLARE dom INTEGER DEFAULT vILB_Date % 100;
+    
+    DECLARE mon INTEGER DEFAULT ((vILB_Date % 10000) - dom) / 100;
+    
+    DECLARE yea INTEGER DEFAULT (vILB_Date - (vILB_Date % 10000)) / 10000;
+
+    IF operator = '=' and hasTime != 0 THEN
+        RETURN CONCAT(" subdata.value=", vILB_Date);
+    ELSEIF operator = "!=" and hasTime != 0 THEN
+        IF mon != 0  and dom != 0 THEN
+            RETURN CONCAT(" subdata.value!=", vILB_Date, " and subdata.value%100!=0");
+        ELSEIF mon != 0 THEN
+            RETURN CONCAT(" subdata.value!=", vILB_Date, " and subdata.value%100=0 and subdata.value%10000!=0");
+        ELSE
+            RETURN CONCAT(" subdata.value!=", vILB_Date, " and subdata.value%10000=0");
+        END IF;
+    ELSEIF operator = "(" and hasTime != 0 THEN
+        IF mon != 0 and dom != 0 THEN
+            RETURN CONCAT(" subdata.value=", vILB_Date);
+        ELSEIF mon != 0 THEN
+            RETURN CONCAT(" subdata.value=",vILB_Date," OR (subdata.value>", vILB_Date, " and subdata.value<", vEUB_Date, " and subdata.value%10000!=0)");
+        ELSE
+            RETURN CONCAT(" subdata.value=",vILB_Date," OR (subdata.value>", vILB_Date, " and subdata.value<", vEUB_Date,")");
+        END IF;
+    ELSEIF operator = "!(" THEN
+        IF hasTime = 0 THEN
+            RETURN " 0=0";
+        END IF;
+        IF mon != 0 and dom != 0 THEN
+            RETURN CONCAT(" subdata.value!=",vILB_Date);
+        ELSEIF mon != 0 THEN
+            RETURN CONCAT(" (subdata.value!=",vILB_Date, " AND subdata.value%100=0) OR ((subdata.value<", vILB_Date, " or subdata.value>", vEUB_Date, ") and subdata.value%100!=0)");
+        ELSE
+            RETURN CONCAT(" (subdata.value!=",vILB_Date, " AND subdata.value%10000=0) OR ((subdata.value<", vILB_Date, " or subdata.value>=", vEUB_Date, ") and subdata.value%10000!=0)");
+        END IF;
+    ELSEIF operator = "<" THEN
+        IF mon != 0 and dom != 0 THEN
+            RETURN CONCAT(" subdata.value<", vILB_Date, " and (subdata.value%100!=0 or (subdata.value<", yea*10000+mon*100, " and subdata.value%10000!=0) or (subdata.value<", yea*10000, " and subdata.value%10000=0))");
+        ELSEIF mon != 0 THEN
+            RETURN CONCAT(" subdata.value<", vILB_Date, " and (subdata.value%10000!=0 or (subdata.value<", yea*10000, "))");
+        ELSE
+            RETURN CONCAT(" subdata.value<", vILB_Date);
+        END IF;
+    ELSEIF operator = ">" THEN
+        IF mon != 0 and dom != 0 THEN
+            RETURN CONCAT(" subdata.value>", vILB_Date);
+        ELSEIF mon != 0 THEN
+            RETURN CONCAT(" subdata.value>=",vEUB_Date);
+        ELSE
+            RETURN CONCAT(" subdata.value>=",vEUB_Date);
+        END IF;
+    ELSEIF operator = "<=" THEN
+        IF mon != 0 and dom != 0 THEN
+            
+            RETURN CONCAT(" subdata.value<=", vILB_Date,
+                          " or (subdata.value<=", yea*10000 + mon*100, " and subdata.value%100=0)");
+        ELSEIF mon != 0 THEN
+            
+            RETURN CONCAT(" subdata.value<", vEUB_Date);
+        ELSE
+            
+            RETURN CONCAT(" subdata.value<", vEUB_Date);
+        END IF;
+    ELSEIF operator = ">=" THEN
+        IF mon != 0 and dom != 0 THEN
+            
+            RETURN CONCAT(" subdata.value>=", vILB_Date,
+                          " or (subdata.value>=", yea*10000 + mon*100, " and subdata.value%100=0)",
+                          " or (subdata.value>=", yea*10000, " and subdata.value%10000=0)");
+        ELSEIF mon != 0 THEN
+            
+            RETURN CONCAT(" subdata.value>=", yea*10000 + mon*100,
+                          " or (subdata.value>=", yea*10000, " and subdata.value%10000=0)");
+        ELSE
+            
+            RETURN CONCAT(" subdata.value>=", yea*10000);
+        END IF;
+    END IF;
+
+    return ' 0=1';
+END ;;
+DELIMITER ;
+/*!50003 SET sql_mode              = @saved_sql_mode */ ;
+/*!50003 SET character_set_client  = @saved_cs_client */ ;
+/*!50003 SET character_set_results = @saved_cs_results */ ;
+/*!50003 SET collation_connection  = @saved_col_connection */ ;
+/*!50003 SET @saved_sql_mode       = @@sql_mode */ ;
+/*!50003 SET sql_mode              = 'STRICT_TRANS_TABLES,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION' */ ;
+/*!50003 DROP FUNCTION IF EXISTS `getDoubleWhereClause` */;
+/*!50003 SET @saved_cs_client      = @@character_set_client */ ;
+/*!50003 SET @saved_cs_results     = @@character_set_results */ ;
+/*!50003 SET @saved_col_connection = @@collation_connection */ ;
+/*!50003 SET character_set_client  = utf8 */ ;
+/*!50003 SET character_set_results = utf8 */ ;
+/*!50003 SET collation_connection  = utf8_general_ci */ ;
+DELIMITER ;;
+CREATE DEFINER=`root`@`%` FUNCTION `getDoubleWhereClause`(value DOUBLE, unit_sig BIGINT, valueStdUnit DECIMAL(65,30), stdUnit_sig BIGINT, o CHAR(4)) RETURNS varchar(20000) CHARSET utf8 COLLATE utf8_unicode_ci
+    DETERMINISTIC
+BEGIN
+    RETURN IF(unit_sig IS NULL AND value IS NOT NULL, 
+        CONCAT('subdata.value ', o, ' \'', value, '\''), 
+        CONCAT(
+            IF(value IS NULL, '', 
+                CONCAT('(subdata.unit_sig=', unit_sig, ' AND subdata.value ', o, ' \'', value, '\') OR ')), 
+        	IF(unit_sig = stdUnit_sig,'',CONCAT('(subdata.unit_sig=', stdUnit_sig,' AND subdata.value ', o, ' \'', valueStdUnit, '\') OR ')),'(standard_unit(subdata.unit_sig)=', stdUnit_sig,' AND convert_unit(subdata.unit_sig,subdata.value) ', o, ' ', valueStdUnit, ')')); 
+END ;;
+DELIMITER ;
+/*!50003 SET sql_mode              = @saved_sql_mode */ ;
+/*!50003 SET character_set_client  = @saved_cs_client */ ;
+/*!50003 SET character_set_results = @saved_cs_results */ ;
+/*!50003 SET collation_connection  = @saved_col_connection */ ;
+/*!50003 SET @saved_sql_mode       = @@sql_mode */ ;
+/*!50003 SET sql_mode              = 'STRICT_TRANS_TABLES,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION' */ ;
+/*!50003 DROP FUNCTION IF EXISTS `get_head_relative` */;
+/*!50003 SET @saved_cs_client      = @@character_set_client */ ;
+/*!50003 SET @saved_cs_results     = @@character_set_results */ ;
+/*!50003 SET @saved_col_connection = @@collation_connection */ ;
+/*!50003 SET character_set_client  = utf8 */ ;
+/*!50003 SET character_set_results = utf8 */ ;
+/*!50003 SET collation_connection  = utf8_general_ci */ ;
+DELIMITER ;;
+CREATE DEFINER=`root`@`%` FUNCTION `get_head_relative`(EntityID VARCHAR(255),
+    HeadOffset INT UNSIGNED) RETURNS varbinary(255)
+    READS SQL DATA
+BEGIN
+    DECLARE InternalEntityID INT UNSIGNED DEFAULT NULL;
+
+    SELECT internal_id INTO InternalEntityID FROM entity_ids WHERE id = EntityID;
+
+    
+    
+    
+    
+    RETURN (
+        SELECT e.version
+            FROM entity_version AS e
+            WHERE e.entity_id = InternalEntityID
+            ORDER BY e._iversion DESC
+            LIMIT 1 OFFSET HeadOffset
+        );
+END ;;
+DELIMITER ;
+/*!50003 SET sql_mode              = @saved_sql_mode */ ;
+/*!50003 SET character_set_client  = @saved_cs_client */ ;
+/*!50003 SET character_set_results = @saved_cs_results */ ;
+/*!50003 SET collation_connection  = @saved_col_connection */ ;
+/*!50003 SET @saved_sql_mode       = @@sql_mode */ ;
+/*!50003 SET sql_mode              = 'STRICT_TRANS_TABLES,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION' */ ;
+/*!50003 DROP FUNCTION IF EXISTS `get_head_version` */;
+/*!50003 SET @saved_cs_client      = @@character_set_client */ ;
+/*!50003 SET @saved_cs_results     = @@character_set_results */ ;
+/*!50003 SET @saved_col_connection = @@collation_connection */ ;
+/*!50003 SET character_set_client  = utf8 */ ;
+/*!50003 SET character_set_results = utf8 */ ;
+/*!50003 SET collation_connection  = utf8_general_ci */ ;
+DELIMITER ;;
+CREATE DEFINER=`root`@`%` FUNCTION `get_head_version`(EntityID VARCHAR(255)) RETURNS varbinary(255)
+    READS SQL DATA
+BEGIN
+    RETURN get_head_relative(EntityID, 0);
+END ;;
+DELIMITER ;
+/*!50003 SET sql_mode              = @saved_sql_mode */ ;
+/*!50003 SET character_set_client  = @saved_cs_client */ ;
+/*!50003 SET character_set_results = @saved_cs_results */ ;
+/*!50003 SET collation_connection  = @saved_col_connection */ ;
+/*!50003 SET @saved_sql_mode       = @@sql_mode */ ;
+/*!50003 SET sql_mode              = 'STRICT_TRANS_TABLES,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION' */ ;
+/*!50003 DROP FUNCTION IF EXISTS `get_iversion` */;
+/*!50003 SET @saved_cs_client      = @@character_set_client */ ;
+/*!50003 SET @saved_cs_results     = @@character_set_results */ ;
+/*!50003 SET @saved_col_connection = @@collation_connection */ ;
+/*!50003 SET character_set_client  = utf8 */ ;
+/*!50003 SET character_set_results = utf8 */ ;
+/*!50003 SET collation_connection  = utf8_general_ci */ ;
+DELIMITER ;;
+CREATE DEFINER=`root`@`%` FUNCTION `get_iversion`(InternalEntityID INT UNSIGNED,
+    Version VARBINARY(255)) RETURNS int(10) unsigned
+    READS SQL DATA
+BEGIN
+    RETURN (
+        SELECT e._iversion
+            FROM entity_version AS e
+            WHERE e.entity_id = InternalEntityID
+                AND e.version = Version
+        );
+END ;;
+DELIMITER ;
+/*!50003 SET sql_mode              = @saved_sql_mode */ ;
+/*!50003 SET character_set_client  = @saved_cs_client */ ;
+/*!50003 SET character_set_results = @saved_cs_results */ ;
+/*!50003 SET collation_connection  = @saved_col_connection */ ;
+/*!50003 SET @saved_sql_mode       = @@sql_mode */ ;
+/*!50003 SET sql_mode              = 'STRICT_TRANS_TABLES,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION' */ ;
+/*!50003 DROP FUNCTION IF EXISTS `get_primary_parent_version` */;
+/*!50003 SET @saved_cs_client      = @@character_set_client */ ;
+/*!50003 SET @saved_cs_results     = @@character_set_results */ ;
+/*!50003 SET @saved_col_connection = @@collation_connection */ ;
+/*!50003 SET character_set_client  = utf8 */ ;
+/*!50003 SET character_set_results = utf8 */ ;
+/*!50003 SET collation_connection  = utf8_general_ci */ ;
+DELIMITER ;;
+CREATE DEFINER=`root`@`%` FUNCTION `get_primary_parent_version`(EntityID VARCHAR(255),
+    Version VARBINARY(255)) RETURNS varbinary(255)
+    READS SQL DATA
+BEGIN
+    DECLARE InternalEntityID INT UNSIGNED DEFAULT NULL;
+
+    SELECT internal_id INTO InternalEntityID FROM entity_ids WHERE id = EntityID;
+
+    RETURN (
+        SELECT p.version
+            FROM entity_version AS e INNER JOIN entity_version AS p
+                ON (e._ipparent = p._iversion
+                    AND e.entity_id = p.entity_id)
+            WHERE e.entity_id = InternalEntityID
+                AND e.version = Version
+        );
+END ;;
+DELIMITER ;
+/*!50003 SET sql_mode              = @saved_sql_mode */ ;
+/*!50003 SET character_set_client  = @saved_cs_client */ ;
+/*!50003 SET character_set_results = @saved_cs_results */ ;
+/*!50003 SET collation_connection  = @saved_col_connection */ ;
+/*!50003 SET @saved_sql_mode       = @@sql_mode */ ;
+/*!50003 SET sql_mode              = 'STRICT_TRANS_TABLES,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION' */ ;
+/*!50003 DROP FUNCTION IF EXISTS `get_version_timestamp` */;
+/*!50003 SET @saved_cs_client      = @@character_set_client */ ;
+/*!50003 SET @saved_cs_results     = @@character_set_results */ ;
+/*!50003 SET @saved_col_connection = @@collation_connection */ ;
+/*!50003 SET character_set_client  = utf8 */ ;
+/*!50003 SET character_set_results = utf8 */ ;
+/*!50003 SET collation_connection  = utf8_general_ci */ ;
+DELIMITER ;;
+CREATE DEFINER=`root`@`%` FUNCTION `get_version_timestamp`(EntityID VARCHAR(255),
+    Version VARBINARY(255)) RETURNS varchar(255) CHARSET utf8 COLLATE utf8_unicode_ci
+    READS SQL DATA
+BEGIN
+    DECLARE InternalEntityID INT UNSIGNED DEFAULT NULL;
+
+    SELECT internal_id INTO InternalEntityID FROM entity_ids WHERE id = EntityID;
+
+    RETURN (
+        SELECT concat(t.seconds, '.', t.nanos)
+            FROM entity_version AS e INNER JOIN transactions AS t
+                ON ( e.srid = t.srid )
+            WHERE e.entity_id = InternalEntityID
+            AND e.version = Version
+    );
+END ;;
+DELIMITER ;
+/*!50003 SET sql_mode              = @saved_sql_mode */ ;
+/*!50003 SET character_set_client  = @saved_cs_client */ ;
+/*!50003 SET character_set_results = @saved_cs_results */ ;
+/*!50003 SET collation_connection  = @saved_col_connection */ ;
+/*!50003 SET @saved_sql_mode       = @@sql_mode */ ;
+/*!50003 SET sql_mode              = 'STRICT_TRANS_TABLES,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION' */ ;
+/*!50003 DROP FUNCTION IF EXISTS `is_feature_config` */;
+/*!50003 SET @saved_cs_client      = @@character_set_client */ ;
+/*!50003 SET @saved_cs_results     = @@character_set_results */ ;
+/*!50003 SET @saved_col_connection = @@collation_connection */ ;
+/*!50003 SET character_set_client  = utf8 */ ;
+/*!50003 SET character_set_results = utf8 */ ;
+/*!50003 SET collation_connection  = utf8_general_ci */ ;
+DELIMITER ;;
+CREATE DEFINER=`root`@`%` FUNCTION `is_feature_config`(_Key VARCHAR(255),
+    Expected VARCHAR(255)) RETURNS tinyint(1)
+    READS SQL DATA
+BEGIN
+    RETURN (
+        SELECT f._value = Expected FROM feature_config as f WHERE f._key = _Key
+    );
+END ;;
+DELIMITER ;
+/*!50003 SET sql_mode              = @saved_sql_mode */ ;
+/*!50003 SET character_set_client  = @saved_cs_client */ ;
+/*!50003 SET character_set_results = @saved_cs_results */ ;
+/*!50003 SET collation_connection  = @saved_col_connection */ ;
+/*!50003 SET @saved_sql_mode       = @@sql_mode */ ;
+/*!50003 SET sql_mode              = 'STRICT_TRANS_TABLES,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION' */ ;
+/*!50003 DROP FUNCTION IF EXISTS `makeStmt` */;
+/*!50003 SET @saved_cs_client      = @@character_set_client */ ;
+/*!50003 SET @saved_cs_results     = @@character_set_results */ ;
+/*!50003 SET @saved_col_connection = @@collation_connection */ ;
+/*!50003 SET character_set_client  = utf8 */ ;
+/*!50003 SET character_set_results = utf8 */ ;
+/*!50003 SET collation_connection  = utf8_general_ci */ ;
+DELIMITER ;;
+CREATE DEFINER=`root`@`%` FUNCTION `makeStmt`(sourceSet VARCHAR(255), targetSet VARCHAR(255), data VARCHAR(20000),
+                                properties VARCHAR(20000), versioned BOOLEAN) RETURNS varchar(20000) CHARSET utf8 COLLATE utf8_unicode_ci
+    NO SQL
+BEGIN
+        IF sourceSet = "entities" AND versioned THEN
+            RETURN CONCAT('INSERT IGNORE INTO `',
+                targetSet,
+                '` (id, _iversion) SELECT entity_id, _iversion FROM ',
+                data,
+                IF(properties IS NULL, '',
+                    CONCAT(' AS data JOIN `', properties, '` AS prop ON (data.property_id = prop.id) WHERE ',
+                           'data.entity_id = prop.id2 OR prop.id2 = 0')));
+        END IF;
+        RETURN CONCAT(
+            IF(targetSet IS NULL,
+                CONCAT('DELETE FROM `',sourceSet,'` WHERE NOT EXISTS (SELECT 1 FROM '), 
+                CONCAT('INSERT IGNORE INTO `',targetSet,'` (id) SELECT id FROM `',sourceSet,'` ',
+                       'WHERE EXISTS (SELECT 1 FROM ')),
+            IF(properties IS NULL,
+                CONCAT(data,' as data WHERE '),
+                CONCAT('`',properties,'` as prop JOIN ',data,' as data ON (data.property_id=prop.id) WHERE ',
+                       '(data.entity_id=prop.id2 OR prop.id2=0) AND ')),
+            'data.entity_id=`', sourceSet, '`.`id` LIMIT 1)'
+        );
+
+END ;;
+DELIMITER ;
+/*!50003 SET sql_mode              = @saved_sql_mode */ ;
+/*!50003 SET character_set_client  = @saved_cs_client */ ;
+/*!50003 SET character_set_results = @saved_cs_results */ ;
+/*!50003 SET collation_connection  = @saved_col_connection */ ;
+/*!50003 SET @saved_sql_mode       = @@sql_mode */ ;
+/*!50003 SET sql_mode              = 'STRICT_TRANS_TABLES,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION' */ ;
+/*!50003 DROP FUNCTION IF EXISTS `standard_unit` */;
+/*!50003 SET @saved_cs_client      = @@character_set_client */ ;
+/*!50003 SET @saved_cs_results     = @@character_set_results */ ;
+/*!50003 SET @saved_col_connection = @@collation_connection */ ;
+/*!50003 SET character_set_client  = utf8 */ ;
+/*!50003 SET character_set_results = utf8 */ ;
+/*!50003 SET collation_connection  = utf8_general_ci */ ;
+DELIMITER ;;
+CREATE DEFINER=`root`@`%` FUNCTION `standard_unit`(unit_sig BIGINT) RETURNS bigint(20)
+    DETERMINISTIC
+BEGIN
+    DECLARE ret BIGINT DEFAULT unit_sig;
+
+    SELECT signature_to INTO ret FROM units_lin_con WHERE signature_from=unit_sig;
+    RETURN ret;
+END ;;
+DELIMITER ;
+/*!50003 SET sql_mode              = @saved_sql_mode */ ;
+/*!50003 SET character_set_client  = @saved_cs_client */ ;
+/*!50003 SET character_set_results = @saved_cs_results */ ;
+/*!50003 SET collation_connection  = @saved_col_connection */ ;
+/*!50003 SET @saved_sql_mode       = @@sql_mode */ ;
+/*!50003 SET sql_mode              = 'STRICT_TRANS_TABLES,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION' */ ;
+/*!50003 DROP FUNCTION IF EXISTS `_get_head_iversion` */;
+/*!50003 SET @saved_cs_client      = @@character_set_client */ ;
+/*!50003 SET @saved_cs_results     = @@character_set_results */ ;
+/*!50003 SET @saved_col_connection = @@collation_connection */ ;
+/*!50003 SET character_set_client  = utf8 */ ;
+/*!50003 SET character_set_results = utf8 */ ;
+/*!50003 SET collation_connection  = utf8_general_ci */ ;
+DELIMITER ;;
+CREATE DEFINER=`root`@`%` FUNCTION `_get_head_iversion`(InternalEntityID INT UNSIGNED) RETURNS int(10) unsigned
+    READS SQL DATA
+BEGIN
+    
+    
+    
+    
+    RETURN (
+        SELECT e._iversion
+            FROM entity_version AS e
+            WHERE e.entity_id = InternalEntityID
+            ORDER BY e._iversion DESC
+            LIMIT 1
+        );
+END ;;
+DELIMITER ;
+/*!50003 SET sql_mode              = @saved_sql_mode */ ;
+/*!50003 SET character_set_client  = @saved_cs_client */ ;
+/*!50003 SET character_set_results = @saved_cs_results */ ;
+/*!50003 SET collation_connection  = @saved_col_connection */ ;
+/*!50003 SET @saved_sql_mode       = @@sql_mode */ ;
+/*!50003 SET sql_mode              = 'STRICT_TRANS_TABLES,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION' */ ;
+/*!50003 DROP FUNCTION IF EXISTS `_get_version` */;
+/*!50003 SET @saved_cs_client      = @@character_set_client */ ;
+/*!50003 SET @saved_cs_results     = @@character_set_results */ ;
+/*!50003 SET @saved_col_connection = @@collation_connection */ ;
+/*!50003 SET character_set_client  = utf8 */ ;
+/*!50003 SET character_set_results = utf8 */ ;
+/*!50003 SET collation_connection  = utf8_general_ci */ ;
+DELIMITER ;;
+CREATE DEFINER=`root`@`%` FUNCTION `_get_version`(InternalEntityID INT UNSIGNED,
+    IVersion INT UNSIGNED) RETURNS varbinary(255)
+    READS SQL DATA
+BEGIN
+    RETURN (
+        SELECT version FROM entity_version
+            WHERE entity_id = InternalEntityID
+            AND _iversion = IVersion
+        );
+END ;;
+DELIMITER ;
+/*!50003 SET sql_mode              = @saved_sql_mode */ ;
+/*!50003 SET character_set_client  = @saved_cs_client */ ;
+/*!50003 SET character_set_results = @saved_cs_results */ ;
+/*!50003 SET collation_connection  = @saved_col_connection */ ;
+/*!50003 SET @saved_sql_mode       = @@sql_mode */ ;
+/*!50003 SET sql_mode              = 'STRICT_TRANS_TABLES,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION' */ ;
+/*!50003 DROP PROCEDURE IF EXISTS `applyBackReference` */;
+/*!50003 SET @saved_cs_client      = @@character_set_client */ ;
+/*!50003 SET @saved_cs_results     = @@character_set_results */ ;
+/*!50003 SET @saved_col_connection = @@collation_connection */ ;
+/*!50003 SET character_set_client  = utf8 */ ;
+/*!50003 SET character_set_results = utf8 */ ;
+/*!50003 SET collation_connection  = utf8_general_ci */ ;
+DELIMITER ;;
+CREATE DEFINER=`root`@`%` PROCEDURE `applyBackReference`(in sourceSet VARCHAR(255), targetSet VARCHAR(255),
+    in propertiesTable VARCHAR(255), in entitiesTable VARCHAR(255), in subQuery BOOLEAN,
+    in versioned BOOLEAN)
+BEGIN
+    DECLARE newTableName VARCHAR(255) DEFAULT NULL;
+
+
+    IF subQuery IS TRUE THEN
+        call registerTempTableName(newTableName);
+
+        SET @createBackRefSubQueryTableStr = CONCAT('CREATE TEMPORARY TABLE `',newTableName,'` ( entity_id INT UNSIGNED NOT NULL, id INT UNSIGNED NOT NULL, CONSTRAINT `',newTableName,'PK` PRIMARY KEY (id, entity_id))');
+
+        PREPARE createBackRefSubQueryTable FROM @createBackRefSubQueryTableStr;
+        EXECUTE createBackRefSubQueryTable;
+        DEALLOCATE PREPARE createBackRefSubQueryTable;
+
+        SET @backRefSubResultSetStmtStr = CONCAT('INSERT IGNORE INTO `',
+            newTableName,
+            '` (id,entity_id) SELECT entity_id AS id, value AS entity_id FROM `reference_data` AS data ',
+            'WHERE EXISTS (SELECT 1 FROM `',
+                sourceSet,
+                '` AS source WHERE source.id=data.value LIMIT 1)',
+            IF(propertiesTable IS NULL,
+                '',
+                CONCAT(' AND EXISTS (SELECT 1 FROM `',
+                    propertiesTable,
+                    '` AS p WHERE p.id=data.property_id LIMIT 1)')),
+            IF(entitiesTable IS NULL,
+                '',
+                CONCAT(' AND EXISTS (SELECT 1 FROM `',
+                    entitiesTable,
+                    '` AS e WHERE e.id=data.entity_id LIMIT 1)'))
+        );
+
+        PREPARE backRefSubResultSetStmt FROM @backRefSubResultSetStmtStr;
+        EXECUTE backRefSubResultSetStmt;
+        DEALLOCATE PREPARE backRefSubResultSetStmt;
+
+        SELECT newTableName as list;
+    ELSE
+        IF versioned THEN
+            IF sourceSet = "entities" THEN
+                
+                SET @stmtBackRefStr = CONCAT('INSERT IGNORE INTO `',
+                    targetSet,
+                    '` (id, _iversion) SELECT source.id, _get_head_iversion(source.id)',
+                    
+                    ' FROM entities AS source WHERE EXISTS (',
+                        'SELECT 1 FROM `reference_data` AS data WHERE data.value=source.id AND (',
+                            'data.value_iversion IS NULL OR data.value_iversion=_get_head_iversion(source.id))',
+                    IF(entitiesTable IS NULL,
+                        '',
+                        CONCAT(' AND EXISTS (SELECT 1 FROM `',
+                            entitiesTable,
+                            '` AS e WHERE e.id=data.entity_id LIMIT 1)')),
+                    IF(propertiesTable IS NULL,
+                        '',
+                        CONCAT(' AND EXISTS (SELECT 1 FROM `',
+                            propertiesTable,
+                            '` AS p WHERE p.id=data.property_id LIMIT 1)')),
+                    ') UNION ALL ',
+                    
+                    'SELECT source.id, source._iversion FROM archive_entities AS source WHERE EXISTS (',
+                        'SELECT 1 FROM `reference_data` AS data WHERE data.value=source.id AND ',
+                          '(data.value_iversion IS NULL OR data.value_iversion=source._iversion)',
+                    IF(entitiesTable IS NULL,
+                        '',
+                        CONCAT(' AND EXISTS (SELECT 1 FROM `',
+                            entitiesTable,
+                            '` AS e WHERE e.id=data.entity_id LIMIT 1)')),
+                    IF(propertiesTable IS NULL,
+                        '',
+                        CONCAT(' AND EXISTS (SELECT 1 FROM `',
+                            propertiesTable,
+                            '` AS p WHERE p.id=data.property_id LIMIT 1)')),
+
+                    ')');
+            ELSEIF targetSet IS NULL OR sourceSet = targetSet THEN
+                SET @stmtBackRefStr = CONCAT('DELETE FROM `',
+                    sourceSet,
+                    '` WHERE NOT EXISTS (SELECT 1 FROM `reference_data` AS data WHERE data.value=`',
+                    sourceSet,
+                    '`.`id` AND ( data.value_iversion IS NULL OR data.value_iversion=`',
+                    sourceSet,
+                    '`._iversion)',
+                    IF(entitiesTable IS NULL,
+                        '',
+                        CONCAT(' AND EXISTS (SELECT 1 FROM `',
+                            entitiesTable,
+                            '` AS e WHERE e.id=data.entity_id LIMIT 1)')),
+                    IF(propertiesTable IS NULL,
+                        '',
+                        CONCAT(' AND EXISTS (SELECT 1 FROM `',
+                            propertiesTable,
+                            '` AS p WHERE p.id=data.property_id LIMIT 1)')),
+                    ')');
+            ELSE
+                SET @stmtBackRefStr = CONCAT('INSERT IGNORE INTO `',
+                    targetSet,
+                    '` (id, _iversion) SELECT source.id, source._iversion FROM `',
+                    sourceSet,
+                    '` AS source WHERE EXISTS (',
+                    'SELECT 1 FROM `reference_data` AS data WHERE data.value=source.id AND',
+                    ' (data.value_iversion IS NULL OR data.value_iversion=source._iversion)',
+                    IF(entitiesTable IS NULL,
+                        '',
+                        CONCAT(' AND EXISTS (SELECT 1 FROM `',
+                            entitiesTable,
+                            '` AS e WHERE e.id=data.entity_id LIMIT 1)')),
+                    IF(propertiesTable IS NULL,
+                        '',
+                        CONCAT(' AND EXISTS (SELECT 1 FROM `',
+                            propertiesTable,
+                            '` AS p WHERE p.id=data.property_id LIMIT 1)')),
+
+                    ')');
+            END IF;
+        ELSE
+            
+            IF targetSet IS NULL OR sourceSet = targetSet THEN
+                
+                SET @stmtBackRefStr = CONCAT('DELETE FROM `',
+                    sourceSet,
+                    '` WHERE NOT EXISTS (SELECT 1 FROM `reference_data` AS data WHERE data.value=`',
+                    sourceSet,
+                    '`.`id`',
+                    IF(entitiesTable IS NULL,
+                        '',
+                        CONCAT('
+                            AND EXISTS (SELECT 1 FROM `',
+                            entitiesTable,
+                            '` AS e WHERE e.id=data.entity_id LIMIT 1)')),
+                    IF(propertiesTable IS NULL,
+                        '',
+                        CONCAT('
+                            AND EXISTS (SELECT 1 FROM `',
+                            propertiesTable,
+                            '` AS p WHERE p.id=data.property_id LIMIT 1)')),
+                    ')');
+            ELSE
+                
+                SET @stmtBackRefStr = CONCAT('INSERT IGNORE INTO `',
+                    targetSet,
+                    '` (id) SELECT id FROM `',
+                    sourceSet,
+                    '` AS source WHERE EXISTS (SELECT 1 FROM `reference_data` AS data WHERE data.value=source.id',
+                    IF(entitiesTable IS NULL,
+                        '',
+                        CONCAT(' AND EXISTS (SELECT 1 FROM `',
+                            entitiesTable,
+                            '` AS e WHERE e.id=data.entity_id LIMIT 1)')),
+                    IF(propertiesTable IS NULL,
+                        '',
+                        CONCAT(' AND EXISTS (SELECT 1 FROM `',
+                            propertiesTable,
+                            '` AS p WHERE p.id=data.property_id LIMIT 1)')),
+                    ')');
+            END IF;
+        END IF;
+
+        PREPARE stmtBackRef FROM @stmtBackRefStr;
+        EXECUTE stmtBackRef;
+        DEALLOCATE PREPARE stmtBackRef;
+    END IF;
+
+END ;;
+DELIMITER ;
+/*!50003 SET sql_mode              = @saved_sql_mode */ ;
+/*!50003 SET character_set_client  = @saved_cs_client */ ;
+/*!50003 SET character_set_results = @saved_cs_results */ ;
+/*!50003 SET collation_connection  = @saved_col_connection */ ;
+/*!50003 SET @saved_sql_mode       = @@sql_mode */ ;
+/*!50003 SET sql_mode              = 'STRICT_TRANS_TABLES,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION' */ ;
+/*!50003 DROP PROCEDURE IF EXISTS `applyIDFilter` */;
+/*!50003 SET @saved_cs_client      = @@character_set_client */ ;
+/*!50003 SET @saved_cs_results     = @@character_set_results */ ;
+/*!50003 SET @saved_col_connection = @@collation_connection */ ;
+/*!50003 SET character_set_client  = utf8 */ ;
+/*!50003 SET character_set_results = utf8 */ ;
+/*!50003 SET collation_connection  = utf8_general_ci */ ;
+DELIMITER ;;
+CREATE DEFINER=`root`@`%` PROCEDURE `applyIDFilter`(in sourceSet VARCHAR(255), in targetSet VARCHAR(255),
+    in o CHAR(2), in EntityID VARCHAR(255), in agg CHAR(3), in versioned BOOLEAN)
+IDFILTER_LABEL: BEGIN
+DECLARE data VARCHAR(20000) DEFAULT NULL;
+DECLARE aggVal VARCHAR(255) DEFAULT NULL;
+DECLARE direction CHAR(4) DEFAULT NULL;
+DECLARE entity_id_type VARCHAR(255) DEFAULT "eids.id ";
+
+
+IF agg IS NOT NULL THEN
+    IF versioned THEN
+        
+        SELECT 1 FROM id_agg_with_versioning_not_implemented;
+    ELSEIF agg = "max" THEN
+        SET direction = "DESC";
+    ELSEIF agg = "min" THEN
+        SET direction = "ASC ";
+    ELSE
+        SELECT 1 FROM unknown_agg_parameter;
+    END IF;
+
+    SET @stmtIDAggValStr = CONCAT(
+        "SELECT e.internal_id INTO @sAggVal FROM `",
+        sourceSet,
+        "` AS s LEFT JOIN entity_ids AS e ON (s.id=e.internal_id) WHERE s.id>99 ORDER BY CAST(e.id AS UNSIGNED INT) ",
+        direction,
+        " LIMIT 1");
+
+    PREPARE stmtIDAggVal FROM @stmtIDAggValStr;
+    EXECUTE stmtIDAggVal;
+    DEALLOCATE PREPARE stmtIDAggVal;
+    SET aggVal = @sAggVal;
+END IF;
+
+IF o = ">" OR o = ">=" OR o = "<" or o = "<=" THEN
+    SET entity_id_type = "CAST(eids.id AS UNSIGNED INT) ";
+END IF;
+
+
+IF targetSet IS NULL OR targetSet = sourceSet THEN
+    SET data = CONCAT(
+        "DELETE FROM `",
+        sourceSet,
+        "` WHERE ",
+        IF(o IS NULL OR EntityID IS NULL,
+            "1=1",
+            CONCAT("NOT EXISTS (SELECT 1 FROM entity_ids AS eids WHERE ",
+                entity_id_type,
+                o,
+                ' "',
+                EntityID,
+                '" ',
+                " AND eids.internal_id = `",
+                sourceSet,
+                "`.id)"
+            )),
+        IF(aggVal IS NULL,
+            "",
+            CONCAT(" AND `", sourceSet, "`.id!=",
+                aggVal)));
+ELSEIF versioned AND sourceSet = "entities" THEN
+    
+    SET data = CONCAT(
+        "INSERT IGNORE INTO `",
+        targetSet,
+        '` (id, _iversion) SELECT e.id, _get_head_iversion(e.id) FROM `entities` AS e JOIN entity_ids AS eids ON (e.id = eids.internal_id) WHERE ',
+        IF(o IS NULL OR EntityID IS NULL,
+            "1=1",
+            CONCAT(entity_id_type,
+                o,
+                ' "',
+                EntityID,
+                '"'
+            )),
+        IF(aggVal IS NULL,
+            "",
+            CONCAT(" AND e.id=",
+                aggVal)),
+        ' UNION SELECT e.id, _iversion FROM `archive_entities` AS e JOIN entity_ids AS eids ON (e.id = eids.internal_id) WHERE ',
+        IF(o IS NULL OR EntityID IS NULL,
+            "1=1",
+            CONCAT(entity_id_type,
+                o,
+                ' "',
+                EntityID,
+                '"'
+            )),
+        IF(aggVal IS NULL,
+            "",
+            CONCAT(" AND e.id=",
+                aggVal)));
+    
+
+ELSE
+    SET data = CONCAT(
+        "INSERT IGNORE INTO `",
+        targetSet,
+        IF(versioned,
+            '` (id, _iversion) SELECT data.id, data._iversion FROM `',
+            '` (id) SELECT data.id FROM `'),
+        sourceSet,
+        "` AS data JOIN entity_ids AS eids ON (eids.internal_id = data.id) WHERE ",
+        IF(o IS NULL OR EntityID IS NULL,
+            "1=1",
+            CONCAT(entity_id_type,
+                o,
+                ' "',
+                EntityID,
+                '"'
+            )),
+        IF(aggVal IS NULL,
+            "",
+            CONCAT(" AND data.id=",
+                aggVal)));
+END IF;
+
+Set @stmtIDFilterStr = data;
+PREPARE stmtIDFilter FROM @stmtIDFilterStr;
+EXECUTE stmtIDFilter;
+DEALLOCATE PREPARE stmtIDFilter;
+
+END ;;
+DELIMITER ;
+/*!50003 SET sql_mode              = @saved_sql_mode */ ;
+/*!50003 SET character_set_client  = @saved_cs_client */ ;
+/*!50003 SET character_set_results = @saved_cs_results */ ;
+/*!50003 SET collation_connection  = @saved_col_connection */ ;
+/*!50003 SET @saved_sql_mode       = @@sql_mode */ ;
+/*!50003 SET sql_mode              = 'STRICT_TRANS_TABLES,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION' */ ;
+/*!50003 DROP PROCEDURE IF EXISTS `applyPOV` */;
+/*!50003 SET @saved_cs_client      = @@character_set_client */ ;
+/*!50003 SET @saved_cs_results     = @@character_set_results */ ;
+/*!50003 SET @saved_col_connection = @@collation_connection */ ;
+/*!50003 SET character_set_client  = utf8 */ ;
+/*!50003 SET character_set_results = utf8 */ ;
+/*!50003 SET collation_connection  = utf8_general_ci */ ;
+DELIMITER ;;
+CREATE DEFINER=`root`@`%` PROCEDURE `applyPOV`(in sourceSet VARCHAR(255), 
+                                 in targetSet VARCHAR(255), 
+                                 in propertiesTable VARCHAR(255),
+                                 in refIdsTable VARCHAR(255), 
+                                 in o CHAR(4), 
+                                 in vText VARCHAR(255),  
+                                 in vInt INT,  
+                                 in vDouble DOUBLE,  
+                                 in unit_sig BIGINT, 
+                                 in vDoubleStdUnit DOUBLE, 
+                                 in stdUnit_sig BIGINT, 
+                                 in vDateTime VARCHAR(255),
+                                 in vDateTimeDotNotation VARCHAR(255),
+                                 in agg CHAR(3), 
+                                 in pname VARCHAR(255), 
+                                 in versioned BOOLEAN)
+POV_LABEL: BEGIN
+    DECLARE data TEXT DEFAULT NULL; 
+    DECLARE sTextData VARCHAR(20000) DEFAULT NULL; 
+    DECLARE sNameData VARCHAR(20000) DEFAULT NULL; 
+    DECLARE sEnumData VARCHAR(20000) DEFAULT NULL; 
+    DECLARE sIntData VARCHAR(20000) DEFAULT NULL; 
+    DECLARE sDoubleData VARCHAR(20000) DEFAULT NULL; 
+    DECLARE sDatetimeData VARCHAR(20000) DEFAULT NULL; 
+    DECLARE sNullData VARCHAR(20000) DEFAULT NULL; 
+    DECLARE sDateData VARCHAR(20000) DEFAULT NULL; 
+    DECLARE sRefData VARCHAR(20000) DEFAULT NULL; 
+    DECLARE aggValue VARCHAR(255) DEFAULT NULL;
+    DECLARE aggValueWhereClause VARCHAR(20000) DEFAULT NULL;
+    DECLARE distinctUnits INT DEFAULT 0;
+    DECLARE usedStdUnit BIGINT DEFAULT NULL;
+    DECLARE keepTabl VARCHAR(255) DEFAULT NULL;
+    DECLARE existence_op VARCHAR(255) DEFAULT "EXISTS";
+
+        
+        
+        
+
+    IF o = '->' THEN
+        
+        call applyRefPOV(sourceSet,targetSet, propertiesTable, refIdsTable, versioned);
+        LEAVE POV_LABEL;
+    ELSEIF o = '0' THEN
+        
+        
+        SET vText = NULL;
+        SET sTextData = 'SELECT domain_id, entity_id, property_id FROM `null_data` AS subdata';
+
+        
+
+    ELSEIF o = '!0' THEN
+        
+        
+        SET vText = NULL;
+        
+        SET sTextData = CONCAT(
+        'SELECT DISTINCT domain_id, entity_id, property_id FROM `text_data` AS subdata ',
+            'WHERE subdata.value IS NOT NULL UNION ALL ',
+        'SELECT DISTINCT domain_id, entity_id, property_id FROM `name_data` AS subdata ',
+            'WHERE subdata.value IS NOT NULL UNION ALL ',
+        'SELECT DISTINCT domain_id, entity_id, property_id FROM `enum_data` AS subdata ',
+            'WHERE subdata.value IS NOT NULL UNION ALL ',
+        'SELECT DISTINCT domain_id, entity_id, property_id FROM `integer_data` AS subdata ',
+            'WHERE subdata.value IS NOT NULL UNION ALL ',
+        'SELECT DISTINCT domain_id, entity_id, property_id FROM `double_data` AS subdata ',
+            'WHERE subdata.value IS NOT NULL UNION ALL ',
+        'SELECT DISTINCT domain_id, entity_id, property_id FROM `date_data` AS subdata ',
+            'WHERE subdata.value IS NOT NULL UNION ALL ',
+        'SELECT DISTINCT domain_id, entity_id, property_id FROM `datetime_data` AS subdata ',
+            'WHERE subdata.value IS NOT NULL UNION ALL ',
+        'SELECT DISTINCT domain_id, entity_id, property_id FROM `reference_data` AS subdata ',
+            'WHERE subdata.value IS NOT NULL');
+
+    ELSEIF o = "(" or o = "!(" THEN  
+        IF versioned THEN
+            SET sTextData = IF(vText IS NULL,
+            CONCAT(
+                ' SELECT DISTINCT domain_id, entity_id, _get_head_iversion(entity_id) ',
+                'AS _iversion, property_id FROM `date_data` UNION ALL ',
+                'SELECT DISTINCT domain_id, entity_id, _iversion, property_id FROM `archive_date_data`'),
+            IF(vDateTimeDotNotation IS NULL, NULL,  
+                CONCAT(' SELECT DISTINCT domain_id, entity_id, _get_head_iversion(entity_id) ',
+                       'AS _iversion, property_id FROM `date_data` AS subdata WHERE ',
+                       getDateWhereClause(vDateTimeDotNotation, o), ' UNION ALL ',
+                       'SELECT DISTINCT domain_id, entity_id, _iversion, property_id FROM `archive_date_data` ',
+                       'AS subdata WHERE ', getDateWhereClause(vDateTimeDotNotation, o))));
+            SET sDatetimeData = IF(vText IS NULL, ' UNION ALL SELECT DISTINCT domain_id, entity_id, _get_head_iversion(entity_id) AS _iversion, property_id FROM `datetime_data` UNION ALL SELECT DISTINCT domain_id, entity_id, _iversion, property_id FROM `archive_datetime_data`',
+                                                  IF(vDateTime IS NULL, NULL,
+                                                                        CONCAT(' UNION ALL SELECT DISTINCT domain_id, entity_id, _get_head_iversion(entity_id) AS _iversion, property_id FROM `datetime_data` AS subdata WHERE ', getDateTimeWhereClause(vDateTime, o), ' UNION ALL SELECT DISTINCT domain_id, entity_id,_iversion, property_id FROM `archive_datetime_data` AS subdata WHERE ', getDateTimeWhereClause(vDateTime, o))));
+        ELSE  
+            SET sTextData = IF(vText IS NULL,
+                ' SELECT DISTINCT domain_id, entity_id, property_id FROM `date_data`',
+                IF(vDateTimeDotNotation IS NULL, NULL,
+                    CONCAT(' SELECT DISTINCT domain_id, entity_id, property_id FROM `date_data` AS subdata WHERE ',
+                             getDateWhereClause(vDateTimeDotNotation, o))));
+            SET sDatetimeData = IF(vText IS NULL,
+                ' UNION ALL SELECT DISTINCT domain_id, entity_id, property_id FROM `datetime_data`',
+                IF(vDateTime IS NULL, NULL,
+                   CONCAT(' UNION ALL SELECT DISTINCT domain_id, entity_id, property_id FROM `datetime_data` ',
+                           'AS subdata WHERE ', getDateTimeWhereClause(vDateTime, o))));
+        END IF;
+        SET vText = NULL;
+    ELSEIF agg IS NOT NULL THEN
+        IF versioned THEN
+            SELECT 1 FROM versioned_agg_pov_filter_not_implemented;
+        END IF;
+        
+
+        
+        SET aggValueWhereClause = CONCAT(getDoubleWhereClause(vDouble, unit_sig, vDoubleStdUnit, stdUnit_sig, o), ' AND ');
+        SET aggValueWhereClause = CONCAT(IF(aggValueWhereClause IS NULL, '', aggValueWhereClause), getAggValueWhereClause(sourceSet, propertiesTable));
+
+        
+        SET @aggValueStmtStr = CONCAT('SELECT ',agg,'(subdata.value), ', agg, '(convert_unit(subdata.unit_sig,subdata.value)), COUNT(DISTINCT standard_unit(subdata.unit_sig)), max(standard_unit(subdata.unit_sig)) INTO @sAggValue, @sAggValueConvert, @distinctUnits, @StdUnitSig FROM (SELECT entity_id, property_id, value, unit_sig FROM `integer_data` UNION ALL SELECT entity_id, property_id, value, unit_sig FROM `double_data`) AS subdata WHERE ', aggValueWhereClause);
+
+        
+        PREPARE stmtAggValueStmt FROM @aggValueStmtStr;
+        EXECUTE stmtAggValueStmt;
+        DEALLOCATE PREPARE stmtAggValueStmt;
+
+        SET distinctUnits = @distinctUnits;
+        SET aggValue = @sAggValue;
+
+        
+        IF distinctUnits = 1 THEN
+            SET aggValue = @sAggValueConvert;
+            SET usedStdUnit = @StdUnitSig;
+        ELSE
+            call raiseWarning(CONCAT("The filter POV(",IF(pname IS NULL, 'NULL', pname),",",IF(o IS NULL, 'NULL', o),",",IF(vText IS NULL, 'NULL', vText),") with the aggregate function '", agg, "' could not match the values against each other with their units. The values had different base units. Only their numric value had been taken into account." ));
+        END IF;
+
+        IF aggValue IS NULL THEN
+            SET sTextData = 'SELECT NULL as domain_id, NULL as entity_id, NULL as property_id';
+        ELSE
+            SET sTextData = '';
+            SET sIntData = CONCAT(' UNION ALL SELECT DISTINCT domain_id, entity_id, property_id FROM `integer_data` as subdata WHERE ', getDoubleWhereClause(aggValue, usedStdUnit, aggValue, usedStdUnit, '='));
+            SET sDoubleData = CONCAT(' SELECT DISTINCT domain_id, entity_id, property_id FROM `double_data` as subdata WHERE ', getDoubleWhereClause(aggValue, usedStdUnit, aggValue, usedStdUnit, '='));
+        END IF;
+
+        SET vText = NULL;
+    ELSE
+        
+        IF versioned THEN
+            SET sTextData = IF(vText IS NULL,
+            'SELECT DISTINCT domain_id, entity_id, _get_head_iversion(entity_id) AS _iversion, property_id FROM `text_data` UNION ALL SELECT DISTINCT domain_id, entity_id, _iversion, property_id FROM `archive_text_data` ',
+            CONCAT(
+            'SELECT DISTINCT domain_id, entity_id, _get_head_iversion(entity_id) AS _iversion, property_id ',
+              'FROM `text_data` AS subdata WHERE subdata.value ', o,' ? ',
+            'UNION ALL SELECT DISTINCT domain_id, entity_id, _iversion, property_id ',
+              'FROM `archive_text_data` AS subdata WHERE subdata.value ', o, '?'
+            ));
+            SET sNameData = IF(vText IS NULL, ' UNION ALL SELECT DISTINCT domain_id, entity_id, _get_head_iversion(entity_id) AS _iversion, property_id FROM `name_data` UNION ALL SELECT DISTINCT domain_id, entity_id, _iversion, property_id FROM `archive_name_data` ', CONCAT(' UNION ALL SELECT DISTINCT domain_id, entity_id, _get_head_iversion(entity_id) AS _iversion, property_id FROM `name_data` AS subdata WHERE subdata.value ', o, ' ? UNION ALL SELECT DISTINCT domain_id, entity_id, _iversion, property_id FROM `archive_name_data` AS subdata WHERE subdata.value ', o, '?'));
+            SET sEnumData = IF(vText IS NULL, ' UNION ALL SELECT DISTINCT domain_id, entity_id, _get_head_iversion(entity_id) AS _iversion, property_id FROM `enum_data` UNION ALL SELECT DISTINCT domain_id, entity_id, _iversion, property_id FROM `archive_enum_data` ', CONCAT(' UNION ALL SELECT DISTINCT domain_id, entity_id, _get_head_iversion(entity_id) AS _iversion, property_id FROM `enum_data` AS subdata WHERE subdata.value ', o, ' ? UNION ALL SELECT DISTINCT domain_id, entity_id, _iversion, property_id FROM `archive_enum_data` AS subdata WHERE subdata.value ', o, '?'));
+            IF o = "!=" AND refIdsTable IS NOT NULL THEN
+                SET existence_op = "NOT EXISTS";
+            END IF;
+            SET sRefData = IF(vText IS NULL,
+                ' UNION ALL SELECT DISTINCT domain_id, entity_id, _get_head_iversion(entity_id) AS _iversion, property_id FROM `reference_data` UNION ALL SELECT DISTINCT domain_id, entity_id, _iversion, property_id FROM `archive_reference_data`',
+                IF(refIdsTable IS NULL,
+                    NULL,
+                    CONCAT(' UNION ALL SELECT DISTINCT domain_id, entity_id, _get_head_iversion(entity_id) AS _iversion, property_id FROM `reference_data` AS subdata WHERE ', existence_op, ' (SELECT 1 FROM `', refIdsTable, '` AS refIdsTable WHERE subdata.value=refIdsTable.id LIMIT 1) AND subdata.status != "REPLACEMENT" UNION ALL SELECT DISTINCT domain_id, entity_id, _iversion, property_id FROM `archive_reference_data` AS subdata WHERE ', existence_op, ' (SELECT 1 FROM `', refIdsTable, '` AS refIdsTable WHERE subdata.value=refIdsTable.id LIMIT 1) AND subdata.status != "REPLACEMENT"')));
+            SET sDoubleData = IF(vText IS NULL, ' UNION ALL SELECT DISTINCT subdata.domain_id, subdata.entity_id, _get_head_iversion(subdata.entity_id) AS _iversion, subdata.property_id FROM `double_data` AS subdata UNION ALL SELECT DISTINCT domain_id, entity_id, _iversion, property_id FROM `archive_double_data` ', IF(vDouble IS NULL, NULL, CONCAT(' UNION ALL SELECT DISTINCT domain_id, entity_id, _get_head_iversion(entity_id), property_id FROM `double_data` AS subdata WHERE ', getDoubleWhereClause(vDouble,unit_sig,vDoubleStdUnit,stdUnit_sig,o), ' UNION ALL SELECT DISTINCT domain_id, entity_id, _iversion, property_id FROM `archive_double_data` AS subdata WHERE ', getDoubleWhereClause(vDouble, unit_sig, vDoubleStdUnit, stdUnit_sig, o))));
+            SET sIntData = IF(vText IS NULL, ' UNION ALL SELECT DISTINCT subdata.domain_id, subdata.entity_id, _get_head_iversion(subdata.entity_id) AS _iversion, subdata.property_id FROM `integer_data` AS subdata UNION ALL SELECT DISTINCT domain_id, entity_id, _iversion, property_id FROM `archive_integer_data`', IF(vInt IS NULL AND vDoubleStdUnit IS NULL, NULL, CONCAT(' UNION ALL SELECT DISTINCT domain_id, entity_id, _get_head_iversion(entity_id) AS _iversion, property_id FROM `integer_data` AS subdata WHERE ', getDoubleWhereClause(vInt, unit_sig, vDoubleStdUnit, stdUnit_sig, o), ' UNION ALL SELECT DISTINCT domain_id, entity_id, _iversion, property_id FROM `archive_integer_data` AS subdata WHERE ', getDoubleWhereClause(vInt, unit_sig, vDoubleStdUnit, stdUnit_sig, o))));
+            SET sDatetimeData = IF(vText IS NULL,' UNION ALL SELECT DISTINCT domain_id, entity_id, _get_head_iversion(entity_id) AS _iversion, property_id FROM `datetime_data` UNION ALL SELECT DISTINCT domain_id, entity_id, _iversion, property_id FROM `archive_datetime_data`', IF(vDateTime IS NULL, NULL, CONCAT(' UNION ALL SELECT DISTINCT domain_id, entity_id, _get_head_iversion(entity_id) AS _iversion, property_id FROM `datetime_data` AS subdata WHERE ',getDateTimeWhereClause(vDateTime,o), ' UNION ALL SELECT DISTINCT domain_id, entity_id, _iversion, property_id FROM `archive_datetime_data` AS subdata WHERE ',getDateTimeWhereClause(vDateTime,o))));
+            SET sDateData = IF(vText IS NULL,' UNION ALL SELECT DISTINCT domain_id, entity_id, _get_head_iversion(entity_id) AS _iversion, property_id FROM `date_data` UNION ALL SELECT DISTINCT domain_id, entity_id, _iversion, property_id FROM `archive_date_data`', IF(vDateTimeDotNotation IS NULL, NULL, CONCAT(' UNION ALL SELECT DISTINCT domain_id, entity_id, _get_head_iversion(entity_id) AS _iversion, property_id FROM `date_data` AS subdata WHERE ', getDateWhereClause(vDateTimeDotNotation,o), ' UNION ALL SELECT DISTINCT domain_id, entity_id, _iversion, property_id FROM `archive_date_data` AS subdata WHERE ', getDateWhereClause(vDateTimeDotNotation,o))));
+            SET sNullData = IF(vText IS NULL, ' UNION ALL SELECT DISTINCT domain_id, entity_id, _get_head_iversion(entity_id) AS _iversion, property_id FROM `null_data` UNION ALL SELECT DISTINCT domain_id, entity_id, _iversion, property_id FROM `archive_null_data`', NULL);
+        
+        ELSE
+            SET sTextData = IF(vText IS NULL, 'SELECT DISTINCT domain_id, entity_id, property_id FROM `text_data`', CONCAT('SELECT DISTINCT domain_id, entity_id, property_id FROM `text_data` AS subdata WHERE subdata.value ',o,' ?'));
+            SET sNameData = IF(vText IS NULL, ' UNION ALL SELECT DISTINCT domain_id, entity_id, property_id FROM `name_data`', CONCAT(' UNION ALL SELECT DISTINCT domain_id, entity_id, property_id FROM `name_data` AS subdata WHERE subdata.value ', o, ' ?'));
+            SET sEnumData = IF(vText IS NULL, ' UNION ALL SELECT DISTINCT domain_id, entity_id, property_id FROM `enum_data`', CONCAT(' UNION ALL SELECT DISTINCT domain_id, entity_id, property_id FROM `enum_data` AS subdata WHERE subdata.value ', o, ' ?'));
+            IF o = "!=" AND refIdsTable IS NOT NULL THEN
+                SET existence_op = "NOT EXISTS";
+            END IF;
+			
+            SET sRefData = IF(vText IS NULL,
+                ' UNION ALL SELECT DISTINCT domain_id, entity_id, property_id FROM `reference_data`',
+                IF(refIdsTable IS NULL,
+                    NULL,
+                    CONCAT(' UNION ALL SELECT DISTINCT domain_id, entity_id, property_id FROM `reference_data` AS subdata WHERE ',existence_op ,' (SELECT 1 FROM `', refIdsTable, '` AS refIdsTable WHERE subdata.value=refIdsTable.id LIMIT 1) AND subdata.status != "REPLACEMENT"')));
+            SET sDoubleData = IF(vText IS NULL, ' UNION ALL SELECT DISTINCT subdata.domain_id, subdata.entity_id, subdata.property_id FROM `double_data` AS subdata', IF(vDouble IS NULL, NULL, CONCAT(' UNION ALL SELECT DISTINCT domain_id, entity_id, property_id FROM `double_data` AS subdata WHERE ', getDoubleWhereClause(vDouble,unit_sig,vDoubleStdUnit,stdUnit_sig,o))));
+            SET sIntData = IF(vText IS NULL, ' UNION ALL SELECT DISTINCT subdata.domain_id, subdata.entity_id, subdata.property_id FROM `integer_data` AS subdata', IF(vInt IS NULL AND vDoubleStdUnit IS NULL, NULL, CONCAT(' UNION ALL SELECT DISTINCT domain_id, entity_id, property_id FROM `integer_data` AS subdata WHERE ', getDoubleWhereClause(vInt, unit_sig, vDoubleStdUnit, stdUnit_sig, o))));
+            SET sDatetimeData = IF(vText IS NULL,' UNION ALL SELECT DISTINCT domain_id, entity_id, property_id FROM `datetime_data`', IF(vDateTime IS NULL, NULL, CONCAT(' UNION ALL SELECT DISTINCT domain_id, entity_id, property_id FROM `datetime_data` AS subdata WHERE ',getDateTimeWhereClause(vDateTime,o))));
+            SET sDateData = IF(vText IS NULL,' UNION ALL SELECT DISTINCT domain_id, entity_id, property_id FROM `date_data`', IF(vDateTimeDotNotation IS NULL, NULL, CONCAT(' UNION ALL SELECT DISTINCT domain_id, entity_id, property_id FROM `date_data` AS subdata WHERE ',getDateWhereClause(vDateTimeDotNotation,o))));
+            SET sNullData = IF(vText IS NULL, ' UNION ALL SELECT DISTINCT domain_id, entity_id, property_id FROM `null_data`', NULL);
+        END IF;
+
+    END IF;
+
+    
+    SET data = CONCAT('(',sTextData,
+                IF(sNameData IS NULL, '', sNameData),
+                IF(sEnumData IS NULL, '', sEnumData),
+                IF(sDoubleData IS NULL, '', sDoubleData),
+                IF(sIntData IS NULL, '', sIntData),
+                IF(sDatetimeData IS NULL, '', sDatetimeData),
+                IF(sDateData IS NULL, '', sDateData),
+                IF(sRefData IS NULL, '', sRefData),
+                IF(sNullData IS NULL, '', sNullData),
+                ')'
+            );
+
+
+    call createTmpTable(keepTabl, versioned);
+    IF versioned THEN
+        
+        
+        SET @stmtPOVkeepTblStr = CONCAT(
+            'INSERT IGNORE INTO `', keepTabl, '` (id, _iversion) SELECT entity_id AS id, _iversion FROM ', data,
+            ' as data', IF(propertiesTable IS NULL, '', CONCAT(
+                ' WHERE EXISTS (Select 1 from `', propertiesTable, '` AS prop ',
+                  'WHERE prop.id = data.property_id AND (prop.id2=data.entity_id OR prop.id2=0))')));
+
+        IF targetSet IS NOT NULL THEN
+            SET @stmtPOVStr = CONCAT('INSERT IGNORE INTO `',
+                    targetSet,
+                    '` (id, _iversion) SELECT source.id, source._iversion FROM `',
+                    keepTabl,
+                    '` AS source');
+        ELSE
+        
+            SET @stmtPOVStr = CONCAT('DELETE FROM `',
+                    sourceSet,
+                    '` WHERE NOT EXISTS (SELECT 1 FROM `',
+                    keepTabl,
+                    '` AS data WHERE data.id=`',
+                    sourceSet,
+                    '`.`id` AND data._iversion=`',
+                    sourceSet,
+                    '`._iversion LIMIT 1)');
+
+        END IF;
+
+        
+        PREPARE stmt3 FROM @stmtPOVStr;
+        PREPARE stmtPOVkeepTbl FROM @stmtPOVkeepTblStr;
+        IF vText IS NULL THEN
+            EXECUTE stmtPOVkeepTbl;
+        ELSE
+            SET @vText = vText;
+            EXECUTE stmtPOVkeepTbl USING @vText, @vText, @vText, @vText, @vText, @vText;
+        END IF;
+        EXECUTE stmt3;
+        DEALLOCATE PREPARE stmt3;
+        DEALLOCATE PREPARE stmtPOVkeepTbl;
+    ELSE
+        
+        SET @stmtPOVkeepTblStr = CONCAT(
+            'INSERT IGNORE INTO `', keepTabl,
+            '` (id) SELECT DISTINCT entity_id AS id FROM ', data, ' as data',
+            IF(propertiesTable IS NULL, '',
+                CONCAT(' WHERE EXISTS (Select 1 from `', propertiesTable,
+                        '` AS prop WHERE prop.id = data.property_id AND
+                        (prop.id2=data.entity_id OR prop.id2=0))')));
+        
+
+        SET @stmtPOVStr = CONCAT(
+                IF(targetSet IS NULL,
+                    CONCAT('DELETE FROM `',
+                        sourceSet,
+                        '` WHERE NOT EXISTS (SELECT 1 FROM `'),
+                    CONCAT('INSERT IGNORE INTO `',
+                        targetSet,
+                        '` (id) SELECT id FROM `',
+                        sourceSet,
+                        '` WHERE EXISTS (SELECT 1 FROM `')),
+                keepTabl,
+                '` AS data WHERE data.id=`',
+                sourceSet,
+                '`.`id` LIMIT 1)'
+            );
+
+        
+        PREPARE stmt3 FROM @stmtPOVStr;
+        PREPARE stmtPOVkeepTbl FROM @stmtPOVkeepTblStr;
+        IF vText IS NULL THEN
+            EXECUTE stmtPOVkeepTbl;
+        ELSE
+            SET @vText = vText;
+            EXECUTE stmtPOVkeepTbl USING @vText, @vText, @vText;
+        END IF;
+        EXECUTE stmt3;
+        DEALLOCATE PREPARE stmt3;
+        DEALLOCATE PREPARE stmtPOVkeepTbl;
+    END IF;
+
+    SELECT @stmtPOVkeepTblStr as applyPOVStmt1, @stmtPOVStr as applyPOVStmt2, keepTabl as applyPOVIntermediateResultSet;
+
+
+END ;;
+DELIMITER ;
+/*!50003 SET sql_mode              = @saved_sql_mode */ ;
+/*!50003 SET character_set_client  = @saved_cs_client */ ;
+/*!50003 SET character_set_results = @saved_cs_results */ ;
+/*!50003 SET collation_connection  = @saved_col_connection */ ;
+/*!50003 SET @saved_sql_mode       = @@sql_mode */ ;
+/*!50003 SET sql_mode              = 'STRICT_TRANS_TABLES,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION' */ ;
+/*!50003 DROP PROCEDURE IF EXISTS `applyRefPOV` */;
+/*!50003 SET @saved_cs_client      = @@character_set_client */ ;
+/*!50003 SET @saved_cs_results     = @@character_set_results */ ;
+/*!50003 SET @saved_col_connection = @@collation_connection */ ;
+/*!50003 SET character_set_client  = utf8 */ ;
+/*!50003 SET character_set_results = utf8 */ ;
+/*!50003 SET collation_connection  = utf8_general_ci */ ;
+DELIMITER ;;
+CREATE DEFINER=`root`@`%` PROCEDURE `applyRefPOV`(in sourceSet VARCHAR(255), in targetSet VARCHAR(255),
+                                    in properties VARCHAR(255), in refs VARCHAR(255),
+                                    in versioned BOOLEAN)
+BEGIN
+    DECLARE data VARCHAR(20000) DEFAULT CONCAT(
+        '(SELECT domain_id, entity_id, property_id FROM `reference_data` AS subdata ',
+        'WHERE EXISTS (SELECT 1 FROM `', refs, '` AS refs WHERE subdata.value=refs.id LIMIT 1))');
+
+    IF versioned THEN
+        SET data = CONCAT(
+            '(SELECT domain_id, entity_id, _get_head_iversion(entity_id) AS _iversion, property_id ',
+                'FROM `reference_data` AS subdata WHERE EXISTS (',
+                    'SELECT 1 FROM `', refs, '` AS refs WHERE subdata.value=refs.id LIMIT 1) ',
+            'UNION ALL SELECT domain_id, entity_id, _iversion, property_id ',
+                'FROM `archive_reference_data` AS subdata WHERE EXISTS (',
+                    'SELECT 1 FROM `', refs, '` AS refs WHERE subdata.value=refs.id LIMIT 1))');
+    END IF;
+    SET @stmtRefPOVStr = makeStmt(sourceSet,targetSet,data,properties, versioned);
+
+    PREPARE stmt4 FROM @stmtRefPOVStr;
+    EXECUTE stmt4;
+
+    SELECT @stmtRefPOVstr as applyRefPOVStmt;
+
+END ;;
+DELIMITER ;
+/*!50003 SET sql_mode              = @saved_sql_mode */ ;
+/*!50003 SET character_set_client  = @saved_cs_client */ ;
+/*!50003 SET character_set_results = @saved_cs_results */ ;
+/*!50003 SET collation_connection  = @saved_col_connection */ ;
+/*!50003 SET @saved_sql_mode       = @@sql_mode */ ;
+/*!50003 SET sql_mode              = 'STRICT_TRANS_TABLES,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION' */ ;
+/*!50003 DROP PROCEDURE IF EXISTS `applySAT` */;
+/*!50003 SET @saved_cs_client      = @@character_set_client */ ;
+/*!50003 SET @saved_cs_results     = @@character_set_results */ ;
+/*!50003 SET @saved_col_connection = @@collation_connection */ ;
+/*!50003 SET character_set_client  = utf8 */ ;
+/*!50003 SET character_set_results = utf8 */ ;
+/*!50003 SET collation_connection  = utf8_general_ci */ ;
+DELIMITER ;;
+CREATE DEFINER=`root`@`%` PROCEDURE `applySAT`(in sourceSet VARCHAR(255), in targetSet VARCHAR(255), in loc MEDIUMTEXT, in op CHAR(5))
+BEGIN
+
+    IF targetSet IS NULL OR sourceSet = targetSet THEN
+        SET @stmtSATString = CONCAT('DELETE FROM `', sourceSet, '` WHERE id NOT IN (SELECT file_id FROM files WHERE path ', op, ' ?)');  
+    ELSE
+        SET @stmtSATString = CONCAT('INSERT INTO `', targetSet, '` (id) SELECT data.id FROM `',sourceSet,'` as data WHERE EXISTS (SELECT 1 FROM `files` as f WHERE f.file_id=data.id AND f.path ', op, ' ?)');
+    END IF;
+    PREPARE stmtSAT FROM @stmtSATString;
+	SET @loc = loc;
+    EXECUTE stmtSAT USING @loc;
+    DEALLOCATE PREPARE stmtSAT;
+
+END ;;
+DELIMITER ;
+/*!50003 SET sql_mode              = @saved_sql_mode */ ;
+/*!50003 SET character_set_client  = @saved_cs_client */ ;
+/*!50003 SET character_set_results = @saved_cs_results */ ;
+/*!50003 SET collation_connection  = @saved_col_connection */ ;
+/*!50003 SET @saved_sql_mode       = @@sql_mode */ ;
+/*!50003 SET sql_mode              = 'STRICT_TRANS_TABLES,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION' */ ;
+/*!50003 DROP PROCEDURE IF EXISTS `applyTransactionFilter` */;
+/*!50003 SET @saved_cs_client      = @@character_set_client */ ;
+/*!50003 SET @saved_cs_results     = @@character_set_results */ ;
+/*!50003 SET @saved_col_connection = @@collation_connection */ ;
+/*!50003 SET character_set_client  = utf8 */ ;
+/*!50003 SET character_set_results = utf8 */ ;
+/*!50003 SET collation_connection  = utf8_general_ci */ ;
+DELIMITER ;;
+CREATE DEFINER=`root`@`%` PROCEDURE `applyTransactionFilter`(in sourceSet VARCHAR(255), targetSet VARCHAR(255), in transaction VARCHAR(255), in operator_u CHAR(2), in realm VARCHAR(255), in userName VARCHAR(255), in ilb BIGINT, in ilb_nanos INT UNSIGNED, in eub BIGINT, in eub_nanos INT UNSIGNED, in operator_t CHAR(2))
+BEGIN
+    DECLARE data TEXT default CONCAT("(SELECT internal_id AS entity_id FROM transaction_log AS t JOIN entity_ids AS eids ON ( t.entity_id = eids.id ) WHERE t.transaction='",
+        transaction,
+        "'",
+        IF(userName IS NOT NULL,
+            CONCAT(' AND t.realm', operator_u, '? AND t.username', operator_u, '?'),
+            ''
+        ),
+        IF(ilb IS NOT NULL,
+            CONCAT(" AND", constructDateTimeWhereClauseForColumn("t.seconds", "t.nanos", ilb, ilb_nanos, eub, eub_nanos, operator_t)),
+            ""
+        ),
+        ')'
+    );
+
+    SET @stmtTransactionStr = makeStmt(sourceSet, targetSet, data, NULL, FALSE);
+    PREPARE stmtTransactionFilter from @stmtTransactionStr;
+    IF userName IS NOT NULL THEN
+        SET @userName = userName;
+        SET @realm = realm;
+        EXECUTE stmtTransactionFilter USING @realm, @userName;
+    ELSE
+        EXECUTE stmtTransactionFilter;
+    END IF;
+
+END ;;
+DELIMITER ;
+/*!50003 SET sql_mode              = @saved_sql_mode */ ;
+/*!50003 SET character_set_client  = @saved_cs_client */ ;
+/*!50003 SET character_set_results = @saved_cs_results */ ;
+/*!50003 SET collation_connection  = @saved_col_connection */ ;
+/*!50003 SET @saved_sql_mode       = @@sql_mode */ ;
+/*!50003 SET sql_mode              = 'STRICT_TRANS_TABLES,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION' */ ;
+/*!50003 DROP PROCEDURE IF EXISTS `calcComplementUnion` */;
+/*!50003 SET @saved_cs_client      = @@character_set_client */ ;
+/*!50003 SET @saved_cs_results     = @@character_set_results */ ;
+/*!50003 SET @saved_col_connection = @@collation_connection */ ;
+/*!50003 SET character_set_client  = utf8 */ ;
+/*!50003 SET character_set_results = utf8 */ ;
+/*!50003 SET collation_connection  = utf8_general_ci */ ;
+DELIMITER ;;
+CREATE DEFINER=`root`@`%` PROCEDURE `calcComplementUnion`(in targetSet VARCHAR(255), in subResultSet VARCHAR(255), in universe VARCHAR(255), in versioned BOOLEAN)
+BEGIN
+    IF versioned AND universe = "entities" THEN
+        SET @stmtComplementUnionStr = CONCAT(
+            'INSERT IGNORE INTO `', targetSet,
+            '` SELECT e.id, _get_head_iversion(e.id) FROM entities as e WHERE NOT EXISTS ( SELECT 1 FROM `',
+            subResultSet,
+            '` AS diff WHERE diff.id=e.id AND diff._iversion = _get_head_iversion(e.id)) UNION ALL SELECT e.id, e._iversion FROM archive_entities AS e WHERE NOT EXISTS ( SELECT 1 FROM `',
+            subResultSet,
+            '` as diff WHERE e.id = diff.id AND e._iversion = diff._iversion)');
+    ELSEIF versioned THEN
+        SET @stmtComplementUnionStr = CONCAT(
+            'INSERT IGNORE INTO `', targetSet,
+            '` SELECT id FROM `',universe,
+            '` AS universe WHERE NOT EXISTS ( SELECT 1 FROM `',
+                subResultSet,'`
+                AS diff WHERE diff.id=universe.id AND diff._iversion = universe.id_version)');
+    ELSE
+        SET @stmtComplementUnionStr = CONCAT('INSERT IGNORE INTO `', targetSet, '` SELECT id FROM `',universe, '` AS universe WHERE NOT EXISTS ( SELECT 1 FROM `', subResultSet,'` AS diff WHERE diff.id=universe.id)');
+    END IF;
+    PREPARE stmtComplementUnion FROM @stmtComplementUnionStr;
+    EXECUTE stmtComplementUnion;
+    DEALLOCATE PREPARE stmtComplementUnion;
+
+END ;;
+DELIMITER ;
+/*!50003 SET sql_mode              = @saved_sql_mode */ ;
+/*!50003 SET character_set_client  = @saved_cs_client */ ;
+/*!50003 SET character_set_results = @saved_cs_results */ ;
+/*!50003 SET collation_connection  = @saved_col_connection */ ;
+/*!50003 SET @saved_sql_mode       = @@sql_mode */ ;
+/*!50003 SET sql_mode              = 'STRICT_TRANS_TABLES,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION' */ ;
+/*!50003 DROP PROCEDURE IF EXISTS `calcDifference` */;
+/*!50003 SET @saved_cs_client      = @@character_set_client */ ;
+/*!50003 SET @saved_cs_results     = @@character_set_results */ ;
+/*!50003 SET @saved_col_connection = @@collation_connection */ ;
+/*!50003 SET character_set_client  = utf8 */ ;
+/*!50003 SET character_set_results = utf8 */ ;
+/*!50003 SET collation_connection  = utf8_general_ci */ ;
+DELIMITER ;;
+CREATE DEFINER=`root`@`%` PROCEDURE `calcDifference`(in resultSetTable VARCHAR(255), in diff VARCHAR(255), in versioned BOOLEAN)
+BEGIN
+    IF versioned THEN
+        SET @diffStmtStr = CONCAT('DELETE FROM `', resultSetTable, '` WHERE EXISTS ( SELECT 1 FROM `', diff,'` AS diff WHERE diff.id=`',resultSetTable,'`.`id` AND diff._iversion=`', resultSetTable, '`.`_iversion`)');
+    ELSE
+        SET @diffStmtStr = CONCAT('DELETE FROM `', resultSetTable, '` WHERE EXISTS ( SELECT 1 FROM `', diff,'` AS diff WHERE diff.id=`',resultSetTable,'`.`id`)');
+    END IF;
+    PREPARE diffStmt FROM @diffStmtStr;
+    EXECUTE diffStmt;
+    DEALLOCATE PREPARE diffStmt;
+
+END ;;
+DELIMITER ;
+/*!50003 SET sql_mode              = @saved_sql_mode */ ;
+/*!50003 SET character_set_client  = @saved_cs_client */ ;
+/*!50003 SET character_set_results = @saved_cs_results */ ;
+/*!50003 SET collation_connection  = @saved_col_connection */ ;
+/*!50003 SET @saved_sql_mode       = @@sql_mode */ ;
+/*!50003 SET sql_mode              = 'STRICT_TRANS_TABLES,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION' */ ;
+/*!50003 DROP PROCEDURE IF EXISTS `calcIntersection` */;
+/*!50003 SET @saved_cs_client      = @@character_set_client */ ;
+/*!50003 SET @saved_cs_results     = @@character_set_results */ ;
+/*!50003 SET @saved_col_connection = @@collation_connection */ ;
+/*!50003 SET character_set_client  = utf8 */ ;
+/*!50003 SET character_set_results = utf8 */ ;
+/*!50003 SET collation_connection  = utf8_general_ci */ ;
+DELIMITER ;;
+CREATE DEFINER=`root`@`%` PROCEDURE `calcIntersection`(in resultSetTable VARCHAR(255), in intersectWith VARCHAR(255), in versioned BOOLEAN)
+BEGIN
+    IF versioned THEN
+        SET @diffStmtStr = CONCAT('DELETE FROM `',
+            resultSetTable,
+            '` WHERE NOT EXISTS ( SELECT 1 FROM `',
+            intersectWith,
+            '` AS diff WHERE diff.id=`',
+            resultSetTable,
+            '`.`id` AND diff._iversion=`',
+            resultSetTable,
+            '`.`_iversion`)');
+    ELSE
+        SET @diffStmtStr = CONCAT('DELETE FROM `', resultSetTable, '` WHERE NOT EXISTS ( SELECT 1 FROM `', intersectWith,'` AS diff WHERE diff.id=`',resultSetTable,'`.`id`)');
+    END IF;
+    PREPARE diffStmt FROM @diffStmtStr;
+    EXECUTE diffStmt;
+
+    
+END ;;
+DELIMITER ;
+/*!50003 SET sql_mode              = @saved_sql_mode */ ;
+/*!50003 SET character_set_client  = @saved_cs_client */ ;
+/*!50003 SET character_set_results = @saved_cs_results */ ;
+/*!50003 SET collation_connection  = @saved_col_connection */ ;
+/*!50003 SET @saved_sql_mode       = @@sql_mode */ ;
+/*!50003 SET sql_mode              = 'STRICT_TRANS_TABLES,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION' */ ;
+/*!50003 DROP PROCEDURE IF EXISTS `calcUnion` */;
+/*!50003 SET @saved_cs_client      = @@character_set_client */ ;
+/*!50003 SET @saved_cs_results     = @@character_set_results */ ;
+/*!50003 SET @saved_col_connection = @@collation_connection */ ;
+/*!50003 SET character_set_client  = utf8 */ ;
+/*!50003 SET character_set_results = utf8 */ ;
+/*!50003 SET collation_connection  = utf8_general_ci */ ;
+DELIMITER ;;
+CREATE DEFINER=`root`@`%` PROCEDURE `calcUnion`(in targetSet VARCHAR(255), in sourceSet VARCHAR(255))
+BEGIN
+    SET @diffStmtStr = CONCAT('INSERT IGNORE INTO `', targetSet, '` SELECT * FROM `',sourceSet,'`');
+    PREPARE diffStmt FROM @diffStmtStr;
+    EXECUTE diffStmt;
+    DEALLOCATE PREPARE diffStmt;
+
+END ;;
+DELIMITER ;
+/*!50003 SET sql_mode              = @saved_sql_mode */ ;
+/*!50003 SET character_set_client  = @saved_cs_client */ ;
+/*!50003 SET character_set_results = @saved_cs_results */ ;
+/*!50003 SET collation_connection  = @saved_col_connection */ ;
+/*!50003 SET @saved_sql_mode       = @@sql_mode */ ;
+/*!50003 SET sql_mode              = 'STRICT_TRANS_TABLES,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION' */ ;
+/*!50003 DROP PROCEDURE IF EXISTS `cleanUpLinCon` */;
+/*!50003 SET @saved_cs_client      = @@character_set_client */ ;
+/*!50003 SET @saved_cs_results     = @@character_set_results */ ;
+/*!50003 SET @saved_col_connection = @@collation_connection */ ;
+/*!50003 SET character_set_client  = utf8 */ ;
+/*!50003 SET character_set_results = utf8 */ ;
+/*!50003 SET collation_connection  = utf8_general_ci */ ;
+DELIMITER ;;
+CREATE DEFINER=`root`@`%` PROCEDURE `cleanUpLinCon`()
+BEGIN
+
+    DELETE FROM units_lin_con WHERE NOT EXISTS (SELECT '1' FROM double_data WHERE unit_sig=signature_from) AND NOT EXISTS (SELECT '1' FROM integer_data WHERE unit_sig=signature_from);
+
+END ;;
+DELIMITER ;
+/*!50003 SET sql_mode              = @saved_sql_mode */ ;
+/*!50003 SET character_set_client  = @saved_cs_client */ ;
+/*!50003 SET character_set_results = @saved_cs_results */ ;
+/*!50003 SET collation_connection  = @saved_col_connection */ ;
+/*!50003 SET @saved_sql_mode       = @@sql_mode */ ;
+/*!50003 SET sql_mode              = 'STRICT_TRANS_TABLES,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION' */ ;
+/*!50003 DROP PROCEDURE IF EXISTS `cleanUpQuery` */;
+/*!50003 SET @saved_cs_client      = @@character_set_client */ ;
+/*!50003 SET @saved_cs_results     = @@character_set_results */ ;
+/*!50003 SET @saved_col_connection = @@collation_connection */ ;
+/*!50003 SET character_set_client  = utf8 */ ;
+/*!50003 SET character_set_results = utf8 */ ;
+/*!50003 SET collation_connection  = utf8_general_ci */ ;
+DELIMITER ;;
+CREATE DEFINER=`root`@`%` PROCEDURE `cleanUpQuery`()
+BEGIN
+    CREATE TEMPORARY TABLE IF NOT EXISTS warnings (warning TEXT NOT NULL);
+    SELECT * from warnings;
+
+    SET @pstmtstr = CONCAT('DROP TEMPORARY TABLE IF EXISTS `warnings`',
+        IF(@tempTableList IS NULL, '', CONCAT(',',@tempTableList)));
+    PREPARE pstmt FROM @pstmtstr;
+    EXECUTE pstmt;
+
+    SET @tempTableList = NULL;
+END ;;
+DELIMITER ;
+/*!50003 SET sql_mode              = @saved_sql_mode */ ;
+/*!50003 SET character_set_client  = @saved_cs_client */ ;
+/*!50003 SET character_set_results = @saved_cs_results */ ;
+/*!50003 SET collation_connection  = @saved_col_connection */ ;
+/*!50003 SET @saved_sql_mode       = @@sql_mode */ ;
+/*!50003 SET sql_mode              = 'STRICT_TRANS_TABLES,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION' */ ;
+/*!50003 DROP PROCEDURE IF EXISTS `copyTable` */;
+/*!50003 SET @saved_cs_client      = @@character_set_client */ ;
+/*!50003 SET @saved_cs_results     = @@character_set_results */ ;
+/*!50003 SET @saved_col_connection = @@collation_connection */ ;
+/*!50003 SET character_set_client  = utf8 */ ;
+/*!50003 SET character_set_results = utf8 */ ;
+/*!50003 SET collation_connection  = utf8_general_ci */ ;
+DELIMITER ;;
+CREATE DEFINER=`root`@`%` PROCEDURE `copyTable`(in fromTable VARCHAR(255), in toTable VARCHAR(255))
+BEGIN
+    SET @copyTableStmtStr = CONCAT('INSERT IGNORE INTO `', toTable, '` (id) SELECT id FROM `', fromTable, '`');
+    PREPARE copyTableStmt FROM @copyTableStmtStr;
+    EXECUTE copyTableStmt;
+    DEALLOCATE PREPARE copyTableStmt;
+    
+END ;;
+DELIMITER ;
+/*!50003 SET sql_mode              = @saved_sql_mode */ ;
+/*!50003 SET character_set_client  = @saved_cs_client */ ;
+/*!50003 SET character_set_results = @saved_cs_results */ ;
+/*!50003 SET collation_connection  = @saved_col_connection */ ;
+/*!50003 SET @saved_sql_mode       = @@sql_mode */ ;
+/*!50003 SET sql_mode              = 'STRICT_TRANS_TABLES,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION' */ ;
+/*!50003 DROP PROCEDURE IF EXISTS `createTmpTable` */;
+/*!50003 SET @saved_cs_client      = @@character_set_client */ ;
+/*!50003 SET @saved_cs_results     = @@character_set_results */ ;
+/*!50003 SET @saved_col_connection = @@collation_connection */ ;
+/*!50003 SET character_set_client  = utf8 */ ;
+/*!50003 SET character_set_results = utf8 */ ;
+/*!50003 SET collation_connection  = utf8_general_ci */ ;
+DELIMITER ;;
+CREATE DEFINER=`root`@`%` PROCEDURE `createTmpTable`(out newTableName VARCHAR(255), in versioned BOOLEAN)
+BEGIN
+    call registerTempTableName(newTableName);
+
+    IF versioned THEN
+        SET @createTableStmtStr = CONCAT('CREATE TEMPORARY TABLE `', newTableName,
+            '` ( id INT UNSIGNED, _iversion INT UNSIGNED, PRIMARY KEY (id, _iversion))' );
+    ELSE
+        SET @createTableStmtStr = CONCAT('CREATE TEMPORARY TABLE `', newTableName,'` ( id INT UNSIGNED PRIMARY KEY)' );
+    END IF;
+
+    PREPARE createTableStmt FROM @createTableStmtStr; 
+    EXECUTE createTableStmt;
+    DEALLOCATE PREPARE createTableStmt;
+END ;;
+DELIMITER ;
+/*!50003 SET sql_mode              = @saved_sql_mode */ ;
+/*!50003 SET character_set_client  = @saved_cs_client */ ;
+/*!50003 SET character_set_results = @saved_cs_results */ ;
+/*!50003 SET collation_connection  = @saved_col_connection */ ;
+/*!50003 SET @saved_sql_mode       = @@sql_mode */ ;
+/*!50003 SET sql_mode              = 'STRICT_TRANS_TABLES,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION' */ ;
+/*!50003 DROP PROCEDURE IF EXISTS `createTmpTable2` */;
+/*!50003 SET @saved_cs_client      = @@character_set_client */ ;
+/*!50003 SET @saved_cs_results     = @@character_set_results */ ;
+/*!50003 SET @saved_col_connection = @@collation_connection */ ;
+/*!50003 SET character_set_client  = utf8 */ ;
+/*!50003 SET character_set_results = utf8 */ ;
+/*!50003 SET collation_connection  = utf8_general_ci */ ;
+DELIMITER ;;
+CREATE DEFINER=`root`@`%` PROCEDURE `createTmpTable2`(out newTableName VARCHAR(255))
+BEGIN
+    call registerTempTableName(newTableName);
+    SET @createTableStmtStr = CONCAT('CREATE TEMPORARY TABLE `', newTableName,
+        '` ( id INT UNSIGNED, id2 INT UNSIGNED, domain INT UNSIGNED, CONSTRAINT `',
+        newTableName,'PK` PRIMARY KEY (id,id2,domain) )' );
+
+    PREPARE createTableStmt FROM @createTableStmtStr; 
+    EXECUTE createTableStmt;
+    DEALLOCATE PREPARE createTableStmt;
+END ;;
+DELIMITER ;
+/*!50003 SET sql_mode              = @saved_sql_mode */ ;
+/*!50003 SET character_set_client  = @saved_cs_client */ ;
+/*!50003 SET character_set_results = @saved_cs_results */ ;
+/*!50003 SET collation_connection  = @saved_col_connection */ ;
+/*!50003 SET @saved_sql_mode       = @@sql_mode */ ;
+/*!50003 SET sql_mode              = 'STRICT_TRANS_TABLES,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION' */ ;
+/*!50003 DROP PROCEDURE IF EXISTS `deleteEntity` */;
+/*!50003 SET @saved_cs_client      = @@character_set_client */ ;
+/*!50003 SET @saved_cs_results     = @@character_set_results */ ;
+/*!50003 SET @saved_col_connection = @@collation_connection */ ;
+/*!50003 SET character_set_client  = utf8 */ ;
+/*!50003 SET character_set_results = utf8 */ ;
+/*!50003 SET collation_connection  = utf8_general_ci */ ;
+DELIMITER ;;
+CREATE DEFINER=`root`@`%` PROCEDURE `deleteEntity`(in EntityID VARCHAR(255))
+BEGIN
+    DECLARE InternalEntityID INT UNSIGNED DEFAULT NULL;
+
+    SELECT internal_id INTO InternalEntityID from entity_ids WHERE id = EntityID;
+
+    
+    DELETE FROM files where file_id=InternalEntityID;
+
+    
+    DELETE FROM data_type
+        WHERE ( domain_id = 0
+            AND entity_id = 0
+            AND property_id = InternalEntityID )
+        OR datatype = InternalEntityID;
+    DELETE FROM collection_type
+        WHERE domain_id = 0
+        AND entity_id = 0
+        AND property_id = InternalEntityID;
+
+    
+    DELETE FROM name_data
+        WHERE domain_id = 0
+        AND entity_id = InternalEntityID
+        AND property_id = 20;
+
+    DELETE FROM entity_ids
+        WHERE internal_id = InternalEntityID;
+
+    DELETE FROM entities where id=InternalEntityID;
+
+    
+    DELETE FROM entity_acl
+        WHERE NOT EXISTS (
+            SELECT 1 FROM entities
+            WHERE entities.acl = entity_acl.id LIMIT 1)
+        AND NOT EXISTS (
+            SELECT 1 FROM archive_entities
+            WHERE archive_entities.acl = entity_acl.id LIMIT 1);
+
+END ;;
+DELIMITER ;
+/*!50003 SET sql_mode              = @saved_sql_mode */ ;
+/*!50003 SET character_set_client  = @saved_cs_client */ ;
+/*!50003 SET character_set_results = @saved_cs_results */ ;
+/*!50003 SET collation_connection  = @saved_col_connection */ ;
+/*!50003 SET @saved_sql_mode       = @@sql_mode */ ;
+/*!50003 SET sql_mode              = 'STRICT_TRANS_TABLES,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION' */ ;
+/*!50003 DROP PROCEDURE IF EXISTS `deleteEntityProperties` */;
+/*!50003 SET @saved_cs_client      = @@character_set_client */ ;
+/*!50003 SET @saved_cs_results     = @@character_set_results */ ;
+/*!50003 SET @saved_col_connection = @@collation_connection */ ;
+/*!50003 SET character_set_client  = utf8 */ ;
+/*!50003 SET character_set_results = utf8 */ ;
+/*!50003 SET collation_connection  = utf8_general_ci */ ;
+DELIMITER ;;
+CREATE DEFINER=`root`@`%` PROCEDURE `deleteEntityProperties`(in EntityID VARCHAR(255))
+BEGIN
+    DECLARE IVersion INT UNSIGNED DEFAULT NULL;
+    DECLARE InternalEntityID INT UNSIGNED DEFAULT NULL;
+
+    SELECT internal_id INTO InternalEntityID from entity_ids WHERE id = EntityID;
+
+    CALL deleteIsa(InternalEntityID);
+
+    IF is_feature_config("ENTITY_VERSIONING", "ENABLED") THEN
+        SELECT max(e._iversion) INTO IVersion 
+            FROM entity_version AS e
+            WHERE e.entity_id = InternalEntityID;
+
+        
+        INSERT INTO archive_reference_data (domain_id, entity_id,
+                property_id, value, value_iversion, status, pidx, _iversion)
+            SELECT domain_id, entity_id, property_id, value, value_iversion,
+                status, pidx, IVersion AS _iversion
+            FROM reference_data
+            WHERE (domain_id = 0 AND entity_id = InternalEntityID)
+            OR domain_id = InternalEntityID;
+
+        INSERT INTO archive_null_data (domain_id, entity_id,
+                property_id, status, pidx, _iversion)
+            SELECT domain_id, entity_id, property_id, status,
+                pidx, IVersion AS _iversion
+            FROM null_data
+            WHERE (domain_id = 0 AND entity_id = InternalEntityID)
+            OR domain_id = InternalEntityID;
+
+        INSERT INTO archive_text_data (domain_id, entity_id,
+                property_id, value, status, pidx, _iversion)
+            SELECT domain_id, entity_id, property_id, value, status,
+                pidx, IVersion AS _iversion
+            FROM text_data
+            WHERE (domain_id = 0 AND entity_id = InternalEntityID)
+            OR domain_id = InternalEntityID;
+
+        INSERT INTO archive_name_data (domain_id, entity_id,
+                property_id, value, status, pidx, _iversion)
+            SELECT domain_id, entity_id, property_id, value, status,
+                pidx, IVersion AS _iversion
+            FROM name_data
+            WHERE (domain_id = 0 AND entity_id = InternalEntityID)
+            OR domain_id = InternalEntityID;
+
+        INSERT INTO archive_enum_data (domain_id, entity_id,
+                property_id, value, status, pidx, _iversion)
+            SELECT domain_id, entity_id, property_id, value, status,
+                pidx, IVersion AS _iversion
+            FROM enum_data
+            WHERE (domain_id = 0 AND entity_id = InternalEntityID)
+            OR domain_id = InternalEntityID;
+
+        INSERT INTO archive_integer_data (domain_id, entity_id,
+                property_id, value, status, pidx, _iversion, unit_sig)
+            SELECT domain_id, entity_id, property_id, value, status,
+                pidx, IVersion AS _iversion, unit_sig
+            FROM integer_data
+            WHERE (domain_id = 0 AND entity_id = InternalEntityID)
+            OR domain_id = InternalEntityID;
+
+        INSERT INTO archive_double_data (domain_id, entity_id,
+                property_id, value, status, pidx, _iversion, unit_sig)
+            SELECT domain_id, entity_id, property_id, value, status,
+                pidx, IVersion AS _iversion, unit_sig
+            FROM double_data
+            WHERE (domain_id = 0 AND entity_id = InternalEntityID)
+            OR domain_id = InternalEntityID;
+
+        INSERT INTO archive_datetime_data (domain_id, entity_id,
+                property_id, value, value_ns, status, pidx, _iversion)
+            SELECT domain_id, entity_id, property_id, value, value_ns,
+                status, pidx, IVersion AS _iversion
+            FROM datetime_data
+            WHERE (domain_id = 0 AND entity_id = InternalEntityID)
+            OR domain_id = InternalEntityID;
+
+        INSERT INTO archive_date_data (domain_id, entity_id,
+                property_id, value, status, pidx, _iversion)
+            SELECT domain_id, entity_id, property_id, value, status,
+                pidx, IVersion AS _iversion
+            FROM date_data
+            WHERE (domain_id = 0 AND entity_id = InternalEntityID)
+            OR domain_id = InternalEntityID;
+
+        INSERT INTO archive_name_overrides (domain_id, entity_id,
+                property_id, name, _iversion)
+            SELECT domain_id, entity_id, property_id, name,
+                IVersion AS _iversion
+            FROM name_overrides
+            WHERE (domain_id = 0 AND entity_id = InternalEntityID)
+            OR domain_id = InternalEntityID;
+
+        INSERT INTO archive_desc_overrides (domain_id, entity_id,
+                property_id, description, _iversion)
+            SELECT domain_id, entity_id, property_id, description,
+                IVersion AS _iversion
+            FROM desc_overrides
+            WHERE (domain_id = 0 AND entity_id = InternalEntityID)
+            OR domain_id = InternalEntityID;
+
+        INSERT INTO archive_data_type (domain_id, entity_id,
+                property_id, datatype, _iversion)
+            SELECT domain_id, entity_id, property_id, datatype,
+                IVersion AS _iversion
+            FROM data_type
+            WHERE (domain_id = 0 AND entity_id = InternalEntityID)
+            OR domain_id = InternalEntityID;
+
+        INSERT INTO archive_collection_type (domain_id, entity_id,
+                property_id, collection, _iversion)
+            SELECT domain_id, entity_id, property_id, collection,
+                IVersion AS _iversion
+            FROM collection_type
+            WHERE (domain_id = 0 AND entity_id = InternalEntityID)
+            OR domain_id = InternalEntityID;
+
+        INSERT INTO archive_query_template_def (id, definition, _iversion)
+            SELECT id, definition, IVersion AS _iversion
+            FROM query_template_def
+            WHERE id = InternalEntityID;
+
+    END IF;
+
+    DELETE FROM reference_data
+    where (domain_id=0 AND entity_id=InternalEntityID) OR domain_id=InternalEntityID;
+    DELETE FROM null_data
+    where (domain_id=0 AND entity_id=InternalEntityID) OR domain_id=InternalEntityID;
+    DELETE FROM text_data
+    where (domain_id=0 AND entity_id=InternalEntityID) OR domain_id=InternalEntityID;
+    DELETE FROM name_data
+    where (domain_id=0 AND entity_id=InternalEntityID) OR domain_id=InternalEntityID;
+    DELETE FROM enum_data
+    where (domain_id=0 AND entity_id=InternalEntityID) OR domain_id=InternalEntityID;
+    DELETE FROM integer_data
+    where (domain_id=0 AND entity_id=InternalEntityID) OR domain_id=InternalEntityID;
+    DELETE FROM double_data
+    where (domain_id=0 AND entity_id=InternalEntityID) OR domain_id=InternalEntityID;
+    DELETE FROM datetime_data
+    where (domain_id=0 AND entity_id=InternalEntityID) OR domain_id=InternalEntityID;
+    DELETE FROM date_data
+    where (domain_id=0 AND entity_id=InternalEntityID) OR domain_id=InternalEntityID;
+
+    DELETE FROM name_overrides
+    WHERE (domain_id=0 AND entity_id=InternalEntityID) OR domain_id=InternalEntityID;
+    DELETE FROM desc_overrides
+    WHERE (domain_id=0 AND entity_id=InternalEntityID) OR domain_id=InternalEntityID;
+
+    DELETE FROM data_type
+    WHERE (domain_id=0 AND entity_id=InternalEntityID) OR domain_id=InternalEntityID;
+    DELETE FROM collection_type
+    WHERE (domain_id=0 AND entity_id=InternalEntityID) OR domain_id=InternalEntityID;
+
+    DELETE FROM query_template_def WHERE id=InternalEntityID;
+
+END ;;
+DELIMITER ;
+/*!50003 SET sql_mode              = @saved_sql_mode */ ;
+/*!50003 SET character_set_client  = @saved_cs_client */ ;
+/*!50003 SET character_set_results = @saved_cs_results */ ;
+/*!50003 SET collation_connection  = @saved_col_connection */ ;
+/*!50003 SET @saved_sql_mode       = @@sql_mode */ ;
+/*!50003 SET sql_mode              = 'STRICT_TRANS_TABLES,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION' */ ;
+/*!50003 DROP PROCEDURE IF EXISTS `deleteIsa` */;
+/*!50003 SET @saved_cs_client      = @@character_set_client */ ;
+/*!50003 SET @saved_cs_results     = @@character_set_results */ ;
+/*!50003 SET @saved_col_connection = @@collation_connection */ ;
+/*!50003 SET character_set_client  = utf8 */ ;
+/*!50003 SET character_set_results = utf8 */ ;
+/*!50003 SET collation_connection  = utf8_general_ci */ ;
+DELIMITER ;;
+CREATE DEFINER=`root`@`%` PROCEDURE `deleteIsa`(IN InternalEntityID INT UNSIGNED)
+BEGIN
+    DECLARE IVersion INT UNSIGNED DEFAULT NULL;
+
+    IF is_feature_config("ENTITY_VERSIONING", "ENABLED") THEN
+        SELECT max(_iversion) INTO IVersion
+            FROM entity_version
+            WHERE entity_id = InternalEntityID;
+
+        
+        INSERT IGNORE INTO archive_isa (child, child_iversion, parent, direct)
+            SELECT e.child, IVersion AS child_iversion, e.parent, rpath = InternalEntityID
+            FROM isa_cache AS e
+            WHERE e.child = InternalEntityID;
+    END IF;
+
+    DELETE FROM isa_cache
+        WHERE child = InternalEntityID
+        OR rpath = InternalEntityID
+        OR rpath LIKE concat('%>', InternalEntityID)
+        OR rpath LIKE concat('%>', InternalEntityID, '>%');
+
+END ;;
+DELIMITER ;
+/*!50003 SET sql_mode              = @saved_sql_mode */ ;
+/*!50003 SET character_set_client  = @saved_cs_client */ ;
+/*!50003 SET character_set_results = @saved_cs_results */ ;
+/*!50003 SET collation_connection  = @saved_col_connection */ ;
+/*!50003 SET @saved_sql_mode       = @@sql_mode */ ;
+/*!50003 SET sql_mode              = 'STRICT_TRANS_TABLES,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION' */ ;
+/*!50003 DROP PROCEDURE IF EXISTS `deleteLinCon` */;
+/*!50003 SET @saved_cs_client      = @@character_set_client */ ;
+/*!50003 SET @saved_cs_results     = @@character_set_results */ ;
+/*!50003 SET @saved_col_connection = @@collation_connection */ ;
+/*!50003 SET character_set_client  = utf8 */ ;
+/*!50003 SET character_set_results = utf8 */ ;
+/*!50003 SET collation_connection  = utf8_general_ci */ ;
+DELIMITER ;;
+CREATE DEFINER=`root`@`%` PROCEDURE `deleteLinCon`(in sig BIGINT)
+BEGIN
+
+    DELETE FROM units_lin_con WHERE signature_from=sig;
+
+END ;;
+DELIMITER ;
+/*!50003 SET sql_mode              = @saved_sql_mode */ ;
+/*!50003 SET character_set_client  = @saved_cs_client */ ;
+/*!50003 SET character_set_results = @saved_cs_results */ ;
+/*!50003 SET collation_connection  = @saved_col_connection */ ;
+/*!50003 SET @saved_sql_mode       = @@sql_mode */ ;
+/*!50003 SET sql_mode              = 'STRICT_TRANS_TABLES,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION' */ ;
+/*!50003 DROP PROCEDURE IF EXISTS `entityACL` */;
+/*!50003 SET @saved_cs_client      = @@character_set_client */ ;
+/*!50003 SET @saved_cs_results     = @@character_set_results */ ;
+/*!50003 SET @saved_col_connection = @@collation_connection */ ;
+/*!50003 SET character_set_client  = utf8 */ ;
+/*!50003 SET character_set_results = utf8 */ ;
+/*!50003 SET collation_connection  = utf8_general_ci */ ;
+DELIMITER ;;
+CREATE DEFINER=`root`@`%` PROCEDURE `entityACL`(out ACLID INT UNSIGNED, in ACLSTR VARBINARY(65525))
+BEGIN
+   SELECT id INTO ACLID FROM entity_acl as t WHERE t.acl=ACLSTR LIMIT 1;
+   IF ACLID IS NULL THEN
+        INSERT INTO entity_acl (acl) VALUES (ACLSTR);
+        SET ACLID = LAST_INSERT_ID();
+   END IF;
+END ;;
+DELIMITER ;
+/*!50003 SET sql_mode              = @saved_sql_mode */ ;
+/*!50003 SET character_set_client  = @saved_cs_client */ ;
+/*!50003 SET character_set_results = @saved_cs_results */ ;
+/*!50003 SET collation_connection  = @saved_col_connection */ ;
+/*!50003 SET @saved_sql_mode       = @@sql_mode */ ;
+/*!50003 SET sql_mode              = 'STRICT_TRANS_TABLES,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION' */ ;
+/*!50003 DROP PROCEDURE IF EXISTS `finishNegationFilter` */;
+/*!50003 SET @saved_cs_client      = @@character_set_client */ ;
+/*!50003 SET @saved_cs_results     = @@character_set_results */ ;
+/*!50003 SET @saved_col_connection = @@collation_connection */ ;
+/*!50003 SET character_set_client  = utf8 */ ;
+/*!50003 SET character_set_results = utf8 */ ;
+/*!50003 SET collation_connection  = utf8_general_ci */ ;
+DELIMITER ;;
+CREATE DEFINER=`root`@`%` PROCEDURE `finishNegationFilter`(in resultSetTable VARCHAR(255), in diff VARCHAR(255))
+BEGIN
+    call calcDifference(resultSetTable, diff);
+END ;;
+DELIMITER ;
+/*!50003 SET sql_mode              = @saved_sql_mode */ ;
+/*!50003 SET character_set_client  = @saved_cs_client */ ;
+/*!50003 SET character_set_results = @saved_cs_results */ ;
+/*!50003 SET collation_connection  = @saved_col_connection */ ;
+/*!50003 SET @saved_sql_mode       = @@sql_mode */ ;
+/*!50003 SET sql_mode              = 'STRICT_TRANS_TABLES,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION' */ ;
+/*!50003 DROP PROCEDURE IF EXISTS `finishSubProperty` */;
+/*!50003 SET @saved_cs_client      = @@character_set_client */ ;
+/*!50003 SET @saved_cs_results     = @@character_set_results */ ;
+/*!50003 SET @saved_col_connection = @@collation_connection */ ;
+/*!50003 SET character_set_client  = utf8 */ ;
+/*!50003 SET character_set_results = utf8 */ ;
+/*!50003 SET collation_connection  = utf8_general_ci */ ;
+DELIMITER ;;
+CREATE DEFINER=`root`@`%` PROCEDURE `finishSubProperty`(in sourceSet VARCHAR(255),in targetSet VARCHAR(255),
+                                          in list VARCHAR(255), in versioned BOOLEAN)
+BEGIN
+    DECLARE data VARCHAR(20000) DEFAULT CONCAT('`',list,'`');
+    SET @finishSubPropertyStmtStr = makeStmt(sourceSet, targetSet, data, NULL, versioned);
+
+    PREPARE finishSubPropertyStmt FROM @finishSubPropertyStmtStr;
+    EXECUTE finishSubPropertyStmt;
+    DEALLOCATE PREPARE finishSubPropertyStmt;
+
+    SELECT @finishSubPropertyStmtStr AS finishSubPropertyStmt;
+
+END ;;
+DELIMITER ;
+/*!50003 SET sql_mode              = @saved_sql_mode */ ;
+/*!50003 SET character_set_client  = @saved_cs_client */ ;
+/*!50003 SET character_set_results = @saved_cs_results */ ;
+/*!50003 SET collation_connection  = @saved_col_connection */ ;
+/*!50003 SET @saved_sql_mode       = @@sql_mode */ ;
+/*!50003 SET sql_mode              = 'STRICT_TRANS_TABLES,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION' */ ;
+/*!50003 DROP PROCEDURE IF EXISTS `getChildren` */;
+/*!50003 SET @saved_cs_client      = @@character_set_client */ ;
+/*!50003 SET @saved_cs_results     = @@character_set_results */ ;
+/*!50003 SET @saved_col_connection = @@collation_connection */ ;
+/*!50003 SET character_set_client  = utf8 */ ;
+/*!50003 SET character_set_results = utf8 */ ;
+/*!50003 SET collation_connection  = utf8_general_ci */ ;
+DELIMITER ;;
+CREATE DEFINER=`root`@`%` PROCEDURE `getChildren`(in tableName varchar(255), in versioned BOOLEAN)
+BEGIN
+    DECLARE found_children INT UNSIGNED DEFAULT 0;
+
+    DROP TEMPORARY TABLE IF EXISTS dependTemp;
+    CREATE TEMPORARY TABLE dependTemp (id INT UNSIGNED, _iversion INT UNSIGNED, PRIMARY KEY(id, _iversion));
+
+
+    SET @initDepend = CONCAT(
+        'INSERT IGNORE INTO dependTemp (id, _iversion) SELECT i.child, ',
+        IF(versioned,
+            '_get_head_iversion(i.child)',
+            '0'),
+        ' FROM isa_cache AS i INNER JOIN `',
+        tableName,
+        '` AS t ON (i.parent=t.id);');
+    PREPARE initDependStmt FROM @initDepend;
+
+    EXECUTE initDependStmt;
+    SET found_children = found_children + ROW_COUNT();
+
+    
+
+    IF versioned IS TRUE THEN
+        SET @initDepend = CONCAT(
+            'INSERT IGNORE INTO dependTemp (id, _iversion) ',
+            'SELECT i.child, i.child_iversion FROM archive_isa AS i INNER JOIN `',
+            tableName,
+            '` AS t ON (i.parent=t.id);');
+        PREPARE initDependStmt FROM @initDepend;
+
+        EXECUTE initDependStmt;
+        SET found_children = found_children + ROW_COUNT();
+    END IF;
+
+    
+
+
+    IF found_children != 0 THEN
+        SET @transfer = CONCAT(
+            'INSERT IGNORE INTO `',
+            tableName,
+            IF(versioned,
+                '` (id, _iversion) SELECT id, _iversion FROM dependTemp',
+                '` (id) SELECT id FROM dependTemp'));
+        PREPARE transferstmt FROM @transfer;
+        EXECUTE transferstmt;
+        DEALLOCATE PREPARE transferstmt;
+    END IF;
+
+
+    DEALLOCATE PREPARE initDependStmt;
+
+END ;;
+DELIMITER ;
+/*!50003 SET sql_mode              = @saved_sql_mode */ ;
+/*!50003 SET character_set_client  = @saved_cs_client */ ;
+/*!50003 SET character_set_results = @saved_cs_results */ ;
+/*!50003 SET collation_connection  = @saved_col_connection */ ;
+/*!50003 SET @saved_sql_mode       = @@sql_mode */ ;
+/*!50003 SET sql_mode              = 'STRICT_TRANS_TABLES,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION' */ ;
+/*!50003 DROP PROCEDURE IF EXISTS `getDependentEntities` */;
+/*!50003 SET @saved_cs_client      = @@character_set_client */ ;
+/*!50003 SET @saved_cs_results     = @@character_set_results */ ;
+/*!50003 SET @saved_col_connection = @@collation_connection */ ;
+/*!50003 SET character_set_client  = utf8 */ ;
+/*!50003 SET character_set_results = utf8 */ ;
+/*!50003 SET collation_connection  = utf8_general_ci */ ;
+DELIMITER ;;
+CREATE DEFINER=`root`@`%` PROCEDURE `getDependentEntities`(in EntityID VARCHAR(255))
+BEGIN
+
+    DECLARE InternalEntityID INT UNSIGNED DEFAULT NULL;
+
+    DROP TEMPORARY TABLE IF EXISTS referring;
+    CREATE TEMPORARY TABLE referring (
+        id INT UNSIGNED UNIQUE
+    );
+
+    SELECT internal_id INTO InternalEntityID from entity_ids WHERE id = EntityID;
+
+    INSERT IGNORE INTO referring (id) SELECT entity_id FROM reference_data WHERE (value=InternalEntityID OR property_id=InternalEntityID) AND domain_id=0 AND entity_id!=InternalEntityID;
+    INSERT IGNORE INTO referring (id) SELECT domain_id FROM reference_data WHERE (value=InternalEntityID OR property_id=InternalEntityID) AND domain_id!=InternalEntityID AND entity_id!=InternalEntityID AND domain_id!=0;
+
+    INSERT IGNORE INTO referring (id) SELECT entity_id FROM text_data WHERE property_id=InternalEntityID AND domain_id=0 AND entity_id!=InternalEntityID;
+    INSERT IGNORE INTO referring (id) SELECT domain_id FROM text_data WHERE property_id=InternalEntityID AND domain_id!=InternalEntityID AND entity_id!=InternalEntityID AND domain_id!=0;
+
+    INSERT IGNORE INTO referring (id) SELECT entity_id FROM enum_data WHERE property_id=InternalEntityID AND domain_id=0 AND entity_id!=InternalEntityID;
+    INSERT IGNORE INTO referring (id) SELECT domain_id FROM enum_data WHERE property_id=InternalEntityID AND domain_id!=InternalEntityID AND entity_id!=InternalEntityID AND domain_id!=0;
+
+    INSERT IGNORE INTO referring (id) SELECT entity_id FROM name_data WHERE property_id=InternalEntityID AND domain_id=0 AND entity_id!=InternalEntityID;
+    INSERT IGNORE INTO referring (id) SELECT domain_id FROM name_data WHERE property_id=InternalEntityID AND domain_id!=InternalEntityID AND entity_id!=InternalEntityID AND domain_id!=0;
+
+    INSERT IGNORE INTO referring (id) SELECT entity_id FROM integer_data WHERE property_id=InternalEntityID AND domain_id=0 AND entity_id!=InternalEntityID;
+    INSERT IGNORE INTO referring (id) SELECT domain_id FROM integer_data WHERE property_id=InternalEntityID AND domain_id!=InternalEntityID AND entity_id!=InternalEntityID AND domain_id!=0;
+
+    INSERT IGNORE INTO referring (id) SELECT entity_id FROM double_data WHERE property_id=InternalEntityID AND domain_id=0 AND entity_id!=InternalEntityID;
+    INSERT IGNORE INTO referring (id) SELECT domain_id FROM double_data WHERE property_id=InternalEntityID AND domain_id!=InternalEntityID AND entity_id!=InternalEntityID AND domain_id!=0;
+
+    INSERT IGNORE INTO referring (id) SELECT entity_id FROM datetime_data WHERE property_id=InternalEntityID AND domain_id=0 AND entity_id!=InternalEntityID;
+    INSERT IGNORE INTO referring (id) SELECT domain_id FROM datetime_data WHERE property_id=InternalEntityID AND domain_id!=InternalEntityID AND entity_id!=InternalEntityID AND domain_id!=0;
+
+    INSERT IGNORE INTO referring (id) SELECT entity_id FROM date_data WHERE property_id=InternalEntityID AND domain_id=0 AND entity_id!=InternalEntityID;
+    INSERT IGNORE INTO referring (id) SELECT domain_id FROM date_data WHERE property_id=InternalEntityID AND domain_id!=InternalEntityID AND entity_id!=InternalEntityID AND domain_id!=0;
+
+    INSERT IGNORE INTO referring (id) SELECT entity_id FROM null_data WHERE property_id=InternalEntityID AND domain_id=0 AND entity_id!=InternalEntityID;
+    INSERT IGNORE INTO referring (id) SELECT domain_id FROM null_data WHERE property_id=InternalEntityID AND domain_id!=InternalEntityID AND entity_id!=InternalEntityID AND domain_id!=0;
+
+    INSERT IGNORE INTO referring (id) SELECT entity_id from data_type WHERE datatype=InternalEntityID AND domain_id=0 AND entity_id!=InternalEntityID;
+    INSERT IGNORE INTO referring (id) SELECT domain_id from data_type WHERE datatype=InternalEntityID;
+
+    INSERT IGNORE INTO referring (id) SELECT child FROM isa_cache WHERE parent = InternalEntityID AND rpath = child;
+
+    SELECT e.id FROM referring AS r LEFT JOIN entity_ids AS e ON r.id = e.internal_id WHERE r.id!=0 AND e.internal_id!=InternalEntityID;
+
+    DROP TEMPORARY TABLE referring;
+
+END ;;
+DELIMITER ;
+/*!50003 SET sql_mode              = @saved_sql_mode */ ;
+/*!50003 SET character_set_client  = @saved_cs_client */ ;
+/*!50003 SET character_set_results = @saved_cs_results */ ;
+/*!50003 SET collation_connection  = @saved_col_connection */ ;
+/*!50003 SET @saved_sql_mode       = @@sql_mode */ ;
+/*!50003 SET sql_mode              = 'STRICT_TRANS_TABLES,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION' */ ;
+/*!50003 DROP PROCEDURE IF EXISTS `getFile` */;
+/*!50003 SET @saved_cs_client      = @@character_set_client */ ;
+/*!50003 SET @saved_cs_results     = @@character_set_results */ ;
+/*!50003 SET @saved_col_connection = @@collation_connection */ ;
+/*!50003 SET character_set_client  = utf8mb4 */ ;
+/*!50003 SET character_set_results = utf8mb4 */ ;
+/*!50003 SET collation_connection  = utf8mb4_general_ci */ ;
+DELIMITER ;;
+CREATE DEFINER=`caosdb`@`%` PROCEDURE `getFile`(in FileID INT)
+BEGIN 
+
+Select name, description, role into @name, @description, @role from entities where id=FileID LIMIT 1;
+
+IF @role = 'file' Then
+		Select path, hash, size into @FilePath, @FileHash, @FileSize from files where file_id=FileID LIMIT 1;
+		Select timestamp, user_id, user_agent into @FileCreated, @FileCreator, @FileGenerator from history where entity_id=FileID AND event='insertion' LIMIT 1;
+
+Select 
+FileID as FileID,
+@FilePath as FilePath,
+@FileSize as FileSize,
+@FileHash as FileHash,
+@FileDescription as FileDescription,
+@FileCreated as FileCreated,
+@FileCreator as FileCreator,
+@FileGenerator as FileGenerator,
+NULL	as FileOwner,
+NULL as FilePermission,
+NULL as FileChecksum;
+
+END IF;
+
+END ;;
+DELIMITER ;
+/*!50003 SET sql_mode              = @saved_sql_mode */ ;
+/*!50003 SET character_set_client  = @saved_cs_client */ ;
+/*!50003 SET character_set_results = @saved_cs_results */ ;
+/*!50003 SET collation_connection  = @saved_col_connection */ ;
+/*!50003 SET @saved_sql_mode       = @@sql_mode */ ;
+/*!50003 SET sql_mode              = 'STRICT_TRANS_TABLES,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION' */ ;
+/*!50003 DROP PROCEDURE IF EXISTS `getFileIdByPath` */;
+/*!50003 SET @saved_cs_client      = @@character_set_client */ ;
+/*!50003 SET @saved_cs_results     = @@character_set_results */ ;
+/*!50003 SET @saved_col_connection = @@collation_connection */ ;
+/*!50003 SET character_set_client  = utf8 */ ;
+/*!50003 SET character_set_results = utf8 */ ;
+/*!50003 SET collation_connection  = utf8_general_ci */ ;
+DELIMITER ;;
+CREATE DEFINER=`root`@`%` PROCEDURE `getFileIdByPath`(in FilePath TEXT)
+BEGIN
+
+    SELECT e.id AS FileID FROM files AS f LEFT JOIN entity_ids ON e.internal_in = f.file_id WHERE f.path=FilePath LIMIT 1;
+
+END ;;
+DELIMITER ;
+/*!50003 SET sql_mode              = @saved_sql_mode */ ;
+/*!50003 SET character_set_client  = @saved_cs_client */ ;
+/*!50003 SET character_set_results = @saved_cs_results */ ;
+/*!50003 SET collation_connection  = @saved_col_connection */ ;
+/*!50003 SET @saved_sql_mode       = @@sql_mode */ ;
+/*!50003 SET sql_mode              = 'STRICT_TRANS_TABLES,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION' */ ;
+/*!50003 DROP PROCEDURE IF EXISTS `getIdByName` */;
+/*!50003 SET @saved_cs_client      = @@character_set_client */ ;
+/*!50003 SET @saved_cs_results     = @@character_set_results */ ;
+/*!50003 SET @saved_col_connection = @@collation_connection */ ;
+/*!50003 SET character_set_client  = utf8 */ ;
+/*!50003 SET character_set_results = utf8 */ ;
+/*!50003 SET collation_connection  = utf8_general_ci */ ;
+DELIMITER ;;
+CREATE DEFINER=`root`@`%` PROCEDURE `getIdByName`(in Name VARCHAR(255), in Role VARCHAR(255), in Lmt INT UNSIGNED)
+BEGIN
+
+    SET @stmtStr = "SELECT e.id AS id FROM name_data AS n JOIN entity_ids AS e ON (n.domain_id=0 AND n.property_id=20 AND e.internal_id = n.entity_id) JOIN entities AS i ON (i.id = e.internal_id) WHERE n.value = ?";
+
+    IF Role IS NULL THEN
+        SET @stmtStr = CONCAT(@stmtStr, " AND i.role!='ROLE'");
+    ELSE
+        SET @stmtStr = CONCAT(@stmtStr, " AND i.role='", Role, "'");
+    END IF;
+
+    IF Lmt IS NOT NULL THEN
+        SET @stmtStr = CONCAT(@stmtStr, " LIMIT ", Lmt);
+    END IF;
+
+    SET @vName = Name;
+    PREPARE stmt FROM @stmtStr;
+    EXECUTE stmt USING @vName;
+    DEALLOCATE PREPARE stmt;
+
+END ;;
+DELIMITER ;
+/*!50003 SET sql_mode              = @saved_sql_mode */ ;
+/*!50003 SET character_set_client  = @saved_cs_client */ ;
+/*!50003 SET character_set_results = @saved_cs_results */ ;
+/*!50003 SET collation_connection  = @saved_col_connection */ ;
+/*!50003 SET @saved_sql_mode       = @@sql_mode */ ;
+/*!50003 SET sql_mode              = 'STRICT_TRANS_TABLES,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION' */ ;
+/*!50003 DROP PROCEDURE IF EXISTS `getRules` */;
+/*!50003 SET @saved_cs_client      = @@character_set_client */ ;
+/*!50003 SET @saved_cs_results     = @@character_set_results */ ;
+/*!50003 SET @saved_col_connection = @@collation_connection */ ;
+/*!50003 SET character_set_client  = utf8mb4 */ ;
+/*!50003 SET character_set_results = utf8mb4 */ ;
+/*!50003 SET collation_connection  = utf8mb4_general_ci */ ;
+DELIMITER ;;
+CREATE DEFINER=`caosdb`@`%` PROCEDURE `getRules`(in DomainID INT UNSIGNED, in EntityID INT UNSIGNED, in TransType VARCHAR(255))
+BEGIN
+
+		
+		
+		
+SELECT rules.transaction, rules.criterion, rules.modus from rules where if(DomainID is null, rules.domain_id=0,rules.domain_id=DomainID) AND if(EntityID is null, rules.entity_id=0,rules.entity_id=EntityID) AND if(TransType is null,true=true,rules.transaction=TransType);
+
+
+
+
+END ;;
+DELIMITER ;
+/*!50003 SET sql_mode              = @saved_sql_mode */ ;
+/*!50003 SET character_set_client  = @saved_cs_client */ ;
+/*!50003 SET character_set_results = @saved_cs_results */ ;
+/*!50003 SET collation_connection  = @saved_col_connection */ ;
+/*!50003 SET @saved_sql_mode       = @@sql_mode */ ;
+/*!50003 SET sql_mode              = 'STRICT_TRANS_TABLES,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION' */ ;
+/*!50003 DROP PROCEDURE IF EXISTS `get_version_history` */;
+/*!50003 SET @saved_cs_client      = @@character_set_client */ ;
+/*!50003 SET @saved_cs_results     = @@character_set_results */ ;
+/*!50003 SET @saved_col_connection = @@collation_connection */ ;
+/*!50003 SET character_set_client  = utf8 */ ;
+/*!50003 SET character_set_results = utf8 */ ;
+/*!50003 SET collation_connection  = utf8_general_ci */ ;
+DELIMITER ;;
+CREATE DEFINER=`root`@`%` PROCEDURE `get_version_history`(
+    in EntityID VARCHAR(255))
+BEGIN
+    DECLARE InternalEntityID INT UNSIGNED DEFAULT NULL;
+
+    SELECT internal_id INTO InternalEntityID FROM entity_ids WHERE id = EntityID;
+
+    
+    SELECT c.version AS child,
+            NULL as parent,
+            t.seconds AS child_seconds,
+            t.nanos AS child_nanos,
+            t.username AS child_username,
+            t.realm AS child_realm
+        FROM entity_version AS c INNER JOIN transactions as t
+        ON ( c.srid = t.srid )
+        WHERE c.entity_id = InternalEntityID
+        AND c._ipparent is Null
+
+    
+    
+    
+
+    
+    UNION SELECT c.version AS child,
+            p.version AS parent,
+            t.seconds AS child_seconds,
+            t.nanos AS child_nanos,
+            t.username AS child_username,
+            t.realm AS child_realm
+        FROM entity_version AS p
+            INNER JOIN entity_version as c
+            INNER JOIN transactions AS t
+            ON (c._ipparent = p._iversion
+                AND c.entity_id = p.entity_id
+                AND t.srid = c.srid)
+        WHERE p.entity_id = InternalEntityID;
+END ;;
+DELIMITER ;
+/*!50003 SET sql_mode              = @saved_sql_mode */ ;
+/*!50003 SET character_set_client  = @saved_cs_client */ ;
+/*!50003 SET character_set_results = @saved_cs_results */ ;
+/*!50003 SET collation_connection  = @saved_col_connection */ ;
+/*!50003 SET @saved_sql_mode       = @@sql_mode */ ;
+/*!50003 SET sql_mode              = 'STRICT_TRANS_TABLES,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION' */ ;
+/*!50003 DROP PROCEDURE IF EXISTS `initBackReference` */;
+/*!50003 SET @saved_cs_client      = @@character_set_client */ ;
+/*!50003 SET @saved_cs_results     = @@character_set_results */ ;
+/*!50003 SET @saved_col_connection = @@collation_connection */ ;
+/*!50003 SET character_set_client  = utf8 */ ;
+/*!50003 SET character_set_results = utf8 */ ;
+/*!50003 SET collation_connection  = utf8_general_ci */ ;
+DELIMITER ;;
+CREATE DEFINER=`root`@`%` PROCEDURE `initBackReference`(in PropertyID VARCHAR(255), in PropertyName VARCHAR(255), in EntityID VARCHAR(255), in EntityName VARCHAR(255))
+BEGIN
+    DECLARE propertiesTable VARCHAR(255) DEFAULT NULL;
+    DECLARE entitiesTable VARCHAR(255) DEFAULT NULL;
+
+    IF PropertyName IS NOT NULL THEN
+        
+        call createTmpTable(propertiesTable, FALSE);
+        call initSubEntity(PropertyID, PropertyName, propertiesTable);
+    END IF;
+
+    IF EntityName IS NOT NULL THEN
+        
+        call createTmpTable(entitiesTable, FALSE);
+        call initSubEntity(EntityID, EntityName, entitiesTable);
+    END IF;
+
+    SELECT propertiesTable, entitiesTable;
+
+
+END ;;
+DELIMITER ;
+/*!50003 SET sql_mode              = @saved_sql_mode */ ;
+/*!50003 SET character_set_client  = @saved_cs_client */ ;
+/*!50003 SET character_set_results = @saved_cs_results */ ;
+/*!50003 SET collation_connection  = @saved_col_connection */ ;
+/*!50003 SET @saved_sql_mode       = @@sql_mode */ ;
+/*!50003 SET sql_mode              = 'STRICT_TRANS_TABLES,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION' */ ;
+/*!50003 DROP PROCEDURE IF EXISTS `initDisjunctionFilter` */;
+/*!50003 SET @saved_cs_client      = @@character_set_client */ ;
+/*!50003 SET @saved_cs_results     = @@character_set_results */ ;
+/*!50003 SET @saved_col_connection = @@collation_connection */ ;
+/*!50003 SET character_set_client  = utf8 */ ;
+/*!50003 SET character_set_results = utf8 */ ;
+/*!50003 SET collation_connection  = utf8_general_ci */ ;
+DELIMITER ;;
+CREATE DEFINER=`root`@`%` PROCEDURE `initDisjunctionFilter`(in versioned BOOLEAN)
+BEGIN
+    call initEmptyTargetSet(NULL, versioned);
+END ;;
+DELIMITER ;
+/*!50003 SET sql_mode              = @saved_sql_mode */ ;
+/*!50003 SET character_set_client  = @saved_cs_client */ ;
+/*!50003 SET character_set_results = @saved_cs_results */ ;
+/*!50003 SET collation_connection  = @saved_col_connection */ ;
+/*!50003 SET @saved_sql_mode       = @@sql_mode */ ;
+/*!50003 SET sql_mode              = 'STRICT_TRANS_TABLES,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION' */ ;
+/*!50003 DROP PROCEDURE IF EXISTS `initEmptyTargetSet` */;
+/*!50003 SET @saved_cs_client      = @@character_set_client */ ;
+/*!50003 SET @saved_cs_results     = @@character_set_results */ ;
+/*!50003 SET @saved_col_connection = @@collation_connection */ ;
+/*!50003 SET character_set_client  = utf8 */ ;
+/*!50003 SET character_set_results = utf8 */ ;
+/*!50003 SET collation_connection  = utf8_general_ci */ ;
+DELIMITER ;;
+CREATE DEFINER=`root`@`%` PROCEDURE `initEmptyTargetSet`(in targetSet VARCHAR(255), in versioned BOOLEAN)
+BEGIN
+    DECLARE newTableName VARCHAR(255) DEFAULT targetSet;
+    IF targetSet IS NOT NULL THEN
+        SET @isNotEmptyVar = NULL; 
+        SET @isEmptyStmtStr = CONCAT("SELECT 1 INTO @isNotEmptyVar FROM `",targetSet,"` LIMIT 1");
+        PREPARE stmtIsNotEmpty FROM @isEmptyStmtStr;
+        EXECUTE stmtIsNotEmpty;
+        DEALLOCATE PREPARE stmtIsNotEmpty;
+        IF @isNotEmptyVar IS NOT NULL THEN 
+            call createTmpTable(newTableName, versioned);
+        END IF;
+    ELSE
+        call createTmpTable(newTableName, versioned);
+    END IF;
+    SELECT newTableName AS newTableName;
+END ;;
+DELIMITER ;
+/*!50003 SET sql_mode              = @saved_sql_mode */ ;
+/*!50003 SET character_set_client  = @saved_cs_client */ ;
+/*!50003 SET character_set_results = @saved_cs_results */ ;
+/*!50003 SET collation_connection  = @saved_col_connection */ ;
+/*!50003 SET @saved_sql_mode       = @@sql_mode */ ;
+/*!50003 SET sql_mode              = 'STRICT_TRANS_TABLES,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION' */ ;
+/*!50003 DROP PROCEDURE IF EXISTS `initEntity` */;
+/*!50003 SET @saved_cs_client      = @@character_set_client */ ;
+/*!50003 SET @saved_cs_results     = @@character_set_results */ ;
+/*!50003 SET @saved_col_connection = @@collation_connection */ ;
+/*!50003 SET character_set_client  = utf8 */ ;
+/*!50003 SET character_set_results = utf8 */ ;
+/*!50003 SET collation_connection  = utf8_general_ci */ ;
+DELIMITER ;;
+CREATE DEFINER=`root`@`%` PROCEDURE `initEntity`(in eid VARCHAR(255), in ename VARCHAR(255),
+                                   in enameLike VARCHAR(255), in enameRegexp VARCHAR(255),
+                                   in resultset VARCHAR(255), in versioned BOOLEAN)
+initEntityLabel: BEGIN
+    DECLARE select_columns VARCHAR(255) DEFAULT '` (id) SELECT entity_id FROM name_data ';
+    SET @initEntityStmtStr = NULL;
+
+    
+    
+    IF versioned IS TRUE THEN
+        SET select_columns = '` (id, _iversion) SELECT entity_id, _get_head_iversion(entity_id) FROM name_data ';
+    END IF;
+    IF ename IS NOT NULL THEN
+        SET @initEntityStmtStr = CONCAT(
+            'INSERT IGNORE INTO `',
+            resultset,
+            select_columns,
+            'WHERE value=?; ');
+        SET @query_param = ename;
+    ELSEIF enameLike IS NOT NULL THEN
+        SET @initEntityStmtStr = CONCAT(
+            'INSERT IGNORE INTO `',
+            resultset,
+            select_columns,
+            'WHERE value LIKE ?;');
+        SET @query_param = enameLike;
+    ELSEIF enameRegexp IS NOT NULL THEN 
+        SET @initEntityStmtStr = CONCAT(
+            'INSERT IGNORE INTO `',
+            resultset,
+            select_columns,
+            'WHERE value REGEXP ?;');
+        SET @query_param = enameRegexp;
+    END IF;
+
+    
+    IF @initEntityStmtStr IS NOT NULL THEN
+        PREPARE initEntityStmt FROM @initEntityStmtStr;
+        EXECUTE initEntityStmt USING @query_param;
+        DEALLOCATE PREPARE initEntityStmt;
+    END IF;
+
+    IF eid IS NOT NULL THEN
+        
+        SET @initEntityStmtStr = CONCAT(
+            'INSERT IGNORE INTO `',
+            resultset,
+            IF(versioned,
+                '` (id, _iversion) SELECT eids.internal_id, _get_head_iversion(eids.internal_id) ',
+                '` (id) SELECT eids.internal_id '),
+            'FROM entity_ids AS eids WHERE eids.id=',eid,';');
+        PREPARE initEntityStmt FROM @initEntityStmtStr;
+        EXECUTE initEntityStmt;
+        DEALLOCATE PREPARE initEntityStmt;
+    END IF;
+
+
+    
+    
+    IF versioned IS TRUE THEN
+        SET select_columns = '` (id, _iversion) SELECT entity_id, _iversion FROM archive_name_data ';
+        IF ename IS NOT NULL THEN
+            SET @initEntityStmtStr = CONCAT(
+                'INSERT IGNORE INTO `',
+                resultset,
+                select_columns,
+                'WHERE value=?; ');
+            SET @query_param = ename;
+        ELSEIF enameLike IS NOT NULL THEN
+            SET @initEntityStmtStr = CONCAT(
+                'INSERT IGNORE INTO `',
+                resultset,
+                select_columns,
+                'WHERE value LIKE ?;');
+            SET @query_param = enameLike;
+        ELSEIF enameRegexp IS NOT NULL THEN
+            SET @initEntityStmtStr = CONCAT(
+                'INSERT IGNORE INTO `',
+                resultset,
+                'WHERE value REGEXP ?;');
+            SET @query_param = enameRegexp;
+        END IF;
+
+        
+        IF @initEntityStmtStr IS NOT NULL THEN
+            PREPARE initEntityStmt FROM @initEntityStmtStr;
+            EXECUTE initEntityStmt USING @query_param;
+            DEALLOCATE PREPARE initEntityStmt;
+        END IF;
+    END IF;
+    
+
+
+    IF @initEntityStmtStr IS NOT NULL THEN
+        call getChildren(resultset, versioned);
+    END IF;
+
+END ;;
+DELIMITER ;
+/*!50003 SET sql_mode              = @saved_sql_mode */ ;
+/*!50003 SET character_set_client  = @saved_cs_client */ ;
+/*!50003 SET character_set_results = @saved_cs_results */ ;
+/*!50003 SET collation_connection  = @saved_col_connection */ ;
+/*!50003 SET @saved_sql_mode       = @@sql_mode */ ;
+/*!50003 SET sql_mode              = 'STRICT_TRANS_TABLES,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION' */ ;
+/*!50003 DROP PROCEDURE IF EXISTS `initPOVPropertiesTable` */;
+/*!50003 SET @saved_cs_client      = @@character_set_client */ ;
+/*!50003 SET @saved_cs_results     = @@character_set_results */ ;
+/*!50003 SET @saved_col_connection = @@collation_connection */ ;
+/*!50003 SET character_set_client  = utf8 */ ;
+/*!50003 SET character_set_results = utf8 */ ;
+/*!50003 SET collation_connection  = utf8_general_ci */ ;
+DELIMITER ;;
+CREATE DEFINER=`root`@`%` PROCEDURE `initPOVPropertiesTable`(in PropertyID VARCHAR(255), in PropertyName VARCHAR(255), in sourceSet VARCHAR(255))
+BEGIN
+    DECLARE propertiesTable VARCHAR(255) DEFAULT NULL; 
+    DECLARE replTbl VARCHAR(255) DEFAULT NULL;
+    DECLARE ecount INT DEFAULT 0;
+    DECLARE t1 BIGINT DEFAULT 0;
+    DECLARE t2 BIGINT DEFAULT 0;
+    DECLARE t3 BIGINT DEFAULT 0;
+    DECLARE t4 BIGINT DEFAULT 0;
+    DECLARE t5 BIGINT DEFAULT 0;
+    DECLARE t6 BIGINT DEFAULT 0;
+
+
+    IF PropertyName is NOT NULL THEN 
+        SELECT conv( concat( substring(uid,16,3), substring(uid,10,4), substring(uid,1,8)),16,10) div 10000 - (141427 * 24 * 60 * 60 * 1000) as current_mills INTO t1 from (select uuid() uid) as alias;
+        call createTmpTable2(propertiesTable);
+
+        
+        
+        
+        
+        
+        
+        
+        
+        SET @initPOVPropertiesTableStmt1 = CONCAT('INSERT IGNORE INTO `', propertiesTable, '` (id, id2, domain) SELECT property_id, entity_id, domain_id from name_overrides WHERE name = ? UNION ALL SELECT entity_id, domain_id, 0 FROM name_data WHERE value = ?;');
+        PREPARE stmt FROM @initPOVPropertiesTableStmt1;
+        SET @PropertyName = PropertyName;
+        EXECUTE stmt USING @PropertyName, @PropertyName;
+        SET ecount = ROW_COUNT();
+
+        
+        SELECT conv( concat( substring(uid,16,3), substring(uid,10,4), substring(uid,1,8)),16,10) div 10000 - (141427 * 24 * 60 * 60 * 1000) as current_mills INTO t2 from (select uuid() uid) as alias;
+        IF PropertyID IS NOT NULL THEN
+            SET @initPOVPropertiesTableStmt2 = CONCAT('INSERT IGNORE INTO `', propertiesTable, '` (id, id2, domain) VALUES (?, 0, 0)');
+            PREPARE stmt FROM @initPOVPropertiesTableStmt2;
+            SET @PropertyID = PropertyID;
+            EXECUTE stmt USING @PropertyID;
+            SET ecount = ecount + ROW_COUNT();
+        END IF;
+
+        
+        SELECT conv( concat( substring(uid,16,3), substring(uid,10,4), substring(uid,1,8)),16,10) div 10000 - (141427 * 24 * 60 * 60 * 1000) as current_mills INTO t3 from (select uuid() uid) as alias;
+        IF ecount > 0 THEN
+            
+            call getChildren(propertiesTable, False);
+        END IF;
+
+        
+        SELECT conv( concat( substring(uid,16,3), substring(uid,10,4), substring(uid,1,8)),16,10) div 10000 - (141427 * 24 * 60 * 60 * 1000) as current_mills INTO t4 from (select uuid() uid) as alias;
+        IF ecount > 0 THEN
+            call createTmpTable2(replTbl);
+            SET @replTblStmt1 := CONCAT('INSERT IGNORE INTO `',replTbl, '` (id, id2, domain) SELECT r.value as id, r.entity_id as id2, 0 as domain_id FROM reference_data AS r WHERE status="REPLACEMENT" AND domain_id=0 AND EXISTS (SELECT * FROM `', sourceSet, '` AS s WHERE s.id=r.entity_id) AND EXISTS (SELECT * FROM `', propertiesTable, '` AS p WHERE p.domain = 0 AND p.id2=0 AND p.id=r.property_id);');
+            PREPARE replStmt1 FROM @replTblStmt1;
+            EXECUTE replStmt1;
+            DEALLOCATE PREPARE replStmt1;
+            SELECT conv( concat( substring(uid,16,3), substring(uid,10,4), substring(uid,1,8)),16,10) div 10000 - (141427 * 24 * 60 * 60 * 1000) as current_mills INTO t5 from (select uuid() uid) as alias;
+
+            SET @replTblStmt2 := CONCAT('INSERT IGNORE INTO `', propertiesTable, '` SELECT id, id2, domain FROM `', replTbl, '`;');
+            PREPARE replStmt2 FROM @replTblStmt2;
+            EXECUTE replStmt2;
+            DEALLOCATE PREPARE replStmt2;
+            SELECT conv( concat( substring(uid,16,3), substring(uid,10,4), substring(uid,1,8)),16,10) div 10000 - (141427 * 24 * 60 * 60 * 1000) as current_mills INTO t6 from (select uuid() uid) as alias;
+        END IF;
+    END IF;
+    SELECT propertiesTable, t1, t2, t3, t4, t5, t6, @initPOVPropertiesTableStmt1 as initPOVPropertiesTableStmt1, @initPOVPropertiesTableStmt2 as initPOVPropertiesTableStmt2, @replTblStmt1 as replTblStmt1, @replTblStmt2 as replTblStmt2;
+END ;;
+DELIMITER ;
+/*!50003 SET sql_mode              = @saved_sql_mode */ ;
+/*!50003 SET character_set_client  = @saved_cs_client */ ;
+/*!50003 SET character_set_results = @saved_cs_results */ ;
+/*!50003 SET collation_connection  = @saved_col_connection */ ;
+/*!50003 SET @saved_sql_mode       = @@sql_mode */ ;
+/*!50003 SET sql_mode              = 'STRICT_TRANS_TABLES,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION' */ ;
+/*!50003 DROP PROCEDURE IF EXISTS `initPOVRefidsTable` */;
+/*!50003 SET @saved_cs_client      = @@character_set_client */ ;
+/*!50003 SET @saved_cs_results     = @@character_set_results */ ;
+/*!50003 SET @saved_col_connection = @@collation_connection */ ;
+/*!50003 SET character_set_client  = utf8 */ ;
+/*!50003 SET character_set_results = utf8 */ ;
+/*!50003 SET collation_connection  = utf8_general_ci */ ;
+DELIMITER ;;
+CREATE DEFINER=`root`@`%` PROCEDURE `initPOVRefidsTable`(in PropertyID VARCHAR(255), in PropertyName VARCHAR(255))
+BEGIN
+    DECLARE refIdsTable VARCHAR(255) DEFAULT NULL; 
+
+    
+    IF PropertyName IS NOT NULL THEN
+        
+        call createTmpTable(refIdsTable, FALSE);
+        call initSubEntity(PropertyID, PropertyName, refIdsTable);
+        
+    END IF;
+    SELECT refIdsTable;
+
+END ;;
+DELIMITER ;
+/*!50003 SET sql_mode              = @saved_sql_mode */ ;
+/*!50003 SET character_set_client  = @saved_cs_client */ ;
+/*!50003 SET character_set_results = @saved_cs_results */ ;
+/*!50003 SET collation_connection  = @saved_col_connection */ ;
+/*!50003 SET @saved_sql_mode       = @@sql_mode */ ;
+/*!50003 SET sql_mode              = 'STRICT_TRANS_TABLES,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION' */ ;
+/*!50003 DROP PROCEDURE IF EXISTS `initQuery` */;
+/*!50003 SET @saved_cs_client      = @@character_set_client */ ;
+/*!50003 SET @saved_cs_results     = @@character_set_results */ ;
+/*!50003 SET @saved_col_connection = @@collation_connection */ ;
+/*!50003 SET character_set_client  = utf8 */ ;
+/*!50003 SET character_set_results = utf8 */ ;
+/*!50003 SET collation_connection  = utf8_general_ci */ ;
+DELIMITER ;;
+CREATE DEFINER=`root`@`%` PROCEDURE `initQuery`(in versioned BOOLEAN)
+BEGIN
+    CREATE TEMPORARY TABLE IF NOT EXISTS warnings (warning TEXT NOT NULL);
+
+    call createTmpTable(@resultSet, versioned);
+    SELECT @resultSet as tablename;
+
+END ;;
+DELIMITER ;
+/*!50003 SET sql_mode              = @saved_sql_mode */ ;
+/*!50003 SET character_set_client  = @saved_cs_client */ ;
+/*!50003 SET character_set_results = @saved_cs_results */ ;
+/*!50003 SET collation_connection  = @saved_col_connection */ ;
+/*!50003 SET @saved_sql_mode       = @@sql_mode */ ;
+/*!50003 SET sql_mode              = 'STRICT_TRANS_TABLES,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION' */ ;
+/*!50003 DROP PROCEDURE IF EXISTS `initSubEntity` */;
+/*!50003 SET @saved_cs_client      = @@character_set_client */ ;
+/*!50003 SET @saved_cs_results     = @@character_set_results */ ;
+/*!50003 SET @saved_col_connection = @@collation_connection */ ;
+/*!50003 SET character_set_client  = utf8 */ ;
+/*!50003 SET character_set_results = utf8 */ ;
+/*!50003 SET collation_connection  = utf8_general_ci */ ;
+DELIMITER ;;
+CREATE DEFINER=`root`@`%` PROCEDURE `initSubEntity`(in EntityID VARCHAR(255), in ename VARCHAR(255), in tableName VARCHAR(255))
+BEGIN
+    DECLARE ecount INT DEFAULT 0;
+    DECLARE op VARCHAR(255) DEFAULT '=';
+
+
+    IF LOCATE("%", ename) > 0 THEN
+        SET op = "LIKE";
+    END IF;
+
+    SET @stmtStr = CONCAT('INSERT IGNORE INTO `',
+        tableName,
+        '` (id) SELECT entity_id FROM name_data WHERE value ',
+        op,
+        ' ? AND domain_id=0;');
+
+    PREPARE stmt FROM @stmtStr;
+    SET @ename = ename;
+    EXECUTE stmt USING @ename;
+    SET ecount = ROW_COUNT();
+    DEALLOCATE PREPARE stmt;
+
+    IF EntityID IS NOT NULL THEN
+        SET @stmtStr = CONCAT('INSERT IGNORE INTO `', tableName, '` (id) SELECT internal_id FROM entity_ids WHERE id = ?');
+        PREPARE stmt FROM @stmtStr;
+        SET @eid = EntityID;
+        EXECUTE stmt USING @eid;
+        SET ecount = ecount + ROW_COUNT();
+        DEALLOCATE PREPARE stmt;
+    END IF;
+
+    IF ecount > 0 THEN
+        
+        call getChildren(tableName, False);
+    END IF;
+
+END ;;
+DELIMITER ;
+/*!50003 SET sql_mode              = @saved_sql_mode */ ;
+/*!50003 SET character_set_client  = @saved_cs_client */ ;
+/*!50003 SET character_set_results = @saved_cs_results */ ;
+/*!50003 SET collation_connection  = @saved_col_connection */ ;
+/*!50003 SET @saved_sql_mode       = @@sql_mode */ ;
+/*!50003 SET sql_mode              = 'STRICT_TRANS_TABLES,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION' */ ;
+/*!50003 DROP PROCEDURE IF EXISTS `initSubProperty` */;
+/*!50003 SET @saved_cs_client      = @@character_set_client */ ;
+/*!50003 SET @saved_cs_results     = @@character_set_results */ ;
+/*!50003 SET @saved_col_connection = @@collation_connection */ ;
+/*!50003 SET character_set_client  = utf8 */ ;
+/*!50003 SET character_set_results = utf8 */ ;
+/*!50003 SET collation_connection  = utf8_general_ci */ ;
+DELIMITER ;;
+CREATE DEFINER=`root`@`%` PROCEDURE `initSubProperty`(in sourceSet VARCHAR(255), in propertiesTable VARCHAR(255), in refIdsTable VARCHAR(255))
+BEGIN
+DECLARE newTableName VARCHAR(255) DEFAULT NULL;
+    call registerTempTableName(newTableName);	
+    
+    SET @createSubPropertyListTableStr = CONCAT('CREATE TEMPORARY TABLE `', newTableName,'` ( entity_id INT UNSIGNED NOT NULL, id INT UNSIGNED NOT NULL, domain INT UNSIGNED NOT NULL, CONSTRAINT `',newTableName,'PK` PRIMARY KEY (entity_id, id, domain)) ' );
+    
+    PREPARE createSubPropertyListTable FROM @createSubPropertyListTableStr; 
+    EXECUTE createSubPropertyListTable;
+    DEALLOCATE PREPARE createSubPropertyListTable;
+
+	SET @subResultSetStmtStr = CONCAT('INSERT IGNORE INTO `', newTableName, '` (domain, entity_id, id) 
+            SELECT data1.domain_id as domain, data1.entity_id as entity_id, data1.value as id 
+                FROM reference_data as data1 JOIN reference_data as data2 
+                    ON (data1.domain_id=0 
+                        AND data1.domain_id=data2.domain_id 
+                        AND data2.entity_id=data1.entity_id 
+                        AND (
+                            (data1.property_id=data2.value AND data2.status="REPLACEMENT")
+                            OR
+                            (data1.property_id!=data2.value AND data2.status!="REPLACEMENT" AND data1.status!="REPLACEMENT" AND data1.property_id=data2.property_id)
+                        )
+                        AND EXISTS (SELECT 1 FROM `', sourceSet, '` as source WHERE source.id=data1.entity_id LIMIT 1)',
+                        IF(propertiesTable IS NULL, '', CONCAT(' AND EXISTS (SELECT 1 FROM `', propertiesTable, '` as props WHERE props.id=data2.property_id LIMIT 1)')),
+                        IF(refIdsTable IS NULL, '', CONCAT(' AND EXISTS (SELECT 1 FROM `', refIdsTable, '` as refs WHERE refs.id=data1.value LIMIT 1)')),	
+		')'
+        );
+
+
+	PREPARE subResultSetStmt FROM @subResultSetStmtStr;
+	EXECUTE subResultSetStmt;
+    DEALLOCATE PREPARE subResultSetStmt;
+
+	SELECT newTableName as list;
+
+END ;;
+DELIMITER ;
+/*!50003 SET sql_mode              = @saved_sql_mode */ ;
+/*!50003 SET character_set_client  = @saved_cs_client */ ;
+/*!50003 SET character_set_results = @saved_cs_results */ ;
+/*!50003 SET collation_connection  = @saved_col_connection */ ;
+/*!50003 SET @saved_sql_mode       = @@sql_mode */ ;
+/*!50003 SET sql_mode              = 'STRICT_TRANS_TABLES,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION' */ ;
+/*!50003 DROP PROCEDURE IF EXISTS `insertEntity` */;
+/*!50003 SET @saved_cs_client      = @@character_set_client */ ;
+/*!50003 SET @saved_cs_results     = @@character_set_results */ ;
+/*!50003 SET @saved_col_connection = @@collation_connection */ ;
+/*!50003 SET character_set_client  = utf8 */ ;
+/*!50003 SET character_set_results = utf8 */ ;
+/*!50003 SET collation_connection  = utf8_general_ci */ ;
+DELIMITER ;;
+CREATE DEFINER=`root`@`%` PROCEDURE `insertEntity`(in EntityID VARCHAR(255), in EntityName VARCHAR(255), in EntityDesc TEXT, in EntityRole VARCHAR(255), in ACL VARBINARY(65525))
+BEGIN
+    DECLARE NewACLID INT UNSIGNED DEFAULT NULL;
+    DECLARE Hash VARBINARY(255) DEFAULT NULL;
+    DECLARE Version VARBINARY(255) DEFAULT NULL;
+    DECLARE Transaction VARBINARY(255) DEFAULT NULL;
+    DECLARE InternalEntityID INT UNSIGNED DEFAULT NULL;
+
+    
+    
+    call entityACL(NewACLID, ACL);
+
+    
+    INSERT INTO entities (description, role, acl)
+        VALUES (EntityDesc, EntityRole, NewACLID);
+
+    
+    SET InternalEntityID = LAST_INSERT_ID();
+
+    INSERT INTO entity_ids (internal_id, id) VALUES (InternalEntityID, EntityID);
+
+    IF is_feature_config("ENTITY_VERSIONING", "ENABLED") THEN
+        
+        SET Transaction = @SRID;
+        SET Version = SHA1(UUID());
+        CALL insert_single_child_version(InternalEntityID, Hash, Version, Null, Transaction);
+    END IF;
+
+    
+    
+    IF EntityName IS NOT NULL THEN
+        INSERT INTO name_data
+            (domain_id, entity_id, property_id, value, status, pidx)
+            VALUES (0, InternalEntityID, 20, EntityName, "FIX", 0);
+    END IF;
+
+    SELECT Version as Version;
+
+END ;;
+DELIMITER ;
+/*!50003 SET sql_mode              = @saved_sql_mode */ ;
+/*!50003 SET character_set_client  = @saved_cs_client */ ;
+/*!50003 SET character_set_results = @saved_cs_results */ ;
+/*!50003 SET collation_connection  = @saved_col_connection */ ;
+/*!50003 SET @saved_sql_mode       = @@sql_mode */ ;
+/*!50003 SET sql_mode              = 'STRICT_TRANS_TABLES,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION' */ ;
+/*!50003 DROP PROCEDURE IF EXISTS `insertEntityCollection` */;
+/*!50003 SET @saved_cs_client      = @@character_set_client */ ;
+/*!50003 SET @saved_cs_results     = @@character_set_results */ ;
+/*!50003 SET @saved_col_connection = @@collation_connection */ ;
+/*!50003 SET character_set_client  = utf8 */ ;
+/*!50003 SET character_set_results = utf8 */ ;
+/*!50003 SET collation_connection  = utf8_general_ci */ ;
+DELIMITER ;;
+CREATE DEFINER=`root`@`%` PROCEDURE `insertEntityCollection`(in PropertyID VARCHAR(255), in Collection VARCHAR(255))
+BEGIN
+    DECLARE InternalPropertyID INT UNSIGNED DEFAULT NULL;
+
+    SELECT internal_id INTO InternalPropertyID FROM entity_ids WHERE id=PropertyID;
+
+    INSERT INTO collection_type (domain_id, entity_id, property_id, collection) SELECT 0, 0, InternalPropertyID, Collection;
+
+END ;;
+DELIMITER ;
+/*!50003 SET sql_mode              = @saved_sql_mode */ ;
+/*!50003 SET character_set_client  = @saved_cs_client */ ;
+/*!50003 SET character_set_results = @saved_cs_results */ ;
+/*!50003 SET collation_connection  = @saved_col_connection */ ;
+/*!50003 SET @saved_sql_mode       = @@sql_mode */ ;
+/*!50003 SET sql_mode              = 'STRICT_TRANS_TABLES,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION' */ ;
+/*!50003 DROP PROCEDURE IF EXISTS `insertEntityDataType` */;
+/*!50003 SET @saved_cs_client      = @@character_set_client */ ;
+/*!50003 SET @saved_cs_results     = @@character_set_results */ ;
+/*!50003 SET @saved_col_connection = @@collation_connection */ ;
+/*!50003 SET character_set_client  = utf8 */ ;
+/*!50003 SET character_set_results = utf8 */ ;
+/*!50003 SET collation_connection  = utf8_general_ci */ ;
+DELIMITER ;;
+CREATE DEFINER=`root`@`%` PROCEDURE `insertEntityDataType`(in PropertyID VARCHAR(255), in DataTypeID VARCHAR(255))
+BEGIN
+    DECLARE InternalPropertyID INT UNSIGNED DEFAULT NULL;
+
+    SELECT internal_id INTO InternalPropertyID FROM entity_ids WHERE id=PropertyID;
+
+    INSERT INTO data_type (domain_id, entity_id, property_id, datatype) SELECT 0, 0, InternalPropertyID, ( SELECT internal_id FROM entity_ids WHERE id = DataTypeID);
+
+
+END ;;
+DELIMITER ;
+/*!50003 SET sql_mode              = @saved_sql_mode */ ;
+/*!50003 SET character_set_client  = @saved_cs_client */ ;
+/*!50003 SET character_set_results = @saved_cs_results */ ;
+/*!50003 SET collation_connection  = @saved_col_connection */ ;
+/*!50003 SET @saved_sql_mode       = @@sql_mode */ ;
+/*!50003 SET sql_mode              = 'STRICT_TRANS_TABLES,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION' */ ;
+/*!50003 DROP PROCEDURE IF EXISTS `insertEntityProperty` */;
+/*!50003 SET @saved_cs_client      = @@character_set_client */ ;
+/*!50003 SET @saved_cs_results     = @@character_set_results */ ;
+/*!50003 SET @saved_col_connection = @@collation_connection */ ;
+/*!50003 SET character_set_client  = utf8 */ ;
+/*!50003 SET character_set_results = utf8 */ ;
+/*!50003 SET collation_connection  = utf8_general_ci */ ;
+DELIMITER ;;
+CREATE DEFINER=`root`@`%` PROCEDURE `insertEntityProperty`(
+    in DomainID VARCHAR(255),
+    in EntityID VARCHAR(255),
+    in PropertyID VARCHAR(255),
+    in Datatable VARCHAR(255),
+    in PropertyValue TEXT,
+    in PropertyUnitSig BIGINT,
+    in PropertyStatus VARCHAR(255),
+    in NameOverride VARCHAR(255),
+    in DescOverride TEXT,
+    in DatatypeOverride VARCHAR(255),
+    in Collection VARCHAR(255),
+    in PropertyIndex INT UNSIGNED)
+BEGIN
+    DECLARE ReferenceValueIVersion INT UNSIGNED DEFAULT NULL;
+    DECLARE ReferenceValue INT UNSIGNED DEFAULT NULL;
+    DECLARE AT_PRESENT INTEGER DEFAULT NULL;
+    DECLARE InternalDataTypeID INT UNSIGNED DEFAULT NULL;
+    DECLARE InternalPropertyID INT UNSIGNED DEFAULT NULL;
+    DECLARE InternalEntityID INT UNSIGNED DEFAULT NULL;
+    DECLARE InternalDomainID INT UNSIGNED DEFAULT 0;
+
+    SELECT internal_id INTO InternalDomainID FROM entity_ids WHERE id = DomainID;
+    
+    
+    
+    IF LOCATE("$", EntityID) = 1 THEN
+        SET InternalEntityID=SUBSTRING(EntityID, 2);
+    ELSE
+        SELECT internal_id INTO InternalEntityID FROM entity_ids WHERE id = EntityID;
+    END IF;
+    IF LOCATE("$", PropertyID) = 1 THEN
+        SET InternalPropertyID=SUBSTRING(PropertyID, 2);
+    ELSE
+        SELECT internal_id INTO InternalPropertyID FROM entity_ids WHERE id = PropertyID;
+    END IF;
+
+    CASE Datatable
+    WHEN 'double_data' THEN
+        INSERT INTO double_data
+        (domain_id, entity_id, property_id, value, unit_sig, status, pidx)
+        VALUES
+        (InternalDomainID, InternalEntityID, InternalPropertyID, PropertyValue, PropertyUnitSig, PropertyStatus, PropertyIndex);
+    WHEN 'integer_data' THEN
+        INSERT INTO integer_data
+        (domain_id, entity_id, property_id, value, unit_sig, status, pidx)
+        VALUES
+        (InternalDomainID, InternalEntityID, InternalPropertyID, PropertyValue, PropertyUnitSig, PropertyStatus, PropertyIndex);
+    WHEN 'datetime_data' THEN
+        INSERT INTO datetime_data
+        (domain_id, entity_id, property_id, value, value_ns, status, pidx)
+        VALUES
+        (InternalDomainID, InternalEntityID, InternalPropertyID, SUBSTRING_INDEX(PropertyValue, 'UTC', 1), IF(SUBSTRING_INDEX(PropertyValue, 'UTC', -1)='',NULL,SUBSTRING_INDEX(PropertyValue, 'UTC', -1)), PropertyStatus, PropertyIndex);
+    WHEN 'reference_data' THEN
+
+        
+        SET AT_PRESENT=LOCATE("@", PropertyValue);
+        IF is_feature_config("ENTITY_VERSIONING", "ENABLED") AND AT_PRESENT > 0 THEN
+            SELECT internal_id INTO ReferenceValue FROM entity_ids WHERE id = SUBSTRING_INDEX(PropertyValue, '@', 1);
+            SET ReferenceValueIVersion = get_iversion(ReferenceValue,
+                SUBSTRING_INDEX(PropertyValue, '@', -1));
+            IF ReferenceValueIVersion IS NULL THEN
+                
+                SELECT 0 from `ReferenceValueIVersion_WAS_NULL`;
+            END IF;
+
+        ELSEIF LOCATE("$", PropertyValue) = 1 THEN
+            SET ReferenceValue = SUBSTRING(PropertyValue, 2);
+        ELSE
+            SELECT internal_id INTO ReferenceValue FROM entity_ids WHERE id = PropertyValue;
+        END IF;
+
+
+        INSERT INTO reference_data
+            (domain_id, entity_id, property_id, value, value_iversion, status,
+                pidx)
+        VALUES
+            (InternalDomainID, InternalEntityID, InternalPropertyID, ReferenceValue,
+                ReferenceValueIVersion, PropertyStatus, PropertyIndex);
+    WHEN 'enum_data' THEN
+        INSERT INTO enum_data
+        (domain_id, entity_id, property_id, value, status, pidx)
+        VALUES
+        (InternalDomainID, InternalEntityID, InternalPropertyID, PropertyValue, PropertyStatus, PropertyIndex);
+    WHEN 'date_data' THEN
+        INSERT INTO date_data
+        (domain_id, entity_id, property_id, value, status, pidx)
+        VALUES
+        (InternalDomainID, InternalEntityID, InternalPropertyID, SUBSTRING_INDEX(PropertyValue, '.', 1), PropertyStatus, PropertyIndex);
+    WHEN 'text_data' THEN
+        INSERT INTO text_data
+        (domain_id, entity_id, property_id, value, status, pidx)
+        VALUES
+        (InternalDomainID, InternalEntityID, InternalPropertyID, PropertyValue, PropertyStatus, PropertyIndex);
+    WHEN 'null_data' THEN
+        INSERT INTO null_data
+        (domain_id, entity_id, property_id, status, pidx)
+        VALUES
+        (InternalDomainID, InternalEntityID, InternalPropertyID, PropertyStatus, PropertyIndex);
+    WHEN 'name_data' THEN
+        INSERT INTO name_data
+        (domain_id, entity_id, property_id, value, status, pidx)
+        VALUES
+        (InternalDomainID, InternalEntityID, InternalPropertyID, PropertyValue, PropertyStatus, PropertyIndex);
+
+    ELSE
+        
+        SELECT * FROM table_does_not_exist;
+    END CASE;
+
+    IF DatatypeOverride IS NOT NULL THEN
+        SELECT internal_id INTO InternalDataTypeID from entity_ids WHERE id = DatatypeOverride;
+        call overrideType(InternalDomainID, InternalEntityID, InternalPropertyID, InternalDataTypeID);
+        IF Collection IS NOT NULL THEN
+            INSERT INTO collection_type (domain_id, entity_id, property_id, collection) VALUES (InternalDomainID, InternalEntityID, InternalPropertyID, Collection);
+        END IF;
+    END IF;
+
+    IF NameOverride IS NOT NULL THEN
+        call overrideName(InternalDomainID, InternalEntityID, InternalPropertyID, NameOverride);
+    END IF;
+
+    IF DescOverride IS NOT NULL THEN
+        call overrideDesc(InternalDomainID, InternalEntityID, InternalPropertyID, DescOverride);
+    END IF;
+
+END ;;
+DELIMITER ;
+/*!50003 SET sql_mode              = @saved_sql_mode */ ;
+/*!50003 SET character_set_client  = @saved_cs_client */ ;
+/*!50003 SET character_set_results = @saved_cs_results */ ;
+/*!50003 SET collation_connection  = @saved_col_connection */ ;
+/*!50003 SET @saved_sql_mode       = @@sql_mode */ ;
+/*!50003 SET sql_mode              = 'STRICT_TRANS_TABLES,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION' */ ;
+/*!50003 DROP PROCEDURE IF EXISTS `insertIsa` */;
+/*!50003 SET @saved_cs_client      = @@character_set_client */ ;
+/*!50003 SET @saved_cs_results     = @@character_set_results */ ;
+/*!50003 SET @saved_col_connection = @@collation_connection */ ;
+/*!50003 SET character_set_client  = utf8 */ ;
+/*!50003 SET character_set_results = utf8 */ ;
+/*!50003 SET collation_connection  = utf8_general_ci */ ;
+DELIMITER ;;
+CREATE DEFINER=`root`@`%` PROCEDURE `insertIsa`(IN ChildID VARCHAR(255), IN ParentID VARCHAR(255))
+insert_is_a_proc: BEGIN
+
+    DECLARE c INT UNSIGNED DEFAULT NULL;
+    DECLARE p INT UNSIGNED DEFAULT NULL;
+
+    SELECT internal_id INTO c FROM entity_ids WHERE id = ChildID;
+    SELECT internal_id INTO p FROM entity_ids WHERE id = ParentID;
+
+    INSERT INTO isa_cache (child, parent, rpath) VALUES (c, p, c);
+
+    IF p = c THEN
+        
+        LEAVE insert_is_a_proc;
+    END IF;
+    
+
+    
+    
+    
+    INSERT IGNORE INTO isa_cache SELECT
+        c
+            AS child,   
+        i.parent
+            AS parent,  
+        IF(p=i.rpath or i.rpath=parent,  
+           p,                            
+           concat(p, ">", i.rpath))      
+            AS rpath
+        FROM isa_cache AS i WHERE i.child = p AND i.child != i.parent;  
+
+    
+    
+    INSERT IGNORE INTO isa_cache SELECT
+        l.child,    
+        r.parent,   
+        IF(l.rpath=l.child AND r.rpath=c,  
+           c,                              
+           concat(IF(l.rpath=l.child,        
+                     c,                         
+                     concat(l.rpath, '>', c)),  
+                  IF(r.rpath=c,              
+                     '',                        
+                     concat('>', r.rpath))))    
+            AS rpath
+        FROM
+            isa_cache AS l INNER JOIN isa_cache AS r
+            ON (l.parent = c AND c = r.child AND l.child != l.parent); 
+
+END ;;
+DELIMITER ;
+/*!50003 SET sql_mode              = @saved_sql_mode */ ;
+/*!50003 SET character_set_client  = @saved_cs_client */ ;
+/*!50003 SET character_set_results = @saved_cs_results */ ;
+/*!50003 SET collation_connection  = @saved_col_connection */ ;
+/*!50003 SET @saved_sql_mode       = @@sql_mode */ ;
+/*!50003 SET sql_mode              = 'STRICT_TRANS_TABLES,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION' */ ;
+/*!50003 DROP PROCEDURE IF EXISTS `insertLinCon` */;
+/*!50003 SET @saved_cs_client      = @@character_set_client */ ;
+/*!50003 SET @saved_cs_results     = @@character_set_results */ ;
+/*!50003 SET @saved_col_connection = @@collation_connection */ ;
+/*!50003 SET character_set_client  = utf8 */ ;
+/*!50003 SET character_set_results = utf8 */ ;
+/*!50003 SET collation_connection  = utf8_general_ci */ ;
+DELIMITER ;;
+CREATE DEFINER=`root`@`%` PROCEDURE `insertLinCon`(in signature_from BIGINT, in signature_to BIGINT, in a DECIMAL(65,30), in b_dividend BIGINT, in b_divisor BIGINT, in c DECIMAL(65,30))
+BEGIN
+
+    INSERT IGNORE INTO units_lin_con (signature_from, signature_to, a, b_dividend, b_divisor, c) VALUES (signature_from, signature_to, a, b_dividend, b_divisor, c);
+
+END ;;
+DELIMITER ;
+/*!50003 SET sql_mode              = @saved_sql_mode */ ;
+/*!50003 SET character_set_client  = @saved_cs_client */ ;
+/*!50003 SET character_set_results = @saved_cs_results */ ;
+/*!50003 SET collation_connection  = @saved_col_connection */ ;
+/*!50003 SET @saved_sql_mode       = @@sql_mode */ ;
+/*!50003 SET sql_mode              = 'STRICT_TRANS_TABLES,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION' */ ;
+/*!50003 DROP PROCEDURE IF EXISTS `insert_single_child_version` */;
+/*!50003 SET @saved_cs_client      = @@character_set_client */ ;
+/*!50003 SET @saved_cs_results     = @@character_set_results */ ;
+/*!50003 SET @saved_col_connection = @@collation_connection */ ;
+/*!50003 SET character_set_client  = utf8 */ ;
+/*!50003 SET character_set_results = utf8 */ ;
+/*!50003 SET collation_connection  = utf8_general_ci */ ;
+DELIMITER ;;
+CREATE DEFINER=`root`@`%` PROCEDURE `insert_single_child_version`(
+    in InternalEntityID INT UNSIGNED,
+    in Hash VARBINARY(255),
+    in Version VARBINARY(255),
+    in Parent VARBINARY(255),
+    in Transaction VARBINARY(255))
+BEGIN
+    DECLARE newiversion INT UNSIGNED DEFAULT NULL;
+    DECLARE newipparent INT UNSIGNED DEFAULT NULL;
+
+    
+    IF Parent IS NOT NULL THEN
+        SELECT e._iversion INTO newipparent
+            FROM entity_version AS e
+            WHERE e.entity_id = InternalEntityID
+            AND e.version = Parent;
+        IF newipparent IS NULL THEN
+            
+            SELECT concat("This parent does not exists: ", Parent)
+            FROM parent_version_does_not_exist;
+        END IF;
+    END IF;
+
+
+    
+    SELECT max(e._iversion)+1 INTO newiversion
+        FROM entity_version AS e
+        WHERE e.entity_id=InternalEntityID;
+    IF newiversion IS NULL THEN
+        SET newiversion = 1;
+    END IF;
+
+    INSERT INTO entity_version
+        (entity_id, hash, version, _iversion, _ipparent, srid)
+        VALUES
+        (InternalEntityID, Hash, Version, newiversion, newipparent, Transaction);
+
+
+
+END ;;
+DELIMITER ;
+/*!50003 SET sql_mode              = @saved_sql_mode */ ;
+/*!50003 SET character_set_client  = @saved_cs_client */ ;
+/*!50003 SET character_set_results = @saved_cs_results */ ;
+/*!50003 SET collation_connection  = @saved_col_connection */ ;
+/*!50003 SET @saved_sql_mode       = @@sql_mode */ ;
+/*!50003 SET sql_mode              = 'STRICT_TRANS_TABLES,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION' */ ;
+/*!50003 DROP PROCEDURE IF EXISTS `intersectTable` */;
+/*!50003 SET @saved_cs_client      = @@character_set_client */ ;
+/*!50003 SET @saved_cs_results     = @@character_set_results */ ;
+/*!50003 SET @saved_col_connection = @@collation_connection */ ;
+/*!50003 SET character_set_client  = utf8 */ ;
+/*!50003 SET character_set_results = utf8 */ ;
+/*!50003 SET collation_connection  = utf8_general_ci */ ;
+DELIMITER ;;
+CREATE DEFINER=`root`@`%` PROCEDURE `intersectTable`(in resultSetTable VARCHAR(255), in diff VARCHAR(255))
+BEGIN
+    SET @diffStmtStr = CONCAT('DELETE FROM `', resultSetTable, '` WHERE id NOT IN ( SELECT id FROM `', diff,'`)');
+    PREPARE diffStmt FROM @diffStmtStr;
+    EXECUTE diffStmt;
+
+    
+END ;;
+DELIMITER ;
+/*!50003 SET sql_mode              = @saved_sql_mode */ ;
+/*!50003 SET character_set_client  = @saved_cs_client */ ;
+/*!50003 SET character_set_results = @saved_cs_results */ ;
+/*!50003 SET collation_connection  = @saved_col_connection */ ;
+/*!50003 SET @saved_sql_mode       = @@sql_mode */ ;
+/*!50003 SET sql_mode              = 'STRICT_TRANS_TABLES,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION' */ ;
+/*!50003 DROP PROCEDURE IF EXISTS `isSubtype` */;
+/*!50003 SET @saved_cs_client      = @@character_set_client */ ;
+/*!50003 SET @saved_cs_results     = @@character_set_results */ ;
+/*!50003 SET @saved_col_connection = @@collation_connection */ ;
+/*!50003 SET character_set_client  = utf8 */ ;
+/*!50003 SET character_set_results = utf8 */ ;
+/*!50003 SET collation_connection  = utf8_general_ci */ ;
+DELIMITER ;;
+CREATE DEFINER=`root`@`%` PROCEDURE `isSubtype`(in ChildID VARCHAR(255), in ParentID VARCHAR(255))
+BEGIN
+    DECLARE c INT UNSIGNED DEFAULT NULL;
+    DECLARE p INT UNSIGNED DEFAULT NULL;
+    DECLARE ret BOOLEAN DEFAULT FALSE;
+
+    SELECT internal_id INTO c from entity_ids WHERE id = ChildID;
+    SELECT internal_id INTO p from entity_ids WHERE id = ParentID;
+
+    SELECT TRUE INTO ret FROM isa_cache AS i WHERE i.child=c AND i.parent=p LIMIT 1;
+    SELECT ret as ISA;
+END ;;
+DELIMITER ;
+/*!50003 SET sql_mode              = @saved_sql_mode */ ;
+/*!50003 SET character_set_client  = @saved_cs_client */ ;
+/*!50003 SET character_set_results = @saved_cs_results */ ;
+/*!50003 SET collation_connection  = @saved_col_connection */ ;
+/*!50003 SET @saved_sql_mode       = @@sql_mode */ ;
+/*!50003 SET sql_mode              = 'STRICT_TRANS_TABLES,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION' */ ;
+/*!50003 DROP PROCEDURE IF EXISTS `overrideDesc` */;
+/*!50003 SET @saved_cs_client      = @@character_set_client */ ;
+/*!50003 SET @saved_cs_results     = @@character_set_results */ ;
+/*!50003 SET @saved_col_connection = @@collation_connection */ ;
+/*!50003 SET character_set_client  = utf8 */ ;
+/*!50003 SET character_set_results = utf8 */ ;
+/*!50003 SET collation_connection  = utf8_general_ci */ ;
+DELIMITER ;;
+CREATE DEFINER=`root`@`%` PROCEDURE `overrideDesc`(in InternalDomainID INT UNSIGNED, in InternalEntityID INT UNSIGNED, in InternalPropertyID INT UNSIGNED, in Description TEXT)
+BEGIN
+    INSERT INTO desc_overrides (domain_id, entity_id, property_id, description) VALUES (InternalDomainID, InternalEntityID, InternalPropertyID, Description);
+END ;;
+DELIMITER ;
+/*!50003 SET sql_mode              = @saved_sql_mode */ ;
+/*!50003 SET character_set_client  = @saved_cs_client */ ;
+/*!50003 SET character_set_results = @saved_cs_results */ ;
+/*!50003 SET collation_connection  = @saved_col_connection */ ;
+/*!50003 SET @saved_sql_mode       = @@sql_mode */ ;
+/*!50003 SET sql_mode              = 'STRICT_TRANS_TABLES,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION' */ ;
+/*!50003 DROP PROCEDURE IF EXISTS `overrideName` */;
+/*!50003 SET @saved_cs_client      = @@character_set_client */ ;
+/*!50003 SET @saved_cs_results     = @@character_set_results */ ;
+/*!50003 SET @saved_col_connection = @@collation_connection */ ;
+/*!50003 SET character_set_client  = utf8 */ ;
+/*!50003 SET character_set_results = utf8 */ ;
+/*!50003 SET collation_connection  = utf8_general_ci */ ;
+DELIMITER ;;
+CREATE DEFINER=`root`@`%` PROCEDURE `overrideName`(in InternalDomainID INT UNSIGNED, in InternalEntityID INT UNSIGNED, in InternalPropertyID INT UNSIGNED, in Name VARCHAR(255))
+BEGIN
+    INSERT INTO name_overrides (domain_id, entity_id, property_id, name) VALUES (InternalDomainID, InternalEntityID, InternalPropertyID, Name);
+END ;;
+DELIMITER ;
+/*!50003 SET sql_mode              = @saved_sql_mode */ ;
+/*!50003 SET character_set_client  = @saved_cs_client */ ;
+/*!50003 SET character_set_results = @saved_cs_results */ ;
+/*!50003 SET collation_connection  = @saved_col_connection */ ;
+/*!50003 SET @saved_sql_mode       = @@sql_mode */ ;
+/*!50003 SET sql_mode              = 'STRICT_TRANS_TABLES,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION' */ ;
+/*!50003 DROP PROCEDURE IF EXISTS `overrideType` */;
+/*!50003 SET @saved_cs_client      = @@character_set_client */ ;
+/*!50003 SET @saved_cs_results     = @@character_set_results */ ;
+/*!50003 SET @saved_col_connection = @@collation_connection */ ;
+/*!50003 SET character_set_client  = utf8 */ ;
+/*!50003 SET character_set_results = utf8 */ ;
+/*!50003 SET collation_connection  = utf8_general_ci */ ;
+DELIMITER ;;
+CREATE DEFINER=`root`@`%` PROCEDURE `overrideType`(in InternalDomainID INT UNSIGNED, in InternalEntityID INT UNSIGNED, in InternalPropertyID INT UNSIGNED, in InternalDataTypeID INT UNSIGNED)
+BEGIN
+    INSERT INTO data_type (domain_id, entity_id, property_id, datatype) VALUES (InternalDomainID, InternalEntityID, InternalPropertyID, InternalDataTypeID);
+END ;;
+DELIMITER ;
+/*!50003 SET sql_mode              = @saved_sql_mode */ ;
+/*!50003 SET character_set_client  = @saved_cs_client */ ;
+/*!50003 SET character_set_results = @saved_cs_results */ ;
+/*!50003 SET collation_connection  = @saved_col_connection */ ;
+/*!50003 SET @saved_sql_mode       = @@sql_mode */ ;
+/*!50003 SET sql_mode              = 'STRICT_TRANS_TABLES,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION' */ ;
+/*!50003 DROP PROCEDURE IF EXISTS `raiseWarning` */;
+/*!50003 SET @saved_cs_client      = @@character_set_client */ ;
+/*!50003 SET @saved_cs_results     = @@character_set_results */ ;
+/*!50003 SET @saved_col_connection = @@collation_connection */ ;
+/*!50003 SET character_set_client  = utf8 */ ;
+/*!50003 SET character_set_results = utf8 */ ;
+/*!50003 SET collation_connection  = utf8_general_ci */ ;
+DELIMITER ;;
+CREATE DEFINER=`root`@`%` PROCEDURE `raiseWarning`(in str VARCHAR(20000))
+BEGIN
+    INSERT INTO warnings VALUES (str);
+END ;;
+DELIMITER ;
+/*!50003 SET sql_mode              = @saved_sql_mode */ ;
+/*!50003 SET character_set_client  = @saved_cs_client */ ;
+/*!50003 SET character_set_results = @saved_cs_results */ ;
+/*!50003 SET collation_connection  = @saved_col_connection */ ;
+/*!50003 SET @saved_sql_mode       = @@sql_mode */ ;
+/*!50003 SET sql_mode              = 'STRICT_TRANS_TABLES,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION' */ ;
+/*!50003 DROP PROCEDURE IF EXISTS `registerReplacementIds` */;
+/*!50003 SET @saved_cs_client      = @@character_set_client */ ;
+/*!50003 SET @saved_cs_results     = @@character_set_results */ ;
+/*!50003 SET @saved_col_connection = @@collation_connection */ ;
+/*!50003 SET character_set_client  = utf8 */ ;
+/*!50003 SET character_set_results = utf8 */ ;
+/*!50003 SET collation_connection  = utf8_general_ci */ ;
+DELIMITER ;;
+CREATE DEFINER=`root`@`%` PROCEDURE `registerReplacementIds`(in amount INT UNSIGNED)
+BEGIN
+    DECLARE ED INTEGER DEFAULT NULL;
+
+    SELECT COUNT(id) INTO ED FROM entities WHERE Role='_REPLACEMENT' AND id!=0;
+
+    WHILE ED < amount DO
+        INSERT INTO entities (description, role, acl) VALUES
+            (NULL, '_REPLACEMENT', 0);
+
+        SET ED = ED + 1;
+    END WHILE;
+
+    SELECT CONCAT("$", e.id) as ReplacementID FROM entities AS e WHERE e.Role='_REPLACEMENT' and e.id!=0;
+
+END ;;
+DELIMITER ;
+/*!50003 SET sql_mode              = @saved_sql_mode */ ;
+/*!50003 SET character_set_client  = @saved_cs_client */ ;
+/*!50003 SET character_set_results = @saved_cs_results */ ;
+/*!50003 SET collation_connection  = @saved_col_connection */ ;
+/*!50003 SET @saved_sql_mode       = @@sql_mode */ ;
+/*!50003 SET sql_mode              = 'STRICT_TRANS_TABLES,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION' */ ;
+/*!50003 DROP PROCEDURE IF EXISTS `registerTempTableName` */;
+/*!50003 SET @saved_cs_client      = @@character_set_client */ ;
+/*!50003 SET @saved_cs_results     = @@character_set_results */ ;
+/*!50003 SET @saved_col_connection = @@collation_connection */ ;
+/*!50003 SET character_set_client  = utf8 */ ;
+/*!50003 SET character_set_results = utf8 */ ;
+/*!50003 SET collation_connection  = utf8_general_ci */ ;
+DELIMITER ;;
+CREATE DEFINER=`root`@`%` PROCEDURE `registerTempTableName`(out newTableName VARCHAR(255))
+BEGIN
+    SET newTableName = md5(CONCAT(RAND(),CURRENT_TIMESTAMP()));
+    SET @tempTableList = IF(@tempTableList IS NULL,
+        CONCAT('`',newTableName,'`'),
+        CONCAT(@tempTableList, ',`', newTableName, '`')
+    );
+END ;;
+DELIMITER ;
+/*!50003 SET sql_mode              = @saved_sql_mode */ ;
+/*!50003 SET character_set_client  = @saved_cs_client */ ;
+/*!50003 SET character_set_results = @saved_cs_results */ ;
+/*!50003 SET collation_connection  = @saved_col_connection */ ;
+/*!50003 SET @saved_sql_mode       = @@sql_mode */ ;
+/*!50003 SET sql_mode              = 'STRICT_TRANS_TABLES,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION' */ ;
+/*!50003 DROP PROCEDURE IF EXISTS `retrieveEntity` */;
+/*!50003 SET @saved_cs_client      = @@character_set_client */ ;
+/*!50003 SET @saved_cs_results     = @@character_set_results */ ;
+/*!50003 SET @saved_col_connection = @@collation_connection */ ;
+/*!50003 SET character_set_client  = utf8 */ ;
+/*!50003 SET character_set_results = utf8 */ ;
+/*!50003 SET collation_connection  = utf8_general_ci */ ;
+DELIMITER ;;
+CREATE DEFINER=`root`@`%` PROCEDURE `retrieveEntity`(
+    in EntityID VARCHAR(255),
+    in Version VARBINARY(255))
+retrieveEntityBody: BEGIN
+    DECLARE FilePath VARCHAR(255) DEFAULT NULL;
+    DECLARE FileSize VARCHAR(255) DEFAULT NULL;
+    DECLARE FileHash VARCHAR(255) DEFAULT NULL;
+    DECLARE DatatypeID VARCHAR(255) DEFAULT NULL;
+    DECLARE InternalDatatypeID INT UNSIGNED DEFAULT NULL;
+    DECLARE CollectionName VARCHAR(255) DEFAULT NULL;
+    DECLARE IsHead BOOLEAN DEFAULT TRUE;
+    DECLARE IVersion INT UNSIGNED DEFAULT NULL;
+    DECLARE InternalEntityID INT UNSIGNED DEFAULT NULL;
+
+    SELECT internal_id INTO InternalEntityID from entity_ids WHERE id = EntityID;
+
+    IF InternalEntityID IS NULL THEN
+        
+        SELECT 0 FROM entities WHERE 0 = 1;
+        LEAVE retrieveEntityBody;
+    END IF;
+
+    IF is_feature_config("ENTITY_VERSIONING", "ENABLED") THEN
+        
+        IF Version IS NULL OR UPPER(Version) = "HEAD" THEN
+            SET Version = get_head_version(EntityID);
+        ELSEIF UPPER(LEFT(Version, 5)) = "HEAD~" THEN
+            SET IsHead = FALSE;
+            SET Version = get_head_relative(EntityID, SUBSTR(Version, 6));
+        ELSE
+            SELECT get_head_version(EntityID) = Version INTO IsHead;
+        END IF;
+
+        IF IsHead IS FALSE THEN
+            SET IVersion=get_iversion(InternalEntityID, Version);
+
+            IF IVersion IS NULL THEN
+                
+                SELECT 0 FROM entities WHERE 0 = 1;
+                LEAVE retrieveEntityBody;
+            END IF;
+
+            SELECT path, size, HEX(hash)
+                INTO FilePath, FileSize, FileHash
+                FROM archive_files
+                WHERE file_id = InternalEntityID
+                AND _iversion = IVersion
+                LIMIT 1;
+
+            SELECT datatype
+                INTO InternalDatatypeID
+                FROM archive_data_type
+                WHERE domain_id = 0
+                AND entity_id = 0
+                AND property_id = InternalEntityID
+                AND _iversion = IVersion
+                LIMIT 1;
+
+            SELECT collection
+                INTO CollectionName
+                FROM archive_collection_type
+                WHERE domain_id = 0
+                AND entity_id = 0
+                AND property_id = InternalEntityID
+                AND _iversion = IVersion
+                LIMIT 1;
+
+            
+            SELECT
+                ( SELECT value FROM
+                    ( SELECT value FROM name_data
+                        WHERE domain_id = 0
+                        AND entity_id = InternalDatatypeID
+                        AND property_id = 20
+                    ) AS tmp LIMIT 1 ) AS DatatypeName,
+                ( SELECT id FROM entity_ids WHERE internal_id=InternalDatatypeID ) AS DatatypeID,
+                CollectionName AS Collection,
+                EntityID AS EntityID,
+                ( SELECT value FROM archive_name_data
+                    WHERE domain_id = 0
+                    AND entity_ID = InternalEntityID
+                    AND property_id = 20
+                    AND _iversion = IVersion
+                    ) AS EntityName,
+                e.description AS EntityDesc,
+                e.role AS EntityRole,
+                FileSize AS FileSize,
+                FilePath AS FilePath,
+                FileHash AS FileHash,
+                (SELECT acl FROM entity_acl AS a WHERE a.id = e.acl) AS ACL,
+                Version AS Version
+            FROM archive_entities AS e
+            WHERE e.id = InternalEntityID
+            AND e._iversion = IVersion
+            LIMIT 1;
+
+            
+            LEAVE retrieveEntityBody;
+
+        END IF;
+    END IF;
+
+    SELECT path, size, hex(hash)
+        INTO FilePath, FileSize, FileHash
+        FROM files
+        WHERE file_id = InternalEntityID
+        LIMIT 1;
+
+    SELECT dt.datatype INTO InternalDatatypeID
+        FROM data_type as dt
+        WHERE dt.domain_id=0
+        AND dt.entity_id=0
+        AND dt.property_id=InternalEntityID
+        LIMIT 1;
+
+    SELECT collection INTO CollectionName
+        FROM collection_type
+        WHERE domain_id=0
+        AND entity_id=0
+        AND property_id=InternalEntityID
+        LIMIT 1;
+
+    SELECT
+        ( SELECT value FROM name_data
+            WHERE domain_id = 0
+            AND entity_id = InternalDatatypeID
+            AND property_id = 20 LIMIT 1 ) AS DatatypeName,
+        ( SELECT id FROM entity_ids WHERE internal_id=InternalDatatypeID ) AS DatatypeID,
+        CollectionName AS Collection,
+        EntityID AS EntityID,
+        ( SELECT value FROM name_data
+            WHERE domain_id = 0
+            AND entity_ID = InternalEntityID
+            AND property_id = 20 LIMIT 1) AS EntityName,
+        e.description AS EntityDesc,
+        e.role AS EntityRole,
+        FileSize AS FileSize,
+        FilePath AS FilePath,
+        FileHash AS FileHash,
+        (SELECT acl FROM entity_acl AS a WHERE a.id = e.acl) AS ACL,
+        Version AS Version
+    FROM entities e WHERE id = InternalEntityID LIMIT 1;
+END ;;
+DELIMITER ;
+/*!50003 SET sql_mode              = @saved_sql_mode */ ;
+/*!50003 SET character_set_client  = @saved_cs_client */ ;
+/*!50003 SET character_set_results = @saved_cs_results */ ;
+/*!50003 SET collation_connection  = @saved_col_connection */ ;
+/*!50003 SET @saved_sql_mode       = @@sql_mode */ ;
+/*!50003 SET sql_mode              = 'STRICT_TRANS_TABLES,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION' */ ;
+/*!50003 DROP PROCEDURE IF EXISTS `retrieveEntityParents` */;
+/*!50003 SET @saved_cs_client      = @@character_set_client */ ;
+/*!50003 SET @saved_cs_results     = @@character_set_results */ ;
+/*!50003 SET @saved_col_connection = @@collation_connection */ ;
+/*!50003 SET character_set_client  = utf8 */ ;
+/*!50003 SET character_set_results = utf8 */ ;
+/*!50003 SET collation_connection  = utf8_general_ci */ ;
+DELIMITER ;;
+CREATE DEFINER=`root`@`%` PROCEDURE `retrieveEntityParents`(
+    in EntityID VARCHAR(255),
+    in Version VARBINARY(255))
+retrieveEntityParentsBody: BEGIN
+
+    DECLARE IVersion INT UNSIGNED DEFAULT NULL;
+    DECLARE IsHead BOOLEAN DEFAULT TRUE;
+    DECLARE InternalEntityID INT UNSIGNED DEFAULT NULL;
+
+    SELECT internal_id INTO InternalEntityID from entity_ids WHERE id = EntityID;
+
+    IF is_feature_config("ENTITY_VERSIONING", "ENABLED") THEN
+        IF Version IS NOT NULL THEN
+            SELECT get_head_version(EntityID) = Version INTO IsHead;
+        END IF;
+
+        IF IsHead IS FALSE THEN
+            SELECT e._iversion INTO IVersion
+                FROM entity_version as e
+                WHERE e.entity_id = InternalEntityID
+                AND e.version = Version;
+
+            IF IVersion IS NULL THEN
+                
+                LEAVE retrieveEntityParentsBody;
+            END IF;
+
+            SELECT
+                ( SELECT id FROM entity_ids WHERE internal_id = i.parent) AS ParentID,
+                ( SELECT value FROM name_data
+                    WHERE domain_id = 0
+                    AND entity_id = i.parent
+                    AND property_id = 20
+                ) AS ParentName, 
+                                 
+                                 
+                                 
+                                 
+                e.description AS ParentDescription,
+                e.role AS ParentRole,
+                (SELECT acl FROM entity_acl AS a WHERE a.id = e.acl) AS ACL
+                FROM archive_isa AS i JOIN entities AS e
+                    ON (i.parent = e.id)
+                WHERE i.child = InternalEntityID
+                AND i.child_iversion = IVersion
+                AND i.direct IS TRUE
+                ;
+
+            LEAVE retrieveEntityParentsBody;
+        END IF;
+    END IF;
+
+    SELECT
+        ( SELECT id FROM entity_ids WHERE internal_id = i.parent) AS ParentID,
+        ( SELECT value FROM name_data
+            WHERE domain_id = 0
+            AND entity_id = i.parent
+            AND property_id = 20 ) AS ParentName,
+        e.description AS ParentDescription,
+        e.role AS ParentRole,
+        (SELECT acl FROM entity_acl AS a WHERE a.id = e.acl) AS ACL
+        FROM isa_cache AS i JOIN entities AS e
+            ON (i.parent = e.id)
+        WHERE i.child = InternalEntityID
+        AND i.rpath = InternalEntityID;
+END ;;
+DELIMITER ;
+/*!50003 SET sql_mode              = @saved_sql_mode */ ;
+/*!50003 SET character_set_client  = @saved_cs_client */ ;
+/*!50003 SET character_set_results = @saved_cs_results */ ;
+/*!50003 SET collation_connection  = @saved_col_connection */ ;
+/*!50003 SET @saved_sql_mode       = @@sql_mode */ ;
+/*!50003 SET sql_mode              = 'STRICT_TRANS_TABLES,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION' */ ;
+/*!50003 DROP PROCEDURE IF EXISTS `retrieveEntityProperties` */;
+/*!50003 SET @saved_cs_client      = @@character_set_client */ ;
+/*!50003 SET @saved_cs_results     = @@character_set_results */ ;
+/*!50003 SET @saved_col_connection = @@collation_connection */ ;
+/*!50003 SET character_set_client  = utf8 */ ;
+/*!50003 SET character_set_results = utf8 */ ;
+/*!50003 SET collation_connection  = utf8_general_ci */ ;
+DELIMITER ;;
+CREATE DEFINER=`root`@`%` PROCEDURE `retrieveEntityProperties`(
+    in DomainID VARCHAR(255),
+    in EntityID VARCHAR(255),
+    in Version VARBINARY(255))
+retrieveEntityPropertiesBody: BEGIN
+
+    DECLARE IVersion INT UNSIGNED DEFAULT NULL;
+    DECLARE IsHead BOOLEAN DEFAULT TRUE;
+    DECLARE InternalEntityID INT UNSIGNED DEFAULT NULL;
+    DECLARE InternalDomainID INT UNSIGNED DEFAULT 0;
+
+    
+    
+    
+    IF LOCATE("$", EntityID) = 1 THEN
+        SET InternalEntityID=SUBSTRING(EntityID, 2);
+    ELSE
+        SELECT internal_id INTO InternalEntityID FROM entity_ids WHERE id = EntityID;
+    END IF;
+    
+    SELECT internal_id INTO InternalDomainID from entity_ids WHERE id = DomainID;
+
+    IF is_feature_config("ENTITY_VERSIONING", "ENABLED") THEN
+        IF Version IS NOT NULL THEN
+            IF InternalDomainID = 0 THEN
+                SELECT get_head_version(EntityID) = Version INTO IsHead;
+            ELSE
+                SELECT get_head_version(DomainID) = Version INTO IsHead;
+            END IF;
+        END IF;
+
+        IF IsHead IS FALSE THEN
+            SELECT e._iversion INTO IVersion
+                FROM entity_version as e
+                WHERE ((e.entity_id = InternalEntityID AND InternalDomainID = 0)
+                    OR (e.entity_id = InternalDomainID))
+                AND e.version = Version;
+
+            IF IVersion IS NULL THEN
+                
+                LEAVE retrieveEntityPropertiesBody;
+            END IF;
+
+            
+            SELECT
+                CONCAT("$", property_id) AS InternalPropertyID,
+                ( SELECT id FROM entity_ids WHERE internal_id = property_id ) AS PropertyID,
+                value AS PropertyValue,
+                status AS PropertyStatus,
+                pidx AS PropertyIndex
+            FROM archive_double_data
+            WHERE domain_id = InternalDomainID
+            AND entity_id = InternalEntityID
+            AND _iversion = IVersion
+
+            UNION ALL
+
+            
+            SELECT
+                CONCAT("$", property_id) AS InternalPropertyID,
+                ( SELECT id FROM entity_ids WHERE internal_id = property_id ) AS PropertyID,
+                value AS PropertyValue,
+                status AS PropertyStatus,
+                pidx AS PropertyIndex
+            FROM archive_integer_data
+            WHERE domain_id = InternalDomainID
+            AND entity_id = InternalEntityID
+            AND _iversion = IVersion
+
+            UNION ALL
+
+            
+            SELECT
+                CONCAT("$", property_id) AS InternalPropertyID,
+                ( SELECT id FROM entity_ids WHERE internal_id = property_id ) AS PropertyID,
+                CONCAT(value, '.NULL.NULL') AS PropertyValue,
+                status AS PropertyStatus,
+                pidx AS PropertyIndex
+            FROM archive_date_data
+            WHERE domain_id = InternalDomainID
+            AND entity_id = InternalEntityID
+            AND _iversion = IVersion
+
+            UNION ALL
+
+            
+            SELECT
+                CONCAT("$", property_id) AS InternalPropertyID,
+                ( SELECT id FROM entity_ids WHERE internal_id = property_id ) AS PropertyID,
+                CONCAT(value, 'UTC', IF(value_ns IS NULL, '', value_ns))
+                    AS PropertyValue,
+                status AS PropertyStatus,
+                pidx AS PropertyIndex
+            FROM archive_datetime_data
+            WHERE domain_id = InternalDomainID
+            AND entity_id = InternalEntityID
+            AND _iversion = IVersion
+
+            UNION ALL
+
+            
+            SELECT
+                CONCAT("$", property_id) AS InternalPropertyID,
+                ( SELECT id FROM entity_ids WHERE internal_id = property_id ) AS PropertyID,
+                value AS PropertyValue,
+                status AS PropertyStatus,
+                pidx AS PropertyIndex
+            FROM archive_text_data
+            WHERE domain_id = InternalDomainID
+            AND entity_id = InternalEntityID
+            AND _iversion = IVersion
+
+            UNION ALL
+
+            
+            SELECT
+                CONCAT("$", property_id) AS InternalPropertyID,
+                ( SELECT id FROM entity_ids WHERE internal_id = property_id ) AS PropertyID,
+                value AS PropertyValue,
+                status AS PropertyStatus,
+                pidx AS PropertyIndex
+            FROM archive_enum_data
+            WHERE domain_id = InternalDomainID
+            AND entity_id = InternalEntityID
+            AND _iversion = IVersion
+
+            UNION ALL
+
+            
+            SELECT
+                CONCAT("$", property_id) AS InternalPropertyID,
+                ( SELECT id FROM entity_ids WHERE internal_id = property_id ) AS PropertyID,
+                IF(value_iversion IS NULL,
+                    IF(status = "REPLACEMENT",
+                        CONCAT("$", value),
+                        ( SELECT id FROM entity_ids WHERE internal_id = value )),
+                    
+                    CONCAT(
+                        ( SELECT id FROM entity_ids WHERE internal_id = value ),
+                          "@", _get_version(value, value_iversion)))
+                    AS PropertyValue,
+                status AS PropertyStatus,
+                pidx AS PropertyIndex
+            FROM archive_reference_data
+            WHERE domain_id = InternalDomainID
+            AND entity_id = InternalEntityID
+            AND _iversion = IVersion
+
+            UNION ALL
+
+            
+            SELECT
+                CONCAT("$", property_id) AS InternalPropertyID,
+                ( SELECT id FROM entity_ids WHERE internal_id = property_id ) AS PropertyID,
+                NULL AS PropertyValue,
+                status AS PropertyStatus,
+                pidx AS PropertyIndex
+            FROM archive_null_data
+            WHERE domain_id = InternalDomainID
+            AND entity_id = InternalEntityID
+            AND _iversion = IVersion
+
+            UNION ALL
+
+            
+            SELECT
+                CONCAT("$", property_id) AS InternalPropertyID,
+                ( SELECT id FROM entity_ids WHERE internal_id = property_id ) AS PropertyID,
+                value AS PropertyValue,
+                status AS PropertyStatus,
+                pidx AS PropertyIndex
+            FROM archive_name_data
+            WHERE domain_id = InternalDomainID
+            AND entity_id = InternalEntityID
+            AND property_id != 20
+            AND _iversion = IVersion;
+
+            LEAVE retrieveEntityPropertiesBody;
+        END IF;
+    END IF;
+
+    
+    SELECT
+        CONCAT("$", property_id) AS InternalPropertyID,
+        ( SELECT id FROM entity_ids WHERE internal_id = property_id ) AS PropertyID,
+        value AS PropertyValue,
+        status AS PropertyStatus,
+        pidx AS PropertyIndex
+    FROM double_data
+    WHERE domain_id = InternalDomainID
+    AND entity_id = InternalEntityID
+
+    UNION ALL
+
+    
+    SELECT
+        CONCAT("$", property_id) AS InternalPropertyID,
+        ( SELECT id FROM entity_ids WHERE internal_id = property_id ) AS PropertyID,
+        value AS PropertyValue,
+        status AS PropertyStatus,
+        pidx AS PropertyIndex
+    FROM integer_data
+    WHERE domain_id = InternalDomainID
+    AND entity_id = InternalEntityID
+
+    UNION ALL
+
+    
+    SELECT
+        CONCAT("$", property_id) AS InternalPropertyID,
+        ( SELECT id FROM entity_ids WHERE internal_id = property_id ) AS PropertyID,
+        CONCAT(value, '.NULL.NULL') AS PropertyValue,
+        status AS PropertyStatus,
+        pidx AS PropertyIndex
+    FROM date_data
+    WHERE domain_id = InternalDomainID
+    AND entity_id = InternalEntityID
+
+    UNION ALL
+
+    
+    SELECT
+        CONCAT("$", property_id) AS InternalPropertyID,
+        ( SELECT id FROM entity_ids WHERE internal_id = property_id ) AS PropertyID,
+        CONCAT(value, 'UTC', IF(value_ns IS NULL, '', value_ns))
+            AS PropertyValue,
+        status AS PropertyStatus,
+        pidx AS PropertyIndex
+    FROM datetime_data
+    WHERE domain_id = InternalDomainID
+    AND entity_id = InternalEntityID
+
+    UNION ALL
+
+    
+    SELECT
+        CONCAT("$", property_id) AS InternalPropertyID,
+        ( SELECT id FROM entity_ids WHERE internal_id = property_id ) AS PropertyID,
+        value AS PropertyValue,
+        status AS PropertyStatus,
+        pidx AS PropertyIndex
+    FROM text_data
+    WHERE domain_id = InternalDomainID
+    AND entity_id = InternalEntityID
+
+    UNION ALL
+
+    
+    SELECT
+        CONCAT("$", property_id) AS InternalPropertyID,
+        ( SELECT id FROM entity_ids WHERE internal_id = property_id ) AS PropertyID,
+        value AS PropertyValue,
+        status AS PropertyStatus,
+        pidx AS PropertyIndex
+    FROM enum_data
+    WHERE domain_id = InternalDomainID
+    AND entity_id = InternalEntityID
+
+    UNION ALL
+
+    
+    SELECT
+        CONCAT("$", property_id) AS InternalPropertyID,
+        ( SELECT id FROM entity_ids WHERE internal_id = property_id ) AS PropertyID,
+        IF(value_iversion IS NULL,
+            IF(status = "REPLACEMENT",
+                CONCAT("$", value),
+                ( SELECT id FROM entity_ids WHERE internal_id = value )),
+            
+            CONCAT(
+                ( SELECT id FROM entity_ids WHERE internal_id = value ),
+                  "@", _get_version(value, value_iversion)))
+            AS PropertyValue,
+        status AS PropertyStatus,
+        pidx AS PropertyIndex
+    FROM reference_data
+    WHERE domain_id = InternalDomainID
+    AND entity_id = InternalEntityID
+
+    UNION ALL
+
+    
+    SELECT
+        CONCAT("$", property_id) AS InternalPropertyID,
+        ( SELECT id FROM entity_ids WHERE internal_id = property_id ) AS PropertyID,
+        NULL AS PropertyValue,
+        status AS PropertyStatus,
+        pidx AS PropertyIndex
+    FROM null_data
+    WHERE domain_id = InternalDomainID
+    AND entity_id = InternalEntityID
+
+    UNION ALL
+
+    
+    SELECT
+        CONCAT("$", property_id) AS InternalPropertyID,
+        ( SELECT id FROM entity_ids WHERE internal_id = property_id ) AS PropertyID,
+        value AS PropertyValue,
+        status AS PropertyStatus,
+        pidx AS PropertyIndex
+    FROM name_data
+    WHERE domain_id = InternalDomainID
+    AND entity_id = InternalEntityID
+    AND property_id != 20;
+
+END ;;
+DELIMITER ;
+/*!50003 SET sql_mode              = @saved_sql_mode */ ;
+/*!50003 SET character_set_client  = @saved_cs_client */ ;
+/*!50003 SET character_set_results = @saved_cs_results */ ;
+/*!50003 SET collation_connection  = @saved_col_connection */ ;
+/*!50003 SET @saved_sql_mode       = @@sql_mode */ ;
+/*!50003 SET sql_mode              = 'STRICT_TRANS_TABLES,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION' */ ;
+/*!50003 DROP PROCEDURE IF EXISTS `retrieveOverrides` */;
+/*!50003 SET @saved_cs_client      = @@character_set_client */ ;
+/*!50003 SET @saved_cs_results     = @@character_set_results */ ;
+/*!50003 SET @saved_col_connection = @@collation_connection */ ;
+/*!50003 SET character_set_client  = utf8 */ ;
+/*!50003 SET character_set_results = utf8 */ ;
+/*!50003 SET collation_connection  = utf8_general_ci */ ;
+DELIMITER ;;
+CREATE DEFINER=`root`@`%` PROCEDURE `retrieveOverrides`(
+    in DomainID VARCHAR(255),
+    in EntityID VARCHAR(255),
+    in Version VARBINARY(255))
+retrieveOverridesBody: BEGIN
+
+    DECLARE IVersion INT UNSIGNED DEFAULT NULL;
+    DECLARE IsHead BOOLEAN DEFAULT TRUE;
+    DECLARE InternalEntityID INT UNSIGNED DEFAULT NULL;
+    DECLARE InternalDomainID INT UNSIGNED DEFAULT 0;
+
+    
+    
+    
+    IF LOCATE("$", EntityID) = 1 THEN
+        SET InternalEntityID=SUBSTRING(EntityID, 2);
+    ELSE
+        SELECT internal_id INTO InternalEntityID FROM entity_ids WHERE id = EntityID;
+    END IF;
+    
+    SELECT internal_id INTO InternalDomainID from entity_ids WHERE id = DomainID;
+
+    IF is_feature_config("ENTITY_VERSIONING", "ENABLED") THEN
+        IF Version IS NOT NULL THEN
+            IF InternalDomainID = 0 THEN
+                SELECT get_head_version(EntityID) = Version INTO IsHead;
+            ELSE
+                SELECT get_head_version(DomainID) = Version INTO IsHead;
+            END IF;
+        END IF;
+
+        IF IsHead IS FALSE THEN
+            SELECT e._iversion INTO IVersion
+                FROM entity_version as e
+                WHERE ((e.entity_id = InternalEntityID AND InternalDomainID = 0)
+                    OR (e.entity_id = InternalDomainID))
+                AND e.version = Version;
+
+            IF IVersion IS NULL THEN
+                
+                LEAVE retrieveOverridesBody;
+            END IF;
+
+            
+            SELECT
+                NULL AS collection_override,
+                name AS name_override,
+                NULL AS desc_override,
+                NULL AS type_name_override,
+                NULL AS type_id_override,
+                EntityID AS entity_id,
+                CONCAT("$", property_id) AS InternalPropertyID,
+                ( SELECT id FROM entity_ids WHERE internal_id = property_id ) AS property_id
+            FROM archive_name_overrides
+            WHERE domain_id = InternalDomainID
+            AND entity_id = InternalEntityID
+            AND _iversion = IVersion
+
+            UNION ALL
+
+            
+            SELECT
+                NULL AS collection_override,
+                NULL AS name_override,
+                description AS desc_override,
+                NULL AS type_name_override,
+                NULL AS type_id_override,
+                EntityID AS entity_id,
+                CONCAT("$", property_id) AS InternalPropertyID,
+                ( SELECT id FROM entity_ids WHERE internal_id = property_id ) AS property_id
+            FROM archive_desc_overrides
+            WHERE domain_id = InternalDomainID
+            AND entity_id = InternalEntityID
+            AND _iversion = IVersion
+
+            UNION ALL
+
+            
+            SELECT
+                NULL AS collection_override,
+                NULL AS name_override,
+                NULL AS desc_override,
+                (SELECT value FROM name_data
+                    WHERE domain_id = 0
+                    AND entity_id = datatype
+                    AND property_id = 20
+                    LIMIT 1) AS type_name_override,
+                (SELECT id FROM entity_ids WHERE internal_id = datatype) AS type_id_override,
+                EntityID AS entity_id,
+                CONCAT("$", property_id) AS InternalPropertyID,
+                ( SELECT id FROM entity_ids WHERE internal_id = property_id ) AS property_id
+            FROM archive_data_type
+            WHERE domain_id = InternalDomainID
+            AND entity_id = InternalEntityID
+            AND _iversion = IVersion
+
+            UNION ALL
+
+            
+            SELECT
+                collection AS collection_override,
+                NULL AS name_override,
+                NULL AS desc_override,
+                NULL AS type_name_override,
+                NULL AS type_id_override,
+                EntityID AS entity_id,
+                CONCAT("$", property_id) AS InternalPropertyID,
+                ( SELECT id FROM entity_ids WHERE internal_id = property_id ) AS property_id
+            FROM archive_collection_type
+            WHERE domain_id = InternalDomainID
+            AND entity_id = InternalEntityID
+            AND _iversion = IVersion;
+
+            LEAVE retrieveOverridesBody;
+        END IF;
+    END IF;
+
+    SELECT
+        NULL AS collection_override,
+        name AS name_override,
+        NULL AS desc_override,
+        NULL AS type_name_override,
+        NULL AS type_id_override,
+        EntityID AS entity_id,
+        CONCAT("$", property_id) AS InternalPropertyID,
+        ( SELECT id FROM entity_ids WHERE internal_id = property_id ) AS property_id
+    FROM name_overrides
+    WHERE domain_id = InternalDomainID
+    AND entity_id = InternalEntityID
+
+    UNION ALL
+
+    SELECT
+        NULL AS collection_override,
+        NULL AS name_override,
+        description AS desc_override,
+        NULL AS type_name_override,
+        NULL AS type_id_override,
+        EntityID AS entity_id,
+        CONCAT("$", property_id) AS InternalPropertyID,
+        ( SELECT id FROM entity_ids WHERE internal_id = property_id ) AS property_id
+    FROM desc_overrides
+    WHERE domain_id = InternalDomainID
+    AND entity_id = InternalEntityID
+
+    UNION ALL
+
+    SELECT
+        NULL AS collection_override,
+        NULL AS name_override,
+        NULL AS desc_override,
+        (SELECT value FROM name_data
+            WHERE domain_id = 0
+            AND entity_ID = datatype
+            AND property_id = 20 LIMIT 1) AS type_name_override,
+        (SELECT id FROM entity_ids WHERE internal_id = datatype) AS type_id_override,
+        EntityID AS entity_id,
+        CONCAT("$", property_id) AS InternalPropertyID,
+        ( SELECT id FROM entity_ids WHERE internal_id = property_id ) AS property_id
+    FROM data_type
+    WHERE domain_id = InternalDomainID
+    AND entity_id = InternalEntityID
+
+    UNION ALL
+
+    SELECT
+        collection AS collection_override,
+        NULL AS name_override,
+        NULL AS desc_override,
+        NULL AS type_name_override,
+        NULL AS type_id_override,
+        EntityID AS entity_id,
+        CONCAT("$", property_id) AS InternalPropertyID,
+        ( SELECT id FROM entity_ids WHERE internal_id = property_id ) AS property_id
+    FROM collection_type
+    WHERE domain_id = InternalDomainID
+    AND entity_id = InternalEntityID;
+
+
+END ;;
+DELIMITER ;
+/*!50003 SET sql_mode              = @saved_sql_mode */ ;
+/*!50003 SET character_set_client  = @saved_cs_client */ ;
+/*!50003 SET character_set_results = @saved_cs_results */ ;
+/*!50003 SET collation_connection  = @saved_col_connection */ ;
+/*!50003 SET @saved_sql_mode       = @@sql_mode */ ;
+/*!50003 SET sql_mode              = 'STRICT_TRANS_TABLES,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION' */ ;
+/*!50003 DROP PROCEDURE IF EXISTS `retrieveQueryTemplateDef` */;
+/*!50003 SET @saved_cs_client      = @@character_set_client */ ;
+/*!50003 SET @saved_cs_results     = @@character_set_results */ ;
+/*!50003 SET @saved_col_connection = @@collation_connection */ ;
+/*!50003 SET character_set_client  = utf8 */ ;
+/*!50003 SET character_set_results = utf8 */ ;
+/*!50003 SET collation_connection  = utf8_general_ci */ ;
+DELIMITER ;;
+CREATE DEFINER=`root`@`%` PROCEDURE `retrieveQueryTemplateDef`(
+    in EntityID VARCHAR(255),
+    in Version VARBINARY(255))
+retrieveQueryTemplateDefBody: BEGIN
+
+    DECLARE IVersion INT UNSIGNED DEFAULT NULL;
+    DECLARE IsHead BOOLEAN DEFAULT TRUE;
+    DECLARE InternalEntityID INT UNSIGNED DEFAULT NULL;
+
+    SELECT internal_id INTO InternalEntityID FROM entity_ids WHERE id = EntityID;
+
+
+    IF is_feature_config("ENTITY_VERSIONING", "ENABLED") THEN
+        
+        IF Version IS NOT NULL THEN
+            SELECT get_head_version(EntityID) = Version INTO IsHead;
+        END IF;
+
+        IF IsHead IS FALSE THEN
+            SET IVersion = get_iversion(InternalEntityID, Version);
+
+            IF IVersion IS NULL THEN
+                
+                LEAVE retrieveQueryTemplateDefBody;
+            END IF;
+
+            SELECT definition
+            FROM archive_query_template_def
+            WHERE id = InternalEntityID
+            AND _iversion = IVersion;
+
+            LEAVE retrieveQueryTemplateDefBody;
+        END IF;
+    END IF;
+
+    SELECT definition
+    FROM query_template_def
+    WHERE id = InternalEntityID;
+
+END ;;
+DELIMITER ;
+/*!50003 SET sql_mode              = @saved_sql_mode */ ;
+/*!50003 SET character_set_client  = @saved_cs_client */ ;
+/*!50003 SET character_set_results = @saved_cs_results */ ;
+/*!50003 SET collation_connection  = @saved_col_connection */ ;
+/*!50003 SET @saved_sql_mode       = @@sql_mode */ ;
+/*!50003 SET sql_mode              = 'STRICT_TRANS_TABLES,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION' */ ;
+/*!50003 DROP PROCEDURE IF EXISTS `setFileProperties` */;
+/*!50003 SET @saved_cs_client      = @@character_set_client */ ;
+/*!50003 SET @saved_cs_results     = @@character_set_results */ ;
+/*!50003 SET @saved_col_connection = @@collation_connection */ ;
+/*!50003 SET character_set_client  = utf8 */ ;
+/*!50003 SET character_set_results = utf8 */ ;
+/*!50003 SET collation_connection  = utf8_general_ci */ ;
+DELIMITER ;;
+CREATE DEFINER=`root`@`%` PROCEDURE `setFileProperties`(
+    in EntityID VARCHAR(255),
+    in FilePath TEXT,
+    in FileSize BIGINT UNSIGNED,
+    in FileHash VARCHAR(255)
+)
+BEGIN
+    DECLARE InternalEntityID INT UNSIGNED DEFAULT NULL;
+    DECLARE IVersion INT UNSIGNED DEFAULT NULL;
+
+    SELECT internal_id INTO InternalEntityID FROM entity_ids WHERE id = EntityID;
+
+    IF is_feature_config("ENTITY_VERSIONING", "ENABLED") THEN
+        SELECT max(e._iversion) INTO IVersion
+            FROM entity_version AS e
+            WHERE e.entity_id = InternalEntityID;
+
+        INSERT INTO archive_files (file_id, path, size, hash,
+                _iversion)
+            SELECT file_id, path, size, hash, IVersion AS _iversion
+            FROM files
+            WHERE file_id = InternalEntityID;
+    END IF;
+
+    DELETE FROM files WHERE file_id = InternalEntityID;
+
+    IF FilePath IS NOT NULL THEN
+        INSERT INTO files (file_id, path, size, hash)
+            VALUES (InternalEntityID, FilePath, FileSize, unhex(FileHash));
+    END IF;
+
+END ;;
+DELIMITER ;
+/*!50003 SET sql_mode              = @saved_sql_mode */ ;
+/*!50003 SET character_set_client  = @saved_cs_client */ ;
+/*!50003 SET character_set_results = @saved_cs_results */ ;
+/*!50003 SET collation_connection  = @saved_col_connection */ ;
+/*!50003 SET @saved_sql_mode       = @@sql_mode */ ;
+/*!50003 SET sql_mode              = 'STRICT_TRANS_TABLES,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION' */ ;
+/*!50003 DROP PROCEDURE IF EXISTS `set_transaction` */;
+/*!50003 SET @saved_cs_client      = @@character_set_client */ ;
+/*!50003 SET @saved_cs_results     = @@character_set_results */ ;
+/*!50003 SET @saved_col_connection = @@collation_connection */ ;
+/*!50003 SET character_set_client  = utf8 */ ;
+/*!50003 SET character_set_results = utf8 */ ;
+/*!50003 SET collation_connection  = utf8_general_ci */ ;
+DELIMITER ;;
+CREATE DEFINER=`root`@`%` PROCEDURE `set_transaction`(
+    srid VARBINARY(255),
+    username VARCHAR(255),
+    realm VARCHAR(255),
+    seconds BIGINT UNSIGNED,
+    nanos INT(10) UNSIGNED)
+BEGIN
+
+    SET @SRID = srid;  
+    INSERT INTO transactions (srid, username, realm, seconds, nanos)
+        VALUES (srid, username, realm, seconds, nanos);
+END ;;
+DELIMITER ;
+/*!50003 SET sql_mode              = @saved_sql_mode */ ;
+/*!50003 SET character_set_client  = @saved_cs_client */ ;
+/*!50003 SET character_set_results = @saved_cs_results */ ;
+/*!50003 SET collation_connection  = @saved_col_connection */ ;
+/*!50003 SET @saved_sql_mode       = @@sql_mode */ ;
+/*!50003 SET sql_mode              = 'STRICT_TRANS_TABLES,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION' */ ;
+/*!50003 DROP PROCEDURE IF EXISTS `showEntityAutoIncr` */;
+/*!50003 SET @saved_cs_client      = @@character_set_client */ ;
+/*!50003 SET @saved_cs_results     = @@character_set_results */ ;
+/*!50003 SET @saved_col_connection = @@collation_connection */ ;
+/*!50003 SET character_set_client  = utf8 */ ;
+/*!50003 SET character_set_results = utf8 */ ;
+/*!50003 SET collation_connection  = utf8_general_ci */ ;
+DELIMITER ;;
+CREATE DEFINER=`root`@`%` PROCEDURE `showEntityAutoIncr`()
+BEGIN
+SELECT `AUTO_INCREMENT`
+FROM  INFORMATION_SCHEMA.TABLES
+WHERE TABLE_SCHEMA = 'caosdb'
+AND   TABLE_NAME   = 'entities';
+
+
+END ;;
+DELIMITER ;
+/*!50003 SET sql_mode              = @saved_sql_mode */ ;
+/*!50003 SET character_set_client  = @saved_cs_client */ ;
+/*!50003 SET character_set_results = @saved_cs_results */ ;
+/*!50003 SET collation_connection  = @saved_col_connection */ ;
+/*!50003 SET @saved_sql_mode       = @@sql_mode */ ;
+/*!50003 SET sql_mode              = 'STRICT_TRANS_TABLES,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION' */ ;
+/*!50003 DROP PROCEDURE IF EXISTS `updateEntity` */;
+/*!50003 SET @saved_cs_client      = @@character_set_client */ ;
+/*!50003 SET @saved_cs_results     = @@character_set_results */ ;
+/*!50003 SET @saved_col_connection = @@collation_connection */ ;
+/*!50003 SET character_set_client  = utf8 */ ;
+/*!50003 SET character_set_results = utf8 */ ;
+/*!50003 SET collation_connection  = utf8_general_ci */ ;
+DELIMITER ;;
+CREATE DEFINER=`root`@`%` PROCEDURE `updateEntity`(
+    in EntityID VARCHAR(255),
+    in EntityName VARCHAR(255),
+    in EntityDescription TEXT,
+    in EntityRole VARCHAR(255),
+    in DatatypeID VARCHAR(255),
+    in Collection VARCHAR(255),
+    in ACL VARBINARY(65525))
+BEGIN
+    DECLARE ACLID INT UNSIGNED DEFAULT NULL;
+    DECLARE Hash VARBINARY(255) DEFAULT NULL;
+    DECLARE Version VARBINARY(255) DEFAULT SHA1(UUID());
+    DECLARE ParentVersion VARBINARY(255) DEFAULT NULL;
+    DECLARE Transaction VARBINARY(255) DEFAULT NULL;
+    DECLARE OldIVersion INT UNSIGNED DEFAULT NULL;
+    DECLARE InternalEntityID INT UNSIGNED DEFAULT NULL;
+
+    SELECT internal_id INTO InternalEntityID from entity_ids WHERE id = EntityID;
+
+    call entityACL(ACLID, ACL);
+
+    IF is_feature_config("ENTITY_VERSIONING", "ENABLED") THEN
+        SELECT max(_iversion) INTO OldIVersion
+            FROM entity_version
+            WHERE entity_id = InternalEntityID;
+
+        
+        INSERT INTO archive_entities (id, description, role,
+                acl, _iversion)
+            SELECT e.id, e.description, e.role, e.acl, OldIVersion
+            FROM entities AS e
+            WHERE e.id = InternalEntityID;
+
+        INSERT INTO archive_data_type (domain_id, entity_id, property_id,
+                datatype, _iversion)
+            SELECT e.domain_id, e.entity_id, e.property_id, e.datatype,
+                OldIVersion
+            FROM data_type AS e
+            WHERE e.domain_id = 0
+            AND e.entity_id = 0
+            AND e.property_id = InternalEntityID;
+
+        INSERT INTO archive_collection_type (domain_id, entity_id, property_id,
+                collection, _iversion)
+            SELECT e.domain_id, e.entity_id, e.property_id, e.collection,
+                OldIVersion
+            FROM collection_type as e
+            WHERE e.domain_id = 0
+            AND e.entity_id = 0
+            AND e.property_id = InternalEntityID;
+
+
+        SET Transaction = @SRID;
+        SELECT e.version INTO ParentVersion
+            FROM entity_version as e
+            WHERE e.entity_id = InternalEntityID
+            AND e._iversion = OldIVersion;
+        CALL insert_single_child_version(
+            InternalEntityID, Hash, Version,
+            ParentVersion, Transaction);
+    END IF;
+
+    UPDATE entities e
+        SET e.description = EntityDescription,
+            e.role=EntityRole,
+            e.acl = ACLID
+        WHERE e.id = InternalEntityID;
+
+    
+    
+    DELETE FROM name_data
+        WHERE domain_id = 0 AND entity_id = InternalEntityID AND property_id = 20;
+    IF EntityName IS NOT NULL THEN
+        INSERT INTO name_data
+                (domain_id, entity_id, property_id, value, status, pidx)
+            VALUES (0, InternalEntityID, 20, EntityName, "FIX", 0);
+    END IF;
+
+    DELETE FROM data_type
+        WHERE domain_id=0 AND entity_id=0 AND property_id=InternalEntityID;
+
+    DELETE FROM collection_type
+        WHERE domain_id=0 AND entity_id=0 AND property_id=InternalEntityID;
+
+    IF DatatypeID IS NOT NULL THEN
+        INSERT INTO data_type (domain_id, entity_id, property_id, datatype)
+            SELECT 0, 0, InternalEntityID,
+                ( SELECT internal_id FROM entity_ids WHERE id = DatatypeID );
+
+        IF Collection IS NOT NULL THEN
+            INSERT INTO collection_type (domain_id, entity_id, property_id,
+                    collection)
+                SELECT 0, 0, InternalEntityID, Collection;
+        END IF;
+    END IF;
+
+    Select Version as Version;
+
+END ;;
+DELIMITER ;
+/*!50003 SET sql_mode              = @saved_sql_mode */ ;
+/*!50003 SET character_set_client  = @saved_cs_client */ ;
+/*!50003 SET character_set_results = @saved_cs_results */ ;
+/*!50003 SET collation_connection  = @saved_col_connection */ ;
+/*!50003 SET @saved_sql_mode       = @@sql_mode */ ;
+/*!50003 SET sql_mode              = 'STRICT_TRANS_TABLES,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION' */ ;
+/*!50003 DROP PROCEDURE IF EXISTS `updateLinCon` */;
+/*!50003 SET @saved_cs_client      = @@character_set_client */ ;
+/*!50003 SET @saved_cs_results     = @@character_set_results */ ;
+/*!50003 SET @saved_col_connection = @@collation_connection */ ;
+/*!50003 SET character_set_client  = utf8 */ ;
+/*!50003 SET character_set_results = utf8 */ ;
+/*!50003 SET collation_connection  = utf8_general_ci */ ;
+DELIMITER ;;
+CREATE DEFINER=`root`@`%` PROCEDURE `updateLinCon`(in sig_from BIGINT, in sig_to BIGINT, in new_a DECIMAL(65,30), in new_b_dividend BIGINT, in new_b_divisor BIGINT, in new_c DECIMAL(65,30))
+BEGIN
+    UPDATE units_lin_con SET signature_to=sig_to, a=new_a, b_dividend=new_b_dividend, b_divisor=new_b_divisor, c=new_c where signature_from=sig_from;
+
+END ;;
+DELIMITER ;
+/*!50003 SET sql_mode              = @saved_sql_mode */ ;
+/*!50003 SET character_set_client  = @saved_cs_client */ ;
+/*!50003 SET character_set_results = @saved_cs_results */ ;
+/*!50003 SET collation_connection  = @saved_col_connection */ ;
+/*!40103 SET TIME_ZONE=@OLD_TIME_ZONE */;
+
+/*!40101 SET SQL_MODE=@OLD_SQL_MODE */;
+/*!40014 SET FOREIGN_KEY_CHECKS=@OLD_FOREIGN_KEY_CHECKS */;
+/*!40014 SET UNIQUE_CHECKS=@OLD_UNIQUE_CHECKS */;
+/*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */;
+/*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */;
+/*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */;
+/*!40111 SET SQL_NOTES=@OLD_SQL_NOTES */;
+
+-- Dump completed on 2024-10-02  9:54:57
diff --git a/integrationtests/test_profile/paths/extroot/README.md b/integrationtests/test_profile/paths/extroot/README.md
new file mode 100644
index 0000000000000000000000000000000000000000..ee741757ce62d03d49358a7245faf44b20e6cd60
--- /dev/null
+++ b/integrationtests/test_profile/paths/extroot/README.md
@@ -0,0 +1,2 @@
+This directory is mounted into the LinkAhead docker container when the debug
+profile is used, to allow the inclusion of external file systems.
diff --git a/integrationtests/test_profile/profile.yaml b/integrationtests/test_profile/profile.yaml
new file mode 100644
index 0000000000000000000000000000000000000000..82f95b112ba040b91c960bac901d3eb9bf7b1085
--- /dev/null
+++ b/integrationtests/test_profile/profile.yaml
@@ -0,0 +1,29 @@
+default:
+  paths:
+    extroot:
+      "": "../extroot"
+
+  refs:
+    # SERVER: dev
+    # PYLIB: dev
+    # MYSQLBACKEND: dev
+    # WEBUI: dev
+    # ADVANCEDUSERTOOLS: dev
+
+  # General configuration options
+  conf:
+    restore: true
+    debug: true
+    timezone: "Cuba"
+
+    network:
+
+    server:
+      conf:
+        _CAOSDB_INTEGRATION_TEST_SUITE_KEY: "_CAOSDB_ADV_TEST_SUITE"
+        TRANSACTION_BENCHMARK_ENABLED: "TRUE"
+        # SERVER_SIDE_SCRIPTING_BIN_DIRS: ""
+
+  # Development configuration options
+  # devel:
+    # jar: /var/build/caosdb-server/0123abcd/target/caosdb-server-<version>-jar-with-dependencies.jar
diff --git a/setup.py b/setup.py
index aa8ced3dde24399ee4652d978f011c8c0ccc01ef..2f257286a19275a228d97dd1efd552afc18ee487 100755
--- a/setup.py
+++ b/setup.py
@@ -155,7 +155,7 @@ def setup_package():
         author='Henrik tom Wörden',
         author_email='h.tomwoerden@indiscale.com',
         python_requires='>=3.9',
-        install_requires=["linkahead>=0.13.1",
+        install_requires=["linkahead>0.17.0",
                           "jsonref",
                           "jsonschema[format]>=4.4.0",
                           "numpy>=1.24.0, < 2",
diff --git a/src/caosadvancedtools/json_schema_exporter.py b/src/caosadvancedtools/json_schema_exporter.py
index 56568ca18eb10f501fa13bc766613367050c034d..2b4a977f8b5e8b0199a7554ab2435f3c6622dda1 100644
--- a/src/caosadvancedtools/json_schema_exporter.py
+++ b/src/caosadvancedtools/json_schema_exporter.py
@@ -55,11 +55,12 @@ single schema.
 """
 
 from collections import OrderedDict
-from typing import Any, Dict, Iterable, List, Optional, Sequence, Tuple, Union
+from typing import Any, Iterable, Optional, Sequence, Union
 
 import linkahead as db
 from linkahead.cached import cache_clear, cached_query
 from linkahead.common.datatype import get_list_datatype, is_list_datatype
+from linkahead.utils.get_entity import get_entity_by_name
 
 from .models.data_model import DataModel
 
@@ -70,16 +71,18 @@ class JsonSchemaExporter:
 
     def __init__(self, additional_properties: bool = True,
                  name_property_for_new_records: bool = False,
+                 use_id_for_identification: bool = False,
                  description_property_for_new_records: bool = False,
-                 additional_options_for_text_props: dict = None,
-                 additional_json_schema: Dict[str, dict] = None,
-                 additional_ui_schema: Dict[str, dict] = None,
+                 additional_options_for_text_props: Optional[dict] = None,
+                 additional_json_schema: Optional[dict[str, dict]] = None,
+                 additional_ui_schema: Optional[dict[str, dict]] = None,
                  units_in_description: bool = True,
-                 do_not_create: List[str] = None,
-                 do_not_retrieve: List[str] = None,
+                 do_not_create: Optional[list[str]] = None,
+                 do_not_retrieve: Optional[Union[list[str], str]] = None,
                  no_remote: bool = False,
-                 use_rt_pool: DataModel = None,
-                 multiple_choice: List[str] = None,
+                 use_rt_pool: Optional[DataModel] = None,
+                 multiple_choice: Optional[list[str]] = None,
+                 multiple_choice_guess: bool = False,
                  wrap_files_in_objects: bool = False,
                  ):
         """Set up a JsonSchemaExporter, which can then be applied on RecordTypes.
@@ -92,6 +95,9 @@ class JsonSchemaExporter:
         name_property_for_new_records : bool, optional
             Whether objects shall generally have a `name` property in the generated schema.
             Optional, default is False.
+        use_id_for_identification: bool, optional
+            If set to true, an 'id' property is added to all records, and
+            foreign key references are assumed to be ids.
         description_property_for_new_records : bool, optional
             Whether objects shall generally have a `description` property in the generated schema.
             Optional, default is False.
@@ -111,21 +117,27 @@ class JsonSchemaExporter:
             A list of reference Property names, for which there should be no option
             to create them.  Instead, only the choice of existing elements should
             be given.
-        do_not_retrieve : list[str], optional
+        do_not_retrieve : list[str] or str, optional
             A list of RecordType names, for which no Records shall be retrieved.  Instead, only an
             object description should be given.  If this list overlaps with the `do_not_create`
             parameter, the behavior is undefined.
+            If this parameter is the string "``auto``", only multiple choice references (see
+            parameter ``multiple_choice``) will be retrieved.
+            The default is the empty list.
         no_remote : bool, optional
             If True, do not attempt to connect to a LinkAhead server at all. Default is False. Note
             that the exporter may fail if this option is activated and the data model is not
             self-sufficient.
         use_rt_pool : models.data_model.DataModel, optional
-            If given, do not attempt to retrieve RecordType information remotely but from this parameter
-            instead.
+            If given, do not attempt to retrieve RecordType information remotely but from this
+            parameter instead.
         multiple_choice : list[str], optional
             A list of reference Property names which shall be denoted as multiple choice properties.
             This means that each option in this property may be selected at most once.  This is not
             implemented yet if the Property is not in ``do_not_create`` as well.
+        multiple_choice_guess : bool, default=False
+            If True, try to guess for all reference Properties that are not in ``multiple_choice``
+            if they are enum-like and thus should be handled as multiple choice.
         wrap_files_in_objects : bool, optional
             Whether (lists of) files should be wrapped into an array of objects
             that have a file property. The sole purpose of this wrapping is to
@@ -133,6 +145,15 @@ class JsonSchemaExporter:
             bug<https://github.com/rjsf-team/react-jsonschema-form/issues/3957>`_
             so only set this to True if you're using the exported schema with
             react-json-form and you are experiencing the bug. Default is False.
+
+        Notes on reference properties
+        -----------------------------
+
+        List references will have the "uniqueItems" property set if:
+
+        - ``do_not_retrieve`` is not set for the referenced RecordType
+        - ``multiple_choice`` is true or guessed to be true (if ``multiple_choice_guess`` is set)
+
         """
         if not additional_options_for_text_props:
             additional_options_for_text_props = {}
@@ -151,6 +172,7 @@ class JsonSchemaExporter:
 
         self._additional_properties = additional_properties
         self._name_property_for_new_records = name_property_for_new_records
+        self._use_id_for_identification = use_id_for_identification
         self._description_property_for_new_records = description_property_for_new_records
         self._additional_options_for_text_props = additional_options_for_text_props
         self._additional_json_schema = additional_json_schema
@@ -161,6 +183,7 @@ class JsonSchemaExporter:
         self._no_remote = no_remote
         self._use_rt_pool = use_rt_pool
         self._multiple_choice = multiple_choice
+        self._multiple_choice_guess = multiple_choice_guess
         self._wrap_files_in_objects = wrap_files_in_objects
 
     @staticmethod
@@ -175,7 +198,8 @@ class JsonSchemaExporter:
 
         return required_list
 
-    def _make_segment_from_prop(self, prop: db.Property) -> Tuple[OrderedDict, dict]:
+    def _make_segment_from_prop(self, prop: db.Property, multiple_choice_enforce: bool = False
+                                ) -> tuple[OrderedDict, dict]:
         """Return the JSON Schema and ui schema segments for the given property.
 
 The result may either be a simple json schema segment, such as a `string
@@ -189,6 +213,9 @@ Parameters
 prop : db.Property
     The property to be transformed.
 
+multiple_choice_enforce : bool, default=False
+    If True, this property shall be handled as multiple choice items.
+
 Returns
 -------
 
@@ -235,19 +262,31 @@ ui_schema : dict
             json_prop["type"] = "integer"
         elif prop.datatype == db.DOUBLE:
             json_prop["type"] = "number"
+        # list-valued non-files
         elif is_list_datatype(prop.datatype) and not (
-                self._wrap_files_in_objects and get_list_datatype(prop.datatype,
-                                                                  strict=True) == db.FILE):
+                self._wrap_files_in_objects
+                and get_list_datatype(prop.datatype, strict=True) == db.FILE):
             json_prop["type"] = "array"
             list_element_prop = db.Property(
                 name=prop.name, datatype=get_list_datatype(prop.datatype, strict=True))
-            json_prop["items"], inner_ui_schema = self._make_segment_from_prop(list_element_prop)
+
+            # Is this a multiple choice array?
+            multiple_choice = prop.name in self._multiple_choice
+            # breakpoint()
+            if (not multiple_choice and self._multiple_choice_guess
+                    and db.common.datatype.is_reference(list_element_prop.datatype)):
+                multiple_choice = self._guess_recordtype_is_enum(list_element_prop.datatype)
+            # breakpoint()
+
+            # Get inner content of list
+            json_prop["items"], inner_ui_schema = self._make_segment_from_prop(
+                list_element_prop, multiple_choice_enforce=multiple_choice)
             if "type" in json_prop["items"] and (
                     json_prop["items"]["type"] in ["boolean", "integer", "number", "string"]
             ):
                 json_prop["items"]["type"] = [json_prop["items"]["type"], "null"]
 
-            if prop.name in self._multiple_choice and prop.name in self._do_not_create:
+            if multiple_choice:
                 # TODO: if not multiple_choice, but do_not_create:
                 # "ui:widget" = "radio" & "ui:inline" = true
                 # TODO: set threshold for number of items.
@@ -256,7 +295,11 @@ ui_schema : dict
                 ui_schema["ui:inline"] = True
             if inner_ui_schema:
                 ui_schema["items"] = inner_ui_schema
+        # scalar references
         elif prop.is_reference():
+            # We must distinguish between multiple kinds of "reference" properties.
+
+            # Case 1: Plain reference without RecordType
             if prop.datatype == db.REFERENCE:
                 # No Record creation since no RT is specified and we don't know what
                 # schema to use, so only enum of all Records and all Files.
@@ -264,6 +307,8 @@ ui_schema : dict
                 json_prop["enum"] = values
                 if prop.name in self._multiple_choice:
                     json_prop["uniqueItems"] = True
+
+            # Case 2: Files are data-url strings in json schema
             elif prop.datatype == db.FILE or (
                 self._wrap_files_in_objects and
                     is_list_datatype(prop.datatype) and
@@ -300,15 +345,32 @@ ui_schema : dict
                 else:
                     json_prop["type"] = "string"
                     json_prop["format"] = "data-url"
+
+            # Case 3: Reference property with a type
             else:
                 prop_name = prop.datatype
                 if isinstance(prop.datatype, db.Entity):
                     prop_name = prop.datatype.name
-                if prop.name in self._do_not_retrieve:
-                    values = []
-                else:
+
+                # Find out if this property is an enum.
+                is_enum = (multiple_choice_enforce
+                           or
+                           (self._multiple_choice_guess
+                            and self._guess_recordtype_is_enum(prop_name)))
+                # If `is_enum` -> always get values
+                # Otherwise -> `do_not_retrieve` may prevent retrieval
+                if is_enum or not (
+                        (
+                            isinstance(self._do_not_retrieve, list)
+                            and prop_name in self._do_not_retrieve)
+                        or (
+                            self._do_not_retrieve == "auto"
+                        )):
                     values = self._retrieve_enum_values(f"RECORD '{prop_name}'")
-                if prop.name in self._do_not_create:
+                else:
+                    values = []
+
+                if is_enum or prop.name in self._do_not_create:
                     # Only a simple list of values
                     json_prop["enum"] = values
                 else:
@@ -325,7 +387,9 @@ ui_schema : dict
                             rt = db.Entity()
 
                     if isinstance(rt, str):
-                        raise NotImplementedError("Behavior is not implemented when _no_remote == True and datatype is given as a string.")
+                        raise NotImplementedError("Behavior is not implemented when "
+                                                  "_no_remote == True and datatype is given as a "
+                                                  "string.")
 
                     subschema, ui_schema = self._make_segment_from_recordtype(rt)
                     if prop.is_reference():
@@ -333,6 +397,14 @@ ui_schema : dict
                             subschema["title"] = prop.name
                         if prop.description:
                             subschema["description"] = prop.description
+                        if self._use_id_for_identification:
+                            subschema["properties"]["name"] = {
+                                "type": "string",
+                                "description": "The name of the Record to be created"}
+                            subschema["properties"]["id"] = {"type": "string"}
+                            subschema["properties"].move_to_end("name", last=False)
+                            subschema["properties"].move_to_end("id", last=False)
+                            # {"oneOf": [{"type": "integer"}, {"type": "string"}]}
 
                     # if inner_ui_schema:
                     #     ui_schema = inner_ui_schema
@@ -401,6 +473,22 @@ ui_schema : dict
 
         return prop
 
+    def _guess_recordtype_is_enum(self, rt_name: str) -> bool:
+        """For a given RecordType, guess if it represents an enum.
+
+        Parameters
+        ----------
+        rt_name : str
+          Name of the RecordType to be guessed.
+
+        Returns
+        -------
+        out : guess
+          True, if the RecordType is guessed to be an enum.  False otherwise.
+        """
+        rt = get_entity_by_name(rt_name)
+        return len(rt.get_properties()) == 0
+
     def _retrieve_enum_values(self, role: str):
 
         if self._no_remote:
@@ -410,6 +498,8 @@ ui_schema : dict
 
         vals = []
         for val in possible_values:
+            # if self._use_id_for_identification:
+            #     vals.append(val.id)
             if val.name:
                 vals.append(f"{val.name}")
             else:
@@ -417,7 +507,7 @@ ui_schema : dict
 
         return vals
 
-    def _make_segment_from_recordtype(self, rt: db.RecordType) -> Tuple[OrderedDict, dict]:
+    def _make_segment_from_recordtype(self, rt: db.RecordType) -> tuple[OrderedDict, dict]:
         """Return Json schema and uischema segments for the given RecordType.
 
         The result is an element of type `object
@@ -453,6 +543,8 @@ ui_schema : dict
         props = OrderedDict()
         if self._name_property_for_new_records:
             props["name"] = self._make_text_property("The name of the Record to be created")
+        # if self._use_id_for_identification:
+        #     props["id"] = self._make_text_property("The id of the Record")
         if self._description_property_for_new_records:
             props["description"] = self._make_text_property(
                 "The description of the Record to be created")
@@ -468,12 +560,20 @@ ui_schema : dict
             if inner_ui_schema:
                 ui_schema[prop.name] = inner_ui_schema
 
+        if self._use_id_for_identification:
+            props["name"] = {
+                "type": "string",
+                "description": "The name of the Record to be created"}
+            props["id"] = {"type": "string"}
+            props.move_to_end("name", last=False)
+            props.move_to_end("id", last=False)
+
         schema["properties"] = props
 
         return schema, ui_schema
 
     def _customize(self, schema: OrderedDict, ui_schema: dict, entity: db.Entity = None) -> (
-            Tuple[OrderedDict, dict]):
+            tuple[OrderedDict, dict]):
         """Generic customization method.
 
 Walk over the available customization stores and apply all applicable ones.  No specific order is
@@ -505,7 +605,7 @@ guaranteed (as of now).
         return schema, ui_schema
 
     def recordtype_to_json_schema(self, rt: db.RecordType, rjsf: bool = False) -> Union[
-            dict, Tuple[dict, dict]]:
+            dict, tuple[dict, dict]]:
         """Create a jsonschema from a given RecordType that can be used, e.g., to
         validate a json specifying a record of the given type.
 
@@ -544,19 +644,21 @@ guaranteed (as of now).
 
 def recordtype_to_json_schema(rt: db.RecordType, additional_properties: bool = True,
                               name_property_for_new_records: bool = False,
+                              use_id_for_identification: bool = False,
                               description_property_for_new_records: bool = False,
                               additional_options_for_text_props: Optional[dict] = None,
-                              additional_json_schema: Dict[str, dict] = None,
-                              additional_ui_schema: Dict[str, dict] = None,
+                              additional_json_schema: Optional[dict[str, dict]] = None,
+                              additional_ui_schema: Optional[dict[str, dict]] = None,
                               units_in_description: bool = True,
-                              do_not_create: List[str] = None,
-                              do_not_retrieve: List[str] = None,
+                              do_not_create: Optional[list[str]] = None,
+                              do_not_retrieve: Optional[Union[list[str], str]] = None,
                               no_remote: bool = False,
-                              use_rt_pool: DataModel = None,
-                              multiple_choice: List[str] = None,
+                              use_rt_pool: Optional[DataModel] = None,
+                              multiple_choice: Optional[list[str]] = None,
+                              multiple_choice_guess: bool = False,
                               rjsf: bool = False,
                               wrap_files_in_objects: bool = False
-                              ) -> Union[dict, Tuple[dict, dict]]:
+                              ) -> Union[dict, tuple[dict, dict]]:
     """Create a jsonschema from a given RecordType that can be used, e.g., to
     validate a json specifying a record of the given type.
 
@@ -567,55 +669,8 @@ def recordtype_to_json_schema(rt: db.RecordType, additional_properties: bool = T
     ----------
     rt : RecordType
         The RecordType from which a json schema will be created.
-    additional_properties : bool, optional
-        Whether additional properties will be admitted in the resulting
-        schema. Optional, default is True.
-    name_property_for_new_records : bool, optional
-        Whether objects shall generally have a `name` property in the generated schema. Optional,
-        default is False.
-    description_property_for_new_records : bool, optional
-        Whether objects shall generally have a `description` property in the generated schema.
-        Optional, default is False.
-    additional_options_for_text_props : dict, optional
-        Dictionary containing additional "pattern" or "format" options for
-        string-typed properties. Optional, default is empty.
-    additional_json_schema : dict[str, dict], optional
-        Additional schema content for elements of the given names.
-    additional_ui_schema : dict[str, dict], optional
-        Additional ui schema content for elements of the given names.
-    units_in_description : bool, optional
-        Whether to add the unit of a LinkAhead property (if it has any) to the
-        description of the corresponding schema entry. If set to false, an
-        additional `unit` key is added to the schema itself which is purely
-        annotational and ignored, e.g., in validation. Default is True.
-    do_not_create : list[str], optional
-        A list of reference Property names, for which there should be no option
-        to create them.  Instead, only the choice of existing elements should
-        be given.
-    do_not_retrieve : list[str], optional
-        A list of RedcordType names, for which no Records shall be retrieved.  Instead, only an
-        object description should be given.  If this list overlaps with the `do_not_create`
-        parameter, the behavior is undefined.
-    no_remote : bool, optional
-        If True, do not attempt to connect to a LinkAhead server at all.  Default is False.
-    use_rt_pool : models.data_model.DataModel, optional
-        If given, do not attempt to retrieve RecordType information remotely but from this parameter
-        instead.
-    multiple_choice : list[str], optional
-        A list of reference Property names which shall be denoted as multiple choice properties.
-        This means that each option in this property may be selected at most once.  This is not
-        implemented yet if the Property is not in ``do_not_create`` as well.
-    rjsf : bool, optional
-        If True, uiSchema definitions for react-jsonschema-forms will be output as the second return
-        value.  Default is False.
-    wrap_files_in_objects : bool, optional
-        Whether (lists of) files should be wrapped into an array of objects that
-        have a file property. The sole purpose of this wrapping is to provide a
-        workaround for a `react-jsonschema-form bug
-        <https://github.com/rjsf-team/react-jsonschema-form/issues/3957>`_ so
-        only set this to True if you're using the exported schema with
-        react-json-form and you are experiencing the bug. Default is False.
 
+    The other parameters are identical to the ones use by ``JsonSchemaExporter``
 
     Returns
     -------
@@ -629,6 +684,7 @@ def recordtype_to_json_schema(rt: db.RecordType, additional_properties: bool = T
     exporter = JsonSchemaExporter(
         additional_properties=additional_properties,
         name_property_for_new_records=name_property_for_new_records,
+        use_id_for_identification=use_id_for_identification,
         description_property_for_new_records=description_property_for_new_records,
         additional_options_for_text_props=additional_options_for_text_props,
         additional_json_schema=additional_json_schema,
@@ -639,12 +695,13 @@ def recordtype_to_json_schema(rt: db.RecordType, additional_properties: bool = T
         no_remote=no_remote,
         use_rt_pool=use_rt_pool,
         multiple_choice=multiple_choice,
+        multiple_choice_guess=multiple_choice_guess,
         wrap_files_in_objects=wrap_files_in_objects
     )
     return exporter.recordtype_to_json_schema(rt, rjsf=rjsf)
 
 
-def make_array(schema: dict, rjsf_uischema: dict = None) -> Union[dict, Tuple[dict, dict]]:
+def make_array(schema: dict, rjsf_uischema: dict = None) -> Union[dict, tuple[dict, dict]]:
     """Create an array of the given schema.
 
 The result will look like this:
@@ -695,9 +752,9 @@ ui_schema : dict, optional
     return result
 
 
-def merge_schemas(schemas: Union[Dict[str, dict], Iterable[dict]],
-                  rjsf_uischemas: Union[Dict[str, dict], Sequence[dict]] = None) -> (
-                      Union[dict, Tuple[dict, dict]]):
+def merge_schemas(schemas: Union[dict[str, dict], Iterable[dict]],
+                  rjsf_uischemas: Optional[Union[dict[str, dict], Sequence[dict]]] = None,
+                  return_data_schema=False) -> (Union[dict, tuple[dict, dict]]):
     """Merge the given schemata into a single schema.
 
 The result will look like this:
@@ -728,6 +785,11 @@ rjsf_uischemas : dict[str, dict] | Iterable[dict], optional
   If given, also merge the react-jsonschema-forms from this argument and return as the second return
   value.  If ``schemas`` is a dict, this parameter must also be a dict, if ``schemas`` is only an
   iterable, this paramater must support numerical indexing.
+return_data_schema : bool, default False
+  If set to True, a second schema with all top-level entries wrapped in an
+  array will be returned. This is necessary if the schema describes the
+  data layout of an XLSX file.
+  Cannot be used together with rjsf_uischemas.
 
 Returns
 -------
@@ -737,10 +799,13 @@ schema : dict
 
 uischema : dict
   If ``rjsf_uischemas`` was given, this contains the merged UI schemata.
+data_schema : dict
+  If ``return_data_schema`` was given, this contains the XLSX file schema.
     """
     sub_schemas: dict[str, dict] = OrderedDict()
     required = []
     ui_schema = None
+    data_sub_schemas = OrderedDict()
 
     if isinstance(schemas, dict):
         sub_schemas = schemas
@@ -754,6 +819,9 @@ uischema : dict
         for i, schema in enumerate(schemas, start=1):
             title = schema.get("title", str(i))
             sub_schemas[title] = schema
+            if return_data_schema:
+                # data_sub_schemas[title] = {"type": "array", "items": schema}
+                data_sub_schemas[title] = schema
             required.append(title)
         if rjsf_uischemas is not None:
             if not isinstance(rjsf_uischemas, Sequence):
@@ -771,7 +839,17 @@ uischema : dict
         "additionalProperties": False,
         "$schema": "https://json-schema.org/draft/2020-12/schema",
     }
+    if return_data_schema:
+        data_schema = {
+            "type": "object",
+            "properties": data_sub_schemas,
+            "required": required,
+            "additionalProperties": False,
+            "$schema": "https://json-schema.org/draft/2020-12/schema",
+        }
 
     if ui_schema is not None:
         return result, ui_schema
+    if return_data_schema:
+        return result, data_schema
     return result
diff --git a/src/caosadvancedtools/table_json_conversion/_validation_utils.py b/src/caosadvancedtools/table_json_conversion/_validation_utils.py
new file mode 100644
index 0000000000000000000000000000000000000000..5dfd171e48a8141d27f52511e716ddeed75edd89
--- /dev/null
+++ b/src/caosadvancedtools/table_json_conversion/_validation_utils.py
@@ -0,0 +1,101 @@
+# encoding: utf-8
+#
+# This file is a part of the LinkAhead Project.
+#
+# Copyright (C) 2025 Indiscale GmbH <info@indiscale.com>
+#
+# This program is free software: you can redistribute it and/or modify
+# it under the terms of the GNU Affero General Public License as
+# published by the Free Software Foundation, either version 3 of the
+# License, or (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU Affero General Public License for more details.
+#
+# You should have received a copy of the GNU Affero General Public License
+# along with this program. If not, see <https://www.gnu.org/licenses/>.
+
+"""
+Utilities for validation of conversion / import / export results.
+For internal use.
+"""
+
+import datetime
+import json
+from copy import deepcopy
+from typing import Union
+
+import jsonschema
+
+
+def validate_jsonschema(instance: Union[dict, int, str, bool],
+                        schema: Union[str, dict]):
+    """
+    A table_json_conversion compatible variant of jsonschema.validate().
+    Accepts instances with datetime instances and None in not-nullable entries.
+
+    Parameters
+    ----------
+    instance : dict, int, str, bool
+        Either a dict or a json entry to check against the given schema.
+    schema : str, dict
+        Either a dict with the jsonschema to check against, or a path to a file
+        containing the same.
+    """
+    # Helper Functions
+    def _in_schema(key, val, schema):
+        """
+        Checks whether a key: value pair is in the given schema or fulfills the
+        criteria of a direct subschema (anyOf, allOf, oneOf).
+        """
+        if schema.get(key, None) == val:
+            return True
+        if 'anyOf' in schema:
+            return any([_in_schema(key, val, sub) for sub in schema['anyOf']])
+        if 'allOf' in schema:
+            return all([_in_schema(key, val, sub) for sub in schema['allOf']])
+        if 'oneOf' in schema:
+            return [_in_schema(key, val, sub) for sub in schema['oneOf']].count(True) == 1
+        return False
+
+    def _remove_incompatible_vals(iterable, schema):
+        """
+        Removes Key: None and datetime instances from nested dicts and lists of
+        any depth. Key: None is currently valid as there is no 'obligatory with
+        value', and datetime cannot be checked by jsonschema.
+        """
+        if isinstance(iterable, list):
+            schema = schema.get('items', schema)
+            for elem in iterable:
+                _remove_incompatible_vals(elem, schema)
+        elif isinstance(iterable, dict):
+            schema = schema.get('properties', schema)
+            for key, elem in list(iterable.items()):
+                if elem is None:
+                    iterable.pop(key)
+                elif isinstance(elem, (datetime.date, datetime.datetime)):
+                    if (_in_schema('format', 'date', schema[key]) or
+                            _in_schema('format', 'date-time', schema[key])):
+                        iterable.pop(key)
+                elif isinstance(iterable, (dict, list)):
+                    try:
+                        _remove_incompatible_vals(elem, schema[key])
+                    except KeyError:
+                        pass
+        return iterable
+
+    # If jsonschema is a file, load its content
+    if str(schema).endswith(".json"):
+        with open(schema, encoding="utf-8") as content:
+            schema = json.load(content)
+    # If instance is not a dict, remove_incompatible_values would not remove
+    # the value if it is valid, so we need to check manually by wrapping
+    instance = deepcopy(instance)
+    if not isinstance(instance, dict):
+        if _remove_incompatible_vals({'key': instance}, {'key': schema}) == {}:
+            return
+    # Clean dict and validate
+    instance = _remove_incompatible_vals(deepcopy(instance), schema)
+    jsonschema.validate(instance, schema=schema)
diff --git a/src/caosadvancedtools/table_json_conversion/convert.py b/src/caosadvancedtools/table_json_conversion/convert.py
index 7a3d63a2444d09f0c9f695edfa8fd6865593f62e..dc2126d7bc7d3cff270e9920b6dea392be2a9348 100644
--- a/src/caosadvancedtools/table_json_conversion/convert.py
+++ b/src/caosadvancedtools/table_json_conversion/convert.py
@@ -31,12 +31,12 @@ from operator import getitem
 from types import SimpleNamespace
 from typing import Any, BinaryIO, Callable, TextIO, Union, Optional
 from warnings import warn
-from copy import deepcopy
 
 import jsonschema
 from openpyxl import load_workbook
 from openpyxl.worksheet.worksheet import Worksheet
 
+from ._validation_utils import validate_jsonschema
 from caosadvancedtools.table_json_conversion import xlsx_utils
 from caosadvancedtools.table_json_conversion.fill_xlsx import read_or_dict
 
@@ -153,51 +153,6 @@ class ForeignError(KeyError):
         self.definitions = definitions
 
 
-def _validate_jsonschema(instance, schema):
-    # Checks whether a key: value pair is in the given schema or fulfills the
-    # criteria of a direct subschema (anyOf, allOf, oneOf)
-    def in_schema(key, val, schema):
-        if schema.get(key, None) == val:
-            return True
-        if 'anyOf' in schema:
-            return any([in_schema(key, val, sub) for sub in schema['anyOf']])
-        if 'allOf' in schema:
-            return all([in_schema(key, val, sub) for sub in schema['allOf']])
-        if 'oneOf' in schema:
-            return [in_schema(key, val, sub) for sub in schema['oneOf']].count(True) == 1
-        return False
-
-    # Removes Key: None and datetime instances from nested dicts and lists of
-    # any depth. Key: None is currently valid as there is no 'obligatory with
-    # value', and datetime cannot be checked by jsonschema.
-    def remove_incompatible_values(it, schema):
-        if isinstance(it, list):
-            schema = schema.get('items', schema)
-            for elem in it:
-                remove_incompatible_values(elem, schema)
-        elif isinstance(it, dict):
-            schema = schema.get('properties', schema)
-            for key, elem in list(it.items()):
-                if elem is None:
-                    it.pop(key)
-                elif isinstance(elem, datetime.date) or isinstance(elem, datetime.datetime):
-                    if in_schema('format', 'date', schema[key]) or in_schema('format', 'date-time', schema[key]):
-                        it.pop(key)
-                elif isinstance(it, (dict, list)):
-                    remove_incompatible_values(elem, schema[key])
-        return it
-
-    # If instance is not a dict, remove_incompatible_values would not remove
-    # the value if it is valid, so we need to check manually by wrapping
-    instance = deepcopy(instance)
-    if not isinstance(instance, dict):
-        if remove_incompatible_values({'key': instance}, {'key': schema}) == {}:
-            return
-    # Clean dict and validate
-    instance = remove_incompatible_values(deepcopy(instance), schema)
-    jsonschema.validate(instance, schema=schema)
-
-
 class XLSXConverter:
     """Class for conversion from XLSX to JSON.
 
@@ -374,7 +329,7 @@ class XLSXConverter:
                                      for e in exceptions])
                 raise jsonschema.ValidationError(mess)
         if validate:
-            _validate_jsonschema(self._result, self._schema)
+            validate_jsonschema(self._result, self._schema)
         if self._errors:
             raise RuntimeError("There were error while handling the XLSX file.")
         return self._result
@@ -609,7 +564,7 @@ class XLSXConverter:
                 value = False
             if value == 1 or isinstance(value, str) and '=true()' == value.lower():
                 value = True
-        _validate_jsonschema(value, subschema)
+        validate_jsonschema(value, subschema)
 
         # Finally: convert to target type
         return self.PARSER[subschema.get("type", "string")](value)
diff --git a/src/caosadvancedtools/table_json_conversion/export_import_xlsx.py b/src/caosadvancedtools/table_json_conversion/export_import_xlsx.py
new file mode 100644
index 0000000000000000000000000000000000000000..5677f6293fd8994ccc0fabd4107fbc8bf09a0f2d
--- /dev/null
+++ b/src/caosadvancedtools/table_json_conversion/export_import_xlsx.py
@@ -0,0 +1,273 @@
+# encoding: utf-8
+#
+# This file is a part of the LinkAhead Project.
+#
+# Copyright (C) 2025 Indiscale GmbH <info@indiscale.com>
+#
+# This program is free software: you can redistribute it and/or modify
+# it under the terms of the GNU Affero General Public License as
+# published by the Free Software Foundation, either version 3 of the
+# License, or (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU Affero General Public License for more details.
+#
+# You should have received a copy of the GNU Affero General Public License
+# along with this program. If not, see <https://www.gnu.org/licenses/>.
+
+"""
+Utilities for automatically exporting and importing data to and from xlsx.
+"""
+
+import json
+import tempfile
+import warnings
+import logging
+from typing import Any, Iterable, Optional, Union
+from pathlib import Path
+
+from linkahead import (
+    Container,
+    # LinkAheadException,
+    )
+from linkahead.cached import cached_get_entity_by
+
+from ..json_schema_exporter import JsonSchemaExporter, merge_schemas
+from .table_generator import XLSXTemplateGenerator
+from .fill_xlsx import fill_template
+
+# The high_level_api import would normally warn about the API being
+# experimental. We know this, so suppress the warning.
+logging.disable(logging.WARNING)
+from linkahead.high_level_api import (  # noqa: E402, pylint: disable=wrong-import-position
+    convert_to_python_object,
+    # query
+    )
+logging.disable(logging.NOTSET)
+
+
+def _generate_jsonschema_from_recordtypes(recordtypes: Iterable,
+                                          out_path: Optional[Union[str, Path]] = None) -> dict:
+    """
+    Generate a combined jsonschema for all given recordtypes.
+
+    Parameters
+    ----------
+    recordtypes : Iterable
+        List of RecordType entities for which a schema should be generated.
+    out_path : str or Path, optional
+        If given, the resulting jsonschema will also be written to the file
+        given by out_path.
+        Optional, default None
+
+    Returns
+    -------
+    data_schema : dict
+        The generated schema.
+    """
+    # Generate schema
+    schema_generator = JsonSchemaExporter(additional_properties=False,
+                                          name_property_for_new_records=False,
+                                          use_id_for_identification=True,
+                                          do_not_retrieve="auto",
+                                          multiple_choice_guess=True)
+    schemas = [schema_generator.recordtype_to_json_schema(recordtype)
+               for recordtype in recordtypes]
+    _, data_schema = merge_schemas(schemas, return_data_schema=True)
+    # If indicated, save as json file
+    if out_path is not None:
+        with open(out_path, mode="w", encoding="utf8") as json_file:
+            json.dump(data_schema, json_file, ensure_ascii=False, indent=2)
+    # Return
+    return data_schema
+
+
+def _generate_jsondata_from_records(records: list,
+                                    out_path: Optional[Union[str, Path]] = None) -> dict:
+    """
+    Extract relevant information (id, name, properties, etc.) from the given
+    records and converts this information to json.
+
+    Parameters
+    ----------
+    records :  Iterable
+        List of high-level API objects from which the data will be converted to json.
+    out_path : str or Path, optional
+        If given, the resulting jsondata will also be written to the file given
+        by out_path.
+        Optional, default None
+
+    Returns
+    -------
+    json_data : dict
+        The given records data in json form.
+    """
+    json_data: dict[str, Any] = {}
+    for record_obj in records:
+        raw_data = record_obj.serialize(plain_json=True)
+
+        parent_name = record_obj.get_parents()[0].name  # We do not handle multiple inheritance yet.
+        if parent_name not in json_data:
+            json_data[parent_name] = []
+        json_data[parent_name].append(raw_data)
+    # If indicated, save as json file
+    if out_path is not None:
+        with open(out_path, mode="w", encoding="utf8") as json_file:
+            json.dump(json_data, json_file, ensure_ascii=False, indent=2, default=str)
+
+    return json_data
+
+
+def _generate_xlsx_template_file(schema: dict,
+                                 out_path: Union[str, Path]):
+    """
+    Generate an empty XLSX template file for the given schema at the indicated
+    location.
+
+    Parameters
+    ----------
+    schema : dict
+        Jsonschema for which an xlsx template should be generated.
+    out_path : str, Path
+        The resulting xlsx template will be written to the file at this path.
+    """
+    generator = XLSXTemplateGenerator()
+    foreign_keys: dict = {}
+    generator.generate(schema=schema, foreign_keys=foreign_keys,
+                       filepath=out_path, use_ids_as_foreign=True)
+
+
+def export_container_to_xlsx(records: Container,
+                             xlsx_data_filepath: Union[str, Path],
+                             include_referenced_entities: bool = False,
+                             jsonschema_filepath: Optional[Union[str, Path]] = None,
+                             jsondata_filepath: Optional[Union[str, Path]] = None,
+                             xlsx_template_filepath: Optional[Union[str, Path]] = None):
+    """Export the data of the given records to an xlsx file.
+
+    Parameters
+    ----------
+    records : Container, Iterable
+        List of records to export.
+    xlsx_data_filepath : str, Path
+        Write the resulting xlsx file to the file at this location.
+    include_referenced_entities : bool
+        If set to true, any records referenced by properties of those given in
+        'records' will also be exported.
+        Optional, default False
+    jsonschema_filepath : str, Path
+        If given, write the jsonschema to this file.
+        Optional, default None
+    jsondata_filepath : str, Path
+        If given, write the json data to this file.
+        Optional, default None
+    xlsx_template_filepath : str, Path
+        If given, write the xlsx template to this file.
+        Optional, default None
+
+    Limitations
+    -----------
+
+    This function drops any versioning information from versioned references, references are reduced
+    to unversioned references.
+
+    """
+
+    # JSON schema and JSON data ############################
+
+    # 1. Generate json schema for all top-level record types
+
+    rt_ids = set()
+    rt_names = set()
+    recordtypes = set()
+    for record in records:
+        parent = record.parents[0]
+        if parent.id:
+            rt_ids.add(parent.id)
+        else:
+            rt_names.add(parent.name)
+    for rt_name in rt_names:
+        rt_ids.add(cached_get_entity_by(name=rt_name).id)
+    for rt_id in rt_ids:
+        recordtypes.add(cached_get_entity_by(eid=rt_id))
+
+    # recordtype_ids = {recordtype.id for recordtype in recordtypes}
+    # recordtypes = [execute_query(f"FIND RECORDTYPE WITH (ID = {rt_id})",
+    #                              unique=True)
+    #                for rt_id in recordtype_ids]
+    # recordtype_names = {recordtype.name for recordtype in recordtypes}
+    # recordtype_names.add("Sample.Preparation.SourceMaterial")
+    # Generate schema and data from the records
+    json_schema = _generate_jsonschema_from_recordtypes(recordtypes,
+                                                        jsonschema_filepath)
+
+    # 2. Generate json data for all entities.
+
+    # Ensure every record is only handled once by using id as key.
+    # entity_ids = {record.id for record in records}
+    # If indicated, also get and add the records referenced on the first level
+    # in the given container
+    # if include_referenced_entities:
+    #     for record in records:
+    #         for prop in record.properties:
+    #             if prop.is_reference() and prop.value is not None:
+    #                 try:
+    #                     ref_list = prop.value
+    #                     if not isinstance(ref_list, list):
+    #                         ref_list = [ref_list]
+    #                     for element in ref_list:
+    #                         if isinstance(element, (int, str)):
+    #                             elem_id = element
+    #                         elif isinstance(element, Entity):
+    #                             elem_id = element.id
+    #                         else:
+    #                             warnings.warn(f"Cannot handle referenced "
+    #                                           f"entity '{prop.value}'")
+    #                             continue
+    #                         entity_ids.add(elem_id)
+    #                 except LinkAheadException as e:
+    #                     warnings.warn(f"Cannot handle referenced entity "
+    #                                   f"'{prop.value}' because of error '{e}'")
+
+    # Retrieve data
+    # if include_referenced_entities:
+    #     # new_records = []
+    #     # entity_ids = {record.id for record in records}
+    #     # for entity_id in entity_ids:
+    #     #     entity_id = str(entity_id).split('@')[0]
+    #     #     new_records.extend(query(f"FIND ENTITY WITH (ID = {entity_id})"))
+    #     # ToDo: Handle Files and other Entities (e.g. Properties) separately
+    #     high_level_objs = convert_to_python_object(records, resolve_references=True)
+    # else:
+    #     high_level_objs = convert_to_python_object(records)
+
+    high_level_objs = convert_to_python_object(records,
+                                               resolve_references=include_referenced_entities)
+    json_data = _generate_jsondata_from_records(high_level_objs, jsondata_filepath)
+
+    # XLSX generation and filling with data ################
+
+    # 1. Generate xlsx template
+
+    # _generate_xlsx_template_file needs a file name, so use NamedTemporaryFile
+    # ToDo: This might not work on windows, if not, fix _generate file handling
+    if xlsx_template_filepath is None:
+        xlsx_template_file = tempfile.NamedTemporaryFile(suffix='.xlsx')
+        xlsx_template_filepath = xlsx_template_file.name
+    else:
+        xlsx_template_file = None
+    _generate_xlsx_template_file(json_schema, xlsx_template_filepath)
+    # Fill xlsx file with data
+    with warnings.catch_warnings():
+        # We have a lot of information in the json data that we do not need
+        warnings.filterwarnings("ignore",
+                                message="^.*Ignoring path with missing sheet index.*$")
+        warnings.filterwarnings("ignore",
+                                message="^.*No validation schema.*$")
+        fill_template(data=json_data, template=xlsx_template_filepath,
+                      result=xlsx_data_filepath)
+    # Cleanup
+    if xlsx_template_file is not None:
+        xlsx_template_file.close()
diff --git a/src/caosadvancedtools/table_json_conversion/fill_xlsx.py b/src/caosadvancedtools/table_json_conversion/fill_xlsx.py
index f2e0abc3fc684172065d683c99c1c4309c80d6c0..005742f04c62c9e6bd7f6bbc6bcb82cf03096e83 100644
--- a/src/caosadvancedtools/table_json_conversion/fill_xlsx.py
+++ b/src/caosadvancedtools/table_json_conversion/fill_xlsx.py
@@ -23,16 +23,17 @@
 from __future__ import annotations
 
 import datetime
-import pathlib
+import warnings
+from pathlib import Path
 from types import SimpleNamespace
 from typing import Any, Optional, TextIO, Union
 from warnings import warn
 
-from jsonschema import FormatChecker, validate
 from jsonschema.exceptions import ValidationError
 from openpyxl import load_workbook, Workbook
 from openpyxl.cell.cell import ILLEGAL_CHARACTERS_RE
 
+from ._validation_utils import validate_jsonschema
 from .xlsx_utils import (
     array_schema_from_model_schema,
     get_foreign_key_columns,
@@ -186,6 +187,7 @@ Returns
 out: union[dict, None]
   If ``only_collect_insertables`` is True, return a dict (path string -> value)
         """
+        # FIXME The `utc` parameter is neither used, tested nor propagated recursively.
         assert (current_path is None) is (context is None), (
             "`current_path` and `context` must either both be given, or none of them.")
         if current_path is None:
@@ -329,8 +331,8 @@ to_insert: Optional[dict[str, str]]
         return to_insert
 
 
-def fill_template(data: Union[dict, str, TextIO], template: str, result: str,
-                  validation_schema: Union[dict, str, TextIO] = None) -> None:
+def fill_template(data: Union[dict, str, TextIO], template: str, result: Union[str, Path],
+                  validation_schema: Optional[Union[dict, str, TextIO]] = None) -> None:
     """Insert json data into an xlsx file, according to a template.
 
 This function fills the json data into the template stored at ``template`` and stores the result as
@@ -354,21 +356,27 @@ validation_schema: dict, optional
 
     # Validation
     if validation_schema is not None:
-        validation_schema = array_schema_from_model_schema(read_or_dict(validation_schema))
+        validation_schema = read_or_dict(validation_schema)
+        assert isinstance(validation_schema, dict)
+
+        # convert to array_schema if given schema is a model_schema
+        if 'properties' in validation_schema and validation_schema['properties'].values():
+            if list(validation_schema['properties'].values())[0]["type"] != "array":
+                validation_schema = array_schema_from_model_schema(read_or_dict(validation_schema))
         try:
-            # FIXME redefine checker for datetime
-            validate(data, validation_schema, format_checker=FormatChecker())
+            validate_jsonschema(data, validation_schema)
         except ValidationError as verr:
             print(verr.message)
             raise verr
     else:
-        print("No validation schema given, continue at your own risk.")
+        warnings.warn("No validation schema given, continue at your own risk.")
 
     # Filling the data
     result_wb = load_workbook(template)
     template_filler = TemplateFiller(result_wb, graceful=(validation_schema is None))
     template_filler.fill_data(data=data)
 
-    parentpath = pathlib.Path(result).parent
-    parentpath.mkdir(parents=True, exist_ok=True)
+    if not isinstance(result, Path):
+        result = Path(result)
+    result.parent.mkdir(parents=True, exist_ok=True)
     result_wb.save(result)
diff --git a/src/caosadvancedtools/table_json_conversion/table_generator.py b/src/caosadvancedtools/table_json_conversion/table_generator.py
index b8c50e7d8d40775f86c1a01d0934effe570cf20d..17ed5dac8d4e3a48190ebb58901feb744853fea2 100644
--- a/src/caosadvancedtools/table_json_conversion/table_generator.py
+++ b/src/caosadvancedtools/table_json_conversion/table_generator.py
@@ -26,10 +26,10 @@ This module allows to generate template tables from JSON schemas.
 
 from __future__ import annotations
 
-import pathlib
 import re
 from abc import ABC, abstractmethod
-from typing import Optional
+from pathlib import Path
+from typing import Optional, Union
 
 from openpyxl import Workbook
 from openpyxl.styles import PatternFill
@@ -45,7 +45,8 @@ class TableTemplateGenerator(ABC):
         pass
 
     @abstractmethod
-    def generate(self, schema: dict, foreign_keys: dict, filepath: str):
+    def generate(self, schema: dict, foreign_keys: dict, filepath: str,
+                 use_ids_as_foreign: bool = False):
         """Generate a sheet definition from a given JSON schema.
 
         Parameters:
@@ -76,9 +77,14 @@ class TableTemplateGenerator(ABC):
               be distiguished by the "name" and "email" of a "Person" which it references.  The
               foreign keys for this example are specified like this:
             | ``{"Training": {"__this__": [["Person", "name"], ["Person", "email"]]}}``
+
+        use_ids_as_foreign: bool, optional
+            If True, use the id (a property named "id") as foreign key, if the key does not exist in
+            the dict.  Default is False.
         """
 
-    def _generate_sheets_from_schema(self, schema: dict, foreign_keys: Optional[dict] = None
+    def _generate_sheets_from_schema(self, schema: dict, foreign_keys: Optional[dict] = None,
+                                     use_ids_as_foreign: bool = False
                                      ) -> dict[str, dict[str,
                                                          tuple[ColumnType, Optional[str], list]]]:
         """Generate a sheet definition from a given JSON schema.
@@ -91,6 +97,9 @@ class TableTemplateGenerator(ABC):
             a configuration that defines which attributes shall be used to create
             additional columns when a list of references exists. See ``foreign_keys``
             argument of TableTemplateGenerator.generate.
+        use_ids_as_foreign: bool, optional
+            If True, use the id (a property named "id") as foreign key, if the key does not exist in
+            the dict.  Default is False.
 
         Returns
         -------
@@ -122,10 +131,12 @@ class TableTemplateGenerator(ABC):
         sheets: dict[str, dict[str, tuple[ColumnType, Optional[str], list]]] = {}
         for rt_name, rt_def in schema["properties"].items():
             sheets[rt_name] = self._treat_schema_element(schema=rt_def, sheets=sheets,
-                                                         path=[rt_name], foreign_keys=foreign_keys)
+                                                         path=[rt_name], foreign_keys=foreign_keys,
+                                                         use_ids_as_foreign=use_ids_as_foreign)
         return sheets
 
-    def _get_foreign_keys(self, keys: dict, path: list) -> list[list[str]]:
+    def _get_foreign_keys(self, keys: dict, path: list, use_ids_as_foreign: bool = False
+                          ) -> list[list[str]]:
         """Return the foreign keys that are needed at the location to which path points.
 
 Returns
@@ -137,7 +148,10 @@ foreign_keys: list[list[str]]
         orig_path = path.copy()
         while path:
             if keys is None or path[0] not in keys:
-                raise ValueError(msg_missing)
+                if use_ids_as_foreign:  # Create entry ad-hoc.  TODO: don't modify passed argument?
+                    keys[path[0]] = {'__this__': ['id']}
+                else:
+                    raise ValueError(msg_missing)
             keys = keys[path[0]]
             path = path[1:]
         if isinstance(keys, dict) and "__this__" in keys:
@@ -160,7 +174,7 @@ foreign_keys: list[list[str]]
 
     def _treat_schema_element(self, schema: dict, sheets: dict, path: list[str],
                               foreign_keys: Optional[dict] = None, level_in_sheet_name: int = 1,
-                              array_paths: Optional[list] = None
+                              array_paths: Optional[list] = None, use_ids_as_foreign: bool = False,
                               ) -> dict[str, tuple[ColumnType, Optional[str], list]]:
         """Recursively transform elements from the schema into column definitions.
 
@@ -210,13 +224,15 @@ foreign_keys: list[list[str]]
                 col_def = self._treat_schema_element(
                     schema=items, sheets=sheets, path=path, foreign_keys=foreign_keys,
                     level_in_sheet_name=len(path),
-                    array_paths=array_paths+[path]  # since this level is an array extend the list
+                    array_paths=array_paths+[path],  # since this level is an array extend the list
+                    use_ids_as_foreign=use_ids_as_foreign,
                 )
                 if col_def:
                     sheets[sheetname] = col_def
                     # and add the foreign keys that are necessary up to this point
                     for array_path in array_paths:
-                        foreigns = self._get_foreign_keys(foreign_keys, array_path)
+                        foreigns = self._get_foreign_keys(foreign_keys, array_path,
+                                                          use_ids_as_foreign=use_ids_as_foreign)
                         for foreign in foreigns:
                             internal_key = p2s(array_path + foreign)
                             if internal_key in sheets[sheetname]:
@@ -261,7 +277,8 @@ foreign_keys: list[list[str]]
             for pname in schema["properties"]:
                 col_defs = self._treat_schema_element(
                     schema["properties"][pname], sheets, path+[pname], foreign_keys,
-                    level_in_sheet_name, array_paths=array_paths)
+                    level_in_sheet_name, array_paths=array_paths,
+                    use_ids_as_foreign=use_ids_as_foreign)
                 for k in col_defs:
                     if k in cols:
                         raise ValueError(f"The schema would lead to two columns with the same "
@@ -280,7 +297,9 @@ foreign_keys: list[list[str]]
                 assert d['type'] == 'string'
                 assert d['format'] == 'date' or d['format'] == 'date-time'
             return default_return
-        if schema["type"] in ['string', 'number', 'integer', 'boolean']:
+        scalars = ['string', 'number', 'integer', 'boolean']
+        # Also add "null" combinations, such as ["string", "null"].
+        if schema["type"] in (scalars + [[scal, "null"] for scal in scalars]):
             if 'format' in schema and schema['format'] == 'data-url':
                 return {}  # file; ignore for now
             return default_return
@@ -294,7 +313,8 @@ class XLSXTemplateGenerator(TableTemplateGenerator):
     # def __init__(self):
     #     super().__init__()
 
-    def generate(self, schema: dict, foreign_keys: dict, filepath: str) -> None:
+    def generate(self, schema: dict, foreign_keys: dict, filepath: Union[str, Path],
+                 use_ids_as_foreign: bool = False) -> None:
         """Generate a sheet definition from a given JSON schema.
 
         Parameters:
@@ -305,12 +325,15 @@ class XLSXTemplateGenerator(TableTemplateGenerator):
             A configuration that defines which attributes shall be used to create
             additional columns when a list of references exists. See ``foreign_keys``
             argument of :meth:`TableTemplateGenerator.generate` .
-        filepath: str
+        filepath: Union[str, Path]
             The XLSX file will be stored under this path.
         """
-        sheets = self._generate_sheets_from_schema(schema, foreign_keys)
+        sheets = self._generate_sheets_from_schema(schema, foreign_keys,
+                                                   use_ids_as_foreign=use_ids_as_foreign)
         wb = self._create_workbook_from_sheets_def(sheets)
-        parentpath = pathlib.Path(filepath).parent
+        if not isinstance(filepath, Path):
+            filepath = Path(filepath)
+        parentpath = filepath.parent
         parentpath.mkdir(parents=True, exist_ok=True)
         wb.save(filepath)
 
diff --git a/unittests/table_json_conversion/create_jsonschema.py b/unittests/table_json_conversion/create_jsonschema.py
index 8ab4ad2d973b78522e858b3ee866b870ecf187a4..0a244425847140b5802729aca901dffcb7b5a0cf 100755
--- a/unittests/table_json_conversion/create_jsonschema.py
+++ b/unittests/table_json_conversion/create_jsonschema.py
@@ -30,8 +30,9 @@ from caosadvancedtools.models import parser
 # import tomli
 
 
-def prepare_datamodel(modelfile, recordtypes: list[str], outfile: str,
+def prepare_datamodel(modelfile: str, recordtypes: list[str], outfile: str,
                       do_not_create: list[str] = None):
+    """Dump the schema generated from ``modelfile`` to ``outfile``."""
     if do_not_create is None:
         do_not_create = []
     model = parser.parse_model_from_yaml(modelfile)
diff --git a/unittests/table_json_conversion/data/multiple_choice_id_template.xlsx b/unittests/table_json_conversion/data/multiple_choice_id_template.xlsx
new file mode 100644
index 0000000000000000000000000000000000000000..6ee3cbda5f5007e8562b71de1f89f5cdd0ef7f78
Binary files /dev/null and b/unittests/table_json_conversion/data/multiple_choice_id_template.xlsx differ
diff --git a/unittests/table_json_conversion/data/multiple_choice_model.yaml b/unittests/table_json_conversion/data/multiple_choice_model.yaml
new file mode 100644
index 0000000000000000000000000000000000000000..bbbe45b9de3bdb064c7a484dc4c4831bc24b1203
--- /dev/null
+++ b/unittests/table_json_conversion/data/multiple_choice_model.yaml
@@ -0,0 +1,17 @@
+Training:
+  recommended_properties:
+    date:
+      datatype: DATETIME
+      description: 'The date of the training.'
+    skills:
+      datatype: LIST<Skill>
+      description: Skills that are trained.
+    exam_types:
+      datatype: LIST<ExamType>
+
+# Enum RecordTypes
+Skill:
+  description: Skills that are trained.
+
+ExamType:
+  description: The type of an exam.
diff --git a/unittests/table_json_conversion/data/multiple_choice_retrieved_data.json b/unittests/table_json_conversion/data/multiple_choice_retrieved_data.json
new file mode 100644
index 0000000000000000000000000000000000000000..78969122f012b84e545e46855375ac5ef9f5bd62
--- /dev/null
+++ b/unittests/table_json_conversion/data/multiple_choice_retrieved_data.json
@@ -0,0 +1,17 @@
+{
+  "Training": [
+    {
+      "name": "Super Skill Training",
+      "date": "2024-04-17 00:00:00-04:00",
+      "skills": [
+        {
+          "name": "Planning"
+        },
+        {
+          "name": "Evaluation"
+        }
+      ],
+      "exam_types": null
+    }
+  ]
+}
diff --git a/unittests/table_json_conversion/data/multiple_choice_schema.json b/unittests/table_json_conversion/data/multiple_choice_schema.json
index 71bf0379aba4ad6f8510581ba0defadb81a66609..01be127f6cb9dc18374b23aca302b13e48751838 100644
--- a/unittests/table_json_conversion/data/multiple_choice_schema.json
+++ b/unittests/table_json_conversion/data/multiple_choice_schema.json
@@ -30,8 +30,8 @@
           "items": {
             "enum": [
               "Planning",
-	            "Communication",
-	            "Evaluation"
+              "Communication",
+              "Evaluation"
             ]
           },
           "uniqueItems": true
@@ -41,7 +41,7 @@
           "items": {
             "enum": [
               "Oral",
-	            "Written"
+              "Written"
             ]
           },
           "uniqueItems": true
diff --git a/unittests/table_json_conversion/data/simple_model.yml b/unittests/table_json_conversion/data/simple_model.yml
index 74fb5bc5dc4251bb3834ea2f6201f991cab510d1..87249f5fc3267f979161e88edfa019ee7c44402a 100644
--- a/unittests/table_json_conversion/data/simple_model.yml
+++ b/unittests/table_json_conversion/data/simple_model.yml
@@ -30,7 +30,3 @@ Training:
 ProgrammingCourse:
   inherit_from_suggested:
     - Training
-Organisation:
-  recommended_properties:
-    Country:
-      datatype: TEXT
diff --git a/unittests/table_json_conversion/test_fill_xlsx.py b/unittests/table_json_conversion/test_fill_xlsx.py
index 899bb81ef1f91f3326f214f49f135a55b97d299f..084f19baccedb6778ceb4ff67bf21dbe0b8e66ac 100644
--- a/unittests/table_json_conversion/test_fill_xlsx.py
+++ b/unittests/table_json_conversion/test_fill_xlsx.py
@@ -59,13 +59,20 @@ schema: str, optional,
 custom_output: str, optional
   If given, write to this file and drop into an IPython shell.  For development only.
     """
+    if schema is not None:
+        with open(schema, encoding="utf8", mode="r") as sch_f:
+            model_schema = json.load(sch_f)
+        data_schema = xlsx_utils.array_schema_from_model_schema(model_schema)
+    else:
+        data_schema = schema
+
     with tempfile.TemporaryDirectory() as tmpdir:
         outfile = os.path.join(tmpdir, 'test.xlsx')
         assert not os.path.exists(outfile)
         if custom_output is not None:
             outfile = custom_output
         fill_template(data=json_file, template=template_file, result=outfile,
-                      validation_schema=schema)
+                      validation_schema=data_schema)
         assert os.path.exists(outfile)
         generated = load_workbook(outfile)  # workbook can be read
     known_good_wb = load_workbook(known_good)
@@ -189,6 +196,32 @@ def test_errors():
                          known_good=rfp("data/simple_data.xlsx"),
                          schema=rfp("data/simple_schema.json"))
     assert exc.value.message == "0.5 is not of type 'integer'"
+    # Check wrong data
+    with open(rfp("data/simple_data.json")) as json_file:
+        json_data = json.load(json_file)
+    json_data["Training"][0]["date"] = "2023-01"
+    with tempfile.NamedTemporaryFile(suffix='.json', mode='w+t') as temp_file:
+        json.dump(json_data, temp_file)
+        temp_file.seek(0)
+        with pytest.raises(AssertionError) as exc:
+            fill_and_compare(json_file=temp_file.name,
+                             template_file=rfp("data/simple_template.xlsx"),
+                             known_good=rfp("data/simple_data.xlsx"),
+                             schema=rfp("data/simple_schema.json"))
+        assert "Training" in str(exc) and "2023-01" in str(exc)
+    # Check wrong schema
+    with open(rfp("data/simple_schema.json")) as json_file:
+        json_schema = json.load(json_file)
+    json_schema["properties"]["Person"]["properties"]["given_name"]["type"] = "integer"
+    with tempfile.NamedTemporaryFile(suffix='.json', mode='w+t') as temp_file:
+        json.dump(json_schema, temp_file)
+        temp_file.seek(0)
+        with pytest.raises(schema_exc.ValidationError) as exc:
+            fill_and_compare(json_file=rfp("data/simple_data.json"),
+                             template_file=rfp("data/simple_template.xlsx"),
+                             known_good=rfp("data/simple_data.xlsx"),
+                             schema=temp_file.name)
+        assert "integer" in str(exc)
 
 
 def test_data_schema_generation():
diff --git a/unittests/table_json_conversion/test_read_xlsx.py b/unittests/table_json_conversion/test_read_xlsx.py
index d453ab3593ec36aa1197727f5ed51d1fb6fea10f..10b462df83a68a6a51088170f8d7c0216bd495e5 100644
--- a/unittests/table_json_conversion/test_read_xlsx.py
+++ b/unittests/table_json_conversion/test_read_xlsx.py
@@ -24,6 +24,7 @@ import datetime
 import json
 import os
 import re
+import tempfile
 
 from types import SimpleNamespace
 from typing import Optional
@@ -43,7 +44,7 @@ def rfp(*pathcomponents):
 
 def convert_and_compare(xlsx_file: str, schema_file: str, known_good_file: str,
                         known_good_data: Optional[dict] = None, strict: bool = False,
-                        validate: bool = False) -> dict:
+                        validate: bool = True) -> dict:
     """Convert an XLSX file and compare to a known result.
 
 Exactly one of ``known_good_file`` and ``known_good_data`` should be non-empty.
@@ -57,7 +58,7 @@ json: dict
         model_schema = json.load(sch_f)
     data_schema = xlsx_utils.array_schema_from_model_schema(model_schema)
 
-    result = convert.to_dict(xlsx=xlsx_file, schema=data_schema, validate=True)
+    result = convert.to_dict(xlsx=xlsx_file, schema=data_schema, validate=validate)
     if known_good_file:
         with open(known_good_file, encoding="utf-8") as myfile:
             expected = json.load(myfile)
@@ -101,6 +102,33 @@ def test_conversions():
     assert str(err.value).startswith("Values at path ['Training', 0, ")
 
 
+def test_validation():
+    # Check wrong data
+    with open(rfp("data/simple_data.json")) as json_file:
+        known_good = json.load(json_file)
+    known_good["Training"][0]["date"] = "2023-01-02"
+    with tempfile.NamedTemporaryFile(suffix='.json', mode='w+t') as temp_file:
+        json.dump(known_good, temp_file)
+        temp_file.seek(0)
+        with pytest.raises(AssertionError) as exc:
+            convert_and_compare(xlsx_file=rfp("data/simple_data.xlsx"),
+                                schema_file=rfp("data/simple_schema.json"),
+                                known_good_file=temp_file.name)
+        assert "Training" in str(exc) and "2023-01-02" in str(exc)
+    # Check wrong schema
+    with open(rfp("data/simple_schema.json")) as json_file:
+        json_schema = json.load(json_file)
+    json_schema["properties"]["Person"]["properties"]["given_name"]["type"] = "integer"
+    with tempfile.NamedTemporaryFile(suffix='.json', mode='w+t') as temp_file:
+        json.dump(json_schema, temp_file)
+        temp_file.seek(0)
+        with pytest.raises(jsonschema.ValidationError) as exc:
+            convert_and_compare(xlsx_file=rfp("data/simple_data.xlsx"),
+                                schema_file=temp_file.name,
+                                known_good_file=rfp("data/simple_data.json"))
+        assert "integer" in str(exc)
+
+
 def test_missing_columns():
     with pytest.raises(ValueError) as caught:
         convert.to_dict(xlsx=rfp("data/simple_data_missing.xlsx"),
diff --git a/unittests/table_json_conversion/test_table_template_generator.py b/unittests/table_json_conversion/test_table_template_generator.py
index d9a84dcf53ec991eec709aab406a7652881e6ea8..b4291818a60bc018a67c31a33c0517505c4979b2 100644
--- a/unittests/table_json_conversion/test_table_template_generator.py
+++ b/unittests/table_json_conversion/test_table_template_generator.py
@@ -1,4 +1,3 @@
-#!/usr/bin/env python3
 # encoding: utf-8
 #
 # This file is a part of the LinkAhead Project.
@@ -29,6 +28,7 @@ from caosadvancedtools.table_json_conversion.xlsx_utils import ColumnType
 from openpyxl import load_workbook
 
 from .utils import compare_workbooks
+from .create_jsonschema import prepare_datamodel
 
 
 def rfp(*pathcomponents):
@@ -64,6 +64,7 @@ out: tuple
                            filepath=outpath)
         assert os.path.exists(outpath)
         generated = load_workbook(outpath)
+
     good = load_workbook(known_good)
     compare_workbooks(generated, good)
     return generated, good
@@ -274,6 +275,23 @@ def test_model_with_multiple_choice():
         outfile=None)
 
 
+def test_schema_with_null_arrays(tmp_path):
+    """Schemas may be generated with allow `None` as list content."""
+
+    # Generate json automatically
+    prepare_datamodel(modelfile=rfp("data/simple_model.yml"),
+                      recordtypes=["Training", "Person"],
+                      outfile=tmp_path / "simple_schema.json",
+                      do_not_create=["Organisation"])
+
+    # Compare result
+    _compare_generated_to_known_good(
+        schema_file=tmp_path / "simple_schema.json",
+        known_good=rfp("data/simple_template.xlsx"),
+        foreign_keys={'Training': {"__this__": ['date', 'url']}},
+        outfile=None)
+
+
 def test_exceptions():
     # Foreign keys must be lists
     with pytest.raises(ValueError, match="Foreign keys must be a list of strings, but a single "
diff --git a/unittests/table_json_conversion/utils.py b/unittests/table_json_conversion/utils.py
index ac76fbea4508017261385e4e8fd70bedc378da5a..0134c062358eeee271798e70bed07dc6fd2aa915 100644
--- a/unittests/table_json_conversion/utils.py
+++ b/unittests/table_json_conversion/utils.py
@@ -21,16 +21,26 @@
 """Utilities for the tests.
 """
 
-from typing import Iterable, Union
+from datetime import datetime
+from typing import Iterable, Optional, Union
 
 from openpyxl import Workbook
 
 
 def assert_equal_jsons(json1, json2, allow_none: bool = True, allow_empty: bool = True,
-                       path: list = None) -> None:
+                       ignore_datetime: bool = False, ignore_id_value: bool = False,
+                       allow_name_dict: bool = False,
+                       path: Optional[list] = None) -> None:
     """Compare two json objects for near equality.
 
-Raise an assertion exception if they are not equal."""
+Raise an assertion exception if they are not equal.
+
+Parameters
+----------
+
+allow_name_dict: bool, default=False
+    If True, a string and a dict ``{"name": "string's value"}`` are considered equal.
+    """
     if path is None:
         path = []
     assert isinstance(json1, dict) == isinstance(json2, dict), f"Type mismatch, path: {path}"
@@ -42,13 +52,33 @@ Raise an assertion exception if they are not equal."""
             if key in json1 and key in json2:
                 el1 = json1[key]
                 el2 = json2[key]
+                if allow_none and (el1 is None and (el2 == [] or el2 == {})
+                                   or el2 is None and (el1 == [] or el1 == {})):
+                    # shortcut in case of equivalent empty content
+                    continue
+                if allow_name_dict:  # Special exception
+                    my_str = None
+                    if isinstance(el1, str) and isinstance(el2, dict):
+                        my_str = el1
+                        my_dict = el2
+                    elif isinstance(el2, str) and isinstance(el1, dict):
+                        my_str = el2
+                        my_dict = el1
+                    if my_str is not None:
+                        if len(my_dict) == 1 and my_dict.get("name") == my_str:
+                            continue
                 assert isinstance(el1, type(el2)), f"Type mismatch, path: {this_path}"
                 if isinstance(el1, (dict, list)):
                     # Iterables: Recursion
-                    assert_equal_jsons(el1, el2, allow_none=allow_none, allow_empty=allow_empty,
-                                       path=this_path)
+                    assert_equal_jsons(
+                        el1, el2, allow_none=allow_none, allow_empty=allow_empty,
+                        ignore_datetime=ignore_datetime, ignore_id_value=ignore_id_value,
+                        allow_name_dict=allow_name_dict,
+                        path=this_path)
                     continue
-                assert el1 == el2, f"Values at path {this_path} are not equal:\n{el1},\n{el2}"
+                if not (ignore_id_value and key == "id"):
+                    assert equals_with_casting(el1, el2, ignore_datetime=ignore_datetime), (
+                        f"Values at path {this_path} are not equal:\n{el1},\n{el2}")
                 continue
             # Case 2: exists only in one collection
             existing = json1.get(key, json2.get(key))
@@ -64,9 +94,64 @@ Raise an assertion exception if they are not equal."""
         this_path = path + [idx]
         if isinstance(el1, dict):
             assert_equal_jsons(el1, el2, allow_none=allow_none, allow_empty=allow_empty,
+                               ignore_datetime=ignore_datetime, ignore_id_value=ignore_id_value,
+                               allow_name_dict=allow_name_dict,
                                path=this_path)
         else:
-            assert el1 == el2, f"Values at path {this_path} are not equal:\n{el1},\n{el2}"
+            assert equals_with_casting(el1, el2), (
+                f"Values at path {this_path} are not equal:\n{el1},\n{el2}")
+
+
+def equals_with_casting(value1, value2, ignore_datetime: bool = False) -> bool:
+    """Compare two values, return True if equal, False otherwise.  Try to cast to clever datatypes.
+    """
+    try:
+        dt1 = datetime.fromisoformat(value1)
+        dt2 = datetime.fromisoformat(value2)
+        if ignore_datetime:
+            return True
+        return dt1 == dt2
+    except (ValueError, TypeError):
+        pass
+    return value1 == value2
+
+
+def purge_from_json(data: Union[dict, list], remove_keys: list[str]) -> Union[dict, list]:
+    """Remove matching entries from json data.
+
+
+    Parameters
+    ----------
+    data : Union[dict, list]
+      The json data to clean.
+
+    remove_keys : list[str]
+       Remove all keys that are in this list
+
+    Returns
+    -------
+    out : Union[dict, list]
+      The cleaned result.
+    """
+
+    # Remove only from dicts
+    if isinstance(data, dict):
+        keys = set(data.keys())
+        for removable in remove_keys:
+            if removable in keys:
+                data.pop(removable)
+        elements = list(data.values())
+    else:
+        if not isinstance(data, list):
+            raise ValueError("Data must be a dict or list.")
+        elements = data
+
+    # Recurse for all elements
+    for element in elements:
+        if isinstance(element, dict) or isinstance(element, list):
+            purge_from_json(element, remove_keys=remove_keys)
+
+    return data
 
 
 def compare_workbooks(wb1: Workbook, wb2: Workbook, hidden: bool = True):
@@ -101,7 +186,7 @@ hidden: bool, optional
                 )
 
 
-def _is_recursively_none(obj: Union[list, dict] = None):
+def _is_recursively_none(obj: Union[list, dict, None] = None):
     """Test if ``obj`` is None or recursively consists only of None-like objects."""
     if obj is None:
         return True