• This repository has been archived on 15/Jul/2021
  • Stars
    star
    330
  • Rank 127,657 (Top 3 %)
  • Language PLpgSQL
  • License
    MIT License
  • Created over 9 years ago
  • Updated over 5 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

JavaScript implentation of SQLite 3 query parser

sqlite-parser

NPM Version Image dependencies Status Image devDependencies Status Image License Type Image

This JavaScript library parses SQLite queries to generate abstract syntax tree (AST) representations of the parsed statements.

Try out the interactive demo to see it in action.

This parser is written against the SQLite 3 spec.

Install

npm install sqlite-parser

Install as a global module (since v1.0.0)

Use the command-line interface of the parser by installing it as a global module. The sqlite-parser command is then available to use to parse input SQL files and write the results to stdout or a JSON file. Additional usage instructions and options available through sqlite-parser --help.

npm i -g sqlite-parser

Basic Usage

The library exposes a function that accepts two arguments: a string containing SQL to parse and a callback function. If an AST cannot be generated from the input string then a descriptive error is generated.

If invoked without a callback function the parser will runs synchronously and return the resulting AST or throw an error if one occurs.

var sqliteParser = require('sqlite-parser');
var query = 'select pants from laundry;';
// sync
var ast = sqliteParser(query);
console.log(ast);

// async
sqliteParser(query, function (err, ast) {
  if (err) {
    console.error(err);
    return;
  }
  console.log(ast);
});

Use parser on Node streams (experimental) (since v1.0.0)

This library also includes experimental support as a stream transform that can accept a readable stream of SQL statements and produce a JSON string, representing the AST of each statement, as it is read and transformed. Using this method, the parser can handle files containing hundreds or thousands of queries at once without running into memory limitations. The AST for each statement is pushed down the stream as soon as it is read and parsed instead of reading the entire file into memory before parsing begins.

var parserTransform = require('sqlite-parser').createParser();
var readStream = require('fs').createReadStream('./large-input-file.sql');

readStream.pipe(parserTransform);
parserTransform.pipe(process.stdout);

parserTransform.on('error', function (err) {
  console.error(err);
  process.exit(1);
});

parserTransform.on('finish', function () {
  process.exit(0);
});

To pipe the output into a file that contains a single valid JSON structure, the output of the parser steam transform needs to be wrapped in statement list node where every statement is separated by a comma.

var fs = require('fs');
var sqliteParser = require('sqlite-parser');
var parserTransform = sqliteParser.createParser();
var singleNodeTransform = sqliteParser.createStitcher();
var readStream = fs.createReadStream('./large-input-file.sql');
var writeStream = fs.createWriteStream('./large-output-file.json');

readStream.pipe(parserTransform);
parserTransform.pipe(singleNodeTransform);
singleNodeTransform.pipe(writeStream);

parserTransform.on('error', function (err) {
  console.error(err);
  process.exit(1);
});

writeStream.on('finish', function () {
  process.exit(0);
});

AST

The AST is stable as of release 1.0.0. However, if changes need to be made to improve consistency between node types, they will be explicitly listed in the CHANGELOG.

Example

You can provide one or more SQL statements at a time. The resulting AST object has, at the highest level, a statement list node that contains an array of statements.

Input SQL

SELECT
 MAX(honey) AS "Max Honey"
FROM
 BeeHive

Result AST

{
  "type": "statement",
  "variant": "list",
  "statement": [
    {
      "type": "statement",
      "variant": "select",
      "result": [
        {
          "type": "function",
          "name": {
            "type": "identifier",
            "variant": "function",
            "name": "max"
          },
          "args": {
            "type": "expression",
            "variant": "list",
            "expression": [
              {
                "type": "identifier",
                "variant": "column",
                "name": "honey"
              }
            ]
          },
          "alias": "Max Honey"
        }
      ],
      "from": {
        "type": "identifier",
        "variant": "table",
        "name": "beehive"
      }
    }
  ]
}

Syntax Errors

This parser will try to create descriptive error messages when it cannot parse some input SQL. In addition to an approximate location for the syntax error, the parser will attempt to describe the area of concern (e.g.: Syntax error found near Column Identifier (WHERE Clause)).

Contributing

Contributions are welcome! You can get started by checking out the contributing guidelines.

More Repositories

1

NoteWrangler

CSS
144
star
2

RFZ2-ZombieTweets

A great deal of the code seen in Rails for Zombies 2
Ruby
127
star
3

