package database

// The code below was generated by lxd-generate - DO NOT EDIT!

import (
	"context"
	"database/sql"
	"errors"
	"fmt"
	"net/http"
	"strings"

	"github.com/canonical/lxd/lxd/db/query"
	"github.com/canonical/lxd/shared/api"
	"github.com/canonical/microcluster/v2/cluster"
)

var _ = api.ServerEnvironment{}

var clusterManagerObjects = cluster.RegisterStmt(`
SELECT cluster_manager.id, cluster_manager.addresses, cluster_manager.certificate_fingerprint, cluster_manager.name, cluster_manager.status_last_success_time, cluster_manager.status_last_error_time, cluster_manager.status_last_error_response
  FROM cluster_manager
  ORDER BY cluster_manager.id
`)

var clusterManagerObjectsByID = cluster.RegisterStmt(`
SELECT cluster_manager.id, cluster_manager.addresses, cluster_manager.certificate_fingerprint, cluster_manager.name, cluster_manager.status_last_success_time, cluster_manager.status_last_error_time, cluster_manager.status_last_error_response
  FROM cluster_manager
  WHERE ( cluster_manager.id = ? )
  ORDER BY cluster_manager.id
`)

var clusterManagerObjectsByName = cluster.RegisterStmt(`
SELECT cluster_manager.id, cluster_manager.addresses, cluster_manager.certificate_fingerprint, cluster_manager.name, cluster_manager.status_last_success_time, cluster_manager.status_last_error_time, cluster_manager.status_last_error_response
  FROM cluster_manager
  WHERE ( cluster_manager.name = ? )
  ORDER BY cluster_manager.id
`)

var clusterManagerID = cluster.RegisterStmt(`
SELECT cluster_manager.id FROM cluster_manager
  WHERE cluster_manager.id = ?
`)

var clusterManagerDeleteByID = cluster.RegisterStmt(`
DELETE FROM cluster_manager WHERE id = ?
`)

var clusterManagerCreate = cluster.RegisterStmt(`
INSERT INTO cluster_manager (addresses, certificate_fingerprint, name, status_last_success_time, status_last_error_time, status_last_error_response)
  VALUES (?, ?, ?, ?, ?, ?)
`)

var clusterManagerUpdate = cluster.RegisterStmt(`
UPDATE cluster_manager
  SET addresses = ?, certificate_fingerprint = ?, name = ?, status_last_success_time = ?, status_last_error_time = ?, status_last_error_response = ?
 WHERE id = ?
`)

// clusterManagerColumns 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 ClusterManager entity.
func clusterManagerColumns() string {
	return "cluster_manager.id, cluster_manager.addresses, cluster_manager.certificate_fingerprint, cluster_manager.name, cluster_manager.status_last_success_time, cluster_manager.status_last_error_time, cluster_manager.status_last_error_response"
}

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

	dest := func(scan func(dest ...any) error) error {
		c := ClusterManager{}
		err := scan(&c.ID, &c.Addresses, &c.CertificateFingerprint, &c.Name, &c.StatusLastSuccessTime, &c.StatusLastErrorTime, &c.StatusLastErrorResponse)
		if err != nil {
			return err
		}

		objects = append(objects, c)

		return nil
	}

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

	return objects, nil
}

// getClusterManagersRaw can be used to run handwritten query strings to return a slice of objects.
func getClusterManagersRaw(ctx context.Context, tx *sql.Tx, sql string, args ...any) ([]ClusterManager, error) {
	objects := make([]ClusterManager, 0)

	dest := func(scan func(dest ...any) error) error {
		c := ClusterManager{}
		err := scan(&c.ID, &c.Addresses, &c.CertificateFingerprint, &c.Name, &c.StatusLastSuccessTime, &c.StatusLastErrorTime, &c.StatusLastErrorResponse)
		if err != nil {
			return err
		}

		objects = append(objects, c)

		return nil
	}

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

	return objects, nil
}

