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)
| 182 | |
| 183 | |
| 184 | def _get_select_comparisons(statement): |
| 185 | """Search a Select or Query object for binary expressions. |
| 186 | |
| 187 | Returns expressions which match a Column against one or more |
| 188 | literal values as a list of tuples of the form |
| 189 | (column, operator, values). "values" is a single value |
| 190 | or tuple of values depending on the operator. |
| 191 | |
| 192 | """ |
| 193 | binds = {} |
| 194 | clauses = set() |
| 195 | comparisons = [] |
| 196 | |
| 197 | def visit_bindparam(bind): |
| 198 | # visit a bind parameter. |
| 199 | |
| 200 | value = bind.effective_value |
| 201 | binds[bind] = value |
| 202 | |
| 203 | def visit_column(column): |
| 204 | clauses.add(column) |
| 205 | |
| 206 | def visit_binary(binary): |
| 207 | if binary.left in clauses and binary.right in binds: |
| 208 | comparisons.append( |
| 209 | (binary.left, binary.operator, binds[binary.right]) |
| 210 | ) |
| 211 | |
| 212 | elif binary.left in binds and binary.right in clauses: |
| 213 | comparisons.append( |
| 214 | (binary.right, binary.operator, binds[binary.left]) |
| 215 | ) |
| 216 | |
| 217 | # here we will traverse through the query's criterion, searching |
| 218 | # for SQL constructs. We will place simple column comparisons |
| 219 | # into a list. |
| 220 | if statement.whereclause is not None: |
| 221 | visitors.traverse( |
| 222 | statement.whereclause, |
| 223 | {}, |
| 224 | { |
| 225 | "bindparam": visit_bindparam, |
| 226 | "binary": visit_binary, |
| 227 | "column": visit_column, |
| 228 | }, |
| 229 | ) |
| 230 | return comparisons |
| 231 | |
| 232 | |
| 233 | # further configure create_session to use these functions |
no test coverage detected