Skip to content

jrebagliatti/ra-to-sql

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

74 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Really Simple (and quite inefficient) Relational Algebra parser and SQL converter

Very simple Relational Algebra to SQL converter. Designed for educational purposes only.

The tool also provides a simple Relational Algebra to LaTeX mathematical expression.

Usage

ra-to-sql can be installed as a Node.js package or as a JavaScript library (see Installation section for more detailed information). The ra-to-sql.js library can be downloaded from here, or used from a CDN https://unpkg.com/ra-to-sql.

As a JavaScript library:

var sql = raToSql.getSQL(<<ra-expression>>);
var latex = raToSql.getLatexExpression(<<ra-expression>>);

As a Node.js app:

var raToSql = require('ra-to-sql');
var sql = raToSql.getSQL(<<ra-expression>>);
var latex = raToSql.getLatexExpression(<<ra-expression>>);

Where <<ra-expression>> is any valid Relational Algebra expression.

Currently, the following operations are supported

  • Rename: Ren[a,b](A), ρ[a,b](A)
  • Projection: Proj[a,b](A), π[a,b](A)
  • Selection: Sel[Condition](A), σ[Condition](A)
  • Cartesian product: A x B, A × B
  • Natural Join A |x| B, A ⋈ B
  • Theta Join A |x(Condition)| B
  • Union: A U B, A ∪ B
  • Intersection: A INT B, A ∩ B
  • Set Difference: A - B
  • Asignment: A <- B
  • Asignment with Rename: A(a,b) <- B

The structure of the language is quite simple, here are a few rules:

  • Operations can be nested
  • There are two types of sentences:
    • Assignments
    • Result (any sentence without an assignment)
  • Parser ends evaluating when a "Result" sentence is found. If no Result sentence is found, the last assignment is considered the Result sentence
  • Semicolon (;) and New Line are both valid as sentence separator

The following are examples of valid set of sentences:

A(a,b) <- Proj[PersonId,PersonName](Person)
A x A x A
OldPeople <- Sel[Age>80](Person); Person |X| City;
Sel[Number > 14 AND !(Number = 15 OR Number = 18)](Numbers)

You can try it online here:

Note: The SQL generated is compatible with SQL Server and sqlite.

Installation

CDN

<script src="https://unpkg.com/ra-to-sql@<version>/dist/ra-to-sql.js"></script>

Replace <version> with expected version number. For example https://unpkg.com/ra-to-sql@0.0.2/dist/ra-to-sql.js

Node.js

npm install ra-to-sql

For contrinutors

Pre-requisites

  • Node.js

Setup

Fork/Clone the repository (https://github.com/jrebagliatti/ra-to-sql)

Install the dependencies:

nmp install

Update the code

The following are the main components of the library:

Additionally, the following files are generated as part of the building process:

Running the tests

Tests are defined in the test/test-definitions folder. Each Json file included in that folder becomes a test. File structure is self-explanatory.

Use the following command to run the tests:

npm test

Built With

Contributing

Any contribution is welcomed!. There are no guidelines for contributing, just common-sense.

Versioning

We use SemVer for versioning. For the versions available, see the tags on this repository.

Authors

See also the list of contributors who participated in this project.

License

This project is licensed under the MIT License.

About

Relational Algebra parser and SQL converter

Resources

Stars

Watchers

Forks

Packages

No packages published