base.py 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. # Licensed to the Apache Software Foundation (ASF) under one
  2. # or more contributor license agreements. See the NOTICE file
  3. # distributed with this work for additional information
  4. # regarding copyright ownership. The ASF licenses this file
  5. # to you under the Apache License, Version 2.0 (the
  6. # "License"); you may not use this file except in compliance
  7. # with the License. You may obtain a copy of the License at
  8. #
  9. # http://www.apache.org/licenses/LICENSE-2.0
  10. #
  11. # Unless required by applicable law or agreed to in writing,
  12. # software distributed under the License is distributed on an
  13. # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  14. # KIND, either express or implied. See the License for the
  15. # specific language governing permissions and limitations
  16. # under the License.
  17. # pylint: disable=too-few-public-methods
  18. from typing import Any, Dict, List, Optional
  19. class SQLValidationAnnotation:
  20. """Represents a single annotation (error/warning) in an SQL querytext"""
  21. def __init__(
  22. self,
  23. message: str,
  24. line_number: Optional[int],
  25. start_column: Optional[int],
  26. end_column: Optional[int],
  27. ):
  28. self.message = message
  29. self.line_number = line_number
  30. self.start_column = start_column
  31. self.end_column = end_column
  32. def to_dict(self) -> Dict:
  33. """Return a dictionary representation of this annotation"""
  34. return {
  35. "line_number": self.line_number,
  36. "start_column": self.start_column,
  37. "end_column": self.end_column,
  38. "message": self.message,
  39. }
  40. class BaseSQLValidator:
  41. """BaseSQLValidator defines the interface for checking that a given sql
  42. query is valid for a given database engine."""
  43. name = "BaseSQLValidator"
  44. @classmethod
  45. def validate(
  46. cls, sql: str, schema: str, database: Any
  47. ) -> List[SQLValidationAnnotation]:
  48. """Check that the given SQL querystring is valid for the given engine"""
  49. raise NotImplementedError