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

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

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

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

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

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

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

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

-- TagRepository.addAssetIds
insert into
  "tag_asset" ("tagsId", "assetsId")
values
  ($1, $2)

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

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

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