From 9eeca52e7942ea68cc6a31d639c4463c7627acd6 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Henrik=20tom=20W=C3=B6rden?= <h.tomwoerden@indiscale.com>
Date: Thu, 27 Feb 2025 11:46:38 +0100
Subject: [PATCH] wip

---
 notes                                         | 28 +++++++++++++++++++
 real_property_id.sql                          | 28 +++++++++++++++++++
 real_property_id_alt.sql                      | 23 +++++++++++++++
 .../MySQL/MySQLInsertEntityProperties.java    |  3 +-
 .../server/database/proto/FlatProperty.java   |  1 +
 5 files changed, 82 insertions(+), 1 deletion(-)
 create mode 100644 notes
 create mode 100644 real_property_id.sql
 create mode 100644 real_property_id_alt.sql

diff --git a/notes b/notes
new file mode 100644
index 00000000..12c393f4
--- /dev/null
+++ b/notes
@@ -0,0 +1,28 @@
+
+
+
+Query
+
+Entry point Retrieve Transaction. In init() make_schedule creates flag jobs:
+(db.execute_query("FIND Record ", flags = {"P":"0L2"}))
+
+- P
+- query
+
+
+
+    // subject has complete permissions for this kind of transaction
+	- > check to easy? Entity permissions ignored?
+	(filterEntitiesWithoutRetrievePermission)
+
+
+	Warum ruft Query execute von BAckendtransaction auf um RetrieveSparseEntity
+	zu executen (Query hat außerdem seine eigene execute Funktion)?
+
+	is caching user specific?? filtering within query occurs depending on user
+	permissions!!
+
+
+
+
+
diff --git a/real_property_id.sql b/real_property_id.sql
new file mode 100644
index 00000000..71d2295c
--- /dev/null
+++ b/real_property_id.sql
@@ -0,0 +1,28 @@
+alter table reference_data add if not exists real_property_id INTEGER;
+update reference_data  set real_property_id=property_id where property_id not in (select distinct value from reference_data where status='replacement');
+update reference_data  join reference_data as ref2 on reference_data.entity_id=ref2.entity_id and reference_data.property_id=ref2.value set reference_data.real_property_id=ref2.property_id;
+
+drop index if exists prop on reference_data;
+create index prop on reference_data (real_property_id, value);
+
+drop temporary table if exists tmprealids;
+drop temporary table if exists tmpresult;
+create temporary table tmprealids select distinct child as real_property_id from isa_cache where parent = 112;
+insert into tmprealids select 112;
+select count(*) from tmprealids;
+create temporary table tmpresult (id INTEGER);
+set @start = CURRENT_TIMESTAMP(4);
+DELIMITER //
+set @length = (SELECT COUNT(*) FROM tmprealids)-1;
+FOR i IN 0..@length
+DO
+    insert into tmpresult
+    select distinct entity_id from reference_data where real_property_id=(
+      select real_property_id from tmprealids limit i,1) and status!="REPLACEMENT" and value!=518;
+END FOR;
+//
+
+DELIMITER ;
+select TIMESTAMPDIFF( MICROSECOND, @start,CURRENT_TIMESTAMP(4))/1000.0 as "spent(ms)";
+select count(distinct id) from tmpresult order by id;
+--select * from tmpresult;
diff --git a/real_property_id_alt.sql b/real_property_id_alt.sql
new file mode 100644
index 00000000..f6741e75
--- /dev/null
+++ b/real_property_id_alt.sql
@@ -0,0 +1,23 @@
+drop temporary table if exists sourcesettmp ;
+create temporary table sourcesettmp  select distinct entity_id as id from reference_data;
+
+call initPOVPropertiesTable(104, "104", "entities");
+set @propertiestable = (select SUBSTRING(@tempTableList ,2 ,32));
+select @propertiestable;
+show index from reference_data;
+
+drop temporary table if exists propertiestab ;
+prepare stmt from CONCAT("create temporary table propertiestab SELECT * from ", @propertiestable);
+execute stmt;
+
+drop temporary table if exists tmpresult;
+create temporary table tmpresult (id INTEGER);
+select * from propertiestab;
+
+set @start = CURRENT_TIMESTAMP(4);
+insert into tmpresult
+ SELECT distinct entity_id from (select entity_id, value from reference_data join propertiestab as prop where (prop.id2=NULL OR prop.id2 = reference_data.entity_id) AND 
+    reference_data.property_id=prop.id) as propfiltered
+     where propfiltered.value=518;
+select TIMESTAMPDIFF( MICROSECOND, @start,CURRENT_TIMESTAMP(4))/1000.0 as "spent(ms)";
+select count(id) from tmpresult order by id;
diff --git a/src/main/java/org/caosdb/server/database/backend/implementation/MySQL/MySQLInsertEntityProperties.java b/src/main/java/org/caosdb/server/database/backend/implementation/MySQL/MySQLInsertEntityProperties.java
index d303acca..0cab6a17 100644
--- a/src/main/java/org/caosdb/server/database/backend/implementation/MySQL/MySQLInsertEntityProperties.java
+++ b/src/main/java/org/caosdb/server/database/backend/implementation/MySQL/MySQLInsertEntityProperties.java
@@ -62,7 +62,7 @@ public class MySQLInsertEntityProperties extends MySQLTransaction
     implements InsertEntityPropertiesImpl {
 
   public static final String STMT_INSERT_ENTITY_PROPERTY =
-      "call insertEntityProperty(?,?,?,?,?,?,?,?,?,?,?,?)";
+      "call insertEntityProperty(?,?,?,?,?,?,?,?,?,?,?,?,?)";
 
   public MySQLInsertEntityProperties(final Access access) {
     super(access);
@@ -116,6 +116,7 @@ public class MySQLInsertEntityProperties extends MySQLTransaction
       stmt.setString(10, fp.type_id);
       stmt.setString(11, fp.collection);
       stmt.setInt(12, fp.idx);
+      stmt.setInt(13, fp.real_id);
       stmt.execute();
     } catch (final SQLIntegrityConstraintViolationException exc) {
       throw new IntegrityException(exc);
diff --git a/src/main/java/org/caosdb/server/database/proto/FlatProperty.java b/src/main/java/org/caosdb/server/database/proto/FlatProperty.java
index ecb49147..8dc861ff 100644
--- a/src/main/java/org/caosdb/server/database/proto/FlatProperty.java
+++ b/src/main/java/org/caosdb/server/database/proto/FlatProperty.java
@@ -36,4 +36,5 @@ public class FlatProperty implements Serializable {
   public String type_id = null;
   public String type_name = null;
   public String collection = null;
+  public Integer real_id = null;
 }
-- 
GitLab