# sql-parser

> Lexer and Parser for SQL Syntax

Latest version **0.5.0** (published 2015-03-17) · 0 weekly downloads

## Install

```sh
npm install sql-parser
pnpm add sql-parser
yarn add sql-parser
bun add sql-parser
```

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support; pre 1.0.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.5.0 |
| Published | 2015-03-17 |
| First published | 2011-11-23 |
| Weekly downloads | 0 |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 0 |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 540 |
| Author | Andy Kent |
| Maintainers | andykent |

## Links

- npm: https://www.npmjs.com/package/sql-parser
- Repository: http://github.com/forward/sql-parser
- Homepage: https://github.com/forward/sql-parser
- Issues: https://github.com/forward/sql-parser/issues
- npm.io page: https://npm.io/package/sql-parser

## Recent versions

- 0.5.0 (latest) — 2015-03-17
- 0.4.0 — 2011-12-24
- 0.1.1 — 2011-12-07
- 0.1.2 — 2011-12-07
- 0.1.3 — 2011-12-07
- 0.1.4 — 2011-12-07
- 0.1.5 — 2011-12-07
- 0.1.6 — 2011-12-07
- 0.1.7 — 2011-12-07
- 0.1.8 — 2011-12-07
- 0.3.0 — 2011-12-07
- 0.2.1 — 2011-11-29
- 0.2.0 — 2011-11-29
- 0.1.9 — 2011-11-23

## README

SQL Parser
==========

SQL Parser is a lexer, grammar and parser for SQL written in JS. Currently it is only capable of parsing fairly basic SELECT queries but full SQL support will hopefully come in time. See the specs for examples of currently supported queries.

Lexer
-----

The lexer takes a SQL query string as input and returns a stream of tokens in the format 

    ['NAME', 'value', lineNumber]

Here is a simple example...

    lexer.tokenize('select * from my_table')

    [
      ['SELECT','select',1], 
      ['STAR','*',1], 
      ['FROM','from',1], 
      ['LITERAL','my_table',1]
    ]

The tokenized output is in a format compatible with JISON.


Parser
------

The parser only currently supports SELECT queries but is able to produce a Select object with properties for where, group, order, limit. See lib/nodes.coffee for more info of the returned object structure. Calling .toString() on a Select object should give you back a well formatted version of the original SQL input.

    tokens = lexer.tokenize('select * from my_table where foo = 'bar')
    parser.parse(tokens).toString()

    SELECT *
      FROM `my_table`
      WHERE `foo` = 'bar'
    


Credits
-------

A lot of the boilerplate and compilation code in this project is borrowed from the CoffeeScript project as it was the best example of a project using JISON that I could find. Thanks.

---
_Source: https://npm.io/package/sql-parser · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
