diff --git a/src/pytsql/grammar/__init__.py b/src/pytsql/grammar/__init__.py index 9d32e91..5c61225 100644 --- a/src/pytsql/grammar/__init__.py +++ b/src/pytsql/grammar/__init__.py @@ -1 +1 @@ -from .sa_tsql import parse, SA_ErrorListener, tsqlParser +from .sa_tsql import parse, SA_ErrorListener, tsqlParser, USE_CPP_IMPLEMENTATION diff --git a/src/pytsql/grammar/sa_tsql.py b/src/pytsql/grammar/sa_tsql.py index 1170445..3512771 100644 --- a/src/pytsql/grammar/sa_tsql.py +++ b/src/pytsql/grammar/sa_tsql.py @@ -1,6 +1,5 @@ # This file was auto-generated by speedy-antlr-tool v1.3.1 # https://github.com/amykyta3/speedy-antlr-tool - import sys import types from typing import Optional diff --git a/src/pytsql/tsql.py b/src/pytsql/tsql.py index 3f6d55c..4f5c716 100644 --- a/src/pytsql/tsql.py +++ b/src/pytsql/tsql.py @@ -1,5 +1,6 @@ import logging import re +import warnings from pathlib import Path from re import Match from typing import Any, Dict, List, Optional, Union @@ -9,7 +10,7 @@ from antlr4 import InputStream, Token from sqlalchemy.engine import Connection -from pytsql.grammar import SA_ErrorListener, parse, tsqlParser +from pytsql.grammar import USE_CPP_IMPLEMENTATION, SA_ErrorListener, parse, tsqlParser _REPLACE_START = "" _REPLACE_END = "" @@ -143,6 +144,13 @@ def syntaxError( def _split(code: str) -> List[str]: + if not USE_CPP_IMPLEMENTATION: + warnings.warn( + "Can not find C++ version of the parser, Python version will be used instead." + " Something is likely wrong with your installation of the package," + " and is preventing the use of the faster C++ parser." + ) + logging.info("Started SQL script parsing") # The default error listener only prints to the console without raising exceptions. diff --git a/tests/unit/test_py_vs_cpp.py b/tests/unit/test_py_vs_cpp.py index b61ef99..374216d 100644 --- a/tests/unit/test_py_vs_cpp.py +++ b/tests/unit/test_py_vs_cpp.py @@ -8,6 +8,7 @@ from antlr4.Token import CommonToken from antlr4.tree.Tree import TerminalNodeImpl +from pytsql import tsql from pytsql.grammar import sa_tsql from pytsql.tsql import _split @@ -135,3 +136,9 @@ def test_compare_py_and_cpp__split(py_parse_mock, cpp_parse_mock, seed): cpp_parse_mock.assert_called_once() assert py_result == cpp_result + + +def test__py_parse__warns(): + tsql.USE_CPP_IMPLEMENTATION = False + with pytest.warns(match=r"Can not find C\+\+ version of the parser"): + _split("")