Search in sources :

Example 1 with MetadataRepository

use of org.teiid.metadata.MetadataRepository in project teiid by teiid.

the class TestEmbeddedServer method testImportExcept.

@Ignore("limit to/exclude not yet implemented")
@Test
public void testImportExcept() throws Exception {
    es.start(new EmbeddedConfiguration());
    es.addMetadataRepository("x", new MetadataRepository() {

        @Override
        public void loadMetadata(MetadataFactory factory, ExecutionFactory executionFactory, Object connectionFactory, String text) throws TranslatorException {
            assertEquals("helloworld1,other", factory.getModelProperties().get("importer.excludeTables"));
            Table t = factory.addTable("helloworld");
            t.setVirtual(true);
            factory.addColumn("col", "string", t);
            t.setSelectTransformation("select 'HELLO WORLD'");
        }
    });
    String externalDDL = "CREATE DATABASE test VERSION '1';" + "USE DATABASE test VERSION '1';" + "CREATE VIRTUAL SCHEMA test2;" + "IMPORT FOREIGN SCHEMA public except (helloworld1, other) FROM REPOSITORY x INTO test2;";
    es.deployVDB(new ByteArrayInputStream(externalDDL.getBytes(Charset.forName("UTF-8"))), true);
    ResultSet rs = es.getDriver().connect("jdbc:teiid:test", null).createStatement().executeQuery("select * from helloworld");
    rs.next();
    assertEquals("HELLO WORLD", rs.getString(1));
}
Also used : MetadataRepository(org.teiid.metadata.MetadataRepository) Table(org.teiid.metadata.Table) MetadataFactory(org.teiid.metadata.MetadataFactory) ByteArrayInputStream(java.io.ByteArrayInputStream) ExecutionFactory(org.teiid.translator.ExecutionFactory) TranslatorException(org.teiid.translator.TranslatorException) Ignore(org.junit.Ignore) Test(org.junit.Test)

Example 2 with MetadataRepository

use of org.teiid.metadata.MetadataRepository in project teiid by teiid.

the class AbstractVDBDeployer method loadMetadata.

protected void loadMetadata(VDBMetaData vdb, ConnectorManagerRepository cmr, MetadataStore store, VDBResources vdbResources) throws TranslatorException {
    // add the system types
    store.addDataTypes(SystemMetadata.getInstance().getRuntimeTypeMap());
    // add domains if defined
    String value = vdb.getPropertyValue(VDBMetaData.TEIID_DOMAINS);
    if (value != null) {
        // use a temporary store/db to retrieve the domains
        DatabaseStore dbStore = new DatabaseStore() {

            @Override
            public Map<String, Datatype> getRuntimeTypes() {
                return getVDBRepository().getRuntimeTypeMap();
            }
        };
        dbStore.startEditing(true);
        // $NON-NLS-1$ //$NON-NLS-2$
        dbStore.databaseCreated(new Database("x", "1"));
        // $NON-NLS-1$ //$NON-NLS-2$
        dbStore.databaseSwitched("x", "1");
        dbStore.setMode(Mode.DOMAIN);
        QueryParser.getQueryParser().parseDDL(dbStore, new StringReader(value));
        dbStore.stopEditing();
        // $NON-NLS-1$ //$NON-NLS-2$
        store.addDataTypes(dbStore.getDatabase("x", "1").getMetadataStore().getDatatypes());
    }
    // load metadata from the models
    AtomicInteger loadCount = new AtomicInteger();
    for (ModelMetaData model : vdb.getModelMetaDatas().values()) {
        if (model.getModelType() == Model.Type.PHYSICAL || model.getModelType() == Model.Type.VIRTUAL) {
            loadCount.incrementAndGet();
        }
    }
    if (loadCount.get() == 0) {
        processVDBDDL(vdb, store, cmr, vdbResources);
        getVDBRepository().finishDeployment(vdb.getName(), vdb.getVersion());
        return;
    }
    for (ModelMetaData model : vdb.getModelMetaDatas().values()) {
        MetadataRepository metadataRepository = model.getAttachment(MetadataRepository.class);
        if (model.getModelType() == Model.Type.PHYSICAL || model.getModelType() == Model.Type.VIRTUAL) {
            loadMetadata(vdb, model, cmr, metadataRepository, store, loadCount, vdbResources);
            // $NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
            LogManager.logTrace(LogConstants.CTX_RUNTIME, "Model ", model.getName(), "in VDB ", vdb.getName(), " was being loaded from its repository");
        } else {
            // $NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
            LogManager.logTrace(LogConstants.CTX_RUNTIME, "Model ", model.getName(), "in VDB ", vdb.getName(), " skipped being loaded because of its type ", model.getModelType());
        }
    }
}
Also used : MetadataRepository(org.teiid.metadata.MetadataRepository) DeploymentBasedDatabaseStore(org.teiid.metadatastore.DeploymentBasedDatabaseStore) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Database(org.teiid.metadata.Database) StringReader(java.io.StringReader) Datatype(org.teiid.metadata.Datatype) ModelMetaData(org.teiid.adminapi.impl.ModelMetaData)