// GetClusterManagers returns all available ClusterManagers.
// generator: ClusterManager GetMany
func GetClusterManagers(ctx context.Context, tx *sql.Tx, filters ...ClusterManagerFilter) ([]ClusterManager, error) {
	var err error

	// Result slice.
	objects := make([]ClusterManager, 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 = cluster.Stmt(tx, clusterManagerObjects)
		if err != nil {
			return nil, fmt.Errorf("Failed to get \"clusterManagerObjects\" prepared statement: %w", err)
		}
	}

	for i, filter := range filters {
		if filter.Name != nil && filter.ID == nil {
			args = append(args, []any{filter.Name}...)
			if len(filters) == 1 {
				sqlStmt, err = cluster.Stmt(tx, clusterManagerObjectsByName)
				if err != nil {
					return nil, fmt.Errorf("Failed to get \"clusterManagerObjectsByName\" prepared statement: %w", err)
				}

				break
			}

			query, err := cluster.StmtString(clusterManagerObjectsByName)
			if err != nil {
				return nil, fmt.Errorf("Failed to get \"clusterManagerObjects\" prepared statement: %w", err)
			}

			parts := strings.SplitN(query, "ORDER BY", 2)
			if i == 0 {
				copy(queryParts[:], parts)
				continue
			}

			_, where, _ := strings.Cut(parts[0], "WHERE")
			queryParts[0] += "OR" + where
		} else if filter.ID != nil && filter.Name == nil {
			args = append(args, []any{filter.ID}...)
			if len(filters) == 1 {
				sqlStmt, err = cluster.Stmt(tx, clusterManagerObjectsByID)
				if err != nil {
					return nil, fmt.Errorf("Failed to get \"clusterManagerObjectsByID\" prepared statement: %w", err)
				}

				break
			}

			query, err := cluster.StmtString(clusterManagerObjectsByID)
			if err != nil {
				return nil, fmt.Errorf("Failed to get \"clusterManagerObjects\" prepared statement: %w", err)
			}

			parts := strings.SplitN(query, "ORDER BY", 2)
			if i == 0 {
				copy(queryParts[:], parts)
				continue
			}

			_, where, _ := strings.Cut(parts[0], "WHERE")
			queryParts[0] += "OR" + where
		} else if filter.ID == nil && filter.Name == nil {
			return nil, fmt.Errorf("Cannot filter on empty ClusterManagerFilter")
		} else {
			return nil, fmt.Errorf("No statement exists for the given Filter")
		}
	}

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

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

	return objects, nil
}

// GetClusterManager returns the ClusterManager with the given key.
// generator: ClusterManager GetOne
func GetClusterManager(ctx context.Context, tx *sql.Tx, id int64) (*ClusterManager, error) {
	filter := ClusterManagerFilter{}
	filter.ID = &id

	objects, err := GetClusterManagers(ctx, tx, filter)
	if err != nil {
		return nil, fmt.Errorf("Failed to fetch from \"cluster_manager\" table: %w", err)
	}

	switch len(objects) {
	case 0:
		return nil, api.StatusErrorf(http.StatusNotFound, "ClusterManager not found")
	case 1:
		return &objects[0], nil
	default:
		return nil, fmt.Errorf("More than one \"cluster_manager\" entry matches")
	}
}

// GetClusterManagerID return the ID of the ClusterManager with the given key.
// generator: ClusterManager ID
func GetClusterManagerID(ctx context.Context, tx *sql.Tx, id int64) (int64, error) {
	stmt, err := cluster.Stmt(tx, clusterManagerID)
	if err != nil {
		return -1, fmt.Errorf("Failed to get \"clusterManagerID\" prepared statement: %w", err)
	}

	row := stmt.QueryRowContext(ctx, id)
	err = row.Scan(&id)
	if errors.Is(err, sql.ErrNoRows) {
		return -1, api.StatusErrorf(http.StatusNotFound, "ClusterManager not found")
	}

	if err != nil {
		return -1, fmt.Errorf("Failed to get \"cluster_manager\" ID: %w", err)
	}

	return id, nil
}

