// Code generated by sqlc. DO NOT EDIT.
// versions:
//   sqlc v1.29.0
// source: var_ref.sql

package sqlcgen

import (
	"context"
)

const varRefCreate = `-- name: VarRefCreate :exec
INSERT INTO var_ref(
    env_id, name, comment, create_time, update_time, var_id
) VALUES (
    ?     , ?   , ?      , ?          , ?          , ?
)
`

type VarRefCreateParams struct {
	EnvID      int64
	Name       string
	Comment    string
	CreateTime string
	UpdateTime string
	VarID      int64
}

func (q *Queries) VarRefCreate(ctx context.Context, arg VarRefCreateParams) error {
	_, err := q.db.ExecContext(ctx, varRefCreate,
		arg.EnvID,
		arg.Name,
		arg.Comment,
		arg.CreateTime,
		arg.UpdateTime,
		arg.VarID,
	)
	return err
}

const varRefDelete = `-- name: VarRefDelete :execrows
DELETE FROM var_ref WHERE env_id = ? AND name = ?
`

type VarRefDeleteParams struct {
	EnvID int64
	Name  string
}

func (q *Queries) VarRefDelete(ctx context.Context, arg VarRefDeleteParams) (int64, error) {
	result, err := q.db.ExecContext(ctx, varRefDelete, arg.EnvID, arg.Name)
	if err != nil {
		return 0, err
	}
	return result.RowsAffected()
}

const varRefList = `-- name: VarRefList :many
SELECT var_ref_id, env_id, name, comment, create_time, update_time, var_id FROM var_ref
WHERE env_id = ?
ORDER BY name ASC
`

func (q *Queries) VarRefList(ctx context.Context, envID int64) ([]VarRef, error) {
	rows, err := q.db.QueryContext(ctx, varRefList, envID)
	if err != nil {
		return nil, err
	}
	defer rows.Close()
	var items []VarRef
	for rows.Next() {
		var i VarRef
		if err := rows.Scan(
			&i.VarRefID,
			&i.EnvID,
			&i.Name,
			&i.Comment,
			&i.CreateTime,
			&i.UpdateTime,
			&i.VarID,
		); 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 varRefListByVarID = `-- name: VarRefListByVarID :many
SELECT env.name AS env_name, var_ref.var_ref_id, var_ref.env_id, var_ref.name, var_ref.comment, var_ref.create_time, var_ref.update_time, var_ref.var_id FROM var_ref
JOIN env ON var_ref.env_id = env.env_id
WHERE var_id = ?
ORDER BY var_ref.name ASC
`

type VarRefListByVarIDRow struct {
	EnvName    string
	VarRefID   int64
	EnvID      int64
	Name       string
	Comment    string
	CreateTime string
	UpdateTime string
	VarID      int64
}

func (q *Queries) VarRefListByVarID(ctx context.Context, varID int64) ([]VarRefListByVarIDRow, error) {
	rows, err := q.db.QueryContext(ctx, varRefListByVarID, varID)
	if err != nil {
		return nil, err
	}
	defer rows.Close()
	var items []VarRefListByVarIDRow
	for rows.Next() {
		var i VarRefListByVarIDRow
		if err := rows.Scan(
			&i.EnvName,
			&i.VarRefID,
			&i.EnvID,
			&i.Name,
			&i.Comment,
			&i.CreateTime,
			&i.UpdateTime,
			&i.VarID,
		); 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 varRefShow = `-- name: VarRefShow :one
SELECT var_ref_id, env_id, name, comment, create_time, update_time, var_id
FROM var_ref
WHERE env_id = ? AND name = ?
`

type VarRefShowParams struct {
	EnvID int64
	Name  string
}

func (q *Queries) VarRefShow(ctx context.Context, arg VarRefShowParams) (VarRef, error) {
	row := q.db.QueryRowContext(ctx, varRefShow, arg.EnvID, arg.Name)
	var i VarRef
	err := row.Scan(
		&i.VarRefID,
		&i.EnvID,
		&i.Name,
		&i.Comment,
		&i.CreateTime,
		&i.UpdateTime,
		&i.VarID,
	)
	return i, err
}

const varRefUpdate = `-- name: VarRefUpdate :execrows
UPDATE var_ref SET
    env_id = COALESCE(?1, env_id),
    name = COALESCE(?2, name),
    comment = COALESCE(?3, comment),
    create_time = COALESCE(?4, create_time),
    update_time = COALESCE(?5, update_time),
    var_id = COALESCE(?6, var_id)
WHERE var_ref_id = ?7
`

type VarRefUpdateParams struct {
	EnvID      *int64
	Name       *string
	Comment    *string
	CreateTime *string
	UpdateTime *string
	VarID      *int64
	VarRefID   int64
}

func (q *Queries) VarRefUpdate(ctx context.Context, arg VarRefUpdateParams) (int64, error) {
	result, err := q.db.ExecContext(ctx, varRefUpdate,
		arg.EnvID,
		arg.Name,
		arg.Comment,
		arg.CreateTime,
		arg.UpdateTime,
		arg.VarID,
		arg.VarRefID,
	)
	if err != nil {
		return 0, err
	}
	return result.RowsAffected()
}
