ViewDdlBuilder { public static String getODataViewDdl(UnitOfWork uow, String viewName, Table table, List<String> columnNames) throws KException { List<String> includedColumnNames = new ArrayList<String>(); if( columnNames!=null && !columnNames.isEmpty() ) { includedColumnNames.addAll(columnNames); } boolean includeAllColumns = (includedColumnNames.isEmpty()) ? true : false; StringBuilder sb = new StringBuilder(); TeiidVersion teiidVersion = TeiidVersionProvider.getInstance().getTeiidVersion(); String constraintStr = getPkConstraint(uow, teiidVersion, table); if(StringUtils.isEmpty(constraintStr)) { constraintStr = getUcConstraint(uow, teiidVersion, table); } if(constraintStr.length() > 0 && !includeAllColumns) { Set<String> constrColNames = getConstraintColumnNames(uow, table); for(String constrColName : constrColNames) { if(!includedColumnNames.contains(constrColName)) { includedColumnNames.add(constrColName); } } } List<String> colNames = new ArrayList<String>(); List<String> colTypes = new ArrayList<String>(); Column[] columns = table.getColumns(uow); for (int i = 0; i < columns.length; i++) { if(includeAllColumns || includedColumnNames.contains(columns[i].getName(uow))) { colNames.add(columns[i].getName(uow)); colTypes.add(columns[i].getDatatypeName(uow)); } } sb.append("CREATE VIEW "); sb.append(viewName); sb.append(StringConstants.SPACE+StringConstants.OPEN_BRACKET); if(constraintStr.length()>0) { sb.append(getColWithTypeString(teiidVersion, colNames, colTypes)); sb.append(StringConstants.COMMA+StringConstants.SPACE); sb.append(constraintStr); sb.append(") AS \n"); sb.append("SELECT "); sb.append(getColString(teiidVersion, colNames)); } else { sb.append("RowId integer PRIMARY KEY,"); sb.append(getColWithTypeString(teiidVersion, colNames, colTypes)); sb.append(") AS \nSELECT ROW_NUMBER() OVER (ORDER BY "); sb.append(escapeSQLName(teiidVersion, colNames.get(0))); sb.append(StringConstants.CLOSE_BRACKET + StringConstants.COMMA); sb.append(getColString(teiidVersion, colNames)); } sb.append(" \n"); sb.append("FROM "); String vdbName = table.getParent(uow).getParent(uow).getName(uow); sb.append(escapeSQLName(teiidVersion, vdbName) + StringConstants.DOT + escapeSQLName(teiidVersion, table.getName(uow))); sb.append(StringConstants.SEMI_COLON); return sb.toString(); }  static String getODataViewDdl(UnitOfWork uow,
                                         String viewName,
                                         Table table,
                                         List<String> columnNames); static String getODataViewJoinDdl(UnitOfWork uow, String viewName, 
                                             Table lhTable, String lhTableAlias, List<String> lhColNames, 
                                             Table rhTable, String rhTableAlias, List<String> rhColNames, 
                                             String joinType, List<ViewBuilderCriteriaPredicate> criteriaPredicates); static final String JOIN_INNER; static final String JOIN_LEFT_OUTER; static final String JOIN_RIGHT_OUTER; static final String JOIN_FULL_OUTER; }
