This class is the main entry point to this library. First, you instantiate an instance of this class with the SQL dialect you require. Then, you can use the survey method to analyze your SQL script.
Requires the SQL dialect you will be parsing. See SQLDialect.
Optionally, you can provide an options object matching the SQLSurveyorOptions interface.
import { SQLSurveyor, SQLDialect } from 'sql-surveyor';
const surveyor = new SQLSurveyor(SQLDialect.MYSQL);
This method returns a ParsedSql object that describes the key attributes of the SQL script.
sqlScript
: The string containing the SQL script to be surveyed.
This can be one or more SQL statements. Incomplete statements are also handled gracefully,
as described in the ParsedQuery documentation.
import { SQLSurveyor, SQLDialect } from 'sql-surveyor';
const surveyor = new SQLSurveyor(SQLDialect.MYSQL);
const sql = 'SELECT t1.columnA, t2.columnB FROM table1 t1 JOIN table2 t2 ON t1.id = t2.table1_id';
const parsedSql = surveyor.survey(sql);
console.dir(parsedSql, { depth: null });
// ParsedSql {
// parsedQueries: {
// '0': ParsedQuery {
// outputColumns: [
// OutputColumn { columnName: 'columnA', columnAlias: null, tableName: 'table1', tableAlias: 't1'},
// OutputColumn { columnName: 'columnB', columnAlias: null, tableName: 'table2', tableAlias: 't2'}
// ],
// referencedColumns: [
// ReferencedColumn {
// columnName: 'id', tableName: 'table1', tableAlias: 't1',
// locations: Set {
// TokenLocation { lineStart: 1, lineEnd: 1, startIndex: 63, stopIndex: 67 }
// }
// },
// ReferencedColumn {
// columnName: 'table1_id', tableName: 'table2', tableAlias: 't2',
// locations: Set {
// TokenLocation { lineStart: 1, lineEnd: 1, startIndex: 71, stopIndex: 82 }
// }
// }
// ],
// referencedTables: {
// table1: ReferencedTable {
// tableName: 'table1', schemaName: null, databaseName: null, aliases: Set { 't1' },
// locations: Set {
// TokenLocation { lineStart: 1, lineEnd: 1, startIndex: 35, stopIndex: 40 },
// TokenLocation { lineStart: 1, lineEnd: 1, startIndex: 63, stopIndex: 64 },
// TokenLocation { lineStart: 1, lineEnd: 1, startIndex: 7, stopIndex: 8 } }
// },
// table2: ReferencedTable {
// tableName: 'table2', schemaName: null, databaseName: null, aliases: Set { 't2' },
// locations: Set {
// TokenLocation { lineStart: 1, lineEnd: 1, startIndex: 50, stopIndex: 55 },
// TokenLocation { lineStart: 1, lineEnd: 1, startIndex: 71, stopIndex: 72 },
// TokenLocation { lineStart: 1, lineEnd: 1, startIndex: 19, stopIndex: 20 }
// }
// }
// },
// tokens: {
// '0': Token { value: 'SELECT', location: TokenLocation { lineStart: 1, lineEnd: 1, startIndex: 0, stopIndex: 5 }},
// '7': Token { value: 't1', location: TokenLocation { lineStart: 1, lineEnd: 1, startIndex: 7, stopIndex: 8 }},
// '9': Token { value: '.', location: TokenLocation { lineStart: 1, lineEnd: 1, startIndex: 9, stopIndex: 9 }},
// '10': Token { value: 'columnA', location: TokenLocation { lineStart: 1, lineEnd: 1, startIndex: 10, stopIndex: 16 }},
// '17': Token { value: ',', location: TokenLocation { lineStart: 1, lineEnd: 1, startIndex: 17, stopIndex: 17 }},
// '19': Token { value: 't2', location: TokenLocation { lineStart: 1, lineEnd: 1, startIndex: 19, stopIndex: 20 }},
// '21': Token { value: '.', location: TokenLocation { lineStart: 1, lineEnd: 1, startIndex: 21, stopIndex: 21 }},
// '22': Token { value: 'columnB', location: TokenLocation { lineStart: 1, lineEnd: 1, startIndex: 22, stopIndex: 28 }},
// '30': Token { value: 'FROM', location: TokenLocation { lineStart: 1, lineEnd: 1, startIndex: 30, stopIndex: 33 }},
// '35': Token { value: 'table1', location: TokenLocation { lineStart: 1, lineEnd: 1, startIndex: 35, stopIndex: 40 }},
// '42': Token { value: 't1', location: TokenLocation { lineStart: 1, lineEnd: 1, startIndex: 42, stopIndex: 43 }},
// '45': Token { value: 'JOIN', location: TokenLocation { lineStart: 1, lineEnd: 1, startIndex: 45, stopIndex: 48 }},
// '50': Token { value: 'table2', location: TokenLocation { lineStart: 1, lineEnd: 1, startIndex: 50, stopIndex: 55 }},
// '57': Token { value: 't2', location: TokenLocation { lineStart: 1, lineEnd: 1, startIndex: 57, stopIndex: 58 }},
// '60': Token { value: 'ON', location: TokenLocation { lineStart: 1, lineEnd: 1, startIndex: 60, stopIndex: 61 }},
// '63': Token { value: 't1', location: TokenLocation { lineStart: 1, lineEnd: 1, startIndex: 63, stopIndex: 64 }},
// '65': Token { value: '.', location: TokenLocation { lineStart: 1, lineEnd: 1, startIndex: 65, stopIndex: 65 }},
// '66': Token { value: 'id', location: TokenLocation { lineStart: 1, lineEnd: 1, startIndex: 66, stopIndex: 67 }},
// '69': Token { value: '=', location: TokenLocation { lineStart: 1, lineEnd: 1, startIndex: 69, stopIndex: 69 }},
// '71': Token { value: 't2', location: TokenLocation { lineStart: 1, lineEnd: 1, startIndex: 71, stopIndex: 72 }},
// '73': Token { value: '.', location: TokenLocation { lineStart: 1, lineEnd: 1, startIndex: 73, stopIndex: 73 }},
// '74': Token { value: 'table1_id', location: TokenLocation { lineStart: 1, lineEnd: 1, startIndex: 74, stopIndex: 82 }}
// },
// query: 'SELECT t1.columnA, t2.columnB FROM table1 t1 JOIN table2 t2 ON t1.id = t2.table1_id',
// queryType: 'DML',
// queryLocation: TokenLocation { lineStart: 1,lineEnd: 1,startIndex: 0,stopIndex: 82 },
// queryErrors: [],
// subqueries: {},
// commonTableExpressions: {}
// }
// }
// }
Changes the SQL dialect to be used for surveying future SQL strings.
import { SQLSurveyor, SQLDialect } from 'sql-surveyor';
const surveyor = new SQLSurveyor(SQLDialect.MYSQL);
const sql = 'SELECT t1.columnA, t2.columnB FROM table1 t1 JOIN table2 t2 ON t1.id = t2.table1_id';
const parsedSql = surveyor.survey(sql);
const tsql = 'SELECT [t1].[columnA], [t2].[columnB] FROM [table1] [t1] JOIN [table2] [t2] ON [t1].[id] = [t2].[table1_id]';
surveyor.setDialect(SQLDialect.TSQL);
const parsedTSql = surveyor.survey(tsql);
This class represents a column that will be output or returned by a given SQL statement.
For example, in the SELECT statement SELECT columnA FROM table1 WHERE columnB > 0
,
the output column is columnA
.
import { SQLSurveyor, SQLDialect } from 'sql-surveyor';
const surveyor = new SQLSurveyor(SQLDialect.MYSQL);
import { SQLSurveyor, SQLDialect } from 'sql-surveyor';
const surveyor = new SQLSurveyor(SQLDialect.MYSQL);
const sql = 'SELECT t1.columnA cA, t2.columnB cB FROM table1 t1 JOIN table2 t2 ON t1.id = t2.table1_id';
const parsedSql = surveyor.survey(sql);
console.dir(parsedSql.parsedQueries[0].outputColumns, { depth: null });
// [
// OutputColumn { columnName: 'columnA', columnAlias: 'cA', tableName: 'table1', tableAlias: 't1'},
// OutputColumn { columnName: 'columnB', columnAlias: 'cB', tableName: 'table2', tableAlias: 't2'}
// ]
The name of the column being output.
The alias for the column (if one was used when defining the output column).
The name of the table that the column belongs to (if it, or an alias, is used when defining the output column).
The alias of the table that the column belongs to (if it is used when defining the output column)
Each query that is parsed in the SQL Script provided to the SQLSurveyor.survey method,
will be represented by a ParsedQuery object.
Each subquery or common table expression within the query will also be defined as a
separate ParsedQuery object (stored in the subqueries
and commonTableExpressions
properties).
If you need to access the values that pertain only to a specific ParsedQuery object, you should
access their properties directly.
If you need to retrieve all values for the entire query (and all the subqueries and common table expressions),
then you should use the relevant method instead.
import { SQLSurveyor, SQLDialect } from 'sql-surveyor';
const surveyor = new SQLSurveyor(SQLDialect.MYSQL);
const sql = 'SELECT t1.columnA, t2.columnB FROM table1 t1 JOIN table2 t2 ON t1.id = t2.table1_id';
const parsedSql = surveyor.survey(sql);
console.dir(parsedSql.parsedQueries[0], { depth: null });
// ParsedQuery {
// outputColumns: [
// OutputColumn { columnName: 'columnA', columnAlias: null, tableName: 'table1', tableAlias: 't1'},
// OutputColumn { columnName: 'columnB', columnAlias: null, tableName: 'table2', tableAlias: 't2'}
// ],
// referencedColumns: [
// ReferencedColumn {
// columnName: 'id', tableName: 'table1', tableAlias: 't1',
// locations: Set {
// TokenLocation { lineStart: 1, lineEnd: 1, startIndex: 63, stopIndex: 67 }
// }
// },
// ReferencedColumn {
// columnName: 'table1_id', tableName: 'table2', tableAlias: 't2',
// locations: Set {
// TokenLocation { lineStart: 1, lineEnd: 1, startIndex: 71, stopIndex: 82 }
// }
// }
// ],
// referencedTables: {
// table1: ReferencedTable {
// tableName: 'table1', schemaName: null, databaseName: null, aliases: Set { 't1' },
// locations: Set {
// TokenLocation { lineStart: 1, lineEnd: 1, startIndex: 35, stopIndex: 40 },
// TokenLocation { lineStart: 1, lineEnd: 1, startIndex: 63, stopIndex: 64 },
// TokenLocation { lineStart: 1, lineEnd: 1, startIndex: 7, stopIndex: 8 } }
// },
// table2: ReferencedTable {
// tableName: 'table2', schemaName: null, databaseName: null, aliases: Set { 't2' },
// locations: Set {
// TokenLocation { lineStart: 1, lineEnd: 1, startIndex: 50, stopIndex: 55 },
// TokenLocation { lineStart: 1, lineEnd: 1, startIndex: 71, stopIndex: 72 },
// TokenLocation { lineStart: 1, lineEnd: 1, startIndex: 19, stopIndex: 20 }
// }
// }
// },
// tokens: {
// '0': Token { value: 'SELECT', location: TokenLocation { lineStart: 1, lineEnd: 1, startIndex: 0, stopIndex: 5 }},
// '7': Token { value: 't1', location: TokenLocation { lineStart: 1, lineEnd: 1, startIndex: 7, stopIndex: 8 }},
// '9': Token { value: '.', location: TokenLocation { lineStart: 1, lineEnd: 1, startIndex: 9, stopIndex: 9 }},
// '10': Token { value: 'columnA', location: TokenLocation { lineStart: 1, lineEnd: 1, startIndex: 10, stopIndex: 16 }},
// '17': Token { value: ',', location: TokenLocation { lineStart: 1, lineEnd: 1, startIndex: 17, stopIndex: 17 }},
// '19': Token { value: 't2', location: TokenLocation { lineStart: 1, lineEnd: 1, startIndex: 19, stopIndex: 20 }},
// '21': Token { value: '.', location: TokenLocation { lineStart: 1, lineEnd: 1, startIndex: 21, stopIndex: 21 }},
// '22': Token { value: 'columnB', location: TokenLocation { lineStart: 1, lineEnd: 1, startIndex: 22, stopIndex: 28 }},
// '30': Token { value: 'FROM', location: TokenLocation { lineStart: 1, lineEnd: 1, startIndex: 30, stopIndex: 33 }},
// '35': Token { value: 'table1', location: TokenLocation { lineStart: 1, lineEnd: 1, startIndex: 35, stopIndex: 40 }},
// '42': Token { value: 't1', location: TokenLocation { lineStart: 1, lineEnd: 1, startIndex: 42, stopIndex: 43 }},
// '45': Token { value: 'JOIN', location: TokenLocation { lineStart: 1, lineEnd: 1, startIndex: 45, stopIndex: 48 }},
// '50': Token { value: 'table2', location: TokenLocation { lineStart: 1, lineEnd: 1, startIndex: 50, stopIndex: 55 }},
// '57': Token { value: 't2', location: TokenLocation { lineStart: 1, lineEnd: 1, startIndex: 57, stopIndex: 58 }},
// '60': Token { value: 'ON', location: TokenLocation { lineStart: 1, lineEnd: 1, startIndex: 60, stopIndex: 61 }},
// '63': Token { value: 't1', location: TokenLocation { lineStart: 1, lineEnd: 1, startIndex: 63, stopIndex: 64 }},
// '65': Token { value: '.', location: TokenLocation { lineStart: 1, lineEnd: 1, startIndex: 65, stopIndex: 65 }},
// '66': Token { value: 'id', location: TokenLocation { lineStart: 1, lineEnd: 1, startIndex: 66, stopIndex: 67 }},
// '69': Token { value: '=', location: TokenLocation { lineStart: 1, lineEnd: 1, startIndex: 69, stopIndex: 69 }},
// '71': Token { value: 't2', location: TokenLocation { lineStart: 1, lineEnd: 1, startIndex: 71, stopIndex: 72 }},
// '73': Token { value: '.', location: TokenLocation { lineStart: 1, lineEnd: 1, startIndex: 73, stopIndex: 73 }},
// '74': Token { value: 'table1_id', location: TokenLocation { lineStart: 1, lineEnd: 1, startIndex: 74, stopIndex: 82 }}
// },
// query: 'SELECT t1.columnA, t2.columnB FROM table1 t1 JOIN table2 t2 ON t1.id = t2.table1_id',
// queryType: 'DML',
// queryLocation: TokenLocation { lineStart: 1,lineEnd: 1,startIndex: 0,stopIndex: 82 },
// queryErrors: [],
// subqueries: {},
// commonTableExpressions: {}
// }
// }
Requires the SQL dialect you will be parsing. See SQLDialect.
Retrieves the ReferencedTable objects for all tables that were detected in the query, and any subqueries and common table expressions, indexed by the name of the table.
Retrieves the ReferencedColumn objects for all columns that were detected in the query, and any subqueries and common table expressions.
Returns the name of the table, based on the provided alias.
Returns all aliases used for a table with the provided name.
Retrieves the token located at the zero-based index within the query.
The index is based on the token’s index in the entire SQL script object that was parsed.
Returns null
if no token matches the stringIndex
.
Retrieves the token immediately preceding the token at the given string location.
The index is based on the token’s index in the entire SQL script object that was parsed.
Returns null
if no token is found before the stringIndex
.
Retrieves the token immediately after the token at the given string location.
The index is based on the token’s index in the entire SQL script object that was parsed.
Returns null
if no token is found after the stringIndex
.
Get the ReferencedColumn from the query if it matches the columnName
and tableName
(optional) and tableAlias
(optional) provided.
Retrieves the lowest level ParsedQuery object at the given string location. That is, if there is a subquery at that location (or a subquery within that subquery, etc.), return the ParsedQuery representing the deepest (or smallest).
The string with the full query that was parsed and identified.
The type of query (i.e. DDL, DML, etc.). See QueryType for all possible types.
The columns that are output by this query. See OutputColumn for more details. Does not include columns output by subqueries or common table expressions within the given query.
The columns referenced by this query. See ReferencedColumn for more details. Does not include columns referenced by subqueries or common table expressions within the given query.
The tables referenced by this query. See ReferencedTable for more details. Does not include columns output by subqueries or common table expressions within the given query.
All the individual tokens that have been identified in the query. See Token for more details. This does include tokens in subqueries and common table expressions within the given query.
The location within the originally surveyed SQL script. See TokenLocation for more details.
If parsing the SQL in this query caused an error, this property will be populated. See ParsingError for more details.
If this ParsedQuery is a common table expression (CTE), the name or alias for this CTE will be populated here.
The subqueries that are contained within this query, indexed by their location within the SQL script that was originally surveyed.
The common table expressions that are contained within this query, indexed by their location within the SQL script that was originally surveyed.
This class represents all the queries that were identified by the SQL surveyor.
import { SQLSurveyor, SQLDialect } from 'sql-surveyor';
const surveyor = new SQLSurveyor(SQLDialect.MYSQL);
const sql = 'SELECT t1.columnA, t2.columnB FROM table1 t1 JOIN table2 t2 ON t1.id = t2.table1_id';
const parsedSql = surveyor.survey(sql);
console.dir(parsedSql, { depth: null });
// ParsedSql {
// parsedQueries: {
// '0': ParsedQuery {
// outputColumns: [
// OutputColumn { columnName: 'columnA', columnAlias: null, tableName: 'table1', tableAlias: 't1'},
// OutputColumn { columnName: 'columnB', columnAlias: null, tableName: 'table2', tableAlias: 't2'}
// ],
// referencedColumns: [
// ReferencedColumn {
// columnName: 'id', tableName: 'table1', tableAlias: 't1',
// locations: Set {
// TokenLocation { lineStart: 1, lineEnd: 1, startIndex: 63, stopIndex: 67 }
// }
// },
// ReferencedColumn {
// columnName: 'table1_id', tableName: 'table2', tableAlias: 't2',
// locations: Set {
// TokenLocation { lineStart: 1, lineEnd: 1, startIndex: 71, stopIndex: 82 }
// }
// }
// ],
// referencedTables: {
// table1: ReferencedTable {
// tableName: 'table1', schemaName: null, databaseName: null, aliases: Set { 't1' },
// locations: Set {
// TokenLocation { lineStart: 1, lineEnd: 1, startIndex: 35, stopIndex: 40 },
// TokenLocation { lineStart: 1, lineEnd: 1, startIndex: 63, stopIndex: 64 },
// TokenLocation { lineStart: 1, lineEnd: 1, startIndex: 7, stopIndex: 8 } }
// },
// table2: ReferencedTable {
// tableName: 'table2', schemaName: null, databaseName: null, aliases: Set { 't2' },
// locations: Set {
// TokenLocation { lineStart: 1, lineEnd: 1, startIndex: 50, stopIndex: 55 },
// TokenLocation { lineStart: 1, lineEnd: 1, startIndex: 71, stopIndex: 72 },
// TokenLocation { lineStart: 1, lineEnd: 1, startIndex: 19, stopIndex: 20 }
// }
// }
// },
// tokens: {
// '0': Token { value: 'SELECT', location: TokenLocation { lineStart: 1, lineEnd: 1, startIndex: 0, stopIndex: 5 }},
// '7': Token { value: 't1', location: TokenLocation { lineStart: 1, lineEnd: 1, startIndex: 7, stopIndex: 8 }},
// '9': Token { value: '.', location: TokenLocation { lineStart: 1, lineEnd: 1, startIndex: 9, stopIndex: 9 }},
// '10': Token { value: 'columnA', location: TokenLocation { lineStart: 1, lineEnd: 1, startIndex: 10, stopIndex: 16 }},
// '17': Token { value: ',', location: TokenLocation { lineStart: 1, lineEnd: 1, startIndex: 17, stopIndex: 17 }},
// '19': Token { value: 't2', location: TokenLocation { lineStart: 1, lineEnd: 1, startIndex: 19, stopIndex: 20 }},
// '21': Token { value: '.', location: TokenLocation { lineStart: 1, lineEnd: 1, startIndex: 21, stopIndex: 21 }},
// '22': Token { value: 'columnB', location: TokenLocation { lineStart: 1, lineEnd: 1, startIndex: 22, stopIndex: 28 }},
// '30': Token { value: 'FROM', location: TokenLocation { lineStart: 1, lineEnd: 1, startIndex: 30, stopIndex: 33 }},
// '35': Token { value: 'table1', location: TokenLocation { lineStart: 1, lineEnd: 1, startIndex: 35, stopIndex: 40 }},
// '42': Token { value: 't1', location: TokenLocation { lineStart: 1, lineEnd: 1, startIndex: 42, stopIndex: 43 }},
// '45': Token { value: 'JOIN', location: TokenLocation { lineStart: 1, lineEnd: 1, startIndex: 45, stopIndex: 48 }},
// '50': Token { value: 'table2', location: TokenLocation { lineStart: 1, lineEnd: 1, startIndex: 50, stopIndex: 55 }},
// '57': Token { value: 't2', location: TokenLocation { lineStart: 1, lineEnd: 1, startIndex: 57, stopIndex: 58 }},
// '60': Token { value: 'ON', location: TokenLocation { lineStart: 1, lineEnd: 1, startIndex: 60, stopIndex: 61 }},
// '63': Token { value: 't1', location: TokenLocation { lineStart: 1, lineEnd: 1, startIndex: 63, stopIndex: 64 }},
// '65': Token { value: '.', location: TokenLocation { lineStart: 1, lineEnd: 1, startIndex: 65, stopIndex: 65 }},
// '66': Token { value: 'id', location: TokenLocation { lineStart: 1, lineEnd: 1, startIndex: 66, stopIndex: 67 }},
// '69': Token { value: '=', location: TokenLocation { lineStart: 1, lineEnd: 1, startIndex: 69, stopIndex: 69 }},
// '71': Token { value: 't2', location: TokenLocation { lineStart: 1, lineEnd: 1, startIndex: 71, stopIndex: 72 }},
// '73': Token { value: '.', location: TokenLocation { lineStart: 1, lineEnd: 1, startIndex: 73, stopIndex: 73 }},
// '74': Token { value: 'table1_id', location: TokenLocation { lineStart: 1, lineEnd: 1, startIndex: 74, stopIndex: 82 }}
// },
// query: 'SELECT t1.columnA, t2.columnB FROM table1 t1 JOIN table2 t2 ON t1.id = t2.table1_id',
// queryType: 'DML',
// queryLocation: TokenLocation { lineStart: 1,lineEnd: 1,startIndex: 0,stopIndex: 82 },
// queryErrors: [],
// subqueries: {},
// commonTableExpressions: {}
// }
// }
// }
Retrieves the ParsedQuery representing the query located at the zero-based string index within the SQL script originally surveyed.
Retrieves the index of the query located at the zero-based string index within the SQL script originally surveyed. The query index corresponds to the indexes within the parsedQueries property.
Retrieves the locations of all the queries. See TokenLocation for more details.
The queries that are contained within this SQL script, indexed by the index within the SQL script string that the query starts.
If a SQL query is not able to be parsed by our ANTLR4 parser, because it is incomplete or incorrect, the error will be defined by this class.
See ParsingErrorType for more details on the types of errors that can be reported.
import { SQLSurveyor, SQLDialect } from 'sql-surveyor';
const surveyor = new SQLSurveyor(SQLDialect.MYSQL);
const sql = 'SELECT * FROM WHERE ';
const parsedSql = surveyor.survey(sql);
console.dir(parsedSql.parsedQueries[0].queryErrors, { depth: null });
// [
// ParsingError {
// token: Token {
// value: 'WHERE',
// location: TokenLocation {
// lineStart: 1,
// lineEnd: 1,
// startIndex: 14,
// stopIndex: 18
// }
// },
// type: 'MISMATCHED_INPUT'
// }
// ]
The Token where the parsing error occurred.
The type of parsing error that occurred.
This class enumerates the parsing error types that can be reported. This parsing errors are defined by ANTLR4. Possible values include:
FAILED_PREDICATE
MISSING_TOKEN
MISMATCHED_INPUT
NO_VIABLE_ALTERNATIVE
UNWANTED_TOKEN
There is a syntax error with the token.
See the official ANTLR4 documentation for this error here.
There is a token (or multiple tokens) missing before the token at this location.
The query is likely incomplete.
See the official ANTLR4 documentation for this error here.
Either the token specified, or the tokens that follow it are not valid at their current location.
See the official ANTLR4 documentation for this error here.
Likely, if the specified token was removed, the query would be correct.
This class enumerates the types of queries that can be identified. Possible values include:
DML // Data Manipulation Language
// - Queries like SELECT, UPDATE, DELETE that handle data
DDL // Data Definition Language
// - Queries like CREATE, ALTER, DROP that handle data structures
STORED_PROCEDURE // Stored Procedures
This class defines columns that have been referenced in the query, but are not output by the query.
import { SQLSurveyor, SQLDialect } from 'sql-surveyor';
const surveyor = new SQLSurveyor(SQLDialect.MYSQL);
import { SQLSurveyor, SQLDialect } from 'sql-surveyor';
const surveyor = new SQLSurveyor(SQLDialect.MYSQL);
const sql = 'SELECT t1.columnA, t2.columnB FROM table1 t1 JOIN table2 t2 ON t1.id = t2.table1_id';
const parsedSql = surveyor.survey(sql);
console.dir(parsedSql.parsedQueries[0].referencedColumns, { depth: null });
// [
// ReferencedColumn {
// columnName: 'id', tableName: 'table1', tableAlias: 't1',
// locations: Set {
// TokenLocation { lineStart: 1, lineEnd: 1, startIndex: 63, stopIndex: 67 }
// }
// },
// ReferencedColumn {
// columnName: 'table1_id', tableName: 'table2', tableAlias: 't2',
// locations: Set {
// TokenLocation { lineStart: 1, lineEnd: 1, startIndex: 71, stopIndex: 82 }
// }
// }
// ]
The name of the column that has been referenced.
The name of the table that the column belongs to (if it, or an alias, is used when referencing the column).
The name of the table that the column belongs to (if it is used when referencing the column).
The locations within the original SQL script that this column is referenced at.
This class defines tables that have been referenced in the query.
import { SQLSurveyor, SQLDialect } from 'sql-surveyor';
const surveyor = new SQLSurveyor(SQLDialect.MYSQL);
const sql = 'SELECT t1.columnA, t2.columnB FROM table1 t1 JOIN table2 t2 ON t1.id = t2.table1_id';
const parsedSql = surveyor.survey(sql);
console.dir(parsedSql.parsedQueries[0].referencedTables, { depth: null });
// {
// table1: ReferencedTable {
// tableName: 'table1', schemaName: null, databaseName: null, aliases: Set { 't1' },
// locations: Set {
// TokenLocation { lineStart: 1, lineEnd: 1, startIndex: 35, stopIndex: 40 },
// TokenLocation { lineStart: 1, lineEnd: 1, startIndex: 63, stopIndex: 64 },
// TokenLocation { lineStart: 1, lineEnd: 1, startIndex: 7, stopIndex: 8 } }
// },
// table2: ReferencedTable {
// tableName: 'table2', schemaName: null, databaseName: null, aliases: Set { 't2' },
// locations: Set {
// TokenLocation { lineStart: 1, lineEnd: 1, startIndex: 50, stopIndex: 55 },
// TokenLocation { lineStart: 1, lineEnd: 1, startIndex: 71, stopIndex: 72 },
// TokenLocation { lineStart: 1, lineEnd: 1, startIndex: 19, stopIndex: 20 }
// }
// }
// }
Creates an idenitical copy of the referenced table.
The name of the table, as used in the query.
The schema the table belongs to. Populated if a schema hint was used in the query.
The database the table belongs to. Populated if a database hint was used in the query (currently only used for T-SQL).
All aliases used for the table in the query.
The locations within the original SQL script that this table is referenced at.
This class enumerates the SQL dialects that are supported. Possible values include:
MYSQL
TSQL // For SQL Server
PLSQL // For Oracle
PLpgSQL // For PostgreSQL
import { SQLSurveyor, SQLDialect } from 'sql-surveyor';
const surveyor = new SQLSurveyor(SQLDialect.MYSQL);
This interface describes customizable behaviour for the SQLSurveyor instance.
Controls whether errors that occur during processing are logged to the console. This option does not affect error tracking due to SQL syntax errors, only runtime errors.
Default: false
Controls whether errors that occur during processing are thrown. This option does not affect error tracking due to SQL syntax errors, only runtime errors.
Default: true
import { SQLSurveyor, SQLDialect } from 'sql-surveyor';
const options = {
logErrors: true,
throwErrors: false
};
const surveyor = new SQLSurveyor(SQLDialect.MYSQL, options);
A single word or punctuation used in the SQL script. A token could be a SQL keyword, a comma, a column or table, a semi-colon, etc. Whitespace is not included as a token.
import { SQLSurveyor, SQLDialect } from 'sql-surveyor';
const surveyor = new SQLSurveyor(SQLDialect.MYSQL);
const sql = 'SELECT t1.columnA, t2.columnB FROM table1 t1 JOIN table2 t2 ON t1.id = t2.table1_id';
const parsedSql = surveyor.survey(sql);
console.dir(parsedSql.parsedQueries[0].tokens, { depth: null });
// {
// '0': Token { value: 'SELECT', type: 'KEYWORD', location: TokenLocation { lineStart: 1, lineEnd: 1, startIndex: 0, stopIndex: 5 }},
// '7': Token { value: 't1', type: 'IDENTIFIER', location: TokenLocation { lineStart: 1, lineEnd: 1, startIndex: 7, stopIndex: 8 }},
// '9': Token { value: '.', type: 'OPERATOR', location: TokenLocation { lineStart: 1, lineEnd: 1, startIndex: 9, stopIndex: 9 }},
// '10': Token { value: 'columnA', type: 'IDENTIFIER', location: TokenLocation { lineStart: 1, lineEnd: 1, startIndex: 10, stopIndex: 16 }},
// '17': Token { value: ',', type: 'OPERATOR', location: TokenLocation { lineStart: 1, lineEnd: 1, startIndex: 17, stopIndex: 17 }},
// '19': Token { value: 't2', type: 'IDENTIFIER', location: TokenLocation { lineStart: 1, lineEnd: 1, startIndex: 19, stopIndex: 20 }},
// '21': Token { value: '.', type: 'OPERATOR', location: TokenLocation { lineStart: 1, lineEnd: 1, startIndex: 21, stopIndex: 21 }},
// '22': Token { value: 'columnB', type: 'IDENTIFIER', location: TokenLocation { lineStart: 1, lineEnd: 1, startIndex: 22, stopIndex: 28 }},
// '30': Token { value: 'FROM', type: 'KEYWORD', location: TokenLocation { lineStart: 1, lineEnd: 1, startIndex: 30, stopIndex: 33 }},
// '35': Token { value: 'table1', type: 'IDENTIFIER', location: TokenLocation { lineStart: 1, lineEnd: 1, startIndex: 35, stopIndex: 40 }},
// '42': Token { value: 't1', type: 'IDENTIFIER', location: TokenLocation { lineStart: 1, lineEnd: 1, startIndex: 42, stopIndex: 43 }},
// '45': Token { value: 'JOIN', type: 'KEYWORD', location: TokenLocation { lineStart: 1, lineEnd: 1, startIndex: 45, stopIndex: 48 }},
// '50': Token { value: 'table2', type: 'IDENTIFIER', location: TokenLocation { lineStart: 1, lineEnd: 1, startIndex: 50, stopIndex: 55 }},
// '57': Token { value: 't2', type: 'IDENTIFIER', location: TokenLocation { lineStart: 1, lineEnd: 1, startIndex: 57, stopIndex: 58 }},
// '60': Token { value: 'ON', type: 'KEYWORD', location: TokenLocation { lineStart: 1, lineEnd: 1, startIndex: 60, stopIndex: 61 }},
// '63': Token { value: 't1', type: 'IDENTIFIER', location: TokenLocation { lineStart: 1, lineEnd: 1, startIndex: 63, stopIndex: 64 }},
// '65': Token { value: '.', type: 'OPERATOR', location: TokenLocation { lineStart: 1, lineEnd: 1, startIndex: 65, stopIndex: 65 }},
// '66': Token { value: 'id', type: 'IDENTIFIER', location: TokenLocation { lineStart: 1, lineEnd: 1, startIndex: 66, stopIndex: 67 }},
// '69': Token { value: '=', type: 'OPERATOR', location: TokenLocation { lineStart: 1, lineEnd: 1, startIndex: 69, stopIndex: 69 }},
// '71': Token { value: 't2', type: 'IDENTIFIER', location: TokenLocation { lineStart: 1, lineEnd: 1, startIndex: 71, stopIndex: 72 }},
// '73': Token { value: '.', type: 'OPERATOR', location: TokenLocation { lineStart: 1, lineEnd: 1, startIndex: 73, stopIndex: 73 }},
// '74': Token { value: 'table1_id', type: 'IDENTIFIER', location: TokenLocation { lineStart: 1, lineEnd: 1, startIndex: 74, stopIndex: 82 }}
// }
Based on the location already populated by the token, set the value of the token by getting the substring from the provided input
.
The text of the token.
The location, within the SQL script of the token.
Defines the location of a token within the SQL script.
import { SQLSurveyor, SQLDialect } from 'sql-surveyor';
const surveyor = new SQLSurveyor(SQLDialect.MYSQL);
const sql = 'SELECT t1.columnA, t2.columnB FROM table1 t1 JOIN table2 t2 ON t1.id = t2.table1_id';
const parsedSql = surveyor.survey(sql);
console.dir(parsedSql.parsedQueries[0].tokens[0].location, { depth: null });
// { lineStart: 1, lineEnd: 1, startIndex: 0, stopIndex: 5 }
Get the value of the token by getting the substring from the provided input
.
Creates an idenitical copy of the token.
The zero-based index where the token starts (inclusive of this index).
The zero-based index where the token stops (inclusive of this index).
The one-based row number where the token starts.
The one-based row number where the token ends.
This class enumerates types of Tokens that will be identified. Possible values include:
COMMENT // i.e. -- This is a comment
IDENTIFIER // i.e. tableName
KEYWORD // i.e. SELECT
LITERAL // i.e. 100 or 'This is a string literal'
OPERATOR // i.e. >, <, = or .