Example 3 with MetadataRepository

use of org.teiid.metadata.MetadataRepository in project teiid by teiid.

the class VDBService method getMetadataRepository.

/**
 * Override for module based loading
 */
@SuppressWarnings("rawtypes")
@Override
protected MetadataRepository<?, ?> getMetadataRepository(String repoType) throws VirtualDatabaseException {
    MetadataRepository<?, ?> repo = super.getMetadataRepository(repoType);
    if (repo != null) {
        return repo;
    }
    try {
        repo = TeiidAdd.buildService(MetadataRepository.class, repoType);
    } catch (OperationFailedException e) {
        throw new VirtualDatabaseException(IntegrationPlugin.Event.TEIID50057, e, IntegrationPlugin.Util.gs(IntegrationPlugin.Event.TEIID50057, repoType));
    }
    MetadataRepository old = this.repositories.putIfAbsent(repoType, repo);
    return old != null ? old : repo;
}
Also used : MetadataRepository(org.teiid.metadata.MetadataRepository) IndexMetadataRepository(org.teiid.metadata.index.IndexMetadataRepository) OperationFailedException(org.jboss.as.controller.OperationFailedException) VirtualDatabaseException(org.teiid.deployers.VirtualDatabaseException)

Example 4 with MetadataRepository

use of org.teiid.metadata.MetadataRepository in project teiid by teiid.

the class AbstractVDBDeployer method processVDBDDL.

