Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I check (My)SQL statements for syntactical correctness

Tags:

we're currently setting up out integration server and during that process we've set up pre-commit hooks on the SVN so that our developers can't check in files that are syntactically invalid (primarily PHP and XML).

We also have a bunch of .sql files (for MySQL) which I'd like to lint as well. Unfortunately, Google didn't turn up anything useful for this task.

Any ideas?

like image 411
n3rd Avatar asked Oct 19 '10 16:10

n3rd


People also ask

How do I check if a SQL query is correct?

Check - The check is a way for you to check if you have written a legal SQL query. Arrow - This is the execute command button. This will send the query to the server and the server will write back the result to you. Square - This is the stop execution command.

How do I validate a SQL statement?

To validate the syntax of the statements, right-click in the editor, and then select a validation option. To validate the syntax of the statements for the connection that is selected in the Configuration tab, select the Validate statement syntax for current configuration option.

Where can I test MySQL queries?

MySQL and SQL Online Test | TestDome.


2 Answers

After searching for a CLI tool for syntax linting in Mysql to use in Jenkins and didn't find anything quickly (this Stackoverflow question is one of the first results - LOL) I came up with the following solution (OS: Linux, but should be feasible with Windows too):

Something like the follwoing:

lint_result=`mysql mysql_test -B -f -e 'select asdf s where x;' 2>&1`; if [ `echo $lint_result | sed -r "s/ERROR ([0-9]*).*/\1/g"` -eq 1064 ]; then echo -e "Syntax error:\n${lint_result}"; fi Syntax error: ERROR 1064 (42000) at line 1: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'where x' at line 1 

(To check sql files you can use "< filename.sql" instead of -b -e 'statement')

If the syntax of the query can not be parsed by mysql it claims: ERROR 1064 (42000) at line 1: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '' at line 1

Only if the syntax is correct it tries to execute the query and realize that the table don't exists but this isn't interesting anymore:

ERROR 1146 (42S02) at line 1: Table 'mysql_test.s' doesn't exist 

Therefor Error 1064 is the invalid syntax. You only need to create an empty test database because otherwise only errors with a wrong FROM part would appear (here for example the database is needed in order to get a valid syntax check result: 'select asdf from s where x and if;).

As far as i tested it works fine (Version Mysql 5.5).

Here a complete bash script vesion:

#!/bin/bash  source_dir=${1}; database="mysql_test"; mysql_args="-h127.0.0.1";  mysql $mysql_args -B -e "DROP DATABASE IF EXISTS $database; CREATE DATABASE $database;"; for file in `find $source_dir -name "*.sql"`; do     lint_result=`mysql $mysql_args $database -f -b < $file 2>&1`;     if [ "`echo $lint_result | sed -r \"s/ERROR ([0-9]*).*/\1/g\"`" = "1064" ]; then         echo -e "Syntax error in file ${file}:\n${lint_result}" && exit 1;     fi; done 
like image 197
Georg Buske Avatar answered Sep 20 '22 04:09

Georg Buske


The commercial version of MySQL Workbench has a syntax checker for MySQL statements, but of course that would only cover the database aspects. See http://mysql.com/products/workbench/ (though I found the factoid in the help index for the free app).

like image 22
Richard Careaga Avatar answered Sep 19 '22 04:09

Richard Careaga