return ReflectiveSchema.create(schema, name, target);
public OptiqConnection createConnection() throws Exception { Class.forName("net.hydromatic.optiq.jdbc.Driver"); Connection connection = DriverManager.getConnection("jdbc:optiq:"); OptiqConnection optiqConnection = connection.unwrap(OptiqConnection.class); MutableSchema rootSchema = optiqConnection.getRootSchema(); ReflectiveSchema.create(rootSchema, name, schema); optiqConnection.setSchema(name); return optiqConnection; } });
static OptiqConnection getConnection(String... schema) throws ClassNotFoundException, SQLException { Class.forName("net.hydromatic.optiq.jdbc.Driver"); Connection connection = DriverManager.getConnection("jdbc:optiq:"); OptiqConnection optiqConnection = connection.unwrap(OptiqConnection.class); MutableSchema rootSchema = optiqConnection.getRootSchema(); final List<String> schemaList = Arrays.asList(schema); if (schemaList.contains("hr")) { ReflectiveSchema.create(rootSchema, "hr", new HrSchema()); } if (schemaList.contains("foodmart")) { ReflectiveSchema.create( rootSchema, "foodmart", new FoodmartSchema()); } if (schemaList.contains("lingual")) { ReflectiveSchema.create( rootSchema, "SALES", new LingualSchema()); } if (schemaList.contains("metadata")) { // always present } return optiqConnection; }
connection.unwrap(OptiqConnection.class); MutableSchema rootSchema = optiqConnection.getRootSchema(); ReflectiveSchema.create(rootSchema, "hr", new HrSchema()); ReflectiveSchema.create(rootSchema, "foodmart", new FoodmartSchema()); Statement statement = connection.createStatement(); ResultSet resultSet =
/** Test case for bug where if two tables have different element classes * but those classes have identical fields, Optiq would generate code to use * the wrong element class; a {@link ClassCastException} would ensue. */ @Test public void testDifferentTypesSameFields() throws Exception { Class.forName("net.hydromatic.optiq.jdbc.Driver"); Connection connection = DriverManager.getConnection("jdbc:optiq:"); OptiqConnection optiqConnection = connection.unwrap(OptiqConnection.class); final MutableSchema rootSchema = optiqConnection.getRootSchema(); ReflectiveSchema.create(rootSchema, "TEST", new MySchema()); Statement statement = optiqConnection.createStatement(); ResultSet resultSet = statement.executeQuery("SELECT \"myvalue\" from TEST.\"mytable2\""); assertEquals("myvalue=2\n", toString(resultSet)); resultSet.close(); statement.close(); connection.close(); }
/** * The example in the README. */ @Test public void testReadme() throws ClassNotFoundException, SQLException { Class.forName("net.hydromatic.optiq.jdbc.Driver"); Connection connection = DriverManager.getConnection("jdbc:optiq:"); OptiqConnection optiqConnection = connection.unwrap(OptiqConnection.class); ReflectiveSchema.create( optiqConnection.getRootSchema(), "hr", new HrSchema()); Statement statement = optiqConnection.createStatement(); ResultSet resultSet = statement.executeQuery( "select d.\"deptno\", min(e.\"empid\")\n" + "from \"hr\".\"emps\" as e\n" + "join \"hr\".\"depts\" as d\n" + " on e.\"deptno\" = d.\"deptno\"\n" + "group by d.\"deptno\"\n" + "having count(*) > 1"); toString(resultSet); resultSet.close(); statement.close(); connection.close(); }
public Schema create( MutableSchema parentSchema, String name, Map<String, Object> operand) { final ReflectiveSchema schema = ReflectiveSchema.create(parentSchema, name, new HrSchema()); // Mine the EMPS table and add it under another name e.g. ELVIS final Table table = schema.getTable("emps", Object.class); String tableName = (String) operand.get("tableName"); schema.addTable( new TableInSchemaImpl( schema, tableName, Schema.TableType.TABLE, table)); final Boolean mutable = (Boolean) operand.get("mutable"); if (mutable == null || mutable) { return schema; } else { // Wrap the schema in DelegatingSchema so that it does not // implement MutableSchema. return new DelegatingSchema(schema); } } }
/** * Tests a view. */ @Test public void testView() throws SQLException, ClassNotFoundException { Class.forName("net.hydromatic.optiq.jdbc.Driver"); Connection connection = DriverManager.getConnection("jdbc:optiq:"); OptiqConnection optiqConnection = connection.unwrap(OptiqConnection.class); MutableSchema rootSchema = optiqConnection.getRootSchema(); MapSchema schema = MapSchema.create(rootSchema, "s"); schema.addTableFunction( ViewTable.viewFunction( schema, "emps_view", "select * from \"hr\".\"emps\" where \"deptno\" = 10", null)); ReflectiveSchema.create(rootSchema, "hr", new JdbcTest.HrSchema()); ResultSet resultSet = connection.createStatement().executeQuery( "select *\n" + "from \"s\".\"emps_view\"\n" + "where \"empid\" < 120"); assertEquals( "empid=100; deptno=10; name=Bill; salary=10000.0; commission=1000\n" + "empid=110; deptno=10; name=Theodore; salary=11500.0; commission=250\n", JdbcTest.toString(resultSet)); }
/** * Tests a relation that is accessed via method syntax. * The function returns a {@link net.hydromatic.linq4j.Queryable}. */ @Ignore @Test public void testOperator() throws SQLException, ClassNotFoundException { Class.forName("net.hydromatic.optiq.jdbc.Driver"); Connection connection = DriverManager.getConnection("jdbc:optiq:"); OptiqConnection optiqConnection = connection.unwrap(OptiqConnection.class); JavaTypeFactory typeFactory = optiqConnection.getTypeFactory(); MutableSchema rootSchema = optiqConnection.getRootSchema(); MapSchema schema = MapSchema.create(rootSchema, "s"); schema.addTableFunction( new TableFunctionInSchemaImpl(schema, "GenerateStrings", Schemas.methodMember(JdbcTest.GENERATE_STRINGS_METHOD, typeFactory))); schema.addTableFunction( new TableFunctionInSchemaImpl(schema, "StringUnion", Schemas.methodMember(JdbcTest.STRING_UNION_METHOD, typeFactory))); ReflectiveSchema.create(rootSchema, "hr", new JdbcTest.HrSchema()); ResultSet resultSet = connection.createStatement().executeQuery( "select *\n" + "from table(s.StringUnion(\n" + " GenerateStrings(5),\n" + " cursor (select name from emps)))\n" + "where char_length(s) > 3"); assertTrue(resultSet.next()); }
"select * from \"emps\"", null)); ReflectiveSchema.create(rootSchema, "hr", new JdbcTest.HrSchema()); final Statement statement = connection.createStatement(); ResultSet resultSet;
driver.connect("jdbc:optiq:", new Properties()); MutableSchema rootSchema = connection.getRootSchema(); ReflectiveSchema.create( rootSchema, "hr", new HrSchema()); connection.setSchema("hr");
/** Tests {@link java.sql.Statement#closeOnCompletion()}. */ @Test public void testStatementCloseOnCompletion() throws Exception { final Driver driver = new Driver(); OptiqConnection connection = (OptiqConnection) driver.connect("jdbc:optiq:", new Properties()); MutableSchema rootSchema = connection.getRootSchema(); ReflectiveSchema.create( rootSchema, "hr", new HrSchema()); connection.setSchema("hr"); final Statement statement = connection.createStatement(); assertFalse((Boolean) OptiqAssert.call(statement, "isCloseOnCompletion")); OptiqAssert.call(statement, "closeOnCompletion"); assertTrue((Boolean) OptiqAssert.call(statement, "isCloseOnCompletion")); final ResultSet resultSet = statement.executeQuery("select * from \"emps\""); assertFalse(resultSet.isClosed()); assertFalse(statement.isClosed()); assertFalse(connection.isClosed()); // when result set is closed, statement is closed automatically resultSet.close(); assertTrue(resultSet.isClosed()); assertTrue(statement.isClosed()); assertFalse(connection.isClosed()); connection.close(); assertTrue(resultSet.isClosed()); assertTrue(statement.isClosed()); assertTrue(connection.isClosed()); }