|
| 1 | +package sqlc |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "database/sql" |
| 6 | + "strconv" |
| 7 | + "strings" |
| 8 | +) |
| 9 | + |
| 10 | +// ActionQueryParams defines the parameters for querying actions. |
| 11 | +type ActionQueryParams struct { |
| 12 | + SessionID sql.NullInt64 |
| 13 | + AccountID sql.NullInt64 |
| 14 | + FeatureName sql.NullString |
| 15 | + ActorName sql.NullString |
| 16 | + RpcMethod sql.NullString |
| 17 | + State sql.NullInt16 |
| 18 | + EndTime sql.NullTime |
| 19 | + StartTime sql.NullTime |
| 20 | + GroupID sql.NullInt64 |
| 21 | +} |
| 22 | + |
| 23 | +// ListActionsParams defines the parameters for listing actions, including |
| 24 | +// the ActionQueryParams for filtering and a Pagination struct for |
| 25 | +// pagination. The Reversed field indicates whether the results should be |
| 26 | +// returned in reverse order based on the created_at timestamp. |
| 27 | +type ListActionsParams struct { |
| 28 | + ActionQueryParams |
| 29 | + Reversed bool |
| 30 | + *Pagination |
| 31 | +} |
| 32 | + |
| 33 | +// Pagination defines the pagination parameters for listing actions. |
| 34 | +type Pagination struct { |
| 35 | + NumOffset int32 |
| 36 | + NumLimit int32 |
| 37 | +} |
| 38 | + |
| 39 | +// ListActions retrieves a list of actions based on the provided |
| 40 | +// ListActionsParams. |
| 41 | +func (q *Queries) ListActions(ctx context.Context, |
| 42 | + arg ListActionsParams) ([]Action, error) { |
| 43 | + |
| 44 | + query, args := buildListActionsQuery(arg) |
| 45 | + rows, err := q.db.QueryContext(ctx, fillPlaceHolders(query), args...) |
| 46 | + if err != nil { |
| 47 | + return nil, err |
| 48 | + } |
| 49 | + defer rows.Close() |
| 50 | + var items []Action |
| 51 | + for rows.Next() { |
| 52 | + var i Action |
| 53 | + if err := rows.Scan( |
| 54 | + &i.ID, |
| 55 | + &i.SessionID, |
| 56 | + &i.AccountID, |
| 57 | + &i.MacaroonIdentifier, |
| 58 | + &i.ActorName, |
| 59 | + &i.FeatureName, |
| 60 | + &i.ActionTrigger, |
| 61 | + &i.Intent, |
| 62 | + &i.StructuredJsonData, |
| 63 | + &i.RpcMethod, |
| 64 | + &i.RpcParamsJson, |
| 65 | + &i.CreatedAt, |
| 66 | + &i.ActionState, |
| 67 | + &i.ErrorReason, |
| 68 | + ); err != nil { |
| 69 | + return nil, err |
| 70 | + } |
| 71 | + items = append(items, i) |
| 72 | + } |
| 73 | + if err := rows.Close(); err != nil { |
| 74 | + return nil, err |
| 75 | + } |
| 76 | + if err := rows.Err(); err != nil { |
| 77 | + return nil, err |
| 78 | + } |
| 79 | + return items, nil |
| 80 | +} |
| 81 | + |
| 82 | +// CountActions returns the number of actions that match the provided |
| 83 | +// ActionQueryParams. |
| 84 | +func (q *Queries) CountActions(ctx context.Context, |
| 85 | + arg ActionQueryParams) (int64, error) { |
| 86 | + |
| 87 | + query, args := buildActionsQuery(arg, true) |
| 88 | + row := q.db.QueryRowContext(ctx, query, args...) |
| 89 | + |
| 90 | + var count int64 |
| 91 | + err := row.Scan(&count) |
| 92 | + |
| 93 | + return count, err |
| 94 | +} |
| 95 | + |
| 96 | +// buildActionsQuery constructs a SQL query to retrieve actions based on the |
| 97 | +// provided parameters. We do this manually so that if, for example, we have |
| 98 | +// a sessionID we are filtering by, then this appears in the query as: |
| 99 | +// `WHERE a.session_id = ?` which will properly make use of the underlying |
| 100 | +// index. If we were instead to use a single SQLC query, it would include many |
| 101 | +// WHERE clauses like: |
| 102 | +// "WHERE a.session_id = COALESCE(sqlc.narg('session_id'), a.session_id)". |
| 103 | +// This would use the index if run against postres but not when run against |
| 104 | +// sqlite. |
| 105 | +// |
| 106 | +// The 'count' param indicates whether the query should return a count of |
| 107 | +// actions that match the criteria or the actions themselves. |
| 108 | +func buildActionsQuery(params ActionQueryParams, count bool) (string, []any) { |
| 109 | + var ( |
| 110 | + conditions []string |
| 111 | + args []any |
| 112 | + ) |
| 113 | + |
| 114 | + if params.SessionID.Valid { |
| 115 | + conditions = append(conditions, "a.session_id = ?") |
| 116 | + args = append(args, params.SessionID.Int64) |
| 117 | + } |
| 118 | + if params.AccountID.Valid { |
| 119 | + conditions = append(conditions, "a.account_id = ?") |
| 120 | + args = append(args, params.AccountID.Int64) |
| 121 | + } |
| 122 | + if params.FeatureName.Valid { |
| 123 | + conditions = append(conditions, "a.feature_name = ?") |
| 124 | + args = append(args, params.FeatureName.String) |
| 125 | + } |
| 126 | + if params.ActorName.Valid { |
| 127 | + conditions = append(conditions, "a.actor_name = ?") |
| 128 | + args = append(args, params.ActorName.String) |
| 129 | + } |
| 130 | + if params.RpcMethod.Valid { |
| 131 | + conditions = append(conditions, "a.rpc_method = ?") |
| 132 | + args = append(args, params.RpcMethod.String) |
| 133 | + } |
| 134 | + if params.State.Valid { |
| 135 | + conditions = append(conditions, "a.action_state = ?") |
| 136 | + args = append(args, params.State.Int16) |
| 137 | + } |
| 138 | + if params.EndTime.Valid { |
| 139 | + conditions = append(conditions, "a.created_at <= ?") |
| 140 | + args = append(args, params.EndTime.Time) |
| 141 | + } |
| 142 | + if params.StartTime.Valid { |
| 143 | + conditions = append(conditions, "a.created_at >= ?") |
| 144 | + args = append(args, params.StartTime.Time) |
| 145 | + } |
| 146 | + if params.GroupID.Valid { |
| 147 | + conditions = append(conditions, ` |
| 148 | + EXISTS ( |
| 149 | + SELECT 1 |
| 150 | + FROM sessions s |
| 151 | + WHERE s.id = a.session_id AND s.group_id = ? |
| 152 | + )`) |
| 153 | + args = append(args, params.GroupID.Int64) |
| 154 | + } |
| 155 | + |
| 156 | + query := "SELECT a.* FROM actions a" |
| 157 | + if count { |
| 158 | + query = "SELECT COUNT(*) FROM actions a" |
| 159 | + } |
| 160 | + if len(conditions) > 0 { |
| 161 | + query += " WHERE " + strings.Join(conditions, " AND ") |
| 162 | + } |
| 163 | + |
| 164 | + return query, args |
| 165 | +} |
| 166 | + |
| 167 | +// buildListActionsQuery constructs a SQL query to retrieve a list of actions |
| 168 | +// based on the provided parameters. It builds upon the `buildActionsQuery` |
| 169 | +// function, adding pagination and ordering based on the reversed parameter. |
| 170 | +func buildListActionsQuery(params ListActionsParams) (string, []interface{}) { |
| 171 | + query, args := buildActionsQuery(params.ActionQueryParams, false) |
| 172 | + |
| 173 | + // Determine order direction. |
| 174 | + order := "ASC" |
| 175 | + if params.Reversed { |
| 176 | + order = "DESC" |
| 177 | + } |
| 178 | + query += " ORDER BY a.created_at " + order |
| 179 | + |
| 180 | + // Maybe paginate. |
| 181 | + if params.Pagination != nil { |
| 182 | + query += " LIMIT ? OFFSET ?" |
| 183 | + args = append(args, params.NumLimit, params.NumOffset) |
| 184 | + } |
| 185 | + |
| 186 | + return query, args |
| 187 | +} |
| 188 | + |
| 189 | +// fillPlaceHolders replaces all '?' placeholders in the SQL query with |
| 190 | +// positional placeholders like $1, $2, etc. This is necessary for |
| 191 | +// compatibility with Postgres. |
| 192 | +func fillPlaceHolders(query string) string { |
| 193 | + var ( |
| 194 | + sb strings.Builder |
| 195 | + argNum = 1 |
| 196 | + ) |
| 197 | + |
| 198 | + for i := range len(query) { |
| 199 | + if query[i] != '?' { |
| 200 | + sb.WriteByte(query[i]) |
| 201 | + continue |
| 202 | + } |
| 203 | + |
| 204 | + sb.WriteString("$") |
| 205 | + sb.WriteString(strconv.Itoa(argNum)) |
| 206 | + argNum++ |
| 207 | + } |
| 208 | + |
| 209 | + return sb.String() |
| 210 | +} |
0 commit comments