12.3.148. crate_anon.crateweb.research.sql_writer
crate_anon/crateweb/research/sql_writer.py
Copyright (C) 2015, University of Cambridge, Department of Psychiatry. Created by Rudolf Cardinal (rnc1001@cam.ac.uk).
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 <https://www.gnu.org/licenses/>.
Automatically create/manipulate SQL statements based on our extra knowledge of the fields that can be used to link across tables/databases.
- class crate_anon.crateweb.research.sql_writer.SelectElement(column_id: ColumnId | None = None, raw_select: str = '', from_table_for_raw_select: TableId | None = None, alias: str = '')[source]
Class to represent a result column in an SQL
SELECTstatement.- __init__(column_id: ColumnId | None = None, raw_select: str = '', from_table_for_raw_select: TableId | None = None, alias: str = '')[source]
- Parameters:
column_id – a
crate_anon.common.sql.ColumnIdobject; using this will automatically add the column’s table to theFROMclauseraw_select – as an alternative to
column_id, raw SQL for theSELECTclausefrom_table_for_raw_select – if
raw_selectis used, acrate_anon.common.sql.TableIdthat should be added to theFROMclausealias – alias to be used, i.e. for
SELECT something AS alias
- from_table() TableId | None[source]
Returns details of the table to be added to the
FROMclause of theSELECTstatement.- Returns:
a
crate_anon.common.sql.TableId, orNone(ifraw_selectis used andfrom_table_for_raw_selectwas not specified)
- from_table_str(grammar: SqlGrammar) str[source]
Returns a string form of
from_table(), i.e. an SQL identifier for theFROMclause.- Parameters:
grammar –
cardinal_pythonlib.sql.sql_grammar.SqlGrammarrepresenting the SQL dialect/grammar in use- Returns:
SQL like
from_table- Return type:
str
- crate_anon.crateweb.research.sql_writer.add_to_select(sql: str, grammar: SqlGrammar, select_elements: List[SelectElement] | None = None, where_conditions: List[WhereCondition] | None = None, distinct: bool | None = 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
SELECTandFROMunless they already exist.If you specify
where_expression, elements will be added toWHERE. In this situation, you should also specifywhere_table; if thewhere_tableisn’t yet in theFROMclause, this will be added as well.Parsing is SLOW, so we should do as much as possible in a single call to this function.
- Parameters:
sql – existing SQL statement
grammar –
cardinal_pythonlib.sql.sql_grammar.SqlGrammarrepresenting the SQL dialect/grammar in useselect_elements – optional list of
SelectElementobjects representing things to add to theSELECTclause of theSELECTstatement (i.e. results columns)where_conditions – optional list of
crate_anon.common.sql.WhereConditionrepresenting conditions to add to theWHEREclause of theSELECTstatementdistinct – if
True, make theSELECTstatement aSELECT DISTINCT; ifFalse, remove anyDISTINCT; ifNone, leave theDISTINCTstatus as it is.where_type – logical operator with which to join multiple parts of the
WHEREexpression, typicallyAND(but maybeOR, etc.)bracket_where – put brackets
()around each new part of theWHEREexpression?magic_join – perform a “magic join”, i.e. join the new table in based on our knowledge of the research database structure?
join_type – if
not magic_join, this is an SQL string specifying the join type, e.g."INNER JOIN"join_condition – if
not magic_join, this is an SQL string specifying the join condition, e.g."ON x = y"formatted – reformat the SQL to look pretty?
debug – show debugging information
debug_verbose – show verbose debugging information
- Returns:
SQL statement
- Return type:
str
- Raises:
DatabaseStructureNotUnderstood` if the relevant schema informatio –
cannot be looked up. –
- crate_anon.crateweb.research.sql_writer.get_join_info(grammar: SqlGrammar, parsed: ParseResults, jointable: TableId, magic_join: bool = False, nonmagic_join_type: str = 'INNER JOIN', nonmagic_join_condition: str = '') List[JoinInfo][source]
Works out how to join a new table into an existing SQL
SELECTquery.- Parameters:
grammar –
cardinal_pythonlib.sql.sql_grammar.SqlGrammarrepresenting the SQL dialect/grammar in useparsed – existing
pyparsing.ParseResultsrepresenting theSELECTstatement so farjointable –
crate_anon.common.sql.TableIdrepresenting the table to be joined inmagic_join – perform a “magic join”, i.e. join the new table in based on our knowledge of the research database structure?
nonmagic_join_type – if
not magic_join, this is an SQL string specifying the join type, e.g."INNER JOIN"nonmagic_join_condition – if
not magic_join, this is an SQL string specifying the join condition, e.g."ON x = y"
- Returns:
a list of
crate_anon.common.sql.JoinInfoobjects, e.g.[JoinInfo("tablename", "INNER JOIN", "WHERE somecondition")].- Raises:
DatabaseStructureNotUnderstood` if the relevant schema informatio –
cannot be looked up. –
Notes:
INNER JOINetc. is part of ANSI SQL
- crate_anon.crateweb.research.sql_writer.reparse_select(p: ParseResults, grammar: SqlGrammar) ParseResults[source]
Internal function for when we get desperate trying to hack around the results of
pyparsing’s efforts.takes a
pyparsing.ParseResultsconverts it to an SQL string
parses the string as a
SELECTstatementreturns the resulting
pyparsing.ParseResults