// Code generated by sqlc. DO NOT EDIT.
// versions:
//   sqlc v1.28.0
// source: role.sql

package db

import (
	"context"
)

const attachRole = `-- name: AttachRole :exec
 INSERT INTO user_roles (
    user_id,
    role_id
 ) VALUES (
    $1, $2
 )
`

type AttachRoleParams struct {
	UserID int32 `json:"user_id"`
	RoleID int32 `json:"role_id"`
}

func (q *Queries) AttachRole(ctx context.Context, arg AttachRoleParams) error {
	_, err := q.db.ExecContext(ctx, attachRole, arg.UserID, arg.RoleID)
	return err
}

const createRole = `-- name: CreateRole :one
INSERT INTO roles (
    name,
    description
 ) VALUES (
    $1, $2
 ) RETURNING id, name, exportcompany_id, description, created_at
`

type CreateRoleParams struct {
	Name        string `json:"name"`
	Description string `json:"description"`
}

func (q *Queries) CreateRole(ctx context.Context, arg CreateRoleParams) (Role, error) {
	row := q.db.QueryRowContext(ctx, createRole, arg.Name, arg.Description)
	var i Role
	err := row.Scan(
		&i.ID,
		&i.Name,
		&i.ExportcompanyID,
		&i.Description,
		&i.CreatedAt,
	)
	return i, err
}

const deleteRole = `-- name: DeleteRole :exec
DELETE FROM roles WHERE id= $1
`

func (q *Queries) DeleteRole(ctx context.Context, id int64) error {
	_, err := q.db.ExecContext(ctx, deleteRole, id)
	return err
}

const getRole = `-- name: GetRole :one
SELECT id, name, exportcompany_id, description, created_at FROM roles
WHERE id = $1 LIMIT 1
`

func (q *Queries) GetRole(ctx context.Context, id int64) (Role, error) {
	row := q.db.QueryRowContext(ctx, getRole, id)
	var i Role
	err := row.Scan(
		&i.ID,
		&i.Name,
		&i.ExportcompanyID,
		&i.Description,
		&i.CreatedAt,
	)
	return i, err
}

const listAllRoles = `-- name: ListAllRoles :many
SELECT id, name, exportcompany_id, description, created_at FROM roles
`

func (q *Queries) ListAllRoles(ctx context.Context) ([]Role, error) {
	rows, err := q.db.QueryContext(ctx, listAllRoles)
	if err != nil {
		return nil, err
	}
	defer rows.Close()
	items := []Role{}
	for rows.Next() {
		var i Role
		if err := rows.Scan(
			&i.ID,
			&i.Name,
			&i.ExportcompanyID,
			&i.Description,
			&i.CreatedAt,
		); 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 listRoles = `-- name: ListRoles :many
SELECT id, name, exportcompany_id, description, created_at FROM roles
ORDER BY id LIMIT $1
OFFSET $2
`

type ListRolesParams struct {
	Limit  int32 `json:"limit"`
	Offset int32 `json:"offset"`
}

func (q *Queries) ListRoles(ctx context.Context, arg ListRolesParams) ([]Role, error) {
	rows, err := q.db.QueryContext(ctx, listRoles, arg.Limit, arg.Offset)
	if err != nil {
		return nil, err
	}
	defer rows.Close()
	items := []Role{}
	for rows.Next() {
		var i Role
		if err := rows.Scan(
			&i.ID,
			&i.Name,
			&i.ExportcompanyID,
			&i.Description,
			&i.CreatedAt,
		); 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 updateRole = `-- name: UpdateRole :one
UPDATE roles
  set 
  name= $2,
  description = $3
WHERE id = $1
RETURNING id, name, exportcompany_id, description, created_at
`

type UpdateRoleParams struct {
	ID          int64  `json:"id"`
	Name        string `json:"name"`
	Description string `json:"description"`
}

func (q *Queries) UpdateRole(ctx context.Context, arg UpdateRoleParams) (Role, error) {
	row := q.db.QueryRowContext(ctx, updateRole, arg.ID, arg.Name, arg.Description)
	var i Role
	err := row.Scan(
		&i.ID,
		&i.Name,
		&i.ExportcompanyID,
		&i.Description,
		&i.CreatedAt,
	)
	return i, err
}
