use of io.crate.metadata.view.ViewsMetadata in project crate by crate.
the class DocSchemaInfo method viewNames.
private Collection<String> viewNames() {
ViewsMetadata viewMetadata = clusterService.state().metadata().custom(ViewsMetadata.TYPE);
if (viewMetadata == null) {
return Collections.emptySet();
}
Set<String> views = new HashSet<>();
extractRelationNamesForSchema(StreamSupport.stream(viewMetadata.names().spliterator(), false), schemaName, views);
return views;
}
use of io.crate.metadata.view.ViewsMetadata in project crate by crate.
the class Schemas method resolveRelation.
/**
* Resolves the provided ident relation (table or view) against the search path.
* @param ident
* @param searchPath
* @throws RelationUnknown in case a valid relation cannot be resolved in the search path.
* @return the corresponding RelationName
*/
public RelationName resolveRelation(QualifiedName ident, SearchPath searchPath) {
String identSchema = schemaName(ident);
String relation = relationName(ident);
ViewsMetadata views = clusterService.state().metadata().custom(ViewsMetadata.TYPE);
if (identSchema == null) {
for (String pathSchema : searchPath) {
RelationName tableOrViewRelation = getTableOrViewRelation(pathSchema, relation, views);
if (tableOrViewRelation != null) {
return tableOrViewRelation;
}
}
} else {
RelationName tableOrViewRelation = getTableOrViewRelation(identSchema, relation, views);
if (tableOrViewRelation != null) {
return tableOrViewRelation;
}
}
throw new RelationUnknown(ident.toString());
}
use of io.crate.metadata.view.ViewsMetadata in project crate by crate.
the class Schemas method resolveView.
/**
* @throws RelationUnknown if the view cannot be resolved against the search path.
*/
public Tuple<ViewMetadata, RelationName> resolveView(QualifiedName ident, SearchPath searchPath) {
ViewsMetadata views = clusterService.state().metadata().custom(ViewsMetadata.TYPE);
ViewMetadata view = null;
RelationName viewRelationName = null;
String identSchema = schemaName(ident);
String viewName = relationName(ident);
if (views != null) {
if (identSchema == null) {
for (String pathSchema : searchPath) {
SchemaInfo schemaInfo = schemas.get(pathSchema);
if (schemaInfo != null) {
viewRelationName = new RelationName(pathSchema, viewName);
view = views.getView(viewRelationName);
if (view != null) {
break;
}
}
}
} else {
viewRelationName = new RelationName(identSchema, viewName);
view = views.getView(viewRelationName);
}
}
if (view == null) {
throw new RelationUnknown(viewName);
}
return Tuple.tuple(view, viewRelationName);
}
use of io.crate.metadata.view.ViewsMetadata in project crate by crate.
the class ViewsITest method testViewCanBeCreatedSelectedAndThenDropped.
@Test
public void testViewCanBeCreatedSelectedAndThenDropped() throws Exception {
execute("create table t1 (x int)");
execute("insert into t1 (x) values (1)");
execute("refresh table t1");
execute("create view v1 as select * from t1 where x > ?", $(0));
for (ClusterService clusterService : internalCluster().getInstances(ClusterService.class)) {
ViewsMetadata views = clusterService.state().metadata().custom(ViewsMetadata.TYPE);
assertThat(views, Matchers.notNullValue());
assertThat(views.contains(RelationName.fromIndexName(sqlExecutor.getCurrentSchema() + ".v1")), is(true));
}
assertThat(printedTable(execute("select * from v1").rows()), is("1\n"));
assertThat(printedTable(execute("select view_definition from information_schema.views").rows()), is("SELECT *\nFROM \"t1\"\nWHERE \"x\" > 0\n\n"));
execute("drop view v1");
for (ClusterService clusterService : internalCluster().getInstances(ClusterService.class)) {
ViewsMetadata views = clusterService.state().metadata().custom(ViewsMetadata.TYPE);
assertThat(views.contains(RelationName.fromIndexName(sqlExecutor.getCurrentSchema() + ".v1")), is(false));
}
}
use of io.crate.metadata.view.ViewsMetadata in project crate by crate.
the class Schemas method getNewCurrentSchemas.
@VisibleForTesting
static Set<String> getNewCurrentSchemas(Metadata metadata) {
Set<String> schemas = new HashSet<>();
// 'doc' schema is always available and has the special property that its indices
// don't have to be prefixed with the schema name
schemas.add(DOC_SCHEMA_NAME);
for (String index : metadata.getConcreteAllIndices()) {
addIfSchema(schemas, index);
}
for (ObjectCursor<String> cursor : metadata.templates().keys()) {
addIfSchema(schemas, cursor.value);
}
UserDefinedFunctionsMetadata udfMetadata = metadata.custom(UserDefinedFunctionsMetadata.TYPE);
if (udfMetadata != null) {
udfMetadata.functionsMetadata().stream().map(UserDefinedFunctionMetadata::schema).forEach(schemas::add);
}
ViewsMetadata viewsMetadata = metadata.custom(ViewsMetadata.TYPE);
if (viewsMetadata != null) {
StreamSupport.stream(viewsMetadata.names().spliterator(), false).map(IndexParts::new).map(IndexParts::getSchema).forEach(schemas::add);
}
return schemas;
}
Aggregations