| 96 | } |
| 97 | |
| 98 | func executeQueries(ctx context.Context, initData *query.InitData) int { |
| 99 | utils.LogTime("queryexecute.executeQueries start") |
| 100 | defer utils.LogTime("queryexecute.executeQueries end") |
| 101 | |
| 102 | // Check if Client is nil - this can happen if initialization failed |
| 103 | if initData.Client == nil { |
| 104 | error_helpers.ShowWarning("cannot execute queries: database client is not initialized") |
| 105 | return len(initData.Queries) |
| 106 | } |
| 107 | |
| 108 | // failures return the number of queries that failed and also the number of rows that |
| 109 | // returned errors |
| 110 | failures := 0 |
| 111 | t := time.Now() |
| 112 | |
| 113 | var err error |
| 114 | |
| 115 | for i, q := range initData.Queries { |
| 116 | // if executeQuery fails it returns err, else it returns the number of rows that returned errors while execution |
| 117 | if err, failures = executeQuery(ctx, initData, q); err != nil { |
| 118 | failures++ |
| 119 | error_helpers.ShowWarning(fmt.Sprintf("query %d of %d failed: %v", i+1, len(initData.Queries), error_helpers.DecodePgError(err))) |
| 120 | // if timing flag is enabled, show the time taken for the query to fail |
| 121 | if cmdconfig.Viper().GetString(pconstants.ArgTiming) != pconstants.ArgOff { |
| 122 | querydisplay.DisplayErrorTiming(t) |
| 123 | } |
| 124 | } |
| 125 | // TODO move into display layer |
| 126 | // Only show the blank line between queries, not after the last one |
| 127 | if (i < len(initData.Queries)-1) && showBlankLineBetweenResults() { |
| 128 | fmt.Println() |
| 129 | } |
| 130 | } |
| 131 | |
| 132 | return failures |
| 133 | } |
| 134 | |
| 135 | func executeQuery(ctx context.Context, initData *query.InitData, resolvedQuery *modconfig.ResolvedQuery) (error, int) { |
| 136 | utils.LogTime("query.execute.executeQuery start") |