github.com/kotovmak/go-admin@v1.1.1/modules/db/converter.go (about) 1 // Copyright 2019 GoAdmin Core Team. All rights reserved. 2 // Use of this source code is governed by a Apache-2.0 style 3 // license that can be found in the LICENSE file. 4 5 package db 6 7 import ( 8 "database/sql" 9 ) 10 11 // SetColVarType set the column type. 12 func SetColVarType(colVar *[]interface{}, i int, typeName string) { 13 dt := DT(typeName) 14 switch { 15 case Contains(dt, BoolTypeList): 16 var s sql.NullBool 17 (*colVar)[i] = &s 18 case Contains(dt, IntTypeList): 19 var s sql.NullInt64 20 (*colVar)[i] = &s 21 case Contains(dt, FloatTypeList): 22 var s sql.NullFloat64 23 (*colVar)[i] = &s 24 case Contains(dt, UintTypeList): 25 var s []uint8 26 (*colVar)[i] = &s 27 case Contains(dt, StringTypeList): 28 var s sql.NullString 29 (*colVar)[i] = &s 30 default: 31 var s interface{} 32 (*colVar)[i] = &s 33 } 34 } 35 36 // SetResultValue set the result value. 37 func SetResultValue(result *map[string]interface{}, index string, colVar interface{}, typeName string) { 38 dt := DT(typeName) 39 switch { 40 case Contains(dt, BoolTypeList): 41 temp := *(colVar.(*sql.NullBool)) 42 if temp.Valid { 43 (*result)[index] = temp.Bool 44 } else { 45 (*result)[index] = nil 46 } 47 case Contains(dt, IntTypeList): 48 temp := *(colVar.(*sql.NullInt64)) 49 if temp.Valid { 50 (*result)[index] = temp.Int64 51 } else { 52 (*result)[index] = nil 53 } 54 case Contains(dt, FloatTypeList): 55 temp := *(colVar.(*sql.NullFloat64)) 56 if temp.Valid { 57 (*result)[index] = temp.Float64 58 } else { 59 (*result)[index] = nil 60 } 61 case Contains(dt, UintTypeList): 62 (*result)[index] = *(colVar.(*[]uint8)) 63 case Contains(dt, StringTypeList): 64 temp := *(colVar.(*sql.NullString)) 65 if temp.Valid { 66 (*result)[index] = temp.String 67 } else { 68 (*result)[index] = nil 69 } 70 default: 71 if colVar2, ok := colVar.(*interface{}); ok { 72 if colVar, ok = (*colVar2).(int64); ok { 73 (*result)[index] = colVar 74 } else if colVar, ok = (*colVar2).(string); ok { 75 (*result)[index] = colVar 76 } else if colVar, ok = (*colVar2).(float64); ok { 77 (*result)[index] = colVar 78 } else if colVar, ok = (*colVar2).([]uint8); ok { 79 (*result)[index] = colVar 80 } else { 81 (*result)[index] = colVar 82 } 83 } 84 } 85 }