Search in sources :

Example 1 with ViewMetadata

use of io.crate.metadata.view.ViewMetadata 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);
}
Also used : RelationUnknown(io.crate.exceptions.RelationUnknown) ViewsMetadata(io.crate.metadata.view.ViewsMetadata) ViewMetadata(io.crate.metadata.view.ViewMetadata) BlobSchemaInfo(io.crate.metadata.blob.BlobSchemaInfo) PgCatalogSchemaInfo(io.crate.metadata.pgcatalog.PgCatalogSchemaInfo) SysSchemaInfo(io.crate.metadata.sys.SysSchemaInfo) SchemaInfo(io.crate.metadata.table.SchemaInfo) InformationSchemaInfo(io.crate.metadata.information.InformationSchemaInfo)

Example 2 with ViewMetadata

use of io.crate.metadata.view.ViewMetadata in project crate by crate.

the class RelationAnalyzer method visitTable.

@Override
protected AnalyzedRelation visitTable(Table<?> node, StatementAnalysisContext context) {
    QualifiedName tableQualifiedName = node.getName();
    SearchPath searchPath = context.sessionContext().searchPath();
    AnalyzedRelation relation;
    TableInfo tableInfo;
    try {
        tableInfo = schemas.resolveTableInfo(tableQualifiedName, context.currentOperation(), context.sessionContext().sessionUser(), searchPath);
        if (tableInfo instanceof DocTableInfo) {
            // Dispatching of doc relations is based on the returned class of the schema information.
            relation = new DocTableRelation((DocTableInfo) tableInfo);
        } else {
            relation = new TableRelation(tableInfo);
        }
    } catch (RelationUnknown e) {
        Tuple<ViewMetadata, RelationName> viewMetadata;
        try {
            viewMetadata = schemas.resolveView(tableQualifiedName, searchPath);
        } catch (RelationUnknown e1) {
            // don't shadow original exception, as looking for the view is just a fallback
            throw e;
        }
        ViewMetadata view = viewMetadata.v1();
        AnalyzedRelation resolvedView = SqlParser.createStatement(view.stmt()).accept(this, context);
        relation = new AnalyzedView(viewMetadata.v2(), view.owner(), resolvedView);
    }
    context.currentRelationContext().addSourceRelation(relation);
    return relation;
}
Also used : DocTableInfo(io.crate.metadata.doc.DocTableInfo) RelationUnknown(io.crate.exceptions.RelationUnknown) QualifiedName(io.crate.sql.tree.QualifiedName) SearchPath(io.crate.metadata.SearchPath) DocTableInfo(io.crate.metadata.doc.DocTableInfo) TableInfo(io.crate.metadata.table.TableInfo) Tuple(io.crate.common.collections.Tuple) ViewMetadata(io.crate.metadata.view.ViewMetadata)

Aggregations

RelationUnknown (io.crate.exceptions.RelationUnknown)2 ViewMetadata (io.crate.metadata.view.ViewMetadata)2 Tuple (io.crate.common.collections.Tuple)1 SearchPath (io.crate.metadata.SearchPath)1 BlobSchemaInfo (io.crate.metadata.blob.BlobSchemaInfo)1 DocTableInfo (io.crate.metadata.doc.DocTableInfo)1 InformationSchemaInfo (io.crate.metadata.information.InformationSchemaInfo)1 PgCatalogSchemaInfo (io.crate.metadata.pgcatalog.PgCatalogSchemaInfo)1 SysSchemaInfo (io.crate.metadata.sys.SysSchemaInfo)1 SchemaInfo (io.crate.metadata.table.SchemaInfo)1 TableInfo (io.crate.metadata.table.TableInfo)1 ViewsMetadata (io.crate.metadata.view.ViewsMetadata)1 QualifiedName (io.crate.sql.tree.QualifiedName)1