-1

I am trying to lint some SQL code using regex and have managed to patch together some code. However the grep returns a new line character when I run it in a bash file but returns nothing when I run it from the command line. I suspect there is something wrong in my bash file.

LINT='pcregrep -Mi'
PATTERN="(^\s*SELECT(.*)(\n|\r\n|\r))((^(\s*--(.*))|^(\s*(.*),))(\n|\r\n|\r))*(^(\s*--(.*))(\n|\r\n|\r))*(^\s*FROM(.*))"
files=$(git diff --name-only --diff-filter=ACM | grep "\.sql$")
if [ "$files" = "" ]; then
    exit 0
fi

pass=true

echo -e "\nLinting SQL:\n"

for file in ${files}; do
    result=$($LINT $PATTERN ${file} | grep "No syntax errors detected in ${file}")
    if [ "$result" != "" ]; then
        echo -e "\t\033[32mPassed: ${file}\033[0m"
    else
        echo -e "\t\033[31mFailed: ${file}\033[0m"
        echo $result | tr ' \n' '#$'
        echo "$($LINT $PATTERN ${file})"
        pass=false
    fi
done

Here some code that I test on,

SELECT
    val1,
    val2,
    val3 
FROM
Reed Richards
  • 4,178
  • 8
  • 41
  • 55
  • [Have you tried using a SQL parser instead?](https://stackoverflow.com/a/1732454/402322) – ceving Mar 02 '21 at 16:40
  • Obviously that would be the first choice. But that means buying one which there is currently no budget. The free ones are not that great. The git hook can be applied to many cases other than SQL. – Reed Richards Mar 02 '21 at 18:58
  • Bying? There are many free parser generators. This is a DDL example for Perl using Marpa https://gist.github.com/ceving/c6df834bbf5ec815becd and Antlr for Java is another option https://stackoverflow.com/q/14464372/402322 – ceving Mar 02 '21 at 19:40
  • While there are parsers out there, I haven't been able to find one that you can plug into Atom or PyCharm without spending plenty of time building it yourself. They should also work with Spark SQL and my templating system. This seems to be the easy way out to catch any problems related to commas or other small mistakes. Without getting into the weeds of parsing. – Reed Richards Mar 04 '21 at 07:37
  • Perl plugs into anything as well as Grep. – ceving Mar 04 '21 at 10:24
  • I answered the question, but you do not like it although the answer is obvious: regular expressions are the wrong tool to parse irregular languages. Your question is like "how to use a screw driver to drill a hole into concrete". And the answer is: "no way". I have told you which is the right tool and you can trust me: it works. I wrote already two SQL parsers: one with Marpa and the other with Antlr. And the [list of alternative parser generators](https://w.wiki/38Rf) is almost endless and [writing a recursive descent parser](https://w.wiki/38Rc) is also well documented and not complicated. – ceving Mar 26 '21 at 08:36

1 Answers1

0

This statement

"$result" != "" 

should be

"$result" = ""

that is empty if it passes.

Reed Richards
  • 4,178
  • 8
  • 41
  • 55