Search in sources :

Example 26 with LogicalTable

use of org.pentaho.metadata.model.LogicalTable in project pentaho-kettle by pentaho.

the class StarModelDialog method refreshTablesList.

protected void refreshTablesList() {
    wTablesList.clearAll();
    for (LogicalTable logicalTable : logicalModel.getLogicalTables()) {
        TableType tableType = (TableType) logicalTable.getProperty(DefaultPropertyID.TABLE_TYPE.getId());
        if (tableType == TableType.DIMENSION) {
            TableItem item = new TableItem(wTablesList.table, SWT.NONE);
            item.setText(1, Const.NVL(ConceptUtil.getName(logicalTable, locale), ""));
            item.setText(2, Const.NVL(ConceptUtil.getDescription(logicalTable, locale), ""));
            String typeDescription = tableType == null ? "" : tableType.name();
            if (tableType == TableType.DIMENSION) {
                DimensionType dimType = ConceptUtil.getDimensionType(logicalTable);
                if (dimType != DimensionType.OTHER) {
                    typeDescription += " - " + dimType.name();
                }
            }
            item.setText(3, typeDescription);
        }
    }
    wTablesList.removeEmptyRows();
    wTablesList.setRowNums();
    wTablesList.optWidth(true);
    String[] dimensionNames = getDimensionTableNames();
    factColumns[5].setComboValues(dimensionNames);
}
Also used : TableType(org.pentaho.metadata.model.concept.types.TableType) TableItem(org.eclipse.swt.widgets.TableItem) LogicalTable(org.pentaho.metadata.model.LogicalTable) LocalizedString(org.pentaho.metadata.model.concept.types.LocalizedString)

Example 27 with LogicalTable

use of org.pentaho.metadata.model.LogicalTable in project pentaho-kettle by pentaho.

the class JobGenerator method generateSqlJob.