// ClusterManagerExists checks if a ClusterManager with the given key exists.
// generator: ClusterManager Exists
func ClusterManagerExists(ctx context.Context, tx *sql.Tx, id int64) (bool, error) {
	_, err := GetClusterManagerID(ctx, tx, id)
	if err != nil {
		if api.StatusErrorCheck(err, http.StatusNotFound) {
			return false, nil
		}

		return false, err
	}

	return true, nil
}

// CreateClusterManager adds a new ClusterManager to the database.
// generator: ClusterManager Create
func CreateClusterManager(ctx context.Context, tx *sql.Tx, object ClusterManager) (int64, error) {
	// Check if a ClusterManager with the same key exists.
	exists, err := ClusterManagerExists(ctx, tx, object.ID)
	if err != nil {
		return -1, fmt.Errorf("Failed to check for duplicates: %w", err)
	}

	if exists {
		return -1, api.StatusErrorf(http.StatusConflict, "This \"cluster_manager\" entry already exists")
	}

	args := make([]any, 6)

	// Populate the statement arguments.
	args[0] = object.Addresses
	args[1] = object.CertificateFingerprint
	args[2] = object.Name
	args[3] = object.StatusLastSuccessTime
	args[4] = object.StatusLastErrorTime
	args[5] = object.StatusLastErrorResponse

	// Prepared statement to use.
	stmt, err := cluster.Stmt(tx, clusterManagerCreate)
	if err != nil {
		return -1, fmt.Errorf("Failed to get \"clusterManagerCreate\" prepared statement: %w", err)
	}

	// Execute the statement.
	result, err := stmt.Exec(args...)
	if err != nil {
		return -1, fmt.Errorf("Failed to create \"cluster_manager\" entry: %w", err)
	}

	id, err := result.LastInsertId()
	if err != nil {
		return -1, fmt.Errorf("Failed to fetch \"cluster_manager\" entry ID: %w", err)
	}

	return id, nil
}

// UpdateClusterManager updates the ClusterManager matching the given key parameters.
// generator: ClusterManager Update
func UpdateClusterManager(ctx context.Context, tx *sql.Tx, id int64, object ClusterManager) error {
	id, err := GetClusterManagerID(ctx, tx, id)
	if err != nil {
		return err
	}

	stmt, err := cluster.Stmt(tx, clusterManagerUpdate)
	if err != nil {
		return fmt.Errorf("Failed to get \"clusterManagerUpdate\" prepared statement: %w", err)
	}

	result, err := stmt.Exec(object.Addresses, object.CertificateFingerprint, object.Name, object.StatusLastSuccessTime, object.StatusLastErrorTime, object.StatusLastErrorResponse, id)
	if err != nil {
		return fmt.Errorf("Update \"cluster_manager\" entry failed: %w", err)
	}

	n, err := result.RowsAffected()
	if err != nil {
		return fmt.Errorf("Fetch affected rows: %w", err)
	}

	if n != 1 {
		return fmt.Errorf("Query updated %d rows instead of 1", n)
	}

	return nil
}

// DeleteClusterManager deletes the ClusterManager matching the given key parameters.
// generator: ClusterManager DeleteOne-by-ID
func DeleteClusterManager(ctx context.Context, tx *sql.Tx, id int64) error {
	stmt, err := cluster.Stmt(tx, clusterManagerDeleteByID)
	if err != nil {
		return fmt.Errorf("Failed to get \"clusterManagerDeleteByID\" prepared statement: %w", err)
	}

	result, err := stmt.Exec(id)
	if err != nil {
		return fmt.Errorf("Delete \"cluster_manager\": %w", err)
	}

	n, err := result.RowsAffected()
	if err != nil {
		return fmt.Errorf("Fetch affected rows: %w", err)
	}

	if n == 0 {
		return api.StatusErrorf(http.StatusNotFound, "ClusterManager not found")
	} else if n > 1 {
		return fmt.Errorf("Query deleted %d ClusterManager rows instead of 1", n)
	}

	return nil
}

