use of org.apache.calcite.model.ModelHandler in project calcite by apache.
the class Driver method createHandler.
@Override
protected Handler createHandler() {
return new HandlerImpl() {
@Override
public void onConnectionInit(AvaticaConnection connection_) throws SQLException {
final CalciteConnectionImpl connection = (CalciteConnectionImpl) connection_;
super.onConnectionInit(connection);
final String model = model(connection);
if (model != null) {
try {
new ModelHandler(connection, model);
} catch (IOException e) {
throw new SQLException(e);
}
}
connection.init();
}
String model(CalciteConnectionImpl connection) {
String model = connection.config().model();
if (model != null) {
return model;
}
SchemaFactory schemaFactory = connection.config().schemaFactory(SchemaFactory.class, null);
final Properties info = connection.getProperties();
final String schemaName = Util.first(connection.config().schema(), "adhoc");
if (schemaFactory == null) {
final JsonSchema.Type schemaType = connection.config().schemaType();
if (schemaType != null) {
switch(schemaType) {
case JDBC:
schemaFactory = JdbcSchema.Factory.INSTANCE;
break;
case MAP:
schemaFactory = AbstractSchema.Factory.INSTANCE;
break;
}
}
}
if (schemaFactory != null) {
final JsonBuilder json = new JsonBuilder();
final Map<String, Object> root = json.map();
root.put("version", "1.0");
root.put("defaultSchema", schemaName);
final List<Object> schemaList = json.list();
root.put("schemas", schemaList);
final Map<String, Object> schema = json.map();
schemaList.add(schema);
schema.put("type", "custom");
schema.put("name", schemaName);
schema.put("factory", schemaFactory.getClass().getName());
final Map<String, Object> operandMap = json.map();
schema.put("operand", operandMap);
for (Map.Entry<String, String> entry : Util.toMap(info).entrySet()) {
if (entry.getKey().startsWith("schema.")) {
operandMap.put(entry.getKey().substring("schema.".length()), entry.getValue());
}
}
return "inline:" + json.toJsonString(root);
}
return null;
}
};
}
Aggregations