Search in sources :

Example 6 with AqlOptions

use of catdata.aql.AqlOptions in project fql by CategoricalData.

the class InstExpJdbcAll method makeSchema.

public Schema<Ty, En, Sym, Fk, Att> makeSchema(AqlEnv env, SqlSchema info, AqlOptions ops) {
    boolean checkJava = !(Boolean) ops.getOrDefault(AqlOption.allow_java_eqs_unsafe);
    TypeSide<Ty, Sym> typeSide = new SqlTypeSide(ops);
    // typeSide.validate(true);
    Collage<Ty, En, Sym, Fk, Att, Void, Void> col0 = new Collage<>(typeSide.collage());
    Set<Triple<Pair<Var, En>, Term<Ty, En, Sym, Fk, Att, Void, Void>, Term<Ty, En, Sym, Fk, Att, Void, Void>>> eqs = new HashSet<>();
    for (SqlTable table : info.tables) {
        col0.ens.add(new En(table.name));
        for (SqlColumn c : table.columns) {
            if (col0.atts.containsKey(new Att(new En(table.name), c.name))) {
                throw new RuntimeException("Name collision: table " + c.table.name + " col " + c.name + " against table " + col0.atts.get(new Att(new En(table.name), c.name)).first + "\n\n.Possible solution: set option jdbc_import_col_seperator so as to avoid name collisions.");
            }
            col0.atts.put(new Att(new En(table.name), c.name), new Pair<>(new En(table.name), new Ty(sqlTypeToAqlType(c.type.name))));
        }
    }
    for (SqlForeignKey fk : info.fks) {
        col0.fks.put(new Fk(new En(fk.source.name), fk.toString()), new Pair<>(new En(fk.source.name), new En(fk.target.name)));
        Var v = new Var("x");
        for (SqlColumn tcol : fk.map.keySet()) {
            SqlColumn scol = fk.map.get(tcol);
            Att l = new Att(new En(scol.table.name), scol.name);
            Att r = new Att(new En(tcol.table.name), tcol.name);
            Term<Ty, En, Sym, Fk, Att, Void, Void> lhs = Term.Att(l, Term.Var(v));
            Term<Ty, En, Sym, Fk, Att, Void, Void> rhs = Term.Att(r, Term.Fk(new Fk(new En(fk.source.name), fk.toString()), Term.Var(v)));
            eqs.add(new Triple<>(new Pair<>(v, new En(fk.source.name)), lhs, rhs));
            col0.eqs.add(new Eq<>(new Ctx<>(new Pair<>(v, Chc.inRight(new En(fk.source.name)))), lhs, rhs));
        }
    }
    DP<Ty, En, Sym, Fk, Att, Void, Void> dp = AqlProver.create(new AqlOptions(options, col0, env.defaults), col0, typeSide.js);
    Schema<Ty, En, Sym, Fk, Att> sch = new Schema<>(typeSide, col0.ens, col0.atts.map, col0.fks.map, eqs, dp, checkJava);
    return sch;
}
Also used : Att(catdata.aql.exp.SchExpRaw.Att) SqlTypeSide(catdata.aql.SqlTypeSide) Var(catdata.aql.Var) Ctx(catdata.Ctx) SqlSchema(catdata.sql.SqlSchema) Schema(catdata.aql.Schema) En(catdata.aql.exp.SchExpRaw.En) HashSet(java.util.HashSet) Pair(catdata.Pair) Ty(catdata.aql.exp.TyExpRaw.Ty) Fk(catdata.aql.exp.SchExpRaw.Fk) Sym(catdata.aql.exp.TyExpRaw.Sym) Triple(catdata.Triple) SqlForeignKey(catdata.sql.SqlForeignKey) SqlTable(catdata.sql.SqlTable) AqlOptions(catdata.aql.AqlOptions) Collage(catdata.aql.Collage) SqlColumn(catdata.sql.SqlColumn)

Example 7 with AqlOptions

use of catdata.aql.AqlOptions in project fql by CategoricalData.

the class InstExpJdbcAll method toInstance.

