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;
}
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());
}
}
Aggregations