Skip to content

Commit

Permalink
Break out query_term_t parser
Browse files Browse the repository at this point in the history
Makes non_join_query_term_t a derivative of query_term_t and prepares
the term parser for upcoming INTERSECTS processing.

Issue #78
  • Loading branch information
jaypipes committed Jun 13, 2018
1 parent bf25711 commit b2dda89
Show file tree
Hide file tree
Showing 9 changed files with 181 additions and 11 deletions.
1 change: 1 addition & 0 deletions libsqltoast/include/sqltoast/print.h
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,7 @@ std::ostream& operator<< (std::ostream& out, const position_expression_t& pe);
std::ostream& operator<< (std::ostream& out, const query_expression_t& qe);
std::ostream& operator<< (std::ostream& out, const query_specification_non_join_query_primary_t& primary);
std::ostream& operator<< (std::ostream& out, const query_specification_t& qs);
std::ostream& operator<< (std::ostream& out, const query_term_t& qt);
std::ostream& operator<< (std::ostream& out, const row_value_constructor_element_t& rvce);
std::ostream& operator<< (std::ostream& out, const row_value_constructor_t& rvc);
std::ostream& operator<< (std::ostream& out, const row_value_expression_t& rve);
Expand Down
5 changes: 3 additions & 2 deletions libsqltoast/include/sqltoast/query.h
Original file line number Diff line number Diff line change
Expand Up @@ -143,9 +143,10 @@ typedef struct joined_table_query_term : query_term_t {
} joined_table_query_term_t;

