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

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

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

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