Report analysis error for view column mismatches

This commit is contained in:
Simon Binder 2020-05-16 19:36:56 +02:00
parent 58335958c0
commit 888e131d50
No known key found for this signature in database
GPG Key ID: 7891917E4147B8C0
3 changed files with 40 additions and 0 deletions

View File

@ -62,6 +62,7 @@ enum AnalysisErrorType {
compoundColumnCountMismatch,
cteColumnCountMismatch,
valuesSelectCountMismatch,
viewColumnNamesMismatch,
rowValueMisuse,
other,
}

View File

@ -8,6 +8,28 @@ class LintingVisitor extends RecursiveVisitor<void, void> {
LintingVisitor(this.options, this.context);
@override
void visitCreateViewStatement(CreateViewStatement e, void arg) {
final resolvedColumns = e.query.resolvedColumns;
if (e.columns == null || resolvedColumns == null) {
return super.visitCreateViewStatement(e, arg);
}
final amountOfNames = e.columns.length;
final amountOfColumns = resolvedColumns.length;
if (amountOfNames != amountOfColumns) {
context.reportError(AnalysisError(
type: AnalysisErrorType.viewColumnNamesMismatch,
relevantNode: e,
message: 'This view declares $amountOfNames column names, but the '
'inner select statement returns $amountOfColumns',
));
}
visitChildren(e, arg);
}
@override
void visitInvocation(SqlInvocation e, void arg) {
final lowercaseCall = e.name.toLowerCase();

View File

@ -0,0 +1,17 @@
import 'package:sqlparser/sqlparser.dart';
import 'package:test/test.dart';
import '../data.dart';
void main() {
test('reports column name mismatches in CREATE VIEW statements', () {
final engine = SqlEngine()..registerTable(demoTable);
final result = engine.analyze('CREATE VIEW my_view (foo) AS '
'SELECT * FROM demo;');
expect(result.errors, hasLength(1));
final error = result.errors.single;
expect(error.type, AnalysisErrorType.viewColumnNamesMismatch);
});
}