readView method
Creates a View from a CreateViewStatement. The CREATE VIEW
statement
must be fully resolved through context
when calling this method.
Example:
// this will run analysis on the inner select statement and resolve columns
final ctx = engine.analyze('CREATE VIEW ...');
final createViewStmt = ctx.root as CreateViewStatement;
final view = const SchemaFromCreateTable().readView(ctx, createViewStmt);
Implementation
View readView(AnalysisContext context, CreateViewStatement stmt) {
final columnsFromSelect = stmt.query.resolvedColumns!;
final overriddenNames = stmt.columns ?? const [];
final viewColumns = <ViewColumn>[];
for (var i = 0; i < columnsFromSelect.length; i++) {
final column = columnsFromSelect[i];
// overriddenNames might be shorter than the columns. That's not a valid
// CREATE VIEW statement, but we try not to crash.
final name = i < overriddenNames.length ? overriddenNames[i] : null;
viewColumns.add(ViewColumn(column, context.typeOf(column).type, name));
}
return View(
name: stmt.viewName,
resolvedColumns: viewColumns,
definition: stmt,
);
}