From 081e27e11934307a30033082dcf82ecc59ed4a42 Mon Sep 17 00:00:00 2001 From: aozhiwei Date: Tue, 25 Jun 2024 20:59:26 +0800 Subject: [PATCH] 1 --- dataset.go | 2 +- dbpool.go | 4 +- new_dataset.go | 146 +++++++++++++++++++++++++++++++++++++++++++++++++ types.go | 2 +- 4 files changed, 150 insertions(+), 4 deletions(-) create mode 100644 new_dataset.go diff --git a/dataset.go b/dataset.go index a1b4394..d78ef2b 100644 --- a/dataset.go +++ b/dataset.go @@ -139,7 +139,7 @@ func (this *OldDataSet) GetRawValueByIndex(index int32) *string { } } -func NewDataSet(rows *sql.Rows) *OldDataSet { +func newOldDataSet(rows *sql.Rows) *OldDataSet { dataSet := new(OldDataSet) dataSet.rows = rows return dataSet diff --git a/dbpool.go b/dbpool.go index 3a4f6ba..e57c5c7 100644 --- a/dbpool.go +++ b/dbpool.go @@ -534,7 +534,7 @@ func (this *dbPool) internalQueryEx(dataSource string, sql string, params []stri var dataSet *DataSet if err == nil { - dataSet = NewDataSet(rows) + dataSet = newDataSet(rows) } freeFunc := func () { if dataSet != nil { @@ -570,7 +570,7 @@ func (this *dbPool) internalQueryOne(dataSource string, sql string, params []str var dataSet *DataSet if err == nil { - dataSet = NewDataSet(rows) + dataSet = newDataSet(rows) } freeFunc := func () { if dataSet != nil { diff --git a/new_dataset.go b/new_dataset.go new file mode 100644 index 0000000..afcf04f --- /dev/null +++ b/new_dataset.go @@ -0,0 +1,146 @@ +package f5 + +import ( + "database/sql" + "q5" +) + +type NewDataSet struct { + rows *sql.Rows + columns []string + values []interface{} + numOfReaded int64 + closed bool +} + +//已读取函数(调用Next成功的次数) +func (this *NewDataSet) NumOfReaded() int64 { + return this.numOfReaded +} + +func (this *NewDataSet) close() { + if this.rows != nil { + this.rows.Close() + } + this.closed = true +} + +func (this *NewDataSet) Next() bool { + if this.closed { + panic("NewDataSet 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("NewDataSet Next error:" + err.Error()) + } + return ret +} + +func (this *NewDataSet) GetColumns() []string { + if len(this.columns) <= 0 { + columns, err := this.rows.Columns() + if err == nil { + this.columns = columns + } + } + return this.columns +} + +/* + 安全版:nil值视为"" +*/ +func (this *NewDataSet) GetValues() *[]string { + values := []string{} + for _, val := range this.values { + 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.values { + 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.values[index].(*sql.NullString) + if sql_val.Valid { + return &sql_val.String + } else { + return nil + } +} + +func newDataSet(rows *sql.Rows) *NewDataSet { + dataSet := new(NewDataSet) + dataSet.rows = rows + return dataSet +} diff --git a/types.go b/types.go index 531a1e9..1eb1c79 100644 --- a/types.go +++ b/types.go @@ -34,7 +34,7 @@ type StreamPagination struct { Remaining int32 } -type DataSet = OldDataSet; +type DataSet = NewDataSet; type HandlerFunc func(*Context) type GinHandlerFunc func(*gin.Context)