]> gerrit.simantics Code Review - simantics/platform.git/commitdiff
(refs #7090) New functions subquery and subqueryC 79/379/2
authorHannu Niemistö <hannu.niemisto@semantum.fi>
Fri, 24 Mar 2017 13:00:43 +0000 (15:00 +0200)
committerTuukka Lehtonen <tuukka.lehtonen@semantum.fi>
Fri, 24 Mar 2017 16:59:22 +0000 (18:59 +0200)
For making graph queries.

Change-Id: I3ca5088ff2fd43730a64bb7a169d84500a939cd5

bundles/org.simantics.scl.db/scl/Simantics/DB.scl
bundles/org.simantics.scl.db/src/org/simantics/scl/db/SCLFunctions.java

index e42af1878214e75fdf90378fdafa57631b33aa57..70b6b97a63ba7f8fc1575f08786d8aef32cd119a 100644 (file)
@@ -302,6 +302,11 @@ importJava "org.simantics.scl.db.SCLFunctions" where
     unaryQuery :: (a -> <ReadGraph,e> b) -> a -> <ReadGraph> b
     unaryQueryCached :: (a -> <ReadGraph,e> b) -> a -> <ReadGraph> b
 
+    "Makes a new read request with given procedure for calculating the result. The request is cached only if the current request is listened."
+    subquery :: (<ReadGraph,Proc> a) -> <ReadGraph,Proc> a
+    "Makes a new read request with given procedure for calculating the result. The request is always cached."
+    subqueryC :: (<ReadGraph,Proc> a) -> <ReadGraph,Proc> a
+
 importJava "org.simantics.db.layer0.util.Layer0Utils" where
     undo :: () -> <Proc> String
     undoOperations :: Integer -> <Proc> String
index 77f6a6f11ee1e298c1fa104d3fa0b6ef4f97ff3b..9b4d6b8ac92ec90d5c3970285efbe15d7550c008 100644 (file)
@@ -263,4 +263,51 @@ public class SCLFunctions {
        return graph.syncRequest(new SCLUnaryRead(fn, value), TransientCacheAsyncListener.<Object>instance());
     }
     
+
+    private static class Subquery implements Read<Object> {
+        Function q;
+
+        public Subquery(Function q) {
+            this.q = q;
+        }
+
+        @Override
+        public Object perform(ReadGraph graph) throws DatabaseException {
+            SCLContext sclContext = SCLContext.getCurrent();
+            Object oldGraph = sclContext.put("graph", graph);
+            try {
+                return q.apply(Tuple0.INSTANCE);
+            } catch (Throwable e) {
+                if(e instanceof DatabaseException)
+                    throw (DatabaseException)e;
+                else
+                    throw new DatabaseException(e);
+            } finally {
+                sclContext.put("graph", oldGraph);
+            }
+        }
+
+        @Override
+        public int hashCode() {
+            return q.hashCode();
+        }
+        
+        @Override
+        public boolean equals(Object obj) {
+            if(this == obj)
+                return true;
+            if(obj == null || obj.getClass() != getClass())
+                return false;
+            Subquery other = (Subquery)obj;
+            return q.equals(other.q);
+        }
+    }
+
+    public static Object subquery(ReadGraph graph, Function q) throws DatabaseException {
+        return graph.syncRequest(new Subquery(q));
+    }
+
+    public static Object subqueryC(ReadGraph graph, Function q) throws DatabaseException {
+        return graph.syncRequest(new Subquery(q), TransientCacheAsyncListener.<Object>instance());
+    }
 }