group TagDao;

fields(prefix) ::= <<
    <prefix>id,
    <prefix>tag_definition_id,
    <prefix>object_id,
    <prefix>object_type,
    <prefix>created_by,
    <prefix>created_date
>>

insertFromTransaction() ::= <<
  INSERT INTO tags(<fields()>)
  VALUES (:id, :tagDefinitionId, :objectId, :objectType, :userName, :createdDate);
>>

deleteFromTransaction() ::= <<
    DELETE FROM tags
    WHERE tag_definition_id = :tagDefinitionId
        AND object_id = :objectId AND object_type = :objectType;
>>

addTagFromTransaction() ::= <<
    INSERT INTO tags(<fields()>)
    VALUES (:id, :tagDefinitionId, :objectId, :objectType, :userName, :createdDate);
>>

removeTagFromTransaction() ::= <<
    DELETE FROM tags
    WHERE tag_definition_id = :tagDefinitionId
    AND object_id = :objectId AND object_type = :objectType;
>>

findTag() ::= <<
    SELECT <fields()> FROM tags
    WHERE tag_definition_id = :tagDefinitionId
    AND object_id = :objectId AND object_type = :objectType;
>>

getRecordIds() ::= <<
    SELECT record_id, id
    FROM tags
    WHERE object_id = :objectId AND object_type = :objectType;
>>

historyFields(prefix) ::= <<
  <prefix>record_id,
  <prefix>id,
  <prefix>object_id,
  <prefix>object_type,
  <prefix>tag_definition_id,
  <prefix>updated_by,
  <prefix>date,
  <prefix>change_type
>>

addHistoryFromTransaction() ::= <<
    INSERT INTO tag_history(<historyFields()>)
    VALUES(:recordId, :id, :objectId, :objectType, :tagDefinitionId, :userName, :updatedDate, :changeType);
>>

getMaxHistoryRecordId() ::= <<
    SELECT MAX(history_record_id)
    FROM tag_history;
>>

getHistoryRecordIds() ::= <<
    SELECT history_record_id, record_id
    FROM tag_history
    WHERE history_record_id > :maxHistoryRecordId;
>>

auditFields(prefix) ::= <<
    <prefix>table_name,
    <prefix>record_id,
    <prefix>change_type,
    <prefix>change_date,
    <prefix>changed_by,
    <prefix>reason_code,
    <prefix>comments,
    <prefix>user_token
>>

load() ::= <<
  SELECT <fields()>
    FROM tags
  WHERE object_id = :objectId AND object_type = :objectType;
>>

insertAuditFromTransaction() ::= <<
    INSERT INTO audit_log(<auditFields()>)
    VALUES(:tableName, :recordId, :changeType, :createdDate, :userName, :reasonCode, :comment, :userToken);
>>

test() ::= <<
  SELECT 1 FROM tags;
>>
;