|
| 1 | +package sqlite |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "database/sql" |
| 6 | + "time" |
| 7 | + |
| 8 | + "github.com/cschleiden/go-workflows/backend" |
| 9 | + "github.com/cschleiden/go-workflows/diag" |
| 10 | + "github.com/cschleiden/go-workflows/internal/core" |
| 11 | +) |
| 12 | + |
| 13 | +var _ diag.Backend = (*sqliteBackend)(nil) |
| 14 | + |
| 15 | +func (sb *sqliteBackend) GetWorkflowInstances(ctx context.Context, afterInstanceID string, count int) ([]*diag.WorkflowInstanceRef, error) { |
| 16 | + var err error |
| 17 | + tx, err := sb.db.BeginTx(ctx, nil) |
| 18 | + if err != nil { |
| 19 | + return nil, err |
| 20 | + } |
| 21 | + defer tx.Rollback() |
| 22 | + |
| 23 | + var rows *sql.Rows |
| 24 | + if afterInstanceID != "" { |
| 25 | + rows, err = tx.QueryContext( |
| 26 | + ctx, |
| 27 | + `SELECT i.id, i.execution_id, i.created_at, i.completed_at |
| 28 | + FROM instances i |
| 29 | + INNER JOIN (SELECT id, created_at FROM instances WHERE id = ?) ii |
| 30 | + ON i.created_at < ii.created_at OR (i.created_at = ii.created_at AND i.id < ii.id) |
| 31 | + ORDER BY i.created_at DESC, i.id DESC |
| 32 | + LIMIT ?`, |
| 33 | + afterInstanceID, |
| 34 | + count, |
| 35 | + ) |
| 36 | + } else { |
| 37 | + rows, err = tx.QueryContext( |
| 38 | + ctx, |
| 39 | + `SELECT i.id, i.execution_id, i.created_at, i.completed_at |
| 40 | + FROM instances i |
| 41 | + ORDER BY i.created_at DESC, i.id DESC |
| 42 | + LIMIT ?`, |
| 43 | + count, |
| 44 | + ) |
| 45 | + } |
| 46 | + if err != nil { |
| 47 | + return nil, err |
| 48 | + } |
| 49 | + |
| 50 | + var instances []*diag.WorkflowInstanceRef |
| 51 | + |
| 52 | + for rows.Next() { |
| 53 | + var id, executionID string |
| 54 | + var createdAt time.Time |
| 55 | + var completedAt *time.Time |
| 56 | + err = rows.Scan(&id, &executionID, &createdAt, &completedAt) |
| 57 | + if err != nil { |
| 58 | + return nil, err |
| 59 | + } |
| 60 | + |
| 61 | + var state backend.WorkflowState |
| 62 | + if completedAt != nil { |
| 63 | + state = backend.WorkflowStateFinished |
| 64 | + } |
| 65 | + |
| 66 | + instances = append(instances, &diag.WorkflowInstanceRef{ |
| 67 | + Instance: core.NewWorkflowInstance(id, executionID), |
| 68 | + CreatedAt: createdAt, |
| 69 | + CompletedAt: completedAt, |
| 70 | + State: state, |
| 71 | + }) |
| 72 | + } |
| 73 | + |
| 74 | + return instances, nil |
| 75 | +} |
| 76 | + |
| 77 | +func (sb *sqliteBackend) GetWorkflowInstance(ctx context.Context, instanceID string) (*diag.WorkflowInstanceRef, error) { |
| 78 | + tx, err := sb.db.BeginTx(ctx, nil) |
| 79 | + if err != nil { |
| 80 | + return nil, err |
| 81 | + } |
| 82 | + defer tx.Rollback() |
| 83 | + |
| 84 | + res := tx.QueryRowContext(ctx, "SELECT id, execution_id, created_at, completed_at FROM instances WHERE id = ?", instanceID) |
| 85 | + |
| 86 | + var id, executionID string |
| 87 | + var createdAt time.Time |
| 88 | + var completedAt *time.Time |
| 89 | + |
| 90 | + err = res.Scan(&id, &executionID, &createdAt, &completedAt) |
| 91 | + if err != nil { |
| 92 | + if err == sql.ErrNoRows { |
| 93 | + return nil, nil |
| 94 | + } |
| 95 | + |
| 96 | + return nil, err |
| 97 | + } |
| 98 | + |
| 99 | + var state backend.WorkflowState |
| 100 | + if completedAt != nil { |
| 101 | + state = backend.WorkflowStateFinished |
| 102 | + } |
| 103 | + |
| 104 | + return &diag.WorkflowInstanceRef{ |
| 105 | + Instance: core.NewWorkflowInstance(id, executionID), |
| 106 | + CreatedAt: createdAt, |
| 107 | + CompletedAt: completedAt, |
| 108 | + State: state, |
| 109 | + }, nil |
| 110 | +} |
0 commit comments