github.com/team-ide/go-dialect@v1.9.20/worker/util.go (about) 1 package worker 2 3 import ( 4 "database/sql" 5 "github.com/team-ide/go-dialect/dialect" 6 "os" 7 "reflect" 8 "time" 9 ) 10 11 func PathExists(path string) (bool, error) { 12 _, err := os.Stat(path) 13 if err == nil { 14 return true, nil 15 } 16 if os.IsNotExist(err) { 17 return false, nil 18 } 19 return false, err 20 } 21 22 func PathIsDir(path string) (bool, error) { 23 fileInfo, err := os.Stat(path) 24 if err != nil { 25 return false, err 26 } 27 return fileInfo.IsDir(), nil 28 } 29 30 // NowTime 获取当前时间戳 31 func NowTime() int64 { 32 return GetTime(Now()) 33 } 34 35 // GetTime 获取当前时间戳 36 func GetTime(time time.Time) int64 { 37 return time.UnixNano() / 1e6 38 } 39 40 // Now 获取当前时间 41 func Now() time.Time { 42 return time.Now() 43 } 44 45 func GetSqlValueCache(columnTypes []*sql.ColumnType) (cache []interface{}) { 46 cache = make([]interface{}, len(columnTypes)) //临时存储每行数据 47 for index, _ := range cache { 48 cache[index] = new(interface{}) 49 //columnType := columnTypes[index] 50 //ct := columnType.ScanType() 51 //if ct == nil { 52 // cache[index] = new(sql.NullString) 53 // continue 54 //} 55 //println("GetSqlValueCache type [" + columnType.ScanType().String() + "] columnName [" + columnType.Name() + "] databaseType [" + columnType.DatabaseTypeName() + "]") 56 //switch ct.String() { 57 //case "sql.NullString": 58 // cache[index] = new(sql.NullString) 59 // break 60 //case "sql.NullBool": 61 // cache[index] = new(sql.NullBool) 62 // break 63 //case "sql.NullByte": 64 // cache[index] = new(sql.NullByte) 65 // break 66 //case "sql.NullInt16": 67 // cache[index] = new(sql.NullInt16) 68 // break 69 //case "sql.NullInt32": 70 // cache[index] = new(sql.NullInt32) 71 // break 72 //case "sql.NullInt64": 73 // cache[index] = new(sql.NullInt64) 74 // break 75 //case "sql.NullFloat64": 76 // cache[index] = new(sql.NullFloat64) 77 // break 78 //case "sql.NullTime": 79 // cache[index] = new(sql.NullTime) 80 // break 81 //case "sql.RawBytes": 82 // cache[index] = new(sql.RawBytes) 83 // break 84 //default: 85 // cache[index] = new(interface{}) 86 // //panic("GetSqlValueCache type [" + columnType.ScanType().String() + "] columnName [" + columnType.Name() + "] databaseType [" + columnType.DatabaseTypeName() + "] not support") 87 // break 88 //} 89 } 90 return 91 } 92 93 func GetSqlValue(columnType *sql.ColumnType, data interface{}) (value interface{}) { 94 if data == nil { 95 return 96 } 97 typeName := reflect.TypeOf(data).String() 98 if typeName == "*dm.DmClob" { 99 typeV := reflect.ValueOf(data) 100 method := typeV.MethodByName("GetLength") 101 vs := method.Call([]reflect.Value{}) 102 if vs[1].Interface() == nil { 103 length := vs[0].Int() 104 method = typeV.MethodByName("ReadString") 105 vs = method.Call([]reflect.Value{reflect.ValueOf(1), reflect.ValueOf(int(length))}) 106 value = vs[0].String() 107 } 108 return 109 } else if typeName == "*dm.DmBlob" { 110 typeV := reflect.ValueOf(data) 111 method := typeV.MethodByName("Read") 112 var bs []byte 113 var readBs = make([]byte, 1024*1024) 114 for { 115 vs := method.Call([]reflect.Value{reflect.ValueOf(readBs)}) 116 n := vs[0].Int() 117 if n > 0 { 118 bs = append(bs, readBs[0:0]...) 119 } 120 if vs[1].Interface() != nil { 121 break 122 } 123 } 124 125 value = string(bs) 126 return 127 } else if typeName == "godror.Number" { 128 typeV := reflect.ValueOf(data) 129 method := typeV.MethodByName("String") 130 vs := method.Call([]reflect.Value{}) 131 value = vs[0].String() 132 return 133 } 134 vOf := reflect.ValueOf(data) 135 if vOf.Kind() == reflect.Ptr { 136 if vOf.IsNil() { 137 return nil 138 } 139 return GetSqlValue(columnType, vOf.Elem().Interface()) 140 } 141 //if columnType.Name() == "NESTING_EVENT_TYPE" { 142 // fmt.Println("NESTING_EVENT_TYPE value type", reflect.TypeOf(data).String(), " value is ", data) 143 //} 144 switch v := data.(type) { 145 case sql.NullString: 146 if !v.Valid { 147 return nil 148 } 149 value = (v).String 150 break 151 case sql.NullBool: 152 if !v.Valid { 153 return nil 154 } 155 value = (v).Bool 156 break 157 case sql.NullByte: 158 if !v.Valid { 159 return nil 160 } 161 value = (v).Byte 162 break 163 case sql.NullFloat64: 164 if !v.Valid { 165 return nil 166 } 167 value = (v).Float64 168 break 169 case sql.NullInt16: 170 if !v.Valid { 171 return nil 172 } 173 value = (v).Int16 174 break 175 case sql.NullInt32: 176 if !v.Valid { 177 return nil 178 } 179 value = (v).Int32 180 break 181 case sql.NullInt64: 182 if !v.Valid { 183 return nil 184 } 185 value = (v).Int64 186 break 187 case sql.NullTime: 188 if !v.Valid { 189 return nil 190 } 191 value = (v).Time 192 break 193 case sql.RawBytes: 194 value = string(v) 195 break 196 case []uint8: 197 value = string(v) 198 break 199 case string, int, int8, int16, int32, int64, float32, float64, bool, uint, uint8, uint16, uint32, uint64: 200 value = v 201 break 202 case time.Time: 203 value = v 204 break 205 default: 206 baseValue, isBaseType := dialect.GetBaseTypeValue(value) 207 if isBaseType { 208 value = baseValue 209 return 210 } 211 value = v 212 //panic("GetSqlValue data [" + fmt.Sprint(data) + "] data type [" + reflect.TypeOf(data).String() + "] name [" + columnType.Name() + "] databaseType [" + columnType.DatabaseTypeName() + "] not support") 213 break 214 } 215 return 216 } 217 218 // SplitArrayMap 分割数组,根据传入的数组和分割大小,将数组分割为大小等于指定大小的多个数组,如果不够分,则最后一个数组元素小于其他数组 219 func SplitArrayMap(arr []map[string]interface{}, num int) [][]map[string]interface{} { 220 max := len(arr) 221 //判断数组大小是否小于等于指定分割大小的值,是则把原数组放入二维数组返回 222 if max <= num { 223 if max == 0 { 224 return [][]map[string]interface{}{} 225 } 226 return [][]map[string]interface{}{arr} 227 } 228 //获取应该数组分割为多少份 229 var quantity int 230 if max%num == 0 { 231 quantity = max / num 232 } else { 233 quantity = (max / num) + 1 234 } 235 //声明分割好的二维数组 236 var segments = make([][]map[string]interface{}, 0) 237 //声明分割数组的截止下标 238 var start, end, i int 239 for i = 1; i <= quantity; i++ { 240 end = i * num 241 if i != quantity { 242 segments = append(segments, arr[start:end]) 243 } else { 244 segments = append(segments, arr[start:]) 245 } 246 start = i * num 247 } 248 return segments 249 }