TempTable addTempTable( final String tempTableName, Create create, BufferManager buffer, boolean add, CommandContext context) throws TeiidProcessingException { List<ElementSymbol> columns = create.getColumnSymbols(); TempMetadataID id = tempMetadataStore.getTempGroupID(tempTableName); getSynchronization(context); if (id == null) { // add metadata id = tempMetadataStore.addTempGroup(tempTableName, columns, false, true); TempTableResolver.addAdditionalMetadata(create, id); } columns = new ArrayList<ElementSymbol>(create.getColumnSymbols()); if (!create.getPrimaryKey().isEmpty()) { // reorder the columns to put the key in front List<ElementSymbol> primaryKey = create.getPrimaryKey(); columns.removeAll(primaryKey); columns.addAll(0, primaryKey); } final TempTable tempTable = new TempTable(id, buffer, columns, create.getPrimaryKey().size(), sessionID); if (add) { tempTables.put(tempTableName, tempTable); } return tempTable; }
@Override public void visit(Create obj) { preVisitVisitor(obj); visitNode(obj.getTable()); visitNodes(obj.getColumnSymbols()); visitNodes(obj.getPrimaryKey()); postVisitVisitor(obj); }
@Test public void testCreateTempTableWithPrimaryKey() { Create create = getFactory().newCreate(); create.setTable(getFactory().newGroupSymbol("tempTable")); // $NON-NLS-1$ List<ElementSymbol> columns = new ArrayList<ElementSymbol>(); ElementSymbol column = getFactory().newElementSymbol("c1"); // $NON-NLS-1$ column.setType(DataTypeManagerService.DefaultDataTypes.BOOLEAN.getTypeClass()); columns.add(column); column = getFactory().newElementSymbol("c2"); // $NON-NLS-1$ column.setType(DataTypeManagerService.DefaultDataTypes.BYTE.getTypeClass()); columns.add(column); create.setElementSymbolsAsColumns(columns); create.getPrimaryKey().add(column); helpTest( "Create local TEMPORARY table tempTable(c1 boolean, c2 byte, primary key (c2))", "CREATE LOCAL TEMPORARY TABLE tempTable (c1 boolean, c2 byte, PRIMARY KEY(c2))", create); //$NON-NLS-1$ //$NON-NLS-2$ }
@Override public void alterCreate(Create create) { if (!all) { create.getPrimaryKey().addAll(create.getColumnSymbols()); } }