(useNotOpr bool)
| 137 | type Eq map[string]interface{} |
| 138 | |
| 139 | func (eq Eq) toSQL(useNotOpr bool) (sql string, args []interface{}, err error) { |
| 140 | if len(eq) == 0 { |
| 141 | // Empty Sql{} evaluates to true. |
| 142 | sql = sqlTrue |
| 143 | return |
| 144 | } |
| 145 | |
| 146 | var ( |
| 147 | exprs []string |
| 148 | equalOpr = "=" |
| 149 | inOpr = "IN" |
| 150 | nullOpr = "IS" |
| 151 | inEmptyExpr = sqlFalse |
| 152 | ) |
| 153 | |
| 154 | if useNotOpr { |
| 155 | equalOpr = "<>" |
| 156 | inOpr = "NOT IN" |
| 157 | nullOpr = "IS NOT" |
| 158 | inEmptyExpr = sqlTrue |
| 159 | } |
| 160 | |
| 161 | sortedKeys := getSortedKeys(eq) |
| 162 | for _, key := range sortedKeys { |
| 163 | var expr string |
| 164 | val := eq[key] |
| 165 | |
| 166 | switch v := val.(type) { |
| 167 | case driver.Valuer: |
| 168 | if val, err = v.Value(); err != nil { |
| 169 | return |
| 170 | } |
| 171 | } |
| 172 | |
| 173 | r := reflect.ValueOf(val) |
| 174 | if r.Kind() == reflect.Ptr { |
| 175 | if r.IsNil() { |
| 176 | val = nil |
| 177 | } else { |
| 178 | val = r.Elem().Interface() |
| 179 | } |
| 180 | } |
| 181 | |
| 182 | if val == nil { |
| 183 | expr = fmt.Sprintf("%s %s NULL", key, nullOpr) |
| 184 | } else { |
| 185 | if isListType(val) { |
| 186 | valVal := reflect.ValueOf(val) |
| 187 | if valVal.Len() == 0 { |
| 188 | expr = inEmptyExpr |
| 189 | if args == nil { |
| 190 | args = []interface{}{} |
| 191 | } |
| 192 | } else { |
| 193 | for i := 0; i < valVal.Len(); i++ { |
| 194 | args = append(args, valVal.Index(i).Interface()) |
| 195 | } |
| 196 | expr = fmt.Sprintf("%s %s (%s)", key, inOpr, Placeholders(valVal.Len())) |
no test coverage detected