public JobMeta generateSqlJob() throws KettleException {
    DatabaseMeta databaseMeta = findTargetDatabaseMeta();
    Database db = new Database(Spoon.loggingObject, databaseMeta);
    try {
        db.connect();
        JobMeta jobMeta = new JobMeta();
        jobMeta.setName("Create tables for '" + ConceptUtil.getName(domain, locale) + "'");
        jobMeta.setDescription(ConceptUtil.getDescription(domain, locale));
        // Let's not forget to add the database connection
        // 
        jobMeta.addDatabase(databaseMeta);
        Point location = new Point(GRAPH_LEFT, GRAPH_TOP);
        // Create a job entry
        // 
        JobEntryCopy startEntry = JobMeta.createStartEntry();
        startEntry.setLocation(location.x, location.y);
        startEntry.setDrawn();
        jobMeta.addJobEntry(startEntry);
        JobEntryCopy lastEntry = startEntry;
        nextLocation(location);
        // Create one SQL entry for all the physically unique dimensions and facts
        // We need to get a list of all known dimensions with physical table name.
        // 
        List<LogicalTable> tables = getUniqueLogicalTables();
        for (LogicalTable logicalTable : tables) {
            String phTable = ConceptUtil.getString(logicalTable, DefaultIDs.LOGICAL_TABLE_PHYSICAL_TABLE_NAME);
            String tableName = ConceptUtil.getName(logicalTable, locale);
            String tableDescription = ConceptUtil.getDescription(logicalTable, locale);
            TableType tableType = ConceptUtil.getTableType(logicalTable);
            DimensionType dimensionType = ConceptUtil.getDimensionType(logicalTable);
            boolean isFact = tableType == TableType.FACT;
            boolean isDimension = tableType == TableType.DIMENSION;
            boolean isJunk = isDimension && dimensionType == DimensionType.JUNK_DIMENSION;
            JobEntrySQL sqlEntry = new JobEntrySQL(phTable);
            sqlEntry.setDatabase(databaseMeta);
            // Get the SQL for this table...
            // 
            String schemaTable = databaseMeta.getQuotedSchemaTableCombination(null, phTable);
            String phKeyField = null;
            // The technical key is the first KEY field...
            // 
            LogicalColumn keyColumn = null;
            if (isDimension) {
                keyColumn = ConceptUtil.findLogicalColumn(logicalTable, AttributeType.TECHNICAL_KEY);
            }
            if (keyColumn != null) {
                phKeyField = ConceptUtil.getString(keyColumn, DefaultIDs.LOGICAL_COLUMN_PHYSICAL_COLUMN_NAME);
            }
            // Get all the fields for the logical table...
            // 
            RowMetaInterface fields = getRowForLogicalTable(databaseMeta, logicalTable);
            // Generate the required SQL to make this happen
            // 
            String sql = db.getCreateTableStatement(schemaTable, fields, phKeyField, databaseMeta.supportsAutoinc() && !isFact, null, true);
            // 
            if (keyColumn != null) {
                ValueMetaInterface keyValueMeta = getValueForLogicalColumn(databaseMeta, keyColumn);
                String indexName = databaseMeta.quoteField("IDX_" + phTable.replace(" ", "_").toUpperCase() + "_" + phKeyField.toUpperCase());
                String indexSql = db.getCreateIndexStatement(schemaTable, indexName, new String[] { keyValueMeta.getName() }, true, false, true, true);
                sql += Const.CR + indexSql;
            }
            // 
            if (isFact) {
                List<LogicalColumn> fks = ConceptUtil.findLogicalColumns(logicalTable, AttributeType.TECHNICAL_KEY);
                for (LogicalColumn fk : fks) {
                    ValueMetaInterface keyValueMeta = getValueForLogicalColumn(databaseMeta, fk);
                    String phColumn = ConceptUtil.getString(fk, DefaultIDs.LOGICAL_COLUMN_PHYSICAL_COLUMN_NAME);
                    if (!Utils.isEmpty(phColumn)) {
                        String indexName = databaseMeta.quoteField("IDX_" + phTable.replace(" ", "_").toUpperCase() + "_" + phColumn.toUpperCase());
                        String indexSql = db.getCreateIndexStatement(schemaTable, indexName, new String[] { keyValueMeta.getName() }, true, false, true, true);
                        sql += Const.CR + indexSql;
                    }
                }
            }
            // 
            if (isDimension) {
                List<LogicalColumn> naturalKeys = ConceptUtil.findLogicalColumns(logicalTable, AttributeType.NATURAL_KEY);
                if (!naturalKeys.isEmpty()) {
                    String indexName = databaseMeta.quoteField("IDX_" + phTable.replace(" ", "_").toUpperCase() + "_LOOKUP");
                    String[] fieldNames = new String[naturalKeys.size()];
                    for (int i = 0; i < fieldNames.length; i++) {
                        ValueMetaInterface keyValueMeta = getValueForLogicalColumn(databaseMeta, naturalKeys.get(i));
                        fieldNames[i] = keyValueMeta.getName();
                    }
                    String indexSql = db.getCreateIndexStatement(schemaTable, indexName, fieldNames, false, false, false, true);
                    sql += Const.CR + indexSql;
                }
            }
            if (isJunk) {
                List<LogicalColumn> attributes = ConceptUtil.findLogicalColumns(logicalTable, AttributeType.ATTRIBUTE);
                if (!attributes.isEmpty()) {
                    String indexName = databaseMeta.quoteField("IDX_" + phTable.replace(" ", "_").toUpperCase() + "_LOOKUP");
                    String[] fieldNames = new String[attributes.size()];
                    for (int i = 0; i < fieldNames.length; i++) {
                        ValueMetaInterface attrValueMeta = getValueForLogicalColumn(databaseMeta, attributes.get(i));
                        fieldNames[i] = attrValueMeta.getName();
                    }
                    String indexSql = db.getCreateIndexStatement(schemaTable, indexName, fieldNames, false, false, false, true);
                    sql += Const.CR + indexSql;
                }
            }
            // If it's
            sqlEntry.setSQL(sql);
            sqlEntry.setDescription("Generated based on logical table '" + tableName + "'" + Const.CR + Const.CR + Const.NVL(tableDescription, ""));
            JobEntryCopy sqlCopy = new JobEntryCopy(sqlEntry);
            sqlCopy.setLocation(location.x, location.y);
            sqlCopy.setDrawn();
            nextLocation(location);
            jobMeta.addJobEntry(sqlCopy);
            // Hook up with the previous job entry too...
            // 
            JobHopMeta jobHop = new JobHopMeta(lastEntry, sqlCopy);
            jobHop.setEnabled();
            jobHop.setConditional();
            jobHop.setEvaluation(true);
            if (lastEntry.isStart()) {
                jobHop.setUnconditional();
            }
            jobMeta.addJobHop(jobHop);
            lastEntry = sqlCopy;
        }
        return jobMeta;
    } catch (Exception e) {
        throw new KettleException("There was an error during the generation of the SQL job", e);
    } finally {
        if (db != null) {
            db.disconnect();
        }
    }
}
Also used : DimensionType(org.pentaho.di.starmodeler.DimensionType) KettleException(org.pentaho.di.core.exception.KettleException) JobMeta(org.pentaho.di.job.JobMeta) LogicalColumn(org.pentaho.metadata.model.LogicalColumn) JobHopMeta(org.pentaho.di.job.JobHopMeta) TableType(org.pentaho.metadata.model.concept.types.TableType) RowMetaInterface(org.pentaho.di.core.row.RowMetaInterface) Point(org.pentaho.di.core.gui.Point) LogicalTable(org.pentaho.metadata.model.LogicalTable) JobEntrySQL(org.pentaho.di.job.entries.sql.JobEntrySQL) DatabaseMeta(org.pentaho.di.core.database.DatabaseMeta) Point(org.pentaho.di.core.gui.Point) KettleException(org.pentaho.di.core.exception.KettleException) ValueMetaInterface(org.pentaho.di.core.row.ValueMetaInterface) JobEntryCopy(org.pentaho.di.job.entry.JobEntryCopy) Database(org.pentaho.di.core.database.Database)

