Compare commits
No commits in common. "master" and "2.0.0" have entirely different histories.
21
archive.go
21
archive.go
@ -6,17 +6,11 @@ import (
|
|||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
"os"
|
"os"
|
||||||
"path/filepath"
|
|
||||||
"strings"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
// Unzip 读取位于src的zip文件,并解压到dst文件夹中
|
// Unzip 读取位于src的zip文件,并解压到dst文件夹中
|
||||||
// shell传入当前解压的文件名称
|
// shell传入当前解压的文件名称
|
||||||
func Unzip(src, dst string, shell func(string)) error {
|
func Unzip(src, dst string, shell func(string)) error {
|
||||||
dst, err := filepath.Abs(dst)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
if !IsFile(src) {
|
if !IsFile(src) {
|
||||||
return errors.New(src + " Not Exists")
|
return errors.New(src + " Not Exists")
|
||||||
}
|
}
|
||||||
@ -24,9 +18,9 @@ func Unzip(src, dst string, shell func(string)) error {
|
|||||||
return errors.New(dst + " Exists And Not A Folder")
|
return errors.New(dst + " Exists And Not A Folder")
|
||||||
}
|
}
|
||||||
if !Exists(dst) {
|
if !Exists(dst) {
|
||||||
err := os.MkdirAll(dst, 0755)
|
err := os.MkdirAll(dst, 0644)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return nil
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
zipreader, err := zip.OpenReader(src)
|
zipreader, err := zip.OpenReader(src)
|
||||||
@ -35,9 +29,8 @@ func Unzip(src, dst string, shell func(string)) error {
|
|||||||
}
|
}
|
||||||
defer zipreader.Close()
|
defer zipreader.Close()
|
||||||
for _, v := range zipreader.File {
|
for _, v := range zipreader.File {
|
||||||
name := strings.ReplaceAll(v.Name, "\\", string(os.PathSeparator))
|
|
||||||
if v.FileInfo().IsDir() {
|
if v.FileInfo().IsDir() {
|
||||||
err := os.MkdirAll(dst+string(os.PathSeparator)+name, 0755)
|
err := os.MkdirAll(dst+string(os.PathSeparator)+v.Name, 0644)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Println(err)
|
fmt.Println(err)
|
||||||
}
|
}
|
||||||
@ -48,12 +41,8 @@ func Unzip(src, dst string, shell func(string)) error {
|
|||||||
fmt.Println(err)
|
fmt.Println(err)
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
go shell(name)
|
go shell(v.Name)
|
||||||
dir := filepath.Dir(dst + string(os.PathSeparator) + name)
|
fpdst, err := os.Create(dst + string(os.PathSeparator) + v.Name)
|
||||||
if !Exists(dir) {
|
|
||||||
os.MkdirAll(dir, 0755)
|
|
||||||
}
|
|
||||||
fpdst, err := os.Create(dst + string(os.PathSeparator) + name)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Println(err)
|
fmt.Println(err)
|
||||||
continue
|
continue
|
||||||
|
@ -823,17 +823,17 @@ func FillWithRandom(filepath string, filesize int, bufcap int, bufnum int, shell
|
|||||||
for i := 0; i < bufnum; i++ {
|
for i := 0; i < bufnum; i++ {
|
||||||
buftmp = []byte{}
|
buftmp = []byte{}
|
||||||
for j := 0; j < bufcap; j++ {
|
for j := 0; j < bufcap; j++ {
|
||||||
buftmp = append(buftmp, byte(rands.Intn(256)))
|
buftmp = append(buftmp, byte(rands.Intn(255)))
|
||||||
}
|
}
|
||||||
buf = append(buf, buftmp)
|
buf = append(buf, buftmp)
|
||||||
}
|
}
|
||||||
sum := 0
|
sum := 0
|
||||||
for {
|
for {
|
||||||
if filesize-sum < bufcap {
|
if filesize-sum < bufcap {
|
||||||
writer.Write(buf[rands.Intn(bufnum)][0 : filesize-sum])
|
writer.Write(buf[rands.Intn(bufnum-1)][0 : filesize-sum])
|
||||||
sum += filesize - sum
|
sum += filesize - sum
|
||||||
} else {
|
} else {
|
||||||
writer.Write(buf[rands.Intn(bufnum)])
|
writer.Write(buf[rands.Intn(bufnum-1)])
|
||||||
sum += bufcap
|
sum += bufcap
|
||||||
}
|
}
|
||||||
go shell(float64(sum) / float64(filesize) * 100)
|
go shell(float64(sum) / float64(filesize) * 100)
|
||||||
|
562
database.go
562
database.go
@ -5,22 +5,16 @@ import (
|
|||||||
"errors"
|
"errors"
|
||||||
"reflect"
|
"reflect"
|
||||||
"strconv"
|
"strconv"
|
||||||
"time"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
// DBRes 单连接下的数据库资源句柄
|
|
||||||
var DBRes *sql.DB
|
var DBRes *sql.DB
|
||||||
|
|
||||||
// DBRows 单连接下的数据库查询句柄
|
|
||||||
var DBRows *sql.Rows
|
var DBRows *sql.Rows
|
||||||
|
|
||||||
// StarDB 一个简单封装的DB库
|
|
||||||
type StarDB struct {
|
type StarDB struct {
|
||||||
DB *sql.DB
|
DB *sql.DB
|
||||||
Rows *sql.Rows
|
Rows *sql.Rows
|
||||||
}
|
}
|
||||||
|
|
||||||
// StarRows 为查询结果集(按行)
|
|
||||||
type StarRows struct {
|
type StarRows struct {
|
||||||
Rows *sql.Rows
|
Rows *sql.Rows
|
||||||
Length int
|
Length int
|
||||||
@ -31,7 +25,6 @@ type StarRows struct {
|
|||||||
result [][]interface{}
|
result [][]interface{}
|
||||||
}
|
}
|
||||||
|
|
||||||
// StarResult 为查询结果集(总)
|
|
||||||
type StarResult struct {
|
type StarResult struct {
|
||||||
Result []interface{}
|
Result []interface{}
|
||||||
Columns []string
|
Columns []string
|
||||||
@ -39,56 +32,35 @@ type StarResult struct {
|
|||||||
ColumnsType []reflect.Type
|
ColumnsType []reflect.Type
|
||||||
}
|
}
|
||||||
|
|
||||||
// StarResultCol 为查询结果集(按列)
|
|
||||||
type StarResultCol struct {
|
type StarResultCol struct {
|
||||||
Result []interface{}
|
Result []interface{}
|
||||||
}
|
}
|
||||||
|
|
||||||
// MustBytes 列查询结果转Bytes
|
func (this *StarResultCol) MustBytes() [][]byte {
|
||||||
func (star *StarResultCol) MustBytes() [][]byte {
|
|
||||||
var res [][]byte
|
var res [][]byte
|
||||||
for _, v := range star.Result {
|
for _, v := range this.Result {
|
||||||
res = append(res, v.([]byte))
|
res = append(res, v.([]byte))
|
||||||
}
|
}
|
||||||
return res
|
return res
|
||||||
}
|
}
|
||||||
|
|
||||||
// MustBool 列查询结果转Bool
|
func (this *StarResultCol) MustBool() []bool {
|
||||||
func (star *StarResultCol) MustBool() []bool {
|
|
||||||
var res []bool
|
var res []bool
|
||||||
var tmp bool
|
var tmp bool
|
||||||
for _, v := range star.Result {
|
for _, v := range this.Result {
|
||||||
switch vtype := v.(type) {
|
switch vtype := v.(type) {
|
||||||
case nil:
|
case nil:
|
||||||
tmp = false
|
tmp = false
|
||||||
case bool:
|
case bool:
|
||||||
tmp = vtype
|
tmp = vtype
|
||||||
case float64:
|
case float64, float32:
|
||||||
if vtype > 0 {
|
if vtype.(float64) > 0 {
|
||||||
tmp = true
|
tmp = true
|
||||||
} else {
|
} else {
|
||||||
tmp = false
|
tmp = false
|
||||||
}
|
}
|
||||||
case float32:
|
case int, int32, int64:
|
||||||
if vtype > 0 {
|
if vtype.(int) > 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 int64:
|
|
||||||
if vtype > 0 {
|
|
||||||
tmp = true
|
tmp = true
|
||||||
} else {
|
} else {
|
||||||
tmp = false
|
tmp = false
|
||||||
@ -102,77 +74,52 @@ func (star *StarResultCol) MustBool() []bool {
|
|||||||
}
|
}
|
||||||
return res
|
return res
|
||||||
}
|
}
|
||||||
|
func (this *StarResultCol) MustFloat32() []float32 {
|
||||||
// MustFloat32 列查询结果转Float32
|
|
||||||
func (star *StarResultCol) MustFloat32() []float32 {
|
|
||||||
var res []float32
|
var res []float32
|
||||||
var tmp float32
|
var tmp float32
|
||||||
for _, v := range star.Result {
|
for _, v := range this.Result {
|
||||||
switch vtype := v.(type) {
|
switch vtype := v.(type) {
|
||||||
case nil:
|
case nil:
|
||||||
tmp = 0
|
tmp = 0
|
||||||
case float32:
|
case float64, float32:
|
||||||
tmp = vtype
|
tmp = float32(vtype.(float64))
|
||||||
case float64:
|
|
||||||
tmp = float32(vtype)
|
|
||||||
case string:
|
case string:
|
||||||
tmps, _ := strconv.ParseFloat(vtype, 32)
|
tmps, _ := strconv.ParseFloat(vtype, 32)
|
||||||
tmp = float32(tmps)
|
tmp = float32(tmps)
|
||||||
case int:
|
case int, int32, int64:
|
||||||
tmp = float32(vtype)
|
tmp = float32(vtype.(int64))
|
||||||
case int32:
|
|
||||||
tmp = float32(vtype)
|
|
||||||
case int64:
|
|
||||||
tmp = float32(vtype)
|
|
||||||
case time.Time:
|
|
||||||
tmp = float32(vtype.Unix())
|
|
||||||
default:
|
default:
|
||||||
tmpt := string(vtype.([]byte))
|
tmp = v.(float32)
|
||||||
tmps, _ := strconv.ParseFloat(tmpt, 32)
|
|
||||||
tmp = float32(tmps)
|
|
||||||
}
|
}
|
||||||
res = append(res, tmp)
|
res = append(res, tmp)
|
||||||
}
|
}
|
||||||
return res
|
return res
|
||||||
}
|
}
|
||||||
|
func (this *StarResultCol) MustFloat64() []float64 {
|
||||||
// MustFloat64 列查询结果转Float64
|
|
||||||
func (star *StarResultCol) MustFloat64() []float64 {
|
|
||||||
var res []float64
|
var res []float64
|
||||||
var tmp float64
|
var tmp float64
|
||||||
for _, v := range star.Result {
|
for _, v := range this.Result {
|
||||||
switch vtype := v.(type) {
|
switch vtype := v.(type) {
|
||||||
case nil:
|
case nil:
|
||||||
tmp = 0
|
tmp = 0
|
||||||
case float64:
|
case float64, float32:
|
||||||
tmp = vtype
|
tmp = vtype.(float64)
|
||||||
case float32:
|
|
||||||
tmp = float64(vtype)
|
|
||||||
case string:
|
case string:
|
||||||
tmp, _ = strconv.ParseFloat(vtype, 64)
|
tmp, _ = strconv.ParseFloat(vtype, 64)
|
||||||
case int:
|
case int, int32, int64:
|
||||||
tmp = float64(vtype)
|
tmp = float64(vtype.(int64))
|
||||||
case int32:
|
|
||||||
tmp = float64(vtype)
|
|
||||||
case int64:
|
|
||||||
tmp = float64(vtype)
|
|
||||||
case time.Time:
|
|
||||||
tmp = float64(vtype.Unix())
|
|
||||||
default:
|
default:
|
||||||
tmpt := string(vtype.([]byte))
|
tmp = v.(float64)
|
||||||
tmps, _ := strconv.ParseFloat(tmpt, 64)
|
|
||||||
tmp = float64(tmps)
|
|
||||||
}
|
}
|
||||||
res = append(res, tmp)
|
res = append(res, tmp)
|
||||||
}
|
}
|
||||||
return res
|
return res
|
||||||
}
|
}
|
||||||
|
|
||||||
// MustString 列查询结果转String
|
func (this *StarResultCol) MustString() []string {
|
||||||
func (star *StarResultCol) MustString() []string {
|
|
||||||
var res []string
|
var res []string
|
||||||
var tmp string
|
var tmp string
|
||||||
for _, v := range star.Result {
|
for _, v := range this.Result {
|
||||||
switch vtype := v.(type) {
|
switch vtype := v.(type) {
|
||||||
case nil:
|
case nil:
|
||||||
tmp = ""
|
tmp = ""
|
||||||
@ -190,8 +137,6 @@ func (star *StarResultCol) MustString() []string {
|
|||||||
tmp = strconv.FormatFloat(float64(vtype), 'f', 10, 32)
|
tmp = strconv.FormatFloat(float64(vtype), 'f', 10, 32)
|
||||||
case int:
|
case int:
|
||||||
tmp = strconv.Itoa(vtype)
|
tmp = strconv.Itoa(vtype)
|
||||||
case time.Time:
|
|
||||||
tmp = vtype.String()
|
|
||||||
default:
|
default:
|
||||||
tmp = string(vtype.([]byte))
|
tmp = string(vtype.([]byte))
|
||||||
}
|
}
|
||||||
@ -200,269 +145,120 @@ func (star *StarResultCol) MustString() []string {
|
|||||||
return res
|
return res
|
||||||
}
|
}
|
||||||
|
|
||||||
// MustInt32 列查询结果转Int32
|
func (this *StarResultCol) MustInt32() []int32 {
|
||||||
func (star *StarResultCol) MustInt32() []int32 {
|
|
||||||
var res []int32
|
var res []int32
|
||||||
var tmp int32
|
var tmp int32
|
||||||
for _, v := range star.Result {
|
for _, v := range this.Result {
|
||||||
switch vtype := v.(type) {
|
switch vtype := v.(type) {
|
||||||
case nil:
|
case nil:
|
||||||
tmp = 0
|
tmp = 0
|
||||||
case float64:
|
case float64, float32:
|
||||||
tmp = int32(vtype)
|
tmp = int32(vtype.(float64))
|
||||||
case float32:
|
|
||||||
tmp = int32(vtype)
|
|
||||||
case string:
|
case string:
|
||||||
tmps, _ := strconv.ParseInt(vtype, 10, 32)
|
tmps, _ := strconv.ParseInt(vtype, 10, 32)
|
||||||
tmp = int32(tmps)
|
tmp = int32(tmps)
|
||||||
case int:
|
case int, int32, int64:
|
||||||
tmp = int32(vtype)
|
tmp = int32(vtype.(int64))
|
||||||
case int64:
|
|
||||||
tmp = int32(vtype)
|
|
||||||
case int32:
|
|
||||||
tmp = vtype
|
|
||||||
case time.Time:
|
|
||||||
tmp = int32(vtype.Unix())
|
|
||||||
default:
|
default:
|
||||||
tmpt := string(vtype.([]byte))
|
tmp = v.(int32)
|
||||||
tmps, _ := strconv.ParseInt(tmpt, 10, 32)
|
|
||||||
tmp = int32(tmps)
|
|
||||||
}
|
}
|
||||||
res = append(res, tmp)
|
res = append(res, tmp)
|
||||||
}
|
}
|
||||||
return res
|
return res
|
||||||
}
|
}
|
||||||
|
func (this *StarResultCol) MustInt64() []int64 {
|
||||||
// MustInt64 列查询结果转Int64
|
|
||||||
func (star *StarResultCol) MustInt64() []int64 {
|
|
||||||
var res []int64
|
var res []int64
|
||||||
var tmp int64
|
var tmp int64
|
||||||
for _, v := range star.Result {
|
for _, v := range this.Result {
|
||||||
switch vtype := v.(type) {
|
switch vtype := v.(type) {
|
||||||
case nil:
|
case nil:
|
||||||
tmp = 0
|
tmp = 0
|
||||||
case float64:
|
case float64, float32:
|
||||||
tmp = int64(vtype)
|
tmp = int64(vtype.(float64))
|
||||||
case float32:
|
|
||||||
tmp = int64(vtype)
|
|
||||||
case string:
|
case string:
|
||||||
tmps, _ := strconv.ParseInt(vtype, 10, 64)
|
tmp, _ = strconv.ParseInt(vtype, 10, 64)
|
||||||
tmp = int64(tmps)
|
case int, int32, int64:
|
||||||
case int:
|
tmp = (vtype.(int64))
|
||||||
tmp = int64(vtype)
|
|
||||||
case int32:
|
|
||||||
tmp = int64(vtype)
|
|
||||||
case int64:
|
|
||||||
tmp = vtype
|
|
||||||
case time.Time:
|
|
||||||
tmp = vtype.Unix()
|
|
||||||
default:
|
default:
|
||||||
tmpt := string(vtype.([]byte))
|
tmp = v.(int64)
|
||||||
tmp, _ = strconv.ParseInt(tmpt, 10, 64)
|
|
||||||
}
|
}
|
||||||
res = append(res, tmp)
|
res = append(res, tmp)
|
||||||
}
|
}
|
||||||
return res
|
return res
|
||||||
}
|
}
|
||||||
|
func (this *StarResultCol) MustInt() []int {
|
||||||
// MustInt 列查询结果转Int
|
|
||||||
func (star *StarResultCol) MustInt() []int {
|
|
||||||
var res []int
|
var res []int
|
||||||
var tmp int
|
var tmp int
|
||||||
for _, v := range star.Result {
|
for _, v := range this.Result {
|
||||||
switch vtype := v.(type) {
|
switch vtype := v.(type) {
|
||||||
case nil:
|
case nil:
|
||||||
tmp = 0
|
tmp = 0
|
||||||
case float64:
|
case float64, float32:
|
||||||
tmp = int(vtype)
|
tmp = int(vtype.(float64))
|
||||||
case float32:
|
|
||||||
tmp = int(vtype)
|
|
||||||
case string:
|
case string:
|
||||||
tmps, _ := strconv.ParseInt(vtype, 10, 64)
|
tmps, _ := strconv.ParseInt(vtype, 10, 64)
|
||||||
tmp = int(tmps)
|
tmp = int(tmps)
|
||||||
case int:
|
case int, int32, int64:
|
||||||
tmp = vtype
|
tmp = int(vtype.(int64))
|
||||||
case int32:
|
|
||||||
tmp = int(vtype)
|
|
||||||
case int64:
|
|
||||||
tmp = int(vtype)
|
|
||||||
case time.Time:
|
|
||||||
tmp = int(vtype.Unix())
|
|
||||||
default:
|
default:
|
||||||
tmpt := string(vtype.([]byte))
|
tmp = int(v.(int64))
|
||||||
tmps, _ := strconv.ParseInt(tmpt, 10, 64)
|
|
||||||
tmp = int(tmps)
|
|
||||||
}
|
}
|
||||||
res = append(res, tmp)
|
res = append(res, tmp)
|
||||||
}
|
}
|
||||||
return res
|
return res
|
||||||
}
|
}
|
||||||
|
|
||||||
// MustDate 列查询结果转Date(time.Time)
|
func (this *StarResult) MustInt64(name string) int64 {
|
||||||
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 = false
|
|
||||||
}
|
|
||||||
res = append(res, tmp)
|
|
||||||
}
|
|
||||||
return res
|
|
||||||
}
|
|
||||||
|
|
||||||
// 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
|
var res int64
|
||||||
num, ok := star.columnref[name]
|
num, ok := this.columnref[name]
|
||||||
if !ok {
|
if !ok {
|
||||||
return 0
|
return 0
|
||||||
}
|
}
|
||||||
tmp := star.Result[num]
|
tmp := this.Result[num]
|
||||||
switch vtype := tmp.(type) {
|
switch vtype := tmp.(type) {
|
||||||
case nil:
|
case nil:
|
||||||
res = 0
|
res = 0
|
||||||
case float64:
|
case float64, float32:
|
||||||
res = int64(vtype)
|
res = int64(vtype.(float64))
|
||||||
case float32:
|
|
||||||
res = int64(vtype)
|
|
||||||
case string:
|
case string:
|
||||||
res, _ = strconv.ParseInt(vtype, 10, 64)
|
res, _ = strconv.ParseInt(vtype, 10, 64)
|
||||||
case int:
|
case int, int32, int64:
|
||||||
res = int64(vtype)
|
res = (vtype.(int64))
|
||||||
case int32:
|
|
||||||
res = int64(vtype)
|
|
||||||
case int64:
|
|
||||||
res = vtype
|
|
||||||
case time.Time:
|
|
||||||
res = int64(vtype.Unix())
|
|
||||||
default:
|
default:
|
||||||
res, _ = strconv.ParseInt(string(tmp.([]byte)), 10, 64)
|
res, _ = strconv.ParseInt(string(tmp.([]byte)), 10, 64)
|
||||||
}
|
}
|
||||||
return res
|
return res
|
||||||
}
|
}
|
||||||
|
func (this *StarResult) MustInt32(name string) int32 {
|
||||||
// MustInt32 列查询结果转Int32
|
|
||||||
func (star *StarResult) MustInt32(name string) int32 {
|
|
||||||
var res int32
|
var res int32
|
||||||
num, ok := star.columnref[name]
|
num, ok := this.columnref[name]
|
||||||
if !ok {
|
if !ok {
|
||||||
return 0
|
return 0
|
||||||
}
|
}
|
||||||
tmp := star.Result[num]
|
tmp := this.Result[num]
|
||||||
switch vtype := tmp.(type) {
|
switch vtype := tmp.(type) {
|
||||||
case nil:
|
case nil:
|
||||||
res = 0
|
res = 0
|
||||||
case float64:
|
case float64, float32:
|
||||||
res = int32(vtype)
|
res = int32(vtype.(float64))
|
||||||
case float32:
|
|
||||||
res = int32(vtype)
|
|
||||||
case string:
|
case string:
|
||||||
ress, _ := strconv.ParseInt(vtype, 10, 32)
|
ress, _ := strconv.ParseInt(vtype, 10, 32)
|
||||||
res = int32(ress)
|
res = int32(ress)
|
||||||
case int:
|
case int, int32, int64:
|
||||||
res = int32(vtype)
|
res = int32(vtype.(int64))
|
||||||
case int32:
|
|
||||||
res = vtype
|
|
||||||
case int64:
|
|
||||||
res = int32(vtype)
|
|
||||||
case time.Time:
|
|
||||||
res = int32(vtype.Unix())
|
|
||||||
default:
|
default:
|
||||||
ress, _ := strconv.ParseInt(string(tmp.([]byte)), 10, 32)
|
ress, _ := strconv.ParseInt(string(tmp.([]byte)), 10, 32)
|
||||||
res = int32(ress)
|
res = int32(ress)
|
||||||
}
|
}
|
||||||
return res
|
return res
|
||||||
}
|
}
|
||||||
|
func (this *StarResult) MustString(name string) string {
|
||||||
// MustString 列查询结果转string
|
|
||||||
func (star *StarResult) MustString(name string) string {
|
|
||||||
var res string
|
var res string
|
||||||
num, ok := star.columnref[name]
|
num, ok := this.columnref[name]
|
||||||
if !ok {
|
if !ok {
|
||||||
return ""
|
return ""
|
||||||
}
|
}
|
||||||
switch vtype := star.Result[num].(type) {
|
switch vtype := this.Result[num].(type) {
|
||||||
case nil:
|
case nil:
|
||||||
res = ""
|
res = ""
|
||||||
case string:
|
case string:
|
||||||
@ -479,52 +275,40 @@ func (star *StarResult) MustString(name string) string {
|
|||||||
res = strconv.FormatFloat(float64(vtype), 'f', 10, 32)
|
res = strconv.FormatFloat(float64(vtype), 'f', 10, 32)
|
||||||
case int:
|
case int:
|
||||||
res = strconv.Itoa(vtype)
|
res = strconv.Itoa(vtype)
|
||||||
case time.Time:
|
|
||||||
res = vtype.String()
|
|
||||||
default:
|
default:
|
||||||
res = string(vtype.([]byte))
|
res = string(vtype.([]byte))
|
||||||
}
|
}
|
||||||
return res
|
return res
|
||||||
}
|
}
|
||||||
|
|
||||||
// MustFloat64 列查询结果转float64
|
func (this *StarResult) MustFloat64(name string) float64 {
|
||||||
func (star *StarResult) MustFloat64(name string) float64 {
|
|
||||||
var res float64
|
var res float64
|
||||||
num, ok := star.columnref[name]
|
num, ok := this.columnref[name]
|
||||||
if !ok {
|
if !ok {
|
||||||
return 0
|
return 0
|
||||||
}
|
}
|
||||||
switch vtype := star.Result[num].(type) {
|
switch vtype := this.Result[num].(type) {
|
||||||
case nil:
|
case nil:
|
||||||
res = 0
|
res = 0
|
||||||
case string:
|
case string:
|
||||||
res, _ = strconv.ParseFloat(vtype, 64)
|
res, _ = strconv.ParseFloat(vtype, 64)
|
||||||
case float64:
|
case float64:
|
||||||
res = vtype
|
res = vtype
|
||||||
case int:
|
case int, int64, int32, float32:
|
||||||
res = float64(vtype)
|
res = vtype.(float64)
|
||||||
case int64:
|
|
||||||
res = float64(vtype)
|
|
||||||
case int32:
|
|
||||||
res = float64(vtype)
|
|
||||||
case float32:
|
|
||||||
res = float64(vtype)
|
|
||||||
case time.Time:
|
|
||||||
res = float64(vtype.Unix())
|
|
||||||
default:
|
default:
|
||||||
res, _ = strconv.ParseFloat(string(vtype.([]byte)), 64)
|
res, _ = strconv.ParseFloat(string(vtype.([]byte)), 64)
|
||||||
}
|
}
|
||||||
return res
|
return res
|
||||||
}
|
}
|
||||||
|
|
||||||
// MustFloat32 列查询结果转float32
|
func (this *StarResult) MustFloat32(name string) float32 {
|
||||||
func (star *StarResult) MustFloat32(name string) float32 {
|
|
||||||
var res float32
|
var res float32
|
||||||
num, ok := star.columnref[name]
|
num, ok := this.columnref[name]
|
||||||
if !ok {
|
if !ok {
|
||||||
return 0
|
return 0
|
||||||
}
|
}
|
||||||
switch vtype := star.Result[num].(type) {
|
switch vtype := this.Result[num].(type) {
|
||||||
case nil:
|
case nil:
|
||||||
res = 0
|
res = 0
|
||||||
case string:
|
case string:
|
||||||
@ -534,14 +318,8 @@ func (star *StarResult) MustFloat32(name string) float32 {
|
|||||||
res = float32(vtype)
|
res = float32(vtype)
|
||||||
case float32:
|
case float32:
|
||||||
res = vtype
|
res = vtype
|
||||||
case int:
|
case int, int64, int32:
|
||||||
res = float32(vtype)
|
res = vtype.(float32)
|
||||||
case int64:
|
|
||||||
res = float32(vtype)
|
|
||||||
case int32:
|
|
||||||
res = float32(vtype)
|
|
||||||
case time.Time:
|
|
||||||
res = float32(vtype.Unix())
|
|
||||||
default:
|
default:
|
||||||
tmp, _ := strconv.ParseFloat(string(vtype.([]byte)), 32)
|
tmp, _ := strconv.ParseFloat(string(vtype.([]byte)), 32)
|
||||||
res = float32(tmp)
|
res = float32(tmp)
|
||||||
@ -549,32 +327,23 @@ func (star *StarResult) MustFloat32(name string) float32 {
|
|||||||
return res
|
return res
|
||||||
}
|
}
|
||||||
|
|
||||||
// MustInt 列查询结果转int
|
func (this *StarResult) MustInt(name string) int {
|
||||||
func (star *StarResult) MustInt(name string) int {
|
|
||||||
var res int
|
var res int
|
||||||
num, ok := star.columnref[name]
|
num, ok := this.columnref[name]
|
||||||
if !ok {
|
if !ok {
|
||||||
return 0
|
return 0
|
||||||
}
|
}
|
||||||
tmp := star.Result[num]
|
tmp := this.Result[num]
|
||||||
switch vtype := tmp.(type) {
|
switch vtype := tmp.(type) {
|
||||||
case nil:
|
case nil:
|
||||||
res = 0
|
res = 0
|
||||||
case float64:
|
case float64, float32:
|
||||||
res = int(vtype)
|
res = int(vtype.(float64))
|
||||||
case float32:
|
|
||||||
res = int(vtype)
|
|
||||||
case string:
|
case string:
|
||||||
ress, _ := strconv.ParseInt(vtype, 10, 64)
|
ress, _ := strconv.ParseInt(vtype, 10, 64)
|
||||||
res = int(ress)
|
res = int(ress)
|
||||||
case int:
|
case int, int32, int64:
|
||||||
res = vtype
|
res = int(vtype.(int64))
|
||||||
case int32:
|
|
||||||
res = int(vtype)
|
|
||||||
case int64:
|
|
||||||
res = int(vtype)
|
|
||||||
case time.Time:
|
|
||||||
res = int(vtype.Unix())
|
|
||||||
default:
|
default:
|
||||||
ress, _ := strconv.ParseInt(string(tmp.([]byte)), 10, 64)
|
ress, _ := strconv.ParseInt(string(tmp.([]byte)), 10, 64)
|
||||||
res = int(ress)
|
res = int(ress)
|
||||||
@ -582,45 +351,26 @@ func (star *StarResult) MustInt(name string) int {
|
|||||||
return res
|
return res
|
||||||
}
|
}
|
||||||
|
|
||||||
// MustBool 列查询结果转bool
|
func (this *StarResult) MustBool(name string) bool {
|
||||||
func (star *StarResult) MustBool(name string) bool {
|
|
||||||
var res bool
|
var res bool
|
||||||
num, ok := star.columnref[name]
|
num, ok := this.columnref[name]
|
||||||
if !ok {
|
if !ok {
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
tmp := star.Result[num]
|
tmp := this.Result[num]
|
||||||
switch vtype := tmp.(type) {
|
switch vtype := tmp.(type) {
|
||||||
case nil:
|
case nil:
|
||||||
res = false
|
res = false
|
||||||
case bool:
|
case bool:
|
||||||
res = vtype
|
res = vtype
|
||||||
case float64:
|
case float64, float32:
|
||||||
if vtype > 0 {
|
if vtype.(float64) > 0 {
|
||||||
res = true
|
res = true
|
||||||
} else {
|
} else {
|
||||||
res = false
|
res = false
|
||||||
}
|
}
|
||||||
case float32:
|
case int, int32, int64:
|
||||||
if vtype > 0 {
|
if vtype.(int) > 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 int64:
|
|
||||||
if vtype > 0 {
|
|
||||||
res = true
|
res = true
|
||||||
} else {
|
} else {
|
||||||
res = false
|
res = false
|
||||||
@ -632,71 +382,65 @@ func (star *StarResult) MustBool(name string) bool {
|
|||||||
}
|
}
|
||||||
return res
|
return res
|
||||||
}
|
}
|
||||||
|
func (this *StarResult) MustBytes(name string) []byte {
|
||||||
// MustBytes 列查询结果转byte
|
num, ok := this.columnref[name]
|
||||||
func (star *StarResult) MustBytes(name string) []byte {
|
|
||||||
num, ok := star.columnref[name]
|
|
||||||
if !ok {
|
if !ok {
|
||||||
return []byte{}
|
return []byte{}
|
||||||
}
|
}
|
||||||
res := star.Result[num].([]byte)
|
res := this.Result[num].([]byte)
|
||||||
return res
|
return res
|
||||||
}
|
}
|
||||||
|
|
||||||
// Rescan 重新分析结果集
|
func (this *StarRows) Rescan() {
|
||||||
func (star *StarRows) Rescan() {
|
this.parserows()
|
||||||
star.parserows()
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Col 选择需要进行操作的数据结果列
|
func (this *StarRows) Col(name string) *StarResultCol {
|
||||||
func (star *StarRows) Col(name string) *StarResultCol {
|
|
||||||
result := new(StarResultCol)
|
result := new(StarResultCol)
|
||||||
if _, ok := star.columnref[name]; !ok {
|
if _, ok := this.columnref[name]; !ok {
|
||||||
return result
|
return result
|
||||||
}
|
}
|
||||||
var rescol []interface{}
|
var rescol []interface{}
|
||||||
for _, v := range star.result {
|
for _, v := range this.result {
|
||||||
rescol = append(rescol, v[star.columnref[name]])
|
rescol = append(rescol, v[this.columnref[name]])
|
||||||
}
|
}
|
||||||
result.Result = rescol
|
result.Result = rescol
|
||||||
return result
|
return result
|
||||||
}
|
}
|
||||||
|
|
||||||
// Row 选择需要进行操作的数据结果行
|
func (this *StarRows) Row(id int) *StarResult {
|
||||||
func (star *StarRows) Row(id int) *StarResult {
|
|
||||||
result := new(StarResult)
|
result := new(StarResult)
|
||||||
if id+1 > len(star.result) {
|
if id+1 > len(this.result) {
|
||||||
return result
|
return result
|
||||||
}
|
}
|
||||||
result.Result = star.result[id]
|
result.Result = this.result[id]
|
||||||
result.Columns = star.Columns
|
result.Columns = this.Columns
|
||||||
result.ColumnsType = star.ColumnsType
|
result.ColumnsType = this.ColumnsType
|
||||||
result.columnref = star.columnref
|
result.columnref = this.columnref
|
||||||
return result
|
return result
|
||||||
}
|
}
|
||||||
|
|
||||||
// Close 关闭打开的结果集
|
func (this *StarRows) Close() error {
|
||||||
func (star *StarRows) Close() error {
|
return this.Rows.Close()
|
||||||
return star.Rows.Close()
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (star *StarRows) parserows() {
|
func (this *StarRows) parserows() {
|
||||||
star.result = [][]interface{}{}
|
this.result = [][]interface{}{}
|
||||||
star.columnref = make(map[string]int)
|
this.columnref = make(map[string]int)
|
||||||
star.StringResult = []map[string]string{}
|
this.StringResult = []map[string]string{}
|
||||||
star.Columns, _ = star.Rows.Columns()
|
this.Columns, _ = this.Rows.Columns()
|
||||||
types, _ := star.Rows.ColumnTypes()
|
types, _ := this.Rows.ColumnTypes()
|
||||||
for _, v := range types {
|
for _, v := range types {
|
||||||
star.ColumnsType = append(star.ColumnsType, v.ScanType())
|
this.ColumnsType = append(this.ColumnsType, v.ScanType())
|
||||||
}
|
}
|
||||||
scanArgs := make([]interface{}, len(star.Columns))
|
scanArgs := make([]interface{}, len(this.Columns))
|
||||||
values := make([]interface{}, len(star.Columns))
|
values := make([]interface{}, len(this.Columns))
|
||||||
for i := range values {
|
for i, _ := range values {
|
||||||
star.columnref[star.Columns[i]] = i
|
this.columnref[this.Columns[i]] = i
|
||||||
scanArgs[i] = &values[i]
|
scanArgs[i] = &values[i]
|
||||||
}
|
}
|
||||||
for star.Rows.Next() {
|
for this.Rows.Next() {
|
||||||
if err := star.Rows.Scan(scanArgs...); err != nil {
|
if err := this.Rows.Scan(scanArgs...); err != nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
record := make(map[string]string)
|
record := make(map[string]string)
|
||||||
@ -704,39 +448,28 @@ func (star *StarRows) parserows() {
|
|||||||
for i, col := range values {
|
for i, col := range values {
|
||||||
rescopy = append(rescopy, col)
|
rescopy = append(rescopy, col)
|
||||||
switch vtype := col.(type) {
|
switch vtype := col.(type) {
|
||||||
case float32:
|
|
||||||
record[star.Columns[i]] = strconv.FormatFloat(float64(vtype), 'f', -1, 64)
|
|
||||||
case float64:
|
case float64:
|
||||||
record[star.Columns[i]] = strconv.FormatFloat(vtype, 'f', -1, 64)
|
record[this.Columns[i]] = strconv.FormatFloat(vtype, 'f', -1, 64)
|
||||||
case int64:
|
case int64:
|
||||||
record[star.Columns[i]] = strconv.FormatInt(vtype, 10)
|
record[this.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:
|
case string:
|
||||||
record[star.Columns[i]] = vtype
|
record[this.Columns[i]] = vtype
|
||||||
case bool:
|
|
||||||
record[star.Columns[i]] = strconv.FormatBool(vtype)
|
|
||||||
case time.Time:
|
|
||||||
record[star.Columns[i]] = vtype.String()
|
|
||||||
case nil:
|
case nil:
|
||||||
record[star.Columns[i]] = ""
|
record[this.Columns[i]] = ""
|
||||||
default:
|
default:
|
||||||
record[star.Columns[i]] = string(vtype.([]byte))
|
record[this.Columns[i]] = string(vtype.([]byte))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
star.result = append(star.result, rescopy)
|
this.result = append(this.result, rescopy)
|
||||||
star.StringResult = append(star.StringResult, record)
|
this.StringResult = append(this.StringResult, record)
|
||||||
}
|
}
|
||||||
star.Length = len(star.StringResult)
|
this.Length = len(this.StringResult)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Query 进行Query操作
|
func (this *StarDB) Query(args ...interface{}) (*StarRows, error) {
|
||||||
func (star *StarDB) Query(args ...interface{}) (*StarRows, error) {
|
|
||||||
var err error
|
var err error
|
||||||
effect := new(StarRows)
|
effect := new(StarRows)
|
||||||
if err = star.DB.Ping(); err != nil {
|
if err = this.DB.Ping(); err != nil {
|
||||||
return effect, err
|
return effect, err
|
||||||
}
|
}
|
||||||
if len(args) == 0 {
|
if len(args) == 0 {
|
||||||
@ -744,15 +477,15 @@ func (star *StarDB) Query(args ...interface{}) (*StarRows, error) {
|
|||||||
}
|
}
|
||||||
if len(args) == 1 {
|
if len(args) == 1 {
|
||||||
sql := args[0]
|
sql := args[0]
|
||||||
if star.Rows, err = star.DB.Query(sql.(string)); err != nil {
|
if this.Rows, err = this.DB.Query(sql.(string)); err != nil {
|
||||||
return effect, err
|
return effect, err
|
||||||
}
|
}
|
||||||
effect.Rows = star.Rows
|
effect.Rows = this.Rows
|
||||||
effect.parserows()
|
effect.parserows()
|
||||||
return effect, nil
|
return effect, nil
|
||||||
}
|
}
|
||||||
sql := args[0]
|
sql := args[0]
|
||||||
stmt, err := star.DB.Prepare(sql.(string))
|
stmt, err := this.DB.Prepare(sql.(string))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return effect, err
|
return effect, err
|
||||||
}
|
}
|
||||||
@ -766,38 +499,35 @@ func (star *StarDB) Query(args ...interface{}) (*StarRows, error) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if star.Rows, err = stmt.Query(para...); err != nil {
|
if this.Rows, err = stmt.Query(para...); err != nil {
|
||||||
return effect, err
|
return effect, err
|
||||||
}
|
}
|
||||||
effect.Rows = star.Rows
|
effect.Rows = this.Rows
|
||||||
effect.parserows()
|
effect.parserows()
|
||||||
return effect, nil
|
return effect, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// Open 打开一个新的数据库
|
func (this *StarDB) Open(Method, ConnStr string) error {
|
||||||
func (star *StarDB) Open(Method, ConnStr string) error {
|
|
||||||
var err error
|
var err error
|
||||||
star.DB, err = sql.Open(Method, ConnStr)
|
this.DB, err = sql.Open(Method, ConnStr)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
err = star.DB.Ping()
|
err = this.DB.Ping()
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
// Close 关闭打开的数据库
|
func (this *StarDB) Close() error {
|
||||||
func (star *StarDB) Close() error {
|
if err := this.DB.Close(); err != nil {
|
||||||
if err := star.DB.Close(); err != nil {
|
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
return star.DB.Close()
|
return this.DB.Close()
|
||||||
}
|
}
|
||||||
|
|
||||||
// Exec 执行Exec操作
|
func (this *StarDB) Exec(args ...interface{}) (sql.Result, error) {
|
||||||
func (star *StarDB) Exec(args ...interface{}) (sql.Result, error) {
|
|
||||||
var err error
|
var err error
|
||||||
var effect sql.Result
|
var effect sql.Result
|
||||||
if err = star.DB.Ping(); err != nil {
|
if err = this.DB.Ping(); err != nil {
|
||||||
return effect, err
|
return effect, err
|
||||||
}
|
}
|
||||||
if len(args) == 0 {
|
if len(args) == 0 {
|
||||||
@ -805,17 +535,17 @@ func (star *StarDB) Exec(args ...interface{}) (sql.Result, error) {
|
|||||||
}
|
}
|
||||||
if len(args) == 1 {
|
if len(args) == 1 {
|
||||||
sql := args[0]
|
sql := args[0]
|
||||||
if _, err = star.DB.Exec(sql.(string)); err != nil {
|
if _, err = this.DB.Exec(sql.(string)); err != nil {
|
||||||
return effect, err
|
return effect, err
|
||||||
}
|
}
|
||||||
return effect, nil
|
return effect, nil
|
||||||
}
|
}
|
||||||
sql := args[0]
|
sql := args[0]
|
||||||
stmt, err := star.DB.Prepare(sql.(string))
|
stmt, err := this.DB.Prepare(sql.(string))
|
||||||
|
defer stmt.Close()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return effect, err
|
return effect, err
|
||||||
}
|
}
|
||||||
defer stmt.Close()
|
|
||||||
var para []interface{}
|
var para []interface{}
|
||||||
for k, v := range args {
|
for k, v := range args {
|
||||||
if k != 0 {
|
if k != 0 {
|
||||||
@ -831,7 +561,6 @@ func (star *StarDB) Exec(args ...interface{}) (sql.Result, error) {
|
|||||||
return effect, nil
|
return effect, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// FetchAll 把结果集全部转为key-value型<string>数据
|
|
||||||
func FetchAll(rows *sql.Rows) (error, map[int]map[string]string) {
|
func FetchAll(rows *sql.Rows) (error, map[int]map[string]string) {
|
||||||
var ii int = 0
|
var ii int = 0
|
||||||
records := make(map[int]map[string]string)
|
records := make(map[int]map[string]string)
|
||||||
@ -869,7 +598,6 @@ func FetchAll(rows *sql.Rows) (error, map[int]map[string]string) {
|
|||||||
return nil, records
|
return nil, records
|
||||||
}
|
}
|
||||||
|
|
||||||
// OpenDB 打开一个数据库
|
|
||||||
func OpenDB(Method, ConnStr string) error {
|
func OpenDB(Method, ConnStr string) error {
|
||||||
var err error
|
var err error
|
||||||
DBRes, err = sql.Open(Method, ConnStr)
|
DBRes, err = sql.Open(Method, ConnStr)
|
||||||
@ -879,14 +607,11 @@ func OpenDB(Method, ConnStr string) error {
|
|||||||
err = DBRes.Ping()
|
err = DBRes.Ping()
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
// CloseDB 关闭打开的数据库
|
|
||||||
func CloseDB() {
|
func CloseDB() {
|
||||||
DBRes.Close()
|
DBRes.Close()
|
||||||
DBRows.Close()
|
DBRows.Close()
|
||||||
}
|
}
|
||||||
|
|
||||||
// Query 进行数据库查询操作
|
|
||||||
func Query(args ...interface{}) (error, map[int]map[string]string) {
|
func Query(args ...interface{}) (error, map[int]map[string]string) {
|
||||||
var err error
|
var err error
|
||||||
records := make(map[int]map[string]string)
|
records := make(map[int]map[string]string)
|
||||||
@ -925,7 +650,6 @@ func Query(args ...interface{}) (error, map[int]map[string]string) {
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// DBExec 进行数据库EXEC查询操作
|
|
||||||
func DBExec(args ...interface{}) error {
|
func DBExec(args ...interface{}) error {
|
||||||
var err error
|
var err error
|
||||||
if err = DBRes.Ping(); err != nil {
|
if err = DBRes.Ping(); err != nil {
|
||||||
|
239
ini.go
239
ini.go
@ -3,11 +3,9 @@ package starainrt
|
|||||||
import (
|
import (
|
||||||
"bufio"
|
"bufio"
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
|
||||||
"io"
|
"io"
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"os"
|
"os"
|
||||||
"reflect"
|
|
||||||
"regexp"
|
"regexp"
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
@ -202,8 +200,7 @@ jump:
|
|||||||
type StarCfg struct {
|
type StarCfg struct {
|
||||||
Data []*CfgSegment
|
Data []*CfgSegment
|
||||||
segmap map[string]int
|
segmap map[string]int
|
||||||
//nodemap map[int]map[string]int
|
nodemap map[int]map[string]int
|
||||||
segid int
|
|
||||||
}
|
}
|
||||||
|
|
||||||
type CfgNode struct {
|
type CfgNode struct {
|
||||||
@ -217,8 +214,7 @@ type CfgSegment struct {
|
|||||||
cmap map[string]int
|
cmap map[string]int
|
||||||
Comment string
|
Comment string
|
||||||
Node []*CfgNode
|
Node []*CfgNode
|
||||||
nodeid int
|
InsertNode []*CfgNode
|
||||||
//InsertNode []*CfgNode
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (this *StarCfg) ParseFromFile(filepath string) error {
|
func (this *StarCfg) ParseFromFile(filepath string) error {
|
||||||
@ -238,33 +234,28 @@ func (this *StarCfg) WriteToFile(filepath string) error {
|
|||||||
return ioutil.WriteFile(filepath, data, 0644)
|
return ioutil.WriteFile(filepath, data, 0644)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Parse 生成INI文件结构
|
|
||||||
func (this *StarCfg) Parse(data []byte) {
|
func (this *StarCfg) Parse(data []byte) {
|
||||||
var newnode *CfgNode
|
var newnode *CfgNode
|
||||||
this.segid = 0 //segment序号
|
segint := 0
|
||||||
nodeint := 0
|
nodeint := 0
|
||||||
this.segmap = make(map[string]int) //segment名 序号
|
this.segmap = make(map[string]int)
|
||||||
//this.nodemap = make(map[int]map[string]int)
|
this.nodemap = make(map[int]map[string]int)
|
||||||
nodemap := make(map[string]int)
|
strdata := string(data)
|
||||||
strdata := string(data) //转换成字符串
|
list := strings.Split(strdata, "\n")
|
||||||
list := strings.Split(strdata, "\n") //分割
|
|
||||||
newseg := new(CfgSegment)
|
newseg := new(CfgSegment)
|
||||||
newseg.Name = "unnamed" //默认名
|
newseg.Name = "unnamed"
|
||||||
//newseg.InsertNode = []*CfgNode{}
|
newseg.InsertNode = []*CfgNode{}
|
||||||
this.segmap["unnamed"] = 0
|
this.segmap["unnamed"] = 0
|
||||||
lastiseg := true
|
lastiseg := true
|
||||||
for _, v := range list {
|
for _, v := range list {
|
||||||
istrans := false
|
istrans := false
|
||||||
isseg := false //是否为新seg
|
isseg := false
|
||||||
isnote := false //是否为注释
|
isnote := false
|
||||||
isequal := false // 是否为等号
|
isequal := false
|
||||||
tmp1 := []rune{}
|
tmp1 := []rune{}
|
||||||
tmp2 := []rune{}
|
tmp2 := []rune{}
|
||||||
note := []rune{}
|
note := []rune{}
|
||||||
v = strings.TrimSpace(v)
|
v = strings.TrimSpace(v)
|
||||||
if len(v) > 0 && v[0:1] == ";" {
|
|
||||||
isnote = true //首字母是;时,代表注释
|
|
||||||
}
|
|
||||||
for k, v2 := range v {
|
for k, v2 := range v {
|
||||||
if k == 0 {
|
if k == 0 {
|
||||||
if v2 == '[' {
|
if v2 == '[' {
|
||||||
@ -272,7 +263,7 @@ func (this *StarCfg) Parse(data []byte) {
|
|||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if v2 == '=' && (!istrans && !isnote && !isequal) {
|
if v2 == '=' && (!istrans && !isnote) {
|
||||||
isequal = true
|
isequal = true
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
@ -300,18 +291,14 @@ func (this *StarCfg) Parse(data []byte) {
|
|||||||
}
|
}
|
||||||
if isseg {
|
if isseg {
|
||||||
this.Data = append(this.Data, newseg)
|
this.Data = append(this.Data, newseg)
|
||||||
newseg.nodeid = nodeint
|
|
||||||
//newseg.cmap = make(map[string]int)
|
|
||||||
newseg.cmap = nodemap
|
|
||||||
nodemap = make(map[string]int)
|
|
||||||
newseg = new(CfgSegment)
|
newseg = new(CfgSegment)
|
||||||
//newseg.InsertNode = []*CfgNode{}
|
newseg.InsertNode = []*CfgNode{}
|
||||||
newseg.Name = strings.TrimSpace(string(tmp1))
|
newseg.Name = strings.TrimSpace(string(tmp1))
|
||||||
if isnote {
|
if isnote {
|
||||||
newseg.Comment = strings.TrimSpace(string(note))
|
newseg.Comment = strings.TrimSpace(string(note))
|
||||||
}
|
}
|
||||||
this.segid++
|
segint++
|
||||||
this.segmap[newseg.Name] = this.segid
|
this.segmap[newseg.Name] = segint
|
||||||
nodeint = 0
|
nodeint = 0
|
||||||
lastiseg = true
|
lastiseg = true
|
||||||
continue
|
continue
|
||||||
@ -324,7 +311,10 @@ func (this *StarCfg) Parse(data []byte) {
|
|||||||
newnode.Comment = strings.TrimSpace(string(note))
|
newnode.Comment = strings.TrimSpace(string(note))
|
||||||
}
|
}
|
||||||
newseg.Node = append(newseg.Node, newnode)
|
newseg.Node = append(newseg.Node, newnode)
|
||||||
nodemap[newnode.Key] = nodeint
|
if this.nodemap[segint] == nil {
|
||||||
|
this.nodemap[segint] = make(map[string]int)
|
||||||
|
}
|
||||||
|
this.nodemap[segint][newnode.Key] = nodeint
|
||||||
nodeint++
|
nodeint++
|
||||||
lastiseg = false
|
lastiseg = false
|
||||||
continue
|
continue
|
||||||
@ -340,137 +330,18 @@ func (this *StarCfg) Parse(data []byte) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
newseg.nodeid = nodeint
|
|
||||||
//newseg.cmap = make(map[string]int)
|
|
||||||
newseg.cmap = nodemap
|
|
||||||
this.Data = append(this.Data, newseg)
|
this.Data = append(this.Data, newseg)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Unmarshal 输出结果到结构体中
|
|
||||||
func (cfg *StarCfg) Unmarshal(ins interface{}) error {
|
|
||||||
var structSet func(t reflect.Type, v reflect.Value) error
|
|
||||||
t := reflect.TypeOf(ins)
|
|
||||||
v := reflect.ValueOf(ins).Elem()
|
|
||||||
if v.Kind() != reflect.Struct {
|
|
||||||
return errors.New("Not a Struct")
|
|
||||||
}
|
|
||||||
if t.Kind() != reflect.Ptr || !v.CanSet() {
|
|
||||||
return errors.New("Cannot Write!")
|
|
||||||
}
|
|
||||||
t = t.Elem()
|
|
||||||
structSet = func(t reflect.Type, v reflect.Value) error {
|
|
||||||
for i := 0; i < t.NumField(); i++ {
|
|
||||||
tp := t.Field(i)
|
|
||||||
vl := v.Field(i)
|
|
||||||
if !vl.CanSet() {
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
if vl.Type().Kind() == reflect.Struct {
|
|
||||||
structSet(vl.Type(), vl)
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
seg := tp.Tag.Get("seg")
|
|
||||||
key := tp.Tag.Get("key")
|
|
||||||
if seg == "" || key == "" {
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
if _, ok := cfg.segmap[seg]; !ok {
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
segs := cfg.Data[cfg.segmap[seg]]
|
|
||||||
if segs.Get(key) == "" {
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
switch vl.Kind() {
|
|
||||||
case reflect.String:
|
|
||||||
vl.SetString(segs.Get(key))
|
|
||||||
case reflect.Int, reflect.Int32, reflect.Int64:
|
|
||||||
vl.SetInt(segs.Int64(key))
|
|
||||||
case reflect.Float32, reflect.Float64:
|
|
||||||
vl.SetFloat(segs.Float64(key))
|
|
||||||
case reflect.Bool:
|
|
||||||
vl.SetBool(segs.Bool(key))
|
|
||||||
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
|
|
||||||
vl.SetUint(uint64(segs.Int64(key)))
|
|
||||||
default:
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
return structSet(t, v)
|
|
||||||
}
|
|
||||||
|
|
||||||
// Marshal 输出结果到结构体中
|
|
||||||
func (cfg *StarCfg) Marshal(ins interface{}) ([]byte, error) {
|
|
||||||
var structSet func(t reflect.Type, v reflect.Value)
|
|
||||||
t := reflect.TypeOf(ins)
|
|
||||||
v := reflect.ValueOf(ins)
|
|
||||||
if v.Kind() != reflect.Struct {
|
|
||||||
return nil, errors.New("Not a Struct")
|
|
||||||
}
|
|
||||||
if t.Kind() == reflect.Ptr {
|
|
||||||
t = t.Elem()
|
|
||||||
v = v.Elem()
|
|
||||||
}
|
|
||||||
structSet = func(t reflect.Type, v reflect.Value) {
|
|
||||||
for i := 0; i < t.NumField(); i++ {
|
|
||||||
var seg, key, comment string = "", "", ""
|
|
||||||
tp := t.Field(i)
|
|
||||||
vl := v.Field(i)
|
|
||||||
if vl.Type().Kind() == reflect.Struct {
|
|
||||||
structSet(vl.Type(), vl)
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
seg = tp.Tag.Get("seg")
|
|
||||||
key = tp.Tag.Get("key")
|
|
||||||
comment = tp.Tag.Get("comment")
|
|
||||||
if seg == "" || key == "" {
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
if _, ok := cfg.segmap[seg]; !ok {
|
|
||||||
cfg.AddSeg(seg)
|
|
||||||
}
|
|
||||||
cfg.Seg(seg).Set(key, fmt.Sprint(vl), comment)
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
structSet(t, v)
|
|
||||||
return cfg.Build(), nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (this *StarCfg) Seg(name string) *CfgSegment {
|
func (this *StarCfg) Seg(name string) *CfgSegment {
|
||||||
if _, ok := this.segmap[name]; !ok {
|
if _, ok := this.segmap[name]; !ok {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
seg := this.Data[this.segmap[name]]
|
seg := this.Data[this.segmap[name]]
|
||||||
|
if seg.Name == name {
|
||||||
|
seg.cmap = this.nodemap[this.segmap[name]]
|
||||||
return seg
|
return seg
|
||||||
}
|
|
||||||
|
|
||||||
func (cfg *StarCfg) AddSeg(name string) *CfgSegment {
|
|
||||||
if _, ok := cfg.segmap[name]; !ok {
|
|
||||||
newseg := new(CfgSegment)
|
|
||||||
newseg.Name = name
|
|
||||||
newseg.cmap = make(map[string]int)
|
|
||||||
cfg.Data = append(cfg.Data, newseg)
|
|
||||||
cfg.segid++
|
|
||||||
if cfg.segmap == nil {
|
|
||||||
cfg.segid = 0
|
|
||||||
cfg.segmap = make(map[string]int)
|
|
||||||
}
|
}
|
||||||
cfg.segmap[newseg.Name] = cfg.segid
|
|
||||||
return newseg
|
|
||||||
}
|
|
||||||
seg := cfg.Data[cfg.segmap[name]]
|
|
||||||
return seg
|
|
||||||
}
|
|
||||||
|
|
||||||
func (cfg *StarCfg) DeleteSeg(name string) error {
|
|
||||||
if _, ok := cfg.segmap[name]; !ok {
|
|
||||||
return errors.New("Seg Not Exists")
|
|
||||||
}
|
|
||||||
cfg.Data[cfg.segmap[name]] = nil
|
|
||||||
delete(cfg.segmap, name)
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -478,9 +349,6 @@ func (this *StarCfg) Build() []byte {
|
|||||||
var res, comm string
|
var res, comm string
|
||||||
for _, v := range this.Data {
|
for _, v := range this.Data {
|
||||||
comm = ""
|
comm = ""
|
||||||
if v == nil {
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
if v.Name != "unnamed" {
|
if v.Name != "unnamed" {
|
||||||
res += `[ ` + this.repkv(v.Name) + ` ]`
|
res += `[ ` + this.repkv(v.Name) + ` ]`
|
||||||
}
|
}
|
||||||
@ -492,11 +360,19 @@ func (this *StarCfg) Build() []byte {
|
|||||||
|
|
||||||
}
|
}
|
||||||
res += comm + "\n"
|
res += comm + "\n"
|
||||||
for _, v2 := range v.Node {
|
|
||||||
comm = ""
|
comm = ""
|
||||||
if v2 == nil {
|
for _, v2 := range v.Node {
|
||||||
continue
|
res += this.repkv(v2.Key) + " = " + this.repkv(v2.Value)
|
||||||
|
if v2.Comment != "" {
|
||||||
|
comm = strings.Replace(v2.Comment, "\n", "\n#", -1)
|
||||||
|
if comm[0:1] != "\n" {
|
||||||
|
comm = " #" + comm
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
res += comm + "\n"
|
||||||
|
}
|
||||||
|
comm = ""
|
||||||
|
for _, v2 := range v.InsertNode {
|
||||||
res += this.repkv(v2.Key) + " = " + this.repkv(v2.Value)
|
res += this.repkv(v2.Key) + " = " + this.repkv(v2.Value)
|
||||||
if v2.Comment != "" {
|
if v2.Comment != "" {
|
||||||
comm = strings.Replace(v2.Comment, "\n", "\n#", -1)
|
comm = strings.Replace(v2.Comment, "\n", "\n#", -1)
|
||||||
@ -513,7 +389,7 @@ func (this *StarCfg) Build() []byte {
|
|||||||
func (this *StarCfg) repkv(value string) string {
|
func (this *StarCfg) repkv(value string) string {
|
||||||
value = strings.Replace(value, `\`, `\\`, -1)
|
value = strings.Replace(value, `\`, `\\`, -1)
|
||||||
value = strings.Replace(value, `#`, `\#`, -1)
|
value = strings.Replace(value, `#`, `\#`, -1)
|
||||||
//value = strings.Replace(value, `=`, `\=`, -1)
|
value = strings.Replace(value, `=`, `\=`, -1)
|
||||||
value = strings.Replace(value, `[`, `\[`, -1)
|
value = strings.Replace(value, `[`, `\[`, -1)
|
||||||
value = strings.Replace(value, `]`, `\]`, -1)
|
value = strings.Replace(value, `]`, `\]`, -1)
|
||||||
return value
|
return value
|
||||||
@ -521,23 +397,24 @@ func (this *StarCfg) repkv(value string) string {
|
|||||||
|
|
||||||
func (this *CfgSegment) GetComment(key string) string {
|
func (this *CfgSegment) GetComment(key string) string {
|
||||||
if v, ok := this.cmap[key]; !ok {
|
if v, ok := this.cmap[key]; !ok {
|
||||||
|
for _, v2 := range this.InsertNode {
|
||||||
|
if v2.Key == key {
|
||||||
|
return this.Node[v].Comment
|
||||||
|
}
|
||||||
|
}
|
||||||
return ""
|
return ""
|
||||||
} else {
|
} else {
|
||||||
return this.Node[v].Comment
|
return this.Node[v].Comment
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (this *CfgSegment) SetComment(key, comment string) error {
|
|
||||||
if v, ok := this.cmap[key]; !ok {
|
|
||||||
return errors.New("Key Not Exists")
|
|
||||||
} else {
|
|
||||||
this.Node[v].Comment = comment
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func (this *CfgSegment) Exist(key string) bool {
|
func (this *CfgSegment) Exist(key string) bool {
|
||||||
if _, ok := this.cmap[key]; !ok {
|
if _, ok := this.cmap[key]; !ok {
|
||||||
|
for _, v2 := range this.InsertNode {
|
||||||
|
if v2.Key == key {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
}
|
||||||
return false
|
return false
|
||||||
} else {
|
} else {
|
||||||
return true
|
return true
|
||||||
@ -546,6 +423,11 @@ func (this *CfgSegment) Exist(key string) bool {
|
|||||||
|
|
||||||
func (this *CfgSegment) Get(key string) string {
|
func (this *CfgSegment) Get(key string) string {
|
||||||
if v, ok := this.cmap[key]; !ok {
|
if v, ok := this.cmap[key]; !ok {
|
||||||
|
for _, v2 := range this.InsertNode {
|
||||||
|
if v2.Key == key {
|
||||||
|
return this.Node[v].Value
|
||||||
|
}
|
||||||
|
}
|
||||||
return ""
|
return ""
|
||||||
} else {
|
} else {
|
||||||
return this.Node[v].Value
|
return this.Node[v].Value
|
||||||
@ -638,13 +520,20 @@ func (this *CfgSegment) SetInt(key string, value int, comment string) error {
|
|||||||
|
|
||||||
func (this *CfgSegment) Set(key, value, comment string) error {
|
func (this *CfgSegment) Set(key, value, comment string) error {
|
||||||
if v, ok := this.cmap[key]; !ok {
|
if v, ok := this.cmap[key]; !ok {
|
||||||
|
for _, v2 := range this.InsertNode {
|
||||||
|
if v2.Key == key {
|
||||||
|
this.Node[v].Value = value
|
||||||
|
if comment != "" {
|
||||||
|
this.Node[v].Comment = comment
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
}
|
||||||
node := new(CfgNode)
|
node := new(CfgNode)
|
||||||
node.Key = key
|
node.Key = key
|
||||||
node.Value = value
|
node.Value = value
|
||||||
node.Comment = comment
|
node.Comment = comment
|
||||||
this.Node = append(this.Node, node)
|
this.InsertNode = append(this.InsertNode, node)
|
||||||
this.cmap[key] = this.nodeid
|
|
||||||
this.nodeid++
|
|
||||||
return nil
|
return nil
|
||||||
} else {
|
} else {
|
||||||
this.Node[v].Value = value
|
this.Node[v].Value = value
|
||||||
@ -654,13 +543,3 @@ func (this *CfgSegment) Set(key, value, comment string) error {
|
|||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (this *CfgSegment) Delete(key string) error {
|
|
||||||
if v, ok := this.cmap[key]; !ok {
|
|
||||||
return errors.New("Key not exists!")
|
|
||||||
} else {
|
|
||||||
this.Node[v] = nil
|
|
||||||
delete(this.cmap, key)
|
|
||||||
}
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
66
ini_test.go
66
ini_test.go
@ -21,69 +21,3 @@ func Test_CfgParse(t *testing.T) {
|
|||||||
ini.Seg("happy").SetInt64("sakura", 986787, "")
|
ini.Seg("happy").SetInt64("sakura", 986787, "")
|
||||||
fmt.Println(string(ini.Build()))
|
fmt.Println(string(ini.Build()))
|
||||||
}
|
}
|
||||||
|
|
||||||
type Ntest struct {
|
|
||||||
N string `seg:"happy" key:"me"`
|
|
||||||
}
|
|
||||||
type Testsss struct {
|
|
||||||
Okk string `seg:"unnamed" key:"ok"`
|
|
||||||
Migrate string `seg:"happy" key:"migrate" comment:"whnb"`
|
|
||||||
Ntest
|
|
||||||
}
|
|
||||||
|
|
||||||
func Test_CfgParseStruct(t *testing.T) {
|
|
||||||
data := `#lalala
|
|
||||||
ok =true\# #ok
|
|
||||||
[happy] #ok
|
|
||||||
#kkk
|
|
||||||
me = \=tru
|
|
||||||
sa = p\\k
|
|
||||||
migrate = ok #oooo
|
|
||||||
[siki]
|
|
||||||
sakurs = ssss #[ossk]`
|
|
||||||
//data, _ := ioutil.ReadFile(`c:\Users\Starainrt\Desktop\postgresql.conf`)
|
|
||||||
okk := Testsss{}
|
|
||||||
ini := new(StarCfg)
|
|
||||||
ini.Parse([]byte(data))
|
|
||||||
//fmt.Println(ini.Seg("happy").Get("migrate"))
|
|
||||||
fmt.Println(ini.Unmarshal(&okk))
|
|
||||||
fmt.Println(okk)
|
|
||||||
}
|
|
||||||
|
|
||||||
func Test_DeleteAdd(t *testing.T) {
|
|
||||||
data := `[A]
|
|
||||||
a=1
|
|
||||||
b=2
|
|
||||||
c=3
|
|
||||||
[B]
|
|
||||||
a=1
|
|
||||||
b=2
|
|
||||||
c=3
|
|
||||||
[C]
|
|
||||||
a=1
|
|
||||||
b=2
|
|
||||||
c=3
|
|
||||||
`
|
|
||||||
ini := new(StarCfg)
|
|
||||||
ini.Parse([]byte(data))
|
|
||||||
fmt.Println(ini.Seg("A").Int("a"))
|
|
||||||
fmt.Println(ini.Seg("A").Int("b"))
|
|
||||||
fmt.Println(ini.Seg("B").Int("c"))
|
|
||||||
fmt.Println("-----------------------")
|
|
||||||
ini.Seg("A").Set("d", "4", "")
|
|
||||||
ini.DeleteSeg("B")
|
|
||||||
ini.AddSeg("D")
|
|
||||||
ini.AddSeg("E").SetInt("a", 1, "")
|
|
||||||
ini.Seg("C").Delete("a")
|
|
||||||
fmt.Println(string(ini.Build()))
|
|
||||||
}
|
|
||||||
|
|
||||||
func Test_Marshal(t *testing.T) {
|
|
||||||
var a Testsss
|
|
||||||
a.Migrate = "1"
|
|
||||||
a.N = "2"
|
|
||||||
a.Okk = "okkkkk"
|
|
||||||
ini := StarCfg{}
|
|
||||||
data, err := ini.Marshal(a)
|
|
||||||
fmt.Println(string(data), err)
|
|
||||||
}
|
|
||||||
|
364
net.go
364
net.go
@ -2,283 +2,141 @@ package starainrt
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
"context"
|
|
||||||
"encoding/binary"
|
|
||||||
"errors"
|
"errors"
|
||||||
"sync"
|
|
||||||
"time"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
//SecretKey 通信加密Key,不应当被修改
|
/*
|
||||||
|
SecretKey 通信加密Key,不应当被修改
|
||||||
|
*/
|
||||||
const SecretKey string = "1996victorique1127B612BTXL"
|
const SecretKey string = "1996victorique1127B612BTXL"
|
||||||
|
|
||||||
// 识别头
|
var header []byte = []byte{11, 27, 19, 96}
|
||||||
var header = []byte{11, 27, 19, 96, 12, 25, 02, 20}
|
var Crypto *StarCrypto
|
||||||
|
|
||||||
// MsgQueue 为基本的信息单位
|
func init() {
|
||||||
type MsgQueue struct {
|
Crypto = new(StarCrypto)
|
||||||
|
}
|
||||||
|
|
||||||
|
type StarQueue struct {
|
||||||
|
Encode bool
|
||||||
|
Msgid uint16
|
||||||
|
MsgPool []MsgUsed
|
||||||
|
UnFinMsg []byte
|
||||||
|
LastID int //= -1
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewQueue() *StarQueue {
|
||||||
|
que := new(StarQueue)
|
||||||
|
que.LastID = -1
|
||||||
|
que.Encode = true
|
||||||
|
return que
|
||||||
|
}
|
||||||
|
|
||||||
|
func (this *StarQueue) BuildMessage(str string) []byte {
|
||||||
|
var msg []byte
|
||||||
|
var buffer bytes.Buffer
|
||||||
|
if this.Encode {
|
||||||
|
msg = Crypto.VicqueEncodeV1([]byte(str), SecretKey)
|
||||||
|
} else {
|
||||||
|
msg = []byte(str)
|
||||||
|
}
|
||||||
|
buffer.Write([]byte(Crypto.CRC32(msg)))
|
||||||
|
buffer.Write([]byte{byte(this.Msgid >> 8), byte(this.Msgid)})
|
||||||
|
buffer.Write(msg)
|
||||||
|
lens := len(buffer.Bytes())
|
||||||
|
if lens > 65535 {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
msg = make([]byte, lens)
|
||||||
|
copy(msg, buffer.Bytes())
|
||||||
|
buffer.Reset()
|
||||||
|
ulens := uint16(lens)
|
||||||
|
buffer.Write(header)
|
||||||
|
buffer.Write([]byte{byte(ulens >> 8), byte(ulens)})
|
||||||
|
buffer.Write(msg)
|
||||||
|
this.Msgid++
|
||||||
|
return buffer.Bytes()
|
||||||
|
}
|
||||||
|
|
||||||
|
type MsgUsed struct {
|
||||||
ID uint16
|
ID uint16
|
||||||
Msg []byte
|
Msg string
|
||||||
|
Crc32 string
|
||||||
Conn interface{}
|
Conn interface{}
|
||||||
}
|
}
|
||||||
|
|
||||||
// StarQueue 为流数据中的消息队列分发
|
func (this *StarQueue) ParseMessage(msg []byte, conn interface{}) int {
|
||||||
type StarQueue struct {
|
var buffer bytes.Buffer
|
||||||
Encode bool
|
buffer.Write(this.UnFinMsg)
|
||||||
Reserve uint16
|
buffer.Write(msg)
|
||||||
Msgid uint16
|
msg = buffer.Bytes()
|
||||||
MsgPool []MsgQueue
|
if len(msg) <= 6 {
|
||||||
UnFinMsg sync.Map
|
this.UnFinMsg = msg
|
||||||
LastID int //= -1
|
return -2
|
||||||
ctx context.Context
|
|
||||||
cancel context.CancelFunc
|
|
||||||
duration time.Duration
|
|
||||||
}
|
|
||||||
|
|
||||||
// NewQueue 建立一个新消息队列
|
|
||||||
func NewQueue() *StarQueue {
|
|
||||||
var que StarQueue
|
|
||||||
que.Encode = true
|
|
||||||
que.ctx, que.cancel = context.WithCancel(context.Background())
|
|
||||||
que.duration = 0
|
|
||||||
return &que
|
|
||||||
}
|
|
||||||
|
|
||||||
// Uint32ToByte 4位uint32转byte
|
|
||||||
func Uint32ToByte(src uint32) []byte {
|
|
||||||
res := make([]byte, 4)
|
|
||||||
res[3] = uint8(src)
|
|
||||||
res[2] = uint8(src >> 8)
|
|
||||||
res[1] = uint8(src >> 16)
|
|
||||||
res[0] = uint8(src >> 24)
|
|
||||||
return res
|
|
||||||
}
|
|
||||||
|
|
||||||
// ByteToUint32 byte转4位uint32
|
|
||||||
func ByteToUint32(src []byte) uint32 {
|
|
||||||
var res uint32
|
|
||||||
buffer := bytes.NewBuffer(src)
|
|
||||||
binary.Read(buffer, binary.BigEndian, &res)
|
|
||||||
return res
|
|
||||||
}
|
|
||||||
|
|
||||||
// Uint16ToByte 2位uint16转byte
|
|
||||||
func Uint16ToByte(src uint16) []byte {
|
|
||||||
res := make([]byte, 2)
|
|
||||||
res[1] = uint8(src)
|
|
||||||
res[0] = uint8(src >> 8)
|
|
||||||
return res
|
|
||||||
}
|
|
||||||
|
|
||||||
// ByteToUint16 用于byte转uint16
|
|
||||||
func ByteToUint16(src []byte) uint16 {
|
|
||||||
var res uint16
|
|
||||||
buffer := bytes.NewBuffer(src)
|
|
||||||
binary.Read(buffer, binary.BigEndian, &res)
|
|
||||||
return res
|
|
||||||
}
|
|
||||||
|
|
||||||
// BuildMessage 生成编码后的信息用于发送
|
|
||||||
func (que *StarQueue) BuildMessage(src []byte) []byte {
|
|
||||||
var buff bytes.Buffer
|
|
||||||
que.Msgid++
|
|
||||||
if que.Encode {
|
|
||||||
cryptos := new(StarCrypto)
|
|
||||||
src = cryptos.VicqueEncodeV1(src, SecretKey)
|
|
||||||
}
|
}
|
||||||
length := uint32(len(src))
|
if msg[0] != byte(11) {
|
||||||
buff.Write(header)
|
this.UnFinMsg = []byte{}
|
||||||
buff.Write(Uint32ToByte(length))
|
//resend last
|
||||||
buff.Write(Uint16ToByte(que.Msgid))
|
return this.LastID + 1
|
||||||
buff.Write(src)
|
|
||||||
return buff.Bytes()
|
|
||||||
}
|
|
||||||
|
|
||||||
type unFinMsg struct {
|
|
||||||
ID uint16
|
|
||||||
LengthRecv uint32
|
|
||||||
// HeaderMsg 信息头,应当为14位:8位识别码+4位长度码+2位id
|
|
||||||
HeaderMsg []byte
|
|
||||||
RecvMsg []byte
|
|
||||||
}
|
|
||||||
|
|
||||||
// ParseMessage 用于解析啊收到的msg信息
|
|
||||||
func (que *StarQueue) ParseMessage(msg []byte, conn interface{}) error {
|
|
||||||
tmp, ok := que.UnFinMsg.Load(conn)
|
|
||||||
cryptos := new(StarCrypto)
|
|
||||||
if ok { //存在未完成的信息
|
|
||||||
lastMsg := tmp.(*unFinMsg)
|
|
||||||
headerLen := len(lastMsg.HeaderMsg)
|
|
||||||
if headerLen < 14 { //未完成头标题
|
|
||||||
//传输的数据不能填充header头
|
|
||||||
if len(msg) < 14-headerLen {
|
|
||||||
//加入header头并退出
|
|
||||||
lastMsg.HeaderMsg = bytesMerge(lastMsg.HeaderMsg, msg)
|
|
||||||
que.UnFinMsg.Store(conn, lastMsg)
|
|
||||||
return nil
|
|
||||||
}
|
}
|
||||||
//获取14字节完整的header
|
if msg[1] != byte(27) || msg[2] != byte(19) || msg[3] != byte(96) {
|
||||||
header := msg[0 : 14-headerLen]
|
//resend last
|
||||||
lastMsg.HeaderMsg = bytesMerge(lastMsg.HeaderMsg, header)
|
this.UnFinMsg = []byte{}
|
||||||
//检查收到的header是否为认证header
|
return this.LastID + 1
|
||||||
//若不是,丢弃并重新来过
|
|
||||||
if !checkHeader(lastMsg.HeaderMsg[0:8]) {
|
|
||||||
que.UnFinMsg.Delete(conn)
|
|
||||||
if len(msg) == 0 {
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
return que.ParseMessage(msg, conn)
|
|
||||||
}
|
|
||||||
//获得本数据包长度
|
|
||||||
lastMsg.LengthRecv = ByteToUint32(lastMsg.HeaderMsg[8:12])
|
|
||||||
//获得本数据包ID
|
|
||||||
lastMsg.ID = ByteToUint16(lastMsg.HeaderMsg[12:14])
|
|
||||||
//存入列表
|
|
||||||
que.UnFinMsg.Store(conn, lastMsg)
|
|
||||||
msg = msg[14-headerLen:]
|
|
||||||
if uint32(len(msg)) < lastMsg.LengthRecv {
|
|
||||||
lastMsg.RecvMsg = msg
|
|
||||||
que.UnFinMsg.Store(conn, lastMsg)
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
if uint32(len(msg)) >= lastMsg.LengthRecv {
|
|
||||||
lastMsg.RecvMsg = msg[0:lastMsg.LengthRecv]
|
|
||||||
if que.Encode {
|
|
||||||
lastMsg.RecvMsg = cryptos.VicqueDecodeV1(lastMsg.RecvMsg, SecretKey)
|
|
||||||
}
|
|
||||||
msg = msg[lastMsg.LengthRecv:]
|
|
||||||
stroeMsg := MsgQueue{
|
|
||||||
ID: lastMsg.ID,
|
|
||||||
Msg: lastMsg.RecvMsg,
|
|
||||||
Conn: conn,
|
|
||||||
}
|
|
||||||
que.MsgPool = append(que.MsgPool, stroeMsg)
|
|
||||||
que.UnFinMsg.Delete(conn)
|
|
||||||
return que.ParseMessage(msg, conn)
|
|
||||||
}
|
}
|
||||||
|
length := uint16(uint(msg[4])<<uint(8) + uint(msg[5]))
|
||||||
|
if 6+length > uint16(len(msg)) {
|
||||||
|
this.UnFinMsg = msg
|
||||||
|
return -2
|
||||||
} else {
|
} else {
|
||||||
lastID := int(lastMsg.LengthRecv) - len(lastMsg.RecvMsg)
|
this.UnFinMsg = []byte{}
|
||||||
if lastID < 0 {
|
strmsg := msg[6 : length+6]
|
||||||
que.UnFinMsg.Delete(conn)
|
crc := strmsg[0:8]
|
||||||
return que.ParseMessage(msg, conn)
|
id := strmsg[8:10]
|
||||||
}
|
strmsg = strmsg[10:]
|
||||||
if len(msg) >= lastID {
|
if Crypto.CRC32([]byte(strmsg)) != string(crc) {
|
||||||
lastMsg.RecvMsg = bytesMerge(lastMsg.RecvMsg, msg[0:lastID])
|
//resend last
|
||||||
if que.Encode {
|
this.UnFinMsg = []byte{}
|
||||||
lastMsg.RecvMsg = cryptos.VicqueDecodeV1(lastMsg.RecvMsg, SecretKey)
|
return this.LastID + 1
|
||||||
}
|
|
||||||
stroeMsg := MsgQueue{
|
|
||||||
ID: lastMsg.ID,
|
|
||||||
Msg: lastMsg.RecvMsg,
|
|
||||||
Conn: conn,
|
|
||||||
}
|
|
||||||
que.MsgPool = append(que.MsgPool, stroeMsg)
|
|
||||||
que.UnFinMsg.Delete(conn)
|
|
||||||
if len(msg) == lastID {
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
msg = msg[lastID:]
|
|
||||||
return que.ParseMessage(msg, conn)
|
|
||||||
}
|
|
||||||
lastMsg.RecvMsg = bytesMerge(lastMsg.RecvMsg, msg)
|
|
||||||
que.UnFinMsg.Store(conn, lastMsg)
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if len(msg) == 0 {
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
var start int
|
|
||||||
if start = searchHeader(msg); start == -1 {
|
|
||||||
return errors.New("data format error")
|
|
||||||
}
|
|
||||||
msg = msg[start:]
|
|
||||||
lastMsg := unFinMsg{}
|
|
||||||
que.UnFinMsg.Store(conn, &lastMsg)
|
|
||||||
return que.ParseMessage(msg, conn)
|
|
||||||
}
|
|
||||||
|
|
||||||
func checkHeader(msg []byte) bool {
|
|
||||||
if len(msg) != 8 {
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
for k, v := range msg {
|
|
||||||
if v != header[k] {
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return true
|
|
||||||
}
|
|
||||||
|
|
||||||
func searchHeader(msg []byte) int {
|
|
||||||
if len(msg) < 8 {
|
|
||||||
return 0
|
|
||||||
}
|
|
||||||
for k, v := range msg {
|
|
||||||
find := 0
|
|
||||||
if v == header[0] {
|
|
||||||
for k2, v2 := range header {
|
|
||||||
if msg[k+k2] == v2 {
|
|
||||||
find++
|
|
||||||
} else {
|
} else {
|
||||||
break
|
if this.Encode {
|
||||||
|
strmsg = Crypto.VicqueDecodeV1(strmsg, SecretKey)
|
||||||
}
|
}
|
||||||
|
msgs := MsgUsed{uint16(uint(id[0])<<8 + uint(id[1])), string(strmsg), string(crc), conn}
|
||||||
|
this.LastID = int(msgs.ID)
|
||||||
|
this.MsgPool = append(this.MsgPool, msgs)
|
||||||
}
|
}
|
||||||
if find == 8 {
|
if 6+length == uint16(len(msg)) {
|
||||||
return k
|
return -2
|
||||||
}
|
}
|
||||||
|
msg = msg[length+6:]
|
||||||
|
return this.ParseMessage(msg, conn)
|
||||||
}
|
}
|
||||||
}
|
return -2
|
||||||
return -1
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func bytesMerge(src ...[]byte) []byte {
|
func (this *StarQueue) Restore(n int) ([]MsgUsed, error) {
|
||||||
var buff bytes.Buffer
|
if n > len(this.MsgPool) {
|
||||||
for _, v := range src {
|
return nil, errors.New("N is Too Large")
|
||||||
buff.Write(v)
|
|
||||||
}
|
}
|
||||||
return buff.Bytes()
|
tmp := this.MsgPool[0:n]
|
||||||
|
if n != len(this.MsgPool) {
|
||||||
|
this.MsgPool = this.MsgPool[n:]
|
||||||
|
} else {
|
||||||
|
this.MsgPool = []MsgUsed{}
|
||||||
|
}
|
||||||
|
return tmp, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// Restore 获取收到的信息
|
func (this *StarQueue) RestoreOne() (MsgUsed, error) {
|
||||||
func (que *StarQueue) Restore(n int) ([]MsgQueue, error) {
|
if len(this.MsgPool) == 0 {
|
||||||
var res []MsgQueue
|
return MsgUsed{}, errors.New("N is Too Large")
|
||||||
dura := time.Duration(0)
|
|
||||||
for len(que.MsgPool) < n {
|
|
||||||
select {
|
|
||||||
case <-que.ctx.Done():
|
|
||||||
return res, errors.New("Stoped By External Function Call")
|
|
||||||
default:
|
|
||||||
time.Sleep(time.Millisecond * 20)
|
|
||||||
dura = time.Millisecond*20 + dura
|
|
||||||
if que.duration != 0 && dura > que.duration {
|
|
||||||
return res, errors.New("Time Exceed")
|
|
||||||
}
|
}
|
||||||
|
tmp := this.MsgPool[0]
|
||||||
|
if 1 != len(this.MsgPool) {
|
||||||
|
this.MsgPool = this.MsgPool[1:]
|
||||||
|
} else {
|
||||||
|
this.MsgPool = []MsgUsed{}
|
||||||
}
|
}
|
||||||
}
|
return tmp, nil
|
||||||
if len(que.MsgPool) < n {
|
|
||||||
return res, errors.New("Result Not Enough")
|
|
||||||
}
|
|
||||||
res = que.MsgPool[0:n]
|
|
||||||
que.MsgPool = que.MsgPool[n:]
|
|
||||||
return res, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// RestoreOne 获取收到的一个信息
|
|
||||||
func (que *StarQueue) RestoreOne() (MsgQueue, error) {
|
|
||||||
data, err := que.Restore(1)
|
|
||||||
if len(data) == 1 {
|
|
||||||
return data[0], err
|
|
||||||
}
|
|
||||||
return MsgQueue{}, err
|
|
||||||
}
|
|
||||||
|
|
||||||
// Stop 立即停止Restore
|
|
||||||
func (que *StarQueue) Stop() {
|
|
||||||
que.cancel()
|
|
||||||
}
|
|
||||||
|
|
||||||
// RestoreDuration Restore最大超时时间
|
|
||||||
func (que *StarQueue) RestoreDuration(tm time.Duration) {
|
|
||||||
que.duration = tm
|
|
||||||
}
|
}
|
||||||
|
184
net_test.go
184
net_test.go
@ -1,184 +0,0 @@
|
|||||||
package starainrt
|
|
||||||
|
|
||||||
import (
|
|
||||||
"fmt"
|
|
||||||
"testing"
|
|
||||||
)
|
|
||||||
|
|
||||||
func Test_oldddd(t *testing.T) {
|
|
||||||
a := []int{}
|
|
||||||
a = append(a, 1)
|
|
||||||
a = append(a, 2)
|
|
||||||
a = append(a, 3)
|
|
||||||
fmt.Println(a)
|
|
||||||
a = a[3:]
|
|
||||||
fmt.Println(a)
|
|
||||||
}
|
|
||||||
|
|
||||||
func Test_binary(t *testing.T) {
|
|
||||||
bytes := Uint16ToByte(65512)
|
|
||||||
fmt.Println(bytes)
|
|
||||||
fmt.Println(ByteToUint16(bytes))
|
|
||||||
}
|
|
||||||
|
|
||||||
func Test_test(t *testing.T) {
|
|
||||||
que := NewQueue()
|
|
||||||
okk := que.BuildMessage([]byte(`
|
|
||||||
with
|
|
||||||
all_values as (
|
|
||||||
(select V.ID,
|
|
||||||
V.OBJID,
|
|
||||||
V.TITLE,
|
|
||||||
V.CONTENT,
|
|
||||||
V.URL,
|
|
||||||
V.URL_TYPE,
|
|
||||||
V.STATUS,
|
|
||||||
V.SEARCH_TIMES,
|
|
||||||
V.UPDATE_TIMES
|
|
||||||
from SPMS_SEARCH_VIEW V
|
|
||||||
)
|
|
||||||
|
|
||||||
),
|
|
||||||
in_values as (
|
|
||||||
select T1.*
|
|
||||||
from SPMS_SEARCH_DATASET T1,
|
|
||||||
(select V.ID,
|
|
||||||
V.OBJID,
|
|
||||||
V.TITLE,
|
|
||||||
V.CONTENT,
|
|
||||||
V.URL,
|
|
||||||
V.URL_TYPE,
|
|
||||||
V.STATUS,
|
|
||||||
V.SEARCH_TIMES,
|
|
||||||
V.UPDATE_TIMES
|
|
||||||
from SPMS_SEARCH_VIEW V
|
|
||||||
) T
|
|
||||||
where T.OBJID = T1.OBJID
|
|
||||||
|
|
||||||
),
|
|
||||||
upsert as (
|
|
||||||
update SPMS_SEARCH_DATASET as T1
|
|
||||||
set
|
|
||||||
ID = T.ID,
|
|
||||||
TITLE = T.TITLE,
|
|
||||||
CONTENT = T.CONTENT,
|
|
||||||
URL = T.URL,
|
|
||||||
URL_TYPE = T.URL_TYPE,
|
|
||||||
STATUS = T.STATUS,
|
|
||||||
SEARCH_TIMES = T.SEARCH_TIMES,
|
|
||||||
UPDATE_TIMES = TO_CHAR(T.UPDATE_TIMES, 'yyyy-MM-dd hh24:mi:ss'),
|
|
||||||
SYNC_TIMES = V_CUR_TIME
|
|
||||||
from all_values T
|
|
||||||
where T.OBJID = T1.OBJID and TO_DATE(T1.UPDATE_TIMES, 'yyyy-MM-dd hh24:mi:ss') \u003c TO_DATE(TO_CHAR(T.UPDATE_TIMES, 'yyyy-MM-dd hh24:mi:ss'), 'yyyy-MM-dd hh24:mi:ss')
|
|
||||||
)
|
|
||||||
insert into SPMS_SEARCH_DATASET(ID, OBJID, TITLE, CONTENT, URL, URL_TYPE, STATUS, SEARCH_TIMES, UPDATE_TIMES, SYNC_TIMES)
|
|
||||||
select T.ID,
|
|
||||||
T.OBJID,
|
|
||||||
T.TITLE,
|
|
||||||
T.CONTENT,
|
|
||||||
T.URL,
|
|
||||||
T.URL_TYPE,
|
|
||||||
T.STATUS,
|
|
||||||
T.SEARCH_TIMES,
|
|
||||||
TO_CHAR(T.UPDATE_TIMES, 'yyyy-MM-dd hh24:mi:ss'),
|
|
||||||
V_CUR_TIME
|
|
||||||
from (select V.ID,
|
|
||||||
V.OBJID,
|
|
||||||
V.TITLE,
|
|
||||||
V.CONTENT,
|
|
||||||
V.URL,
|
|
||||||
V.URL_TYPE,
|
|
||||||
V.STATUS,
|
|
||||||
V.SEARCH_TIMES,
|
|
||||||
V.UPDATE_TIMES
|
|
||||||
from SPMS_SEARCH_VIEW V
|
|
||||||
) T
|
|
||||||
where not exists(select 1
|
|
||||||
from in_values T1
|
|
||||||
where T.OBJID = T1.OBJID
|
|
||||||
)
|
|
||||||
with
|
|
||||||
all_values as (
|
|
||||||
(select V.ID,
|
|
||||||
V.OBJID,
|
|
||||||
V.TITLE,
|
|
||||||
V.CONTENT,
|
|
||||||
V.URL,
|
|
||||||
V.URL_TYPE,
|
|
||||||
V.STATUS,
|
|
||||||
V.SEARCH_TIMES,
|
|
||||||
V.UPDATE_TIMES
|
|
||||||
from SPMS_SEARCH_VIEW V
|
|
||||||
)
|
|
||||||
|
|
||||||
),
|
|
||||||
in_values as (
|
|
||||||
select T1.*
|
|
||||||
from SPMS_SEARCH_DATASET T1,
|
|
||||||
(select V.ID,
|
|
||||||
V.OBJID,
|
|
||||||
V.TITLE,
|
|
||||||
V.CONTENT,
|
|
||||||
V.URL,
|
|
||||||
V.URL_TYPE,
|
|
||||||
V.STATUS,
|
|
||||||
V.SEARCH_TIMES,
|
|
||||||
V.UPDATE_TIMES
|
|
||||||
from SPMS_SEARCH_VIEW V
|
|
||||||
) T
|
|
||||||
where T.OBJID = T1.OBJID
|
|
||||||
|
|
||||||
),
|
|
||||||
upsert as (
|
|
||||||
update SPMS_SEARCH_DATASET as T1
|
|
||||||
set
|
|
||||||
ID = T.ID,
|
|
||||||
TITLE = T.TITLE,
|
|
||||||
CONTENT = T.CONTENT,
|
|
||||||
URL = T.URL,
|
|
||||||
URL_TYPE = T.URL_TYPE,
|
|
||||||
STATUS = T.STATUS,
|
|
||||||
SEARCH_TIMES = T.SEARCH_TIMES,
|
|
||||||
UPDATE_TIMES = TO_CHAR(T.UPDATE_TIMES, 'yyyy-MM-dd hh24:mi:ss'),
|
|
||||||
SYNC_TIMES = V_CUR_TIME
|
|
||||||
from all_values T
|
|
||||||
where T.OBJID = T1.OBJID and TO_DATE(T1.UPDATE_TIMES, 'yyyy-MM-dd hh24:mi:ss') \u003c TO_DATE(TO_CHAR(T.UPDATE_TIMES, 'yyyy-MM-dd hh24:mi:ss'), 'yyyy-MM-dd hh24:mi:ss')
|
|
||||||
)
|
|
||||||
insert into SPMS_SEARCH_DATASET(ID, OBJID, TITLE, CONTENT, URL, URL_TYPE, STATUS, SEARCH_TIMES, UPDATE_TIMES, SYNC_TIMES)
|
|
||||||
select T.ID,
|
|
||||||
T.OBJID,
|
|
||||||
T.TITLE,
|
|
||||||
T.CONTENT,
|
|
||||||
T.URL,
|
|
||||||
T.URL_TYPE,
|
|
||||||
T.STATUS,
|
|
||||||
T.SEARCH_TIMES,
|
|
||||||
TO_CHAR(T.UPDATE_TIMES, 'yyyy-MM-dd hh24:mi:ss'),
|
|
||||||
V_CUR_TIME
|
|
||||||
from (select V.ID,
|
|
||||||
V.OBJID,
|
|
||||||
V.TITLE,
|
|
||||||
V.CONTENT,
|
|
||||||
V.URL,
|
|
||||||
V.URL_TYPE,
|
|
||||||
V.STATUS,
|
|
||||||
V.SEARCH_TIMES,
|
|
||||||
V.UPDATE_TIMES
|
|
||||||
from SPMS_SEARCH_VIEW V
|
|
||||||
) T
|
|
||||||
where not exists(select 1
|
|
||||||
from in_values T1
|
|
||||||
where T.OBJID = T1.OBJID
|
|
||||||
)
|
|
||||||
`))
|
|
||||||
fmt.Println(string(okk))
|
|
||||||
que.ParseMessage([]byte{'n', 'm', 'b'}, "nbb")
|
|
||||||
// que.ParseMessage([]byte{'n', 23, 24, 4, 45, 6, 56, 56, 2, 3, 1, 2}, "nbb")
|
|
||||||
que.ParseMessage(okk[0:123], "nbb")
|
|
||||||
que.ParseMessage([]byte{}, "nbb")
|
|
||||||
que.ParseMessage([]byte{}, "nbb")
|
|
||||||
que.ParseMessage([]byte{}, "nbb")
|
|
||||||
que.ParseMessage(okk[123:135], "nbb")
|
|
||||||
que.ParseMessage(okk[135:], "nbb")
|
|
||||||
fmt.Println(string(que.MsgPool[0].Msg))
|
|
||||||
}
|
|
7
shell.go
7
shell.go
@ -31,7 +31,10 @@ func NewPipeShell(command string, arg ...string) (*suncli, error) {
|
|||||||
lovecli.counter = 0
|
lovecli.counter = 0
|
||||||
cmd := exec.Command(command, arg...)
|
cmd := exec.Command(command, arg...)
|
||||||
lovecli.cmd = cmd
|
lovecli.cmd = cmd
|
||||||
|
lovecli.infile, err = lovecli.cmd.StdinPipe()
|
||||||
|
if err != nil {
|
||||||
|
return &lovecli, err
|
||||||
|
}
|
||||||
lovecli.outfile, err = lovecli.cmd.StdoutPipe()
|
lovecli.outfile, err = lovecli.cmd.StdoutPipe()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return &lovecli, err
|
return &lovecli, err
|
||||||
@ -116,7 +119,7 @@ func (this suncli) WriteCmd(cmdstr string) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
func (this *suncli) ExitCode() int {
|
func (this suncli) ExitCode() int {
|
||||||
return this.cmd.ProcessState.Sys().(syscall.WaitStatus).ExitStatus()
|
return this.cmd.ProcessState.Sys().(syscall.WaitStatus).ExitStatus()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
161
starshell.go
161
starshell.go
@ -1,161 +0,0 @@
|
|||||||
package starainrt
|
|
||||||
|
|
||||||
import (
|
|
||||||
"context"
|
|
||||||
"errors"
|
|
||||||
"io"
|
|
||||||
"os"
|
|
||||||
"os/exec"
|
|
||||||
"syscall"
|
|
||||||
"time"
|
|
||||||
)
|
|
||||||
|
|
||||||
type StarShell struct {
|
|
||||||
outfile *io.ReadCloser
|
|
||||||
infile *io.WriteCloser
|
|
||||||
errfile *io.ReadCloser
|
|
||||||
cmd *exec.Cmd
|
|
||||||
running bool
|
|
||||||
stopSign context.Context
|
|
||||||
stopFunc context.CancelFunc
|
|
||||||
stdout []byte
|
|
||||||
errout []byte
|
|
||||||
runerr error
|
|
||||||
exitcode int
|
|
||||||
}
|
|
||||||
|
|
||||||
func NewStarShell(command string, args ...string) (*StarShell, error) {
|
|
||||||
shell := new(StarShell)
|
|
||||||
shell.stopSign, shell.stopFunc = context.WithCancel(context.Background())
|
|
||||||
cmd := exec.CommandContext(shell.stopSign, command, args...)
|
|
||||||
shell.cmd = cmd
|
|
||||||
infile, err := shell.cmd.StdinPipe()
|
|
||||||
if err != nil {
|
|
||||||
return shell, err
|
|
||||||
}
|
|
||||||
shell.infile = &infile
|
|
||||||
errfile, err := shell.cmd.StderrPipe()
|
|
||||||
if err != nil {
|
|
||||||
return shell, err
|
|
||||||
}
|
|
||||||
shell.errfile = &errfile
|
|
||||||
outfile, err := shell.cmd.StdoutPipe()
|
|
||||||
if err != nil {
|
|
||||||
return shell, err
|
|
||||||
}
|
|
||||||
shell.outfile = &outfile
|
|
||||||
shell.runerr = nil
|
|
||||||
shell.exitcode = -999
|
|
||||||
return shell, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (starcli *StarShell) Start() error {
|
|
||||||
if err := starcli.cmd.Start(); err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
starcli.running = true
|
|
||||||
go func() {
|
|
||||||
err := starcli.cmd.Wait()
|
|
||||||
if err != nil {
|
|
||||||
starcli.runerr = err
|
|
||||||
}
|
|
||||||
starcli.exitcode = starcli.cmd.ProcessState.Sys().(syscall.WaitStatus).ExitStatus()
|
|
||||||
starcli.running = false
|
|
||||||
}()
|
|
||||||
go starcli.queryResult()
|
|
||||||
go starcli.queryErrResult()
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (starcli *StarShell) queryResult() error {
|
|
||||||
for starcli.running {
|
|
||||||
out := make([]byte, 65535)
|
|
||||||
n, err := (*starcli.outfile).Read(out)
|
|
||||||
if n != 0 {
|
|
||||||
for i := 0; i < n; i++ {
|
|
||||||
starcli.stdout = append(starcli.stdout, out[i])
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if err != nil {
|
|
||||||
if err == io.EOF {
|
|
||||||
break
|
|
||||||
} else {
|
|
||||||
starcli.runerr = err
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
time.Sleep(time.Microsecond * 100)
|
|
||||||
}
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (starcli *StarShell) queryErrResult() error {
|
|
||||||
for starcli.running {
|
|
||||||
out := make([]byte, 65535)
|
|
||||||
n, err := (*starcli.errfile).Read(out)
|
|
||||||
if n != 0 {
|
|
||||||
for i := 0; i < n; i++ {
|
|
||||||
starcli.errout = append(starcli.errout, out[i])
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if err != nil {
|
|
||||||
if err == io.EOF {
|
|
||||||
break
|
|
||||||
} else {
|
|
||||||
starcli.runerr = err
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
}
|
|
||||||
time.Sleep(time.Microsecond * 100)
|
|
||||||
}
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (starcli *StarShell) GetResult() (string, error) {
|
|
||||||
np := len(starcli.stdout)
|
|
||||||
res1 := string(starcli.stdout[0:np])
|
|
||||||
starcli.stdout = starcli.stdout[np:]
|
|
||||||
np = len(starcli.errout)
|
|
||||||
res2 := string(starcli.errout[0:np])
|
|
||||||
starcli.errout = starcli.errout[np:]
|
|
||||||
if len(res2) == 0 && starcli.runerr != nil {
|
|
||||||
res2 = starcli.runerr.Error()
|
|
||||||
}
|
|
||||||
if len(res2) == 0 {
|
|
||||||
return res1, nil
|
|
||||||
}
|
|
||||||
return res1, errors.New(res2)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (starcli *StarShell) Exec(cmd string, wait int) (string, error) {
|
|
||||||
(*starcli.infile).Write([]byte(cmd + "\n"))
|
|
||||||
time.Sleep(time.Millisecond * time.Duration(wait))
|
|
||||||
return starcli.GetResult()
|
|
||||||
}
|
|
||||||
|
|
||||||
func (starcli *StarShell) WriteCmd(cmdstr string) {
|
|
||||||
(*starcli.infile).Write([]byte(cmdstr + "\n"))
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
func (starcli *StarShell) ExitCode() int {
|
|
||||||
return starcli.exitcode
|
|
||||||
}
|
|
||||||
|
|
||||||
func (starcli *StarShell) Kill() {
|
|
||||||
starcli.stopFunc()
|
|
||||||
starcli.running = false
|
|
||||||
}
|
|
||||||
|
|
||||||
func (starcli *StarShell) IsRunning() bool {
|
|
||||||
return starcli.running
|
|
||||||
}
|
|
||||||
|
|
||||||
func (starcli *StarShell) GetPid() int {
|
|
||||||
return starcli.cmd.Process.Pid
|
|
||||||
}
|
|
||||||
|
|
||||||
func (starcli *StarShell) Signal(sig os.Signal) error {
|
|
||||||
return starcli.cmd.Process.Signal(sig)
|
|
||||||
}
|
|
@ -1,34 +0,0 @@
|
|||||||
// +build linux
|
|
||||||
|
|
||||||
package starainrt
|
|
||||||
|
|
||||||
import "syscall"
|
|
||||||
|
|
||||||
func (starcli *StarShell) SetRunUser(uid, gid uint32) {
|
|
||||||
starcli.cmd.SysProcAttr = &syscall.SysProcAttr{
|
|
||||||
Credential: &syscall.Credential{
|
|
||||||
Uid: uid,
|
|
||||||
Gid: gid,
|
|
||||||
},
|
|
||||||
Setsid: true,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func (starcli *StarShell) Release() error {
|
|
||||||
if err := starcli.cmd.Start(); err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
starcli.cmd.SysProcAttr = &syscall.SysProcAttr{
|
|
||||||
Setsid: true,
|
|
||||||
}
|
|
||||||
starcli.cmd.Process.Release()
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (starcli *StarShell) SetKeepCaps() error {
|
|
||||||
_, _, err := syscall.RawSyscall(157 /*SYS PRCTL */, 0x8 /*PR SET KEEPCAPS*/, 1, 0)
|
|
||||||
if 0 != err {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
return nil
|
|
||||||
}
|
|
@ -1,60 +0,0 @@
|
|||||||
package starainrt
|
|
||||||
|
|
||||||
import (
|
|
||||||
"fmt"
|
|
||||||
"testing"
|
|
||||||
"time"
|
|
||||||
)
|
|
||||||
|
|
||||||
func Test_Starshell(t *testing.T) {
|
|
||||||
star, err := NewStarShell("cmd.exe")
|
|
||||||
if err != nil {
|
|
||||||
fmt.Println(err)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
go func() {
|
|
||||||
time.Sleep(time.Second * 2)
|
|
||||||
star.WriteCmd("chcp 65001")
|
|
||||||
time.Sleep(time.Second * 2)
|
|
||||||
star.WriteCmd("ping baidu.com -n 10")
|
|
||||||
time.Sleep(time.Second * 12)
|
|
||||||
star.WriteCmd("exit")
|
|
||||||
}()
|
|
||||||
for star.IsRunning() {
|
|
||||||
time.Sleep(time.Millisecond * 100)
|
|
||||||
str, err := star.GetResult()
|
|
||||||
if err != nil {
|
|
||||||
fmt.Println("error:", err)
|
|
||||||
}
|
|
||||||
fmt.Print(str)
|
|
||||||
}
|
|
||||||
fmt.Println(star.ExitCode())
|
|
||||||
}
|
|
||||||
|
|
||||||
func Test_Starbash(t *testing.T) {
|
|
||||||
star, err := NewStarShell("bash", "-c", "ping baidu.com -c 10")
|
|
||||||
err = star.Start()
|
|
||||||
if err != nil {
|
|
||||||
fmt.Println(err)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
/*
|
|
||||||
go func() {
|
|
||||||
time.Sleep(time.Second * 2)
|
|
||||||
star.WriteCmd("chcp 65001")
|
|
||||||
time.Sleep(time.Second * 2)
|
|
||||||
star.WriteCmd("ping baidu.com -n 10")
|
|
||||||
time.Sleep(time.Second * 12)
|
|
||||||
star.WriteCmd("exit")
|
|
||||||
}()
|
|
||||||
*/
|
|
||||||
for star.IsRunning() {
|
|
||||||
time.Sleep(time.Millisecond * 100)
|
|
||||||
str, err := star.GetResult()
|
|
||||||
if err != nil {
|
|
||||||
fmt.Println("error:", err)
|
|
||||||
}
|
|
||||||
fmt.Print(str)
|
|
||||||
}
|
|
||||||
fmt.Println(star.ExitCode())
|
|
||||||
}
|
|
Loading…
x
Reference in New Issue
Block a user