group AuditSqlDao;

fields(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(<fields()>)
    VALUES(:tableName, :recordId, :changeType, :createdDate, :userName, :reasonCode, :comment, :userToken);
>>

getAuditLogsForRecordId() ::= <<
  SELECT <fields()>
  FROM audit_log
  WHERE record_id = :recordId
  AND table_name = :tableName
  ORDER BY change_date ASC
>>

getRecordIdForTable(tableName) ::= <<
  SELECT record_id
  FROM <tableName>
  WHERE id = :id
  LIMIT 1
>>

getHistoryRecordIdsForTable(tableName) ::= <<
  SELECT history_record_id record_id
  FROM <tableName>
  WHERE id = :id
>>
