// Code generated by BobGen psql v0.42.5. DO NOT EDIT.
// This file is meant to be re-generated in place and/or deleted at any time.

package sql

import (
	"context"
	_ "embed"
	"io"
	"iter"

	"github.com/Gleipnir-Technology/bob"
	"github.com/Gleipnir-Technology/bob/dialect/psql"
	"github.com/Gleipnir-Technology/bob/dialect/psql/dialect"
	"github.com/Gleipnir-Technology/bob/orm"
	"github.com/lib/pq"
	"github.com/stephenafamo/scan"
)

//go:embed publicreport_publicid_table.bob.sql
var formattedQueries_publicreport_publicid_table string

var publicreportIDTableSQL = formattedQueries_publicreport_publicid_table[157:638]

type PublicreportIDTableQuery = orm.ModQuery[*dialect.SelectQuery, publicreportIDTable, PublicreportIDTableRow, []PublicreportIDTableRow, publicreportIDTableTransformer]

func PublicreportIDTable(PublicID string) *PublicreportIDTableQuery {
	var expressionTypArgs publicreportIDTable

	expressionTypArgs.PublicID = psql.Arg(PublicID)

	return &PublicreportIDTableQuery{
		Query: orm.Query[publicreportIDTable, PublicreportIDTableRow, []PublicreportIDTableRow, publicreportIDTableTransformer]{
			ExecQuery: orm.ExecQuery[publicreportIDTable]{
				BaseQuery: bob.BaseQuery[publicreportIDTable]{
					Expression: expressionTypArgs,
					Dialect:    dialect.Dialect,
					QueryType:  bob.QueryTypeSelect,
				},
			},
			Scanner: func(context.Context, []string) (func(*scan.Row) (any, error), func(any) (PublicreportIDTableRow, error)) {
				return func(row *scan.Row) (any, error) {
						var t PublicreportIDTableRow
						row.ScheduleScanByIndex(0, &t.ExistsSomewhere)
						row.ScheduleScanByIndex(1, &t.FoundInTables)
						row.ScheduleScanByIndex(2, &t.ReportIds)
						return &t, nil
					}, func(v any) (PublicreportIDTableRow, error) {
						return *(v.(*PublicreportIDTableRow)), nil
					}
			},
		},
		Mod: bob.ModFunc[*dialect.SelectQuery](func(q *dialect.SelectQuery) {
			q.AppendCTE(expressionTypArgs.subExpr(5, 325))
			q.AppendSelect(expressionTypArgs.subExpr(335, 463))
			q.SetTable(expressionTypArgs.subExpr(469, 481))
		}),
	}
}

type PublicreportIDTableRow = struct {
	ExistsSomewhere bool           `db:"exists_somewhere"`
	FoundInTables   pq.StringArray `db:"found_in_tables"`
	ReportIds       pq.StringArray `db:"report_ids"`
}

type publicreportIDTableTransformer = bob.SliceTransformer[PublicreportIDTableRow, []PublicreportIDTableRow]

type publicreportIDTable struct {
	PublicID bob.Expression
}

func (o publicreportIDTable) args() iter.Seq[orm.ArgWithPosition] {
	return func(yield func(arg orm.ArgWithPosition) bool) {
		if !yield(orm.ArgWithPosition{
			Name:       "publicID",
			Start:      112,
			Stop:       114,
			Expression: o.PublicID,
		}) {
			return
		}

		if !yield(orm.ArgWithPosition{
			Name:       "publicID",
			Start:      217,
			Stop:       219,
			Expression: o.PublicID,
		}) {
			return
		}

		if !yield(orm.ArgWithPosition{
			Name:       "publicID",
			Start:      321,
			Stop:       323,
			Expression: o.PublicID,
		}) {
			return
		}
	}
}

func (o publicreportIDTable) raw(from, to int) string {
	return publicreportIDTableSQL[from:to]
}

func (o publicreportIDTable) subExpr(from, to int) bob.Expression {
	return orm.ArgsToExpression(publicreportIDTableSQL, from, to, o.args())
}

func (o publicreportIDTable) WriteSQL(ctx context.Context, w io.StringWriter, d bob.Dialect, start int) ([]any, error) {
	return o.subExpr(0, len(publicreportIDTableSQL)).WriteSQL(ctx, w, d, start)
}
