//go:build linux && cgo && !agent

// Code generated by generate-database from the incus project - DO NOT EDIT.

package cluster

import (
	"context"
	"database/sql"
	"errors"
	"fmt"
	"strings"
)

var networkNodeObjects = RegisterStmt(`
SELECT networks_nodes.id, networks_nodes.network_id, networks_nodes.node_id, nodes.name AS name, networks_nodes.state
  FROM networks_nodes
  JOIN nodes ON networks_nodes.node_id = nodes.id
  ORDER BY networks_nodes.id
`)

// networkNodeColumns returns a string of column names to be used with a SELECT statement for the entity.
// Use this function when building statements to retrieve database entries matching the NetworkNode entity.
func networkNodeColumns() string {
	return "networks_nodes.id, networks_nodes.network_id, networks_nodes.node_id, nodes.name AS name, networks_nodes.state"
}

// getNetworkNodes can be used to run handwritten sql.Stmts to return a slice of objects.
func getNetworkNodes(ctx context.Context, stmt *sql.Stmt, args ...any) ([]NetworkNode, error) {
	objects := make([]NetworkNode, 0)

	dest := func(scan func(dest ...any) error) error {
		n := NetworkNode{}
		err := scan(&n.ID, &n.NetworkID, &n.NodeID, &n.Name, &n.State)
		if err != nil {
			return err
		}

		objects = append(objects, n)

		return nil
	}

	err := selectObjects(ctx, stmt, dest, args...)
	if err != nil {
		return nil, fmt.Errorf("Failed to fetch from \"networks_nodes\" table: %w", err)
	}

	return objects, nil
}

// getNetworkNodesRaw can be used to run handwritten query strings to return a slice of objects.
func getNetworkNodesRaw(ctx context.Context, db dbtx, sql string, args ...any) ([]NetworkNode, error) {
	objects := make([]NetworkNode, 0)

	dest := func(scan func(dest ...any) error) error {
		n := NetworkNode{}
		err := scan(&n.ID, &n.NetworkID, &n.NodeID, &n.Name, &n.State)
		if err != nil {
			return err
		}

		objects = append(objects, n)

		return nil
	}

	err := scan(ctx, db, sql, dest, args...)
	if err != nil {
		return nil, fmt.Errorf("Failed to fetch from \"networks_nodes\" table: %w", err)
	}

	return objects, nil
}

// GetNetworkNodes returns all available network_nodes.
// generator: network_node GetMany
func GetNetworkNodes(ctx context.Context, db dbtx, filters ...NetworkNodeFilter) (_ []NetworkNode, _err error) {
	defer func() {
		_err = mapErr(_err, "Network_node")
	}()

	var err error

	// Result slice.
	objects := make([]NetworkNode, 0)

	// Pick the prepared statement and arguments to use based on active criteria.
	var sqlStmt *sql.Stmt
	args := []any{}
	queryParts := [2]string{}

	if len(filters) == 0 {
		sqlStmt, err = Stmt(db, networkNodeObjects)
		if err != nil {
			return nil, fmt.Errorf("Failed to get \"networkNodeObjects\" prepared statement: %w", err)
		}
	}

	for _, filter := range filters {
		if filter.NetworkID == nil && filter.NodeID == nil {
			return nil, fmt.Errorf("Cannot filter on empty NetworkNodeFilter")
		} else {
			return nil, errors.New("No statement exists for the given Filter")
		}
	}

	// Select.
	if sqlStmt != nil {
		objects, err = getNetworkNodes(ctx, sqlStmt, args...)
	} else {
		queryStr := strings.Join(queryParts[:], "ORDER BY")
		objects, err = getNetworkNodesRaw(ctx, db, queryStr, args...)
	}

	if err != nil {
		return nil, fmt.Errorf("Failed to fetch from \"networks_nodes\" table: %w", err)
	}

	return objects, nil
}
