group CatalogOverrideUsageTierSqlDao;

tableName() ::= "catalog_override_usage_tier"

tableFields(prefix) ::= <<
  <prefix>tier_number
, <prefix>tier_def_record_id
, <prefix>target_usage_def_record_id
, <prefix>created_date
, <prefix>created_by
, <prefix>tenant_record_id
>>

allTableFields(prefix) ::= <<
  <prefix>record_id
, <tableFields(prefix)>
>>

tableValues() ::= <<
  :tierNumber
, :tierDefRecordId
, :targetUsageDefRecordId
, :createdDate
, :createdBy
, :tenantRecordId
>>

allTableValues() ::= <<
  :recordId
, <tableValues()>
>>

create() ::= <<
insert into <tableName()> (
<tableFields()>
)
values (
<tableValues()>
)
;
>>

getByRecordId() ::= <<
select <allTableFields()>
from
<tableName()>
where record_id = :recordId
and tenant_record_id = :tenantRecordId
;
>>

getTargetUsageDefinition(keys) ::= <<
select
target_usage_def_record_id
from (select
      target_usage_def_record_id
      , count(*) count
      from
      <tableName()>
      where
      concat_ws(',', tier_number, tier_def_record_id) in (<keys: {key | :key_<i0>}; separator="," >)
      and tenant_record_id = :tenantRecordId
      group by target_usage_def_record_id) tmp
where
1=1
and tmp.count = :targetCount
;
>>

getLastInsertId() ::= <<
select LAST_INSERT_ID();
>>

