From 79b6f0a6d849b1b90a2f2e416d69f37f2046a763 Mon Sep 17 00:00:00 2001 From: wangqian Date: Sat, 9 Mar 2024 12:50:43 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0ColumnsDetails()=E6=96=B9?= =?UTF-8?q?=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- rows.go | 62 +++++++++++++++++++++++++++++++++++++++++++++++---------- 1 file changed, 51 insertions(+), 11 deletions(-) diff --git a/rows.go b/rows.go index 5d4ee2dba..3cea057f3 100644 --- a/rows.go +++ b/rows.go @@ -10,7 +10,6 @@ package mysql import ( "database/sql/driver" - "github.com/senhe-tech/go-utils/utils" "io" "math" "reflect" @@ -36,25 +35,66 @@ type textRows struct { mysqlRows } +type FieldData []byte + +type Field struct { + Data FieldData + Schema []byte + Table []byte + OrgTable []byte + Name []byte + OrgName []byte + Charset uint16 + ColumnLength uint32 + Type uint8 + Flag uint16 + Decimal uint8 + + DefaultValueLength uint64 + DefaultValue []byte + + DatabaseTypeName string +} + func (rows *mysqlRows) Columns() []string { if rows.rs.columnNames != nil { return rows.rs.columnNames } columns := make([]string, len(rows.rs.columns)) - for i := range columns { - fieldInfo := map[string]any{ - "table_name": rows.rs.columns[i].TableName, - "name": rows.rs.columns[i].Name, - "length": rows.rs.columns[i].Length, - "flags": rows.rs.columns[i].Flags, - "field_type": rows.rs.columns[i].FieldType, - "decimals": rows.rs.columns[i].Decimals, - "charset": rows.rs.columns[i].Charset, + if rows.mc != nil && rows.mc.cfg.ColumnsWithAlias { + for i := range columns { + if tableName := rows.rs.columns[i].TableName; len(tableName) > 0 { + columns[i] = tableName + "." + rows.rs.columns[i].Name + } else { + columns[i] = rows.rs.columns[i].Name + } + } + } else { + for i := range columns { + columns[i] = rows.rs.columns[i].Name } - columns[i] = utils.MustJSON(fieldInfo) } rows.rs.columnNames = columns + + return columns +} + +func (rows *mysqlRows) ColumnsDetails() []*Field { + columns := make([]*Field, len(rows.rs.columns)) + for i := range columns { + columns[i] = &Field{ + Table: []byte(rows.rs.columns[i].TableName), + Name: []byte(rows.rs.columns[i].Name), + Charset: uint16(rows.rs.columns[i].Charset), + ColumnLength: rows.rs.columns[i].Length, + Type: uint8(rows.rs.columns[i].FieldType), + Flag: uint16(rows.rs.columns[i].Flags), + Decimal: rows.rs.columns[i].Decimals, + DatabaseTypeName: rows.rs.columns[i].TypeDatabaseName(), + } + } + return columns }