/
db_client_execute.go
384 lines (338 loc) · 12.6 KB
/
db_client_execute.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
package db_client
import (
"context"
"database/sql"
"errors"
"fmt"
"net/netip"
"strings"
"time"
"github.com/google/uuid"
"github.com/jackc/pgx/v5"
"github.com/jackc/pgx/v5/pgtype"
"github.com/spf13/viper"
"github.com/turbot/go-kit/helpers"
"github.com/turbot/steampipe/pkg/constants"
"github.com/turbot/steampipe/pkg/db/db_common"
"github.com/turbot/steampipe/pkg/error_helpers"
"github.com/turbot/steampipe/pkg/query/queryresult"
"github.com/turbot/steampipe/pkg/statushooks"
"github.com/turbot/steampipe/pkg/utils"
"golang.org/x/text/language"
"golang.org/x/text/message"
)
// ExecuteSync implements Client
// execute a query against this client and wait for the result
func (c *DbClient) ExecuteSync(ctx context.Context, query string, args ...any) (*queryresult.SyncQueryResult, error) {
// acquire a session
sessionResult := c.AcquireSession(ctx)
if sessionResult.Error != nil {
return nil, sessionResult.Error
}
// set setShouldShowTiming flag
// (this will refetch ScanMetadataMaxId if timing has just been enabled)
c.setShouldShowTiming(ctx, sessionResult.Session)
defer func() {
// we need to do this in a closure, otherwise the ctx will be evaluated immediately
// and not in call-time
sessionResult.Session.Close(error_helpers.IsContextCanceled(ctx))
}()
return c.ExecuteSyncInSession(ctx, sessionResult.Session, query, args...)
}
// ExecuteSyncInSession implements Client
// execute a query against this client and wait for the result
func (c *DbClient) ExecuteSyncInSession(ctx context.Context, session *db_common.DatabaseSession, query string, args ...any) (*queryresult.SyncQueryResult, error) {
if query == "" {
return &queryresult.SyncQueryResult{}, nil
}
result, err := c.ExecuteInSession(ctx, session, nil, query, args...)
if err != nil {
return nil, error_helpers.WrapError(err)
}
syncResult := &queryresult.SyncQueryResult{Cols: result.Cols}
for row := range *result.RowChan {
select {
case <-ctx.Done():
default:
// save the first row error to return
if row.Error != nil && err == nil {
err = error_helpers.WrapError(row.Error)
}
syncResult.Rows = append(syncResult.Rows, row)
}
}
if c.shouldShowTiming() {
syncResult.TimingResult = <-result.TimingResult
}
return syncResult, err
}
// Execute implements Client
// execute the query in the given Context
// NOTE: The returned Result MUST be fully read - otherwise the connection will block and will prevent further communication
func (c *DbClient) Execute(ctx context.Context, query string, args ...any) (*queryresult.Result, error) {
// acquire a session
sessionResult := c.AcquireSession(ctx)
if sessionResult.Error != nil {
return nil, sessionResult.Error
}
// disable statushooks when timing is enabled, because setShouldShowTiming internally calls the readRows funcs which
// calls the statushooks.Done, which hides the `Executing query…` spinner, when timing is enabled.
timingCtx := statushooks.DisableStatusHooks(ctx)
// re-read ArgTiming from viper (in case the .timing command has been run)
// (this will refetch ScanMetadataMaxId if timing has just been enabled)
c.setShouldShowTiming(timingCtx, sessionResult.Session)
// define callback to close session when the async execution is complete
closeSessionCallback := func() { sessionResult.Session.Close(error_helpers.IsContextCanceled(ctx)) }
return c.ExecuteInSession(ctx, sessionResult.Session, closeSessionCallback, query, args...)
}
// ExecuteInSession implements Client
// execute the query in the given Context using the provided DatabaseSession
// ExecuteInSession assumes no responsibility over the lifecycle of the DatabaseSession - that is the responsibility of the caller
// NOTE: The returned Result MUST be fully read - otherwise the connection will block and will prevent further communication
func (c *DbClient) ExecuteInSession(ctx context.Context, session *db_common.DatabaseSession, onComplete func(), query string, args ...any) (res *queryresult.Result, err error) {
if query == "" {
return queryresult.NewResult(nil), nil
}
// fail-safes
if session == nil {
return nil, fmt.Errorf("nil session passed to ExecuteInSession")
}
if session.Connection == nil {
return nil, fmt.Errorf("nil database connection passed to ExecuteInSession")
}
startTime := time.Now()
// get a context with a timeout for the query to execute within
// we don't use the cancelFn from this timeout context, since usage will lead to 'pgx'
// prematurely closing the database connection that this query executed in
ctxExecute := c.getExecuteContext(ctx)
var tx *sql.Tx
defer func() {
if err != nil {
err = error_helpers.HandleQueryTimeoutError(err)
// stop spinner in case of error
statushooks.Done(ctxExecute)
// error - rollback transaction if we have one
if tx != nil {
_ = tx.Rollback()
}
// in case of error call the onComplete callback
if onComplete != nil {
onComplete()
}
}
}()
// start query
var rows pgx.Rows
rows, err = c.startQueryWithRetries(ctxExecute, session, query, args...)
if err != nil {
return
}
colDefs := fieldDescriptionsToColumns(rows.FieldDescriptions(), session.Connection.Conn())
result := queryresult.NewResult(colDefs)
// read the rows in a go routine
go func() {
// define a callback which fetches the timing information
// this will be invoked after reading rows is complete but BEFORE closing the rows object (which closes the connection)
timingCallback := func() {
c.getQueryTiming(ctxExecute, startTime, session, result.TimingResult)
}
// read in the rows and stream to the query result object
c.readRows(ctxExecute, rows, result, timingCallback)
// call the completion callback - if one was provided
if onComplete != nil {
onComplete()
}
}()
return result, nil
}
func (c *DbClient) getExecuteContext(ctx context.Context) context.Context {
queryTimeout := time.Duration(viper.GetInt(constants.ArgDatabaseQueryTimeout)) * time.Second
// if timeout is zero, do not set a timeout
if queryTimeout == 0 {
return ctx
}
// create a context with a deadline
shouldBeDoneBy := time.Now().Add(queryTimeout)
//nolint:golint,lostcancel //we don't use this cancel fn because, pgx prematurely cancels the PG connection when this cancel gets called in 'defer'
newCtx, _ := context.WithDeadline(ctx, shouldBeDoneBy)
return newCtx
}
func (c *DbClient) getQueryTiming(ctx context.Context, startTime time.Time, session *db_common.DatabaseSession, resultChannel chan *queryresult.TimingResult) {
if !c.shouldShowTiming() {
return
}
var timingResult = &queryresult.TimingResult{
Duration: time.Since(startTime),
}
// disable fetching timing information to avoid recursion
c.disableTiming = true
// whatever happens, we need to reenable timing, and send the result back with at least the duration
defer func() {
c.disableTiming = false
resultChannel <- timingResult
}()
var scanRows *ScanMetadataRow
err := db_common.ExecuteSystemClientCall(ctx, session.Connection.Conn(), func(ctx context.Context, tx pgx.Tx) error {
query := fmt.Sprintf("select id, rows_fetched, cache_hit, hydrate_calls from %s.%s where id > %d", constants.InternalSchema, constants.ForeignTableScanMetadata, session.ScanMetadataMaxId)
rows, err := tx.Query(ctx, query)
if err != nil {
return err
}
scanRows, err = pgx.CollectOneRow(rows, pgx.RowToAddrOfStructByName[ScanMetadataRow])
return err
})
// if we failed to read scan metadata (either because the query failed or the plugin does not support it) just return
// we don't return the error, since we don't want to error out in this case
if err != nil || scanRows == nil {
return
}
// so we have scan metadata - create the metadata struct
timingResult.Metadata = &queryresult.TimingMetadata{}
timingResult.Metadata.HydrateCalls += scanRows.HydrateCalls
if scanRows.CacheHit {
timingResult.Metadata.CachedRowsFetched += scanRows.RowsFetched
} else {
timingResult.Metadata.RowsFetched += scanRows.RowsFetched
}
// update the max id for this session
session.ScanMetadataMaxId = scanRows.Id
}
func (c *DbClient) updateScanMetadataMaxId(ctx context.Context, session *db_common.DatabaseSession) error {
return db_common.ExecuteSystemClientCall(ctx, session.Connection.Conn(), func(ctx context.Context, tx pgx.Tx) error {
row := tx.QueryRow(ctx, fmt.Sprintf("select max(id) from %s.%s", constants.InternalSchema, constants.ForeignTableScanMetadata))
err := row.Scan(&session.ScanMetadataMaxId)
if errors.Is(err, pgx.ErrNoRows) {
return nil
}
return err
})
}
// run query in a goroutine, so we can check for cancellation
// in case the client becomes unresponsive and does not respect context cancellation
func (c *DbClient) startQuery(ctx context.Context, conn *pgx.Conn, query string, args ...any) (rows pgx.Rows, err error) {
doneChan := make(chan bool)
go func() {
// start asynchronous query
rows, err = conn.Query(ctx, query, args...)
close(doneChan)
}()
select {
case <-doneChan:
case <-ctx.Done():
err = ctx.Err()
}
return
}
func (c *DbClient) readRows(ctx context.Context, rows pgx.Rows, result *queryresult.Result, timingCallback func()) {
// defer this, so that these get cleaned up even if there is an unforeseen error
defer func() {
// we are done fetching results. time for display. clear the status indication
statushooks.Done(ctx)
// call the timing callback BEFORE closing the rows
timingCallback()
// close the sql rows object
rows.Close()
if err := rows.Err(); err != nil {
result.StreamError(err)
}
// close the channels in the result object
result.Close()
}()
rowCount := 0
Loop:
for rows.Next() {
select {
case <-ctx.Done():
statushooks.SetStatus(ctx, "Cancelling query")
break Loop
default:
rowResult, err := readRow(rows, result.Cols)
if err != nil {
// the error will be streamed in the defer
break Loop
}
// TACTICAL
// determine whether to stop the spinner as soon as we stream a row or to wait for completion
if isStreamingOutput() {
statushooks.Done(ctx)
}
result.StreamRow(rowResult)
// update the status message with the count of rows that have already been fetched
// this will not show if the spinner is not active
statushooks.SetStatus(ctx, fmt.Sprintf("Loading results: %3s", humanizeRowCount(rowCount)))
rowCount++
}
}
}
func readRow(rows pgx.Rows, cols []*queryresult.ColumnDef) ([]interface{}, error) {
columnValues, err := rows.Values()
if err != nil {
return nil, error_helpers.WrapError(err)
}
return populateRow(columnValues, cols)
}
func populateRow(columnValues []interface{}, cols []*queryresult.ColumnDef) ([]interface{}, error) {
result := make([]interface{}, len(columnValues))
for i, columnValue := range columnValues {
if columnValue != nil {
result[i] = columnValue
switch cols[i].DataType {
case "_TEXT":
if arr, ok := columnValue.([]interface{}); ok {
elements := utils.Map(arr, func(e interface{}) string { return e.(string) })
result[i] = strings.Join(elements, ",")
}
case "INET":
if inet, ok := columnValue.(netip.Prefix); ok {
result[i] = strings.TrimSuffix(inet.String(), "/32")
}
case "UUID":
if bytes, ok := columnValue.([16]uint8); ok {
if u, err := uuid.FromBytes(bytes[:]); err == nil {
result[i] = u
}
}
case "TIME":
if t, ok := columnValue.(pgtype.Time); ok {
result[i] = time.UnixMicro(t.Microseconds).UTC().Format("15:04:05")
}
case "INTERVAL":
if interval, ok := columnValue.(pgtype.Interval); ok {
var sb strings.Builder
years := interval.Months / 12
months := interval.Months % 12
if years > 0 {
sb.WriteString(fmt.Sprintf("%d %s ", years, utils.Pluralize("year", int(years))))
}
if months > 0 {
sb.WriteString(fmt.Sprintf("%d %s ", months, utils.Pluralize("mon", int(months))))
}
if interval.Days > 0 {
sb.WriteString(fmt.Sprintf("%d %s ", interval.Days, utils.Pluralize("day", int(interval.Days))))
}
if interval.Microseconds > 0 {
d := time.Duration(interval.Microseconds) * time.Microsecond
formatStr := time.Unix(0, 0).UTC().Add(d).Format("15:04:05")
sb.WriteString(formatStr)
}
result[i] = sb.String()
}
case "NUMERIC":
if numeric, ok := columnValue.(pgtype.Numeric); ok {
if f, err := numeric.Float64Value(); err == nil {
result[i] = f.Float64
}
}
}
}
}
return result, nil
}
func isStreamingOutput() bool {
outputFormat := viper.GetString(constants.ArgOutput)
return helpers.StringSliceContains([]string{constants.OutputFormatCSV, constants.OutputFormatLine}, outputFormat)
}
func humanizeRowCount(count int) string {
p := message.NewPrinter(language.English)
return p.Sprintf("%d", count)
}