WatchUsBuild-React

Code sample for the Watch Us Build episode that focused on React
CSS
120
star
4

EmberCourseDemo

Companion code for Code Schools Warming Up With Ember.js Course
JavaScript
106
star
5

WatchUsBuild-NoteTakingAppWithAngularJS

JavaScript
102
star
6

SurvivingAPIsDemoApp

Example app for the Surviving APIs with Rails course.
Ruby
94
star
7

ShapingUpWithAngular.js

The companion code for Code School and Google's Shaping Up With Angular.js course.
HTML
91
star
8

WatchUsBuild-ReadingListAppWithAngularJS

HTML
89
star
9

FeatureFocus

Demo app for the Feature Focus series.
Ruby
85
star
10

babel-with-gulp

Starter code for using Babel with Gulp and Browserify
JavaScript
75
star
11

WatchUsBuild-ReadingListAppWithRails

Ruby
63
star
12

WatchUsBuild-Angular2Final

Angular 2 sample app
TypeScript
63
star
13

WatchUsBuild-ExpressAppWithTDD

JavaScript
60
star
14

BlastingOffWithBootstrapDemo

Example content associated with Code Schools Blasting Off With Bootstrap course.
CSS
59
star
15

jQueryReturnFlightDemos

Demos used in the videos for jQuery: The Return Flight at http://jquery-part2.codeschool.com/
CSS
52
star
16

WatchUsBuild-RailsWebAPI

Ruby
50
star
17

UIImage-ImageEffects

Objective-C
43
star
18

WatchUsBuild-ForumAppWithES2015

JavaScript
40
star
19

DiggingIntoDjango-TreasuregramSampleApp

The Treasuregram Sample App created in the Digging into Django course.
Python
38
star
20

WatchUsBuild-SimpleNodeAppWithDocker

Watch Us Build code for the Simple Node App With Docker screencast.
JavaScript
38
star
21

WatchUsBuild-PortfolioWithHTMLAndCSS

HTML
38
star
22

OlympicsMEAN

Web app created on the MEAN Stack series
JavaScript
35
star
23

WatchUsBuild-ResponsiveSiteWithBootstrap

HTML
33
star
24

angular-demo-app

JavaScript
29
star
25

WatchUsBuild-ImageStreamingAppWithNodeWebServer

JavaScript
29
star
26

EmberReadinglist

Sample Ember Application
JavaScript
29
star
27

FrontEndFormationsBlog

The example blog used in the Front-end Formations course.
CSS
27
star
28

AngularReadinglist

26
star
29

WatchUsBuild-ImageStreamingAppWithNodePubSubServer

JavaScript
26
star
30

WatchUsBuild-FlexboxReactNativeUI

This repository contains the sample code for the Cracking the Case with Flexbox Watch Us Build a React Native UI in Flexbox
JavaScript
26
star
31

WatchUsBuild-StoreiOSApp

Swift
24
star
32

NoteApp

The code for the iOS note application we'll be building with Swift during the Code School Live event on 11/21/2014
Swift
24
star
33

TryDjango-TreasuregramSampleApp

The Treasuregram App featured in the Try Django course.
Python
23
star
34

WatchUsBuild-TeamDirectoryAppWithDjango

Code for the Directory app built during the Watch Us Build - Team Directory App with Django screencast.
Python
23
star
35

WatchUsBuild-QuizAppWithEmber

CSS
23
star
36

MyChatGolang

Demo chat application created on the Watch Us Build episode for On Track With Golang
Go
23
star
37

WatchUsBuild-ConvertSQLAppToMongoDB

JavaScript
21
star
38

CodeEditorDirectiveExample

CSS
21
star
39

budget-elixir

Elixir app built on "Watch Us Build"
Elixir
21
star
40

Practice-CrackingTheCaseWithFlexbox

This is some starter HTML and CSS to practice building UIs with flexbox.
CSS
21
star
41

WatchUsBuild-QuizAppWithCSSAnimations

CSS
20
star
42

javascript-sandbox

Sandbox for running user entered code, either from tests or to get the result.
JavaScript
20
star
43

TryAndroid-CandyCodedSampleApp

The sample app for the Try Android course, a candy store app called Candy Coded.
Java
20
star
44

TweetValidator

endpoint that validates tweets - to be used in the Staying Sharp With AngularJS course
JavaScript
18
star
45

compliments

