diff --git a/db/hotimedb.go b/db/hotimedb.go index c35be85..41a66de 100644 --- a/db/hotimedb.go +++ b/db/hotimedb.go @@ -263,7 +263,7 @@ func (that *HoTimeDB) backupCol(tt string) string { for k := range data[0] { if tempLthData == lthCol-1 { - str += "`" + k + "`)" + str += "`" + k + "`) " } else { str += "`" + k + "`," } @@ -287,7 +287,7 @@ func (that *HoTimeDB) backupCol(tt string) string { } if m == lthCol-1 { - str += v + ")" + str += v + ") " } else { str += v + "," @@ -454,9 +454,9 @@ func (that *HoTimeDB) Select(table string, qu ...interface{}) []Map { query += " *" } if !strings.Contains(table, ".") && !strings.Contains(table, " AS ") { - query += " FROM `" + that.Prefix + table + "`" + query += " FROM `" + that.Prefix + table + "` " } else { - query += " FROM " + that.Prefix + table + query += " FROM " + that.Prefix + table + " " } if join { @@ -472,15 +472,15 @@ func (that *HoTimeDB) Select(table string, qu ...interface{}) []Map { v := testQuData[k] switch Substr(k, 0, 3) { case "[>]": - query += " LEFT JOIN `" + Substr(k, 3, len(k)-3) + "` ON " + v.(string) + query += " LEFT JOIN `" + Substr(k, 3, len(k)-3) + "` ON " + v.(string) + " " case "[<]": - query += " RIGHT JOIN `" + Substr(k, 3, len(k)-3) + "` ON " + v.(string) + query += " RIGHT JOIN `" + Substr(k, 3, len(k)-3) + "` ON " + v.(string) + " " } switch Substr(k, 0, 4) { case "[<>]": - query += " FULL JOIN `" + Substr(k, 4, len(k)-4) + "` ON " + v.(string) + query += " FULL JOIN `" + Substr(k, 4, len(k)-4) + "` ON " + v.(string) + " " case "[><]": - query += " INNER JOIN `" + Substr(k, 4, len(k)-4) + "` ON " + v.(string) + query += " INNER JOIN `" + Substr(k, 4, len(k)-4) + "` ON " + v.(string) + " " } } } @@ -631,7 +631,16 @@ func (that *HoTimeDB) where(data Map) (string, []interface{}) { } if len(where) != 0 { - where = " WHERE " + where + hasWhere := true + for _, v := range vcond { + if strings.Index(where, v) == 0 { + hasWhere = false + } + } + + if hasWhere { + where = " WHERE " + where + " " + } } //特殊字符 @@ -647,31 +656,31 @@ func (that *HoTimeDB) where(data Map) (string, []interface{}) { v := data[k] if vcond[j] == k { if k == "ORDER" { - where += " " + k + " BY " + where += k + " BY " //fmt.Println(reflect.ValueOf(v).Type()) //break } else if k == "GROUP" { - where += " " + k + " BY " + where += k + " BY " } else { - where += " " + k + where += k } if reflect.ValueOf(v).Type().String() == "common.Slice" { for i := 0; i < len(v.(Slice)); i++ { - where += " " + ObjToStr(v.(Slice)[i]) + where += " " + ObjToStr(v.(Slice)[i]) + " " if len(v.(Slice)) != i+1 { - where += "," + where += ", " } } } else { //fmt.Println(v) - where += " " + ObjToStr(v) + where += " " + ObjToStr(v) + " " } break @@ -699,72 +708,72 @@ func (that *HoTimeDB) varCond(k string, v interface{}) (string, []interface{}) { case "[>]": k = strings.Replace(k, "[>]", "", -1) if !strings.Contains(k, ".") { - k = "`" + k + "`" + k = "`" + k + "` " } where += k + ">? " res = append(res, v) case "[<]": k = strings.Replace(k, "[<]", "", -1) if !strings.Contains(k, ".") { - k = "`" + k + "`" + k = "`" + k + "` " } where += k + "b" where += " " + ObjToStr(v) case "[#!]": k = strings.Replace(k, "[#!]", "", -1) if !strings.Contains(k, ".") { - k = "`" + k + "`" + k = "`" + k + "` " } - where += " " + k + "!=" + ObjToStr(v) + where += " " + k + "!=" + ObjToStr(v) + " " case "[!#]": k = strings.Replace(k, "[!#]", "", -1) if !strings.Contains(k, ".") { - k = "`" + k + "`" + k = "`" + k + "` " } - where += " " + k + "!=" + ObjToStr(v) + where += " " + k + "!=" + ObjToStr(v) + " " case "[~]": k = strings.Replace(k, "[~]", "", -1) if !strings.Contains(k, ".") { - k = "`" + k + "`" + k = "`" + k + "` " } where += k + " LIKE ? " - v = "%" + ObjToStr(v) + "%" + v = "%" + ObjToStr(v) + "% " res = append(res, v) case "[!~]": //左边任意 k = strings.Replace(k, "[~]", "", -1) if !strings.Contains(k, ".") { - k = "`" + k + "`" + k = "`" + k + "` " } where += k + " LIKE ? " - v = "%" + ObjToStr(v) + v = "%" + ObjToStr(v) + " " res = append(res, v) case "[~!]": //右边任意 k = strings.Replace(k, "[~]", "", -1) if !strings.Contains(k, ".") { - k = "`" + k + "`" + k = "`" + k + "` " } where += k + " LIKE ? " - v = ObjToStr(v) + "%" + v = ObjToStr(v) + "% " res = append(res, v) case "[~~]": //手动任意 k = strings.Replace(k, "[~]", "", -1) if !strings.Contains(k, ".") { - k = "`" + k + "`" + k = "`" + k + "` " } where += k + " LIKE ? " //v = ObjToStr(v) @@ -778,21 +787,21 @@ func (that *HoTimeDB) varCond(k string, v interface{}) (string, []interface{}) { case "[>=]": k = strings.Replace(k, "[>=]", "", -1) if !strings.Contains(k, ".") { - k = "`" + k + "`" + k = "`" + k + "` " } where += k + ">=? " res = append(res, v) case "[<=]": k = strings.Replace(k, "[<=]", "", -1) if !strings.Contains(k, ".") { - k = "`" + k + "`" + k = "`" + k + "` " } where += k + "<=? " res = append(res, v) case "[><]": k = strings.Replace(k, "[><]", "", -1) if !strings.Contains(k, ".") { - k = "`" + k + "`" + k = "`" + k + "` " } where += k + " NOT BETWEEN ? AND ? " res = append(res, v.(Slice)[0]) @@ -800,30 +809,31 @@ func (that *HoTimeDB) varCond(k string, v interface{}) (string, []interface{}) { case "[<>]": k = strings.Replace(k, "[<>]", "", -1) if !strings.Contains(k, ".") { - k = "`" + k + "`" + k = "`" + k + "` " } where += k + " BETWEEN ? AND ? " res = append(res, v.(Slice)[0]) res = append(res, v.(Slice)[1]) default: if !strings.Contains(k, ".") { - k = "`" + k + "`" + k = "`" + k + "` " } if reflect.ValueOf(v).Type().String() == "common.Slice" || strings.Contains(reflect.ValueOf(v).Type().String(), "[]") { vs := ObjToSlice(v) + if len(vs) == 0 { + return where, res + } + where += k + " IN (" res = append(res, vs...) - if len(v.(Slice)) == 0 { - where += ") " - } else { - for i := 0; i < len(vs); i++ { - if i+1 != len(vs) { - where += "?," - } else { - where += "?) " - } - //res=append(res,(v.(Slice))[i]) + + for i := 0; i < len(vs); i++ { + if i+1 != len(vs) { + where += "?," + } else { + where += "?) " } + //res=append(res,(v.(Slice))[i]) } } else { @@ -837,15 +847,19 @@ func (that *HoTimeDB) varCond(k string, v interface{}) (string, []interface{}) { } else { //fmt.Println(reflect.ValueOf(v).Type().String()) if !strings.Contains(k, ".") { - k = "`" + k + "`" + k = "`" + k + "` " } if v == nil { - where += k + " IS NULL" + where += k + " IS NULL " } else if reflect.ValueOf(v).Type().String() == "common.Slice" || strings.Contains(reflect.ValueOf(v).Type().String(), "[]") { vs := ObjToSlice(v) //fmt.Println(v) + if len(vs) == 0 { + return where, res + } where += k + " IN (" res = append(res, vs...) + for i := 0; i < len(vs); i++ { if i+1 != len(vs) { where += "?," @@ -854,6 +868,7 @@ func (that *HoTimeDB) varCond(k string, v interface{}) (string, []interface{}) { } //res=append(res,(v.(Slice))[i]) } + } else { where += k + "=? " @@ -875,8 +890,12 @@ func (that *HoTimeDB) notIn(k string, v interface{}, where string, res []interfa } else if reflect.ValueOf(v).Type().String() == "common.Slice" || strings.Contains(reflect.ValueOf(v).Type().String(), "[]") { vs := ObjToSlice(v) + if len(vs) == 0 { + return where, res + } where += k + " NOT IN (" res = append(res, vs...) + for i := 0; i < len(vs); i++ { if i+1 != len(vs) { where += "?," @@ -885,6 +904,7 @@ func (that *HoTimeDB) notIn(k string, v interface{}, where string, res []interfa } //res=append(res,(v.(Slice))[i]) } + } else { where += k + " !=? " @@ -931,7 +951,10 @@ func (that *HoTimeDB) cond(tag string, data Map) (string, []interface{}) { if x == len(condition) { tv, vv := that.varCond(k, v) - + if tv == "" { + lens-- + continue + } res = append(res, vv...) if lens--; lens <= 0 { where += tv + "" @@ -962,7 +985,7 @@ func (that *HoTimeDB) Update(table string, data Map, where Map) int64 { } else { qs = append(qs, v) } - query += "`" + k + "`=" + vstr + "" + query += "`" + k + "`=" + vstr + " " if tp--; tp != 0 { query += ", " }