Revision 2222
Added by Aaron Marcuse-Kubitza over 12 years ago
lib/sql_gen.py | ||
---|---|---|
1 | 1 |
# SQL code generation |
2 | 2 |
|
3 | 3 |
import sql |
4 |
import strings |
|
4 | 5 |
|
5 | 6 |
##### SQL code objects |
6 | 7 |
|
... | ... | |
61 | 62 |
|
62 | 63 |
class CompareCond(ValueCond): |
63 | 64 |
def __init__(self, value, operator='='): |
65 |
''' |
|
66 |
@param operator By default, compares NULL values literally. Use '~=' or |
|
67 |
'~!=' to pass NULLs through. |
|
68 |
''' |
|
64 | 69 |
ValueCond.__init__(self, value) |
65 | 70 |
self.operator = operator |
66 | 71 |
|
67 | 72 |
def to_str(self, db, left_value): |
68 | 73 |
if not isinstance(left_value, Code): left_value = Col(left_value) |
69 | 74 |
|
75 |
right_value = self.value |
|
76 |
left = left_value.to_str(db) |
|
77 |
right = right_value.to_str(db) |
|
78 |
|
|
79 |
# Parse operator |
|
70 | 80 |
operator = self.operator |
71 |
if is_null(self.value): operator = 'IS' |
|
72 |
return left_value.to_str(db)+' '+operator+' '+self.value.to_str(db) |
|
81 |
passthru_null_ref = [False] |
|
82 |
operator = strings.remove_prefix('~', operator, passthru_null_ref) |
|
83 |
neg_ref = [False] |
|
84 |
operator = strings.remove_prefix('!', operator, neg_ref) |
|
85 |
equals = operator.endswith('=') |
|
86 |
if equals and is_null(self.value): operator = 'IS' |
|
87 |
|
|
88 |
# Create str |
|
89 |
str_ = left+' '+operator+' '+right |
|
90 |
if equals and not passthru_null_ref[0] and isinstance(right_value, Col): |
|
91 |
str_ += ' OR ('+left+' IS NULL AND '+right+' IS NULL)' |
|
92 |
if neg_ref[0]: str_ = 'NOT ('+str_+')' |
|
93 |
return str_ |
|
73 | 94 |
|
74 | 95 |
def as_ValueCond(value): |
75 | 96 |
if not isinstance(value, ValueCond): return CompareCond(value) |
Also available in: Unified diff
sql_gen.py: CompareCond: By default, compare NULL values literally. Support operator values to pass NULLs through.