private void processVDBDDL(final VDBMetaData vdb, final MetadataStore vdbMetadataStore, final ConnectorManagerRepository cmr, final VDBResources vdbResources) {
    if (vdb.getStatus() == Status.FAILED) {
        return;
    }
    String ddl = vdb.getPropertyValue(VDBMetaData.TEIID_DDL);
    if (ddl != null) {
        final Database database = DatabaseUtil.convert(vdb, vdbMetadataStore);
        CompositeMetadataStore compositeStore = new CompositeMetadataStore(vdbMetadataStore);
        final TransformationMetadata metadata = new TransformationMetadata(vdb, compositeStore, null, getVDBRepository().getSystemFunctionManager().getSystemFunctions(), null);
        DeploymentBasedDatabaseStore deploymentStore = new DeploymentBasedDatabaseStore(getVDBRepository()) {

            @Override
            protected TransformationMetadata getTransformationMetadata() {
                return metadata;
            }

            @Override
            public void importSchema(String schemaName, String serverType, String serverName, String foreignSchemaName, List<String> includeTables, List<String> excludeTables, Map<String, String> properties) {
                ModelMetaData model = vdb.getModel(schemaName);
                MetadataFactory factory = DatabaseStore.createMF(this, getSchema(schemaName), true);
                factory.getModelProperties().putAll(model.getPropertiesMap());
                factory.getModelProperties().putAll(properties);
                if (!includeTables.isEmpty()) {
                    // $NON-NLS-1$
                    factory.getModelProperties().put("importer.includeTables", StringUtil.join(includeTables, ","));
                }
                if (!excludeTables.isEmpty()) {
                    // $NON-NLS-1$
                    factory.getModelProperties().put("importer.excludeTables", StringUtil.join(excludeTables, ","));
                }
                factory.setParser(new QueryParser());
                if (vdbResources != null) {
                    factory.setVdbResources(vdbResources.getEntriesPlusVisibilities());
                }
                MetadataRepository baseRepo = model.getAttachment(MetadataRepository.class);
                MetadataRepository metadataRepository;
                try {
                    metadataRepository = getMetadataRepository(serverType);
                    if (metadataRepository == null) {
                        throw new VirtualDatabaseException(RuntimePlugin.Util.gs(RuntimePlugin.Event.TEIID40094, model.getName(), vdb.getName(), vdb.getVersion(), serverType));
                    }
                } catch (VirtualDatabaseException e1) {
                    throw new MetadataException(e1);
                }
                metadataRepository = new ChainingMetadataRepository(Arrays.asList(new MetadataRepositoryWrapper(metadataRepository, null), baseRepo));
                ExecutionFactory ef = null;
                Object cf = null;
                Exception te = null;
                for (ConnectorManager cm : getConnectorManagers(model, cmr)) {
                    if (te != null) {
                        // $NON-NLS-1$
                        LogManager.logDetail(LogConstants.CTX_RUNTIME, te, "Failed to get metadata, trying next source.");
                        te = null;
                    }
                    try {
                        if (cm != null) {
                            ef = cm.getExecutionFactory();
                            cf = cm.getConnectionFactory();
                        }
                    } catch (TranslatorException e) {
                        // $NON-NLS-1$
                        LogManager.logDetail(LogConstants.CTX_RUNTIME, e, "Failed to get a connection factory for metadata load.");
                    }
                    if (LogManager.isMessageToBeRecorded(LogConstants.CTX_RUNTIME, MessageLevel.TRACE)) {
                        // $NON-NLS-1$ //$NON-NLS-2$
                        LogManager.logTrace(LogConstants.CTX_RUNTIME, "CREATE SCHEMA", factory.getSchema().getName(), ";\n", DDLStringVisitor.getDDLString(factory.getSchema(), null, null));
                    }
                    try {
                        metadataRepository.loadMetadata(factory, ef, cf);
                        break;
                    } catch (Exception e) {
                        te = e;
                        factory = DatabaseStore.createMF(this, getSchema(schemaName), true);
                        factory.getModelProperties().putAll(model.getPropertiesMap());
                        factory.getModelProperties().putAll(properties);
                        factory.setParser(new QueryParser());
                        if (vdbResources != null) {
                            factory.setVdbResources(vdbResources.getEntriesPlusVisibilities());
                        }
                    }
                }
                if (te != null) {
                    if (te instanceof RuntimeException) {
                        throw (RuntimeException) te;
                    }
                    throw new MetadataException(te);
                }
            }
        };
        deploymentStore.startEditing(false);
        deploymentStore.databaseCreated(database);
        deploymentStore.databaseSwitched(database.getName(), database.getVersion());
        deploymentStore.setMode(Mode.SCHEMA);
        try {
            QueryParser.getQueryParser().parseDDL(deploymentStore, new StringReader(ddl));
        } finally {
            deploymentStore.stopEditing();
        }
        DatabaseUtil.copyDatabaseGrantsAndRoles(database, vdb);
    }
}
Also used : ExecutionFactory(org.teiid.translator.ExecutionFactory) ConnectorManager(org.teiid.dqp.internal.datamgr.ConnectorManager) DeploymentBasedDatabaseStore(org.teiid.metadatastore.DeploymentBasedDatabaseStore) MetadataException(org.teiid.metadata.MetadataException) MetadataException(org.teiid.metadata.MetadataException) TranslatorException(org.teiid.translator.TranslatorException) VirtualDatabaseException(org.teiid.deployers.VirtualDatabaseException) ModelMetaData(org.teiid.adminapi.impl.ModelMetaData) MetadataRepository(org.teiid.metadata.MetadataRepository) QueryParser(org.teiid.query.parser.QueryParser) MetadataFactory(org.teiid.metadata.MetadataFactory) Database(org.teiid.metadata.Database) StringReader(java.io.StringReader) ArrayList(java.util.ArrayList) List(java.util.List) TranslatorException(org.teiid.translator.TranslatorException) VirtualDatabaseException(org.teiid.deployers.VirtualDatabaseException) Map(java.util.Map) ConcurrentSkipListMap(java.util.concurrent.ConcurrentSkipListMap)

