group CustomFieldSqlDao: EntitySqlDao;

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

tableName() ::= "custom_fields"

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

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

historyTableName() ::= "custom_field_history"

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


getCustomFieldsForObject() ::= <<
select
<allTableFields()>
from <tableName()>
where
object_id = :objectId
and object_type = :objectType
and is_active
<AND_CHECK_TENANT()>
<defaultOrderBy()>
;
>>

searchCustomFields(searchKey, offset, rowCount) ::= <<
select SQL_CALC_FOUND_ROWS
<allTableFields("t.")>
from <tableName()> t
where 1 = 1
and (
     <idField("t.")> = '<searchKey>'
  or t.object_type like '%<searchKey>%'
  or t.field_name like '%<searchKey>%'
  or t.field_value like '%<searchKey>%'
)
<AND_CHECK_TENANT("t.")>
order by <recordIdField("t.")> ASC
limit :offset, :rowCount
;
>>
