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

package sqlc

import (
	"context"
)

const orderAddItem = `-- name: OrderAddItem :exec
insert into order_items (order_id, sku, count)
select $1, $2, $3
`

type OrderAddItemParams struct {
	OrderID int64
	Sku     int64
	Count   int64
}

func (q *Queries) OrderAddItem(ctx context.Context, arg *OrderAddItemParams) error {
	_, err := q.db.Exec(ctx, orderAddItem, arg.OrderID, arg.Sku, arg.Count)
	return err
}

const orderCreate = `-- name: OrderCreate :one
insert into orders (status_name, user_id)
values ($1, $2)
returning id
`

type OrderCreateParams struct {
	StatusName string
	UserID     int64
}

func (q *Queries) OrderCreate(ctx context.Context, arg *OrderCreateParams) (int64, error) {
	row := q.db.QueryRow(ctx, orderCreate, arg.StatusName, arg.UserID)
	var id int64
	err := row.Scan(&id)
	return id, err
}

const orderGetByID = `-- name: OrderGetByID :many
select
  o.id as order_id,
  o.status_name as status,
  o.user_id,
  oi.sku,
  oi.count
from orders o
left join order_items oi on oi.order_id = o.id
where o.id = $1
order by oi.id
`

type OrderGetByIDRow struct {
	OrderID int64
	Status  string
	UserID  int64
	Sku     *int64
	Count   *int64
}

func (q *Queries) OrderGetByID(ctx context.Context, id int64) ([]*OrderGetByIDRow, error) {
	rows, err := q.db.Query(ctx, orderGetByID, id)
	if err != nil {
		return nil, err
	}
	defer rows.Close()
	var items []*OrderGetByIDRow
	for rows.Next() {
		var i OrderGetByIDRow
		if err := rows.Scan(
			&i.OrderID,
			&i.Status,
			&i.UserID,
			&i.Sku,
			&i.Count,
		); err != nil {
			return nil, err
		}
		items = append(items, &i)
	}
	if err := rows.Err(); err != nil {
		return nil, err
	}
	return items, nil
}

const orderSetStatus = `-- name: OrderSetStatus :execrows
update orders
set status_name = $2
where id = $1
`

type OrderSetStatusParams struct {
	ID         int64
	StatusName string
}

func (q *Queries) OrderSetStatus(ctx context.Context, arg *OrderSetStatusParams) (int64, error) {
	result, err := q.db.Exec(ctx, orderSetStatus, arg.ID, arg.StatusName)
	if err != nil {
		return 0, err
	}
	return result.RowsAffected(), nil
}
