mirror of
https://gitee.com/johng/gf.git
synced 2024-11-30 11:18:02 +08:00
add new functions and mark deprecated for some functions for gdb
This commit is contained in:
parent
c8cf46a5a7
commit
195cae6577
@ -557,7 +557,7 @@ func (md *Model) Struct(pointer interface{}) error {
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return one.ToStruct(pointer)
|
||||
return one.Struct(pointer)
|
||||
}
|
||||
|
||||
// 链式操作,查询多条记录,并自动转换为指定的slice对象, 如: []struct/[]*struct。
|
||||
@ -566,7 +566,7 @@ func (md *Model) Structs(pointer interface{}) error {
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return r.ToStructs(pointer)
|
||||
return r.Structs(pointer)
|
||||
}
|
||||
|
||||
// 链式操作,将结果转换为指定的struct/*struct/[]struct/[]*struct,
|
||||
|
@ -8,24 +8,25 @@ package gdb
|
||||
|
||||
import (
|
||||
"database/sql"
|
||||
"github.com/gogf/gf/container/gmap"
|
||||
|
||||
"github.com/gogf/gf/encoding/gparser"
|
||||
)
|
||||
|
||||
// 将记录结果转换为JSON字符串
|
||||
func (r Record) ToJson() string {
|
||||
func (r Record) Json() string {
|
||||
content, _ := gparser.VarToJson(r.ToMap())
|
||||
return string(content)
|
||||
}
|
||||
|
||||
// 将记录结果转换为XML字符串
|
||||
func (r Record) ToXml(rootTag ...string) string {
|
||||
func (r Record) Xml(rootTag ...string) string {
|
||||
content, _ := gparser.VarToXml(r.ToMap(), rootTag...)
|
||||
return string(content)
|
||||
}
|
||||
|
||||
// 将Record转换为Map,其中最主要的区别是里面的键值被强制转换为string类型,方便json处理
|
||||
func (r Record) ToMap() Map {
|
||||
func (r Record) Map() Map {
|
||||
m := make(map[string]interface{})
|
||||
for k, v := range r {
|
||||
m[k] = v.Val()
|
||||
@ -33,10 +34,15 @@ func (r Record) ToMap() Map {
|
||||
return m
|
||||
}
|
||||
|
||||
// 将Record转换为gmap.StrAnyMap类型
|
||||
func (r Record) GMap() *gmap.StrAnyMap {
|
||||
return gmap.NewStrAnyMapFrom(r.Map())
|
||||
}
|
||||
|
||||
// 将Map变量映射到指定的struct对象中,注意参数应当是一个对象的指针
|
||||
func (r Record) ToStruct(pointer interface{}) error {
|
||||
func (r Record) Struct(pointer interface{}) error {
|
||||
if r == nil {
|
||||
return sql.ErrNoRows
|
||||
}
|
||||
return mapToStruct(r.ToMap(), pointer)
|
||||
return mapToStruct(r.Map(), pointer)
|
||||
}
|
||||
|
41
database/gdb/gdb_type_record_deprecated.go
Normal file
41
database/gdb/gdb_type_record_deprecated.go
Normal file
@ -0,0 +1,41 @@
|
||||
// Copyright 2018 gf Author(https://github.com/gogf/gf). All Rights Reserved.
|
||||
//
|
||||
// This Source Code Form is subject to the terms of the MIT License.
|
||||
// If a copy of the MIT was not distributed with this file,
|
||||
// You can obtain one at https://github.com/gogf/gf.
|
||||
|
||||
package gdb
|
||||
|
||||
import (
|
||||
"database/sql"
|
||||
"github.com/gogf/gf/encoding/gparser"
|
||||
)
|
||||
|
||||
// Deprecated.
|
||||
func (r Record) ToJson() string {
|
||||
content, _ := gparser.VarToJson(r.ToMap())
|
||||
return string(content)
|
||||
}
|
||||
|
||||
// Deprecated.
|
||||
func (r Record) ToXml(rootTag ...string) string {
|
||||
content, _ := gparser.VarToXml(r.ToMap(), rootTag...)
|
||||
return string(content)
|
||||
}
|
||||
|
||||
// Deprecated.
|
||||
func (r Record) ToMap() Map {
|
||||
m := make(map[string]interface{})
|
||||
for k, v := range r {
|
||||
m[k] = v.Val()
|
||||
}
|
||||
return m
|
||||
}
|
||||
|
||||
// Deprecated.
|
||||
func (r Record) ToStruct(pointer interface{}) error {
|
||||
if r == nil {
|
||||
return sql.ErrNoRows
|
||||
}
|
||||
return mapToStruct(r.ToMap(), pointer)
|
||||
}
|
@ -15,61 +15,61 @@ import (
|
||||
)
|
||||
|
||||
// 将结果集转换为JSON字符串
|
||||
func (r Result) ToJson() string {
|
||||
content, _ := gparser.VarToJson(r.ToList())
|
||||
func (r Result) Json() string {
|
||||
content, _ := gparser.VarToJson(r.List())
|
||||
return string(content)
|
||||
}
|
||||
|
||||
// 将结果集转换为XML字符串
|
||||
func (r Result) ToXml(rootTag ...string) string {
|
||||
content, _ := gparser.VarToXml(r.ToList(), rootTag...)
|
||||
func (r Result) Xml(rootTag ...string) string {
|
||||
content, _ := gparser.VarToXml(r.List(), rootTag...)
|
||||
return string(content)
|
||||
}
|
||||
|
||||
// 将结果集转换为List类型返回,便于json处理
|
||||
func (r Result) ToList() List {
|
||||
func (r Result) List() List {
|
||||
l := make(List, len(r))
|
||||
for k, v := range r {
|
||||
l[k] = v.ToMap()
|
||||
l[k] = v.Map()
|
||||
}
|
||||
return l
|
||||
}
|
||||
|
||||
// 将结果列表按照指定的字段值做map[string]Map
|
||||
func (r Result) ToStringMap(key string) map[string]Map {
|
||||
func (r Result) MapKeyStr(key string) map[string]Map {
|
||||
m := make(map[string]Map)
|
||||
for _, item := range r {
|
||||
if v, ok := item[key]; ok {
|
||||
m[v.String()] = item.ToMap()
|
||||
m[v.String()] = item.Map()
|
||||
}
|
||||
}
|
||||
return m
|
||||
}
|
||||
|
||||
// 将结果列表按照指定的字段值做map[int]Map
|
||||
func (r Result) ToIntMap(key string) map[int]Map {
|
||||
func (r Result) MapKeyInt(key string) map[int]Map {
|
||||
m := make(map[int]Map)
|
||||
for _, item := range r {
|
||||
if v, ok := item[key]; ok {
|
||||
m[v.Int()] = item.ToMap()
|
||||
m[v.Int()] = item.Map()
|
||||
}
|
||||
}
|
||||
return m
|
||||
}
|
||||
|
||||
// 将结果列表按照指定的字段值做map[uint]Map
|
||||
func (r Result) ToUintMap(key string) map[uint]Map {
|
||||
func (r Result) MapKeyUint(key string) map[uint]Map {
|
||||
m := make(map[uint]Map)
|
||||
for _, item := range r {
|
||||
if v, ok := item[key]; ok {
|
||||
m[v.Uint()] = item.ToMap()
|
||||
m[v.Uint()] = item.Map()
|
||||
}
|
||||
}
|
||||
return m
|
||||
}
|
||||
|
||||
// 将结果列表按照指定的字段值做map[string]Record
|
||||
func (r Result) ToStringRecord(key string) map[string]Record {
|
||||
func (r Result) RecordKeyStr(key string) map[string]Record {
|
||||
m := make(map[string]Record)
|
||||
for _, item := range r {
|
||||
if v, ok := item[key]; ok {
|
||||
@ -80,7 +80,7 @@ func (r Result) ToStringRecord(key string) map[string]Record {
|
||||
}
|
||||
|
||||
// 将结果列表按照指定的字段值做map[int]Record
|
||||
func (r Result) ToIntRecord(key string) map[int]Record {
|
||||
func (r Result) RecordKeyInt(key string) map[int]Record {
|
||||
m := make(map[int]Record)
|
||||
for _, item := range r {
|
||||
if v, ok := item[key]; ok {
|
||||
@ -91,7 +91,7 @@ func (r Result) ToIntRecord(key string) map[int]Record {
|
||||
}
|
||||
|
||||
// 将结果列表按照指定的字段值做map[uint]Record
|
||||
func (r Result) ToUintRecord(key string) map[uint]Record {
|
||||
func (r Result) RecordKeyUint(key string) map[uint]Record {
|
||||
m := make(map[uint]Record)
|
||||
for _, item := range r {
|
||||
if v, ok := item[key]; ok {
|
||||
@ -102,7 +102,7 @@ func (r Result) ToUintRecord(key string) map[uint]Record {
|
||||
}
|
||||
|
||||
// 将结果列表转换为指定对象的slice。
|
||||
func (r Result) ToStructs(pointer interface{}) (err error) {
|
||||
func (r Result) Structs(pointer interface{}) (err error) {
|
||||
l := len(r)
|
||||
if l == 0 {
|
||||
return sql.ErrNoRows
|
||||
@ -116,13 +116,13 @@ func (r Result) ToStructs(pointer interface{}) (err error) {
|
||||
for i := 0; i < l; i++ {
|
||||
if itemType.Kind() == reflect.Ptr {
|
||||
e := reflect.New(itemType.Elem()).Elem()
|
||||
if err = r[i].ToStruct(e); err != nil {
|
||||
if err = r[i].Struct(e); err != nil {
|
||||
return err
|
||||
}
|
||||
array.Index(i).Set(e.Addr())
|
||||
} else {
|
||||
e := reflect.New(itemType).Elem()
|
||||
if err = r[i].ToStruct(e); err != nil {
|
||||
if err = r[i].Struct(e); err != nil {
|
||||
return err
|
||||
}
|
||||
array.Index(i).Set(e)
|
||||
|
133
database/gdb/gdb_type_result_deprecated.go
Normal file
133
database/gdb/gdb_type_result_deprecated.go
Normal file
@ -0,0 +1,133 @@
|
||||
// Copyright 2018 gf Author(https://github.com/gogf/gf). All Rights Reserved.
|
||||
//
|
||||
// This Source Code Form is subject to the terms of the MIT License.
|
||||
// If a copy of the MIT was not distributed with this file,
|
||||
// You can obtain one at https://github.com/gogf/gf.
|
||||
|
||||
package gdb
|
||||
|
||||
import (
|
||||
"database/sql"
|
||||
"fmt"
|
||||
"reflect"
|
||||
|
||||
"github.com/gogf/gf/encoding/gparser"
|
||||
)
|
||||
|
||||
// Deprecated.
|
||||
func (r Result) ToJson() string {
|
||||
content, _ := gparser.VarToJson(r.ToList())
|
||||
return string(content)
|
||||
}
|
||||
|
||||
// Deprecated.
|
||||
func (r Result) ToXml(rootTag ...string) string {
|
||||
content, _ := gparser.VarToXml(r.ToList(), rootTag...)
|
||||
return string(content)
|
||||
}
|
||||
|
||||
// Deprecated.
|
||||
func (r Result) ToList() List {
|
||||
l := make(List, len(r))
|
||||
for k, v := range r {
|
||||
l[k] = v.ToMap()
|
||||
}
|
||||
return l
|
||||
}
|
||||
|
||||
// Deprecated.
|
||||
func (r Result) ToStringMap(key string) map[string]Map {
|
||||
m := make(map[string]Map)
|
||||
for _, item := range r {
|
||||
if v, ok := item[key]; ok {
|
||||
m[v.String()] = item.ToMap()
|
||||
}
|
||||
}
|
||||
return m
|
||||
}
|
||||
|
||||
// Deprecated.
|
||||
func (r Result) ToIntMap(key string) map[int]Map {
|
||||
m := make(map[int]Map)
|
||||
for _, item := range r {
|
||||
if v, ok := item[key]; ok {
|
||||
m[v.Int()] = item.ToMap()
|
||||
}
|
||||
}
|
||||
return m
|
||||
}
|
||||
|
||||
// Deprecated.
|
||||
func (r Result) ToUintMap(key string) map[uint]Map {
|
||||
m := make(map[uint]Map)
|
||||
for _, item := range r {
|
||||
if v, ok := item[key]; ok {
|
||||
m[v.Uint()] = item.ToMap()
|
||||
}
|
||||
}
|
||||
return m
|
||||
}
|
||||
|
||||
// Deprecated.
|
||||
func (r Result) ToStringRecord(key string) map[string]Record {
|
||||
m := make(map[string]Record)
|
||||
for _, item := range r {
|
||||
if v, ok := item[key]; ok {
|
||||
m[v.String()] = item
|
||||
}
|
||||
}
|
||||
return m
|
||||
}
|
||||
|
||||
// Deprecated.
|
||||
func (r Result) ToIntRecord(key string) map[int]Record {
|
||||
m := make(map[int]Record)
|
||||
for _, item := range r {
|
||||
if v, ok := item[key]; ok {
|
||||
m[v.Int()] = item
|
||||
}
|
||||
}
|
||||
return m
|
||||
}
|
||||
|
||||
// Deprecated.
|
||||
func (r Result) ToUintRecord(key string) map[uint]Record {
|
||||
m := make(map[uint]Record)
|
||||
for _, item := range r {
|
||||
if v, ok := item[key]; ok {
|
||||
m[v.Uint()] = item
|
||||
}
|
||||
}
|
||||
return m
|
||||
}
|
||||
|
||||
// Deprecated.
|
||||
func (r Result) ToStructs(pointer interface{}) (err error) {
|
||||
l := len(r)
|
||||
if l == 0 {
|
||||
return sql.ErrNoRows
|
||||
}
|
||||
t := reflect.TypeOf(pointer)
|
||||
if t.Kind() != reflect.Ptr {
|
||||
return fmt.Errorf("pointer should be type of pointer, but got: %v", t.Kind())
|
||||
}
|
||||
array := reflect.MakeSlice(t.Elem(), l, l)
|
||||
itemType := array.Index(0).Type()
|
||||
for i := 0; i < l; i++ {
|
||||
if itemType.Kind() == reflect.Ptr {
|
||||
e := reflect.New(itemType.Elem()).Elem()
|
||||
if err = r[i].ToStruct(e); err != nil {
|
||||
return err
|
||||
}
|
||||
array.Index(i).Set(e.Addr())
|
||||
} else {
|
||||
e := reflect.New(itemType).Elem()
|
||||
if err = r[i].ToStruct(e); err != nil {
|
||||
return err
|
||||
}
|
||||
array.Index(i).Set(e)
|
||||
}
|
||||
}
|
||||
reflect.ValueOf(pointer).Elem().Set(array)
|
||||
return nil
|
||||
}
|
Loading…
Reference in New Issue
Block a user