private Instance<Ty, En, Sym, Fk, Att, Gen, Null<?>, Gen, Null<?>> toInstance(AqlEnv env, SqlInstance inst, SqlSchema info) {
    AqlOptions ops = new AqlOptions(options, null, env.defaults);
    Schema<Ty, En, Sym, Fk, Att> sch = makeSchema(env, info, ops);
    Ctx<En, Collection<Gen>> ens0 = new Ctx<>(Util.newSetsFor0(sch.ens));
    Ctx<Ty, Collection<Null<?>>> tys0 = new Ctx<>();
    Ctx<Gen, Ctx<Fk, Gen>> fks0 = new Ctx<>();
    Ctx<Gen, Ctx<Att, Term<Ty, Void, Sym, Void, Void, Void, Null<?>>>> atts0 = new Ctx<>();
    Ctx<Null<?>, Term<Ty, En, Sym, Fk, Att, Gen, Null<?>>> extraRepr = new Ctx<>();
    for (Ty ty : sch.typeSide.tys) {
        tys0.put(ty, new HashSet<>());
    }
    boolean schemaOnly = (Boolean) ops.getOrDefault(AqlOption.schema_only);
    boolean nullOnErr = (Boolean) ops.getOrDefault(AqlOption.import_null_on_err_unsafe);
    boolean dontCheckClosure = (Boolean) ops.getOrDefault(AqlOption.import_dont_check_closure_unsafe);
    if (!schemaOnly) {
        int fr = 0;
        Map<SqlTable, Map<Map<SqlColumn, Optional<Object>>, Gen>> iso1 = new HashMap<>();
        for (SqlTable table : info.tables) {
            Set<Map<SqlColumn, Optional<Object>>> tuples = inst.get(table);
            Map<Map<SqlColumn, Optional<Object>>, Gen> i1 = new HashMap<>();
            SqlColumn thePk = null;
            if (table.pk.size() == 1) {
                thePk = Util.get0(table.pk);
            }
            for (Map<SqlColumn, Optional<Object>> tuple : tuples) {
                Gen i = new Gen("v" + (fr++));
                /* can't do this until Gen need not be unique
					if (thePk == null) {
						i = new Gen("v" + (fr++));
					} else {
						Optional<Object> x = tuple.get(thePk);
						if (!x.isPresent()) {
							throw new RuntimeException("Primary key col is null in " + tuple);
						}
						i = new Gen(x.get().toString()); //TODO aql
					}
					*/
                i1.put(tuple, i);
                // tuple.keySet().
                // i2.put(i, tuple);
                ens0.get(new En(table.name)).add(i);
                for (SqlColumn c : table.columns) {
                    if (!atts0.containsKey(i)) {
                        atts0.put(i, new Ctx<>());
                    }
                    Optional<Object> val = tuple.get(c);
                    Term<Ty, Void, Sym, Void, Void, Void, Null<?>> xxx = InstExpJdbc.objectToSk(sch, val.orElse(null), i, new Att(new En(table.name), c.name), tys0, extraRepr, false, nullOnErr);
                    atts0.get(i).put(new Att(new En(table.name), c.name), xxx);
                }
            }
            iso1.put(table, i1);
        // iso2.put(table, i2);
        }
        for (SqlForeignKey fk : info.fks) {
            for (Map<SqlColumn, Optional<Object>> in : inst.get(fk.source)) {
                Map<SqlColumn, Optional<Object>> out = inst.follow(in, fk);
                Gen tgen = iso1.get(fk.target).get(out);
                Gen sgen = iso1.get(fk.source).get(in);
                if (!fks0.containsKey(sgen)) {
                    fks0.put(sgen, new Ctx<>());
                }
                fks0.get(sgen).put(new Fk(new En(fk.source.name), fk.toString()), tgen);
            }
        }
    }
    ImportAlgebra<Ty, En, Sym, Fk, Att, Gen, Null<?>> alg = new ImportAlgebra<Ty, En, Sym, Fk, Att, Gen, Null<?>>(sch, ens0, tys0, fks0, atts0, Object::toString, Object::toString, dontCheckClosure);
    return new SaturatedInstance<>(alg, alg, (Boolean) ops.getOrDefault(AqlOption.require_consistency), (Boolean) ops.getOrDefault(AqlOption.allow_java_eqs_unsafe), true, extraRepr);
}
Also used : Att(catdata.aql.exp.SchExpRaw.Att) Null(catdata.Null) HashMap(java.util.HashMap) Ctx(catdata.Ctx) En(catdata.aql.exp.SchExpRaw.En) ImportAlgebra(catdata.aql.ImportAlgebra) Optional(java.util.Optional) Ty(catdata.aql.exp.TyExpRaw.Ty) Fk(catdata.aql.exp.SchExpRaw.Fk) Sym(catdata.aql.exp.TyExpRaw.Sym) Term(catdata.aql.Term) Gen(catdata.aql.exp.InstExpRaw.Gen) SqlForeignKey(catdata.sql.SqlForeignKey) Collection(java.util.Collection) AqlOptions(catdata.aql.AqlOptions) SqlTable(catdata.sql.SqlTable) SaturatedInstance(catdata.aql.fdm.SaturatedInstance) HashMap(java.util.HashMap) Map(java.util.Map) SqlColumn(catdata.sql.SqlColumn)

