Search a Select or Query object for binary expressions. Returns expressions which match a Column against one or more literal values as a list of tuples of the form (column, operator, values). "values" is a single value or tuple of values depending on the operator.
(statement)
| 196 | |
| 197 | |
| 198 | def _get_select_comparisons(statement): |
| 199 | """Search a Select or Query object for binary expressions. |
| 200 | |
| 201 | Returns expressions which match a Column against one or more |
| 202 | literal values as a list of tuples of the form |
| 203 | (column, operator, values). "values" is a single value |
| 204 | or tuple of values depending on the operator. |
| 205 | |
| 206 | """ |
| 207 | binds = {} |
| 208 | clauses = set() |
| 209 | comparisons = [] |
| 210 | |
| 211 | def visit_bindparam(bind): |
| 212 | # visit a bind parameter. |
| 213 | |
| 214 | value = bind.effective_value |
| 215 | binds[bind] = value |
| 216 | |
| 217 | def visit_column(column): |
| 218 | clauses.add(column) |
| 219 | |
| 220 | def visit_binary(binary): |
| 221 | if binary.left in clauses and binary.right in binds: |
| 222 | comparisons.append( |
| 223 | (binary.left, binary.operator, binds[binary.right]) |
| 224 | ) |
| 225 | |
| 226 | elif binary.left in binds and binary.right in clauses: |
| 227 | comparisons.append( |
| 228 | (binary.right, binary.operator, binds[binary.left]) |
| 229 | ) |
| 230 | |
| 231 | # here we will traverse through the query's criterion, searching |
| 232 | # for SQL constructs. We will place simple column comparisons |
| 233 | # into a list. |
| 234 | if statement.whereclause is not None: |
| 235 | visitors.traverse( |
| 236 | statement.whereclause, |
| 237 | {}, |
| 238 | { |
| 239 | "bindparam": visit_bindparam, |
| 240 | "binary": visit_binary, |
| 241 | "column": visit_column, |
| 242 | }, |
| 243 | ) |
| 244 | return comparisons |
| 245 | |
| 246 | |
| 247 | # further configure create_session to use these functions |
no test coverage detected