Search in sources :

Example 11 with En

use of catdata.aql.exp.SchExpRaw.En in project fql by CategoricalData.

the class InstExpJdbc method shreddedAtt.

@Override
protected void shreddedAtt(Connection conn, Att att, String s, Schema<Ty, En, Sym, Fk, Att> sch) throws Exception {
    Statement stmt = conn.createStatement();
    stmt.execute(s);
    ResultSet rs = stmt.getResultSet();
    ResultSetMetaData rsmd = rs.getMetaData();
    int columnsNumber = rsmd.getColumnCount();
    if (columnsNumber != 2) {
        stmt.close();
        rs.close();
        conn.close();
        throw new RuntimeException("Error in " + att + ": Expected 2 columns but received " + columnsNumber);
    }
    while (rs.next()) {
        Object lhs = rs.getObject(1);
        if (lhs == null) {
            stmt.close();
            rs.close();
            conn.close();
            throw new RuntimeException("Error in " + att + ": Encountered a NULL column 1");
        }
        Object rhs = rs.getObject(2);
        En en = sch.atts.get(att).first;
        if (!atts0.map.containsKey(toGen(en, lhs.toString()))) {
            atts0.put(toGen(en, lhs.toString()), new Ctx<>());
        }
        atts0.get(toGen(en, lhs.toString())).put(att, objectToSk(sch, rhs, toGen(en, lhs.toString()), att, tys0, extraRepr, false, nullOnErr));
    }
    stmt.close();
    rs.close();
}
Also used : ResultSetMetaData(java.sql.ResultSetMetaData) Statement(java.sql.Statement) ResultSet(java.sql.ResultSet) En(catdata.aql.exp.SchExpRaw.En)

Example 12 with En

use of catdata.aql.exp.SchExpRaw.En in project fql by CategoricalData.

the class InstExpJdbc method checkColumns.

private void checkColumns(En en, String s, Schema<Ty, En, Sym, Fk, Att> sch, ResultSetMetaData rsmd) throws SQLException {
    Set<String> colNames = new HashSet<>();
    for (int i = 1; i <= rsmd.getColumnCount(); i++) {
        String colName = rsmd.getColumnLabel(i);
        if (!(colName.equalsIgnoreCase(idCol) || Util.containsUpToCase(sch.attsFrom(en).stream().map(x -> x.str).collect(Collectors.toList()), colName) || Util.containsUpToCase(sch.fksFrom(en).stream().map(x -> x.str).collect(Collectors.toList()), colName))) {
            throw new RuntimeException("Column name " + colName + " does not refer to a foreign key or attribute in \n\n" + s);
        }
        colNames.add(colName);
    }
    for (Att att : sch.attsFrom(en)) {
        if (!Util.containsUpToCase(colNames, att.str)) {
            throw new RuntimeException("Attribute " + att + " has no column in \n\n" + s);
        }
    }
    for (Fk fk : sch.fksFrom(en)) {
        if (!Util.containsUpToCase(colNames, fk.str)) {
            throw new RuntimeException("Foreign key " + fk + " has no column in \n\n" + s);
        }
    }
    if (!Util.containsUpToCase(colNames, idCol)) {
        throw new RuntimeException("No ID column " + idCol + " in \n\n" + s);
    }
}
Also used : Ctx(catdata.Ctx) Fk(catdata.aql.exp.SchExpRaw.Fk) Connection(java.sql.Connection) En(catdata.aql.exp.SchExpRaw.En) Sym(catdata.aql.exp.TyExpRaw.Sym) Util(catdata.Util) Set(java.util.Set) Gen(catdata.aql.exp.InstExpRaw.Gen) AqlOption(catdata.aql.AqlOptions.AqlOption) Att(catdata.aql.exp.SchExpRaw.Att) Collectors(java.util.stream.Collectors) HashSet(java.util.HashSet) SQLException(java.sql.SQLException) List(java.util.List) Schema(catdata.aql.Schema) ResultSet(java.sql.ResultSet) Statement(java.sql.Statement) HashCodeBuilder(org.apache.commons.lang3.builder.HashCodeBuilder) DriverManager(java.sql.DriverManager) ResultSetMetaData(java.sql.ResultSetMetaData) Pair(catdata.Pair) Ty(catdata.aql.exp.TyExpRaw.Ty) EqualsBuilder(org.apache.commons.lang3.builder.EqualsBuilder) Att(catdata.aql.exp.SchExpRaw.Att) Fk(catdata.aql.exp.SchExpRaw.Fk) HashSet(java.util.HashSet)

