Search in sources :

Example 1 with InstOps

use of catdata.fql.decl.InstOps in project fql by CategoricalData.

the class JDBCBridge method run.

public static Triple<Map<String, Set<Map<Object, Object>>>, String, List<Throwable>> run(FQLProgram prog) {
    Map<String, Set<Map<Object, Object>>> ret = new HashMap<>();
    List<Throwable> exns = new LinkedList<>();
    InstOps ops = new InstOps(prog);
    PSMInterp interp = new PSMInterp();
    Connection Conn;
    Statement Stmt = null;
    List<PSM> sqls = new LinkedList<>();
    try {
        switch(DefunctGlobalOptions.debug.fql.sqlKind) {
            case H2:
                Class.forName("org.h2.Driver");
                Conn = DriverManager.getConnection("jdbc:h2:mem:");
                Stmt = Conn.createStatement();
                Stmt.execute("SET @GUID = 0");
                break;
            case JDBC:
                Class.forName(DefunctGlobalOptions.debug.fql.jdbcClass);
                Conn = DriverManager.getConnection(DefunctGlobalOptions.debug.fql.jdbcUrl);
                Stmt = Conn.createStatement();
                String[] prel = DefunctGlobalOptions.debug.fql.prelude.split(";");
                for (String s : prel) {
                    Stmt.execute(s);
                }
                break;
            case NATIVE:
                break;
            default:
                throw new RuntimeException();
        }
        for (String k : prog.order) {
            InstExp v1 = prog.insts.get(k);
            TransExp v2 = prog.transforms.get(k);
            try {
                if (v1 != null) {
                    sqls.addAll(maybeExecInstance(ops, prog, Stmt, k, v1, interp, ret));
                } else if (v2 != null) {
                    sqls.addAll(maybeExecTransform(ops, prog, Stmt, k, v2, interp, ret));
                }
            } catch (Throwable re) {
                String thing = (v1 == null) ? "transform" : "instance";
                re.printStackTrace();
                LineException exn = new LineException(re.getLocalizedMessage(), k, thing);
                if (DefunctGlobalOptions.debug.fql.continue_on_error) {
                    exns.add(exn);
                } else {
                    if (DefunctGlobalOptions.debug.fql.sqlKind == SQLKIND.JDBC) {
                        String[] prel0 = DefunctGlobalOptions.debug.fql.afterlude.split(";");
                        for (String s : prel0) {
                            if (!s.trim().isEmpty()) {
                                if (Stmt == null) {
                                    throw new RuntimeException("Anomaly: please report");
                                }
                                Stmt.execute(s);
                            }
                        }
                    }
                    throw exn;
                }
            }
        }
        List<PSM> drops = Driver.computeDrops(prog);
        if (DefunctGlobalOptions.debug.fql.sqlKind == SQLKIND.JDBC) {
            for (PSM dr : drops) {
                if (Stmt == null) {
                    throw new RuntimeException("Anomaly: please report");
                }
                Stmt.execute(dr.toPSM());
            }
            String[] prel0 = DefunctGlobalOptions.debug.fql.afterlude.split(";");
            for (String s : prel0) {
                if (!s.trim().isEmpty()) {
                    if (Stmt == null) {
                        throw new RuntimeException("Anomaly: please report");
                    }
                    Stmt.execute(s);
                }
            }
        }
        String str;
        try {
            str = DefunctGlobalOptions.debug.fql.prelude + "\n\n" + PSMGen.prettyPrint(sqls) + "\n\n" + (drops.isEmpty() ? "" : PSMGen.prettyPrint(drops) + "\n\n") + DefunctGlobalOptions.debug.fql.afterlude + "\n\n";
        } catch (RuntimeException re) {
            str = re.getLocalizedMessage();
        }
        return new Triple<>(ret, str, exns);
    } catch (Exception exception) {
        if (exception instanceof LineException) {
            throw ((LineException) exception);
        }
        exception.printStackTrace();
        throw new RuntimeException(exception.getLocalizedMessage());
    }
}
Also used : HashSet(java.util.HashSet) ResultSet(java.sql.ResultSet) Set(java.util.Set) PSMInterp(catdata.fql.sql.PSMInterp) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) Statement(java.sql.Statement) TransExp(catdata.fql.decl.TransExp) Connection(java.sql.Connection) InstOps(catdata.fql.decl.InstOps) PSM(catdata.fql.sql.PSM) PropPSM(catdata.fql.sql.PropPSM) ExpPSM(catdata.fql.sql.ExpPSM) LinkedList(java.util.LinkedList) LineException(catdata.LineException) SQLException(java.sql.SQLException) Triple(catdata.Triple) InstExp(catdata.fql.decl.InstExp) LineException(catdata.LineException)

Aggregations

LineException (catdata.LineException)1 Triple (catdata.Triple)1 InstExp (catdata.fql.decl.InstExp)1 InstOps (catdata.fql.decl.InstOps)1 TransExp (catdata.fql.decl.TransExp)1 ExpPSM (catdata.fql.sql.ExpPSM)1 PSM (catdata.fql.sql.PSM)1 PSMInterp (catdata.fql.sql.PSMInterp)1 PropPSM (catdata.fql.sql.PropPSM)1 Connection (java.sql.Connection)1 ResultSet (java.sql.ResultSet)1 SQLException (java.sql.SQLException)1 Statement (java.sql.Statement)1 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1 LinkedHashMap (java.util.LinkedHashMap)1 LinkedList (java.util.LinkedList)1 Set (java.util.Set)1