From 061842cb1fe6ec3d8da79b8ee106c4d2385cf793 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?J=C3=A9r=C3=B4me=20Euzenat?= <Jerome.Euzenat@inria.fr>
Date: Fri, 20 Nov 2015 12:33:11 +0000
Subject: [PATCH] - added storage for EDOAL Aggregate (affects Apply) - moved
 to VERSION 4.8

---
 .../exmo/align/service/EDOALSQLCache.java     | 37 +++++++++++++++++--
 .../exmo/align/service/SQLCache.java          | 16 ++++----
 2 files changed, 40 insertions(+), 13 deletions(-)

diff --git a/src/fr/inrialpes/exmo/align/service/EDOALSQLCache.java b/src/fr/inrialpes/exmo/align/service/EDOALSQLCache.java
index fe0bf95e..bd6420f4 100644
--- a/src/fr/inrialpes/exmo/align/service/EDOALSQLCache.java
+++ b/src/fr/inrialpes/exmo/align/service/EDOALSQLCache.java
@@ -80,6 +80,7 @@ import fr.inrialpes.exmo.align.impl.edoal.Transformation;
 import fr.inrialpes.exmo.align.impl.edoal.ValueExpression;
 import fr.inrialpes.exmo.align.impl.edoal.Value;
 import fr.inrialpes.exmo.align.impl.edoal.Apply;
+import fr.inrialpes.exmo.align.impl.edoal.Aggregate;
 import fr.inrialpes.exmo.align.impl.edoal.Datatype;
 import fr.inrialpes.exmo.align.impl.edoal.EDOALCell;
 import fr.inrialpes.exmo.align.impl.edoal.EDOALVisitor;
