Search in sources :

Example 1 with HandlerImpl

use of org.apache.calcite.avatica.HandlerImpl 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 HandlerImpl

use of org.apache.calcite.avatica.HandlerImpl in project calcite by apache.

the class JdbcTest method testOnConnectionClose.

/**
 * Tests {@link org.apache.calcite.avatica.Handler#onConnectionClose}
 * and  {@link org.apache.calcite.avatica.Handler#onStatementClose}.
 */
@Test
public void testOnConnectionClose() throws Exception {
    final int[] closeCount = { 0 };
    final int[] statementCloseCount = { 0 };
    final HandlerImpl h = new HandlerImpl() {

        @Override
        public void onConnectionClose(AvaticaConnection connection) {
            ++closeCount[0];
            throw new RuntimeException();
        }

        @Override
        public void onStatementClose(AvaticaStatement statement) {
            ++statementCloseCount[0];
            throw new RuntimeException();
        }
    };
    try (final TryThreadLocal.Memo ignore = HandlerDriver.HANDLERS.push(h)) {
        final HandlerDriver driver = new HandlerDriver();
        CalciteConnection connection = (CalciteConnection) driver.connect("jdbc:calcite:", new Properties());
        SchemaPlus rootSchema = connection.getRootSchema();
        rootSchema.add("hr", new ReflectiveSchema(new HrSchema()));
        connection.setSchema("hr");
        final Statement statement = connection.createStatement();
        final ResultSet resultSet = statement.executeQuery("select * from \"emps\"");
        assertEquals(0, closeCount[0]);
        assertEquals(0, statementCloseCount[0]);
        resultSet.close();
        try {
            resultSet.next();
            fail("resultSet.next() should throw SQLException when closed");
        } catch (SQLException e) {
            assertThat(e.getMessage(), containsString("next() called on closed cursor"));
        }
        assertEquals(0, closeCount[0]);
        assertEquals(0, statementCloseCount[0]);
        // Close statement. It throws SQLException, but statement is still closed.
        try {
            statement.close();
            fail("expecting error");
        } catch (SQLException e) {
        // ok
        }
        assertEquals(0, closeCount[0]);
        assertEquals(1, statementCloseCount[0]);
        // Close connection. It throws SQLException, but connection is still closed.
        try {
            connection.close();
            fail("expecting error");
        } catch (SQLException e) {
        // ok
        }
        assertEquals(1, closeCount[0]);
        assertEquals(1, statementCloseCount[0]);
        // Close a closed connection. Handler is not called again.
        connection.close();
        assertEquals(1, closeCount[0]);
        assertEquals(1, statementCloseCount[0]);
    }
}
Also used : SQLException(java.sql.SQLException) PreparedStatement(java.sql.PreparedStatement) Statement(java.sql.Statement) AvaticaStatement(org.apache.calcite.avatica.AvaticaStatement) SchemaPlus(org.apache.calcite.schema.SchemaPlus) ReflectiveSchema(org.apache.calcite.adapter.java.ReflectiveSchema) TryThreadLocal(org.apache.calcite.util.TryThreadLocal) Properties(java.util.Properties) HandlerImpl(org.apache.calcite.avatica.HandlerImpl) AvaticaStatement(org.apache.calcite.avatica.AvaticaStatement) AvaticaConnection(org.apache.calcite.avatica.AvaticaConnection) ResultSet(java.sql.ResultSet) CalciteConnection(org.apache.calcite.jdbc.CalciteConnection) Test(org.junit.Test)

Aggregations

SQLException (java.sql.SQLException)2 Properties (java.util.Properties)2 AvaticaConnection (org.apache.calcite.avatica.AvaticaConnection)2 HandlerImpl (org.apache.calcite.avatica.HandlerImpl)2 IOException (java.io.IOException)1 PreparedStatement (java.sql.PreparedStatement)1 ResultSet (java.sql.ResultSet)1 Statement (java.sql.Statement)1 Map (java.util.Map)1 ReflectiveSchema (org.apache.calcite.adapter.java.ReflectiveSchema)1 AvaticaStatement (org.apache.calcite.avatica.AvaticaStatement)1 CalciteConnection (org.apache.calcite.jdbc.CalciteConnection)1 JsonSchema (org.apache.calcite.model.JsonSchema)1 ModelHandler (org.apache.calcite.model.ModelHandler)1 SchemaFactory (org.apache.calcite.schema.SchemaFactory)1 SchemaPlus (org.apache.calcite.schema.SchemaPlus)1 JsonBuilder (org.apache.calcite.util.JsonBuilder)1 TryThreadLocal (org.apache.calcite.util.TryThreadLocal)1 Test (org.junit.Test)1