mirror of https://github.com/AMT-Cheif/drift.git
Report analysis error for view column mismatches
This commit is contained in:
parent
58335958c0
commit
888e131d50
|
@ -62,6 +62,7 @@ enum AnalysisErrorType {
|
|||
compoundColumnCountMismatch,
|
||||
cteColumnCountMismatch,
|
||||
valuesSelectCountMismatch,
|
||||
viewColumnNamesMismatch,
|
||||
rowValueMisuse,
|
||||
other,
|
||||
}
|
||||
|
|
|
@ -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();
|
||||
|
|
|
@ -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);
|
||||
});
|
||||
}
|
Loading…
Reference in New Issue