Example 28 with LogicalTable

use of org.pentaho.metadata.model.LogicalTable in project pentaho-kettle by pentaho.

the class ModelMetaStoreUtil method populateElement.

private static IMetaStoreElement populateElement(IMetaStore metaStore, LogicalModel model) throws MetaStoreException {
    try {
        IMetaStoreElement element = metaStore.newElement();
        element.setName(model.getName(defaultLocale));
        element.addChild(metaStore.newAttribute(Attribute.ID_MODEL_DESCRIPTION.id, model.getDescription(defaultLocale)));
        IMetaStoreAttribute logicalTablesAttribute = metaStore.newAttribute(Attribute.ID_LOGICAL_TABLES.id, model.getDescription(defaultLocale));
        element.addChild(logicalTablesAttribute);
        for (LogicalTable logicalTable : model.getLogicalTables()) {
            IMetaStoreAttribute logicalTableAttribute = metaStore.newAttribute(Attribute.ID_LOGICAL_TABLE.id, model.getDescription(defaultLocale));
            logicalTablesAttribute.addChild(logicalTableAttribute);
            // 
            // Save the ID as well as the name (for safety)
            // 
            logicalTableAttribute.addChild(metaStore.newAttribute(Attribute.ID_LOGICAL_TABLE_ID.id, logicalTable.getId()));
            logicalTableAttribute.addChild(metaStore.newAttribute(Attribute.ID_LOGICAL_TABLE_NAME.id, logicalTable.getName()));
        }
        return element;
    } catch (Exception e) {
        throw new MetaStoreException("Unable to populate metastore element from logical model", e);
    }
}
Also used : MetaStoreException(org.pentaho.metastore.api.exceptions.MetaStoreException) IMetaStoreAttribute(org.pentaho.metastore.api.IMetaStoreAttribute) IMetaStoreElement(org.pentaho.metastore.api.IMetaStoreElement) LogicalTable(org.pentaho.metadata.model.LogicalTable) MetaStoreException(org.pentaho.metastore.api.exceptions.MetaStoreException)

