package mySql import ( "fmt" "strings" ) func (t *Query) makeColName(colName string) string { //这里的colName其实还没有``包裹 //如果存在.那就是复合字段 if find := strings.Contains(colName, "."); find { countSplit := strings.SplitN(colName, ".", 2) if len(countSplit) == 2 { //因为到了where会无脑加上``包裹,所以两侧``是不需要加的 return fmt.Sprintf("%s`.`%s", countSplit[0], countSplit[1]) } } return colName } func (t *Query) WhereRaw(formula string, values ...interface{}) *Query { t.rawWheres = append(t.rawWheres, NewWhere(formula, values...)) return t } func (t *Query) Where(colName string, value interface{}) *Query { t.autoWheres = append(t.autoWheres, NewEqWhere(t.makeColName(colName), value)) return t } func (t *Query) WhereEq(colName string, value interface{}) *Query { t.autoWheres = append(t.autoWheres, NewEqWhere(t.makeColName(colName), value)) return t } func (t *Query) WhereNeq(colName string, value interface{}) *Query { t.autoWheres = append(t.autoWheres, NewNeqWhere(t.makeColName(colName), value)) return t } func (t *Query) WhereGt(colName string, value interface{}) *Query { t.autoWheres = append(t.autoWheres, NewGtWhere(t.makeColName(colName), value)) return t } func (t *Query) WhereEgt(colName string, value interface{}) *Query { t.autoWheres = append(t.autoWheres, NewEgtWhere(t.makeColName(colName), value)) return t } func (t *Query) WhereLt(colName string, value interface{}) *Query { t.autoWheres = append(t.autoWheres, NewLtWhere(t.makeColName(colName), value)) return t } func (t *Query) WhereElt(colName string, value interface{}) *Query { t.autoWheres = append(t.autoWheres, NewEltWhere(t.makeColName(colName), value)) return t } func (t *Query) WhereNotLike(colName string, value interface{}) *Query { t.autoWheres = append(t.autoWheres, NewNotLikeWhere(t.makeColName(colName), value)) return t } func (t *Query) WhereLike(colName string, value interface{}) *Query { t.autoWheres = append(t.autoWheres, NewLikeWhere(t.makeColName(colName), value)) return t } func (t *Query) WhereBetween(colName string, value1 interface{}, value2 interface{}) *Query { t.autoWheres = append(t.autoWheres, NewBetweenWhere(t.makeColName(colName), value1, value2)) return t } func (t *Query) WhereNotBetween(colName string, value1 interface{}, value2 interface{}) *Query { t.autoWheres = append(t.autoWheres, NewNotBetweenWhere(t.makeColName(colName), value1, value2)) return t } func (t *Query) WhereIn(colName string, value interface{}) *Query { t.autoWheres = append(t.autoWheres, NewInWhere(t.makeColName(colName), value)) return t } func (t *Query) WhereNotIn(colName string, value interface{}) *Query { t.autoWheres = append(t.autoWheres, NewNotInWhere(t.makeColName(colName), value)) return t } func (t *Query) WhereIsNull(colName string) *Query { t.autoWheres = append(t.autoWheres, NewIsNullWhere(t.makeColName(colName))) return t } func (t *Query) WhereIsNotNull(colName string) *Query { t.autoWheres = append(t.autoWheres, NewIsNotNullWhere(t.makeColName(colName))) return t } func (t *Query) Wheres(value map[string]interface{}) *Query { for s, i := range value { t.Where(s, i) } return t } func (t *Query) WhereBetweenFunc(colName string, f func() (start interface{}, end interface{})) *Query { start, end := f() t.WhereBetween(colName, start, end) return t }