f5/dataset.go
2023-08-21 16:42:02 +08:00

63 lines
1.1 KiB
Go

package f5
import (
"database/sql"
)
type DataSet struct {
rows *sql.Rows
columns []string
values []interface{}
}
func (this *DataSet) Next() bool {
ret := this.rows.Next()
this.GetColumns()
this.values = []interface{}{}
for i := 0; i < len(this.columns); i++ {
str := ""
this.values = append(this.values, &str)
}
this.rows.Scan(this.values...)
return ret
}
func (this *DataSet) GetColumns() []string {
if len(this.columns) <= 0 {
columns, err := this.rows.Columns()
if err == nil {
this.columns = columns
}
}
return this.columns
}
func (this *DataSet) GetValues() *[]*string {
values := []*string{}
for _, val := range this.values {
values = append(values, val.(*string))
}
return &values
}
func (this *DataSet) GetByName(name string) *string {
this.GetColumns()
for i := 0; i < len(this.columns); i++ {
if this.columns[i] == name {
return this.GetByIndex(int32(i))
}
}
return nil
}
func (this *DataSet) GetByIndex(index int32) *string {
this.GetColumns()
return this.values[index].(*string)
}
func NewDataSet(rows *sql.Rows) *DataSet {
dataSet := new(DataSet)
dataSet.rows = rows
return dataSet
}