Return FK-based join condition strings for the tables currently in the query. For each FK relation where both the FK table and the referenced table appear in *tables*, yields a string like ``alias1.col = alias2.ref_col`` (using the alias when one exists, otherwise the table
(self, tables: list[tuple[str | None, str, str]])
| 1072 | schema_meta["relations"].append((table, col, ref_table, ref_col)) |
| 1073 | |
| 1074 | def _fk_join_conditions(self, tables: list[tuple[str | None, str, str]]) -> list[str]: |
| 1075 | """Return FK-based join condition strings for the tables currently in the query. |
| 1076 | |
| 1077 | For each FK relation where both the FK table and the referenced table appear in |
| 1078 | *tables*, yields a string like ``alias1.col = alias2.ref_col`` (using the alias |
| 1079 | when one exists, otherwise the table name). |
| 1080 | """ |
| 1081 | schema_meta = self.dbmetadata["foreign_keys"].get(self.dbname, {}) |
| 1082 | relations = schema_meta.get("relations", []) |
| 1083 | |
| 1084 | # Map escaped table name -> alias (or table name when no alias). |
| 1085 | # Skip tables from a different schema; we only have FK metadata for the current db. |
| 1086 | alias_map: dict[str, str] = {} |
| 1087 | for tbl_schema, tbl, alias in tables: |
| 1088 | if tbl_schema and tbl_schema != self.dbname: |
| 1089 | continue |
| 1090 | escaped = self.escape_name(tbl) |
| 1091 | alias_map[escaped] = alias or tbl |
| 1092 | |
| 1093 | conditions: list[str] = [] |
| 1094 | for fk_table, fk_col, ref_table, ref_col in relations: |
| 1095 | lhs = alias_map.get(fk_table) |
| 1096 | rhs = alias_map.get(ref_table) |
| 1097 | if lhs and rhs: |
| 1098 | conditions.append(f"{lhs}.{fk_col} = {rhs}.{ref_col}") |
| 1099 | return conditions |
| 1100 | |
| 1101 | def extend_functions(self, func_data: list[str] | Generator[tuple[str, str]], builtin: bool = False) -> None: |
| 1102 | # if 'builtin' is set this is extending the list of builtin functions |