|
|
|
@ -5,16 +5,22 @@ import (
|
|
|
|
|
"errors"
|
|
|
|
|
"reflect"
|
|
|
|
|
"strconv"
|
|
|
|
|
"time"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
// DBRes 单连接下的数据库资源句柄
|
|
|
|
|
var DBRes *sql.DB
|
|
|
|
|
|
|
|
|
|
// DBRows 单连接下的数据库查询句柄
|
|
|
|
|
var DBRows *sql.Rows
|
|
|
|
|
|
|
|
|
|
// StarDB 一个简单封装的DB库
|
|
|
|
|
type StarDB struct {
|
|
|
|
|
DB *sql.DB
|
|
|
|
|
Rows *sql.Rows
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// StarRows 为查询结果集(按行)
|
|
|
|
|
type StarRows struct {
|
|
|
|
|
Rows *sql.Rows
|
|
|
|
|
Length int
|
|
|
|
@ -25,6 +31,7 @@ type StarRows struct {
|
|
|
|
|
result [][]interface{}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// StarResult 为查询结果集(总)
|
|
|
|
|
type StarResult struct {
|
|
|
|
|
Result []interface{}
|
|
|
|
|
Columns []string
|
|
|
|
@ -32,35 +39,56 @@ type StarResult struct {
|
|
|
|
|
ColumnsType []reflect.Type
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// StarResultCol 为查询结果集(按列)
|
|
|
|
|
type StarResultCol struct {
|
|
|
|
|
Result []interface{}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (this *StarResultCol) MustBytes() [][]byte {
|
|
|
|
|
// MustBytes 列查询结果转Bytes
|
|
|
|
|
func (star *StarResultCol) MustBytes() [][]byte {
|
|
|
|
|
var res [][]byte
|
|
|
|
|
for _, v := range this.Result {
|
|
|
|
|
for _, v := range star.Result {
|
|
|
|
|
res = append(res, v.([]byte))
|
|
|
|
|
}
|
|
|
|
|
return res
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (this *StarResultCol) MustBool() []bool {
|
|
|
|
|
// MustBool 列查询结果转Bool
|
|
|
|
|
func (star *StarResultCol) MustBool() []bool {
|
|
|
|
|
var res []bool
|
|
|
|
|
var tmp bool
|
|
|
|
|
for _, v := range this.Result {
|
|
|
|
|
for _, v := range star.Result {
|
|
|
|
|
switch vtype := v.(type) {
|
|
|
|
|
case nil:
|
|
|
|
|
tmp = false
|
|
|
|
|
case bool:
|
|
|
|
|
tmp = vtype
|
|
|
|
|
case float64, float32:
|
|
|
|
|
if vtype.(float64) > 0 {
|
|
|
|
|
case float64:
|
|
|
|
|
if vtype > 0 {
|
|
|
|
|
tmp = true
|
|
|
|
|
} else {
|
|
|
|
|
tmp = false
|
|
|
|
|
}
|
|
|
|
|
case float32:
|
|
|
|
|
if vtype > 0 {
|
|
|
|
|
tmp = true
|
|
|
|
|
} else {
|
|
|
|
|
tmp = false
|
|
|
|
|
}
|
|
|
|
|
case int:
|
|
|
|
|
if vtype > 0 {
|
|
|
|
|
tmp = true
|
|
|
|
|
} else {
|
|
|
|
|
tmp = false
|
|
|
|
|
}
|
|
|
|
|
case int32:
|
|
|
|
|
if vtype > 0 {
|
|
|
|
|
tmp = true
|
|
|
|
|
} else {
|
|
|
|
|
tmp = false
|
|
|
|
|
}
|
|
|
|
|
case int, int32, int64:
|
|
|
|
|
if vtype.(int) > 0 {
|
|
|
|
|
case int64:
|
|
|
|
|
if vtype > 0 {
|
|
|
|
|
tmp = true
|
|
|
|
|
} else {
|
|
|
|
|
tmp = false
|
|
|
|
@ -74,52 +102,77 @@ func (this *StarResultCol) MustBool() []bool {
|
|
|
|
|
}
|
|
|
|
|
return res
|
|
|
|
|
}
|
|
|
|
|
func (this *StarResultCol) MustFloat32() []float32 {
|
|
|
|
|
|
|
|
|
|
// MustFloat32 列查询结果转Float32
|
|
|
|
|
func (star *StarResultCol) MustFloat32() []float32 {
|
|
|
|
|
var res []float32
|
|
|
|
|
var tmp float32
|
|
|
|
|
for _, v := range this.Result {
|
|
|
|
|
for _, v := range star.Result {
|
|
|
|
|
switch vtype := v.(type) {
|
|
|
|
|
case nil:
|
|
|
|
|
tmp = 0
|
|
|
|
|
case float64, float32:
|
|
|
|
|
tmp = float32(vtype.(float64))
|
|
|
|
|
case float32:
|
|
|
|
|
tmp = vtype
|
|
|
|
|
case float64:
|
|
|
|
|
tmp = float32(vtype)
|
|
|
|
|
case string:
|
|
|
|
|
tmps, _ := strconv.ParseFloat(vtype, 32)
|
|
|
|
|
tmp = float32(tmps)
|
|
|
|
|
case int, int32, int64:
|
|
|
|
|
tmp = float32(vtype.(int64))
|
|
|
|
|
case int:
|
|
|
|
|
tmp = float32(vtype)
|
|
|
|
|
case int32:
|
|
|
|
|
tmp = float32(vtype)
|
|
|
|
|
case int64:
|
|
|
|
|
tmp = float32(vtype)
|
|
|
|
|
case time.Time:
|
|
|
|
|
tmp = float32(vtype.Unix())
|
|
|
|
|
default:
|
|
|
|
|
tmp = v.(float32)
|
|
|
|
|
tmpt := string(vtype.([]byte))
|
|
|
|
|
tmps, _ := strconv.ParseFloat(tmpt, 32)
|
|
|
|
|
tmp = float32(tmps)
|
|
|
|
|
}
|
|
|
|
|
res = append(res, tmp)
|
|
|
|
|
}
|
|
|
|
|
return res
|
|
|
|
|
}
|
|
|
|
|
func (this *StarResultCol) MustFloat64() []float64 {
|
|
|
|
|
|
|
|
|
|
// MustFloat64 列查询结果转Float64
|
|
|
|
|
func (star *StarResultCol) MustFloat64() []float64 {
|
|
|
|
|
var res []float64
|
|
|
|
|
var tmp float64
|
|
|
|
|
for _, v := range this.Result {
|
|
|
|
|
for _, v := range star.Result {
|
|
|
|
|
switch vtype := v.(type) {
|
|
|
|
|
case nil:
|
|
|
|
|
tmp = 0
|
|
|
|
|
case float64, float32:
|
|
|
|
|
tmp = vtype.(float64)
|
|
|
|
|
case float64:
|
|
|
|
|
tmp = vtype
|
|
|
|
|
case float32:
|
|
|
|
|
tmp = float64(vtype)
|
|
|
|
|
case string:
|
|
|
|
|
tmp, _ = strconv.ParseFloat(vtype, 64)
|
|
|
|
|
case int, int32, int64:
|
|
|
|
|
tmp = float64(vtype.(int64))
|
|
|
|
|
case int:
|
|
|
|
|
tmp = float64(vtype)
|
|
|
|
|
case int32:
|
|
|
|
|
tmp = float64(vtype)
|
|
|
|
|
case int64:
|
|
|
|
|
tmp = float64(vtype)
|
|
|
|
|
case time.Time:
|
|
|
|
|
tmp = float64(vtype.Unix())
|
|
|
|
|
default:
|
|
|
|
|
tmp = v.(float64)
|
|
|
|
|
tmpt := string(vtype.([]byte))
|
|
|
|
|
tmps, _ := strconv.ParseFloat(tmpt, 64)
|
|
|
|
|
tmp = float64(tmps)
|
|
|
|
|
}
|
|
|
|
|
res = append(res, tmp)
|
|
|
|
|
}
|
|
|
|
|
return res
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (this *StarResultCol) MustString() []string {
|
|
|
|
|
// MustString 列查询结果转String
|
|
|
|
|
func (star *StarResultCol) MustString() []string {
|
|
|
|
|
var res []string
|
|
|
|
|
var tmp string
|
|
|
|
|
for _, v := range this.Result {
|
|
|
|
|
for _, v := range star.Result {
|
|
|
|
|
switch vtype := v.(type) {
|
|
|
|
|
case nil:
|
|
|
|
|
tmp = ""
|
|
|
|
@ -137,6 +190,8 @@ func (this *StarResultCol) MustString() []string {
|
|
|
|
|
tmp = strconv.FormatFloat(float64(vtype), 'f', 10, 32)
|
|
|
|
|
case int:
|
|
|
|
|
tmp = strconv.Itoa(vtype)
|
|
|
|
|
case time.Time:
|
|
|
|
|
tmp = vtype.String()
|
|
|
|
|
default:
|
|
|
|
|
tmp = string(vtype.([]byte))
|
|
|
|
|
}
|
|
|
|
@ -145,120 +200,269 @@ func (this *StarResultCol) MustString() []string {
|
|
|
|
|
return res
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (this *StarResultCol) MustInt32() []int32 {
|
|
|
|
|
// MustInt32 列查询结果转Int32
|
|
|
|
|
func (star *StarResultCol) MustInt32() []int32 {
|
|
|
|
|
var res []int32
|
|
|
|
|
var tmp int32
|
|
|
|
|
for _, v := range this.Result {
|
|
|
|
|
for _, v := range star.Result {
|
|
|
|
|
switch vtype := v.(type) {
|
|
|
|
|
case nil:
|
|
|
|
|
tmp = 0
|
|
|
|
|
case float64, float32:
|
|
|
|
|
tmp = int32(vtype.(float64))
|
|
|
|
|
case float64:
|
|
|
|
|
tmp = int32(vtype)
|
|
|
|
|
case float32:
|
|
|
|
|
tmp = int32(vtype)
|
|
|
|
|
case string:
|
|
|
|
|
tmps, _ := strconv.ParseInt(vtype, 10, 32)
|
|
|
|
|
tmp = int32(tmps)
|
|
|
|
|
case int, int32, int64:
|
|
|
|
|
tmp = int32(vtype.(int64))
|
|
|
|
|
case int:
|
|
|
|
|
tmp = int32(vtype)
|
|
|
|
|
case int64:
|
|
|
|
|
tmp = int32(vtype)
|
|
|
|
|
case int32:
|
|
|
|
|
tmp = vtype
|
|
|
|
|
case time.Time:
|
|
|
|
|
tmp = int32(vtype.Unix())
|
|
|
|
|
default:
|
|
|
|
|
tmp = v.(int32)
|
|
|
|
|
tmpt := string(vtype.([]byte))
|
|
|
|
|
tmps, _ := strconv.ParseInt(tmpt, 10, 32)
|
|
|
|
|
tmp = int32(tmps)
|
|
|
|
|
}
|
|
|
|
|
res = append(res, tmp)
|
|
|
|
|
}
|
|
|
|
|
return res
|
|
|
|
|
}
|
|
|
|
|
func (this *StarResultCol) MustInt64() []int64 {
|
|
|
|
|
|
|
|
|
|
// MustInt64 列查询结果转Int64
|
|
|
|
|
func (star *StarResultCol) MustInt64() []int64 {
|
|
|
|
|
var res []int64
|
|
|
|
|
var tmp int64
|
|
|
|
|
for _, v := range this.Result {
|
|
|
|
|
for _, v := range star.Result {
|
|
|
|
|
switch vtype := v.(type) {
|
|
|
|
|
case nil:
|
|
|
|
|
tmp = 0
|
|
|
|
|
case float64, float32:
|
|
|
|
|
tmp = int64(vtype.(float64))
|
|
|
|
|
case float64:
|
|
|
|
|
tmp = int64(vtype)
|
|
|
|
|
case float32:
|
|
|
|
|
tmp = int64(vtype)
|
|
|
|
|
case string:
|
|
|
|
|
tmp, _ = strconv.ParseInt(vtype, 10, 64)
|
|
|
|
|
case int, int32, int64:
|
|
|
|
|
tmp = (vtype.(int64))
|
|
|
|
|
tmps, _ := strconv.ParseInt(vtype, 10, 64)
|
|
|
|
|
tmp = int64(tmps)
|
|
|
|
|
case int:
|
|
|
|
|
tmp = int64(vtype)
|
|
|
|
|
case int32:
|
|
|
|
|
tmp = int64(vtype)
|
|
|
|
|
case int64:
|
|
|
|
|
tmp = vtype
|
|
|
|
|
case time.Time:
|
|
|
|
|
tmp = vtype.Unix()
|
|
|
|
|
default:
|
|
|
|
|
tmp = v.(int64)
|
|
|
|
|
tmpt := string(vtype.([]byte))
|
|
|
|
|
tmp, _ = strconv.ParseInt(tmpt, 10, 64)
|
|
|
|
|
}
|
|
|
|
|
res = append(res, tmp)
|
|
|
|
|
}
|
|
|
|
|
return res
|
|
|
|
|
}
|
|
|
|
|
func (this *StarResultCol) MustInt() []int {
|
|
|
|
|
|
|
|
|
|
// MustInt 列查询结果转Int
|
|
|
|
|
func (star *StarResultCol) MustInt() []int {
|
|
|
|
|
var res []int
|
|
|
|
|
var tmp int
|
|
|
|
|
for _, v := range this.Result {
|
|
|
|
|
for _, v := range star.Result {
|
|
|
|
|
switch vtype := v.(type) {
|
|
|
|
|
case nil:
|
|
|
|
|
tmp = 0
|
|
|
|
|
case float64, float32:
|
|
|
|
|
tmp = int(vtype.(float64))
|
|
|
|
|
case float64:
|
|
|
|
|
tmp = int(vtype)
|
|
|
|
|
case float32:
|
|
|
|
|
tmp = int(vtype)
|
|
|
|
|
case string:
|
|
|
|
|
tmps, _ := strconv.ParseInt(vtype, 10, 64)
|
|
|
|
|
tmp = int(tmps)
|
|
|
|
|
case int, int32, int64:
|
|
|
|
|
tmp = int(vtype.(int64))
|
|
|
|
|
case int:
|
|
|
|
|
tmp = vtype
|
|
|
|
|
case int32:
|
|
|
|
|
tmp = int(vtype)
|
|
|
|
|
case int64:
|
|
|
|
|
tmp = int(vtype)
|
|
|
|
|
case time.Time:
|
|
|
|
|
tmp = int(vtype.Unix())
|
|
|
|
|
default:
|
|
|
|
|
tmpt := string(vtype.([]byte))
|
|
|
|
|
tmps, _ := strconv.ParseInt(tmpt, 10, 64)
|
|
|
|
|
tmp = int(tmps)
|
|
|
|
|
}
|
|
|
|
|
res = append(res, tmp)
|
|
|
|
|
}
|
|
|
|
|
return res
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// MustDate 列查询结果转Date(time.Time)
|
|
|
|
|
func (star *StarResultCol) MustDate(layout string) []time.Time {
|
|
|
|
|
var res []time.Time
|
|
|
|
|
var tmp time.Time
|
|
|
|
|
for _, v := range star.Result {
|
|
|
|
|
switch vtype := v.(type) {
|
|
|
|
|
case nil:
|
|
|
|
|
tmp = time.Time{}
|
|
|
|
|
case float64:
|
|
|
|
|
tmp = time.Unix(int64(vtype), int64(vtype-float64(int64(vtype)))*1000000000)
|
|
|
|
|
case float32:
|
|
|
|
|
tmp = time.Unix(int64(vtype), int64(vtype-float32(int64(vtype)))*1000000000)
|
|
|
|
|
case string:
|
|
|
|
|
tmp, _ = time.Parse(layout, vtype)
|
|
|
|
|
case int:
|
|
|
|
|
tmp = time.Unix(int64(vtype), 0)
|
|
|
|
|
case int32:
|
|
|
|
|
tmp = time.Unix(int64(vtype), 0)
|
|
|
|
|
case int64:
|
|
|
|
|
tmp = time.Unix(vtype, 0)
|
|
|
|
|
case time.Time:
|
|
|
|
|
tmp = vtype
|
|
|
|
|
default:
|
|
|
|
|
tmpt := string(vtype.([]byte))
|
|
|
|
|
tmp, _ = time.Parse(layout, tmpt)
|
|
|
|
|
}
|
|
|
|
|
res = append(res, tmp)
|
|
|
|
|
}
|
|
|
|
|
return res
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// IsNil 检测是不是nil 列查询结果是不是nil
|
|
|
|
|
func (star *StarResultCol) IsNil(name string) []bool {
|
|
|
|
|
var res []bool
|
|
|
|
|
var tmp bool
|
|
|
|
|
for _, v := range star.Result {
|
|
|
|
|
switch v.(type) {
|
|
|
|
|
case nil:
|
|
|
|
|
tmp = true
|
|
|
|
|
default:
|
|
|
|
|
tmp = int(v.(int64))
|
|
|
|
|
tmp = false
|
|
|
|
|
}
|
|
|
|
|
res = append(res, tmp)
|
|
|
|
|
}
|
|
|
|
|
return res
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (this *StarResult) MustInt64(name string) int64 {
|
|
|
|
|
// IsNil 检测是不是nil
|
|
|
|
|
func (star *StarResult) IsNil(name string) bool {
|
|
|
|
|
num, ok := star.columnref[name]
|
|
|
|
|
if !ok {
|
|
|
|
|
return false
|
|
|
|
|
}
|
|
|
|
|
tmp := star.Result[num]
|
|
|
|
|
switch tmp.(type) {
|
|
|
|
|
case nil:
|
|
|
|
|
return true
|
|
|
|
|
default:
|
|
|
|
|
return false
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// MustDate 列查询结果转Date
|
|
|
|
|
func (star *StarResult) MustDate(name, layout string) time.Time {
|
|
|
|
|
var res time.Time
|
|
|
|
|
num, ok := star.columnref[name]
|
|
|
|
|
if !ok {
|
|
|
|
|
return time.Time{}
|
|
|
|
|
}
|
|
|
|
|
tmp := star.Result[num]
|
|
|
|
|
switch vtype := tmp.(type) {
|
|
|
|
|
case nil:
|
|
|
|
|
res = time.Time{}
|
|
|
|
|
case float64:
|
|
|
|
|
res = time.Unix(int64(vtype), int64(vtype-float64(int64(vtype)))*1000000000)
|
|
|
|
|
case float32:
|
|
|
|
|
res = time.Unix(int64(vtype), int64(vtype-float32(int64(vtype)))*1000000000)
|
|
|
|
|
case string:
|
|
|
|
|
res, _ = time.Parse(layout, vtype)
|
|
|
|
|
case int:
|
|
|
|
|
res = time.Unix(int64(vtype), 0)
|
|
|
|
|
case int32:
|
|
|
|
|
res = time.Unix(int64(vtype), 0)
|
|
|
|
|
case int64:
|
|
|
|
|
res = time.Unix(vtype, 0)
|
|
|
|
|
case time.Time:
|
|
|
|
|
res = vtype
|
|
|
|
|
default:
|
|
|
|
|
res, _ = time.Parse(layout, string(tmp.([]byte)))
|
|
|
|
|
}
|
|
|
|
|
return res
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// MustInt64 列查询结果转int64
|
|
|
|
|
func (star *StarResult) MustInt64(name string) int64 {
|
|
|
|
|
var res int64
|
|
|
|
|
num, ok := this.columnref[name]
|
|
|
|
|
num, ok := star.columnref[name]
|
|
|
|
|
if !ok {
|
|
|
|
|
return 0
|
|
|
|
|
}
|
|
|
|
|
tmp := this.Result[num]
|
|
|
|
|
tmp := star.Result[num]
|
|
|
|
|
switch vtype := tmp.(type) {
|
|
|
|
|
case nil:
|
|
|
|
|
res = 0
|
|
|
|
|
case float64, float32:
|
|
|
|
|
res = int64(vtype.(float64))
|
|
|
|
|
case float64:
|
|
|
|
|
res = int64(vtype)
|
|
|
|
|
case float32:
|
|
|
|
|
res = int64(vtype)
|
|
|
|
|
case string:
|
|
|
|
|
res, _ = strconv.ParseInt(vtype, 10, 64)
|
|
|
|
|
case int, int32, int64:
|
|
|
|
|
res = (vtype.(int64))
|
|
|
|
|
case int:
|
|
|
|
|
res = int64(vtype)
|
|
|
|
|
case int32:
|
|
|
|
|
res = int64(vtype)
|
|
|
|
|
case int64:
|
|
|
|
|
res = vtype
|
|
|
|
|
case time.Time:
|
|
|
|
|
res = int64(vtype.Unix())
|
|
|
|
|
default:
|
|
|
|
|
res, _ = strconv.ParseInt(string(tmp.([]byte)), 10, 64)
|
|
|
|
|
}
|
|
|
|
|
return res
|
|
|
|
|
}
|
|
|
|
|
func (this *StarResult) MustInt32(name string) int32 {
|
|
|
|
|
|
|
|
|
|
// MustInt32 列查询结果转Int32
|
|
|
|
|
func (star *StarResult) MustInt32(name string) int32 {
|
|
|
|
|
var res int32
|
|
|
|
|
num, ok := this.columnref[name]
|
|
|
|
|
num, ok := star.columnref[name]
|
|
|
|
|
if !ok {
|
|
|
|
|
return 0
|
|
|
|
|
}
|
|
|
|
|
tmp := this.Result[num]
|
|
|
|
|
tmp := star.Result[num]
|
|
|
|
|
switch vtype := tmp.(type) {
|
|
|
|
|
case nil:
|
|
|
|
|
res = 0
|
|
|
|
|
case float64, float32:
|
|
|
|
|
res = int32(vtype.(float64))
|
|
|
|
|
case float64:
|
|
|
|
|
res = int32(vtype)
|
|
|
|
|
case float32:
|
|
|
|
|
res = int32(vtype)
|
|
|
|
|
case string:
|
|
|
|
|
ress, _ := strconv.ParseInt(vtype, 10, 32)
|
|
|
|
|
res = int32(ress)
|
|
|
|
|
case int, int32, int64:
|
|
|
|
|
res = int32(vtype.(int64))
|
|
|
|
|
case int:
|
|
|
|
|
res = int32(vtype)
|
|
|
|
|
case int32:
|
|
|
|
|
res = vtype
|
|
|
|
|
case int64:
|
|
|
|
|
res = int32(vtype)
|
|
|
|
|
case time.Time:
|
|
|
|
|
res = int32(vtype.Unix())
|
|
|
|
|
default:
|
|
|
|
|
ress, _ := strconv.ParseInt(string(tmp.([]byte)), 10, 32)
|
|
|
|
|
res = int32(ress)
|
|
|
|
|
}
|
|
|
|
|
return res
|
|
|
|
|
}
|
|
|
|
|
func (this *StarResult) MustString(name string) string {
|
|
|
|
|
|
|
|
|
|
// MustString 列查询结果转string
|
|
|
|
|
func (star *StarResult) MustString(name string) string {
|
|
|
|
|
var res string
|
|
|
|
|
num, ok := this.columnref[name]
|
|
|
|
|
num, ok := star.columnref[name]
|
|
|
|
|
if !ok {
|
|
|
|
|
return ""
|
|
|
|
|
}
|
|
|
|
|
switch vtype := this.Result[num].(type) {
|
|
|
|
|
switch vtype := star.Result[num].(type) {
|
|
|
|
|
case nil:
|
|
|
|
|
res = ""
|
|
|
|
|
case string:
|
|
|
|
@ -275,40 +479,52 @@ func (this *StarResult) MustString(name string) string {
|
|
|
|
|
res = strconv.FormatFloat(float64(vtype), 'f', 10, 32)
|
|
|
|
|
case int:
|
|
|
|
|
res = strconv.Itoa(vtype)
|
|
|
|
|
case time.Time:
|
|
|
|
|
res = vtype.String()
|
|
|
|
|
default:
|
|
|
|
|
res = string(vtype.([]byte))
|
|
|
|
|
}
|
|
|
|
|
return res
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (this *StarResult) MustFloat64(name string) float64 {
|
|
|
|
|
// MustFloat64 列查询结果转float64
|
|
|
|
|
func (star *StarResult) MustFloat64(name string) float64 {
|
|
|
|
|
var res float64
|
|
|
|
|
num, ok := this.columnref[name]
|
|
|
|
|
num, ok := star.columnref[name]
|
|
|
|
|
if !ok {
|
|
|
|
|
return 0
|
|
|
|
|
}
|
|
|
|
|
switch vtype := this.Result[num].(type) {
|
|
|
|
|
switch vtype := star.Result[num].(type) {
|
|
|
|
|
case nil:
|
|
|
|
|
res = 0
|
|
|
|
|
case string:
|
|
|
|
|
res, _ = strconv.ParseFloat(vtype, 64)
|
|
|
|
|
case float64:
|
|
|
|
|
res = vtype
|
|
|
|
|
case int, int64, int32, float32:
|
|
|
|
|
res = vtype.(float64)
|
|
|
|
|
case int:
|
|
|
|
|
res = float64(vtype)
|
|
|
|
|
case int64:
|
|
|
|
|
res = float64(vtype)
|
|
|
|
|
case int32:
|
|
|
|
|
res = float64(vtype)
|
|
|
|
|
case float32:
|
|
|
|
|
res = float64(vtype)
|
|
|
|
|
case time.Time:
|
|
|
|
|
res = float64(vtype.Unix())
|
|
|
|
|
default:
|
|
|
|
|
res, _ = strconv.ParseFloat(string(vtype.([]byte)), 64)
|
|
|
|
|
}
|
|
|
|
|
return res
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (this *StarResult) MustFloat32(name string) float32 {
|
|
|
|
|
// MustFloat32 列查询结果转float32
|
|
|
|
|
func (star *StarResult) MustFloat32(name string) float32 {
|
|
|
|
|
var res float32
|
|
|
|
|
num, ok := this.columnref[name]
|
|
|
|
|
num, ok := star.columnref[name]
|
|
|
|
|
if !ok {
|
|
|
|
|
return 0
|
|
|
|
|
}
|
|
|
|
|
switch vtype := this.Result[num].(type) {
|
|
|
|
|
switch vtype := star.Result[num].(type) {
|
|
|
|
|
case nil:
|
|
|
|
|
res = 0
|
|
|
|
|
case string:
|
|
|
|
@ -318,8 +534,14 @@ func (this *StarResult) MustFloat32(name string) float32 {
|
|
|
|
|
res = float32(vtype)
|
|
|
|
|
case float32:
|
|
|
|
|
res = vtype
|
|
|
|
|
case int, int64, int32:
|
|
|
|
|
res = vtype.(float32)
|
|
|
|
|
case int:
|
|
|
|
|
res = float32(vtype)
|
|
|
|
|
case int64:
|
|
|
|
|
res = float32(vtype)
|
|
|
|
|
case int32:
|
|
|
|
|
res = float32(vtype)
|
|
|
|
|
case time.Time:
|
|
|
|
|
res = float32(vtype.Unix())
|
|
|
|
|
default:
|
|
|
|
|
tmp, _ := strconv.ParseFloat(string(vtype.([]byte)), 32)
|
|
|
|
|
res = float32(tmp)
|
|
|
|
@ -327,23 +549,32 @@ func (this *StarResult) MustFloat32(name string) float32 {
|
|
|
|
|
return res
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (this *StarResult) MustInt(name string) int {
|
|
|
|
|
// MustInt 列查询结果转int
|
|
|
|
|
func (star *StarResult) MustInt(name string) int {
|
|
|
|
|
var res int
|
|
|
|
|
num, ok := this.columnref[name]
|
|
|
|
|
num, ok := star.columnref[name]
|
|
|
|
|
if !ok {
|
|
|
|
|
return 0
|
|
|
|
|
}
|
|
|
|
|
tmp := this.Result[num]
|
|
|
|
|
tmp := star.Result[num]
|
|
|
|
|
switch vtype := tmp.(type) {
|
|
|
|
|
case nil:
|
|
|
|
|
res = 0
|
|
|
|
|
case float64, float32:
|
|
|
|
|
res = int(vtype.(float64))
|
|
|
|
|
case float64:
|
|
|
|
|
res = int(vtype)
|
|
|
|
|
case float32:
|
|
|
|
|
res = int(vtype)
|
|
|
|
|
case string:
|
|
|
|
|
ress, _ := strconv.ParseInt(vtype, 10, 64)
|
|
|
|
|
res = int(ress)
|
|
|
|
|
case int, int32, int64:
|
|
|
|
|
res = int(vtype.(int64))
|
|
|
|
|
case int:
|
|
|
|
|
res = vtype
|
|
|
|
|
case int32:
|
|
|
|
|
res = int(vtype)
|
|
|
|
|
case int64:
|
|
|
|
|
res = int(vtype)
|
|
|
|
|
case time.Time:
|
|
|
|
|
res = int(vtype.Unix())
|
|
|
|
|
default:
|
|
|
|
|
ress, _ := strconv.ParseInt(string(tmp.([]byte)), 10, 64)
|
|
|
|
|
res = int(ress)
|
|
|
|
@ -351,26 +582,45 @@ func (this *StarResult) MustInt(name string) int {
|
|
|
|
|
return res
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (this *StarResult) MustBool(name string) bool {
|
|
|
|
|
// MustBool 列查询结果转bool
|
|
|
|
|
func (star *StarResult) MustBool(name string) bool {
|
|
|
|
|
var res bool
|
|
|
|
|
num, ok := this.columnref[name]
|
|
|
|
|
num, ok := star.columnref[name]
|
|
|
|
|
if !ok {
|
|
|
|
|
return false
|
|
|
|
|
}
|
|
|
|
|
tmp := this.Result[num]
|
|
|
|
|
tmp := star.Result[num]
|
|
|
|
|
switch vtype := tmp.(type) {
|
|
|
|
|
case nil:
|
|
|
|
|
res = false
|
|
|
|
|
case bool:
|
|
|
|
|
res = vtype
|
|
|
|
|
case float64, float32:
|
|
|
|
|
if vtype.(float64) > 0 {
|
|
|
|
|
case float64:
|
|
|
|
|
if vtype > 0 {
|
|
|
|
|
res = true
|
|
|
|
|
} else {
|
|
|
|
|
res = false
|
|
|
|
|
}
|
|
|
|
|
case float32:
|
|
|
|
|
if vtype > 0 {
|
|
|
|
|
res = true
|
|
|
|
|
} else {
|
|
|
|
|
res = false
|
|
|
|
|
}
|
|
|
|
|
case int:
|
|
|
|
|
if vtype > 0 {
|
|
|
|
|
res = true
|
|
|
|
|
} else {
|
|
|
|
|
res = false
|
|
|
|
|
}
|
|
|
|
|
case int32:
|
|
|
|
|
if vtype > 0 {
|
|
|
|
|
res = true
|
|
|
|
|
} else {
|
|
|
|
|
res = false
|
|
|
|
|
}
|
|
|
|
|
case int, int32, int64:
|
|
|
|
|
if vtype.(int) > 0 {
|
|
|
|
|
case int64:
|
|
|
|
|
if vtype > 0 {
|
|
|
|
|
res = true
|
|
|
|
|
} else {
|
|
|
|
|
res = false
|
|
|
|
@ -382,65 +632,71 @@ func (this *StarResult) MustBool(name string) bool {
|
|
|
|
|
}
|
|
|
|
|
return res
|
|
|
|
|
}
|
|
|
|
|
func (this *StarResult) MustBytes(name string) []byte {
|
|
|
|
|
num, ok := this.columnref[name]
|
|
|
|
|
|
|
|
|
|
// MustBytes 列查询结果转byte
|
|
|
|
|
func (star *StarResult) MustBytes(name string) []byte {
|
|
|
|
|
num, ok := star.columnref[name]
|
|
|
|
|
if !ok {
|
|
|
|
|
return []byte{}
|
|
|
|
|
}
|
|
|
|
|
res := this.Result[num].([]byte)
|
|
|
|
|
res := star.Result[num].([]byte)
|
|
|
|
|
return res
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (this *StarRows) Rescan() {
|
|
|
|
|
this.parserows()
|
|
|
|
|
// Rescan 重新分析结果集
|
|
|
|
|
func (star *StarRows) Rescan() {
|
|
|
|
|
star.parserows()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (this *StarRows) Col(name string) *StarResultCol {
|
|
|
|
|
// Col 选择需要进行操作的数据结果列
|
|
|
|
|
func (star *StarRows) Col(name string) *StarResultCol {
|
|
|
|
|
result := new(StarResultCol)
|
|
|
|
|
if _, ok := this.columnref[name]; !ok {
|
|
|
|
|
if _, ok := star.columnref[name]; !ok {
|
|
|
|
|
return result
|
|
|
|
|
}
|
|
|
|
|
var rescol []interface{}
|
|
|
|
|
for _, v := range this.result {
|
|
|
|
|
rescol = append(rescol, v[this.columnref[name]])
|
|
|
|
|
for _, v := range star.result {
|
|
|
|
|
rescol = append(rescol, v[star.columnref[name]])
|
|
|
|
|
}
|
|
|
|
|
result.Result = rescol
|
|
|
|
|
return result
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (this *StarRows) Row(id int) *StarResult {
|
|
|
|
|
// Row 选择需要进行操作的数据结果行
|
|
|
|
|
func (star *StarRows) Row(id int) *StarResult {
|
|
|
|
|
result := new(StarResult)
|
|
|
|
|
if id+1 > len(this.result) {
|
|
|
|
|
if id+1 > len(star.result) {
|
|
|
|
|
return result
|
|
|
|
|
}
|
|
|
|
|
result.Result = this.result[id]
|
|
|
|
|
result.Columns = this.Columns
|
|
|
|
|
result.ColumnsType = this.ColumnsType
|
|
|
|
|
result.columnref = this.columnref
|
|
|
|
|
result.Result = star.result[id]
|
|
|
|
|
result.Columns = star.Columns
|
|
|
|
|
result.ColumnsType = star.ColumnsType
|
|
|
|
|
result.columnref = star.columnref
|
|
|
|
|
return result
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (this *StarRows) Close() error {
|
|
|
|
|
return this.Rows.Close()
|
|
|
|
|
// Close 关闭打开的结果集
|
|
|
|
|
func (star *StarRows) Close() error {
|
|
|
|
|
return star.Rows.Close()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (this *StarRows) parserows() {
|
|
|
|
|
this.result = [][]interface{}{}
|
|
|
|
|
this.columnref = make(map[string]int)
|
|
|
|
|
this.StringResult = []map[string]string{}
|
|
|
|
|
this.Columns, _ = this.Rows.Columns()
|
|
|
|
|
types, _ := this.Rows.ColumnTypes()
|
|
|
|
|
func (star *StarRows) parserows() {
|
|
|
|
|
star.result = [][]interface{}{}
|
|
|
|
|
star.columnref = make(map[string]int)
|
|
|
|
|
star.StringResult = []map[string]string{}
|
|
|
|
|
star.Columns, _ = star.Rows.Columns()
|
|
|
|
|
types, _ := star.Rows.ColumnTypes()
|
|
|
|
|
for _, v := range types {
|
|
|
|
|
this.ColumnsType = append(this.ColumnsType, v.ScanType())
|
|
|
|
|
star.ColumnsType = append(star.ColumnsType, v.ScanType())
|
|
|
|
|
}
|
|
|
|
|
scanArgs := make([]interface{}, len(this.Columns))
|
|
|
|
|
values := make([]interface{}, len(this.Columns))
|
|
|
|
|
for i, _ := range values {
|
|
|
|
|
this.columnref[this.Columns[i]] = i
|
|
|
|
|
scanArgs := make([]interface{}, len(star.Columns))
|
|
|
|
|
values := make([]interface{}, len(star.Columns))
|
|
|
|
|
for i := range values {
|
|
|
|
|
star.columnref[star.Columns[i]] = i
|
|
|
|
|
scanArgs[i] = &values[i]
|
|
|
|
|
}
|
|
|
|
|
for this.Rows.Next() {
|
|
|
|
|
if err := this.Rows.Scan(scanArgs...); err != nil {
|
|
|
|
|
for star.Rows.Next() {
|
|
|
|
|
if err := star.Rows.Scan(scanArgs...); err != nil {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
record := make(map[string]string)
|
|
|
|
@ -448,28 +704,39 @@ func (this *StarRows) parserows() {
|
|
|
|
|
for i, col := range values {
|
|
|
|
|
rescopy = append(rescopy, col)
|
|
|
|
|
switch vtype := col.(type) {
|
|
|
|
|
case float32:
|
|
|
|
|
record[star.Columns[i]] = strconv.FormatFloat(float64(vtype), 'f', -1, 64)
|
|
|
|
|
case float64:
|
|
|
|
|
record[this.Columns[i]] = strconv.FormatFloat(vtype, 'f', -1, 64)
|
|
|
|
|
record[star.Columns[i]] = strconv.FormatFloat(vtype, 'f', -1, 64)
|
|
|
|
|
case int64:
|
|
|
|
|
record[this.Columns[i]] = strconv.FormatInt(vtype, 10)
|
|
|
|
|
record[star.Columns[i]] = strconv.FormatInt(vtype, 10)
|
|
|
|
|
case int32:
|
|
|
|
|
record[star.Columns[i]] = strconv.FormatInt(int64(vtype), 10)
|
|
|
|
|
case int:
|
|
|
|
|
record[star.Columns[i]] = strconv.Itoa(vtype)
|
|
|
|
|
case string:
|
|
|
|
|
record[this.Columns[i]] = vtype
|
|
|
|
|
record[star.Columns[i]] = vtype
|
|
|
|
|
case bool:
|
|
|
|
|
record[star.Columns[i]] = strconv.FormatBool(vtype)
|
|
|
|
|
case time.Time:
|
|
|
|
|
record[star.Columns[i]] = vtype.String()
|
|
|
|
|
case nil:
|
|
|
|
|
record[this.Columns[i]] = ""
|
|
|
|
|
record[star.Columns[i]] = ""
|
|
|
|
|
default:
|
|
|
|
|
record[this.Columns[i]] = string(vtype.([]byte))
|
|
|
|
|
record[star.Columns[i]] = string(vtype.([]byte))
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
this.result = append(this.result, rescopy)
|
|
|
|
|
this.StringResult = append(this.StringResult, record)
|
|
|
|
|
star.result = append(star.result, rescopy)
|
|
|
|
|
star.StringResult = append(star.StringResult, record)
|
|
|
|
|
}
|
|
|
|
|
this.Length = len(this.StringResult)
|
|
|
|
|
star.Length = len(star.StringResult)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (this *StarDB) Query(args ...interface{}) (*StarRows, error) {
|
|
|
|
|
// Query 进行Query操作
|
|
|
|
|
func (star *StarDB) Query(args ...interface{}) (*StarRows, error) {
|
|
|
|
|
var err error
|
|
|
|
|
effect := new(StarRows)
|
|
|
|
|
if err = this.DB.Ping(); err != nil {
|
|
|
|
|
if err = star.DB.Ping(); err != nil {
|
|
|
|
|
return effect, err
|
|
|
|
|
}
|
|
|
|
|
if len(args) == 0 {
|
|
|
|
@ -477,15 +744,15 @@ func (this *StarDB) Query(args ...interface{}) (*StarRows, error) {
|
|
|
|
|
}
|
|
|
|
|
if len(args) == 1 {
|
|
|
|
|
sql := args[0]
|
|
|
|
|
if this.Rows, err = this.DB.Query(sql.(string)); err != nil {
|
|
|
|
|
if star.Rows, err = star.DB.Query(sql.(string)); err != nil {
|
|
|
|
|
return effect, err
|
|
|
|
|
}
|
|
|
|
|
effect.Rows = this.Rows
|
|
|
|
|
effect.Rows = star.Rows
|
|
|
|
|
effect.parserows()
|
|
|
|
|
return effect, nil
|
|
|
|
|
}
|
|
|
|
|
sql := args[0]
|
|
|
|
|
stmt, err := this.DB.Prepare(sql.(string))
|
|
|
|
|
stmt, err := star.DB.Prepare(sql.(string))
|
|
|
|
|
if err != nil {
|
|
|
|
|
return effect, err
|
|
|
|
|
}
|
|
|
|
@ -499,35 +766,38 @@ func (this *StarDB) Query(args ...interface{}) (*StarRows, error) {
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if this.Rows, err = stmt.Query(para...); err != nil {
|
|
|
|
|
if star.Rows, err = stmt.Query(para...); err != nil {
|
|
|
|
|
return effect, err
|
|
|
|
|
}
|
|
|
|
|
effect.Rows = this.Rows
|
|
|
|
|
effect.Rows = star.Rows
|
|
|
|
|
effect.parserows()
|
|
|
|
|
return effect, nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (this *StarDB) Open(Method, ConnStr string) error {
|
|
|
|
|
// Open 打开一个新的数据库
|
|
|
|
|
func (star *StarDB) Open(Method, ConnStr string) error {
|
|
|
|
|
var err error
|
|
|
|
|
this.DB, err = sql.Open(Method, ConnStr)
|
|
|
|
|
star.DB, err = sql.Open(Method, ConnStr)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
err = this.DB.Ping()
|
|
|
|
|
err = star.DB.Ping()
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (this *StarDB) Close() error {
|
|
|
|
|
if err := this.DB.Close(); err != nil {
|
|
|
|
|
// Close 关闭打开的数据库
|
|
|
|
|
func (star *StarDB) Close() error {
|
|
|
|
|
if err := star.DB.Close(); err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
return this.DB.Close()
|
|
|
|
|
return star.DB.Close()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (this *StarDB) Exec(args ...interface{}) (sql.Result, error) {
|
|
|
|
|
// Exec 执行Exec操作
|
|
|
|
|
func (star *StarDB) Exec(args ...interface{}) (sql.Result, error) {
|
|
|
|
|
var err error
|
|
|
|
|
var effect sql.Result
|
|
|
|
|
if err = this.DB.Ping(); err != nil {
|
|
|
|
|
if err = star.DB.Ping(); err != nil {
|
|
|
|
|
return effect, err
|
|
|
|
|
}
|
|
|
|
|
if len(args) == 0 {
|
|
|
|
@ -535,17 +805,17 @@ func (this *StarDB) Exec(args ...interface{}) (sql.Result, error) {
|
|
|
|
|
}
|
|
|
|
|
if len(args) == 1 {
|
|
|
|
|
sql := args[0]
|
|
|
|
|
if _, err = this.DB.Exec(sql.(string)); err != nil {
|
|
|
|
|
if _, err = star.DB.Exec(sql.(string)); err != nil {
|
|
|
|
|
return effect, err
|
|
|
|
|
}
|
|
|
|
|
return effect, nil
|
|
|
|
|
}
|
|
|
|
|
sql := args[0]
|
|
|
|
|
stmt, err := this.DB.Prepare(sql.(string))
|
|
|
|
|
defer stmt.Close()
|
|
|
|
|
stmt, err := star.DB.Prepare(sql.(string))
|
|
|
|
|
if err != nil {
|
|
|
|
|
return effect, err
|
|
|
|
|
}
|
|
|
|
|
defer stmt.Close()
|
|
|
|
|
var para []interface{}
|
|
|
|
|
for k, v := range args {
|
|
|
|
|
if k != 0 {
|
|
|
|
@ -561,6 +831,7 @@ func (this *StarDB) Exec(args ...interface{}) (sql.Result, error) {
|
|
|
|
|
return effect, nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// FetchAll 把结果集全部转为key-value型<string>数据
|
|
|
|
|
func FetchAll(rows *sql.Rows) (error, map[int]map[string]string) {
|
|
|
|
|
var ii int = 0
|
|
|
|
|
records := make(map[int]map[string]string)
|
|
|
|
@ -598,6 +869,7 @@ func FetchAll(rows *sql.Rows) (error, map[int]map[string]string) {
|
|
|
|
|
return nil, records
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// OpenDB 打开一个数据库
|
|
|
|
|
func OpenDB(Method, ConnStr string) error {
|
|
|
|
|
var err error
|
|
|
|
|
DBRes, err = sql.Open(Method, ConnStr)
|
|
|
|
@ -607,11 +879,14 @@ func OpenDB(Method, ConnStr string) error {
|
|
|
|
|
err = DBRes.Ping()
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// CloseDB 关闭打开的数据库
|
|
|
|
|
func CloseDB() {
|
|
|
|
|
DBRes.Close()
|
|
|
|
|
DBRows.Close()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Query 进行数据库查询操作
|
|
|
|
|
func Query(args ...interface{}) (error, map[int]map[string]string) {
|
|
|
|
|
var err error
|
|
|
|
|
records := make(map[int]map[string]string)
|
|
|
|
@ -650,6 +925,7 @@ func Query(args ...interface{}) (error, map[int]map[string]string) {
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// DBExec 进行数据库EXEC查询操作
|
|
|
|
|
func DBExec(args ...interface{}) error {
|
|
|
|
|
var err error
|
|
|
|
|
if err = DBRes.Ping(); err != nil {
|
|
|
|
|