mercredi 8 janvier 2020

Approach to build dynamic query statement using design patterns, to make it maintainable using php?

I am building dynamic SQL statements that can contain muliple parts(where clause mostly).

In the query below --{ParameterConstants.DateSentForPaymentRange} is a dynamic parameter which will be replaced with some AND abcCondition=abcValue in code by find replace.

But it doesn't look better, scalable, maintainable approach.

How do you people work with generation of dynamic queries? I mean what approach do you take? What design pattern do you use in language for instance in PHP?

select * from 
(
    (select  round(sum(t.vat_potential_local_currency * t.exchange_rate)::numeric, 2) as VatPotential
    from transactions_transactions{ParameterConstants.Group} t
    where
    (t.entity_id = @ClientID or t.company_hq_id = @ClientID)
    and t.rank = 1  
    and (((t.grid_type in ('OK', 'CP', 'NRD')) and t.union_part_nomer = 1 ) or t.union_part_nomer = 2) --{ParameterConstants.DateSentForPaymentRange}) as vat_potential
    inner join 
    (select coalesce(round(sum((case when reclaimed = 0 then 0 else reclaimed_local_currency_based_on_potamount end+case when submitted_local_currency_based_on_fl = 0 then 0 else submitted_local_currency_based_on_potamount end)* t.exchange_rate)::numeric, 2), 0) as submitted,
    coalesce(round(sum((case when reclaimed = 0 then 0 else reclaimed_local_currency_based_on_potamount end)* t.exchange_rate)::numeric, 2), 0) as  reclaimed
    from transactions_transactions{ParameterConstants.Group} t
    where 
    (t.entity_id = @ClientID or t.company_hq_id = @ClientID)
    --{ParameterConstants.DateSentForPaymentRange}
    and t.rank = 1
    and t.grid_type = 'OK'
    and t.union_part_nomer = 1) t2
    on 1=1
)

Aucun commentaire:

Enregistrer un commentaire