// Code generated by sqlc. DO NOT EDIT.
// versions:
//   sqlc v1.20.0
// source: environments.sql

package db

import (
	"context"
	"database/sql"
)

const createEnvironment = `-- name: CreateEnvironment :one
insert into environments (project_id, name, description)
values (?, ?, ?)
returning id, project_id, name, description
`

type CreateEnvironmentParams struct {
	ProjectID   int64
	Name        string
	Description sql.NullString
}

func (q *Queries) CreateEnvironment(ctx context.Context, arg CreateEnvironmentParams) (Environment, error) {
	row := q.db.QueryRowContext(ctx, createEnvironment, arg.ProjectID, arg.Name, arg.Description)
	var i Environment
	err := row.Scan(
		&i.ID,
		&i.ProjectID,
		&i.Name,
		&i.Description,
	)
	return i, err
}

const getEnvironmentById = `-- name: GetEnvironmentById :one
select id, project_id, name, description
from environments
where id = ?
`

func (q *Queries) GetEnvironmentById(ctx context.Context, id int64) (Environment, error) {
	row := q.db.QueryRowContext(ctx, getEnvironmentById, id)
	var i Environment
	err := row.Scan(
		&i.ID,
		&i.ProjectID,
		&i.Name,
		&i.Description,
	)
	return i, err
}

const getEnvironmentByName = `-- name: GetEnvironmentByName :one
select id, project_id, name, description
from environments
where name = ?
  and project_id = ?
`

type GetEnvironmentByNameParams struct {
	Name      string
	ProjectID int64
}

func (q *Queries) GetEnvironmentByName(ctx context.Context, arg GetEnvironmentByNameParams) (Environment, error) {
	row := q.db.QueryRowContext(ctx, getEnvironmentByName, arg.Name, arg.ProjectID)
	var i Environment
	err := row.Scan(
		&i.ID,
		&i.ProjectID,
		&i.Name,
		&i.Description,
	)
	return i, err
}

const getEnvironmentsForProject = `-- name: GetEnvironmentsForProject :many
select id, project_id, name, description
from environments
where project_id = ?
`

func (q *Queries) GetEnvironmentsForProject(ctx context.Context, projectID int64) ([]Environment, error) {
	rows, err := q.db.QueryContext(ctx, getEnvironmentsForProject, projectID)
	if err != nil {
		return nil, err
	}
	defer rows.Close()
	var items []Environment
	for rows.Next() {
		var i Environment
		if err := rows.Scan(
			&i.ID,
			&i.ProjectID,
			&i.Name,
			&i.Description,
		); err != nil {
			return nil, err
		}
		items = append(items, i)
	}
	if err := rows.Close(); err != nil {
		return nil, err
	}
	if err := rows.Err(); err != nil {
		return nil, err
	}
	return items, nil
}

const updateEnvironment = `-- name: UpdateEnvironment :one
update environments
set name        = ?,
    description = ?
where id = ?
returning id, project_id, name, description
`

type UpdateEnvironmentParams struct {
	Name        string
	Description sql.NullString
	ID          int64
}

func (q *Queries) UpdateEnvironment(ctx context.Context, arg UpdateEnvironmentParams) (Environment, error) {
	row := q.db.QueryRowContext(ctx, updateEnvironment, arg.Name, arg.Description, arg.ID)
	var i Environment
	err := row.Scan(
		&i.ID,
		&i.ProjectID,
		&i.Name,
		&i.Description,
	)
	return i, err
}
