AbstractJDBCAdapter.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.io.InputStream;
  22. import java.sql.PreparedStatement;
  23. import java.sql.ResultSet;
  24. import java.sql.SQLException;

  25. import org.openspcoop2.utils.TipiDatabase;
  26. import org.openspcoop2.utils.Utilities;
  27. import org.openspcoop2.utils.UtilsException;

  28. /**
  29.  * AbstractJDBCAdapter
  30.  *
  31.  * @author Poli Andrea (apoli@link.it)
  32.  * @author $Author$
  33.  * @version $Rev$, $Date$
  34.  */

  35. public abstract class AbstractJDBCAdapter implements IJDBCAdapter {

  36.     private TipiDatabase tipoDatabase;
  37.    
  38.     public AbstractJDBCAdapter(TipiDatabase tipoDatabase){
  39.         this.tipoDatabase = tipoDatabase;
  40.     }
  41.        
  42.    
  43.    
  44.    
  45.     /* ***** BYTES ****** */

  46.     /**
  47.      * Si occupa di ottenere il messaggio precedentemente salvato sul DB,
  48.      * effettuando una get all'indice <var>index</var>.
  49.      *
  50.      * @param rs ResultSet da utilizzare.
  51.      * @param index Indice su cui prelevare il messaggio
  52.      *
  53.      */
  54.     @Override
  55.     public byte[] getBinaryData(ResultSet rs, int index) throws UtilsException,SQLException{
  56.         return rs.getBytes(index);
  57.     }
  58.    
  59.     /**
  60.      * Si occupa di ottenere il messaggio precedentemente salvato sul DB,
  61.      * effettuando una get all'indice <var>index</var>.
  62.      *
  63.      * @param rs ResultSet da utilizzare.
  64.      * @param rsName Nome rs su cui prelevare il messaggio
  65.      *
  66.      */
  67.     @Override
  68.     public byte[] getBinaryData(ResultSet rs, String rsName) throws UtilsException,SQLException{
  69.         return rs.getBytes(rsName);
  70.     }
  71.    
  72.     /**
  73.      * Si occupa di registrare il messaggio sul DB,
  74.      * all'indice <var>index</var>.
  75.      *
  76.      * @param s PreparedStatement da utilizzare utilizzare.
  77.      * @param index Indice su cui registrare il messaggio
  78.      * @param data Messaggio
  79.      *
  80.      */
  81.     @Override
  82.     public void setBinaryData(PreparedStatement s, int index, byte[] data) throws UtilsException,SQLException{
  83.         s.setBytes(index, data);
  84.     }
  85.    
  86.    
  87.    
  88.    
  89.    
  90.     /* ***** INPUT STREAM ****** */
  91.    
  92.     /**
  93.      * Si occupa di ottenere il messaggio precedentemente salvato sul DB,
  94.      * effettuando una get all'indice <var>index</var>.
  95.      *
  96.      * @param rs ResultSet da utilizzare.
  97.      * @param index Indice su cui prelevare il messaggio
  98.      *
  99.      */
  100.     @Override
  101.     public InputStream getBinaryStream(ResultSet rs, int index) throws UtilsException,SQLException{
  102.         return rs.getBinaryStream(index);
  103.     }
  104.    
  105.     /**
  106.      * Si occupa di ottenere il messaggio precedentemente salvato sul DB,
  107.      * effettuando una get all'indice <var>index</var>.
  108.      *
  109.      * @param rs ResultSet da utilizzare.
  110.      * @param rsName Nome rs su cui prelevare il messaggio
  111.      *
  112.      */
  113.     @Override
  114.     public InputStream getBinaryStream(ResultSet rs, String rsName) throws UtilsException,SQLException{
  115.         return rs.getBinaryStream(rsName);
  116.     }
  117.    
  118.     /**
  119.      * Si occupa di registrare il messaggio sul DB, all'indice <var>index</var>.
  120.      *
  121.      * @param s PreparedStatement da utilizzare utilizzare.
  122.      * @param index Indice su cui registrare il messaggio
  123.      * @param is InputStream da cui leggere i bytes del messaggio da salvare
  124.      * @param length Lunghezza del Messaggio
  125.      */
  126.     @Override
  127.     public void setBinaryData(PreparedStatement s, int index, InputStream is, int length) throws UtilsException,SQLException{
  128.         s.setBinaryStream(index, is, length);
  129.     }
  130.    
  131.     /**
  132.      * Si occupa di registrare il messaggio sul DB, all'indice <var>index</var>.
  133.      *
  134.      * @param s PreparedStatement da utilizzare utilizzare.
  135.      * @param index Indice su cui registrare il messaggio
  136.      * @param is InputStream da cui leggere i bytes del messaggio da salvare
  137.      * @param bufferingIfNotEnabled Non tutti i database supportano il salvataggio via streaming senza fornire comunque la lunghezza del messaggio. Il parametro indica se deve essere attivato un buffer (true) o se deve essere generata una eccezione
  138.      */
  139.     @Override
  140.     public void setBinaryData(PreparedStatement s, int index, InputStream is, boolean bufferingIfNotEnabled) throws UtilsException,SQLException,BinaryStreamNotSupportedException{
  141.         try{
  142.             s.setBinaryStream(index, is); // This will not work with JDBC 3.0 drivers      
  143.         }catch(Throwable e){
  144.             if(bufferingIfNotEnabled){
  145.                 this.setBinaryData(s, index, Utilities.getAsByteArray(is));
  146.             }
  147.             else{
  148.                 throw new BinaryStreamNotSupportedException("SetBinaryStream non supportata dal tipo di database utilizzato ("+this.tipoDatabase.toString()+") (Verifica di utilizzare un driver JDBC 4.x o superiore): "+e.getMessage(),e);
  149.             }
  150.         }
  151.     }
  152. }