关于golang:聊聊dbsync的Criterion

38次阅读

共计 2610 个字符,预计需要花费 7 分钟才能阅读完成。

本文次要钻研一下 dbsync 的 Criterion

Criterion

//Criterion represents criterion
type Criterion fmt.Stringer

Criterion 是一个 fmt.Stringer 类型

between

type between struct {
    from int
    to   int
}

func (b between) String() string {return fmt.Sprintf("BETWEEN %v AND %v", b.from, b.to)
}

//NewBetween creates new betwee criterion
func NewBetween(from, to int) Criterion {return &between{from: from, to: to}
}

between 定义 from、to 两个属性

lessOrEqual

type lessOrEqual struct {value int}

func (c lessOrEqual) String() string {return fmt.Sprintf("<= %v", c.value)
}

//NewLessOrEqual creates less of equal criterion
func NewLessOrEqual(value int) Criterion {return &lessOrEqual{value}
}

lessOrEqual 定义了 value 属性,表达式为 <=

greaterThan

type greaterThan struct {value int}

func (c greaterThan) String() string {return fmt.Sprintf("> %v", c.value)
}

//NewGraterThan creates grater than criterion
func NewGraterThan(value int) Criterion {return &greaterThan{value}
}

greaterThan 定义了 value 属性,表达式为 >

greaterOrEqual

type greaterOrEqual struct {value int}

func (c greaterOrEqual) String() string {return fmt.Sprintf(">= %v", c.value)
}

//NewGraterOrEqual creates grater or equal criterion
func NewGraterOrEqual(value int) Criterion {return &greaterOrEqual{value}
}

greaterOrEqual 定义了 value 属性,表达式为 >=

ToCriterion

//ToCriterion converts a kv pair to a criterion
func ToCriterion(k string, v interface{}) string {if greaterOrEqual, ok := v.(*greaterOrEqual); ok {return fmt.Sprintf("%v >= %v", k, greaterOrEqual.value)
    } else if greaterThan, ok := v.(*greaterThan); ok {return fmt.Sprintf("%v > %v", k, greaterThan.value)
    } else if lessOrEqual, ok := v.(*lessOrEqual); ok {return fmt.Sprintf("%v <= %v", k, lessOrEqual.value)
    } else if between, ok := v.(*between); ok {return fmt.Sprintf("%v BETWEEN %v AND %v", k, between.from, between.to)
    } else if toolbox.IsSlice(v) {aSlice := toolbox.AsSlice(v)
        var whereValues = make([]string, 0)
        for _, item := range aSlice {if intValue, err := toolbox.ToInt(item); err == nil {whereValues = append(whereValues, fmt.Sprintf(`%v`, intValue))
            } else {itemLiteral := toolbox.AsString(item)
                if strings.HasPrefix(itemLiteral, "(") && strings.HasSuffix(itemLiteral, ")") {whereValues = append(whereValues, fmt.Sprintf(`%v`, item))
                } else {whereValues = append(whereValues, fmt.Sprintf(`'%v'`, item))
                }
            }
        }
        return fmt.Sprintf("%v IN(%v)", k, strings.Join(whereValues, ","))
    } else if _, err := toolbox.ToInt(v); err == nil {return fmt.Sprintf("%v = %v", k, v)
    } else {literal := strings.TrimSpace(toolbox.AsString(v))
        lowerLiteral := strings.ToLower(literal)
        if strings.Contains(literal, ">") ||
            strings.Contains(literal, "<") ||
            strings.Contains(lowerLiteral, "null") {return fmt.Sprintf("%v %v", k, v)
        }
        return fmt.Sprintf("%v ='%v'", k, v)
    }
}

ToCriterion 办法判断 v 是否是 greaterOrEqual、greaterThan、lessOrEqual、between 类型做对应的转换,针对 IsSlice 的做专门的拼接解决

小结

dbsync 的 Criterion 是一个 fmt.Stringer 类型,它内置了 greaterOrEqual、greaterThan、lessOrEqual、between 类型,并提供 ToCriterion 办法进行转换。

doc

  • dbsync

正文完
 0