// Code generated by sqlc. DO NOT EDIT.
// versions:
//   sqlc v1.23.0
// source: query.sql

package querytest

import (
	"context"

	"github.com/jackc/pgx/v5/pgtype"
)

const getDictTree = `-- name: GetDictTree :many
with recursive dictTree(id, code, parent_code, label, value, path, depth) AS (
	select id, code, parent_code, label, value, ARRAY[COALESCE((select id from dict where code=''),'virtual_root'), id], 1 as depth from dict where app_id = '1' and parent_code = '' and is_delete=false
	union
		select d.id, d.code, d.parent_code, d.label, d.value, t.path || ARRAY[d.id], t.depth+1 as depth from dict d join dictTree t on d.parent_code = t.code and not d.id = ANY(t.path) and d.is_delete=false
)
select id, code, parent_code, label, value, path, depth from dictTree d order by depth, parent_code
`

type GetDictTreeRow struct {
	ID         string
	Code       pgtype.Text
	ParentCode string
	Label      string
	Value      pgtype.Text
	Path       []string
	Depth      int32
}

func (q *Queries) GetDictTree(ctx context.Context) ([]GetDictTreeRow, error) {
	rows, err := q.db.Query(ctx, getDictTree)
	if err != nil {
		return nil, err
	}
	defer rows.Close()
	var items []GetDictTreeRow
	for rows.Next() {
		var i GetDictTreeRow
		if err := rows.Scan(
			&i.ID,
			&i.Code,
			&i.ParentCode,
			&i.Label,
			&i.Value,
			&i.Path,
			&i.Depth,
		); err != nil {
			return nil, err
		}
		items = append(items, i)
	}
	if err := rows.Err(); err != nil {
		return nil, err
	}
	return items, nil
}
