InExpressionSQL.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.generic_project.expression.impl.sql;

  21. import java.util.List;
  22. import java.util.Map;

  23. import org.openspcoop2.generic_project.beans.IField;
  24. import org.openspcoop2.generic_project.exception.ExpressionException;
  25. import org.openspcoop2.generic_project.expression.impl.InExpressionImpl;
  26. import org.openspcoop2.generic_project.expression.impl.formatter.IObjectFormatter;
  27. import org.openspcoop2.generic_project.utils.IDGenerator;
  28. import org.openspcoop2.utils.sql.ISQLQueryObject;

  29. /**
  30.  * InExpressionSQL
  31.  *
  32.  * @author Poli Andrea (apoli@link.it)
  33.  * @author $Author$
  34.  * @version $Rev$, $Date$
  35.  */
  36. public class InExpressionSQL extends InExpressionImpl implements ISQLExpression {

  37.     private ISQLFieldConverter sqlFieldConverter;
  38.    
  39.     public InExpressionSQL(ISQLFieldConverter sqlFieldConverter,IObjectFormatter objectFormatter, IField field,
  40.             List<Object> objects) {
  41.         super(objectFormatter, field, objects);
  42.         this.sqlFieldConverter = sqlFieldConverter;
  43.     }
  44.    
  45.     private String toSqlEngine(SQLMode mode,List<Object> oggettiPreparedStatement,Map<String, Object> oggettiJPA)throws ExpressionException{
  46.         StringBuilder bf = new StringBuilder();
  47.         if(isNot()){
  48.             bf.append("( NOT ");
  49.         }
  50.         bf.append("( ");
  51.         bf.append(this.sqlFieldConverter.toColumn(this.getField(),true));
  52.         bf.append(" IN (");
  53.         for (int i = 0; i < this.getObjects().size(); i++) {
  54.             bf.append(" ");
  55.             if(i>0){
  56.                 bf.append(", ");
  57.             }
  58.             switch (mode) {
  59.             case STANDARD:
  60.                 try{
  61.                     bf.append(super.getObjectFormatter().toSQLString(this.getObjects().get(i)));
  62.                 }catch(Exception e){
  63.                     return "ERROR["+i+"]: "+e.getMessage();
  64.                 }  
  65.                 break;
  66.             case PREPARED_STATEMENT:
  67.                 bf.append("?");
  68.                 oggettiPreparedStatement.add(this.getObjects().get(i));
  69.                 break;
  70.             case JPA:
  71.                 String id = "o"+i+"_"+IDGenerator.getUniqueID(this.getField());
  72.                 bf.append(":"+id);
  73.                 oggettiJPA.put(id, this.getObjects().get(i));
  74.                 break;
  75.             }
  76.         }
  77.         bf.append(" )"); // in
  78.         bf.append(" )");
  79.         if(isNot()){
  80.             bf.append(" )");
  81.         }
  82.         return bf.toString();
  83.     }
  84.    
  85.     private void toSqlEngine(ISQLQueryObject sqlQueryObject,SQLMode mode,List<Object> oggettiPreparedStatement,Map<String, Object> oggettiJPA)throws ExpressionException{
  86.         try{
  87.             String s = toSqlEngine(mode, oggettiPreparedStatement, oggettiJPA);
  88.             s = s.substring(1,s.length()-2);
  89.             sqlQueryObject.addWhereCondition(s);
  90.         }catch(Exception e){
  91.             throw new ExpressionException(e);
  92.         }
  93.     }
  94.    
  95.     @Override
  96.     public String toSql() throws ExpressionException {
  97.         return toSqlEngine(SQLMode.STANDARD, null, null);
  98.     }

  99.     @Override
  100.     public String toSqlPreparedStatement(List<Object> oggetti)
  101.             throws ExpressionException {
  102.         return toSqlEngine(SQLMode.PREPARED_STATEMENT, oggetti, null);
  103.     }

  104.     @Override
  105.     public String toSqlJPA(Map<String, Object> oggetti)
  106.             throws ExpressionException {
  107.         return toSqlEngine(SQLMode.JPA, null, oggetti);
  108.     }
  109.    
  110.     @Override
  111.     public void toSql(ISQLQueryObject sqlQueryObject) throws ExpressionException {
  112.         toSqlEngine(sqlQueryObject,SQLMode.STANDARD, null, null);
  113.     }

  114.     @Override
  115.     public void toSqlPreparedStatement(ISQLQueryObject sqlQueryObject,List<Object> oggetti)
  116.             throws ExpressionException {
  117.         toSqlEngine(sqlQueryObject,SQLMode.PREPARED_STATEMENT, oggetti, null);
  118.     }

  119.     @Override
  120.     public void toSqlJPA(ISQLQueryObject sqlQueryObject,Map<String, Object> oggetti)
  121.             throws ExpressionException {
  122.         toSqlEngine(sqlQueryObject,SQLMode.JPA, null, oggetti);
  123.     }
  124. }