初始化项目
This commit is contained in:
104
trunk/goutil/dbUtil/dataRow.go
Normal file
104
trunk/goutil/dbUtil/dataRow.go
Normal file
@@ -0,0 +1,104 @@
|
||||
package dbUtil
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"time"
|
||||
)
|
||||
|
||||
// 数据行结果
|
||||
type DataRow struct {
|
||||
// 所属数据表
|
||||
table *DataTable
|
||||
|
||||
// 行的所有值
|
||||
cells []interface{}
|
||||
}
|
||||
|
||||
// 行的所有原始值
|
||||
func (this *DataRow) CellOriginValues() []interface{} {
|
||||
return this.cells
|
||||
}
|
||||
|
||||
// 值的个数
|
||||
func (this *DataRow) Len() int {
|
||||
return len(this.cells)
|
||||
}
|
||||
|
||||
// 单元格的字符串值(可能为nil),如果有设置连接字符串:parseTime=true,则会有time.Time
|
||||
// celIndex:单元格序号
|
||||
// 返回值:
|
||||
// interface{}:单元格的字符串值
|
||||
// error:错误信息
|
||||
func (this *DataRow) CellValue(celIndex int) (interface{}, error) {
|
||||
if len(this.cells) <= celIndex {
|
||||
return nil, errors.New("cell out of range")
|
||||
}
|
||||
|
||||
// 检查是否为nil
|
||||
if this.cells[celIndex] == nil {
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
// 转换为字符串
|
||||
switch this.cells[celIndex].(type) {
|
||||
case []byte:
|
||||
return string(this.cells[celIndex].([]byte)), nil
|
||||
case string:
|
||||
return this.cells[celIndex].(string), nil
|
||||
case time.Time:
|
||||
return this.cells[celIndex].(time.Time), nil
|
||||
}
|
||||
|
||||
return nil, errors.New("unknown value type")
|
||||
}
|
||||
|
||||
// 单元格的字符串值(可能为nil),如果有设置连接字符串:parseTime=true,则会有time.Time
|
||||
// cellName:单元格名称
|
||||
// 返回值:
|
||||
// interface{}:单元格的字符串值
|
||||
// error:错误信息
|
||||
func (this *DataRow) CellValueByName(cellName string) (interface{}, error) {
|
||||
celIndex := this.table.cellIndex(cellName)
|
||||
if celIndex < 0 {
|
||||
return nil, errors.New("cell name no exist")
|
||||
}
|
||||
|
||||
return this.CellValue(celIndex)
|
||||
}
|
||||
|
||||
// 单元格的原始值
|
||||
// celIndex:单元格序号
|
||||
// 返回值:
|
||||
// interface{}:单元格的字符串值
|
||||
// error:错误信息
|
||||
func (this *DataRow) OriginCellValue(celIndex int) (interface{}, error) {
|
||||
if len(this.cells) <= celIndex {
|
||||
return nil, errors.New("cell out of range")
|
||||
}
|
||||
|
||||
return this.cells[celIndex], nil
|
||||
}
|
||||
|
||||
// 单元格的原始值
|
||||
// cellName:单元格名称
|
||||
// 返回值:
|
||||
// interface{}:单元格的字符串值
|
||||
// error:错误信息
|
||||
func (this *DataRow) OriginCellValueByName(cellName string) (interface{}, error) {
|
||||
celIndex := this.table.cellIndex(cellName)
|
||||
if celIndex < 0 {
|
||||
return nil, errors.New("cell name no exist")
|
||||
}
|
||||
|
||||
return this.OriginCellValue(celIndex)
|
||||
}
|
||||
|
||||
// 创建单元格对象
|
||||
// _table:所属表对象
|
||||
// _cells:单元格的值集合
|
||||
func newDataRow(_table *DataTable, _cells []interface{}) *DataRow {
|
||||
return &DataRow{
|
||||
table: _table,
|
||||
cells: _cells,
|
||||
}
|
||||
}
|
||||
189
trunk/goutil/dbUtil/dataTable.go
Normal file
189
trunk/goutil/dbUtil/dataTable.go
Normal file
@@ -0,0 +1,189 @@
|
||||
package dbUtil
|
||||
|
||||
import (
|
||||
"database/sql"
|
||||
"errors"
|
||||
)
|
||||
|
||||
// 数据表结构
|
||||
type DataTable struct {
|
||||
// 行对象集合
|
||||
rowData []*DataRow
|
||||
|
||||
// 列名称集合
|
||||
columnNames map[string]int
|
||||
}
|
||||
|
||||
// 数据表初始化
|
||||
// rows:原始的数据行信息
|
||||
// 返回值:
|
||||
// error:初始化的错误信息
|
||||
func (this *DataTable) init(rows *sql.Rows) error {
|
||||
defer func() {
|
||||
rows.Close()
|
||||
}()
|
||||
|
||||
// 读取列信息和保存列名称
|
||||
tmpColumns, errMsg := rows.Columns()
|
||||
if errMsg != nil {
|
||||
return errMsg
|
||||
}
|
||||
this.columnNames = make(map[string]int)
|
||||
for index, val := range tmpColumns {
|
||||
this.columnNames[val] = index
|
||||
}
|
||||
|
||||
// 读取行数据
|
||||
this.rowData = make([]*DataRow, 0)
|
||||
columnCount := len(this.columnNames)
|
||||
|
||||
args := make([]interface{}, columnCount)
|
||||
for rows.Next() {
|
||||
values := make([]interface{}, columnCount)
|
||||
for i := 0; i < columnCount; i++ {
|
||||
args[i] = &values[i]
|
||||
}
|
||||
rows.Scan(args...)
|
||||
|
||||
this.rowData = append(this.rowData, newDataRow(this, values))
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// 获取原始单元格值(一般为:string或[]byte)
|
||||
// rowIndex:行序号
|
||||
// cellIndex:单元格序号
|
||||
// 返回值:
|
||||
// interface{}:原始单元格值(一般为:string或[]byte)
|
||||
// error:获取错误信息
|
||||
func (this *DataTable) OriginCellValueByIndex(rowIndex int, cellIndex int) (interface{}, error) {
|
||||
if len(this.rowData) <= rowIndex {
|
||||
return nil, errors.New("row out of range")
|
||||
}
|
||||
|
||||
rowItem := this.rowData[rowIndex]
|
||||
if len(rowItem.cells) <= cellIndex {
|
||||
return nil, errors.New("column out of range")
|
||||
}
|
||||
|
||||
return rowItem.OriginCellValue(cellIndex)
|
||||
}
|
||||
|
||||
// 获取原始单元格值(一般为:string或[]byte)
|
||||
// rowIndex:行序号
|
||||
// cellIndex:单元格序号
|
||||
// 返回值:
|
||||
// interface{}:原始单元格值(一般为:string或[]byte)
|
||||
// error:获取错误信息
|
||||
func (this *DataTable) OriginCellValueByCellName(rowIndex int, cellName string) (interface{}, error) {
|
||||
if len(this.rowData) <= rowIndex {
|
||||
return nil, errors.New("row out of range")
|
||||
}
|
||||
|
||||
rowItem := this.rowData[rowIndex]
|
||||
|
||||
return rowItem.OriginCellValueByName(cellName)
|
||||
}
|
||||
|
||||
// 获取字符串的单元格值(有可能为nil)
|
||||
// rowIndex:行序号
|
||||
// cellIndex:单元格序号
|
||||
// 返回值:
|
||||
// interface{}:字符串的单元格值(有可能为nil)
|
||||
// error:获取错误信息
|
||||
func (this *DataTable) CellValueByIndex(rowIndex int, cellIndex int) (interface{}, error) {
|
||||
if len(this.rowData) <= rowIndex {
|
||||
return nil, errors.New("row out of range")
|
||||
}
|
||||
|
||||
rowItem := this.rowData[rowIndex]
|
||||
if len(rowItem.cells) <= cellIndex {
|
||||
return nil, errors.New("column out of range")
|
||||
}
|
||||
|
||||
return rowItem.CellValue(cellIndex)
|
||||
}
|
||||
|
||||
// 获取字符串的单元格值(有可能为nil)
|
||||
// rowIndex:行序号
|
||||
// cellIndex:单元格序号
|
||||
// 返回值:
|
||||
// interface{}:字符串的单元格值(有可能为nil)
|
||||
// error:获取错误信息
|
||||
func (this *DataTable) CellValueByName(rowIndex int, cellName string) (interface{}, error) {
|
||||
if len(this.rowData) <= rowIndex {
|
||||
return nil, errors.New("row out of range")
|
||||
}
|
||||
|
||||
rowItem := this.rowData[rowIndex]
|
||||
|
||||
return rowItem.CellValueByName(cellName)
|
||||
}
|
||||
|
||||
// 获取行对象
|
||||
// rowIndex:行序号
|
||||
// 返回值:
|
||||
// *DataRow:行对象
|
||||
// error:错误信息
|
||||
func (this *DataTable) Row(rowIndex int) (*DataRow, error) {
|
||||
if len(this.rowData) <= rowIndex {
|
||||
return nil, errors.New("row out of range")
|
||||
}
|
||||
|
||||
return this.rowData[rowIndex], nil
|
||||
}
|
||||
|
||||
// 根据列名获取列序号
|
||||
// cellName:列名
|
||||
// 返回值:
|
||||
// int:列序号
|
||||
func (this *DataTable) cellIndex(cellName string) int {
|
||||
cellIndex, isExist := this.columnNames[cellName]
|
||||
if isExist == false {
|
||||
return -1
|
||||
}
|
||||
|
||||
return cellIndex
|
||||
}
|
||||
|
||||
// 获取所有列的名字
|
||||
// 返回值:
|
||||
// []string:列字段名集合
|
||||
func (this *DataTable) Columns() []string {
|
||||
result := make([]string, len(this.columnNames))
|
||||
for key, val := range this.columnNames {
|
||||
result[val] = key
|
||||
}
|
||||
|
||||
return result
|
||||
}
|
||||
|
||||
// 获取列数量
|
||||
// 返回值:
|
||||
// int:列数量
|
||||
func (this *DataTable) ColumnCount() int {
|
||||
return len(this.columnNames)
|
||||
}
|
||||
|
||||
// 获取数据行数
|
||||
// 返回值:
|
||||
// int:行数
|
||||
func (this *DataTable) RowCount() int {
|
||||
return len(this.rowData)
|
||||
}
|
||||
|
||||
// 新建数据表对象
|
||||
// rows:数据行对象
|
||||
// 返回值:
|
||||
// *DataTable:数据表对象
|
||||
// error:错误信息
|
||||
func NewDataTable(rows *sql.Rows) (*DataTable, error) {
|
||||
table := &DataTable{}
|
||||
errMsg := table.init(rows)
|
||||
if errMsg != nil {
|
||||
return nil, errMsg
|
||||
}
|
||||
|
||||
return table, nil
|
||||
}
|
||||
195
trunk/goutil/dbUtil/valueConvert.go
Normal file
195
trunk/goutil/dbUtil/valueConvert.go
Normal file
@@ -0,0 +1,195 @@
|
||||
package dbUtil
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"time"
|
||||
|
||||
"goutil/typeUtil"
|
||||
)
|
||||
|
||||
// 类型转换为byte
|
||||
// 返回值:
|
||||
// byte:结果
|
||||
// error:错误数据
|
||||
func Byte(row *DataRow, key string) (byte, error) {
|
||||
val, errMsg := row.CellValueByName(key)
|
||||
if errMsg != nil {
|
||||
return 0, errMsg
|
||||
}
|
||||
|
||||
if val == nil {
|
||||
return 0, fmt.Errorf("value is nil")
|
||||
}
|
||||
|
||||
return typeUtil.Byte(val)
|
||||
}
|
||||
|
||||
// 类型转换为int
|
||||
// 返回值:
|
||||
// int:结果
|
||||
// error:错误数据
|
||||
func Int32(row *DataRow, key string) (int32, error) {
|
||||
val, errMsg := row.CellValueByName(key)
|
||||
if errMsg != nil {
|
||||
return 0, errMsg
|
||||
}
|
||||
|
||||
if val == nil {
|
||||
return 0, fmt.Errorf("value is nil")
|
||||
}
|
||||
|
||||
return typeUtil.Int32(val)
|
||||
}
|
||||
|
||||
// 类型转换为uint32
|
||||
// 返回值:
|
||||
// int:结果
|
||||
// error:错误数据
|
||||
func Uint32(row *DataRow, key string) (uint32, error) {
|
||||
val, errMsg := row.CellValueByName(key)
|
||||
if errMsg != nil {
|
||||
return 0, errMsg
|
||||
}
|
||||
|
||||
if val == nil {
|
||||
return 0, fmt.Errorf("value is nil")
|
||||
}
|
||||
|
||||
return typeUtil.Uint32(val)
|
||||
}
|
||||
|
||||
// 类型转换为int
|
||||
// 返回值:
|
||||
// int:结果
|
||||
// error:错误数据
|
||||
func Int(row *DataRow, key string) (int, error) {
|
||||
val, errMsg := row.CellValueByName(key)
|
||||
if errMsg != nil {
|
||||
return 0, errMsg
|
||||
}
|
||||
|
||||
if val == nil {
|
||||
return 0, fmt.Errorf("value is nil")
|
||||
}
|
||||
|
||||
return typeUtil.Int(val)
|
||||
}
|
||||
|
||||
// 类型转换为int
|
||||
// 返回值:
|
||||
// int:结果
|
||||
// error:错误数据
|
||||
func Uint(row *DataRow, key string) (uint, error) {
|
||||
val, errMsg := row.CellValueByName(key)
|
||||
if errMsg != nil {
|
||||
return 0, errMsg
|
||||
}
|
||||
|
||||
if val == nil {
|
||||
return 0, fmt.Errorf("value is nil")
|
||||
}
|
||||
|
||||
return typeUtil.Uint(val)
|
||||
}
|
||||
|
||||
// 类型转换为int
|
||||
// 返回值:
|
||||
// int:结果
|
||||
// error:错误数据
|
||||
func Int64(row *DataRow, key string) (int64, error) {
|
||||
val, errMsg := row.CellValueByName(key)
|
||||
if errMsg != nil {
|
||||
return 0, errMsg
|
||||
}
|
||||
|
||||
if val == nil {
|
||||
return 0, fmt.Errorf("value is nil")
|
||||
}
|
||||
|
||||
return typeUtil.Int64(val)
|
||||
}
|
||||
|
||||
// 类型转换为int
|
||||
// 返回值:
|
||||
// int:结果
|
||||
// error:错误数据
|
||||
func Uint64(row *DataRow, key string) (uint64, error) {
|
||||
val, errMsg := row.CellValueByName(key)
|
||||
if errMsg != nil {
|
||||
return 0, errMsg
|
||||
}
|
||||
|
||||
if val == nil {
|
||||
return 0, fmt.Errorf("value is nil")
|
||||
}
|
||||
|
||||
return typeUtil.Uint64(val)
|
||||
}
|
||||
|
||||
// 类型转换为int
|
||||
// 返回值:
|
||||
// float64:结果
|
||||
// error:错误数据
|
||||
func Float64(row *DataRow, key string) (float64, error) {
|
||||
val, errMsg := row.CellValueByName(key)
|
||||
if errMsg != nil {
|
||||
return 0, errMsg
|
||||
}
|
||||
|
||||
if val == nil {
|
||||
return 0, fmt.Errorf("value is nil")
|
||||
}
|
||||
|
||||
return typeUtil.Float64(val)
|
||||
}
|
||||
|
||||
// 类型转换为bool
|
||||
// 返回值:
|
||||
// bool:结果
|
||||
// error:错误信息
|
||||
func Bool(row *DataRow, key string) (bool, error) {
|
||||
val, errMsg := row.CellValueByName(key)
|
||||
if errMsg != nil {
|
||||
return false, errMsg
|
||||
}
|
||||
|
||||
if val == nil {
|
||||
return false, fmt.Errorf("value is nil")
|
||||
}
|
||||
|
||||
return typeUtil.Bool(val)
|
||||
}
|
||||
|
||||
// 类型转换为字符串
|
||||
// 返回值:
|
||||
// string:结果
|
||||
// error:错误信息
|
||||
func String(row *DataRow, key string) (string, error) {
|
||||
val, errMsg := row.CellValueByName(key)
|
||||
if errMsg != nil {
|
||||
return "", errMsg
|
||||
}
|
||||
|
||||
if val == nil {
|
||||
return "", fmt.Errorf("value is nil")
|
||||
}
|
||||
|
||||
return typeUtil.String(val)
|
||||
}
|
||||
|
||||
// 转换为时间格式,如果是字符串,则要求内容格式形如:2017-02-14 05:20:00
|
||||
// 返回值:
|
||||
// bool:结果
|
||||
// error:错误信息
|
||||
func DateTime(row *DataRow, key string) (time.Time, error) {
|
||||
val, errMsg := row.CellValueByName(key)
|
||||
if errMsg != nil {
|
||||
return time.Time{}, errMsg
|
||||
}
|
||||
|
||||
if val == nil {
|
||||
return time.Time{}, fmt.Errorf("value is nil")
|
||||
}
|
||||
|
||||
return typeUtil.DateTime(val)
|
||||
}
|
||||
Reference in New Issue
Block a user