GetSpecifiedColumnValueAndClose get columns' values whose name is equal to columnName and close the given rows
(rows *sql.Rows, columnName string)
| 780 | |
| 781 | // GetSpecifiedColumnValueAndClose get columns' values whose name is equal to columnName and close the given rows |
| 782 | func GetSpecifiedColumnValueAndClose(rows *sql.Rows, columnName string) ([]string, error) { |
| 783 | if rows == nil { |
| 784 | return []string{}, nil |
| 785 | } |
| 786 | defer rows.Close() |
| 787 | var strs []string |
| 788 | columns, _ := rows.Columns() |
| 789 | addr := make([]any, len(columns)) |
| 790 | oneRow := make([]sql.NullString, len(columns)) |
| 791 | fieldIndex := -1 |
| 792 | for i, col := range columns { |
| 793 | if strings.EqualFold(col, columnName) { |
| 794 | fieldIndex = i |
| 795 | } |
| 796 | addr[i] = &oneRow[i] |
| 797 | } |
| 798 | if fieldIndex == -1 { |
| 799 | return strs, nil |
| 800 | } |
| 801 | for rows.Next() { |
| 802 | err := rows.Scan(addr...) |
| 803 | if err != nil { |
| 804 | return strs, errors.Trace(err) |
| 805 | } |
| 806 | if oneRow[fieldIndex].Valid { |
| 807 | strs = append(strs, oneRow[fieldIndex].String) |
| 808 | } |
| 809 | } |
| 810 | return strs, errors.Trace(rows.Err()) |
| 811 | } |
| 812 | |
| 813 | // GetSpecifiedColumnValuesAndClose get columns' values whose name is equal to columnName |
| 814 | func GetSpecifiedColumnValuesAndClose(rows *sql.Rows, columnName ...string) ([][]string, error) { |