var clusterManagerConfigObjects = cluster.RegisterStmt(`
SELECT cluster_manager_config.id, cluster_manager_config.cluster_manager_id, cluster_manager_config.key, cluster_manager_config.value
  FROM cluster_manager_config
  ORDER BY cluster_manager_config.id
`)

var clusterManagerConfigObjectsByID = cluster.RegisterStmt(`
SELECT cluster_manager_config.id, cluster_manager_config.cluster_manager_id, cluster_manager_config.key, cluster_manager_config.value
  FROM cluster_manager_config
  WHERE ( cluster_manager_config.id = ? )
  ORDER BY cluster_manager_config.id
`)

var clusterManagerConfigObjectsByClusterManagerID = cluster.RegisterStmt(`
SELECT cluster_manager_config.id, cluster_manager_config.cluster_manager_id, cluster_manager_config.key, cluster_manager_config.value
  FROM cluster_manager_config
  WHERE ( cluster_manager_config.cluster_manager_id = ? )
  ORDER BY cluster_manager_config.id
`)

var clusterManagerConfigObjectsByClusterManagerIDAndKey = cluster.RegisterStmt(`
SELECT cluster_manager_config.id, cluster_manager_config.cluster_manager_id, cluster_manager_config.key, cluster_manager_config.value
  FROM cluster_manager_config
  WHERE ( cluster_manager_config.cluster_manager_id = ? AND cluster_manager_config.key = ? )
  ORDER BY cluster_manager_config.id
`)

var clusterManagerConfigID = cluster.RegisterStmt(`
SELECT cluster_manager_config.id FROM cluster_manager_config
  WHERE cluster_manager_config.id = ?
`)

var clusterManagerConfigDeleteByID = cluster.RegisterStmt(`
DELETE FROM cluster_manager_config WHERE id = ?
`)

var clusterManagerConfigCreate = cluster.RegisterStmt(`
INSERT INTO cluster_manager_config (cluster_manager_id, key, value)
  VALUES (?, ?, ?)
`)

var clusterManagerConfigUpdate = cluster.RegisterStmt(`
UPDATE cluster_manager_config
  SET cluster_manager_id = ?, key = ?, value = ?
 WHERE id = ?
`)

// clusterManagerConfigColumns 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 ClusterManagerConfig entity.
func clusterManagerConfigColumns() string {
	return "cluster_manager_config.id, cluster_manager_config.cluster_manager_id, cluster_manager_config.key, cluster_manager_config.value"
}

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

	dest := func(scan func(dest ...any) error) error {
		c := ClusterManagerConfig{}
		err := scan(&c.ID, &c.ClusterManagerID, &c.Key, &c.Value)
		if err != nil {
			return err
		}

		objects = append(objects, c)

		return nil
	}

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

	return objects, nil
}

// getClusterManagerConfigRaw can be used to run handwritten query strings to return a slice of objects.
func getClusterManagerConfigRaw(ctx context.Context, tx *sql.Tx, sql string, args ...any) ([]ClusterManagerConfig, error) {
	objects := make([]ClusterManagerConfig, 0)

	dest := func(scan func(dest ...any) error) error {
		c := ClusterManagerConfig{}
		err := scan(&c.ID, &c.ClusterManagerID, &c.Key, &c.Value)
		if err != nil {
			return err
		}

		objects = append(objects, c)

		return nil
	}

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

	return objects, nil
}

