return new ReflectiveSchema(target);
/** * Creates a ReflectiveSchema within another schema. * * @param parentSchema Parent schema * @param name Name of new schema * @param target Object whose fields become the tables of the schema * @return New ReflectiveSchema */ public static ReflectiveSchema create( MutableSchema parentSchema, String name, Object target) { ReflectiveSchema schema = new ReflectiveSchema( parentSchema, name, target, parentSchema.getSubSchemaExpression( name, ReflectiveSchema.class)); parentSchema.addSchema(name, schema); return schema; }
public OptiqConnection createConnection() throws Exception { Class.forName("net.hydromatic.optiq.jdbc.Driver"); Connection connection = DriverManager.getConnection("jdbc:optiq:"); OptiqConnection optiqConnection = connection.unwrap(OptiqConnection.class); SchemaPlus rootSchema = optiqConnection.getRootSchema(); rootSchema.add(name, new ReflectiveSchema(schema)); optiqConnection.setSchema(name); return optiqConnection; } });
connection.unwrap(OptiqConnection.class); SchemaPlus rootSchema = optiqConnection.getRootSchema(); rootSchema.add("hr", new ReflectiveSchema(new Hr())); rootSchema.add("foodmart", new ReflectiveSchema(new Foodmart())); 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 SchemaPlus rootSchema = optiqConnection.getRootSchema(); rootSchema.add("TEST", new ReflectiveSchema(new MySchema())); Statement statement = optiqConnection.createStatement(); ResultSet resultSet = statement.executeQuery("SELECT \"myvalue\" from TEST.\"mytable2\""); assertEquals("myvalue=2\n", OptiqAssert.toString(resultSet)); resultSet.close(); statement.close(); connection.close(); }
private Connection setup() throws SQLException { // Create a jdbc database & table final String db = TempDb.INSTANCE.getUrl(); Connection c1 = DriverManager.getConnection(db, "", ""); Statement stmt1 = c1.createStatement(); // This is a table we can join with the emps from the hr schema stmt1.execute("create table table1(id integer not null primary key, " + "field1 varchar(10))"); stmt1.execute("insert into table1 values(100, 'foo')"); stmt1.execute("insert into table1 values(200, 'bar')"); c1.close(); // Make an optiq schema with both a jdbc schema and a non-jdbc schema Connection optiqConn = DriverManager.getConnection("jdbc:optiq:"); OptiqConnection optiqConnection = optiqConn.unwrap(OptiqConnection.class); SchemaPlus rootSchema = optiqConnection.getRootSchema(); rootSchema.add("DB", JdbcSchema.create(rootSchema, "DB", JdbcSchema.dataSource(db, "org.hsqldb.jdbcDriver", "", ""), null, null)); rootSchema.add("hr", new ReflectiveSchema(new JdbcTest.HrSchema())); return optiqConn; }
@Before public void setUp() throws SQLException { Connection connection = DriverManager.getConnection("jdbc:optiq:"); OptiqConnection optiqConnection = connection.unwrap(OptiqConnection.class); SchemaPlus rootSchema = optiqConnection.getRootSchema(); rootSchema.add("test", new ReflectiveSchema(new TestSchema())); optiqConnection.setSchema("test"); this.conn = optiqConnection; }
/** * 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); final SchemaPlus rootSchema = optiqConnection.getRootSchema(); rootSchema.add("hr", new ReflectiveSchema(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"); final String s = OptiqAssert.toString(resultSet); assertThat(s, notNullValue()); resultSet.close(); statement.close(); connection.close(); }
private String adviseSql(String sql) throws ClassNotFoundException, SQLException { Class.forName("net.hydromatic.optiq.jdbc.Driver"); Properties info = new Properties(); info.put("lex", "JAVA"); info.put("quoting", "DOUBLE_QUOTE"); Connection connection = DriverManager.getConnection("jdbc:optiq:", info); OptiqConnection optiqConnection = connection.unwrap(OptiqConnection.class); SchemaPlus rootSchema = optiqConnection.getRootSchema(); rootSchema.add("hr", new ReflectiveSchema(new HrSchema())); SchemaPlus schema = rootSchema.add("s", new AbstractSchema()); optiqConnection.setSchema("hr"); final TableFunction table = new SqlAdvisorGetHintsFunction(); schema.add("get_hints", table); PreparedStatement ps = connection.prepareStatement( "select *\n" + "from table(\"s\".\"get_hints\"(?, ?)) as t(id, names, type)"); SqlParserUtil.StringAndPos sap = SqlParserUtil.findPos(sql); ps.setString(1, sap.sql); ps.setInt(2, sap.cursor); return OptiqAssert.toString(ps.executeQuery()); }
/** * 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); SchemaPlus rootSchema = optiqConnection.getRootSchema(); SchemaPlus schema = rootSchema.add("s", new AbstractSchema()); schema.add("emps_view", ViewTable.viewMacro(schema, "select * from \"hr\".\"emps\" where \"deptno\" = 10", null)); rootSchema.add("hr", new ReflectiveSchema(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", OptiqAssert.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); SchemaPlus rootSchema = optiqConnection.getRootSchema(); SchemaPlus schema = rootSchema.add("s", new AbstractSchema()); schema.add("GenerateStrings", TableMacroImpl.create(JdbcTest.GENERATE_STRINGS_METHOD)); schema.add("StringUnion", TableMacroImpl.create(JdbcTest.STRING_UNION_METHOD)); rootSchema.add("hr", new ReflectiveSchema(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()); }
schema.add("null_emps", ViewTable.viewMacro(schema, "select * from \"emps\"", null)); rootSchema.add("hr", new ReflectiveSchema(new JdbcTest.HrSchema())); final Statement statement = connection.createStatement(); ResultSet resultSet;
driver.connect("jdbc:optiq:", new Properties()); SchemaPlus rootSchema = connection.getRootSchema(); rootSchema.add("hr", new ReflectiveSchema(new HrSchema())); connection.setSchema("hr"); final Statement statement = connection.createStatement();
case REFLECTIVE_FOODMART: return rootSchema.add("foodmart", new ReflectiveSchema(new JdbcTest.FoodmartSchema())); case JDBC_FOODMART: final DataSource dataSource = case HR: return rootSchema.add("hr", new ReflectiveSchema(new JdbcTest.HrSchema())); case LINGUAL: return rootSchema.add("SALES", new ReflectiveSchema(new JdbcTest.LingualSchema())); case POST: final SchemaPlus post = rootSchema.add("POST", new AbstractSchema());
driver.connect("jdbc:optiq:", new Properties()); SchemaPlus rootSchema = connection.getRootSchema(); rootSchema.add("hr", new ReflectiveSchema(new HrSchema())); connection.setSchema("hr"); final Statement statement = connection.createStatement();
public String checkTpchQuery(String tpchTestQuery) throws Exception { final SchemaPlus schema = Frameworks.createRootSchema(true).add("tpch", new ReflectiveSchema(new TpchSchema())); final FrameworkConfig config = Frameworks.newConfigBuilder() .lex(Lex.MYSQL) .defaultSchema(schema) .programs(Programs.ofRules(Programs.RULE_SET)) .build(); Planner p = Frameworks.getPlanner(config); SqlNode n = p.parse(tpchTestQuery); n = p.validate(n); RelNode r = p.convert(n); String plan = RelOptUtil.toString(r); p.close(); return plan; }