From 31bd65fa63b58936982f97fa7f60b331a216eccb Mon Sep 17 00:00:00 2001 From: Timm Fitschen <t.fitschen@indiscale.com> Date: Fri, 29 Sep 2023 09:19:52 +0200 Subject: [PATCH] WIP: String IDs --- .../create_entity_ids_table.sql | 3 +- procedures/entityVersioning.sql | 2 +- procedures/insertEntityProperty.sql | 8 ++-- procedures/registerSubdomain.sql | 5 +-- procedures/retrieveChildren.sql | 43 +++++++++++++++++++ procedures/retrieveEntityOverrides.sql | 16 +++++-- procedures/retrieveEntityProperties.sql | 30 +++++++++++-- 7 files changed, 88 insertions(+), 19 deletions(-) create mode 100644 procedures/retrieveChildren.sql diff --git a/patches/patch20221122-6.0-SNAPSHOT/create_entity_ids_table.sql b/patches/patch20221122-6.0-SNAPSHOT/create_entity_ids_table.sql index 2bca817..cbc6ded 100644 --- a/patches/patch20221122-6.0-SNAPSHOT/create_entity_ids_table.sql +++ b/patches/patch20221122-6.0-SNAPSHOT/create_entity_ids_table.sql @@ -17,7 +17,6 @@ CREATE TABLE `entity_ids` ( ) ENGINE=InnoDB; -- fill all existing entities into the new entity_ids table. -INSERT INTO entity_ids (id, internal_id) SELECT id, id FROM entities WHERE id>0; -INSERT INTO entity_ids (id, internal_id) SELECT id, id FROM entities WHERE id=0; +INSERT INTO entity_ids (id, internal_id) SELECT id, id FROM entities WHERE id>0 AND role!="DOMAIN"; UPDATE entity_ids SET id = internal_id; -- ALTER TABLE entity_ids CHANGE id id int(10) unsigned NOT NULL AUTO_INCREMENT COMMENT 'External ID of an entity. This is the id of an entity which is exposed via the CaosDB API.'; diff --git a/procedures/entityVersioning.sql b/procedures/entityVersioning.sql index 6771185..22274f2 100644 --- a/procedures/entityVersioning.sql +++ b/procedures/entityVersioning.sql @@ -297,7 +297,7 @@ READS SQL DATA BEGIN DECLARE InternalEntityID INT UNSIGNED DEFAULT NULL; - SELECT internal_id INTO InternalEntityID FROM entity_ids WHERE id = EntityID; + SELECT temp.internal_id INTO InternalEntityID FROM (SELECT internal_id AS internal_id FROM entity_ids WHERE id = EntityID UNION SELECT EntityID AS internal_id) AS temp LIMIT 1; -- This implementation assumes that the distance from the head equals the -- difference between the _iversion numbers. This will not be correct anymore diff --git a/procedures/insertEntityProperty.sql b/procedures/insertEntityProperty.sql index 02c31bc..f3d56e3 100644 --- a/procedures/insertEntityProperty.sql +++ b/procedures/insertEntityProperty.sql @@ -47,9 +47,9 @@ BEGIN DECLARE InternalEntityID INT UNSIGNED DEFAULT NULL; DECLARE InternalDomainID INT UNSIGNED DEFAULT 0; - SELECT internal_id INTO InternalDomainID from entity_ids WHERE id = DomainID; - SELECT internal_id INTO InternalEntityID from entity_ids WHERE id = EntityID; - SELECT internal_id INTO InternalPropertyID from entity_ids WHERE id = PropertyID; + SELECT temp.internal_id INTO InternalDomainID FROM (SELECT internal_id AS internal_id FROM entity_ids WHERE id = DomainID UNION SELECT DomainID AS internal_id) AS temp LIMIT 1; + SELECT temp.internal_id INTO InternalEntityID FROM (SELECT internal_id AS internal_id FROM entity_ids WHERE id = EntityID UNION SELECT EntityID AS internal_id) AS temp LIMIT 1; + SELECT temp.internal_id INTO InternalPropertyID FROM (SELECT internal_id AS internal_id FROM entity_ids WHERE id = PropertyID UNION SELECT PropertyID AS internal_id) AS temp LIMIT 1; CASE Datatable WHEN 'double_data' THEN @@ -81,7 +81,7 @@ BEGIN END IF; ELSE - SELECT internal_id INTO ReferenceValue FROM entity_ids WHERE id = PropertyValue; + SELECT temp.internal_id INTO ReferenceValue FROM (SELECT internal_id AS internal_id FROM entity_ids WHERE id = PropertyValue UNION SELECT PropertyValue AS internal_id) AS temp LIMIT 1; END IF; diff --git a/procedures/registerSubdomain.sql b/procedures/registerSubdomain.sql index 3d3f44d..547d953 100644 --- a/procedures/registerSubdomain.sql +++ b/procedures/registerSubdomain.sql @@ -34,20 +34,17 @@ delimiter // CREATE PROCEDURE db_5_0.registerSubdomain(in amount INT UNSIGNED) BEGIN DECLARE ED INTEGER DEFAULT NULL; - DECLARE NewID INT UNSIGNED DEFAULT NULL; SELECT COUNT(id) INTO ED FROM entities WHERE Role='DOMAIN' AND id!=0; WHILE ED < amount DO INSERT INTO entities (description, role, acl) VALUES (NULL, 'DOMAIN', 0); - SET NewID = LAST_INSERT_ID(); - INSERT INTO entity_ids (internal_id) VALUES (NewID); SET ED = ED + 1; END WHILE; - SELECT e.id as DomainID FROM entities AS i LEFT JOIN entity_ids AS e ON e.internal_id = i.id WHERE i.Role='DOMAIN' and e.internal_id!=0; + SELECT e.id as DomainID FROM entities AS e WHERE e.Role='DOMAIN' and e.id!=0; END; // diff --git a/procedures/retrieveChildren.sql b/procedures/retrieveChildren.sql new file mode 100644 index 0000000..6721b92 --- /dev/null +++ b/procedures/retrieveChildren.sql @@ -0,0 +1,43 @@ +/* + * This file is a part of the CaosDB Project. + * + * Copyright (C) 2023 IndiScale GmbH <info@indiscale.com> + * Copyright (C) 2023 Timm Fitschen <t.fitschen@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/>. + */ + +/* Retrieve the IDs of all direct children. + +Parameters +========== + +ParentID : INT UNSIGNED + The ID of the parent. +*/ + +DROP PROCEDURE IF EXISTS db_5_0.retrieveChildren; +delimiter // + +CREATE PROCEDURE db_5_0.retrieveChildren(in ParentID INT UNSIGNED) +BEGIN + DECLARE InternalParentID INT UNSIGNED DEFAULT NULL; + + SELECT internal_id INTO InternalParentID from entity_ids WHERE id = ParentID; + + SELECT eids.id FROM isa_cache AS i JOIN entity_ids AS eids ON i.child = eids.internal_id WHERE i.parent=InternalParentID and i.rpath=i.child; + +END; +// +delimiter ; diff --git a/procedures/retrieveEntityOverrides.sql b/procedures/retrieveEntityOverrides.sql index 182cbc4..cc8bfbe 100644 --- a/procedures/retrieveEntityOverrides.sql +++ b/procedures/retrieveEntityOverrides.sql @@ -37,15 +37,15 @@ retrieveOverridesBody: BEGIN DECLARE InternalEntityID INT UNSIGNED DEFAULT NULL; DECLARE InternalDomainID INT UNSIGNED DEFAULT NULL; - SELECT internal_id INTO InternalEntityID from entity_ids WHERE id = EntityID; - SELECT internal_id INTO InternalDomainID from entity_ids WHERE id = DomainID; + SELECT temp.internal_id INTO InternalDomainID FROM (SELECT internal_id AS internal_id FROM entity_ids WHERE id = DomainID UNION SELECT DomainID AS internal_id) AS temp LIMIT 1; + SELECT temp.internal_id INTO InternalEntityID FROM (SELECT internal_id AS internal_id FROM entity_ids WHERE id = EntityID UNION SELECT EntityID AS internal_id) AS temp LIMIT 1; IF is_feature_config("ENTITY_VERSIONING", "ENABLED") THEN IF Version IS NOT NULL THEN IF InternalDomainID = 0 THEN - SELECT get_head_version(InternalEntityID) = Version INTO IsHead; + SELECT get_head_version(EntityID) = Version INTO IsHead; ELSE - SELECT get_head_version(InternalDomainID) = Version INTO IsHead; + SELECT get_head_version(DomainID) = Version INTO IsHead; END IF; END IF; @@ -68,6 +68,7 @@ retrieveOverridesBody: BEGIN NULL AS desc_override, NULL AS type_override, EntityID AS entity_id, + 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 @@ -83,6 +84,7 @@ retrieveOverridesBody: BEGIN description AS desc_override, NULL AS type_override, EntityID AS entity_id, + 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 @@ -102,6 +104,7 @@ retrieveOverridesBody: BEGIN AND property_id = 20 LIMIT 1), datatype) AS type_override, EntityID AS entity_id, + 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 @@ -117,6 +120,7 @@ retrieveOverridesBody: BEGIN NULL AS desc_override, NULL AS type_override, EntityID AS entity_id, + 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 @@ -133,6 +137,7 @@ retrieveOverridesBody: BEGIN NULL AS desc_override, NULL AS type_override, EntityID AS entity_id, + property_id AS InternalPropertyID, ( SELECT id FROM entity_ids WHERE internal_id = property_id ) AS property_id FROM name_overrides WHERE domain_id = InternalDomainID @@ -146,6 +151,7 @@ retrieveOverridesBody: BEGIN description AS desc_override, NULL AS type_override, EntityID AS entity_id, + property_id AS InternalPropertyID, ( SELECT id FROM entity_ids WHERE internal_id = property_id ) AS property_id FROM desc_overrides WHERE domain_id = InternalDomainID @@ -162,6 +168,7 @@ retrieveOverridesBody: BEGIN AND entity_ID = datatype AND property_id = 20 LIMIT 1), datatype) AS type_override, EntityID AS entity_id, + property_id AS InternalPropertyID, ( SELECT id FROM entity_ids WHERE internal_id = property_id ) AS property_id FROM data_type WHERE domain_id = InternalDomainID @@ -175,6 +182,7 @@ retrieveOverridesBody: BEGIN NULL AS desc_override, NULL AS type_override, EntityID AS entity_id, + property_id AS InternalPropertyID, ( SELECT id FROM entity_ids WHERE internal_id = property_id ) AS property_id FROM collection_type WHERE domain_id = InternalDomainID diff --git a/procedures/retrieveEntityProperties.sql b/procedures/retrieveEntityProperties.sql index 775dd19..78e9cf4 100644 --- a/procedures/retrieveEntityProperties.sql +++ b/procedures/retrieveEntityProperties.sql @@ -39,7 +39,7 @@ retrieveEntityPropertiesBody: BEGIN DECLARE InternalEntityID INT UNSIGNED DEFAULT NULL; DECLARE InternalDomainID INT UNSIGNED DEFAULT 0; - SELECT internal_id INTO InternalEntityID from entity_ids WHERE id = EntityID; + SELECT temp.internal_id INTO InternalEntityID FROM (SELECT internal_id AS internal_id FROM entity_ids WHERE id = EntityID UNION SELECT EntityID AS internal_id) AS temp LIMIT 1; SELECT internal_id INTO InternalDomainID from entity_ids WHERE id = DomainID; IF is_feature_config("ENTITY_VERSIONING", "ENABLED") THEN @@ -65,6 +65,7 @@ retrieveEntityPropertiesBody: BEGIN #-- double properties SELECT + property_id AS InternalPropertyID, ( SELECT id FROM entity_ids WHERE internal_id = property_id ) AS PropertyID, value AS PropertyValue, status AS PropertyStatus, @@ -78,6 +79,7 @@ retrieveEntityPropertiesBody: BEGIN #-- integer properties SELECT + property_id AS InternalPropertyID, ( SELECT id FROM entity_ids WHERE internal_id = property_id ) AS PropertyID, value AS PropertyValue, status AS PropertyStatus, @@ -91,6 +93,7 @@ retrieveEntityPropertiesBody: BEGIN #-- date properties SELECT + 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, @@ -104,6 +107,7 @@ retrieveEntityPropertiesBody: BEGIN #-- datetime properties SELECT + 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, @@ -118,6 +122,7 @@ retrieveEntityPropertiesBody: BEGIN #-- text properties SELECT + property_id AS InternalPropertyID, ( SELECT id FROM entity_ids WHERE internal_id = property_id ) AS PropertyID, value AS PropertyValue, status AS PropertyStatus, @@ -131,6 +136,7 @@ retrieveEntityPropertiesBody: BEGIN #-- enum properties SELECT + property_id AS InternalPropertyID, ( SELECT id FROM entity_ids WHERE internal_id = property_id ) AS PropertyID, value AS PropertyValue, status AS PropertyStatus, @@ -144,9 +150,12 @@ retrieveEntityPropertiesBody: BEGIN #-- reference properties SELECT + property_id AS InternalPropertyID, ( SELECT id FROM entity_ids WHERE internal_id = property_id ) AS PropertyID, - IF(value_iversion IS NULL, - ( SELECT id FROM entity_ids WHERE internal_id = value ), + IF(value_iversion IS NULL, + IF(status = "REPLACEMENT", + value, + ( SELECT id FROM entity_ids WHERE internal_id = value )), -- make it "value@version" if necessary CONCAT( ( SELECT id FROM entity_ids WHERE internal_id = value ), @@ -163,6 +172,7 @@ retrieveEntityPropertiesBody: BEGIN #-- null properties SELECT + property_id AS InternalPropertyID, ( SELECT id FROM entity_ids WHERE internal_id = property_id ) AS PropertyID, NULL AS PropertyValue, status AS PropertyStatus, @@ -176,6 +186,7 @@ retrieveEntityPropertiesBody: BEGIN #-- name properties SELECT + property_id AS InternalPropertyID, ( SELECT id FROM entity_ids WHERE internal_id = property_id ) AS PropertyID, value AS PropertyValue, status AS PropertyStatus, @@ -192,6 +203,7 @@ retrieveEntityPropertiesBody: BEGIN #-- double properties SELECT + property_id AS InternalPropertyID, ( SELECT id FROM entity_ids WHERE internal_id = property_id ) AS PropertyID, value AS PropertyValue, status AS PropertyStatus, @@ -204,6 +216,7 @@ retrieveEntityPropertiesBody: BEGIN #-- integer properties SELECT + property_id AS InternalPropertyID, ( SELECT id FROM entity_ids WHERE internal_id = property_id ) AS PropertyID, value AS PropertyValue, status AS PropertyStatus, @@ -216,6 +229,7 @@ retrieveEntityPropertiesBody: BEGIN #-- date properties SELECT + 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, @@ -228,6 +242,7 @@ retrieveEntityPropertiesBody: BEGIN #-- datetime properties SELECT + 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, @@ -241,6 +256,7 @@ retrieveEntityPropertiesBody: BEGIN #-- text properties SELECT + property_id AS InternalPropertyID, ( SELECT id FROM entity_ids WHERE internal_id = property_id ) AS PropertyID, value AS PropertyValue, status AS PropertyStatus, @@ -253,6 +269,7 @@ retrieveEntityPropertiesBody: BEGIN #-- enum properties SELECT + property_id AS InternalPropertyID, ( SELECT id FROM entity_ids WHERE internal_id = property_id ) AS PropertyID, value AS PropertyValue, status AS PropertyStatus, @@ -265,9 +282,12 @@ retrieveEntityPropertiesBody: BEGIN #-- reference properties SELECT + property_id AS InternalPropertyID, ( SELECT id FROM entity_ids WHERE internal_id = property_id ) AS PropertyID, IF(value_iversion IS NULL, - ( SELECT id FROM entity_ids WHERE internal_id = value ), + IF(status = "REPLACEMENT", + value, + ( SELECT id FROM entity_ids WHERE internal_id = value )), -- make it "value@version" if necessary CONCAT( ( SELECT id FROM entity_ids WHERE internal_id = value ), @@ -283,6 +303,7 @@ retrieveEntityPropertiesBody: BEGIN #-- null properties SELECT + property_id AS InternalPropertyID, ( SELECT id FROM entity_ids WHERE internal_id = property_id ) AS PropertyID, NULL AS PropertyValue, status AS PropertyStatus, @@ -295,6 +316,7 @@ retrieveEntityPropertiesBody: BEGIN #-- name properties SELECT + property_id AS InternalPropertyID, ( SELECT id FROM entity_ids WHERE internal_id = property_id ) AS PropertyID, value AS PropertyValue, status AS PropertyStatus, -- GitLab