Purge deletes all records that match the given query. It returns the number of successful deletes and an error.
(ctx context.Context, q *query.Query, local, internal, shadowDelete bool)
| 389 | |
| 390 | // Purge deletes all records that match the given query. It returns the number of successful deletes and an error. |
| 391 | func (db *SQLite) Purge(ctx context.Context, q *query.Query, local, internal, shadowDelete bool) (int, error) { |
| 392 | db.wg.Add(1) |
| 393 | defer db.wg.Done() |
| 394 | |
| 395 | // Optimize for local and internal queries without where clause and without shadow delete. |
| 396 | if local && internal && !shadowDelete && !q.HasWhereCondition() { |
| 397 | // First count entries (SQLite does not support affected rows) |
| 398 | n, err := models.Records.Query( |
| 399 | models.SelectWhere.Records.Key.Like(q.DatabaseKeyPrefix()+"%"), |
| 400 | ).Count(db.ctx, db.bob) |
| 401 | if err != nil || n == 0 { |
| 402 | return int(n), err |
| 403 | } |
| 404 | |
| 405 | // Delete entries. |
| 406 | _, err = models.Records.Delete( |
| 407 | models.DeleteWhere.Records.Key.Like(q.DatabaseKeyPrefix()+"%"), |
| 408 | ).Exec(db.ctx, db.bob) |
| 409 | return int(n), err |
| 410 | } |
| 411 | |
| 412 | // Optimize for local and internal queries without where clause, but with shadow delete. |
| 413 | if local && internal && shadowDelete && !q.HasWhereCondition() { |
| 414 | // First count entries (SQLite does not support affected rows) |
| 415 | n, err := models.Records.Query( |
| 416 | models.SelectWhere.Records.Key.Like(q.DatabaseKeyPrefix()+"%"), |
| 417 | ).Count(db.ctx, db.bob) |
| 418 | if err != nil || n == 0 { |
| 419 | return int(n), err |
| 420 | } |
| 421 | |
| 422 | // Mark purged records as deleted. |
| 423 | now := time.Now().Unix() |
| 424 | _, err = models.Records.Update( |
| 425 | um.SetCol("format").ToArg(nil), |
| 426 | um.SetCol("value").ToArg(nil), |
| 427 | um.SetCol("deleted").ToArg(now), |
| 428 | models.UpdateWhere.Records.Key.Like(q.DatabaseKeyPrefix()+"%"), |
| 429 | ).Exec(db.ctx, db.bob) |
| 430 | return int(n), err |
| 431 | } |
| 432 | |
| 433 | // Otherwise, iterate over all entries and delete matching ones. |
| 434 | |
| 435 | // TODO: Non-local, non-internal or content matching queries are not supported at the moment. |
| 436 | return 0, storage.ErrNotImplemented |
| 437 | } |
| 438 | |
| 439 | // PurgeOlderThan deletes all records last updated before the given time. It returns the number of successful deletes and an error. |
| 440 | func (db *SQLite) PurgeOlderThan(ctx context.Context, prefix string, purgeBefore time.Time, local, internal, shadowDelete bool) (int, error) { |
nothing calls this directly
no test coverage detected