group TagDao: EntitySqlDao;

tableName() ::= "tags"

andCheckSoftDeletionWithComma(prefix) ::= "and <prefix>is_active"

tableFields(prefix) ::= <<
  <prefix>tag_definition_id
, <prefix>object_id
, <prefix>object_type
, <prefix>is_active
, <prefix>created_by
, <prefix>created_date
, <prefix>updated_by
, <prefix>updated_date
>>

tableValues() ::= <<
  :tagDefinitionId
, :objectId
, :objectType
, :isActive
, :createdBy
, :createdDate
, :updatedBy
, :updatedDate
>>

historyTableName() ::= "tag_history"

markTagAsDeleted() ::= <<
update <tableName()> t
set t.is_active = 0
where <idField("t.")> = :id
<AND_CHECK_TENANT("t.")>
;
>>

getTagsForObject() ::= <<
select
  <allTableFields("t.")>
from <tableName()> t
where t.is_active
and t.object_id = :objectId
and t.object_type = :objectType
<AND_CHECK_TENANT("t.")>
;
>>

getTagsForObjectIncludedDeleted() ::= <<
select
  <allTableFields("t.")>
from <tableName()> t
where 1 = 1
and t.object_id = :objectId
and t.object_type = :objectType
<AND_CHECK_TENANT("t.")>
;
>>

searchTags(searchKey, offset, rowCount) ::= <<
select SQL_CALC_FOUND_ROWS
<allTableFields("t.")>
from <tableName()> t
join tag_definitions td on td.id = t.tag_definition_id
where 1 = 1
and (
     <idField("t.")> = '<searchKey>'
  or t.object_type like '%<searchKey>%'
  or td.name like '%<searchKey>%'
  or td.description like '%<searchKey>%'
)
<AND_CHECK_TENANT("t.")>
order by <recordIdField("t.")> ASC
limit :offset, :rowCount
;
>>