Example 5 with MetadataRepository

use of org.teiid.metadata.MetadataRepository in project teiid by teiid.

the class AbstractVDBDeployer method getMetadataRepository.

private MetadataRepository<?, ?> getMetadataRepository(VDBMetaData vdb, ModelMetaData model, MetadataRepository<?, ?> defaultRepo) throws VirtualDatabaseException {
    if (model.getSourceMetadataType().isEmpty()) {
        if (defaultRepo != null) {
            return defaultRepo;
        }
        if (model.isSource()) {
            return new ChainingMetadataRepository(Arrays.asList(new NativeMetadataRepository(), new DirectQueryMetadataRepository()));
        }
        throw new VirtualDatabaseException(RuntimePlugin.Util.gs(RuntimePlugin.Event.TEIID40094, model.getName(), vdb.getName(), vdb.getVersion(), null));
    }
    List<MetadataRepository<?, ?>> repos = new ArrayList<MetadataRepository<?, ?>>(2);
    for (int i = 0; i < model.getSourceMetadataType().size(); i++) {
        String schemaTypes = model.getSourceMetadataType().get(i);
        // $NON-NLS-1$
        StringTokenizer st = new StringTokenizer(schemaTypes, ",");
        while (st.hasMoreTokens()) {
            String repoType = st.nextToken().trim();
            MetadataRepository<?, ?> current = getMetadataRepository(repoType);
            if (current == null) {
                throw new VirtualDatabaseException(RuntimePlugin.Util.gs(RuntimePlugin.Event.TEIID40094, model.getName(), vdb.getName(), vdb.getVersion(), repoType));
            }
            if (model.getSourceMetadataText().size() > i) {
                current = new MetadataRepositoryWrapper(current, model.getSourceMetadataText().get(i));
            }
            repos.add(current);
        }
    }
    if (model.getModelType() == ModelMetaData.Type.PHYSICAL) {
        repos.add(new DirectQueryMetadataRepository());
    }
    if (model.getModelType() == ModelMetaData.Type.VIRTUAL) {
        repos.add(new MaterializationMetadataRepository());
    }
    if (repos.size() == 1) {
        return repos.get(0);
    }
    return new ChainingMetadataRepository(repos);
}
Also used : ArrayList(java.util.ArrayList) MetadataRepository(org.teiid.metadata.MetadataRepository) StringTokenizer(java.util.StringTokenizer) VirtualDatabaseException(org.teiid.deployers.VirtualDatabaseException)

Aggregations

MetadataRepository (org.teiid.metadata.MetadataRepository)6 VirtualDatabaseException (org.teiid.deployers.VirtualDatabaseException)4 ArrayList (java.util.ArrayList)3 ModelMetaData (org.teiid.adminapi.impl.ModelMetaData)3 StringReader (java.io.StringReader)2 Database (org.teiid.metadata.Database)2 MetadataFactory (org.teiid.metadata.MetadataFactory)2 DeploymentBasedDatabaseStore (org.teiid.metadatastore.DeploymentBasedDatabaseStore)2 ExecutionFactory (org.teiid.translator.ExecutionFactory)2 TranslatorException (org.teiid.translator.TranslatorException)2 ByteArrayInputStream (java.io.ByteArrayInputStream)1 List (java.util.List)1 Map (java.util.Map)1 StringTokenizer (java.util.StringTokenizer)1 ConcurrentSkipListMap (java.util.concurrent.ConcurrentSkipListMap)1 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)1 OperationFailedException (org.jboss.as.controller.OperationFailedException)1 Ignore (org.junit.Ignore)1 Test (org.junit.Test)1 ConnectorManager (org.teiid.dqp.internal.datamgr.ConnectorManager)1