package db import ( . "code.hoteas.com/golang/hotime/common" ) // HotimeDBBuilder 链式查询构建器 type HotimeDBBuilder struct { HoTimeDB *HoTimeDB table string selects []interface{} join Slice where Map lastWhere Map page int pageRow int } // Table 创建链式查询构建器 func (that *HoTimeDB) Table(table string) *HotimeDBBuilder { return &HotimeDBBuilder{HoTimeDB: that, table: table, where: Map{}} } // Get 获取单条记录 func (that *HotimeDBBuilder) Get(qu ...interface{}) Map { return that.HoTimeDB.Get(that.table, that.join, qu, that.where) } // Count 统计数量 func (that *HotimeDBBuilder) Count() int { return that.HoTimeDB.Count(that.table, that.join, that.where) } // Page 设置分页 func (that *HotimeDBBuilder) Page(page, pageRow int) *HotimeDBBuilder { that.page = page that.pageRow = pageRow return that } // Select 查询多条记录 func (that *HotimeDBBuilder) Select(qu ...interface{}) []Map { if that.page != 0 { return that.HoTimeDB.Page(that.page, that.pageRow).PageSelect(that.table, that.join, qu, that.where) } return that.HoTimeDB.Select(that.table, that.join, qu, that.where) } // Update 更新记录 func (that *HotimeDBBuilder) Update(qu ...interface{}) int64 { lth := len(qu) if lth == 0 { return 0 } data := Map{} if lth == 1 { data = ObjToMap(qu[0]) } if lth > 1 { for k := 1; k < lth; k++ { data[ObjToStr(qu[k-1])] = qu[k] k++ } } return that.HoTimeDB.Update(that.table, data, that.where) } // Delete 删除记录 func (that *HotimeDBBuilder) Delete() int64 { return that.HoTimeDB.Delete(that.table, that.where) } // LeftJoin 左连接 func (that *HotimeDBBuilder) LeftJoin(table, joinStr string) *HotimeDBBuilder { that.Join(Map{"[>]" + table: joinStr}) return that } // RightJoin 右连接 func (that *HotimeDBBuilder) RightJoin(table, joinStr string) *HotimeDBBuilder { that.Join(Map{"[<]" + table: joinStr}) return that } // InnerJoin 内连接 func (that *HotimeDBBuilder) InnerJoin(table, joinStr string) *HotimeDBBuilder { that.Join(Map{"[><]" + table: joinStr}) return that } // FullJoin 全连接 func (that *HotimeDBBuilder) FullJoin(table, joinStr string) *HotimeDBBuilder { that.Join(Map{"[<>]" + table: joinStr}) return that } // Join 通用连接 func (that *HotimeDBBuilder) Join(qu ...interface{}) *HotimeDBBuilder { lth := len(qu) if lth == 0 { return that } data := Map{} if lth == 1 { data = ObjToMap(qu[0]) } if lth > 1 { for k := 1; k < lth; k++ { data[ObjToStr(qu[k-1])] = qu[k] k++ } } if that.join == nil { that.join = Slice{} } if data == nil { return that } that.join = append(that.join, data) return that } // And 添加 AND 条件 func (that *HotimeDBBuilder) And(qu ...interface{}) *HotimeDBBuilder { lth := len(qu) if lth == 0 { return that } var where Map if lth == 1 { where = ObjToMap(qu[0]) } if lth > 1 { where = Map{} for k := 1; k < lth; k++ { where[ObjToStr(qu[k-1])] = qu[k] k++ } } if where == nil { return that } if that.lastWhere != nil { that.lastWhere["AND"] = where that.lastWhere = where return that } that.lastWhere = where that.where = Map{"AND": where} return that } // Or 添加 OR 条件 func (that *HotimeDBBuilder) Or(qu ...interface{}) *HotimeDBBuilder { lth := len(qu) if lth == 0 { return that } var where Map if lth == 1 { where = ObjToMap(qu[0]) } if lth > 1 { where = Map{} for k := 1; k < lth; k++ { where[ObjToStr(qu[k-1])] = qu[k] k++ } } if where == nil { return that } if that.lastWhere != nil { that.lastWhere["OR"] = where that.lastWhere = where return that } that.lastWhere = where that.where = Map{"Or": where} return that } // Where 设置 WHERE 条件 func (that *HotimeDBBuilder) Where(qu ...interface{}) *HotimeDBBuilder { lth := len(qu) if lth == 0 { return that } var where Map if lth == 1 { where = ObjToMap(qu[0]) } if lth > 1 { where = Map{} for k := 1; k < lth; k++ { where[ObjToStr(qu[k-1])] = qu[k] k++ } } if where == nil { return that } if that.lastWhere != nil { that.lastWhere["AND"] = where that.lastWhere = where return that } that.lastWhere = where that.where = Map{"AND": that.lastWhere} return that } // From 设置表名 func (that *HotimeDBBuilder) From(table string) *HotimeDBBuilder { that.table = table return that } // Order 设置排序 func (that *HotimeDBBuilder) Order(qu ...interface{}) *HotimeDBBuilder { that.where["ORDER"] = ObjToSlice(qu) return that } // Limit 设置限制 func (that *HotimeDBBuilder) Limit(qu ...interface{}) *HotimeDBBuilder { that.where["LIMIT"] = ObjToSlice(qu) return that } // Group 设置分组 func (that *HotimeDBBuilder) Group(qu ...interface{}) *HotimeDBBuilder { that.where["GROUP"] = ObjToSlice(qu) return that } // Having 设置 HAVING 条件 func (that *HotimeDBBuilder) Having(qu ...interface{}) *HotimeDBBuilder { lth := len(qu) if lth == 0 { return that } var having Map if lth == 1 { having = ObjToMap(qu[0]) } if lth > 1 { having = Map{} for k := 1; k < lth; k++ { having[ObjToStr(qu[k-1])] = qu[k] k++ } } that.where["HAVING"] = having return that } // Offset 设置偏移量 func (that *HotimeDBBuilder) Offset(offset int) *HotimeDBBuilder { that.where["OFFSET"] = offset return that }