This commit is contained in:
aozhiwei 2024-06-27 18:32:58 +08:00
parent 717e9616ae
commit a038d666cb
4 changed files with 55 additions and 205 deletions

View File

@ -5,68 +5,75 @@ import (
"q5"
)
type OldDataSet struct {
rows *sql.Rows
type DataSet struct {
columns []string
values []interface{}
rows [][]interface{}
currRow []interface{}
numOfReaded int64
closed bool
}
//已读取函数(调用Next成功的次数)
func (this *OldDataSet) NumOfReaded() int64 {
func (this *DataSet) NumOfReaded() int64 {
return this.numOfReaded
}
func (this *OldDataSet) close() {
if this.rows != nil {
this.rows.Close()
}
func (this *DataSet) close() {
this.closed = true
}
func (this *OldDataSet) init(rows *sql.Rows) {
this.rows = rows
}
func (this *DataSet) init(rows *sql.Rows) {
if rows == nil {
return
}
defer rows.Close()
func (this *OldDataSet) Next() bool {
if this.closed {
panic("OldDataSet is closed")
}
ret := this.rows.Next()
if !ret {
return ret
}
this.numOfReaded += 1
this.GetColumns()
this.values = []interface{}{}
for i := 0; i < len(this.columns); i++ {
str := sql.NullString{}
this.values = append(this.values, &str)
}
err := this.rows.Scan(this.values...)
if err != nil {
panic("OldDataSet Next error:" + err.Error())
}
return ret
}
func (this *OldDataSet) GetColumns() []string {
if len(this.columns) <= 0 {
columns, err := this.rows.Columns()
columns, err := rows.Columns()
if err == nil {
this.columns = columns
} else {
panic("NewDataSet Next Columns error:" + err.Error())
}
}
q5.NewSlice(&this.rows, 0, 100)
for rows.Next() {
values := []interface{}{}
for i := 0; i < len(this.columns); i++ {
str := sql.NullString{}
values = append(values, &str)
}
err := rows.Scan(values...)
if err != nil {
panic("NewDataSet Next Scan error:" + err.Error())
}
q5.AppendSlice(&this.rows, values)
}
}
func (this *DataSet) Next() bool {
if this.closed {
panic("NewDataSet is closed")
}
if this.numOfReaded >= int64(len(this.rows)) {
return false
}
this.currRow = this.rows[this.numOfReaded]
this.numOfReaded += 1
return true
}
func (this *DataSet) GetColumns() []string {
return this.columns
}
/*
安全版:nil值视为""
*/
func (this *OldDataSet) GetValues() *[]string {
func (this *DataSet) GetValues() *[]string {
values := []string{}
for _, val := range this.values {
for _, val := range this.currRow {
raw_val := val.(*sql.NullString)
if raw_val.Valid {
values = append(values, raw_val.String)
@ -80,7 +87,7 @@ func (this *OldDataSet) GetValues() *[]string {
/*
安全版:nil值视为""
*/
func (this *OldDataSet) GetByName(name string) string {
func (this *DataSet) GetByName(name string) string {
val := this.GetRawValueByName(name)
if val == nil {
return ""
@ -92,7 +99,7 @@ func (this *OldDataSet) GetByName(name string) string {
/*
安全版:nil值视为""
*/
func (this *OldDataSet) GetByIndex(index int32) string {
func (this *DataSet) GetByIndex(index int32) string {
val := this.GetRawValueByIndex(index)
if val == nil {
return ""
@ -104,9 +111,9 @@ func (this *OldDataSet) GetByIndex(index int32) string {
/*
!!!原始版: 调用方应处理值为nil的情况
*/
func (this *OldDataSet) GetRawValues() *[]string {
func (this *DataSet) GetRawValues() *[]string {
values := []*string{}
for _, val := range this.values {
for _, val := range this.currRow {
raw_val := val.(*sql.NullString)
if raw_val.Valid {
values = append(values, &raw_val.String)
@ -120,7 +127,7 @@ func (this *OldDataSet) GetRawValues() *[]string {
/*
!!!原始版: 调用方应处理值为nil的情况
*/
func (this *OldDataSet) GetRawValueByName(name string) *string {
func (this *DataSet) GetRawValueByName(name string) *string {
this.GetColumns()
for i := 0; i < len(this.columns); i++ {
if this.columns[i] == name {
@ -133,9 +140,9 @@ func (this *OldDataSet) GetRawValueByName(name string) *string {
/*
!!!原始版: 调用方应处理值为nil的情况
*/
func (this *OldDataSet) GetRawValueByIndex(index int32) *string {
func (this *DataSet) GetRawValueByIndex(index int32) *string {
this.GetColumns()
sql_val := this.values[index].(*sql.NullString)
sql_val := this.currRow[index].(*sql.NullString)
if sql_val.Valid {
return &sql_val.String
} else {
@ -143,8 +150,8 @@ func (this *OldDataSet) GetRawValueByIndex(index int32) *string {
}
}
func newOldDataSet(rows *sql.Rows) *OldDataSet {
dataSet := new(OldDataSet)
func newDataSet(rows *sql.Rows) *DataSet {
dataSet := new(DataSet)
dataSet.init(rows)
return dataSet
}

View File

@ -148,9 +148,10 @@ func (this *dbPool) LoopLoad(
this.RawQuery(
dataSource,
fmt.Sprintf("SELECT MAX(idx) FROM %s", watchTable),
params,
[]string{},
func (err error, ds *DataSet) {
})
time.Sleep(time.Second * 3)
}()
}
var lastIdx int64

View File

@ -1,157 +0,0 @@
package f5
import (
"database/sql"
"q5"
)
type NewDataSet struct {
columns []string
rows [][]interface{}
currRow []interface{}
numOfReaded int64
closed bool
}
//已读取函数(调用Next成功的次数)
func (this *NewDataSet) NumOfReaded() int64 {
return this.numOfReaded
}
func (this *NewDataSet) close() {
this.closed = true
}
func (this *NewDataSet) init(rows *sql.Rows) {
if rows == nil {
return
}
defer rows.Close()
if len(this.columns) <= 0 {
columns, err := rows.Columns()
if err == nil {
this.columns = columns
} else {
panic("NewDataSet Next Columns error:" + err.Error())
}
}
q5.NewSlice(&this.rows, 0, 100)
for rows.Next() {
values := []interface{}{}
for i := 0; i < len(this.columns); i++ {
str := sql.NullString{}
values = append(values, &str)
}
err := rows.Scan(values...)
if err != nil {
panic("NewDataSet Next Scan error:" + err.Error())
}
q5.AppendSlice(&this.rows, values)
}
}
func (this *NewDataSet) Next() bool {
if this.closed {
panic("NewDataSet is closed")
}
if this.numOfReaded >= int64(len(this.rows)) {
return false
}
this.currRow = this.rows[this.numOfReaded]
this.numOfReaded += 1
return true
}
func (this *NewDataSet) GetColumns() []string {
return this.columns
}
/*
安全版:nil值视为""
*/
func (this *NewDataSet) GetValues() *[]string {
values := []string{}
for _, val := range this.currRow {
raw_val := val.(*sql.NullString)
if raw_val.Valid {
values = append(values, raw_val.String)
} else {
values = append(values, *q5.NewEmptyStrPtr())
}
}
return &values
}
/*
安全版:nil值视为""
*/
func (this *NewDataSet) GetByName(name string) string {
val := this.GetRawValueByName(name)
if val == nil {
return ""
} else {
return *val
}
}
/*
安全版:nil值视为""
*/
func (this *NewDataSet) GetByIndex(index int32) string {
val := this.GetRawValueByIndex(index)
if val == nil {
return ""
} else {
return *val
}
}
/*
!!!原始版: 调用方应处理值为nil的情况
*/
func (this *NewDataSet) GetRawValues() *[]string {
values := []*string{}
for _, val := range this.currRow {
raw_val := val.(*sql.NullString)
if raw_val.Valid {
values = append(values, &raw_val.String)
} else {
values = append(values, nil)
}
}
return nil
}
/*
!!!原始版: 调用方应处理值为nil的情况
*/
func (this *NewDataSet) GetRawValueByName(name string) *string {
this.GetColumns()
for i := 0; i < len(this.columns); i++ {
if this.columns[i] == name {
return this.GetRawValueByIndex(int32(i))
}
}
return nil
}
/*
!!!原始版: 调用方应处理值为nil的情况
*/
func (this *NewDataSet) GetRawValueByIndex(index int32) *string {
this.GetColumns()
sql_val := this.currRow[index].(*sql.NullString)
if sql_val.Valid {
return &sql_val.String
} else {
return nil
}
}
func newDataSet(rows *sql.Rows) *NewDataSet {
dataSet := new(NewDataSet)
dataSet.init(rows)
return dataSet
}

View File

@ -34,7 +34,6 @@ type StreamPagination struct {
Remaining int32
}
type DataSet = NewDataSet;
type HandlerFunc func(*Context)
type GinHandlerFunc func(*gin.Context)