15.3.41. crate_anon.crateweb.research.sql_writer


Copyright (C) 2015-2018 Rudolf Cardinal (rudolf@pobox.com).

This file is part of CRATE.

CRATE is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.

CRATE is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.

You should have received a copy of the GNU General Public License along with CRATE. If not, see <http://www.gnu.org/licenses/>.


crate_anon.crateweb.research.sql_writer.add_to_select(sql: str, grammar: cardinal_pythonlib.sql.sql_grammar.SqlGrammar, select_elements: List[crate_anon.crateweb.research.sql_writer.SelectElement] = None, where_conditions: List[crate_anon.common.sql.WhereCondition] = None, distinct: bool = None, where_type: str = 'AND', bracket_where: bool = False, magic_join: bool = True, join_type: str = 'NATURAL JOIN', join_condition: str = '', formatted: bool = True, debug: bool = False, debug_verbose: bool = False) → str[source]

This function encapsulates our query builder’s common operations. One premise is that SQL parsing is relatively slow, so we should do this only once. We parse; add bits to the parsed structure as required; then re-convert to text.

If you specify table/column, elements will be added to SELECT and FROM unless they already exist.

If you specify where_expression, elements will be added to WHERE. In this situation, you should also specify where_table; if the where_table isn’t yet in the FROM clause, this will be added as well.

Parsing is SLOW, so we should do as much as possible in a single call to this function.

crate_anon.crateweb.research.sql_writer.reparse_select(p: pyparsing.ParseResults, grammar: cardinal_pythonlib.sql.sql_grammar.SqlGrammar) → pyparsing.ParseResults[source]

Internal function for when we get desperate trying to hack around the results of pyparsing’s efforts.