convertCall converts a function call to a SQL expression.
(cfg ConvertConfig, call ast.Call)
| 111 | |
| 112 | // convertCall converts a function call to a SQL expression. |
| 113 | func convertCall(cfg ConvertConfig, call ast.Call) (sqltypes.Node, error) { |
| 114 | if len(call) == 0 { |
| 115 | return nil, xerrors.Errorf("empty call") |
| 116 | } |
| 117 | |
| 118 | // Operator is the first term |
| 119 | op := call[0] |
| 120 | var args []*ast.Term |
| 121 | if len(call) > 1 { |
| 122 | args = call[1:] |
| 123 | } |
| 124 | |
| 125 | opString := op.String() |
| 126 | // Supported operators. |
| 127 | switch op.String() { |
| 128 | case "neq", "eq", "equals", "equal": |
| 129 | args, err := convertTerms(cfg, args, 2) |
| 130 | if err != nil { |
| 131 | return nil, xerrors.Errorf("arguments: %w", err) |
| 132 | } |
| 133 | |
| 134 | not := false |
| 135 | if opString == "neq" || opString == "notequals" || opString == "notequal" { |
| 136 | not = true |
| 137 | } |
| 138 | |
| 139 | equality := sqltypes.Equality(not, args[0], args[1]) |
| 140 | return sqltypes.BoolParenthesis(equality), nil |
| 141 | case "internal.member_2": |
| 142 | args, err := convertTerms(cfg, args, 2) |
| 143 | if err != nil { |
| 144 | return nil, xerrors.Errorf("arguments: %w", err) |
| 145 | } |
| 146 | |
| 147 | member := sqltypes.MemberOf(args[0], args[1]) |
| 148 | return sqltypes.BoolParenthesis(member), nil |
| 149 | default: |
| 150 | return nil, xerrors.Errorf("operator %s not supported", op) |
| 151 | } |
| 152 | } |
| 153 | |
| 154 | func convertTerms(cfg ConvertConfig, terms []*ast.Term, expected int) ([]sqltypes.Node, error) { |
| 155 | if len(terms) != expected { |
no test coverage detected