Injection

Those pesky but very useful where-in-list sql statements are made easier with injection. Here's how to use it:

List<AirportBean> getAirportList(Injection regionInList)
<<
    select iata,icao,city_served,region,airport_name
    from   t_airport
    where  region in ( regionInList )
>>

When you run amalegeni on this, and inspect the generated java code, you'll notice that the Injection regionInList is turned into a string, which gets replaced inside your sql statement on execution.

Calling the above function in Java, with eg ..

String regionInList = " 'Antofagasta','Atacama','Coquimbo','Los Lagos','Maule','Santiago Metropolitan'  "

.. will execute this sql statement on the database:

 select iata,icao,city_served,region,airport_name 
 from   t_airport 
where  region in ( 'Antofagasta','Atacama','Coquimbo','Los Lagos','Maule','Santiago Metropolitan' ) 

Mixing positional parameters and injections

It's possible to mix injections and positional parameters, eg.

List<AirportBean> getAirportList2(String iataInitial, Injection regionInList, String cityServedInitial )
<<
    select iata,icao,city_served,region,airport_name
    from   t_airport
    where  substr(iata,1,1)=?
    and    region in ( regionInList )
    and    substr(city_served,1,1)=?
>>

.. will be turned into this sql on execution:

select iata,icao,city_served,region,airport_name 
from   t_airport 
where  substr(iata,1,1)=? 
and    region in ( 'Antofagasta','Atacama','Coquimbo','Los Lagos','Maule','Santiago Metropolitan' ) 
and    substr(city_served,1,1)=?  

© Willem Moors, 2009 - 2013