Example 29 with LogicalTable

use of org.pentaho.metadata.model.LogicalTable in project pentaho-kettle by pentaho.

the class JobGeneratorTest method testGenerateDimensionTransformation.

@Test
public void testGenerateDimensionTransformation() throws Exception {
    final LogicalTable logicalTable = mock(LogicalTable.class);
    final DatabaseMeta databaseMeta = mock(DatabaseMeta.class);
    final TransMeta transMeta = jobGenerator.generateDimensionTransformation(databaseMeta, logicalTable);
    assertNotNull(transMeta);
    assertTrue(transMeta.getDatabases().contains(databaseMeta));
    assertEquals(2, transMeta.getSteps().size());
    assertEquals(1, transMeta.nrTransHops());
}
Also used : TransMeta(org.pentaho.di.trans.TransMeta) LogicalTable(org.pentaho.metadata.model.LogicalTable) DatabaseMeta(org.pentaho.di.core.database.DatabaseMeta) Test(org.junit.Test)

Example 30 with LogicalTable

use of org.pentaho.metadata.model.LogicalTable in project pentaho-kettle by pentaho.

the class JobGeneratorTest method testGenerateDimensionLookupStepFromLogicalTable.

@Test
public void testGenerateDimensionLookupStepFromLogicalTable() throws Exception {
    final LogicalTable logicalTable = mock(LogicalTable.class);
    final DatabaseMeta databaseMeta = mock(DatabaseMeta.class);
    final StepMeta stepMeta = jobGenerator.generateDimensionLookupStepFromLogicalTable(databaseMeta, logicalTable);
    assertNotNull(stepMeta);
    assertEquals(DimensionLookupMeta.class, stepMeta.getStepMetaInterface().getClass());
    assertEquals(databaseMeta, ((DimensionLookupMeta) stepMeta.getStepMetaInterface()).getDatabaseMeta());
}
Also used : LogicalTable(org.pentaho.metadata.model.LogicalTable) DatabaseMeta(org.pentaho.di.core.database.DatabaseMeta) StepMeta(org.pentaho.di.trans.step.StepMeta) Test(org.junit.Test)

Aggregations

LogicalTable (org.pentaho.metadata.model.LogicalTable)34 Test (org.junit.Test)14 LocalizedString (org.pentaho.metadata.model.concept.types.LocalizedString)13 LogicalColumn (org.pentaho.metadata.model.LogicalColumn)12 LogicalModel (org.pentaho.metadata.model.LogicalModel)11 ArrayList (java.util.ArrayList)7 DatabaseMeta (org.pentaho.di.core.database.DatabaseMeta)6 Domain (org.pentaho.metadata.model.Domain)5 SqlPhysicalModel (org.pentaho.metadata.model.SqlPhysicalModel)5 SqlPhysicalTable (org.pentaho.metadata.model.SqlPhysicalTable)5 TableType (org.pentaho.metadata.model.concept.types.TableType)5 SqlDataSource (org.pentaho.metadata.model.SqlDataSource)4 SqlPhysicalColumn (org.pentaho.metadata.model.SqlPhysicalColumn)4 KettleException (org.pentaho.di.core.exception.KettleException)3 TransMeta (org.pentaho.di.trans.TransMeta)3 Category (org.pentaho.metadata.model.Category)3 LogicalRelationship (org.pentaho.metadata.model.LogicalRelationship)3 List (java.util.List)2 TableItem (org.eclipse.swt.widgets.TableItem)2 Matchers.anyString (org.mockito.Matchers.anyString)2