A simple way to say nice things to your teammates
Ruby
17
star
46

TryDotNetMvc-CourseSampleCode

Sample code for the app created in the Try .NET MVC course
C#
16
star
47

WatchUsBuild-Angular2App

TypeScript
16
star
48

badges-socket-client

Demo WebSocket client application
JavaScript
16
star
49

WatchUsBuild-MovieListingsAppWithSQL

JavaScript
16
star
50

FrontEndNewsScreencast

This is the code for the FrontEndNews site built during some screencasts.
HTML
14
star
51

codetv-wandrr

The sample app from the Rails App From Scratch Code TV Episode (Part 1)
Ruby
14
star
52

ExpressCourseDemo

Companion code for Code School's Express course
JavaScript
12
star
53

WatchUsBuild-DataValidationsWithRegEx

Ruby
12
star
54

WatchUsBuild-StoreiOSAppWithSwift3

This repository contains the sample code for the Watch Us Build A Store iOS App with Swift 3.
Swift
11
star
55

WatchUsBuild-WebpageWithSVG

HTML
10
star
56

WatchUsBuild-ReviewingGamesInDotNet

C#
10
star
57

TryiOSCourse-demos

Sample projects from Code Schools Try iOS course http://tryios.codeschool.com/
Objective-C
10
star
58

WatchUsBuild-CloseEncountersWithPHP

WatchUsBuild
PHP
10
star
59

SuperSweetAndroidTime-CandyCodedSampleApp

The sample app for the Super Sweet Android Time course, a candy store app called Candy Coded. Continues from where the Try Android course's sample app left off - https://github.com/codeschool/TryAndroid-CandyCodedSampleApp.
Java
10
star
60

TryEmberQuizApp

Quiz app created in the Try Ember course
CSS
10
star
61

HelloCodeSchoolProject

Demonstration on how projects work at Code School.
JavaScript
9
star
62

TryCSharpSampleApp

C#
7
star
63

HTMLPortfolioProject

Code School Project: HTML + CSS Portfolio
JavaScript
7
star
64

BootstrapPortfolioProject

Code School Project: Portfolio created with Twitter Bootstrap
JavaScript
6
star
65

KeepingItClassyWithCSharpSampleApp

C#
5
star
66

WatchUsBuild-DotNetForgingAhead-ScoreKeeper

C#
5
star
67

WatchUsBuild-BadgeViewerWithPHP

This repository contains the sample code for the Try PHP course and the Watch Us Build a Badge Viewer With PHP
CSS
5
star
68

ForgingAheadWithDotNetMvc-CourseSampleCode

Sample code for the Forging Ahead with .NET MVC course.
C#
5
star
69

git-real

4
star
70

LaravelWUB

JavaScript
4
star
71

TheGameOfLife

Ruby
4
star
72

WatchUsBuild-TodoListiOSApp

Objective-C
4
star
73

phoenix-fire-starter

Phoenix web app built for the Watch Us Build video series.
Elixir
4
star
74

esprimatch

JavaScript
3
star
75

WatchUsBuild-CSharpSinger

Create a Karaoke application using what you've learned in Try C# and Keeping it Classy with C#.
C#
3
star
76

SemanticHTMLPortfolioProject

Add semantic HTML elements to your portfolio.
JavaScript
2
star
77

TryEmberStrawPoll

Straw poll app built in the Try Ember course videos
JavaScript
2
star
78

oa-codeschool

OmniAuth 0.2.x strategy for Code School
Ruby
2
star
79

jQueryBadgesProject

jQuery Badges: Display the courses you've taken using AJAX
JavaScript
2
star
80

esprima-jquery-map

Conversion of an Esprima tree into a DOM like structure for easy searching.
JavaScript
2
star
81

WatchUsBuild-AndroidMemeGeneratorApp

The source code for the Watch Us Build video where we build an Android app for generating Memes from scratch.
Java
2
star
82

WebAnimationsQuiz

What Sweet Lands character are you?
CSS
1
star
83

cs-tslint-rules

The TSLint rules for Code School's TypeScript conventions.
1
star
84

rapporteur-load_check

Machine load check for Rapporteur
Ruby
1
star
85

mixing-it-up-with-elixir-budget

This is the application built on the Mixing It Up With Elixir course.
Elixir
1
star
86

Swift2To3Refactor-XkcdViewer

This repo contains for the Swift 2.2 -> 3.0 refactor that we did during a Facebook live event.
Swift
1
star