Giter Site home page Giter Site logo

sql-query-parser's Introduction

Tiny SQL Parser

Build Status

Small ASCII only SQL parser for the SELECT statements intended to be used in query builders. The statement is decomposed into sections and placeholders are collected for each section. Only unnamed placeholders are supported which means that for every placeholder only it's offset within the section is recorded.

Currently this parser targets MySQL SQL grammar until version 8.0.

This library should parse any valid SELECT statement but it will also parse some invalid statements as valid. This is by design, since intention is to use this to parse already valid statements and this way grammar is simplified.

For more info about the library check include/tsqlp.h.

With the library comes standalone binary to decompose SELECT statements into sections called tsqlp.

For example, for the SQL

SELECT id, 2, ? + ? FROM t WHERE a = ?

tsqlp produces output

columns 2 7 11 12 id, 2, ? + ?
tables 0 1 t
where 1 4 5 a = ?

Statement contains three sections, columns, tables and where. Section column has 2 parameters at offsets 7 and 11 and is 12 bytes long. Then comes 12 bytes of the section content. Section tables contains 0 parameters and is 1 byte long. Content of the tables section is t. Finally, where section has 1 parameter at offset 4 and the content of that section is a = ? which is 5 bytes long.

Placeholders as table names

This parser supports placeholders as table name. That is something invalid in regular SQL but it's useful in query building.

For example, parser can parse SELECT * FROM ? and expose ? as a placeholder which can later have inline subquery or something else.

Installation

Clone this repository and within do the following.

cd build
cmake .. -DCMAKE_BUILD_TYPE=Release 
make
sudo make install

If you get tsqlp: error while loading shared libraries: libtsqlp.so: cannot open shared object file: No such file or directory make sure /usr/local/lib is used by the ldconfig or change install path.

sql-query-parser's People

Contributors

zlikavac32 avatar

Stargazers

 avatar  avatar

Watchers

 avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.