This commit is contained in:
aozhiwei 2024-06-25 21:29:16 +08:00
parent 081e27e119
commit 1ac8c66634
2 changed files with 44 additions and 30 deletions

View File

@ -25,6 +25,10 @@ func (this *OldDataSet) close() {
this.closed = true this.closed = true
} }
func (this *OldDataSet) init(rows *sql.Rows) {
this.rows = rows
}
func (this *OldDataSet) Next() bool { func (this *OldDataSet) Next() bool {
if this.closed { if this.closed {
panic("OldDataSet is closed") panic("OldDataSet is closed")
@ -141,6 +145,6 @@ func (this *OldDataSet) GetRawValueByIndex(index int32) *string {
func newOldDataSet(rows *sql.Rows) *OldDataSet { func newOldDataSet(rows *sql.Rows) *OldDataSet {
dataSet := new(OldDataSet) dataSet := new(OldDataSet)
dataSet.rows = rows dataSet.init(rows)
return dataSet return dataSet
} }

View File

@ -6,9 +6,9 @@ import (
) )
type NewDataSet struct { type NewDataSet struct {
rows *sql.Rows
columns []string columns []string
values []interface{} rows [][]interface{}
currRow []interface{}
numOfReaded int64 numOfReaded int64
closed bool closed bool
} }
@ -19,41 +19,51 @@ func (this *NewDataSet) NumOfReaded() int64 {
} }
func (this *NewDataSet) close() { func (this *NewDataSet) close() {
if this.rows != nil {
this.rows.Close()
}
this.closed = true 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 { func (this *NewDataSet) Next() bool {
if this.closed { if this.closed {
panic("NewDataSet is closed") panic("NewDataSet is closed")
} }
ret := this.rows.Next() if this.numOfReaded >= int64(len(this.rows)) {
if !ret { return false
return ret
} }
this.numOfReaded += 1 this.numOfReaded += 1
this.GetColumns() this.currRow = this.rows[this.numOfReaded]
this.values = []interface{}{} return true
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 { func (this *NewDataSet) GetColumns() []string {
if len(this.columns) <= 0 {
columns, err := this.rows.Columns()
if err == nil {
this.columns = columns
}
}
return this.columns return this.columns
} }
@ -62,7 +72,7 @@ func (this *NewDataSet) GetColumns() []string {
*/ */
func (this *NewDataSet) GetValues() *[]string { func (this *NewDataSet) GetValues() *[]string {
values := []string{} values := []string{}
for _, val := range this.values { for _, val := range this.currRow {
raw_val := val.(*sql.NullString) raw_val := val.(*sql.NullString)
if raw_val.Valid { if raw_val.Valid {
values = append(values, raw_val.String) values = append(values, raw_val.String)
@ -102,7 +112,7 @@ func (this *NewDataSet) GetByIndex(index int32) string {
*/ */
func (this *NewDataSet) GetRawValues() *[]string { func (this *NewDataSet) GetRawValues() *[]string {
values := []*string{} values := []*string{}
for _, val := range this.values { for _, val := range this.currRow {
raw_val := val.(*sql.NullString) raw_val := val.(*sql.NullString)
if raw_val.Valid { if raw_val.Valid {
values = append(values, &raw_val.String) values = append(values, &raw_val.String)
@ -131,7 +141,7 @@ func (this *NewDataSet) GetRawValueByName(name string) *string {
*/ */
func (this *NewDataSet) GetRawValueByIndex(index int32) *string { func (this *NewDataSet) GetRawValueByIndex(index int32) *string {
this.GetColumns() this.GetColumns()
sql_val := this.values[index].(*sql.NullString) sql_val := this.currRow[index].(*sql.NullString)
if sql_val.Valid { if sql_val.Valid {
return &sql_val.String return &sql_val.String
} else { } else {
@ -141,6 +151,6 @@ func (this *NewDataSet) GetRawValueByIndex(index int32) *string {
func newDataSet(rows *sql.Rows) *NewDataSet { func newDataSet(rows *sql.Rows) *NewDataSet {
dataSet := new(NewDataSet) dataSet := new(NewDataSet)
dataSet.rows = rows dataSet.init(rows)
return dataSet return dataSet
} }