RunLimitedVariablesQuery split up a query with more variables than the used database can handle in multiple queries.
(ctx context.Context, query string, qp QueryProvider, variables []interface{}, limit uint, rowHandler func(*sql.Rows) error)
| 130 | |
| 131 | // RunLimitedVariablesQuery split up a query with more variables than the used database can handle in multiple queries. |
| 132 | func RunLimitedVariablesQuery(ctx context.Context, query string, qp QueryProvider, variables []interface{}, limit uint, rowHandler func(*sql.Rows) error) error { |
| 133 | var start int |
| 134 | for start < len(variables) { |
| 135 | n := minOfInts(len(variables)-start, int(limit)) |
| 136 | nextQuery := strings.Replace(query, "($1)", QueryVariadic(n), 1) |
| 137 | rows, err := qp.QueryContext(ctx, nextQuery, variables[start:start+n]...) |
| 138 | if err != nil { |
| 139 | util.GetLogger(ctx).WithError(err).Error("QueryContext returned an error") |
| 140 | return err |
| 141 | } |
| 142 | err = rowHandler(rows) |
| 143 | if closeErr := rows.Close(); closeErr != nil { |
| 144 | util.GetLogger(ctx).WithError(closeErr).Error("RunLimitedVariablesQuery: failed to close rows") |
| 145 | return err |
| 146 | } |
| 147 | if err != nil { |
| 148 | util.GetLogger(ctx).WithError(err).Error("RunLimitedVariablesQuery: rowHandler returned error") |
| 149 | return err |
| 150 | } |
| 151 | start = start + n |
| 152 | } |
| 153 | return nil |
| 154 | } |
| 155 | |
| 156 | // StatementList is a list of SQL statements to prepare and a pointer to where to store the resulting prepared statement. |
| 157 | type StatementList []struct { |