(
self, col: _ET, _include_singleton_constants: bool = False
)
| 1140 | |
| 1141 | @util.preload_module("sqlalchemy.sql.functions") |
| 1142 | def replace( |
| 1143 | self, col: _ET, _include_singleton_constants: bool = False |
| 1144 | ) -> Optional[_ET]: |
| 1145 | functions = util.preloaded.sql_functions |
| 1146 | |
| 1147 | # TODO: cython candidate |
| 1148 | |
| 1149 | if self.include_fn and not self.include_fn(col): # type: ignore |
| 1150 | return None |
| 1151 | elif self.exclude_fn and self.exclude_fn(col): # type: ignore |
| 1152 | return None |
| 1153 | |
| 1154 | if isinstance(col, FromClause) and not isinstance( |
| 1155 | col, functions.FunctionElement |
| 1156 | ): |
| 1157 | if self.selectable.is_derived_from(col): |
| 1158 | if self.adapt_from_selectables: |
| 1159 | for adp in self.adapt_from_selectables: |
| 1160 | if adp.is_derived_from(col): |
| 1161 | break |
| 1162 | else: |
| 1163 | return None |
| 1164 | return self.selectable # type: ignore |
| 1165 | elif isinstance(col, Alias) and isinstance( |
| 1166 | col.element, TableClause |
| 1167 | ): |
| 1168 | # we are a SELECT statement and not derived from an alias of a |
| 1169 | # table (which nonetheless may be a table our SELECT derives |
| 1170 | # from), so return the alias to prevent further traversal |
| 1171 | # or |
| 1172 | # we are an alias of a table and we are not derived from an |
| 1173 | # alias of a table (which nonetheless may be the same table |
| 1174 | # as ours) so, same thing |
| 1175 | return col |
| 1176 | else: |
| 1177 | # other cases where we are a selectable and the element |
| 1178 | # is another join or selectable that contains a table which our |
| 1179 | # selectable derives from, that we want to process |
| 1180 | return None |
| 1181 | |
| 1182 | elif not isinstance(col, ColumnElement): |
| 1183 | return None |
| 1184 | elif not _include_singleton_constants and col._is_singleton_constant: |
| 1185 | # dont swap out NULL, TRUE, FALSE for a label name |
| 1186 | # in a SQL statement that's being rewritten, |
| 1187 | # leave them as the constant. This is first noted in #6259, |
| 1188 | # however the logic to check this moved here as of #7154 so that |
| 1189 | # it is made specific to SQL rewriting and not all column |
| 1190 | # correspondence |
| 1191 | |
| 1192 | return None |
| 1193 | |
| 1194 | if "adapt_column" in col._annotations: |
| 1195 | col = col._annotations["adapt_column"] |
| 1196 | |
| 1197 | if TYPE_CHECKING: |
| 1198 | assert isinstance(col, KeyedColumnElement) |
| 1199 |
no test coverage detected