getRecordIdFromObject(tableName) ::= <<
select
  record_id
from <tableName>
where id = :id
;
>>

getIdFromObject(tableName) ::= <<
select
  id
from <tableName>
where record_id = :recordId
;
>>

getAccountRecordIdFromAccountHistory() ::= <<
select
  target_record_id
from account_history
where id = :id
;
>>

getAccountRecordIdFromAccount() ::= <<
select
  record_id
from accounts
where id = :id
;
>>

getAccountRecordIdFromObjectOtherThanAccount(tableName) ::= <<
select
  account_record_id
from <tableName>
where id = :id
;
>>

getTenantRecordIdFromTenant() ::= <<
select
  record_id
from tenants
where id = :id
;
>>

getTenantRecordIdFromObjectOtherThanTenant(tableName) ::= <<
select
  tenant_record_id
from <tableName>
where id = :id
;
>>


getLastHistoryRecordId(tableName) ::= <<
select
  max(record_id)
from <tableName>
where target_record_id = :targetRecordId
;
>>

getHistoryTargetRecordId(tableName) ::= <<
select
  target_record_id
from <tableName>
where record_id = :recordId
;
>>

getHistoryRecordIdIdMappings(tableName, historyTableName) ::= <<
select
  ht.record_id
, t.id
from <tableName> t
join <historyTableName> ht on ht.target_record_id = t.record_id
where t.account_record_id = :accountRecordId
and t.tenant_record_id = :tenantRecordId
;
>>

getHistoryRecordIdIdMappingsForAccountsTable(tableName, historyTableName) ::= <<
select
  ht.record_id
, t.id
from <tableName> t
join <historyTableName> ht on ht.target_record_id = t.record_id
where t.record_id = :accountRecordId
and t.tenant_record_id = :tenantRecordId
;
>>

getHistoryRecordIdIdMappingsForTablesWithoutAccountRecordId(tableName, historyTableName) ::= <<
select
  ht.record_id
, t.id
from <tableName> t
join <historyTableName> ht on ht.target_record_id = t.record_id
where 1 = 1
and t.tenant_record_id = :tenantRecordId
;
>>

getRecordIdIdMappings(tableName) ::= <<
select
  t.record_id
, t.id
from <tableName> t
where t.account_record_id = :accountRecordId
and t.tenant_record_id = :tenantRecordId
;
>>