// GetClusterManagerConfig returns all available ClusterManagerConfig.
// generator: ClusterManagerConfig GetMany
func GetClusterManagerConfig(ctx context.Context, tx *sql.Tx, filters ...ClusterManagerConfigFilter) ([]ClusterManagerConfig, error) {
	var err error

	// Result slice.
	objects := make([]ClusterManagerConfig, 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 = cluster.Stmt(tx, clusterManagerConfigObjects)
		if err != nil {
			return nil, fmt.Errorf("Failed to get \"clusterManagerConfigObjects\" prepared statement: %w", err)
		}
	}

	for i, filter := range filters {
		if filter.ClusterManagerID != nil && filter.Key != nil && filter.ID == nil {
			args = append(args, []any{filter.ClusterManagerID, filter.Key}...)
			if len(filters) == 1 {
				sqlStmt, err = cluster.Stmt(tx, clusterManagerConfigObjectsByClusterManagerIDAndKey)
				if err != nil {
					return nil, fmt.Errorf("Failed to get \"clusterManagerConfigObjectsByClusterManagerIDAndKey\" prepared statement: %w", err)
				}

				break
			}

			query, err := cluster.StmtString(clusterManagerConfigObjectsByClusterManagerIDAndKey)
			if err != nil {
				return nil, fmt.Errorf("Failed to get \"clusterManagerConfigObjects\" prepared statement: %w", err)
			}

			parts := strings.SplitN(query, "ORDER BY", 2)
			if i == 0 {
				copy(queryParts[:], parts)
				continue
			}

			_, where, _ := strings.Cut(parts[0], "WHERE")
			queryParts[0] += "OR" + where
		} else if filter.ID != nil && filter.ClusterManagerID == nil && filter.Key == nil {
			args = append(args, []any{filter.ID}...)
			if len(filters) == 1 {
				sqlStmt, err = cluster.Stmt(tx, clusterManagerConfigObjectsByID)
				if err != nil {
					return nil, fmt.Errorf("Failed to get \"clusterManagerConfigObjectsByID\" prepared statement: %w", err)
				}

				break
			}

			query, err := cluster.StmtString(clusterManagerConfigObjectsByID)
			if err != nil {
				return nil, fmt.Errorf("Failed to get \"clusterManagerConfigObjects\" prepared statement: %w", err)
			}

			parts := strings.SplitN(query, "ORDER BY", 2)
			if i == 0 {
				copy(queryParts[:], parts)
				continue
			}

			_, where, _ := strings.Cut(parts[0], "WHERE")
			queryParts[0] += "OR" + where
		} else if filter.ClusterManagerID != nil && filter.ID == nil && filter.Key == nil {
			args = append(args, []any{filter.ClusterManagerID}...)
			if len(filters) == 1 {
				sqlStmt, err = cluster.Stmt(tx, clusterManagerConfigObjectsByClusterManagerID)
				if err != nil {
					return nil, fmt.Errorf("Failed to get \"clusterManagerConfigObjectsByClusterManagerID\" prepared statement: %w", err)
				}

				break
			}

			query, err := cluster.StmtString(clusterManagerConfigObjectsByClusterManagerID)
			if err != nil {
				return nil, fmt.Errorf("Failed to get \"clusterManagerConfigObjects\" prepared statement: %w", err)
			}

			parts := strings.SplitN(query, "ORDER BY", 2)
			if i == 0 {
				copy(queryParts[:], parts)
				continue
			}

			_, where, _ := strings.Cut(parts[0], "WHERE")
			queryParts[0] += "OR" + where
		} else if filter.ID == nil && filter.ClusterManagerID == nil && filter.Key == nil {
			return nil, fmt.Errorf("Cannot filter on empty ClusterManagerConfigFilter")
		} else {
			return nil, fmt.Errorf("No statement exists for the given Filter")
		}
	}

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

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

	return objects, nil
}

// GetClusterManagerConfigID return the ID of the ClusterManagerConfig with the given key.
// generator: ClusterManagerConfig ID
func GetClusterManagerConfigID(ctx context.Context, tx *sql.Tx, id int64) (int64, error) {
	stmt, err := cluster.Stmt(tx, clusterManagerConfigID)
	if err != nil {
		return -1, fmt.Errorf("Failed to get \"clusterManagerConfigID\" prepared statement: %w", err)
	}

	row := stmt.QueryRowContext(ctx, id)
	err = row.Scan(&id)
	if errors.Is(err, sql.ErrNoRows) {
		return -1, api.StatusErrorf(http.StatusNotFound, "ClusterManagerConfig not found")
	}

	if err != nil {
		return -1, fmt.Errorf("Failed to get \"cluster_manager_config\" ID: %w", err)
	}

	return id, nil
}

