SQL returns the SQL representation of this binary expression. The operator, left, and right sub-expressions are serialized in infix notation. NOT-qualified operators (LIKE, ILIKE, SIMILAR TO) are rendered as "x NOT OP y".
()
| 155 | // The operator, left, and right sub-expressions are serialized in infix notation. |
| 156 | // NOT-qualified operators (LIKE, ILIKE, SIMILAR TO) are rendered as "x NOT OP y". |
| 157 | func (b *BinaryExpression) SQL() string { |
| 158 | if b == nil { |
| 159 | return "" |
| 160 | } |
| 161 | left := exprSQL(b.Left) |
| 162 | right := exprSQL(b.Right) |
| 163 | op := b.Operator |
| 164 | if b.CustomOp != nil { |
| 165 | op = b.CustomOp.String() |
| 166 | } |
| 167 | |
| 168 | upperOp := strings.ToUpper(op) |
| 169 | |
| 170 | // Handle IS NULL / IS NOT NULL (right side is NULL literal) |
| 171 | if upperOp == "IS NULL" || upperOp == "IS NOT NULL" { |
| 172 | return fmt.Sprintf("%s %s", left, upperOp) |
| 173 | } |
| 174 | |
| 175 | // Handle special operators like LIKE, ILIKE, SIMILAR TO |
| 176 | if b.Not { |
| 177 | switch upperOp { |
| 178 | case "LIKE", "ILIKE", "SIMILAR TO": |
| 179 | return fmt.Sprintf("%s NOT %s %s", left, upperOp, right) |
| 180 | default: |
| 181 | return fmt.Sprintf("NOT (%s %s %s)", left, op, right) |
| 182 | } |
| 183 | } |
| 184 | |
| 185 | return fmt.Sprintf("%s %s %s", left, op, right) |
| 186 | } |
| 187 | |
| 188 | // SQL returns the SQL representation of this unary expression. |
| 189 | // Prefix operators (NOT, +, -, etc.) are prepended; the PostgreSQL |