Search in sources :

Example 1 with SqlSchema

use of catdata.sql.SqlSchema 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 2 with SqlSchema

use of catdata.sql.SqlSchema 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)2 SqlSchema (catdata.sql.SqlSchema)2 Ctx (catdata.Ctx)1 Pair (catdata.Pair)1 Triple (catdata.Triple)1 Collage (catdata.aql.Collage)1 Schema (catdata.aql.Schema)1 SqlTypeSide (catdata.aql.SqlTypeSide)1 Var (catdata.aql.Var)1 Att (catdata.aql.exp.SchExpRaw.Att)1 En (catdata.aql.exp.SchExpRaw.En)1 Fk (catdata.aql.exp.SchExpRaw.Fk)1 Sym (catdata.aql.exp.TyExpRaw.Sym)1 Ty (catdata.aql.exp.TyExpRaw.Ty)1 SqlColumn (catdata.sql.SqlColumn)1 SqlForeignKey (catdata.sql.SqlForeignKey)1 SqlInstance (catdata.sql.SqlInstance)1 SqlTable (catdata.sql.SqlTable)1 Connection (java.sql.Connection)1 SQLException (java.sql.SQLException)1