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

package sqlc

import (
	"context"
)

const stockCancel = `-- name: StockCancel :execrows
update stocks
set reserved  = reserved  - $2
where sku = $1
  and reserved >= $2
`

type StockCancelParams struct {
	Sku      int64
	Reserved int64
}

func (q *Queries) StockCancel(ctx context.Context, arg *StockCancelParams) (int64, error) {
	result, err := q.db.Exec(ctx, stockCancel, arg.Sku, arg.Reserved)
	if err != nil {
		return 0, err
	}
	return result.RowsAffected(), nil
}

const stockGetByID = `-- name: StockGetByID :one
select sku, total_count, reserved
from stocks
where sku = $1
limit 1
`

func (q *Queries) StockGetByID(ctx context.Context, sku int64) (*Stock, error) {
	row := q.db.QueryRow(ctx, stockGetByID, sku)
	var i Stock
	err := row.Scan(&i.Sku, &i.TotalCount, &i.Reserved)
	return &i, err
}

const stockReserve = `-- name: StockReserve :execrows
update stocks
set reserved  = reserved  + $2
where sku = $1
  and total_count >= reserved + $2
`

type StockReserveParams struct {
	Sku      int64
	Reserved int64
}

func (q *Queries) StockReserve(ctx context.Context, arg *StockReserveParams) (int64, error) {
	result, err := q.db.Exec(ctx, stockReserve, arg.Sku, arg.Reserved)
	if err != nil {
		return 0, err
	}
	return result.RowsAffected(), nil
}

const stockReserveRemove = `-- name: StockReserveRemove :execrows
update stocks
set reserved = reserved - $2,
    total_count = total_count - $2
where sku = $1
  and reserved >= $2 and total_count >= $2
`

type StockReserveRemoveParams struct {
	Sku      int64
	Reserved int64
}

func (q *Queries) StockReserveRemove(ctx context.Context, arg *StockReserveRemoveParams) (int64, error) {
	result, err := q.db.Exec(ctx, stockReserveRemove, arg.Sku, arg.Reserved)
	if err != nil {
		return 0, err
	}
	return result.RowsAffected(), nil
}
