Skip to content

Generates SQL when given an AST from the Codeschool/sqlite-parser

License

Notifications You must be signed in to change notification settings

jdrew1303/sqlgenerate

Repository files navigation

Overview | Features | Installation | Credits | Issues


Overview

SQLGenerate takes the Codeschool sqlite-parser AST and generates SQL from it. This allows you to read code (using the Codeschool parser), manipulate the AST (using SQLTraverse) and then generate SQL (using this library 😄).

⬆️

Features

⬆️

Installation

npm install sqlgenerator 👍 done!

⬆️

Using this project

The blow example is a simple case of parsing SQL to an ast and then back again to SQL. One of the better use case for this is to modify your SQL somehow. To do this you can use sqltraverse (yes it's a shameless plug 😁).

import parser       from 'sqlite-parser';
import { generate } from 'sqlgenerate';
import { format }   from "sql-formatter";

const example = `
  SELECT m.title, r.id AS [Theatre Number]
  FROM Movies AS m
  INNER JOIN (
    SELECT r2.movie_id
    FROM Rooms AS r2
    WHERE (r2.seats >= 50)
  ) AS r
  ON ((m.id = r.movie_id) AND (m.title != 'Batman'));`

const ast = parser(sql);
const regeneratedSQL = generate(ast);

console.log(format(regeneratedSQL));

⬆️

Credits

jdrew1303

Copyright (c) 2016

Issues

  • Tests, tests and more tests.
  • Documentation currently under construction (The examples need to be worked through for SQL instead of JavaScript).
  • AST is currently in a state of flux for some node types. We also probably need a builder for these nodes (another project).
  • You need to pass in ast.statement and not just the raw ast given back from the parser (This is an issue with the base node not having a type).
  • Adding support for comments.
  • Adding support for source-maps.

⬆️

About

Generates SQL when given an AST from the Codeschool/sqlite-parser

Resources

License

Stars

Watchers

Forks

Packages

No packages published