Example 8 with AqlOptions

use of catdata.aql.AqlOptions in project fql by CategoricalData.

the class AqlOutline method makeTree.

private DefaultMutableTreeNode makeTree(List<String> set, Program<Exp<?>> prog, boolean prefix, boolean alpha, boolean useTypes) {
    DefaultMutableTreeNode root = new DefaultMutableTreeNode();
    AqlTyping G = new AqlTyping(prog, new AqlOptions(prog.options, null, AqlOptions.initialOptions), true);
    for (String k : set) {
        Exp<?> e = prog.exps.get(k);
        if (e.kind().equals(Kind.COMMENT)) {
            continue;
        }
        DefaultMutableTreeNode n = new DefaultMutableTreeNode();
        n.setUserObject(new TreeLabel(k, prefix, G, useTypes));
        asTree(n, alpha, e);
        root.add(n);
    }
    return root;
}
Also used : DefaultMutableTreeNode(javax.swing.tree.DefaultMutableTreeNode) AqlTyping(catdata.aql.exp.AqlTyping) AqlOptions(catdata.aql.AqlOptions)

Example 9 with AqlOptions

use of catdata.aql.AqlOptions in project fql by CategoricalData.

the class InstExpImport method eval.

@Override
public Instance<Ty, En, Sym, Fk, Att, Gen, Null<?>, Gen, Null<?>> eval(AqlEnv env) {
    Schema<Ty, En, Sym, Fk, Att> sch = schema.eval(env);
    for (Ty ty : sch.typeSide.tys) {
        if (!sch.typeSide.js.java_tys.containsKey(ty)) {
            throw new RuntimeException("Import is only allowed onto java types");
        }
    }
    op = new AqlOptions(options, null, env.defaults);
    import_as_theory = (boolean) op.getOrDefault(AqlOption.import_as_theory);
    isJoined = (boolean) op.getOrDefault(AqlOption.import_joined);
    idCol = (String) op.getOrDefault(AqlOption.id_column_name);
    nullOnErr = (Boolean) op.getOrDefault(AqlOption.import_null_on_err_unsafe);
    prepend_entity_on_ids = (Boolean) op.getOrDefault(AqlOption.prepend_entity_on_ids);
    import_col_seperator = (String) op.getOrDefault(AqlOption.import_col_seperator);
    prefix = (String) op.getOrDefault(AqlOption.csv_import_prefix);
    dont_check_closure = (boolean) op.getOrDefault(AqlOption.import_dont_check_closure_unsafe);
    ens0 = new Ctx<>(Util.newSetsFor0(sch.ens));
    tys0 = new Ctx<>(Util.newSetsFor0(sch.typeSide.tys));
    fks0 = new Ctx<>();
    atts0 = new Ctx<>();
    extraRepr = new Ctx<>();
    En last = null;
    try {
        Handle h = start(sch);
        if (!isJoined) {
            /*
				Map<En, Q> ens = new HashMap<>();
				Map<Ty, Q> tys = new HashMap<>();
				Map<Att, Q> atts = new HashMap<>();
				Map<Fk, Q> fks = new HashMap<>();
	
				for (String o : map.keySet()) {
					assertUnambig(o, sch);
					Q q = map.get(o);
					if (sch.typeSide.tys.contains(new Ty(o))) {
						tys.put(new Ty(o), q);
					} else if (sch.ens.contains(new En(o))) {
						ens.put(new En(o), q);
					} else if (sch.atts.map.containsKey(new Att(o))) {
						atts.put(new Att(o), q);
					} else if (sch.fks.map.containsKey(new Fk(o))) {
						fks.put(new Fk(o), q);
					} //TODO aql
				}
				totalityCheck(sch, ens, tys, atts, fks);
	
				for (En en : ens.keySet()) {
					shreddedEn(h, en, ens.get(en), sch);
				}
				for (Fk fk : fks.keySet()) {
					shreddedFk(h, fk, fks.get(fk), sch);
				}
				for (Att att : atts.keySet()) {
					shreddedAtt(h, att, atts.get(att), sch);
				}
	*/
            throw new RuntimeException("Unjoined form no longer supported.  To request, contact us.");
        } else {
            for (En en : sch.ens) {
                last = en;
                Q z = map.get(en.str);
                // TODO: aql: this check isn't needed
                // if (z == null) {
                // throw new RuntimeException("No binding given for " + en);
                // }
                joinedEn(h, en, z, sch);
            }
        }
        end(h);
    } catch (Exception exn) {
        exn.printStackTrace();
        String pre = "";
        if (last != null) {
            pre = "On entity " + last + ", ";
        }
        throw new RuntimeException(pre + exn.getMessage() + "\n\n" + getHelpStr());
    }
    if (import_as_theory) {
        return forTheory(sch, ens0, tys0, fks0, atts0, op);
    }
    ImportAlgebra<Ty, En, Sym, Fk, Att, Gen, Null<?>> alg = new ImportAlgebra<>(sch, ens0, tys0, fks0, atts0, Object::toString, Object::toString, dont_check_closure);
    return new SaturatedInstance<>(alg, alg, (Boolean) op.getOrDefault(AqlOption.require_consistency), (Boolean) op.getOrDefault(AqlOption.allow_java_eqs_unsafe), true, extraRepr);
}
Also used : Att(catdata.aql.exp.SchExpRaw.Att) ImportAlgebra(catdata.aql.ImportAlgebra) Null(catdata.Null) Ty(catdata.aql.exp.TyExpRaw.Ty) Fk(catdata.aql.exp.SchExpRaw.Fk) Sym(catdata.aql.exp.TyExpRaw.Sym) En(catdata.aql.exp.SchExpRaw.En) Gen(catdata.aql.exp.InstExpRaw.Gen) AqlOptions(catdata.aql.AqlOptions) SaturatedInstance(catdata.aql.fdm.SaturatedInstance)