@@ -155,6 +156,7 @@ public class EDOALSQLCache {
     public static final int PROPERTY = 6;
     public static final int CLASS = 7;
     public static final int REST = 8;
+    public static final int AGGREGATE = 4;
 
     public EDOALSQLCache( DBService serv ) {
 	service = serv;
@@ -790,7 +792,8 @@ public class EDOALSQLCache {
 		    if ( type == INSTANCE ) return extractInstanceExpression( rs.getLong( "joinid" ) );
 		    else if ( type == VALUE ) return extractValue( rs.getLong( "joinid" ) );
 		    else if ( type == PATH ) return extractPathExpression( rs.getLong( "joinid" ) );
-		    else if ( type == APPLY ) return extractApply( rs.getLong( "joinid" ) );
+		    else if ( type == APPLY ) return extractApply( rs.getLong( "joinid" ), 0 );
+		    else if ( type == AGGREGATE ) return extractApply( rs.getLong( "joinid" ), 1 );
 		    else throw new AlignmentException( "Unknown ValueExpression type "+type );
 		} else {
 		    throw new AlignmentException( "Cannot retrieve value expression "+intid );
@@ -813,6 +816,9 @@ public class EDOALSQLCache {
 	} else if ( e instanceof Apply ) {
 	    idres = visit( (Apply)e );
 	    type = APPLY;
+	} else if ( e instanceof Aggregate ) {
+	    idres = visit( (Aggregate)e );
+	    type = AGGREGATE;
 	} else throw new AlignmentException( "Unknown ValueExpression "+e );
 	return registerExpression( "valueexpr", type, idres );
     }
@@ -887,7 +893,7 @@ public class EDOALSQLCache {
     }
 
     // (play with NULL)
-    public Apply extractApply( long intid ) throws SQLException, AlignmentException {
+    public ValueExpression extractApply( long intid, int type ) throws SQLException, AlignmentException {
 	try ( Statement st = service.getConnection().createStatement() ) {
 		ResultSet rs = st.executeQuery( "SELECT operation FROM apply WHERE intid='"+intid+"'" );
 		if ( rs.next() ) {
@@ -899,7 +905,11 @@ public class EDOALSQLCache {
 				while ( rs2.next() ) {
 				    args.add( extractValueExpression( rs2.getLong( "id" ) ) );
 				}
-				return new Apply( opuri, args );
+				if ( type == 0 ) {
+				    return new Apply( opuri, args );
+				} else {
+				    return new Aggregate( opuri, args );
+				}				    
 			    }
 		    } catch ( URISyntaxException urisex ) {
 			throw new AlignmentException( "Invalid operation URI", urisex );
@@ -912,7 +922,26 @@ public class EDOALSQLCache {
 	// Get the constructor
 	final URI op = e.getOperation();
 	// Create the relexpr
-	try ( PreparedStatement st2 = createInsertStatement( "INSERT INTO apply (operation) VALUES (?)" ) ) {
+	try ( PreparedStatement st2 = createInsertStatement( "INSERT INTO apply (type,operation) VALUES (0,?)" ) ) {
+		st2.setString( 1, op.toString() );
+		long exprres = executeUpdateWithId( st2, "apply" );
+		// Iterate on arguments
+		try ( Statement st = service.getConnection().createStatement() ) {
+			for ( final ValueExpression ve : e.getArguments() ) {
+			    long pres = visit( ve );
+			    st.executeUpdate( "INSERT INTO arglist (intid,id) VALUES ('"+exprres+"','"+pres+"')" );
+			}
+		    }
+		// Return the expr
+		return exprres;
+	    }
+    }
+
+    public long visit( final Aggregate e ) throws SQLException, AlignmentException {
+	// Get the constructor
+	final URI op = e.getOperation();
+	// Create the relexpr
+	try ( PreparedStatement st2 = createInsertStatement( "INSERT INTO apply (type,operation) VALUES (1,?)" ) ) {
 		st2.setString( 1, op.toString() );
 		long exprres = executeUpdateWithId( st2, "apply" );
 		// Iterate on arguments
diff --git a/src/fr/inrialpes/exmo/align/service/SQLCache.java b/src/fr/inrialpes/exmo/align/service/SQLCache.java
index 627c7c79..38c3a46d 100644
--- a/src/fr/inrialpes/exmo/align/service/SQLCache.java
+++ b/src/fr/inrialpes/exmo/align/service/SQLCache.java
@@ -76,7 +76,7 @@ public class SQLCache extends VolatilCache implements Cache {
     String port = null;
     int rights = 1; // writing rights in the database (default is 1)
 
-    final int VERSION = 472; // Version of the API to be stored in the database
+    final int VERSION = 480; // Version of the API to be stored in the database
     /* 300: initial database format
        301: ADDED alignment id as primary key
        302: ALTERd cached/stored/ouri tag forms
@@ -99,8 +99,8 @@ public class SQLCache extends VolatilCache implements Cache {
             // URIINdex:
             ADDED multiple uris for alignments (?)
             CHANGED the alext namespace
-       471: ADDED management of EDOAL alignments
-       472: ADDED reltype and conftype to Alignment table
+       480: ADDED management of EDOAL alignments
+            ADDED reltype and conftype to Alignment table
      */
 
     DBService service = null;
@@ -970,6 +970,7 @@ public class SQLCache extends VolatilCache implements Cache {
 
       CREATE TABLE apply (
       intid BIGINT NOT NULL AUTO_INCREMENT, 
+      type INT,
       operation VARCHAR(255), 
       PRIMARY KEY (intid))
 
@@ -1142,7 +1143,7 @@ public class SQLCache extends VolatilCache implements Cache {
 	st.executeUpdate("CREATE TABLE instexpr (intid "+SERIAL+", uri VARCHAR(250), var VARCHAR(250), PRIMARY KEY (intid))");
 	st.executeUpdate("CREATE TABLE literal (intid "+SERIAL+", type BIGINT, value VARCHAR(500), PRIMARY KEY (intid))");
 	st.executeUpdate("CREATE TABLE typeexpr (intid "+SERIAL+", uri VARCHAR(250), PRIMARY KEY (intid))");
-	st.executeUpdate("CREATE TABLE apply (intid "+SERIAL+", operation VARCHAR(255), PRIMARY KEY (intid))");
+	st.executeUpdate("CREATE TABLE apply (intid "+SERIAL+", type INT, operation VARCHAR(255), PRIMARY KEY (intid))");
 	st.executeUpdate("CREATE TABLE arglist (intid BIGINT NOT NULL, id BIGINT NOT NULL)");
 	st.executeUpdate("CREATE TABLE classexpr (intid "+SERIAL+", type INT, joinid BIGINT, var VARCHAR(250), PRIMARY KEY (intid))");
 	st.executeUpdate("CREATE TABLE classid (intid "+SERIAL+", uri VARCHAR(250), PRIMARY KEY (intid))");
@@ -1446,13 +1447,10 @@ public class SQLCache extends VolatilCache implements Cache {
 			    st2.executeUpdate("UPDATE extension SET uri='"+Namespace.ALIGNMENT.uri+"#' WHERE uri='"+Namespace.ALIGNMENT.uri+"'");
 			    st2.executeUpdate("UPDATE extension SET uri='"+Namespace.EXT.uri+"' WHERE uri='"+Namespace.ALIGNMENT.uri+"#' AND (tag='time' OR tag='method' OR tag='pretty')");
 			}
-			if ( version < 471 ) {
-			    logger.info("Upgrading to version 4.71");
+			if ( version < 480 ) {
+			    logger.info("Upgrading to version 4.8");
 			    logger.info("Creating EDOAL tables");
 			    initEDOALTables( createStatement() );
-			}
-			if ( version < 472 ) {
-			    logger.info("Upgrading to version 4.72");
 			    logger.info("Adding reltype attribute");
 			    st.executeUpdate("ALTER TABLE alignment ADD reltype VARCHAR(255);");
 			    st.executeUpdate("ALTER TABLE alignment ADD conftype VARCHAR(255);");
-- 
GitLab