SQL Parsing Utility

raw JSON →
0.1.5 verified Thu Apr 23 auth: no javascript abandoned

`sql-parse` is an unmaintained JavaScript utility for parsing basic SQL queries, with its last known stable version being 0.1.5, published in June 2015. It was designed to provide a simple programmatic interface for tokenizing and parsing SQL strings, extracting components like `SELECT` statements and `WHERE` clauses, including initial support for `OR` operators. Due to its age and lack of maintenance for nearly a decade, it is considered abandoned. Modern SQL dialects, advanced query features, and contemporary JavaScript module systems (ESM) are not supported. Developers seeking robust, actively maintained SQL parsing solutions for diverse database systems (e.g., MySQL, PostgreSQL, BigQuery, Snowflake, FlinkSQL) should consider alternatives like `node-sql-parser`, `dt-sql-parser`, or `@casual-simulation/sql-parser`. Its release cadence was sporadic and ceased many years ago. It lacks official TypeScript types and has no external runtime dependencies.

error SyntaxError: Cannot use import statement outside a module
cause Attempting to use ES Module `import` syntax (`import { parse } from 'sql-parse';`) in a JavaScript file that is not configured as an ES module, or with this CommonJS-only package.
fix
Change the import statement to const { parse } = require('sql-parse');.
error TypeError: (0, _sqlParse.parse) is not a function
cause This error often occurs when a CommonJS package's exports are incorrectly accessed in an ES Module context, or if the `sql-parse` module itself is imported incorrectly.
fix
Ensure you are using CommonJS require syntax: const { parse } = require('sql-parse');. Verify that sql-parse is installed and the path is correct.
error Parse error at line X, column Y: Unexpected token Z
cause The input SQL query contains syntax elements, keywords, or structures that the old `sql-parse` library does not recognize or support. This is common with modern SQL features or database-specific extensions.
fix
Review the problematic SQL statement for advanced features or dialect-specific syntax. If the query is complex or uses modern SQL, consider migrating to a more robust and actively maintained SQL parser.
breaking The `sql-parse` package is abandoned and has not been updated since June 2015. It no longer receives bug fixes, security patches, or feature enhancements. Continued use in production environments is highly discouraged due to significant security risks and compatibility issues with modern JavaScript runtimes and evolving SQL standards.
fix Migrate your application to a actively maintained and modern SQL parsing library, such as `node-sql-parser`, `dt-sql-parser`, `@casual-simulation/sql-parser`, or `js-sql-parser`.
gotcha This package is strictly CommonJS (`require`) and does not support ES Modules (`import/export`). Attempting to use `import` syntax will result in module resolution errors.
fix Ensure all imports for this package use `const { parse } = require('sql-parse');` syntax within a CommonJS environment. For projects primarily using ESM, a full migration to a modern parser is recommended.
gotcha The parser has limited support for SQL dialects and modern SQL syntax. It was designed for basic `SELECT` statements and early SQL standards. Complex queries, specific database dialect features (e.g., PostgreSQL JSON functions, MySQL `WITH` clauses), or advanced DDL/DML operations are likely to cause parse errors or unexpected results.
fix Thoroughly test the parser with your specific SQL queries. For comprehensive and dialect-specific SQL parsing, switch to a more actively developed library that explicitly supports various SQL dialects.
gotcha The `sql-parse` package does not ship with TypeScript declaration files (`.d.ts`). Using it in a TypeScript project will result in implicitly typed `any` or require manual type definitions, reducing type safety.
fix If migration is not immediately feasible, create a custom `sql-parse.d.ts` file with basic type declarations. However, the recommended solution is to migrate to a modern SQL parser that provides built-in TypeScript support.
npm install sql-parse
yarn add sql-parse
pnpm add sql-parse

Demonstrates importing and using the `parse` function to process a basic SQL `SELECT` statement in a CommonJS environment. It also highlights potential issues with unsupported syntax due to the package's age.

const { parse } = require('sql-parse');

const simpleSelect = 'SELECT id, name FROM users WHERE active = true OR age > 30;';
const insertStatement = 'INSERT INTO products (name, price) VALUES ("Laptop", 1200.00);';
const updateStatement = 'UPDATE orders SET status = "shipped" WHERE id = 101;';

try {
  const resultSelect = parse(simpleSelect);
  console.log('Parsed SELECT:', JSON.stringify(resultSelect, null, 2));

  // Note: The specific structure of `result` depends entirely on the library's implementation.
  // Given its age, it might be a simple object or array of tokens/nodes.
  // This example assumes a structure that might expose `statements` or similar.
  if (resultSelect && Array.isArray(resultSelect.statements) && resultSelect.statements.length > 0) {
    console.log('First statement type:', resultSelect.statements[0].type); // e.g., 'SELECT'
  } else {
    console.log('Could not determine statement type for SELECT.');
  }

  // Trying other statement types might fail or yield unexpected results
  // as the README primarily focuses on SELECT queries.
  // const resultInsert = parse(insertStatement);
  // console.log('Parsed INSERT:', JSON.stringify(resultInsert, null, 2));

} catch (error) {
  console.error('SQL Parsing Error:', error.message);
  console.warn('This package is very old and may not support modern or complex SQL syntax.');
}