SQL Parser

0.5.0 · abandoned · verified Sun Apr 19

The `sql-parser` package provides a JavaScript-based lexer and parser specifically designed for SQL syntax. As of version 0.5.0, it is primarily capable of tokenizing and parsing fairly basic `SELECT` queries, offering functionalities like `WHERE`, `GROUP BY`, `ORDER BY`, and `LIMIT` clause parsing. The lexer outputs tokens in a format compatible with JISON, which the parser then consumes to produce a `Select` object. A notable feature is the ability to reconstruct a well-formatted SQL string from the parsed object via its `.toString()` method. The project appears to be in a largely unmaintained state, with no indication of active development towards comprehensive SQL support or regular releases since its initial limited scope. Its implementation borrowed heavily from CoffeeScript project boilerplate, suggesting an older codebase that predates modern JavaScript module patterns and extensive TypeScript adoption.

Common errors

Warnings

Install

Imports

Quickstart

Demonstrates tokenizing a basic SELECT query, parsing it into a 'Select' object, and then re-generating a formatted SQL string.

const { lexer, parser } = require('sql-parser');

const sqlQuery = "select id, name from users where age > 18 order by name asc limit 10";

console.log('Original SQL:', sqlQuery);

// Tokenize the SQL query
const tokens = lexer.tokenize(sqlQuery);
console.log('Tokens:', JSON.stringify(tokens, null, 2));

// Parse the tokens into a Select object (AST-like structure)
try {
  const selectObject = parser.parse(tokens);
  console.log('Parsed Object (simplified):', selectObject);

  // Convert the parsed object back to a formatted SQL string
  const formattedSql = selectObject.toString();
  console.log('Formatted SQL from object:\n', formattedSql);
} catch (e) {
  console.error('Parsing error:', e.message);
}

view raw JSON →