-- NOTE: This file is auto generated by ./sql-generator

-- TagRepository.get
select
  "tag"."id",
  "tag"."value",
  "tag"."createdAt",
  "tag"."updatedAt",
  "tag"."color",
  "tag"."parentId"
from
  "tag"
where
  "id" = $1

-- TagRepository.getByValue
select
  "tag"."id",
  "tag"."value",
  "tag"."createdAt",
  "tag"."updatedAt",
  "tag"."color",
  "tag"."parentId"
from
  "tag"
where
  "userId" = $1
  and "value" = $2

-- TagRepository.upsertValue
begin
insert into
  "tag" ("userId", "value", "parentId")
values
  ($1, $2, $3)
on conflict ("userId", "value") do update
set
  "parentId" = $4
returning
  "tag"."id",
  "tag"."value",
  "tag"."createdAt",
  "tag"."updatedAt",
  "tag"."color",
  "tag"."parentId"
rollback

-- TagRepository.getAll
select
  "tag"."id",
  "tag"."value",
  "tag"."createdAt",
  "tag"."updatedAt",
  "tag"."color",
  "tag"."parentId"
from
  "tag"
where
  "userId" = $1
order by
  "value"

-- TagRepository.create
insert into
  "tag" ("userId", "color", "value")
values
  ($1, $2, $3)
returning
  *

-- TagRepository.update
update "tag"
set
  "color" = $1
where
  "id" = $2
returning
  *

-- TagRepository.delete
delete from "tag"
where
  "id" = $1

-- TagRepository.addAssetIds
insert into
  "tag_asset" ("tagId", "assetId")
values
  ($1, $2)

-- TagRepository.removeAssetIds
delete from "tag_asset"
where
  "tagId" = $1
  and "assetId" in ($2)

-- TagRepository.upsertAssetIds
insert into
  "tag_asset" ("assetId", "tagIds")
values
  ($1, $2)
on conflict do nothing
returning
  *

-- TagRepository.replaceAssetTags
begin
delete from "tag_asset"
where
  "assetId" = $1
insert into
  "tag_asset" ("tagId", "assetId")
values
  ($1, $2)
on conflict do nothing
returning
  *
rollback
