github.com/gogf/gf@v1.16.9/database/gdb/gdb_type_result.go (about) 1 // Copyright GoFrame Author(https://goframe.org). All Rights Reserved. 2 // 3 // This Source Code Form is subject to the terms of the MIT License. 4 // If a copy of the MIT was not distributed with this file, 5 // You can obtain one at https://github.com/gogf/gf. 6 7 package gdb 8 9 import ( 10 "github.com/gogf/gf/container/gvar" 11 "github.com/gogf/gf/encoding/gparser" 12 "github.com/gogf/gf/util/gconv" 13 "math" 14 ) 15 16 // Interface converts and returns `r` as type of interface{}. 17 func (r Result) Interface() interface{} { 18 return r 19 } 20 21 // IsEmpty checks and returns whether `r` is empty. 22 func (r Result) IsEmpty() bool { 23 return r.Len() == 0 24 } 25 26 // Len returns the length of result list. 27 func (r Result) Len() int { 28 return len(r) 29 } 30 31 // Size is alias of function Len. 32 func (r Result) Size() int { 33 return r.Len() 34 } 35 36 // Chunk splits an Result into multiple Results, 37 // the size of each array is determined by `size`. 38 // The last chunk may contain less than size elements. 39 func (r Result) Chunk(size int) []Result { 40 if size < 1 { 41 return nil 42 } 43 length := len(r) 44 chunks := int(math.Ceil(float64(length) / float64(size))) 45 var n []Result 46 for i, end := 0, 0; chunks > 0; chunks-- { 47 end = (i + 1) * size 48 if end > length { 49 end = length 50 } 51 n = append(n, r[i*size:end]) 52 i++ 53 } 54 return n 55 } 56 57 // Json converts `r` to JSON format content. 58 func (r Result) Json() string { 59 content, _ := gparser.VarToJson(r.List()) 60 return string(content) 61 } 62 63 // Xml converts `r` to XML format content. 64 func (r Result) Xml(rootTag ...string) string { 65 content, _ := gparser.VarToXml(r.List(), rootTag...) 66 return string(content) 67 } 68 69 // List converts `r` to a List. 70 func (r Result) List() List { 71 list := make(List, len(r)) 72 for k, v := range r { 73 list[k] = v.Map() 74 } 75 return list 76 } 77 78 // Array retrieves and returns specified column values as slice. 79 // The parameter `field` is optional is the column field is only one. 80 // The default `field` is the first field name of the first item in `Result` if parameter `field` is not given. 81 func (r Result) Array(field ...string) []Value { 82 array := make([]Value, len(r)) 83 if len(r) == 0 { 84 return array 85 } 86 key := "" 87 if len(field) > 0 && field[0] != "" { 88 key = field[0] 89 } else { 90 for k, _ := range r[0] { 91 key = k 92 break 93 } 94 } 95 for k, v := range r { 96 array[k] = v[key] 97 } 98 return array 99 } 100 101 // MapKeyValue converts `r` to a map[string]Value of which key is specified by `key`. 102 // Note that the item value may be type of slice. 103 func (r Result) MapKeyValue(key string) map[string]Value { 104 var ( 105 s = "" 106 m = make(map[string]Value) 107 tempMap = make(map[string][]interface{}) 108 hasMultiValues bool 109 ) 110 for _, item := range r { 111 if k, ok := item[key]; ok { 112 s = k.String() 113 tempMap[s] = append(tempMap[s], item) 114 if len(tempMap[s]) > 1 { 115 hasMultiValues = true 116 } 117 } 118 } 119 for k, v := range tempMap { 120 if hasMultiValues { 121 m[k] = gvar.New(v) 122 } else { 123 m[k] = gvar.New(v[0]) 124 } 125 } 126 return m 127 } 128 129 // MapKeyStr converts `r` to a map[string]Map of which key is specified by `key`. 130 func (r Result) MapKeyStr(key string) map[string]Map { 131 m := make(map[string]Map) 132 for _, item := range r { 133 if v, ok := item[key]; ok { 134 m[v.String()] = item.Map() 135 } 136 } 137 return m 138 } 139 140 // MapKeyInt converts `r` to a map[int]Map of which key is specified by `key`. 141 func (r Result) MapKeyInt(key string) map[int]Map { 142 m := make(map[int]Map) 143 for _, item := range r { 144 if v, ok := item[key]; ok { 145 m[v.Int()] = item.Map() 146 } 147 } 148 return m 149 } 150 151 // MapKeyUint converts `r` to a map[uint]Map of which key is specified by `key`. 152 func (r Result) MapKeyUint(key string) map[uint]Map { 153 m := make(map[uint]Map) 154 for _, item := range r { 155 if v, ok := item[key]; ok { 156 m[v.Uint()] = item.Map() 157 } 158 } 159 return m 160 } 161 162 // RecordKeyStr converts `r` to a map[string]Record of which key is specified by `key`. 163 func (r Result) RecordKeyStr(key string) map[string]Record { 164 m := make(map[string]Record) 165 for _, item := range r { 166 if v, ok := item[key]; ok { 167 m[v.String()] = item 168 } 169 } 170 return m 171 } 172 173 // RecordKeyInt converts `r` to a map[int]Record of which key is specified by `key`. 174 func (r Result) RecordKeyInt(key string) map[int]Record { 175 m := make(map[int]Record) 176 for _, item := range r { 177 if v, ok := item[key]; ok { 178 m[v.Int()] = item 179 } 180 } 181 return m 182 } 183 184 // RecordKeyUint converts `r` to a map[uint]Record of which key is specified by `key`. 185 func (r Result) RecordKeyUint(key string) map[uint]Record { 186 m := make(map[uint]Record) 187 for _, item := range r { 188 if v, ok := item[key]; ok { 189 m[v.Uint()] = item 190 } 191 } 192 return m 193 } 194 195 // Structs converts `r` to struct slice. 196 // Note that the parameter `pointer` should be type of *[]struct/*[]*struct. 197 func (r Result) Structs(pointer interface{}) (err error) { 198 return gconv.StructsTag(r, pointer, OrmTagForStruct) 199 }