Search in sources :

Example 1 with SchemaFactory

use of org.apache.calcite.schema.SchemaFactory 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;
        }
    };
}
Also used : SchemaFactory(org.apache.calcite.schema.SchemaFactory) SQLException(java.sql.SQLException) JsonSchema(org.apache.calcite.model.JsonSchema) ModelHandler(org.apache.calcite.model.ModelHandler) IOException(java.io.IOException) Properties(java.util.Properties) HandlerImpl(org.apache.calcite.avatica.HandlerImpl) JsonBuilder(org.apache.calcite.util.JsonBuilder) AvaticaConnection(org.apache.calcite.avatica.AvaticaConnection) Map(java.util.Map)

Example 2 with SchemaFactory

use of org.apache.calcite.schema.SchemaFactory in project calcite by apache.

the class SqlCreateForeignSchema method execute.

public void execute(CalcitePrepare.Context context) {
    final Pair<CalciteSchema, String> pair = SqlDdlNodes.schema(context, true, name);
    final SchemaPlus subSchema0 = pair.left.plus().getSubSchema(pair.right);
    if (subSchema0 != null) {
        if (!getReplace() && !ifNotExists) {
            throw SqlUtil.newContextException(name.getParserPosition(), RESOURCE.schemaExists(pair.right));
        }
    }
    final Schema subSchema;
    final String libraryName;
    if (type != null) {
        Preconditions.checkArgument(library == null);
        final String typeName = (String) value(this.type);
        final JsonSchema.Type type = Util.enumVal(JsonSchema.Type.class, typeName.toUpperCase(Locale.ROOT));
        if (type != null) {
            switch(type) {
                case JDBC:
                    libraryName = JdbcSchema.Factory.class.getName();
                    break;
                default:
                    libraryName = null;
            }
        } else {
            libraryName = null;
        }
        if (libraryName == null) {
            throw SqlUtil.newContextException(this.type.getParserPosition(), RESOURCE.schemaInvalidType(typeName, Arrays.toString(JsonSchema.Type.values())));
        }
    } else {
        Preconditions.checkArgument(library != null);
        libraryName = (String) value(library);
    }
    final SchemaFactory schemaFactory = AvaticaUtils.instantiatePlugin(SchemaFactory.class, libraryName);
    final Map<String, Object> operandMap = new LinkedHashMap<>();
    for (Pair<SqlIdentifier, SqlNode> option : options(optionList)) {
        operandMap.put(option.left.getSimple(), value(option.right));
    }
    subSchema = schemaFactory.create(pair.left.plus(), pair.right, operandMap);
    pair.left.add(pair.right, subSchema);
}
Also used : SchemaFactory(org.apache.calcite.schema.SchemaFactory) JdbcSchema(org.apache.calcite.adapter.jdbc.JdbcSchema) Schema(org.apache.calcite.schema.Schema) CalciteSchema(org.apache.calcite.jdbc.CalciteSchema) JsonSchema(org.apache.calcite.model.JsonSchema) JsonSchema(org.apache.calcite.model.JsonSchema) SchemaPlus(org.apache.calcite.schema.SchemaPlus) SchemaFactory(org.apache.calcite.schema.SchemaFactory) NlsString(org.apache.calcite.util.NlsString) SqlIdentifier(org.apache.calcite.sql.SqlIdentifier) LinkedHashMap(java.util.LinkedHashMap) CalciteSchema(org.apache.calcite.jdbc.CalciteSchema) SqlNode(org.apache.calcite.sql.SqlNode)

Example 3 with SchemaFactory

use of org.apache.calcite.schema.SchemaFactory in project calcite by apache.

the class ModelHandler method visit.

public void visit(JsonCustomSchema jsonSchema) {
    try {
        final SchemaPlus parentSchema = currentMutableSchema("sub-schema");
        checkRequiredAttributes(jsonSchema, "name", "factory");
        final SchemaFactory schemaFactory = AvaticaUtils.instantiatePlugin(SchemaFactory.class, jsonSchema.factory);
        final Schema schema = schemaFactory.create(parentSchema, jsonSchema.name, operandMap(jsonSchema, jsonSchema.operand));
        final SchemaPlus schemaPlus = parentSchema.add(jsonSchema.name, schema);
        populateSchema(jsonSchema, schemaPlus);
    } catch (Exception e) {
        throw new RuntimeException("Error instantiating " + jsonSchema, e);
    }
}
Also used : SchemaFactory(org.apache.calcite.schema.SchemaFactory) JdbcSchema(org.apache.calcite.adapter.jdbc.JdbcSchema) Schema(org.apache.calcite.schema.Schema) AbstractSchema(org.apache.calcite.schema.impl.AbstractSchema) CalciteSchema(org.apache.calcite.jdbc.CalciteSchema) SchemaPlus(org.apache.calcite.schema.SchemaPlus) SQLException(java.sql.SQLException) IOException(java.io.IOException)

Aggregations

SchemaFactory (org.apache.calcite.schema.SchemaFactory)3 IOException (java.io.IOException)2 SQLException (java.sql.SQLException)2 JdbcSchema (org.apache.calcite.adapter.jdbc.JdbcSchema)2 CalciteSchema (org.apache.calcite.jdbc.CalciteSchema)2 JsonSchema (org.apache.calcite.model.JsonSchema)2 Schema (org.apache.calcite.schema.Schema)2 SchemaPlus (org.apache.calcite.schema.SchemaPlus)2 LinkedHashMap (java.util.LinkedHashMap)1 Map (java.util.Map)1 Properties (java.util.Properties)1 AvaticaConnection (org.apache.calcite.avatica.AvaticaConnection)1 HandlerImpl (org.apache.calcite.avatica.HandlerImpl)1 ModelHandler (org.apache.calcite.model.ModelHandler)1 AbstractSchema (org.apache.calcite.schema.impl.AbstractSchema)1 SqlIdentifier (org.apache.calcite.sql.SqlIdentifier)1 SqlNode (org.apache.calcite.sql.SqlNode)1 JsonBuilder (org.apache.calcite.util.JsonBuilder)1 NlsString (org.apache.calcite.util.NlsString)1