Skip to content

Commit 0909c6a

Browse files
authored
Documentation & Result Move Constructor (hyrise#39)
Updates documentation, adds a move constructor to SQLParserResult, fixes compile-time warnings
1 parent 9184d5d commit 0909c6a

27 files changed

Lines changed: 254 additions & 159 deletions

Makefile

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@ SRC = src
44
SRCPARSER = src/parser
55

66
# files
7-
PARSERFILES = $(SRCPARSER)/bison_parser.cpp $(SRCPARSER)/flex_lexer.cpp
8-
LIBCPP = $(shell find $(SRC) -name '*.cpp' -not -path "$(SRCPARSER)/*") $(SRCPARSER)/bison_parser.cpp $(SRCPARSER)/flex_lexer.cpp
7+
PARSERCPP = $(SRCPARSER)/bison_parser.cpp $(SRCPARSER)/flex_lexer.cpp
8+
LIBCPP = $(shell find $(SRC) -name '*.cpp' -not -path "$(SRCPARSER)/*") $(PARSERCPP)
99
LIBOBJ = $(LIBCPP:%.cpp=%.o)
1010
TESTCPP = $(shell find test/ -name '*.cpp')
1111

@@ -27,13 +27,16 @@ library: $(TARGET)
2727
$(TARGET): $(LIBOBJ)
2828
$(CXX) $(LIBFLAGS) -o $(TARGET) $(LIBOBJ)
2929

30-
%.o: %.cpp $(PARSERFILES)
30+
$(SRCPARSER)/flex_lexer.o: $(SRCPARSER)/flex_lexer.cpp
31+
$(CXX) $(CFLAGS) -c -o $@ $< -Wno-sign-compare -Wno-unneeded-internal-declaration -Wno-deprecated-register
32+
33+
%.o: %.cpp $(PARSERCPP)
3134
$(CXX) $(CFLAGS) -c -o $@ $<
3235

33-
$(SRCPARSER)/bison_parser.cpp:
36+
$(SRCPARSER)/bison_parser.cpp: $(SRCPARSER)/bison_parser.y
3437
make -C $(SRCPARSER)/ bison_parser.cpp
3538

36-
$(SRCPARSER)/flex_lexer.cpp:
39+
$(SRCPARSER)/flex_lexer.cpp: $(SRCPARSER)/flex_lexer.l
3740
make -C $(SRCPARSER)/ flex_lexer.cpp
3841

3942
parser:
@@ -51,6 +54,7 @@ cleanall: clean cleanparser
5154

5255
install:
5356
cp $(TARGET) $(INSTALL)/lib/$(TARGET)
57+
rm -rf $(INSTALL)/include/hsql
5458
cp -r src $(INSTALL)/include/hsql
5559
find $(INSTALL)/include/hsql -not -name '*.h' -type f | xargs rm
5660

README.md

Lines changed: 37 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
1-
C++ SQL Parser for Hyrise
1+
C++ SQL Parser
22
=========================
33
[![GitHub release](https://img.shields.io/github/release/hyrise/sql-parser.svg?maxAge=2592000)]()
44
[![Build Status](https://img.shields.io/travis/hyrise/sql-parser.svg?maxAge=2592000)](https://travis-ci.org/hyrise/sql-parser)
55

66

77
This is a SQL Parser for C++. It parses the given SQL query into C++ objects.
8-
It is developed for integration in hyrise (https://github.com/hyrise/hyrise), but can be used in other environments as well.
8+
It has been developed for integration in [Hyrise](https://github.com/hyrise/hyrise), but can be used perfectly well in other environments as well.
99

1010
In March 2015 we've also written a short paper outlining discussing some development details and the integration into our database Hyrise. You can find the paper [here](http://torpedro.com/paper/HyriseSQL-03-2015.pdf).
1111

1212

13-
### Usage
13+
## Usage
1414

1515
**Note:** You can also find a detailed usage description at this [blog post](http://torpedro.github.io/tech/c++/sql/parser/2016/02/27/c++-sql-parser.html).
1616

@@ -21,52 +21,53 @@ To use the SQL parser in your own projects you simply have to follow these few s
2121

2222
1. Download the [latest release here](https://github.com/hyrise/sql-parser/releases)
2323
2. Compile the library `make` to create `libsqlparser.so`
24-
3. *(Optional)* Run `make install` to copy the library to `/usr/local/lib/`
25-
3. Run the tests `make test` to make sure everything worked
26-
4. Take a look at the [example project here](https://github.com/hyrise/sql-parser/tree/master/example)
27-
5. Include the `SQLParser.h` from `src/` and link the library in your project
28-
29-
30-
### Extending the parser
31-
32-
**Requirements for development:**
33-
* gcc 4.8+ (or clang 3.4+)
34-
* [bison](https://www.gnu.org/software/bison/) (v3.0.2+)
35-
* [flex](http://flex.sourceforge.net/) (v2.5.5+)
36-
37-
First step to extending this parser is cloning the repository `git clone [email protected]:hyrise/sql-parser.git` and making sure everything works by running the following steps:
38-
39-
```bash
40-
make parser # builds the bison parser and flex lexer
41-
make library # builds the libsqlparser.so
42-
make test # runs the tests with the library
24+
3. *(Optional, Recommended)* Run `make install` to copy the library to `/usr/local/lib/`
25+
4. Run the tests `make test` to make sure everything worked
26+
5. Include the `SQLParser.h` from `src/` (or from `/usr/local/lib/hsql/` if you installed it) and link the library in your project
27+
6. Take a look at the [example project here](https://github.com/hyrise/sql-parser/tree/master/example)
28+
29+
```cpp
30+
#include "hsql/SQLParser.h"
31+
32+
/* ... */
33+
34+
{
35+
// Basic Usage Example
36+
37+
const std::string query = "...";
38+
hsql::SQLParserResult result;
39+
hsql::SQLParser::parseSQLString(query, &result);
40+
41+
if (result.isValid() && result.size() > 0) {
42+
const hsql::SQLStatement* statement = result.getStatement(0);
43+
44+
if (statement.isType(hsql::SelectStatement)) {
45+
const hsql::SelectStatement* select = (const hsql::SelectStatement*) statement;
46+
/* ... */
47+
}
48+
}
49+
}
4350
```
4451

45-
Rerun these steps whenever you change part of the parse. To execute the entire pipeline automatically you can run:
52+
Quick Links:
4653

47-
```bash
48-
make cleanall # cleans the parser build and library build
49-
make test # build parser, library and runs the tests
50-
```
54+
* [SQLParser.h](src/SQLParser.h)
55+
* [SQLParserResult.h](src/SQLParserResult.h)
56+
* [SelectStatement.h](src/sql/SelectStatement.h)
5157

58+
## How to Contribute
5259

53-
#### How to contribute
60+
**[Developer Documentation](docs/)**
5461

5562
We strongly encourage you to contribute to this project! If you want to contribute to this project there are several options. If you've noticed a bug or would like an improvement let us know by creating a [new issue](https://github.com/hyrise/sql-parser/issues). If you want to develop a new feature yourself or just improve the quality of the system, feel free to fork the reposistory and implement your changes. Open a pull request as soon as your done and we will look over it. If we think it's good then your pull request will be merged into this repository.
5663

5764

58-
### Resources
59-
60-
* [Working Syntax Examples](docs/syntax.md)
61-
* [Developer Documentation](docs/dev-docs.md)
62-
63-
64-
### License
65+
## License
6566

6667
HYRISE sql-parser is licensed as open source after the OpenSource "Licence of the Hasso-Plattner Institute" declared in the LICENSE file of this project.
6768

6869

69-
### Contributers
70+
## Contributers
7071

7172
The following people contributed to HYRISE sql-parser in various forms.
7273

docs/README.md

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
Documentation
2+
=============
3+
4+
Internal Links:
5+
6+
* [Developer Documentation](dev-docs.md)
7+
* [Working SQL Syntax Examples](syntax-examples.md)
8+
9+
10+
External Resources:
11+
12+
* [Original Dev-Paper (2015)](http://torpedro.com/paper/HyriseSQL-03-2015.pdf)
13+
* [Blog Post about Basic Usage](http://torpedro.github.io/tech/c++/sql/parser/2016/02/27/c++-sql-parser.html)
14+
15+

docs/dev-docs.md

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,29 @@
11
Developer Documentation
22
=======================
33

4+
## Basic Requirements
5+
6+
**Requirements for development:**
7+
* gcc 4.8+ (or clang 3.4+)
8+
* [bison](https://www.gnu.org/software/bison/) (v3.0.2+)
9+
* [flex](http://flex.sourceforge.net/) (v2.5.5+)
10+
11+
First step to extending this parser is cloning the repository `git clone [email protected]:hyrise/sql-parser.git` and making sure everything works by running the following steps:
12+
13+
```bash
14+
make parser # builds the bison parser and flex lexer
15+
make library # builds the libsqlparser.so
16+
make test # runs the tests with the library
17+
```
18+
19+
Rerun these steps whenever you change part of the parse. To execute the entire pipeline automatically you can run:
20+
21+
```bash
22+
make cleanall # cleans the parser build and library build
23+
make test # build parser, library and runs the tests
24+
```
25+
26+
427
## Developing New Functionality
528

629
This section contains information about how to extend this parser with new functionalities.
@@ -18,7 +41,7 @@ Finally you will need to include your new file in `src/sql/statements.h`.
1841
### Extending the Grammar
1942

2043
Related files:
21-
````
44+
```
2245
src/parser/bison_parser.y
2346
src/parser/flex_lexer.l
2447
src/parser/keywordlist_generator.py
File renamed without changes.

src/SQLParserResult.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,16 @@ namespace hsql {
1313
addStatement(stmt);
1414
};
1515

16+
// Move constructor.
17+
SQLParserResult::SQLParserResult(SQLParserResult&& moved) {
18+
isValid_ = moved.isValid_;
19+
errorMsg_ = moved.errorMsg_;
20+
statements_ = std::move(moved.statements_);
21+
22+
moved.errorMsg_ = NULL;
23+
moved.reset();
24+
}
25+
1626
SQLParserResult::~SQLParserResult() {
1727
reset();
1828
}

src/SQLParserResult.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@ namespace hsql {
1515
// Takes ownership of the statement.
1616
SQLParserResult(SQLStatement* stmt);
1717

18+
// Move constructor.
19+
SQLParserResult(SQLParserResult&& moved);
20+
1821
// Deletes all statements in the result.
1922
virtual ~SQLParserResult();
2023

src/parser/.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
1-
*.output
1+
*.output
2+
conflict_test.cpp

src/parser/Makefile

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ all: bison_parser.cpp flex_lexer.cpp
33

44
bison_parser.cpp: bison_parser.y
55
@bison --version | head -n 1
6-
bison bison_parser.y -v
6+
bison bison_parser.y --output=bison_parser.cpp --defines=bison_parser.h --verbose
77

88
flex_lexer.cpp: flex_lexer.l
99
@flex --version
@@ -14,4 +14,4 @@ clean:
1414

1515
# Tests if the parser builds correctly and doesn't contain conflicts.
1616
test:
17-
! bison bison_parser.y -v 2>&1 | grep "conflict" >&2
17+
! bison bison_parser.y -v --output=conflict_test.cpp 2>&1 | grep "conflict" >&2

src/parser/bison_parser.cpp

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2558,7 +2558,7 @@ YYLTYPE yylloc = yyloc_default;
25582558
case 20:
25592559
#line 315 "bison_parser.y" /* yacc.c:1646 */
25602560
{
2561-
(yyval.import_stmt) = new ImportStatement((ImportStatement::ImportType) (yyvsp[-4].uval));
2561+
(yyval.import_stmt) = new ImportStatement((ImportType) (yyvsp[-4].uval));
25622562
(yyval.import_stmt)->filePath = (yyvsp[-2].sval);
25632563
(yyval.import_stmt)->tableName = (yyvsp[0].sval);
25642564
}
@@ -2567,7 +2567,7 @@ YYLTYPE yylloc = yyloc_default;
25672567

25682568
case 21:
25692569
#line 323 "bison_parser.y" /* yacc.c:1646 */
2570-
{ (yyval.uval) = ImportStatement::kImportCSV; }
2570+
{ (yyval.uval) = kImportCSV; }
25712571
#line 2572 "bison_parser.cpp" /* yacc.c:1646 */
25722572
break;
25732573

@@ -2580,7 +2580,7 @@ YYLTYPE yylloc = yyloc_default;
25802580
case 23:
25812581
#line 337 "bison_parser.y" /* yacc.c:1646 */
25822582
{
2583-
(yyval.create_stmt) = new CreateStatement(CreateStatement::kTableFromTbl);
2583+
(yyval.create_stmt) = new CreateStatement(kCreateTableFromTbl);
25842584
(yyval.create_stmt)->ifNotExists = (yyvsp[-5].bval);
25852585
(yyval.create_stmt)->tableName = (yyvsp[-4].sval);
25862586
(yyval.create_stmt)->filePath = (yyvsp[0].sval);
@@ -2591,7 +2591,7 @@ YYLTYPE yylloc = yyloc_default;
25912591
case 24:
25922592
#line 343 "bison_parser.y" /* yacc.c:1646 */
25932593
{
2594-
(yyval.create_stmt) = new CreateStatement(CreateStatement::kTable);
2594+
(yyval.create_stmt) = new CreateStatement(kCreateTable);
25952595
(yyval.create_stmt)->ifNotExists = (yyvsp[-4].bval);
25962596
(yyval.create_stmt)->tableName = (yyvsp[-3].sval);
25972597
(yyval.create_stmt)->columns = (yyvsp[-1].column_vec);
@@ -2602,7 +2602,7 @@ YYLTYPE yylloc = yyloc_default;
26022602
case 25:
26032603
#line 349 "bison_parser.y" /* yacc.c:1646 */
26042604
{
2605-
(yyval.create_stmt) = new CreateStatement(CreateStatement::kView);
2605+
(yyval.create_stmt) = new CreateStatement(kCreateView);
26062606
(yyval.create_stmt)->ifNotExists = (yyvsp[-4].bval);
26072607
(yyval.create_stmt)->tableName = (yyvsp[-3].sval);
26082608
(yyval.create_stmt)->viewColumns = (yyvsp[-2].str_vec);
@@ -2670,7 +2670,7 @@ YYLTYPE yylloc = yyloc_default;
26702670
case 35:
26712671
#line 389 "bison_parser.y" /* yacc.c:1646 */
26722672
{
2673-
(yyval.drop_stmt) = new DropStatement(DropStatement::kTable);
2673+
(yyval.drop_stmt) = new DropStatement(kDropTable);
26742674
(yyval.drop_stmt)->name = (yyvsp[0].sval);
26752675
}
26762676
#line 2677 "bison_parser.cpp" /* yacc.c:1646 */
@@ -2679,7 +2679,7 @@ YYLTYPE yylloc = yyloc_default;
26792679
case 36:
26802680
#line 393 "bison_parser.y" /* yacc.c:1646 */
26812681
{
2682-
(yyval.drop_stmt) = new DropStatement(DropStatement::kView);
2682+
(yyval.drop_stmt) = new DropStatement(kDropView);
26832683
(yyval.drop_stmt)->name = (yyvsp[0].sval);
26842684
}
26852685
#line 2686 "bison_parser.cpp" /* yacc.c:1646 */
@@ -2688,7 +2688,7 @@ YYLTYPE yylloc = yyloc_default;
26882688
case 37:
26892689
#line 397 "bison_parser.y" /* yacc.c:1646 */
26902690
{
2691-
(yyval.drop_stmt) = new DropStatement(DropStatement::kPreparedStatement);
2691+
(yyval.drop_stmt) = new DropStatement(kDropPreparedStatement);
26922692
(yyval.drop_stmt)->name = (yyvsp[0].sval);
26932693
}
26942694
#line 2695 "bison_parser.cpp" /* yacc.c:1646 */
@@ -2716,7 +2716,7 @@ YYLTYPE yylloc = yyloc_default;
27162716
case 40:
27172717
#line 429 "bison_parser.y" /* yacc.c:1646 */
27182718
{
2719-
(yyval.insert_stmt) = new InsertStatement(InsertStatement::kInsertValues);
2719+
(yyval.insert_stmt) = new InsertStatement(kInsertValues);
27202720
(yyval.insert_stmt)->tableName = (yyvsp[-5].sval);
27212721
(yyval.insert_stmt)->columns = (yyvsp[-4].str_vec);
27222722
(yyval.insert_stmt)->values = (yyvsp[-1].expr_vec);
@@ -2727,7 +2727,7 @@ YYLTYPE yylloc = yyloc_default;
27272727
case 41:
27282728
#line 435 "bison_parser.y" /* yacc.c:1646 */
27292729
{
2730-
(yyval.insert_stmt) = new InsertStatement(InsertStatement::kInsertSelect);
2730+
(yyval.insert_stmt) = new InsertStatement(kInsertSelect);
27312731
(yyval.insert_stmt)->tableName = (yyvsp[-2].sval);
27322732
(yyval.insert_stmt)->columns = (yyvsp[-1].str_vec);
27332733
(yyval.insert_stmt)->select = (yyvsp[0].select_stmt);

0 commit comments

Comments
 (0)