| 1 | # This module is part of SQLAlchemy and is released under |
|---|
| 2 | # the MIT License: http://www.opensource.org/licenses/mit-license.php |
|---|
| 3 | |
|---|
| 4 | """Defines operators used in SQL expressions.""" |
|---|
| 5 | |
|---|
| 6 | from operator import ( |
|---|
| 7 | and_, or_, inv, add, mul, sub, div, mod, truediv, lt, le, ne, gt, ge, eq |
|---|
| 8 | ) |
|---|
| 9 | from sqlalchemy.util import symbol |
|---|
| 10 | |
|---|
| 11 | |
|---|
| 12 | def from_(): |
|---|
| 13 | raise NotImplementedError() |
|---|
| 14 | |
|---|
| 15 | def as_(): |
|---|
| 16 | raise NotImplementedError() |
|---|
| 17 | |
|---|
| 18 | def exists(): |
|---|
| 19 | raise NotImplementedError() |
|---|
| 20 | |
|---|
| 21 | def is_(): |
|---|
| 22 | raise NotImplementedError() |
|---|
| 23 | |
|---|
| 24 | def isnot(): |
|---|
| 25 | raise NotImplementedError() |
|---|
| 26 | |
|---|
| 27 | def collate(): |
|---|
| 28 | raise NotImplementedError() |
|---|
| 29 | |
|---|
| 30 | def op(a, opstring, b): |
|---|
| 31 | return a.op(opstring)(b) |
|---|
| 32 | |
|---|
| 33 | def like_op(a, b, escape=None): |
|---|
| 34 | return a.like(b, escape=escape) |
|---|
| 35 | |
|---|
| 36 | def notlike_op(a, b, escape=None): |
|---|
| 37 | raise NotImplementedError() |
|---|
| 38 | |
|---|
| 39 | def ilike_op(a, b, escape=None): |
|---|
| 40 | return a.ilike(b, escape=escape) |
|---|
| 41 | |
|---|
| 42 | def notilike_op(a, b, escape=None): |
|---|
| 43 | raise NotImplementedError() |
|---|
| 44 | |
|---|
| 45 | def between_op(a, b, c): |
|---|
| 46 | return a.between(b, c) |
|---|
| 47 | |
|---|
| 48 | def in_op(a, b): |
|---|
| 49 | return a.in_(b) |
|---|
| 50 | |
|---|
| 51 | def notin_op(a, b): |
|---|
| 52 | raise NotImplementedError() |
|---|
| 53 | |
|---|
| 54 | def distinct_op(a): |
|---|
| 55 | return a.distinct() |
|---|
| 56 | |
|---|
| 57 | def startswith_op(a, b, escape=None): |
|---|
| 58 | return a.startswith(b, escape=escape) |
|---|
| 59 | |
|---|
| 60 | def endswith_op(a, b, escape=None): |
|---|
| 61 | return a.endswith(b, escape=escape) |
|---|
| 62 | |
|---|
| 63 | def contains_op(a, b, escape=None): |
|---|
| 64 | return a.contains(b, escape=escape) |
|---|
| 65 | |
|---|
| 66 | def match_op(a, b): |
|---|
| 67 | return a.match(b) |
|---|
| 68 | |
|---|
| 69 | def comma_op(a, b): |
|---|
| 70 | raise NotImplementedError() |
|---|
| 71 | |
|---|
| 72 | def concat_op(a, b): |
|---|
| 73 | return a.concat(b) |
|---|
| 74 | |
|---|
| 75 | def desc_op(a): |
|---|
| 76 | return a.desc() |
|---|
| 77 | |
|---|
| 78 | def asc_op(a): |
|---|
| 79 | return a.asc() |
|---|
| 80 | |
|---|
| 81 | _commutative = set([eq, ne, add, mul]) |
|---|
| 82 | def is_commutative(op): |
|---|
| 83 | return op in _commutative |
|---|
| 84 | |
|---|
| 85 | _smallest = symbol('_smallest') |
|---|
| 86 | _largest = symbol('_largest') |
|---|
| 87 | |
|---|
| 88 | _PRECEDENCE = { |
|---|
| 89 | from_: 15, |
|---|
| 90 | mul: 7, |
|---|
| 91 | div: 7, |
|---|
| 92 | mod: 7, |
|---|
| 93 | add: 6, |
|---|
| 94 | sub: 6, |
|---|
| 95 | concat_op: 6, |
|---|
| 96 | match_op: 6, |
|---|
| 97 | ilike_op: 5, |
|---|
| 98 | notilike_op: 5, |
|---|
| 99 | like_op: 5, |
|---|
| 100 | notlike_op: 5, |
|---|
| 101 | in_op: 5, |
|---|
| 102 | notin_op: 5, |
|---|
| 103 | is_: 5, |
|---|
| 104 | isnot: 5, |
|---|
| 105 | eq: 5, |
|---|
| 106 | ne: 5, |
|---|
| 107 | gt: 5, |
|---|
| 108 | lt: 5, |
|---|
| 109 | ge: 5, |
|---|
| 110 | le: 5, |
|---|
| 111 | between_op: 5, |
|---|
| 112 | distinct_op: 5, |
|---|
| 113 | inv: 5, |
|---|
| 114 | and_: 3, |
|---|
| 115 | or_: 2, |
|---|
| 116 | comma_op: -1, |
|---|
| 117 | collate: 7, |
|---|
| 118 | as_: -1, |
|---|
| 119 | exists: 0, |
|---|
| 120 | _smallest: -1000, |
|---|
| 121 | _largest: 1000 |
|---|
| 122 | } |
|---|
| 123 | |
|---|
| 124 | def is_precedent(operator, against): |
|---|
| 125 | return (_PRECEDENCE.get(operator, _PRECEDENCE[_smallest]) <= |
|---|
| 126 | _PRECEDENCE.get(against, _PRECEDENCE[_largest])) |
|---|