Last active
October 18, 2019 14:29
-
-
Save mcandre/f4450fc4bf619b22ec51b27e05e87b39 to your computer and use it in GitHub Desktop.
pass SQL code through Go SQL parser
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"github.com/xwb1989/sqlparser" | |
"flag" | |
"fmt" | |
"io" | |
"io/ioutil" | |
"os" | |
) | |
var flagFile = flag.String("file", "", "SQL script path to check") | |
func Usage() { | |
fmt.Printf("Usage: %s -file <sql script path>\n", os.Args[0]) | |
} | |
func main() { | |
flag.Parse() | |
if flagFile == nil || *flagFile == "" { | |
Usage() | |
os.Exit(1) | |
} | |
filePath := *flagFile | |
sqlScriptBytes, err := ioutil.ReadFile(filePath) | |
if err != nil { | |
panic(err) | |
} | |
sqlScript := string(sqlScriptBytes) | |
tokenizer := sqlparser.NewStringTokenizer(sqlScript) | |
for { | |
_, err := sqlparser.ParseNext(tokenizer) | |
if err == io.EOF { | |
break | |
} | |
if err != nil { | |
fmt.Printf("%s: %v\n", filePath, err) | |
os.Exit(1) | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment