tableName() ::= "catalog_override_plan_phase"


tableFields(prefix) ::= <<
  <prefix>phase_number
, <prefix>phase_def_record_id
, <prefix>target_plan_def_record_id
, <prefix>created_date
, <prefix>created_by
, <prefix>tenant_record_id
>>

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


tableValues() ::= <<
  :phaseNumber
, :phaseDefRecordId
, :targetPlanDefRecordId
, :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
;
>>

getTargetPlanDefinition(keys) ::= <<
select
target_plan_def_record_id
from (select
      target_plan_def_record_id
      , count(*) count
      from
      <tableName()>
      where
      concat_ws(',', phase_number, phase_def_record_id) in (<keys>)
      and tenant_record_id = :tenantRecordId
      group by target_plan_def_record_id) tmp
where
1=1
and tmp.count = :targetCount
;
>>


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

