func (m *mysqlPlugin) Exec(ctx context.Context, req *plugin.ExecRequest) (*plugin.ExecResponse, error) {
// ... connection setup ...
rows, err := db.Query(req.Query)
if err != nil {
return &plugin.ExecResponse{Error: fmt.Sprintf("query error: %v", err)}, nil
}
defer rows.Close()
cols, err := rows.Columns()
if err != nil {
return &plugin.ExecResponse{Error: fmt.Sprintf("cols error: %v", err)}, nil
}
// Prepare column metadata
colMeta := make([]*plugin.Column, len(cols))
for i, c := range cols {
colMeta[i] = &plugin.Column{Name: c}
}
// Scan rows
var rowResults []*plugin.Row
for rows.Next() {
vals := make([]interface{}, len(cols))
ptrs := make([]interface{}, len(cols))
for i := range vals {
ptrs[i] = &vals[i]
}
if err := rows.Scan(ptrs...); err != nil {
return &plugin.ExecResponse{Error: fmt.Sprintf("scan error: %v", err)}, nil
}
strs := make([]string, len(cols))
for i, v := range vals {
strs[i] = plugin.FormatSQLValue(v)
}
rowResults = append(rowResults, &plugin.Row{Values: strs})
}
return &plugin.ExecResponse{
Result: &plugin.ExecResult{
Payload: &pluginpb.PluginV1_ExecResult_Sql{
Sql: &plugin.SqlResult{
Columns: colMeta,
Rows: rowResults,
},
},
},
}, nil
}