1
This commit is contained in:
parent
717e9616ae
commit
a038d666cb
99
dataset.go
99
dataset.go
@ -5,68 +5,75 @@ import (
|
|||||||
"q5"
|
"q5"
|
||||||
)
|
)
|
||||||
|
|
||||||
type OldDataSet struct {
|
type DataSet struct {
|
||||||
rows *sql.Rows
|
|
||||||
columns []string
|
columns []string
|
||||||
values []interface{}
|
rows [][]interface{}
|
||||||
|
currRow []interface{}
|
||||||
numOfReaded int64
|
numOfReaded int64
|
||||||
closed bool
|
closed bool
|
||||||
}
|
}
|
||||||
|
|
||||||
//已读取函数(调用Next成功的次数)
|
//已读取函数(调用Next成功的次数)
|
||||||
func (this *OldDataSet) NumOfReaded() int64 {
|
func (this *DataSet) NumOfReaded() int64 {
|
||||||
return this.numOfReaded
|
return this.numOfReaded
|
||||||
}
|
}
|
||||||
|
|
||||||
func (this *OldDataSet) close() {
|
func (this *DataSet) close() {
|
||||||
if this.rows != nil {
|
|
||||||
this.rows.Close()
|
|
||||||
}
|
|
||||||
this.closed = true
|
this.closed = true
|
||||||
}
|
}
|
||||||
|
|
||||||
func (this *OldDataSet) init(rows *sql.Rows) {
|
func (this *DataSet) init(rows *sql.Rows) {
|
||||||
this.rows = 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 {
|
if len(this.columns) <= 0 {
|
||||||
columns, err := this.rows.Columns()
|
columns, err := rows.Columns()
|
||||||
if err == nil {
|
if err == nil {
|
||||||
this.columns = columns
|
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
|
return this.columns
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
安全版:nil值视为""
|
安全版:nil值视为""
|
||||||
*/
|
*/
|
||||||
func (this *OldDataSet) GetValues() *[]string {
|
func (this *DataSet) 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)
|
||||||
@ -80,7 +87,7 @@ func (this *OldDataSet) GetValues() *[]string {
|
|||||||
/*
|
/*
|
||||||
安全版:nil值视为""
|
安全版:nil值视为""
|
||||||
*/
|
*/
|
||||||
func (this *OldDataSet) GetByName(name string) string {
|
func (this *DataSet) GetByName(name string) string {
|
||||||
val := this.GetRawValueByName(name)
|
val := this.GetRawValueByName(name)
|
||||||
if val == nil {
|
if val == nil {
|
||||||
return ""
|
return ""
|
||||||
@ -92,7 +99,7 @@ func (this *OldDataSet) GetByName(name string) string {
|
|||||||
/*
|
/*
|
||||||
安全版:nil值视为""
|
安全版:nil值视为""
|
||||||
*/
|
*/
|
||||||
func (this *OldDataSet) GetByIndex(index int32) string {
|
func (this *DataSet) GetByIndex(index int32) string {
|
||||||
val := this.GetRawValueByIndex(index)
|
val := this.GetRawValueByIndex(index)
|
||||||
if val == nil {
|
if val == nil {
|
||||||
return ""
|
return ""
|
||||||
@ -104,9 +111,9 @@ func (this *OldDataSet) GetByIndex(index int32) string {
|
|||||||
/*
|
/*
|
||||||
!!!原始版: 调用方应处理值为nil的情况
|
!!!原始版: 调用方应处理值为nil的情况
|
||||||
*/
|
*/
|
||||||
func (this *OldDataSet) GetRawValues() *[]string {
|
func (this *DataSet) 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)
|
||||||
@ -120,7 +127,7 @@ func (this *OldDataSet) GetRawValues() *[]string {
|
|||||||
/*
|
/*
|
||||||
!!!原始版: 调用方应处理值为nil的情况
|
!!!原始版: 调用方应处理值为nil的情况
|
||||||
*/
|
*/
|
||||||
func (this *OldDataSet) GetRawValueByName(name string) *string {
|
func (this *DataSet) GetRawValueByName(name string) *string {
|
||||||
this.GetColumns()
|
this.GetColumns()
|
||||||
for i := 0; i < len(this.columns); i++ {
|
for i := 0; i < len(this.columns); i++ {
|
||||||
if this.columns[i] == name {
|
if this.columns[i] == name {
|
||||||
@ -133,9 +140,9 @@ func (this *OldDataSet) GetRawValueByName(name string) *string {
|
|||||||
/*
|
/*
|
||||||
!!!原始版: 调用方应处理值为nil的情况
|
!!!原始版: 调用方应处理值为nil的情况
|
||||||
*/
|
*/
|
||||||
func (this *OldDataSet) GetRawValueByIndex(index int32) *string {
|
func (this *DataSet) 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 {
|
||||||
@ -143,8 +150,8 @@ func (this *OldDataSet) GetRawValueByIndex(index int32) *string {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func newOldDataSet(rows *sql.Rows) *OldDataSet {
|
func newDataSet(rows *sql.Rows) *DataSet {
|
||||||
dataSet := new(OldDataSet)
|
dataSet := new(DataSet)
|
||||||
dataSet.init(rows)
|
dataSet.init(rows)
|
||||||
return dataSet
|
return dataSet
|
||||||
}
|
}
|
||||||
|
@ -148,9 +148,10 @@ func (this *dbPool) LoopLoad(
|
|||||||
this.RawQuery(
|
this.RawQuery(
|
||||||
dataSource,
|
dataSource,
|
||||||
fmt.Sprintf("SELECT MAX(idx) FROM %s", watchTable),
|
fmt.Sprintf("SELECT MAX(idx) FROM %s", watchTable),
|
||||||
params,
|
[]string{},
|
||||||
func (err error, ds *DataSet) {
|
func (err error, ds *DataSet) {
|
||||||
})
|
})
|
||||||
|
time.Sleep(time.Second * 3)
|
||||||
}()
|
}()
|
||||||
}
|
}
|
||||||
var lastIdx int64
|
var lastIdx int64
|
||||||
|
157
new_dataset.go
157
new_dataset.go
@ -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
|
|
||||||
}
|
|
Loading…
x
Reference in New Issue
Block a user