package models

import (
    "fmt"
    "strings"
)

type NullType byte
const (
    _ NullType = iota
    // IsNull the same as `is null`
    IsNull
    // IsNotNull the same as `is not null`
    IsNotNull
)

// sql build where
func WhereBuild(where map[string]interface{}) (whereSQL string, vals []interface{}, err error) {
    for k, v := range where {
        ks := strings.Split(k, " ")
        if len(ks) > 2 {
            return "", nil, fmt.Errorf("Error in query condition: %s. ", k)
        }
    if whereSQL != "" {
        whereSQL += " AND "
    }
    strings.Join(ks, ",")
    switch len(ks) {
    case 1:
        //fmt.Println(reflect.TypeOf(v))
        switch v := v.(type) {
        case NullType:
            if v == IsNotNull {
                whereSQL += fmt.Sprint(k, " IS NOT NULL")
            } else {
                whereSQL += fmt.Sprint(k, " IS NULL")
            }
        default:
            whereSQL += fmt.Sprint(k, "=?")
            vals = append(vals, v)
        }
        break
    case 2:
        k = ks[0]
        switch ks[1] {
        case "=":
            whereSQL += fmt.Sprint(k, "=?")
            vals = append(vals, v)
            break
        case ">":
            whereSQL += fmt.Sprint(k, ">?")
            vals = append(vals, v)
            break
        case ">=":
            whereSQL += fmt.Sprint(k, ">=?")
            vals = append(vals, v)
            break
        case "<":
            whereSQL += fmt.Sprint(k, "<?")
            vals = append(vals, v)
            break
        case "<=":
            whereSQL += fmt.Sprint(k, "<=?")
            vals = append(vals, v)
            break
        case "!=":
            whereSQL += fmt.Sprint(k, "!=?")
            vals = append(vals, v)
            break
        case "<>":
            whereSQL += fmt.Sprint(k, "!=?")
            vals = append(vals, v)
            break
        case "in":
            whereSQL += fmt.Sprint(k, " in (?)")
            vals = append(vals, v)
            break
        case "like":
            whereSQL += fmt.Sprint(k, " like ?")
            vals = append(vals, v)
        }
        break
    }
}
return

}