Merge pull request #81 from javrucebo/join-types

Adjust Join types
This commit is contained in:
mrks 2018-01-23 13:46:17 +01:00 committed by GitHub
commit ea694ad2b3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 62 additions and 7 deletions

View File

@ -993,11 +993,13 @@ join_clause:
opt_join_type: opt_join_type:
INNER { $$ = kJoinInner; } INNER { $$ = kJoinInner; }
| OUTER { $$ = kJoinOuter; } | LEFT OUTER { $$ = kJoinLeft; }
| LEFT OUTER { $$ = kJoinLeftOuter; }
| RIGHT OUTER { $$ = kJoinRightOuter; }
| LEFT { $$ = kJoinLeft; } | LEFT { $$ = kJoinLeft; }
| RIGHT OUTER { $$ = kJoinRight; }
| RIGHT { $$ = kJoinRight; } | RIGHT { $$ = kJoinRight; }
| FULL OUTER { $$ = kJoinFull; }
| OUTER { $$ = kJoinFull; }
| FULL { $$ = kJoinFull; }
| CROSS { $$ = kJoinCross; } | CROSS { $$ = kJoinCross; }
| /* empty, default */ { $$ = kJoinInner; } | /* empty, default */ { $$ = kJoinInner; }
; ;

View File

@ -49,11 +49,9 @@ namespace hsql {
// Possible types of joins. // Possible types of joins.
enum JoinType { enum JoinType {
kJoinInner, kJoinInner,
kJoinOuter, kJoinFull,
kJoinLeft, kJoinLeft,
kJoinRight, kJoinRight,
kJoinLeftOuter,
kJoinRightOuter,
kJoinCross, kJoinCross,
kJoinNatural kJoinNatural
}; };

View File

@ -298,7 +298,7 @@ TEST(SelectJoin) {
const TableRef* table = stmt->fromTable; const TableRef* table = stmt->fromTable;
const JoinDefinition* outer_join = table->join; const JoinDefinition* outer_join = table->join;
ASSERT_EQ(table->type, kTableJoin); ASSERT_EQ(table->type, kTableJoin);
ASSERT_EQ(outer_join->type, kJoinOuter); ASSERT_EQ(outer_join->type, kJoinFull);
ASSERT_EQ(outer_join->right->type, kTableName); ASSERT_EQ(outer_join->right->type, kTableName);
ASSERT_STREQ(outer_join->right->name, "Product"); ASSERT_STREQ(outer_join->right->name, "Product");
@ -347,3 +347,58 @@ TEST(SelectColumnOrder) {
ASSERT_STREQ(stmt->fromTable->list->at(2)->alias, "c"); ASSERT_STREQ(stmt->fromTable->list->at(2)->alias, "c");
ASSERT_STREQ(stmt->fromTable->list->at(3)->alias, "d"); ASSERT_STREQ(stmt->fromTable->list->at(3)->alias, "d");
} }
TEST(JoinTypes) {
SelectStatement* stmt;
SQLParserResult result;
SQLParser::parse("SELECT * FROM x join y on a=b; \
SELECT * FROM x inner join y on a=b; \
SELECT * FROM x left join y on a=b; \
SELECT * FROM x left outer join y on a=b; \
SELECT * FROM x right join y on a=b; \
SELECT * FROM x right outer join y on a=b; \
SELECT * FROM x full join y on a=b; \
SELECT * FROM x outer join y on a=b; \
SELECT * FROM x full outer join y on a=b; \
SELECT * FROM x natural join y; \
SELECT * FROM x cross join y on a=b; \
SELECT * FROM x, y where a = b;", &result);
stmt = (SelectStatement*) result.getStatement(0);
ASSERT_EQ(stmt->fromTable->join->type, kJoinInner);
stmt = (SelectStatement*) result.getStatement(1);
ASSERT_EQ(stmt->fromTable->join->type, kJoinInner);
stmt = (SelectStatement*) result.getStatement(2);
ASSERT_EQ(stmt->fromTable->join->type, kJoinLeft);
stmt = (SelectStatement*) result.getStatement(3);
ASSERT_EQ(stmt->fromTable->join->type, kJoinLeft);
stmt = (SelectStatement*) result.getStatement(4);
ASSERT_EQ(stmt->fromTable->join->type, kJoinRight);
stmt = (SelectStatement*) result.getStatement(5);
ASSERT_EQ(stmt->fromTable->join->type, kJoinRight);
stmt = (SelectStatement*) result.getStatement(6);
ASSERT_EQ(stmt->fromTable->join->type, kJoinFull);
stmt = (SelectStatement*) result.getStatement(7);
ASSERT_EQ(stmt->fromTable->join->type, kJoinFull);
stmt = (SelectStatement*) result.getStatement(8);
ASSERT_EQ(stmt->fromTable->join->type, kJoinFull);
stmt = (SelectStatement*) result.getStatement(9);
ASSERT_EQ(stmt->fromTable->join->type, kJoinNatural);
stmt = (SelectStatement*) result.getStatement(10);
ASSERT_EQ(stmt->fromTable->join->type, kJoinCross);
stmt = (SelectStatement*) result.getStatement(11);
ASSERT_NULL(stmt->fromTable->join);
}