MCPcopy Create free account
hub / github.com/bytebase/bytebase / RowsToQueryResult

Function RowsToQueryResult

backend/plugin/db/util/driverutil.go:89–140  ·  view source on GitHub ↗
(rows *sql.Rows, valueMaker func(string, *sql.ColumnType) any, rowValueConverter func(string, *sql.ColumnType, any) *v1pb.RowValue, limit int64)

Source from the content-addressed store, hash-verified

87}
88
89func RowsToQueryResult(rows *sql.Rows, valueMaker func(string, *sql.ColumnType) any, rowValueConverter func(string, *sql.ColumnType, any) *v1pb.RowValue, limit int64) (*v1pb.QueryResult, error) {
90 columnNames, err := rows.Columns()
91 if err != nil {
92 return nil, err
93 }
94 columnTypes, err := rows.ColumnTypes()
95 if err != nil {
96 return nil, err
97 }
98 // DatabaseTypeName returns the database system name of the column type.
99 // refer: https://pkg.go.dev/database/sql#ColumnType.DatabaseTypeName
100 var columnTypeNames []string
101 for _, v := range columnTypes {
102 columnTypeNames = append(columnTypeNames, strings.ToUpper(v.DatabaseTypeName()))
103 }
104 result := &v1pb.QueryResult{
105 ColumnNames: columnNames,
106 ColumnTypeNames: columnTypeNames,
107 }
108 columnLength := len(columnNames)
109
110 if columnLength > 0 {
111 for rows.Next() {
112 values := make([]any, columnLength)
113 for i, v := range columnTypeNames {
114 values[i] = valueMaker(v, columnTypes[i])
115 }
116
117 if err := rows.Scan(values...); err != nil {
118 return nil, err
119 }
120
121 row := &v1pb.QueryRow{}
122 for i := range columnLength {
123 row.Values = append(row.Values, rowValueConverter(columnTypeNames[i], columnTypes[i], values[i]))
124 }
125
126 result.Rows = append(result.Rows, row)
127 n := len(result.Rows)
128 if (n&(n-1) == 0) && int64(proto.Size(result)) > limit {
129 result.Error = common.FormatMaximumSQLResultSizeMessage(limit)
130 break
131 }
132 }
133 }
134
135 if err := rows.Err(); err != nil {
136 return nil, err
137 }
138 result.RowsCount = int64(len(result.Rows))
139 return result, nil
140}
141
142func MakeCommonValueByTypeName(typeName string, _ *sql.ColumnType) any {
143 switch typeName {

Callers 12

QueryConnMethod · 0.92
QueryConnMethod · 0.92
QueryConnMethod · 0.92
queryBatchMethod · 0.92
QueryConnMethod · 0.92
QueryConnMethod · 0.92
QueryConnMethod · 0.92
QueryConnMethod · 0.92
QueryConnMethod · 0.92
QueryConnMethod · 0.92
QueryConnMethod · 0.92
QueryConnMethod · 0.92

Calls 4

ColumnsMethod · 0.80
ScanMethod · 0.80
NextMethod · 0.45

Tested by

no test coverage detected