// Code generated by sqlc. DO NOT EDIT.
// versions:
//   sqlc v1.29.0
// source: trace_seg_cleanup.sql

package lrdb

import (
	"context"
	"time"

	"github.com/google/uuid"
)

const traceSegmentCleanupDelete = `-- name: TraceSegmentCleanupDelete :exec
DELETE FROM trace_seg
WHERE organization_id = $1
  AND dateint = $2
  AND segment_id = $3
  AND instance_num = $4
`

type TraceSegmentCleanupDeleteParams struct {
	OrganizationID uuid.UUID `json:"organization_id"`
	Dateint        int32     `json:"dateint"`
	SegmentID      int64     `json:"segment_id"`
	InstanceNum    int16     `json:"instance_num"`
}

func (q *Queries) TraceSegmentCleanupDelete(ctx context.Context, arg TraceSegmentCleanupDeleteParams) error {
	_, err := q.db.Exec(ctx, traceSegmentCleanupDelete,
		arg.OrganizationID,
		arg.Dateint,
		arg.SegmentID,
		arg.InstanceNum,
	)
	return err
}

const traceSegmentCleanupGet = `-- name: TraceSegmentCleanupGet :many
SELECT
  organization_id,
  dateint,
  segment_id,
  instance_num,
  file_size,
  lower(ts_range)::bigint as ts_range_lower
FROM trace_seg
WHERE organization_id = $1
  AND dateint = $2
  AND published = false
  AND created_at < $3
LIMIT $4
`

type TraceSegmentCleanupGetParams struct {
	OrganizationID uuid.UUID `json:"organization_id"`
	Dateint        int32     `json:"dateint"`
	AgeThreshold   time.Time `json:"age_threshold"`
	MaxRows        int32     `json:"max_rows"`
}

type TraceSegmentCleanupGetRow struct {
	OrganizationID uuid.UUID `json:"organization_id"`
	Dateint        int32     `json:"dateint"`
	SegmentID      int64     `json:"segment_id"`
	InstanceNum    int16     `json:"instance_num"`
	FileSize       int64     `json:"file_size"`
	TsRangeLower   int64     `json:"ts_range_lower"`
}

func (q *Queries) TraceSegmentCleanupGet(ctx context.Context, arg TraceSegmentCleanupGetParams) ([]TraceSegmentCleanupGetRow, error) {
	rows, err := q.db.Query(ctx, traceSegmentCleanupGet,
		arg.OrganizationID,
		arg.Dateint,
		arg.AgeThreshold,
		arg.MaxRows,
	)
	if err != nil {
		return nil, err
	}
	defer rows.Close()
	var items []TraceSegmentCleanupGetRow
	for rows.Next() {
		var i TraceSegmentCleanupGetRow
		if err := rows.Scan(
			&i.OrganizationID,
			&i.Dateint,
			&i.SegmentID,
			&i.InstanceNum,
			&i.FileSize,
			&i.TsRangeLower,
		); err != nil {
			return nil, err
		}
		items = append(items, i)
	}
	if err := rows.Err(); err != nil {
		return nil, err
	}
	return items, nil
}
