loose-sqlparser-wasm
TypeScript icon, indicating that this package has built-in type declarations

0.1.6 • Public • Published

A simple loose SQL parser for in Web Assembly

Based on a RUST implementation, loose-sqlparser-wasm is a non-validating SQL parser for Javascript. It provides support for parsing and splitting SQL statements.

Usage

import { parseSql, Statement } from "loose-sqlparser-wasm";
const statements = parseSql("SELECT 1; SELECT 2") as Array<Statement>;

Features

  • Support multiple sql statements.

    const statements = parseSql("SELECT 1; SELECT 2") as Array<Statement>;
    statements.len(); // => 2
  • SQL dialect agnostic. Intended to support a wild range of SQL dialects: mysql, postgresql, sqlite, oracle, ...

  • Collect an AST of tokens for each statement:

    const statements = parseSql("SELECT 1; SELECT 2") as Array<Statement>;
    statements[0].tokens; // => ["SELECT", "1", ";"]
    statements[1].tokens; // => ["SELECT", "2"]
  • For token, alongside of the value itself, capture line:column and byte offset of the start and line:column for the end.

    SELECT 1+(4*5)-3
      FROM DUAL
    ;
    start       end         offset  token
    ----------  ----------  ------  ------------------------------------------------
           1:1         1:6       0  SELECT
           1:8         1:8       7  1
           1:9         1:9       8  +
          1:10        1:10       9  (
          1:11        1:11      10    4
          1:12        1:12      11    *
          1:13        1:13      12    5
          1:14        1:14      13  )
          1:15        1:15      14  -
          1:16        1:16      15  3
           2:3         2:6      19  FROM
           2:8        2:11      24  DUAL
           3:1         3:1      29  ;
    
  • Invalid SQL syntaxes don't stop the tokenization.

    // The missing opening parenthesis won't stop the tokenization.
    const statements = parseSql("SELECT (1+2)*3) FROM employee") as Array<Statement>;
    statements[0].tokens; // => ["SELECT", "(", "1", "+", "2", ")", "*", "3", ")", "FROM", "employee"]

/loose-sqlparser-wasm/

    Package Sidebar

    Install

    npm i loose-sqlparser-wasm

    Weekly Downloads

    0

    Version

    0.1.6

    License

    MIT/Apache-2.0

    Unpacked Size

    75.7 kB

    Total Files

    8

    Last publish

    Collaborators

    • pferdinand