Example 10 with AqlOptions

use of catdata.aql.AqlOptions in project fql by CategoricalData.

the class InstExpJdbcAll method eval.

@Override
public Instance<Ty, En, Sym, Fk, Att, Gen, Null<?>, Gen, Null<?>> eval(AqlEnv env) {
    String toGet = jdbcString;
    String driver = clazz;
    AqlOptions op = new AqlOptions(options, null, env.defaults);
    if (clazz.trim().isEmpty()) {
        driver = (String) op.getOrDefault(AqlOption.jdbc_default_class);
        Util.checkClass(driver);
    }
    if (jdbcString.trim().isEmpty()) {
        toGet = (String) op.getOrDefault(AqlOption.jdbc_default_string);
    }
    try (Connection conn = DriverManager.getConnection(toGet)) {
        SqlSchema sch = new SqlSchema(conn.getMetaData());
        boolean noDistinct = (Boolean) op.getOrDefault(AqlOption.jdbc_no_distinct_unsafe);
        boolean schemaOnly = (Boolean) op.getOrDefault(AqlOption.schema_only);
        boolean nullOnErr = (Boolean) op.getOrDefault(AqlOption.import_null_on_err_unsafe);
        if (!schemaOnly) {
            SqlInstance inst = new SqlInstance(sch, conn, nullOnErr, noDistinct);
            return toInstance(env, inst, sch);
        }
        return toInstance(env, null, sch);
    } catch (SQLException exn) {
        exn.printStackTrace();
        throw new RuntimeException("JDBC exception: " + exn.getMessage());
    }
}
Also used : SqlInstance(catdata.sql.SqlInstance) SqlSchema(catdata.sql.SqlSchema) SQLException(java.sql.SQLException) Connection(java.sql.Connection) AqlOptions(catdata.aql.AqlOptions)

Aggregations

AqlOptions (catdata.aql.AqlOptions)19 En (catdata.aql.exp.SchExpRaw.En)17 Sym (catdata.aql.exp.TyExpRaw.Sym)17 Ty (catdata.aql.exp.TyExpRaw.Ty)17 Att (catdata.aql.exp.SchExpRaw.Att)16 Fk (catdata.aql.exp.SchExpRaw.Fk)16 Ctx (catdata.Ctx)13 Collage (catdata.aql.Collage)12 Pair (catdata.Pair)11 Term (catdata.aql.Term)11 HashSet (java.util.HashSet)10 Chc (catdata.Chc)9 RawTerm (catdata.aql.RawTerm)9 Gen (catdata.aql.exp.InstExpRaw.Gen)9 Triple (catdata.Triple)7 Var (catdata.aql.Var)7 Sk (catdata.aql.exp.InstExpRaw.Sk)7 Collection (java.util.Collection)7 HashMap (java.util.HashMap)7 LinkedList (java.util.LinkedList)6