This is a Go port of the NPM sql-query-identifier library. It identifies the type of each statement in a SQL query string, providing the start and end position of each statement.
- Multi-Statement Support: Parses a string containing multiple SQL statements separated by semicolons.
- Dialect-Aware: Supports multiple SQL dialects, including MySQL, PostgreSQL, MSSQL, and more.
- Statement Identification: Identifies a wide range of statement types (e.g.,
SELECT,INSERT,CREATE TABLE). - Execution Type Analysis: Classifies statements by their behavior (
LISTING,MODIFICATION). - Parameter Extraction: Identifies positional (
?,$1) and named (:name) parameters. - Strict & Non-Strict Modes: Choose whether to error on unknown statement types or classify them as
UNKNOWN.
go get github.com/Infisical/sql-query-identifierThe following example demonstrates how to parse a query string and print the identified statements as a JSON object.
package main
import (
"encoding/json"
"fmt"
"log"
sqlqueryidentifier "github.com/Infisical/sql-query-identifier"
)
func main() {
query := `
INSERT INTO Persons (PersonID, Name) VALUES (1, 'Jack');
SELECT * FROM Persons;
`
// Use Dialect constants for type safety
dialect := sqlqueryidentifier.DialectMySQL
strict := false
options := sqlqueryidentifier.IdentifyOptions{
Dialect: &dialect,
Strict: &strict,
}
results, err := sqlqueryidentifier.Identify(query, options)
if err != nil {
log.Fatalf("Failed to identify query: %v", err)
}
// Marshal the results to JSON for clear output
jsonOutput, err := json.MarshalIndent(results, "", " ")
if err != nil {
log.Fatalf("Failed to marshal results to JSON: %v", err)
}
fmt.Println(string(jsonOutput))
}Identify(query string, options IdentifyOptions) ([]IdentifyResult, error)
query (string): The raw SQL string to be processed.options (IdentifyOptions): Configuration for the parser.Strict (*bool): Iffalse, will classify unknown statements asUNKNOWNinstead of returning an error. Defaults totrue.Dialect (*Dialect): The SQL dialect to use for parsing. Defaults togeneric.
mssqlsqlitemysqloraclepsqlbigquerygeneric(default)
SELECTINSERTUPDATEDELETETRUNCATE
CREATE_DATABASECREATE_SCHEMACREATE_TABLECREATE_VIEWCREATE_TRIGGERCREATE_FUNCTIONCREATE_INDEXCREATE_PROCEDUREDROP_DATABASEDROP_SCHEMADROP_TABLEDROP_VIEWDROP_TRIGGERDROP_FUNCTIONDROP_INDEXDROP_PROCEDUREALTER_DATABASEALTER_SCHEMAALTER_TABLEALTER_VIEWALTER_TRIGGERALTER_FUNCTIONALTER_INDEXALTER_PROCEDURE
SHOW_BINARYSHOW_BINLOGSHOW_CHARACTERSHOW_COLLATIONSHOW_COLUMNSSHOW_CREATESHOW_DATABASESSHOW_ENGINESHOW_ENGINESSHOW_ERRORSSHOW_EVENTSSHOW_FUNCTIONSHOW_GRANTSSHOW_INDEXSHOW_MASTERSHOW_OPENSHOW_PLUGINSSHOW_PRIVILEGESSHOW_PROCEDURESHOW_PROCESSLISTSHOW_PROFILESHOW_PROFILESSHOW_RELAYLOGSHOW_REPLICASSHOW_SLAVESHOW_REPLICASHOW_STATUSSHOW_TABLESHOW_TABLESSHOW_TRIGGERSSHOW_VARIABLESSHOW_WARNINGS
ANON_BLOCK(BigQuery and Oracle dialects only)UNKNOWN(only available if strict mode is disabled)
Execution types classify the behavior of a query.
LISTING: The query lists or retrieves data.MODIFICATION: The query modifies the database structure or data.INFORMATION: The query shows information, such as profiling data.ANON_BLOCK: The query is an anonymous block which may contain multiple statements.UNKNOWN: The query type could not be determined (only available if strict mode is disabled).
This library uses AST and parser techniques to identify the SQL query type. It does not validate the entire query; instead, it validates only the required tokens to identify the statement type.
The identification process is:
- Tokenizing: The input string is broken down into tokens (keywords, strings, comments, etc.).
- Parsing: The stream of tokens is parsed to identify the statement boundaries and types.
- Comments and string contents are ignored to prevent false positives.
- Keywords are expected at the beginning of a statement.
- Semicolons are used to identify the end of a statement.
Because the library does not perform a full SQL validation, it is recommended to use it on queries that have already been successfully executed by a SQL client.
This project is licensed under the MIT License.