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));
}
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());
}
}
}
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;
}
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);
}
}
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);
}
Aggregations