Skip to content

Commit

Permalink
增加ColumnsDetails()方法
Browse files Browse the repository at this point in the history
  • Loading branch information
wq247934 committed Mar 8, 2024
1 parent 7f56479 commit 095d84a
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 12 deletions.
44 changes: 32 additions & 12 deletions rows.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,14 @@ package mysql

import (
"database/sql/driver"
"github.com/senhe-tech/go-utils/utils"
"io"
"math"
"reflect"
)

type resultSet struct {
columns []MysqlField
columnNames []string
columnNames []map[string]any
done bool
}

Expand All @@ -37,24 +36,45 @@ type textRows struct {
}

func (rows *mysqlRows) Columns() []string {
columns := make([]string, len(rows.rs.columns))
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
}
}

return columns
}

func (rows *mysqlRows) ColumnsDetails() []map[string]any {
if rows.rs.columnNames != nil {
return rows.rs.columnNames
}

columns := make([]string, len(rows.rs.columns))
columns := make([]map[string]any, 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,
columns[i] = 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,
"database_type_name": rows.rs.columns[i].TypeDatabaseName(),
}
columns[i] = utils.MustJSON(fieldInfo)
}

rows.rs.columnNames = columns

return columns
}

Expand Down
11 changes: 11 additions & 0 deletions utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -841,3 +841,14 @@ func mapIsolationLevel(level driver.IsolationLevel) (string, error) {
return "", fmt.Errorf("mysql: unsupported isolation level: %v", level)
}
}

func Convert[T uint16 | byte](data any) T {
switch v := data.(type) {
case fieldType:
return T(data.(fieldType))
case fieldFlag:
return T(data.(fieldFlag))
default:
panic(fmt.Sprintf("unsupported type: %T", v))
}
}

0 comments on commit 095d84a

Please sign in to comment.