group TagDao;

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

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

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

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

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

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

load() ::= <<
    SELECT t.id, t.object_id, t.object_type, t.created_by, t.created_date,
           td.id AS tag_definition_id,
           t.tag_definition_name AS tag_definition_name,
           td.description AS tag_description,
           td.created_by
    FROM tags t
    LEFT JOIN tag_definitions td ON t.tag_definition_name = td.name
    WHERE t.object_id = :objectId AND t.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_name,
  <prefix>updated_by,
  <prefix>date,
  <prefix>change_type
>>

addHistoryFromTransaction() ::= <<
    INSERT INTO tag_history(<historyFields()>)
    VALUES(:recordId, :id, :objectId, :objectType, :tagDefinitionName, :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
>>

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

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