From: Hannu Niemistö Date: Fri, 24 Mar 2017 13:00:43 +0000 (+0200) Subject: (refs #7090) New functions subquery and subqueryC X-Git-Tag: v1.28.0~40 X-Git-Url: https://gerrit.simantics.org/r/gitweb?a=commitdiff_plain;h=5dfe0a4816064b35e1b21c7754fc64ac52dde0c0;p=simantics%2Fplatform.git (refs #7090) New functions subquery and subqueryC For making graph queries. Change-Id: I3ca5088ff2fd43730a64bb7a169d84500a939cd5 --- diff --git a/bundles/org.simantics.scl.db/scl/Simantics/DB.scl b/bundles/org.simantics.scl.db/scl/Simantics/DB.scl index e42af1878..70b6b97a6 100644 --- a/bundles/org.simantics.scl.db/scl/Simantics/DB.scl +++ b/bundles/org.simantics.scl.db/scl/Simantics/DB.scl @@ -302,6 +302,11 @@ importJava "org.simantics.scl.db.SCLFunctions" where unaryQuery :: (a -> b) -> a -> b unaryQueryCached :: (a -> b) -> a -> 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 :: ( a) -> a + "Makes a new read request with given procedure for calculating the result. The request is always cached." + subqueryC :: ( a) -> a + importJava "org.simantics.db.layer0.util.Layer0Utils" where undo :: () -> String undoOperations :: Integer -> String diff --git a/bundles/org.simantics.scl.db/src/org/simantics/scl/db/SCLFunctions.java b/bundles/org.simantics.scl.db/src/org/simantics/scl/db/SCLFunctions.java index 77f6a6f11..9b4d6b8ac 100644 --- a/bundles/org.simantics.scl.db/src/org/simantics/scl/db/SCLFunctions.java +++ b/bundles/org.simantics.scl.db/src/org/simantics/scl/db/SCLFunctions.java @@ -263,4 +263,51 @@ public class SCLFunctions { return graph.syncRequest(new SCLUnaryRead(fn, value), TransientCacheAsyncListener.instance()); } + + private static class Subquery implements Read { + 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.instance()); + } }