From 0431c236bd92f7176b45852a85f1fab6d9f3ab86 Mon Sep 17 00:00:00 2001 From: aozhiwei Date: Mon, 11 Dec 2023 17:48:52 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8Dnull=E5=80=BC=E9=97=AE?= =?UTF-8?q?=E9=A2=98,=E6=B7=BB=E5=8A=A0=E5=AE=89=E5=85=A8=E7=89=88?= =?UTF-8?q?=E5=92=8C=E9=9D=9E=E5=AE=89=E5=85=A8=E7=89=88=E6=8E=A5=E5=8F=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- dataset.go | 87 ++++++++++++++++++++++++++++++++++++++++++++++-------- dbpool.go | 4 +-- 2 files changed, 77 insertions(+), 14 deletions(-) diff --git a/dataset.go b/dataset.go index f1773d5..deab701 100644 --- a/dataset.go +++ b/dataset.go @@ -2,6 +2,7 @@ package f5 import ( "database/sql" + "q5" ) type DataSet struct { @@ -15,10 +16,13 @@ func (this *DataSet) Next() bool { this.GetColumns() this.values = []interface{}{} for i := 0; i < len(this.columns); i++ { - str := "" + str := sql.NullString{} this.values = append(this.values, &str) } - this.rows.Scan(this.values...) + err := this.rows.Scan(this.values...) + if err != nil { + panic("DataSet Next error:" + err.Error()) + } return ret } @@ -32,27 +36,86 @@ func (this *DataSet) GetColumns() []string { return this.columns } -func (this *DataSet) GetValues() *[]*string { - values := []*string{} +/* + 安全版:nil值视为"" +*/ +func (this *DataSet) GetValues() *[]string { + values := []string{} for _, val := range this.values { - values = append(values, val.(*string)) + raw_val := val.(*sql.NullString) + if raw_val.Valid { + values = append(values, raw_val.String) + } else { + values = append(values, *q5.NewEmptyStrPtr()) + } } 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)) +/* + 安全版:nil值视为"" +*/ +func (this *DataSet) GetByName(name string) string { + val := this.GetRawValueByName(name) + if val == nil { + return "" + } else { + return *val + } +} + +/* + 安全版:nil值视为"" +*/ +func (this *DataSet) GetByIndex(index int32) string { + val := this.GetRawValueByIndex(index) + if val == nil { + return "" + } else { + return *val + } +} + +/* + !!!原始版: 调用方应处理值为nil的情况 +*/ +func (this *DataSet) 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 } -func (this *DataSet) GetByIndex(index int32) *string { +/* + !!!原始版: 调用方应处理值为nil的情况 +*/ +func (this *DataSet) GetRawValueByName(name string) *string { this.GetColumns() - return this.values[index].(*string) + for i := 0; i < len(this.columns); i++ { + if this.columns[i] == name { + return this.GetRawValueByIndex(int32(i)) + } + } + return nil +} + +/* + !!!原始版: 调用方应处理值为nil的情况 +*/ +func (this *DataSet) 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) *DataSet { diff --git a/dbpool.go b/dbpool.go index 7b8abcb..e01a18a 100644 --- a/dbpool.go +++ b/dbpool.go @@ -238,8 +238,8 @@ func (this *dbPool) PageQuery( return } if rows != nil && rows.Next() { - pagination.Total = q5.ToInt32(*rows.GetByIndex(0)) - pagination.TotalPages = int32(math.Ceil(q5.ToFloat64(*rows.GetByIndex(0)) / + pagination.Total = q5.ToInt32(rows.GetByIndex(0)) + pagination.TotalPages = int32(math.Ceil(q5.ToFloat64(rows.GetByIndex(0)) / float64(pagination.PerPage))) } start := pagination.PerPage * (pagination.CurrentPage - 1)