JdbcExplorer implements ISourceMetadataExplorer, ISampleDataDeployer { @Override public Pair<TableDesc, TableExtDesc> loadTableMetadata(String database, String table, String prj) throws SQLException { TableDesc tableDesc = new TableDesc(); tableDesc.setDatabase(database.toUpperCase(Locale.ROOT)); tableDesc.setName(table.toUpperCase(Locale.ROOT)); tableDesc.setUuid(UUID.randomUUID().toString()); tableDesc.setLastModified(0); tableDesc.setProject(prj); tableDesc.setSourceType(JdbcSource.SOURCE_ID); try (CachedRowSet tables = dataSource.getTable(database, table)) { String tableType = null; while (tables.next()) { tableType = tables.getString("TABLE_TYPE"); } if (tableType != null) { tableDesc.setTableType(tableType); } else { throw new RuntimeException(String.format(Locale.ROOT, "table %s not found in schema:%s", table, database)); } } try (CachedRowSet columns = dataSource.listColumns(database, table)) { List<ColumnDesc> columnDescs = new ArrayList<>(); while (columns.next()) { String cname = columns.getString("COLUMN_NAME"); int type = columns.getInt("DATA_TYPE"); int csize = columns.getInt("COLUMN_SIZE"); int digits = columns.getInt("DECIMAL_DIGITS"); int pos = columns.getInt("ORDINAL_POSITION"); String remarks = columns.getString("REMARKS"); ColumnDesc cdesc = new ColumnDesc(); cdesc.setName(cname.toUpperCase(Locale.ROOT)); String kylinType = dataSource.toKylinTypeName(type); if ("any".equals(kylinType)) { String typeName = columns.getString("TYPE_NAME"); int kylinTypeId = dataSource.toKylinTypeId(typeName, type); kylinType = dataSource.toKylinTypeName(kylinTypeId); } int precision = (SqlUtil.isPrecisionApplicable(kylinType) && csize > 0) ? csize : -1; precision = Math.min(precision, KylinConfig.getInstanceFromEnv().getDefaultVarcharPrecision()); int scale = (SqlUtil.isScaleApplicable(kylinType) && digits > 0) ? digits : -1; cdesc.setDatatype(new DataType(kylinType, precision, scale).toString()); cdesc.setId(String.valueOf(pos)); cdesc.setComment(remarks); columnDescs.add(cdesc); } tableDesc.setColumns(columnDescs.toArray(new ColumnDesc[columnDescs.size()])); TableExtDesc tableExtDesc = new TableExtDesc(); tableExtDesc.setIdentity(tableDesc.getIdentity()); tableExtDesc.setUuid(UUID.randomUUID().toString()); tableExtDesc.setLastModified(0); tableExtDesc.init(prj); return Pair.newPair(tableDesc, tableExtDesc); } } JdbcExplorer(JdbcConnector dataSource); @Override List<String> listDatabases(); @Override List<String> listTables(String schema); @Override Pair<TableDesc, TableExtDesc> loadTableMetadata(String database, String table, String prj); @Override void createSampleDatabase(String database); @Override void loadSampleData(String tableName, String tmpDataDir); @Override void createSampleTable(TableDesc table); @Override void createWrapperView(String origTableName, String viewName); @Override List<String> getRelatedKylinResources(TableDesc table); @Override ColumnDesc[] evalQueryMetadata(String query); @Override void validateSQL(String query);  }
@Test public void testLoadTableMetadata() throws Exception { Pair<TableDesc, TableExtDesc> pair = explorer.loadTableMetadata("DEFAULT", "TEST_KYLIN_FACT", "DEFAULT"); Assert.assertNotNull(pair.getFirst()); Assert.assertNotNull(pair.getSecond()); TableDesc tblDesc = pair.getFirst(); TableExtDesc tblExtDesc = pair.getSecond(); Assert.assertEquals("TEST_KYLIN_FACT", tblDesc.getName()); Assert.assertEquals("TABLE", tblDesc.getTableType()); Assert.assertEquals("DEFAULT.TEST_KYLIN_FACT", tblDesc.getIdentity()); Assert.assertEquals("DEFAULT", tblDesc.getDatabase()); Assert.assertEquals("DEFAULT", tblDesc.getProject()); Assert.assertEquals(tblDesc.getIdentity(), tblExtDesc.getIdentity()); Assert.assertEquals(tblDesc.getProject(), tblExtDesc.getProject()); ColumnDesc[] columnDescs = tblDesc.getColumns(); Assert.assertEquals(tblDesc.getColumnCount(), columnDescs.length); Assert.assertNotNull(columnDescs[0].getName()); Assert.assertNotNull(columnDescs[0].getDatatype()); Assert.assertNotNull(columnDescs[0].getType()); Assert.assertNotNull(columnDescs[0].getId()); }