typedef struct non_join_query_expression : query_expression_t {
std::unique_ptr<non_join_query_term_t> term;
// Guaranteed to be static_castable to non_join_query_term_t.
std::unique_ptr<query_term_t> term;
non_join_query_expression(
std::unique_ptr<non_join_query_term_t>& term) :
std::unique_ptr<query_term_t>& term) :
query_expression_t(QUERY_COMPONENT_TYPE_NON_JOIN),
term(std::move(term))
{}
Expand Down
6 changes: 5 additions & 1 deletion libsqltoast/src/parser/parse.h
Original file line number Diff line number Diff line change
Expand Up @@ -135,10 +135,14 @@ bool parse_non_join_query_expression(
parse_context_t& ctx,
token_t& cur_tok,
std::unique_ptr<query_expression_t>& out);
bool parse_query_term(
parse_context_t& ctx,
token_t& cur_tok,
std::unique_ptr<query_term_t>& out);
bool parse_non_join_query_term(
parse_context_t& ctx,
token_t& cur_tok,
std::unique_ptr<non_join_query_term_t>& out);
std::unique_ptr<query_term_t>& out);
bool parse_non_join_query_primary(
parse_context_t& ctx,
token_t& cur_tok,
Expand Down
49 changes: 41 additions & 8 deletions libsqltoast/src/parser/query.cc
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,6 @@ namespace sqltoast {
// <non-join query expression>
// | <joined table>
//
// <query term> ::=
// <non-join query term>
// | <joined table>
//
// <query primary> ::=
// <non-join query primary>
// | <joined table>
Expand Down Expand Up @@ -55,14 +51,51 @@ bool parse_non_join_query_expression(
parse_context_t& ctx,
token_t& cur_tok,
std::unique_ptr<query_expression_t>& out) {
std::unique_ptr<non_join_query_term_t> njqt;
std::unique_ptr<query_term_t> query_term;

if (! parse_non_join_query_term(ctx, cur_tok, njqt))
if (! parse_non_join_query_term(ctx, cur_tok, query_term))
return false;
// TODO(jaypipes): Handle UNION and EXCEPT
if (ctx.opts.disable_statement_construction)
return true;
out = std::make_unique<non_join_query_expression_t>(njqt);
out = std::make_unique<non_join_query_expression_t>(query_term);
return true;
}

// <query term> ::=
// <non-join query term>
// | <joined table>
bool parse_query_term(
parse_context_t& ctx,
token_t& cur_tok,
std::unique_ptr<query_term_t>& out) {
lexer_t& lex = ctx.lexer;
parse_position_t start = lex.cursor;
token_t start_tok = lex.current_token;
std::unique_ptr<joined_table_t> joined_table;
if (parse_non_join_query_term(ctx, cur_tok, out))
return true;
if (ctx.result.code == PARSE_SYNTAX_ERROR)
return false;

// Reset cursor to before parsing of joined table attempt.
lex.cursor = start;
lex.current_token = cur_tok = start_tok;
if (! parse_joined_table(ctx, cur_tok, joined_table))
goto err_expect_joined_table;
goto push_joined_table_term;
err_expect_joined_table:
{
std::stringstream estr;
estr << "Expected <joined table> but found "
<< cur_tok << std::endl;
create_syntax_error_marker(ctx, estr);
return false;
}
push_joined_table_term:
if (ctx.opts.disable_statement_construction)
return true;
out = std::make_unique<joined_table_query_term_t>(joined_table);
return true;
}

Expand All @@ -73,7 +106,7 @@ bool parse_non_join_query_expression(
bool parse_non_join_query_term(
parse_context_t& ctx,
token_t& cur_tok,
std::unique_ptr<non_join_query_term_t>& out) {
std::unique_ptr<query_term_t>& out) {
std::unique_ptr<non_join_query_primary_t> njqp;

if (! parse_non_join_query_primary(ctx, cur_tok, njqp))
Expand Down
20 changes: 20 additions & 0 deletions libsqltoast/src/print/query.cc
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,26 @@ std::ostream& operator<< (std::ostream& out, const non_join_query_expression_t&
return out;
}

std::ostream& operator<< (std::ostream& out, const query_term_t& term) {
switch (term.query_component_type) {
case QUERY_COMPONENT_TYPE_NON_JOIN:
{
const non_join_query_term_t& sub =
static_cast<const non_join_query_term_t&>(term);
out << sub;
}
break;
case QUERY_COMPONENT_TYPE_JOINED_TABLE:
{
const joined_table_query_term_t& sub =
static_cast<const joined_table_query_term_t&>(term);
out << sub;
}
break;
}
return out;
}

std::ostream& operator<< (std::ostream& out, const non_join_query_term_t& term) {
out << *term.primary;
return out;
Expand Down
1 change: 1 addition & 0 deletions sqltoaster/fill.h
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ void fill(mapping_t& node, const sqltoast::quantified_comparison_predicate_t& pr
void fill(mapping_t& node, const sqltoast::query_expression_t& qe);
void fill(mapping_t& node, const sqltoast::query_specification_non_join_query_primary_t& primary);
void fill(mapping_t& node, const sqltoast::query_specification_t& query);
void fill(mapping_t& node, const sqltoast::query_term_t& term);
void fill(mapping_t& node, const sqltoast::row_value_constructor_element_t& rvce);
void fill(mapping_t& node, const sqltoast::row_value_constructor_t& rvc);
void fill(mapping_t& node, const sqltoast::row_value_expression_t& rve);
Expand Down
19 changes: 19 additions & 0 deletions sqltoaster/node/statement.cc
Original file line number Diff line number Diff line change
Expand Up @@ -293,6 +293,25 @@ void fill(mapping_t& node, const sqltoast::table_expression_t& table_exp) {
}
}

void fill(mapping_t& node, const sqltoast::query_term_t& term) {
switch (term.query_component_type) {
case sqltoast::QUERY_COMPONENT_TYPE_NON_JOIN:
{
const sqltoast::non_join_query_term_t& sub =
static_cast<const sqltoast::non_join_query_term_t&>(term);
fill(node, sub);
}
break;
case sqltoast::QUERY_COMPONENT_TYPE_JOINED_TABLE:
{
const sqltoast::joined_table_query_term_t& sub =
static_cast<const sqltoast::joined_table_query_term_t&>(term);
fill(node, sub);
}
break;
}
}

void fill(mapping_t& node, const sqltoast::query_expression_t& qe) {
switch (qe.query_component_type) {
case sqltoast::QUERY_COMPONENT_TYPE_NON_JOIN:
Expand Down
19 changes: 19 additions & 0 deletions sqltoaster/print/yaml.cc
Original file line number Diff line number Diff line change
Expand Up @@ -353,6 +353,25 @@ void to_yaml(printer_t& ptr, std::ostream& out, const sqltoast::non_join_query_e
to_yaml(ptr, out, *qe.term);
}

void to_yaml(printer_t& ptr, std::ostream& out, const sqltoast::query_term_t& term) {
switch (term.query_component_type) {
case sqltoast::QUERY_COMPONENT_TYPE_NON_JOIN:
{
const sqltoast::non_join_query_term_t& sub =
static_cast<const sqltoast::non_join_query_term_t&>(term);
to_yaml(ptr, out, sub);
}
break;
case sqltoast::QUERY_COMPONENT_TYPE_JOINED_TABLE:
{
const sqltoast::joined_table_query_term_t& sub =
static_cast<const sqltoast::joined_table_query_term_t&>(term);
to_yaml(ptr, out, sub);
}
break;
}
}

void to_yaml(printer_t& ptr, std::ostream& out, const sqltoast::non_join_query_term_t& term) {
to_yaml(ptr, out, *term.primary);
}
Expand Down
72 changes: 72 additions & 0 deletions sqltoaster/print/yaml.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
/*
* Use and distribution licensed under the Apache license version 2.
*
* See the COPYING file in the root project directory for full text.
*/

#ifndef SQLTOASTER_PRINT_YAML_PRINTERS_H
#define SQLTOASTER_PRINT_YAML_PRINTERS_H

#include <sqltoast/print.h>

#include "../printer.h"

namespace sqltoaster {
namespace print {

void to_yaml(printer_t& ptr, std::ostream& out, const sqltoast::add_column_action_t& action);
void to_yaml(printer_t& ptr, std::ostream& out, const sqltoast::add_constraint_action_t& action);
void to_yaml(printer_t& ptr, std::ostream& out, const sqltoast::alter_column_action_t& action);
void to_yaml(printer_t& ptr, std::ostream& out, const sqltoast::alter_table_action_t& action);
void to_yaml(printer_t& ptr, std::ostream& out, const sqltoast::alter_table_statement_t& stmt);
void to_yaml(printer_t& ptr, std::ostream& out, const sqltoast::between_predicate_t& pred);
void to_yaml(printer_t& ptr, std::ostream& out, const sqltoast::boolean_factor_t& bf);
void to_yaml(printer_t& ptr, std::ostream& out, const sqltoast::boolean_primary_t& bp);
void to_yaml(printer_t& ptr, std::ostream& out, const sqltoast::boolean_term_t& bt);
void to_yaml(printer_t& ptr, std::ostream& out, const sqltoast::comp_op_t& op);
void to_yaml(printer_t& ptr, std::ostream& out, const sqltoast::comp_predicate_t& pred);
void to_yaml(printer_t& ptr, std::ostream& out, const sqltoast::create_schema_statement_t& stmt);
void to_yaml(printer_t& ptr, std::ostream& out, const sqltoast::create_table_statement_t& stmt);
void to_yaml(printer_t& ptr, std::ostream& out, const sqltoast::create_view_statement_t& stmt);
void to_yaml(printer_t& ptr, std::ostream& out, const sqltoast::delete_statement_t& stmt);
void to_yaml(printer_t& ptr, std::ostream& out, const sqltoast::drop_column_action_t& action);
void to_yaml(printer_t& ptr, std::ostream& out, const sqltoast::drop_column_action_t& action);
void to_yaml(printer_t& ptr, std::ostream& out, const sqltoast::drop_schema_statement_t& stmt);
void to_yaml(printer_t& ptr, std::ostream& out, const sqltoast::drop_table_statement_t& stmt);
void to_yaml(printer_t& ptr, std::ostream& out, const sqltoast::drop_view_statement_t& stmt);
void to_yaml(printer_t& ptr, std::ostream& out, const sqltoast::exists_predicate_t& pred);
void to_yaml(printer_t& ptr, std::ostream& out, const sqltoast::grant_action_t& action);
void to_yaml(printer_t& ptr, std::ostream& out, const sqltoast::grant_statement_t& stmt);
void to_yaml(printer_t& ptr, std::ostream& out, const sqltoast::in_subquery_predicate_t& pred);
void to_yaml(printer_t& ptr, std::ostream& out, const sqltoast::in_values_predicate_t& pred);
void to_yaml(printer_t& ptr, std::ostream& out, const sqltoast::insert_select_statement_t& stmt);
void to_yaml(printer_t& ptr, std::ostream& out, const sqltoast::insert_statement_t& stmt);
void to_yaml(printer_t& ptr, std::ostream& out, const sqltoast::joined_table_query_expression_t& qe);
void to_yaml(printer_t& ptr, std::ostream& out, const sqltoast::joined_table_t& jt);
void to_yaml(printer_t& ptr, std::ostream& out, const sqltoast::like_predicate_t& pred);
void to_yaml(printer_t& ptr, std::ostream& out, const sqltoast::match_predicate_t& pred);
void to_yaml(printer_t& ptr, std::ostream& out, const sqltoast::non_join_query_expression_t& qe);
void to_yaml(printer_t& ptr, std::ostream& out, const sqltoast::non_join_query_primary_t& primary);
void to_yaml(printer_t& ptr, std::ostream& out, const sqltoast::non_join_query_term_t& term);
void to_yaml(printer_t& ptr, std::ostream& out, const sqltoast::null_predicate_t& pred);
void to_yaml(printer_t& ptr, std::ostream& out, const sqltoast::overlaps_predicate_t& pred);
void to_yaml(printer_t& ptr, std::ostream& out, const sqltoast::predicate_t& pred);
void to_yaml(printer_t& ptr, std::ostream& out, const sqltoast::quantified_comparison_predicate_t& pred);
void to_yaml(printer_t& ptr, std::ostream& out, const sqltoast::query_expression_t& qe);
void to_yaml(printer_t& ptr, std::ostream& out, const sqltoast::query_specification_non_join_query_primary_t& primary);
void to_yaml(printer_t& ptr, std::ostream& out, const sqltoast::query_specification_t& query);
void to_yaml(printer_t& ptr, std::ostream& out, const sqltoast::query_term_t& term);
void to_yaml(printer_t& ptr, std::ostream& out, const sqltoast::row_value_constructor_element_t& rvce, bool list_item);
void to_yaml(printer_t& ptr, std::ostream& out, const sqltoast::row_value_constructor_t& rvc);
void to_yaml(printer_t& ptr, std::ostream& out, const sqltoast::row_value_expression_t& rve);
void to_yaml(printer_t& ptr, std::ostream& out, const sqltoast::search_condition_t& sc);
void to_yaml(printer_t& ptr, std::ostream& out, const sqltoast::select_statement_t& stmt);
void to_yaml(printer_t& ptr, std::ostream& out, const sqltoast::statement_t& stmt);
void to_yaml(printer_t& ptr, std::ostream& out, const sqltoast::table_expression_t& table_exp);
void to_yaml(printer_t& ptr, std::ostream& out, const sqltoast::unique_predicate_t& pred);
void to_yaml(printer_t& ptr, std::ostream& out, const sqltoast::update_statement_t& stmt);

} // namespace sqltoaster::print
} // namespace sqltoaster

#endif /* SQLTOASTER_PRINT_YAML_PRINTERS_H */

0 comments on commit b2dda89

Please sign in to comment.