InsertAndGeneratedKey.java

  1. /*
  2.  * GovWay - A customizable API Gateway
  3.  * https://govway.org
  4.  *
  5.  * Copyright (c) 2005-2025 Link.it srl (https://link.it).
  6.  *
  7.  * This program is free software: you can redistribute it and/or modify
  8.  * it under the terms of the GNU General Public License version 3, as published by
  9.  * the Free Software Foundation.
  10.  *
  11.  * This program is distributed in the hope that it will be useful,
  12.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14.  * GNU General Public License for more details.
  15.  *
  16.  * You should have received a copy of the GNU General Public License
  17.  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
  18.  *
  19.  */

  20. package org.openspcoop2.utils.jdbc;

  21. import java.sql.Connection;
  22. import java.sql.PreparedStatement;
  23. import java.sql.Statement;
  24. import java.sql.Timestamp;
  25. import java.util.Calendar;
  26. import java.util.Date;

  27. import org.openspcoop2.utils.TipiDatabase;
  28. import org.openspcoop2.utils.sql.ISQLQueryObject;

  29. /**
  30.  * Utiity che consente di effettuare una INSERT e recuperare l'id generato
  31.  *
  32.  * @author Poli Andrea (apoli@link.it)
  33.  * @author $Author$
  34.  * @version $Rev$, $Date$
  35.  */
  36. public class InsertAndGeneratedKey {

  37.     public static long insertAndReturnGeneratedKey(Connection con,TipiDatabase tipoDatabase,IKeyGeneratorObject object,
  38.             InsertAndGeneratedKeyObject ... objects) throws KeyGeneratorException{

  39.         PreparedStatement stmt = null;
  40.         try{

  41.             // KeyGenerator
  42.             IKeyGenerator keyGenerator = KeyGeneratorFactory.createKeyGeneratorFactory(tipoDatabase.getNome(), con, object);

  43.            
  44.             // Query di insert
  45.             ISQLQueryObject sqlQueryObject = org.openspcoop2.utils.sql.SQLObjectFactory.createSQLQueryObject(tipoDatabase.getNome());
  46.             sqlQueryObject.addInsertTable(object.getTable());
  47.             if(objects==null){
  48.                 throw new Exception("Objects for insert is null");
  49.             }
  50.             if(objects.length<=0){
  51.                 throw new Exception("Objects for insert is empty");
  52.             }
  53.             for (int i = 0; i < objects.length; i++) {
  54.                 sqlQueryObject.addInsertField(objects[i].getName(), "?");
  55.             }
  56.             if(keyGenerator.isReturnGeneratedKeySupported()==false){
  57.                 sqlQueryObject.addInsertField(keyGenerator.getColunmKeyName(), "?");
  58.             }
  59.             String insertString = sqlQueryObject.createSQLInsert();

  60.            
  61.             // Eseguo Prepared Statement
  62.             if(keyGenerator.isReturnGeneratedKeySupported()){
  63.                 stmt = con.prepareStatement(insertString,Statement.RETURN_GENERATED_KEYS);
  64.             }else{
  65.                 stmt = con.prepareStatement(insertString);
  66.             }
  67.             int  i = 0;
  68.             int indexJDBC = 1;
  69.             for ( ; i < objects.length; i++, indexJDBC++) {
  70.                 switch (objects[i].getJdbcType()) {
  71.                 case TIMESTAMP:
  72.                     Timestamp t = null;
  73.                     if(objects[i].getValue()!=null){
  74.                         if(objects[i].getValue() instanceof Timestamp){
  75.                             t = (Timestamp) objects[i].getValue();
  76.                         }else if(objects[i].getValue() instanceof Date){
  77.                             t = new Timestamp( ((Date) objects[i].getValue()).getTime());
  78.                         }else if(objects[i].getValue() instanceof java.sql.Date){
  79.                             t = new Timestamp( ((Date) objects[i].getValue()).getTime());
  80.                         }else if(objects[i].getValue() instanceof Calendar){
  81.                             t = new Timestamp( ((Calendar) objects[i].getValue()).getTime().getTime());
  82.                         }
  83.                     }
  84.                     stmt.setTimestamp(indexJDBC, t);
  85.                     break;
  86.                 case INT:
  87.                     Integer num = null;
  88.                     if(objects[i].getValue()!=null){
  89.                         if(objects[i].getValue() instanceof Integer){
  90.                             num = (Integer) objects[i].getValue();
  91.                         }else if(objects[i].getValue() instanceof Long){
  92.                             num = ((Long) objects[i].getValue()).intValue();
  93.                         }else if(objects[i].getValue() instanceof Float){
  94.                             num = ((Float) objects[i].getValue()).intValue();
  95.                         }else if(objects[i].getValue() instanceof Double){
  96.                             num = ((Double) objects[i].getValue()).intValue();
  97.                         }
  98.                     }
  99.                     if(num!=null) {
  100.                         stmt.setInt(indexJDBC, num);
  101.                     }
  102.                     else {
  103.                         stmt.setNull(indexJDBC, java.sql.Types.INTEGER);
  104.                     }
  105.                     break;
  106.                 case LONG:
  107.                     Long numLong = null;
  108.                     if(objects[i].getValue()!=null){
  109.                         if(objects[i].getValue() instanceof Long){
  110.                             numLong = (Long) objects[i].getValue();
  111.                         }else if(objects[i].getValue() instanceof Integer){
  112.                             numLong = ((Integer) objects[i].getValue()).longValue();
  113.                         }else if(objects[i].getValue() instanceof Float){
  114.                             numLong = ((Float) objects[i].getValue()).longValue();
  115.                         }else if(objects[i].getValue() instanceof Double){
  116.                             numLong = ((Double) objects[i].getValue()).longValue();
  117.                         }
  118.                     }
  119.                     if(numLong!=null) {
  120.                         stmt.setLong(indexJDBC,numLong);
  121.                     }
  122.                     else {
  123.                         stmt.setNull(indexJDBC, java.sql.Types.BIGINT);
  124.                     }
  125.                     break;
  126.                 case STRING:
  127.                     String s = null;
  128.                     if(objects[i].getValue()!=null){
  129.                         s = (String) objects[i].getValue();
  130.                     }
  131.                     JDBCUtilities.setSQLStringValue(stmt,indexJDBC,s);
  132.                     break;
  133.                 default:
  134.                     throw new Exception("JDBC Type non supportato: "+objects[i].getJdbcType());
  135.                 }
  136.             }
  137.             if(keyGenerator.isReturnGeneratedKeySupported()==false){
  138.                 stmt.setLong(indexJDBC, keyGenerator.generateKey());
  139.             }
  140.             stmt.executeUpdate();

  141.            
  142.             // Ritorno id generato
  143.             return keyGenerator.getReturnGeneratedKey(stmt);

  144.         }catch(Exception e){
  145.             throw new KeyGeneratorException ("insertAndReturnGeneratedKey failed: "+e.getMessage(),e);
  146.         }finally{
  147.             try{
  148.                 if(stmt!=null)
  149.                     stmt.close();
  150.             }catch(Exception eClose){
  151.                 // close
  152.             }
  153.         }
  154.     }
  155. }