From 1ac8c66634f54df35039d4e2e4a66ba977a283d8 Mon Sep 17 00:00:00 2001 From: aozhiwei Date: Tue, 25 Jun 2024 21:29:16 +0800 Subject: [PATCH] 1 --- dataset.go | 6 ++++- new_dataset.go | 68 +++++++++++++++++++++++++++++--------------------- 2 files changed, 44 insertions(+), 30 deletions(-) diff --git a/dataset.go b/dataset.go index d78ef2b..44a92bb 100644 --- a/dataset.go +++ b/dataset.go @@ -25,6 +25,10 @@ func (this *OldDataSet) close() { this.closed = true } +func (this *OldDataSet) init(rows *sql.Rows) { + this.rows = rows +} + func (this *OldDataSet) Next() bool { if this.closed { panic("OldDataSet is closed") @@ -141,6 +145,6 @@ func (this *OldDataSet) GetRawValueByIndex(index int32) *string { func newOldDataSet(rows *sql.Rows) *OldDataSet { dataSet := new(OldDataSet) - dataSet.rows = rows + dataSet.init(rows) return dataSet } diff --git a/new_dataset.go b/new_dataset.go index afcf04f..f482238 100644 --- a/new_dataset.go +++ b/new_dataset.go @@ -6,9 +6,9 @@ import ( ) type NewDataSet struct { - rows *sql.Rows columns []string - values []interface{} + rows [][]interface{} + currRow []interface{} numOfReaded int64 closed bool } @@ -19,41 +19,51 @@ func (this *NewDataSet) NumOfReaded() int64 { } func (this *NewDataSet) close() { - if this.rows != nil { - this.rows.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()) + } + } + + 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") } - ret := this.rows.Next() - if !ret { - return ret + if this.numOfReaded >= int64(len(this.rows)) { + return false } 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 + this.currRow = this.rows[this.numOfReaded] + return true } func (this *NewDataSet) GetColumns() []string { - if len(this.columns) <= 0 { - columns, err := this.rows.Columns() - if err == nil { - this.columns = columns - } - } return this.columns } @@ -62,7 +72,7 @@ func (this *NewDataSet) GetColumns() []string { */ func (this *NewDataSet) 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) @@ -102,7 +112,7 @@ func (this *NewDataSet) GetByIndex(index int32) string { */ func (this *NewDataSet) 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) @@ -131,7 +141,7 @@ func (this *NewDataSet) GetRawValueByName(name string) *string { */ func (this *NewDataSet) 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 { @@ -141,6 +151,6 @@ func (this *NewDataSet) GetRawValueByIndex(index int32) *string { func newDataSet(rows *sql.Rows) *NewDataSet { dataSet := new(NewDataSet) - dataSet.rows = rows + dataSet.init(rows) return dataSet }