// ClusterManagerConfigExists checks if a ClusterManagerConfig with the given key exists.
// generator: ClusterManagerConfig Exists
func ClusterManagerConfigExists(ctx context.Context, tx *sql.Tx, id int64) (bool, error) {
	_, err := GetClusterManagerConfigID(ctx, tx, id)
	if err != nil {
		if api.StatusErrorCheck(err, http.StatusNotFound) {
			return false, nil
		}

		return false, err
	}

	return true, nil
}

// CreateClusterManagerConfig adds a new ClusterManagerConfig to the database.
// generator: ClusterManagerConfig Create
func CreateClusterManagerConfig(ctx context.Context, tx *sql.Tx, object ClusterManagerConfig) (int64, error) {
	// Check if a ClusterManagerConfig with the same key exists.
	exists, err := ClusterManagerConfigExists(ctx, tx, object.ID)
	if err != nil {
		return -1, fmt.Errorf("Failed to check for duplicates: %w", err)
	}

	if exists {
		return -1, api.StatusErrorf(http.StatusConflict, "This \"cluster_manager_config\" entry already exists")
	}

	args := make([]any, 3)

	// Populate the statement arguments.
	args[0] = object.ClusterManagerID
	args[1] = object.Key
	args[2] = object.Value

	// Prepared statement to use.
	stmt, err := cluster.Stmt(tx, clusterManagerConfigCreate)
	if err != nil {
		return -1, fmt.Errorf("Failed to get \"clusterManagerConfigCreate\" prepared statement: %w", err)
	}

	// Execute the statement.
	result, err := stmt.Exec(args...)
	if err != nil {
		return -1, fmt.Errorf("Failed to create \"cluster_manager_config\" entry: %w", err)
	}

	id, err := result.LastInsertId()
	if err != nil {
		return -1, fmt.Errorf("Failed to fetch \"cluster_manager_config\" entry ID: %w", err)
	}

	return id, nil
}

// UpdateClusterManagerConfig updates the ClusterManagerConfig matching the given key parameters.
// generator: ClusterManagerConfig Update
func UpdateClusterManagerConfig(ctx context.Context, tx *sql.Tx, id int64, object ClusterManagerConfig) error {
	id, err := GetClusterManagerConfigID(ctx, tx, id)
	if err != nil {
		return err
	}

	stmt, err := cluster.Stmt(tx, clusterManagerConfigUpdate)
	if err != nil {
		return fmt.Errorf("Failed to get \"clusterManagerConfigUpdate\" prepared statement: %w", err)
	}

	result, err := stmt.Exec(object.ClusterManagerID, object.Key, object.Value, id)
	if err != nil {
		return fmt.Errorf("Update \"cluster_manager_config\" entry failed: %w", err)
	}

	n, err := result.RowsAffected()
	if err != nil {
		return fmt.Errorf("Fetch affected rows: %w", err)
	}

	if n != 1 {
		return fmt.Errorf("Query updated %d rows instead of 1", n)
	}

	return nil
}

// DeleteClusterManagerConfig deletes the ClusterManagerConfig matching the given key parameters.
// generator: ClusterManagerConfig DeleteOne-by-ID
func DeleteClusterManagerConfig(ctx context.Context, tx *sql.Tx, id int64) error {
	stmt, err := cluster.Stmt(tx, clusterManagerConfigDeleteByID)
	if err != nil {
		return fmt.Errorf("Failed to get \"clusterManagerConfigDeleteByID\" prepared statement: %w", err)
	}

	result, err := stmt.Exec(id)
	if err != nil {
		return fmt.Errorf("Delete \"cluster_manager_config\": %w", err)
	}

	n, err := result.RowsAffected()
	if err != nil {
		return fmt.Errorf("Fetch affected rows: %w", err)
	}

	if n == 0 {
		return api.StatusErrorf(http.StatusNotFound, "ClusterManagerConfig not found")
	} else if n > 1 {
		return fmt.Errorf("Query deleted %d ClusterManagerConfig rows instead of 1", n)
	}

	return nil
}
