npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@cratedb/cratedb-sqlparse

v0.0.6

Published

![NPM License](https://img.shields.io/npm/l/@cratedb/cratedb-sqlparse) ![NPM Version](https://img.shields.io/npm/v/@cratedb/cratedb-sqlparse) ![NPM Downloads](https://img.shields.io/npm/dm/@cratedb/cratedb-sqlparse)

Downloads

265

Readme

CrateDB SQL Parser for JavaScript

NPM License NPM Version NPM Downloads

Tests

NPM Unpacked Size NPM Type Definitions

CrateDB SQL Parser for JavaScript, compiled from antlr4 JavaScript compile target.

Simple usage

import {sqlparse} from "@cratedb/cratedb-sqlparse";

const query = `
SELECT * FROM SYS.SHARDS;
INSERT INTO doc.tbl1 VALUES ('metric', 1238123, 'true');
`
const queries = sqlparse(query);

console.log(queries.length)
// 2

console.log(queries[0].query)
// SELECT * FROM SYS.SHARDS

console.log(queries[0].type)
// SELECT

console.log(queries[0].original_query)
// SELECT * FROM SYS.SHARDS;
// INSERT INTO doc.tbl1 VALUES ('metric', 1238123, 'true');

CrateDB version

You can programmatically check the CrateDB version the package was compiled for in index.js

import {__cratedb_version__} from "@cratedb/cratedb-sqlparse";

console.log(__cratedb_version__)
// 5.7.2

Features

Currently, the parser supports a subset of the features of CrateDB's Java/ANTLR parser:

  • First class CrateDB SQL dialect support.
  • Input is case-insensitive.
  • Native errors as exceptions or as objects.
  • Dollar strings.
  • Tables
  • Properties and parametrized properties.

Exceptions and errors.

By default, exceptions are stored in statement.exception.

import {sqlparse} from "@cratedb/cratedb-sqlparse";

const query = `
SELECT COUNT(*) FROM doc.tbl f HERE f.id = 1;

INSERT INTO doc.tbl VALUES (1, 23, 4);
`
const statements = sqlparse(query)
const stmt = statements[0]

if (stmt.exception) {
    console.log(stmt.exception.errorMessage)
    // [line 2:43 mismatched input 'HERE' expecting {<EOF>, ';'}]

    console.log(stmt.exception.errorMessageVerbose)
    //      SELECT COUNT(*) FROM doc.tbl f HERE f.id = 1;
    //                                     ^^^^
    //      INSERT INTO doc.tbl VALUES (1, 23, 4);
}

console.log(stmt.exception)

// ParseError: mismatched input 'HERE' expecting {<EOF>, ';'}
//     at ExceptionCollectorListener.syntaxError (file:///home/surister/PycharmProjects/cratedb-sqlparse/cratedb_sqlparse_js/cratedb_sqlparse/parser.js:115:23)
//     at file:///home/surister/PycharmProjects/cratedb-sqlparse/cratedb_sqlparse_js/node_modules/antlr4/dist/antlr4.node.mjs:1:42125
//     at Array.map (<anonymous>)
//     at wt.syntaxError (file:///home/surister/PycharmProjects/cratedb-sqlparse/cratedb_sqlparse_js/node_modules/antlr4/dist/antlr4.node.mjs:1:42115)
//     at SqlBaseParser.notifyErrorListeners (file:///home/surister/PycharmProjects/cratedb-sqlparse/cratedb_sqlparse_js/node_modules/antlr4/dist/antlr4.node.mjs:1:102085)
//     at Ce.reportInputMismatch (file:///home/surister/PycharmProjects/cratedb-sqlparse/cratedb_sqlparse_js/node_modules/antlr4/dist/antlr4.node.mjs:1:90577)
//     at Ce.reportError (file:///home/surister/PycharmProjects/cratedb-sqlparse/cratedb_sqlparse_js/node_modules/antlr4/dist/antlr4.node.mjs:1:88813)
//     at SqlBaseParser.statements (file:///home/surister/PycharmProjects/cratedb-sqlparse/cratedb_sqlparse_js/cratedb_sqlparse/generated_parser/SqlBaseParser.js:1345:28)
//     at sqlparse (file:///home/surister/PycharmProjects/cratedb-sqlparse/cratedb_sqlparse_js/cratedb_sqlparse/parser.js:207:25)
//     at file:///home/surister/PycharmProjects/cratedb-sqlparse/cratedb_sqlparse_js/t.js:4:14 {
//   query: 'SELECT COUNT(*) FROM doc.tbl f HERE',
//   msg: "mismatched input 'HERE' expecting {<EOF>, ';'}",
//   offendingToken: bt {
//     source: [ [SqlBaseLexer], [CaseInsensitiveStream] ],
//     type: 322,
//     channel: 0,
//     start: 32,
//     stop: 35,
//     tokenIndex: 16,
//     line: 2,
//     column: 31,
//     _text: null
//   },
//   line: 2,
//   column: 31,
//   errorMessage: "[line 2:31 mismatched input 'HERE' expecting {<EOF>, ';'}]",
//   errorMessageVerbose: '\n' +
//     'SELECT COUNT(*) FROM doc.tbl f HERE f.id = 1;\n' +
//     '                               ^^^^\n' +
//     '\n' +
//     'INSERT INTO doc.tbl VALUES (1, 23, 4);\n'
// }

In some situations, you might want sqlparse to throw an error.

You can set raise_exception to true

import {sqlparse} from "@cratedb/cratedb-sqlparse";

let stmt = sqlparse('SELECT COUNT(*) FROM doc.tbl f WHERE .id = 1;', true);

//         throw new ParseError(
//            ^
//
// ParseError: no viable alternative at input 'SELECT COUNT(*) FROM doc.tbl f WHERE .'

Catch the exception:

import {sqlparse} from "@cratedb/cratedb-sqlparse";

try {
    sqlparse('SELECT COUNT(*) FROM doc.tbl f WHERE .id = 1;', true)
} catch (e) {
    console.log(e)
}

[!NOTE] It will only raise the first exception it finds, even if you pass in several statements.

Query metadata

Query metadata can be read with statement.metadata

import {sqlparse} from "@cratedb/cratedb-sqlparse";

const stmt = sqlparse("SELECT A, B FROM doc.tbl12")[0]

console.log(stmt.metadata);

// Metadata {
//   tables: [ Table { name: 'tbl12', schema: 'doc' } ],
//   parameterizedProperties: {},
//   withProperties: {}
// }

Query properties

Properties defined within a WITH statement, statement.metadata.withProperties:.

import {sqlparse} from "@cratedb/cratedb-sqlparse";


const stmt = sqlparse(`
    CREATE TABLE doc.tbl12 (A TEXT) WITH (
      "allocation.max_retries" = 5,
      "blocks.metadata" = false
    );
`)[0]

console.log(stmt.metadata);

// Metadata {
//   tables: [ Table { name: 'tbl12', schema: 'doc' } ],
//   parameterizedProperties: {},
//   withProperties: { 'allocation.max_retries': '5', 'blocks.metadata': 'false' }
// }

Table name

console.log(stmt.metadata.tables)
// [ Table { name: 'tbl12', schema: 'doc' } ]

table = stmt.metadata.tables[0]

console.log(table.schema, table.name, table.fqn)
// doc tbl12 "doc"."tbl12"

Parameterized properties

Parameterized properties are properties without a real defined value, marked with a dollar string, metadata.parameterized_properties

import {sqlparse} from "@cratedb/cratedb-sqlparse";

const stmt = sqlparse(`
    CREATE TABLE doc.tbl12 (A TEXT) WITH (
    "allocation.max_retries" = 5,
    "blocks.metadata" = $1
);
`)[0]

console.log(stmt.metadata)

// Metadata {
//   tables: [ Table { name: 'tbl12', schema: 'doc', fqn: '"doc"."tbl12"' } ],
//   parameterizedProperties: { 'blocks.metadata': '$1' },
//   withProperties: { 'allocation.max_retries': '5', 'blocks.metadata': '$1' }
// }

In this case, blocks.metadata will be in with_properties and parameterized_properties as well.

For values to be picked up they need to start with a dollar '$' and be preceded by integers, e.g. '$1' or '$123'. '$123abc' would not be valid.