专栏首页码匠的流水账聊聊dbsync的Criterion
原创

聊聊dbsync的Criterion

本文主要研究一下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

原创声明,本文系作者授权云+社区发表,未经许可,不得转载。

如有侵权,请联系 yunjia_community@tencent.com 删除。

我来说两句

0 条评论
登录 后参与评论

相关文章

  • 聊聊dbsync的Criterion

    dbsync的Criterion是一个fmt.Stringer类型,它内置了greaterOrEqual、greaterThan、lessOrEqual、bet...

    codecraft
  • 聊聊dbsync的jobs

    dbsync的Schedulable定义了URL、ID、*contract.Sync、Schedule、Status、status属性,它提供了Clone、Do...

    codecraft
  • 聊聊dbsync的Schedulable

    dbsync的Schedulable定义了URL、ID、*contract.Sync、Schedule、Status、status属性,它提供了Clone、Do...

    codecraft
  • 聊聊dbsync的jobs

    dbsync的Schedulable定义了URL、ID、*contract.Sync、Schedule、Status、status属性,它提供了Clone、Do...

    codecraft
  • 聊聊dbsync的Schedulable

    dbsync的Schedulable定义了URL、ID、*contract.Sync、Schedule、Status、status属性,它提供了Clone、Do...

    codecraft
  • 自己动手写个聊天机器人吧

    学习来源于Sirajology的视频 Build a Chatbot 昨天写LSTM的时候提到了聊天机器人,今天放松一下,来看看chatrobot是如何实现的。...

    杨熹
  • 聊聊flink的TimeCharacteristic

    flink-streaming-java_2.11-1.7.0-sources.jar!/org/apache/flink/streaming/api/Time...

    codecraft
  • 聊聊flink的SourceFunction

    flink-streaming-java_2.11-1.6.2-sources.jar!/org/apache/flink/streaming/api/func...

    codecraft
  • 聊聊flink的CsvReader

    flink-java-1.6.2-sources.jar!/org/apache/flink/api/java/ExecutionEnvironment.jav...

    codecraft
  • 聊聊flink的BoltWrapper

    flink-storm_2.11-1.6.2-sources.jar!/org/apache/flink/storm/wrappers/BoltWrapper....

    codecraft
  • 聊聊flink的EventTime

    flink-streaming-java_2.11-1.7.0-sources.jar!/org/apache/flink/streaming/api/func...

    codecraft
  • 聊聊flink的ParallelIteratorInputFormat

    本文主要研究一下flink的ParallelIteratorInputFormat

    codecraft
  • 聊聊flink的RichParallelSourceFunction

    flink-streaming-java_2.11-1.6.2-sources.jar!/org/apache/flink/streaming/api/func...

    codecraft
  • 聊聊flink的InputFormatSourceFunction

    flink-streaming-java_2.11-1.6.2-sources.jar!/org/apache/flink/streaming/api/envi...

    codecraft
  • 聊聊flink的MemoryStateBackend

    flink-runtime_2.11-1.7.0-sources.jar!/org/apache/flink/runtime/state/StateBacken...

    codecraft
  • 聊聊flink的OperatorStateBackend

    flink-runtime_2.11-1.7.0-sources.jar!/org/apache/flink/runtime/state/OperatorSta...

    codecraft
  • 聊聊flink的MemCheckpointStreamFactory

    flink-runtime_2.11-1.7.0-sources.jar!/org/apache/flink/runtime/state/CheckpointS...

    codecraft
  • 聊聊storm的OpaquePartitionedTridentSpoutExecutor

    本文主要研究一下storm的OpaquePartitionedTridentSpoutExecutor

    codecraft
  • 聊聊storm的ICommitterTridentSpout

    storm-core-1.2.2-sources.jar!/org/apache/storm/trident/spout/ICommitterTridentSp...

    codecraft

扫码关注云+社区

领取腾讯云代金券