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

tsparser

v1.0.2

Published

A simple parser to split multiple statement SQL queries to seperated statements for MySQL, PostgreSQL and Microsoft SQL Server

Downloads

422

Readme

TSParser / SQL Statement Parser

A simple parser to split multiple statement SQL queries to separated statements for MySQL, PostgreSQL and Microsoft SQL Server

What does it do and which database engines are supported?

It supports MySQL, PostgreSQL and Microsoft SQL Server. Our parser splits multi-statement SQL queries into single statements.

Install via npm

npm install --save tsparser

Import TSParser to your project

import {TSParser} from 'TSParser'

TSParser.parse() function description

static parse(query: string, dbType: string, delimiter: string): Array<string> {
        ...
    }

It expects 3 parameters ;

query : SQL query

dbType : mysql, pg or mssql

delimiter: semicolon (;) for MySQL and PostgreSQL, 'GO' for Microsoft SQL Server

TSParser can parse;

SQL Queries

Stored procedures, functions, views, etc..

PostgreSQL's tags (like $mytag$ )

MySQL's 'DELIMITER’

MySQL Example

In MySQL, semicolon (;) is default delimiter.

    var mysqlQueriesBasic : string = 'SELECT * FROM users;SELECT * FROM user_details;'
    var mysqlStatements = TSParser.parse(mysqlQueriesBasic, 'mysql', ';');
    mysqlStatements.forEach(statement => {
            console.log(statement + '\n-----------');
        });

It will return an array with 2 items, items are;

SELECT * FROM users

SELECT * FROM user_details

MySQL Stored Procedure and Regular Queries

    DELIMITER //
    CREATE PROCEDURE country_hos(IN con CHAR(20))
    BEGIN
        SELECT Name, HeadOfState FROM Country
        WHERE Continent = con;
    END //
    DELIMITER ;
    SELECT * FROM users;
    SELECT * FROM user_details;

It will return an array with 3 items, items are;

  CREATE PROCEDURE country_hos(IN con CHAR(20))
  BEGIN
      SELECT Name, HeadOfState FROM Country
      WHERE Continent = con;
  END

SELECT * FROM users

SELECT * FROM user_details

PostgreSQL Example

In PostgreSQL, semicolon (;) is default delimiter.

    var postgreSQLQueriesBasic : string = 'SELECT * FROM users;SELECT * FROM user_details;'
    var pgStatements = TSParser.parse(postgreSQLQueriesBasic, 'pg', ';');
    pgStatements.forEach(statement => {
            console.log(statement + '\n-----------');
    });

This will return an array with 2 items, items are;

SELECT * FROM users

SELECT * FROM user_details

PostgreSQL Function and Regular Queries

   CREATE OR REPLACE FUNCTION increment(i integer) RETURNS integer AS $$
        BEGIN
                RETURN i + 1;
        END;
$$ LANGUAGE plpgsql;
SELECT * FROM users;
SELECT * FROM user_details;

It will return an array with 3 items, items are;

  CREATE OR REPLACE FUNCTION increment(i integer) RETURNS integer AS $$
        BEGIN
                RETURN i + 1;
        END;
$$ LANGUAGE plpgsql

SELECT * FROM users

SELECT * FROM user_details

Microsoft SQL Server Example

In Microsoft SQL Server, GO keyword is default delimiter.

Unlike MySQL and PostgreSQL, if you don’t use a delimiter ( which is ‘GO’ ), MSSQL will execute it as a multi-statement query. MySQL and PostgreSQL will throw syntax exception.

    var postgreSQLQueriesBasic : string = ‘SELECT * FROM users GO SELECT * FROM user_details;’
    var pgStatements = TSParser.parse(postgreSQLQueriesBasic, ‘pg’, ‘;’);
    pgStatements.forEach(statement => {
            console.log(statement + ‘\n—————‘);
    });

This will return an array with 2 items, items are;

SELECT * FROM users

SELECT * FROM user_details

Microsoft SQL Server Stored Procedure and Regular Queries

  CREATE PROCEDURE dbo.uspGetAddress @City nvarchar(30)
AS
SELECT * 
FROM Person.Address
WHERE City = @City;
GO
SELECT * FROM users;
GO
SELECT * FROM user_details;

It will return an array with 3 items, items are;

CREATE PROCEDURE dbo.uspGetAddress @City nvarchar(30)
AS
SELECT * 
FROM Person.Address
WHERE City = @City;

SELECT * FROM users

SELECT * FROM user_details