Example 13 with En

use of catdata.aql.exp.SchExpRaw.En 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 14 with En

use of catdata.aql.exp.SchExpRaw.En 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 15 with En

use of catdata.aql.exp.SchExpRaw.En in project fql by CategoricalData.

the class ColimitSchema method renameEntity.

public ColimitSchema<N> renameEntity(En src, En dst, boolean checkJava) {
    if (!schemaStr.ens.contains(src)) {
        throw new RuntimeException(src + " is not an entity in \n" + schemaStr);
    }
    if (schemaStr.ens.contains(dst)) {
        throw new RuntimeException(dst + " is already an entity in \n" + schemaStr);
    }
    Mapping<Ty, En, Sym, Fk, Att, En, Fk, Att> isoToUser = Mapping.id(schemaStr);
    Mapping<Ty, En, Sym, Fk, Att, En, Fk, Att> isoFromUser = Mapping.id(schemaStr);
    Function<Fk, Fk> updateFk = x -> {
        if (x.en.equals(src)) {
            return new Fk(dst, x.str);
        }
        return x;
    };
    Function<Fk, Fk> deUpdateFk = x -> {
        if (x.en.equals(dst)) {
            return new Fk(src, x.str);
        }
        return x;
    };
    Function<Att, Att> updateAtt = x -> {
        if (x.en.equals(src)) {
            return new Att(dst, x.str);
        }
        return x;
    };
    Function<Att, Att> deUpdateAtt = x -> {
        if (x.en.equals(dst)) {
            return new Att(src, x.str);
        }
        return x;
    };
    Set<En> ens = new HashSet<>(schemaStr.ens);
    ens.remove(src);
    ens.add(dst);
    Map<Att, Pair<En, Ty>> atts = new HashMap<>();
    for (Att k : schemaStr.atts.keySet()) {
        Pair<En, Ty> v = schemaStr.atts.get(k);
        En s = v.first.equals(src) ? dst : v.first;
        atts.put(updateAtt.apply(k), new Pair<>(s, v.second));
    }
    Map<Fk, Pair<En, En>> fks = new HashMap<>();
    for (Fk k : schemaStr.fks.keySet()) {
        Pair<En, En> v = schemaStr.fks.get(k);
        En s = v.first.equals(src) ? dst : v.first;
        En t = v.second.equals(src) ? dst : v.second;
        fks.put(updateFk.apply(k), new Pair<>(s, t));
    }
    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 (Triple<Pair<Var, En>, Term<Ty, En, Sym, Fk, Att, Void, Void>, Term<Ty, En, Sym, Fk, Att, Void, Void>> eq : schemaStr.eqs) {
        Pair<Var, En> v = eq.first;
        En t = v.second.equals(src) ? dst : v.second;
        eqs.add(new Triple<>(new Pair<>(v.first, t), eq.second.mapFk(updateFk).mapAtt(updateAtt), eq.third.mapFk(updateFk).mapAtt(updateAtt)));
    }
    DP<Ty, En, Sym, Fk, Att, Void, Void> dp = new DP<Ty, En, Sym, Fk, Att, Void, Void>() {

        @Override
        public String toStringProver() {
            return "rename entity of " + schemaStr.dp.toStringProver();
        }

        @Override
        public // TODO aql check this
        boolean eq(Ctx<Var, Chc<Ty, En>> ctx, Term<Ty, En, Sym, Fk, Att, Void, Void> lhs, Term<Ty, En, Sym, Fk, Att, Void, Void> rhs) {
            return schemaStr.dp.eq(ctx.map(v -> v.left ? v : (v.r.equals(dst) ? Chc.inRight(src) : v)), lhs.mapFk(deUpdateFk).mapAtt(deUpdateAtt), rhs.mapFk(deUpdateFk).mapAtt(deUpdateAtt));
        }
    };
    Schema<Ty, En, Sym, Fk, Att> schemaStr2 = // TODO aql java
    new Schema<>(ty, ens, atts, fks, eqs, dp, checkJava);
    Map<En, En> ensM = new HashMap<>();
    for (En k : schemaStr.ens) {
        ensM.put(k, k.equals(src) ? dst : k);
    }
    Map<Att, Triple<Var, En, Term<Ty, En, Sym, Fk, Att, Void, Void>>> attsM = new HashMap<>();
    for (Att k : schemaStr.atts.keySet()) {
        attsM.put(k, new Triple<Var, En, Term<Ty, En, Sym, Fk, Att, Void, Void>>(isoToUser.atts.get(k).first, isoToUser.atts.get(k).second.equals(src) ? dst : isoToUser.atts.get(k).second, isoToUser.atts.get(k).third.mapAtt(updateAtt).mapFk(updateFk)));
    }
    Map<Fk, Pair<En, List<Fk>>> fksM = new HashMap<>();
    for (Fk k : schemaStr.fks.keySet()) {
        fksM.put(k, new Pair<>(isoToUser.fks.get(k).first.equals(src) ? dst : isoToUser.fks.get(k).first, isoToUser.fks.get(k).second.stream().map(updateFk).collect(Collectors.toList())));
    }
    isoToUser = new Mapping<>(ensM, attsM, fksM, schemaStr, schemaStr2, checkJava);
    Map<En, En> ensM2 = new HashMap<>();
    for (En k : schemaStr2.ens) {
        ensM2.put(k, k.equals(dst) ? src : k);
    }
    Map<Att, Triple<Var, En, Term<Ty, En, Sym, Fk, Att, Void, Void>>> attsM2 = new HashMap<>();
    for (Att k : schemaStr2.atts.keySet()) {
        attsM2.put(updateAtt.apply(k), new Triple<>(isoFromUser.atts.get(deUpdateAtt.apply(k)).first, isoFromUser.atts.get(deUpdateAtt.apply(k)).second.equals(dst) ? src : isoFromUser.atts.get(deUpdateAtt.apply(k)).second, isoFromUser.atts.get(deUpdateAtt.apply(k)).third));
    }
    Map<Fk, Pair<En, List<Fk>>> fksM2 = new HashMap<>();
    for (Fk k : schemaStr2.fks.keySet()) {
        fksM2.put(updateFk.apply(k), new Pair<>(isoFromUser.fks.get(deUpdateFk.apply(k)).first.equals(dst) ? src : isoFromUser.fks.get(deUpdateFk.apply(k)).first, isoFromUser.fks.get(deUpdateFk.apply(k)).second.stream().map(deUpdateFk).collect(Collectors.toList())));
    }
    isoFromUser = new Mapping<>(ensM2, attsM2, fksM2, schemaStr2, schemaStr, checkJava);
    return wrap(isoToUser, isoFromUser);
}
Also used : Att(catdata.aql.exp.SchExpRaw.Att) Ctx(catdata.Ctx) En(catdata.aql.exp.SchExpRaw.En) Sym(catdata.aql.exp.TyExpRaw.Sym) Chc(catdata.Chc) HashMap(java.util.HashMap) Gen(catdata.aql.exp.InstExpRaw.Gen) AqlOption(catdata.aql.AqlOptions.AqlOption) Function(java.util.function.Function) HashSet(java.util.HashSet) Map(java.util.Map) UnionFind(catdata.graph.UnionFind) Pair(catdata.Pair) Quad(catdata.Quad) Fk(catdata.aql.exp.SchExpRaw.Fk) TyExpRaw(catdata.aql.exp.TyExpRaw) Util(catdata.Util) Set(java.util.Set) Att(catdata.aql.exp.SchExpRaw.Att) DMG(catdata.graph.DMG) Collectors(java.util.stream.Collectors) List(java.util.List) Sk(catdata.aql.exp.InstExpRaw.Sk) SchExpRaw(catdata.aql.exp.SchExpRaw) Triple(catdata.Triple) Ty(catdata.aql.exp.TyExpRaw.Ty) HashMap(java.util.HashMap) Ctx(catdata.Ctx) 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)

Aggregations

En (catdata.aql.exp.SchExpRaw.En)44 Att (catdata.aql.exp.SchExpRaw.Att)36 Fk (catdata.aql.exp.SchExpRaw.Fk)36 Ty (catdata.aql.exp.TyExpRaw.Ty)35 Sym (catdata.aql.exp.TyExpRaw.Sym)34 Ctx (catdata.Ctx)27 Pair (catdata.Pair)27 Gen (catdata.aql.exp.InstExpRaw.Gen)22 HashSet (java.util.HashSet)20 Chc (catdata.Chc)18 Triple (catdata.Triple)17 AqlOptions (catdata.aql.AqlOptions)17 List (java.util.List)17 Sk (catdata.aql.exp.InstExpRaw.Sk)16 HashMap (java.util.HashMap)15 LinkedList (java.util.LinkedList)14 Collage (catdata.aql.Collage)13 Term (catdata.aql.Term)13 RawTerm (catdata.aql.RawTerm)11 Set (java.util.Set)11