13

I want to parse and store the columns and values of a SQL DML (INSERT, UPDATE, DELETE) statement in C. Need the URL of the open source code or a library with which I can link my C program. The platform is SUSE Linux. Have tried to make and use libSQL unsuccessfully. A detailed answer is appreciated. Thanks.

Additional Notes: Please suggest a library/code that I can link with my C program. In my program I want to use the functions of this library to parse and use the tokens for further processing.

9 Answers9

11

You can have a look at the source code for SQLite. It uses a parser called Lemon.

Links:

SQLite architecture

Lemon parser

You can also look at the source code for postgresql-plpython3. Looks like it has a pure C based SQL parser.

Link:

postgresql-plpython3 @ github

epatel
  • 45,805
  • 17
  • 110
  • 144
5

I would suggest to start from the real parser of a real DBMS. There are several in free software. For instance, the parser of PostgreSQL is in the directory src/backend/parser of the distribution and is written in C and Yacc.

bortzmeyer
  • 34,164
  • 12
  • 67
  • 91
1

See the Chapter "Parsing SQL" in "Lex & Yacc"(O'Reilly) in google books http://books.google.fr/books?id=YrzpxNYegEkC&lpg=PT1&dq=bison%20flex%20sql%20grammar&client=firefox-a&hl=en&pg=PA109

Pierre
  • 34,472
  • 31
  • 113
  • 192
1

Have you looked at SQLite ? It certainly does have the code to parse SQL, so maybe you could avoid reimplementing it..

Andrew Y
  • 5,107
  • 2
  • 28
  • 29
1

ANTLR can target C, among other languages, and its catalog of premade grammars has a bunch of SQL dialects - notably MySQL and Oracle.

Pavel Minaev
  • 99,783
  • 25
  • 219
  • 289
1

µSQL for C++

What is µSQL ?

µSQL is a SQL parser engine for C++ to develop SQL based applications easily, and it supports other SQL like domain specific languages such as UnQL and GQL too. Because µSQL is written only in old standard C++ library such as STL with ANTLR, then you can use it with many C++ compilers and platforms.

Repo on Github

frogatto
  • 28,539
  • 11
  • 83
  • 129
1

The standalone SQL parser of the Hyrise database system is open source and, even though it is written in C++, it can be accessed from C and is easy to understand and modify. It utilizes bison and flex.

Benski
  • 63
  • 5
0

Not sure is there any mature C sql parser can do that easily.

Here is a Java version SQL library can do what you need exactly, it can be used to Retrieve/Refactor table & column name from a complex SQL query easily.

James Wang
  • 453
  • 4
  • 5
-3

Have you considered writing your own using lex and yacc? (the hacker - hardcore approach)

Not trivial .. but this site might help you get started

lexu
  • 8,766
  • 5
  • 45
  • 63