@Test public void shouldGeneratedODataViewDDLFromTableWithNoPK() throws Exception { String EXPECTED_DDL = "CREATE VIEW MyView (RowId integer PRIMARY KEY, Col1 string, Col2 string) AS \n" + "SELECT ROW_NUMBER() OVER (ORDER BY Col1), Col1, Col2 \n" + "FROM MyVDB.MyTable;"; Table aTable = createTable("MyVDB", VDB_PATH, "MyModel", "MyTable"); Column col1 = aTable.addColumn(getTransaction(), "Col1"); col1.setDatatypeName(getTransaction(), "string"); Column col2 = aTable.addColumn(getTransaction(), "Col2"); col2.setDatatypeName(getTransaction(), "string"); List<String> colNames = new ArrayList<String>(); colNames.add("Col1"); colNames.add("Col2"); String viewDdl = ViewDdlBuilder.getODataViewDdl(getTransaction(), "MyView", aTable, colNames); assertThat(viewDdl, is(EXPECTED_DDL)); }
@Test public void shouldGeneratedODataViewDDLFromTableWithPK() throws Exception { String EXPECTED_DDL = "CREATE VIEW MyView ( Col1 string, Col2 string, CONSTRAINT pk PRIMARY KEY (Col1)) AS \n" + "SELECT Col1, Col2 \n" + "FROM MyVDB.MyTable;"; Table aTable = createTable("MyVDB", VDB_PATH, "MyModel", "MyTable"); Column col1 = aTable.addColumn(getTransaction(), "Col1"); col1.setDatatypeName(getTransaction(), "string"); Column col2 = aTable.addColumn(getTransaction(), "Col2"); col2.setDatatypeName(getTransaction(), "string"); PrimaryKey pk = aTable.setPrimaryKey(getTransaction(), "pk"); pk.addColumn(getTransaction(), col1); commit(); List<String> colNames = new ArrayList<String>(); colNames.add("Col1"); colNames.add("Col2"); String viewDdl = ViewDdlBuilder.getODataViewDdl(getTransaction(), "MyView", aTable, colNames); assertThat(viewDdl, is(EXPECTED_DDL)); }
@Test public void shouldGeneratedODataViewDDLFromTableWithUC() throws Exception { String EXPECTED_DDL = "CREATE VIEW MyView ( Col1 string, Col2 string, CONSTRAINT uc UNIQUE (Col1)) AS \n" + "SELECT Col1, Col2 \n" + "FROM MyVDB.MyTable;"; Table aTable = createTable("MyVDB", VDB_PATH, "MyModel", "MyTable"); Column col1 = aTable.addColumn(getTransaction(), "Col1"); col1.setDatatypeName(getTransaction(), "string"); Column col2 = aTable.addColumn(getTransaction(), "Col2"); col2.setDatatypeName(getTransaction(), "string"); UniqueConstraint uc = aTable.addUniqueConstraint(getTransaction(), "uc"); uc.addColumn(getTransaction(), col1); commit(); List<String> colNames = new ArrayList<String>(); colNames.add("Col1"); colNames.add("Col2"); String viewDdl = ViewDdlBuilder.getODataViewDdl(getTransaction(), "MyView", aTable, colNames); assertThat(viewDdl, is(EXPECTED_DDL)); }
@Test public void shouldGeneratedODataViewDDLFromTableWithMultipleUC() throws Exception { String EXPECTED_DDL = "CREATE VIEW MyView ( Col1 string, Col2 string, Col3 long, CONSTRAINT uc1 UNIQUE (Col1),CONSTRAINT uc2 UNIQUE (Col2, Col3)) AS \n" + "SELECT Col1, Col2, Col3 \n" + "FROM MyVDB.MyTable;"; Table aTable = createTable("MyVDB", VDB_PATH, "MyModel", "MyTable"); Column col1 = aTable.addColumn(getTransaction(), "Col1"); col1.setDatatypeName(getTransaction(), "string"); Column col2 = aTable.addColumn(getTransaction(), "Col2"); col2.setDatatypeName(getTransaction(), "string"); Column col3 = aTable.addColumn(getTransaction(), "Col3"); col3.setDatatypeName(getTransaction(), "long"); UniqueConstraint uc1 = aTable.addUniqueConstraint(getTransaction(), "uc1"); uc1.addColumn(getTransaction(), col1); UniqueConstraint uc2 = aTable.addUniqueConstraint(getTransaction(), "uc2"); uc2.addColumn(getTransaction(), col2); uc2.addColumn(getTransaction(), col3); commit(); List<String> colNames = new ArrayList<String>(); colNames.add("Col1"); colNames.add("Col2"); colNames.add("Col3"); String viewDdl = ViewDdlBuilder.getODataViewDdl(getTransaction(), "MyView", aTable, colNames); assertThat(viewDdl, is(EXPECTED_DDL)); }