ComparatorExpressionSQL.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.Comparator;
  26. import org.openspcoop2.generic_project.expression.impl.ComparatorExpressionImpl;
  27. import org.openspcoop2.generic_project.expression.impl.formatter.IObjectFormatter;
  28. import org.openspcoop2.generic_project.utils.IDGenerator;
  29. import org.openspcoop2.utils.sql.ISQLQueryObject;

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

  38.     private ISQLFieldConverter sqlFieldConverter;
  39.    
  40.     public ComparatorExpressionSQL(ISQLFieldConverter sqlFieldConverter, IObjectFormatter objectFormatter,
  41.             IField field, Object object, Comparator comparator) {
  42.         super(objectFormatter, field, object, comparator);
  43.         this.sqlFieldConverter = sqlFieldConverter;
  44.     }
  45.    
  46.     private String toSqlEngine(SQLMode mode,List<Object> oggettiPreparedStatement,Map<String, Object> oggettiJPA)throws ExpressionException{
  47.         StringBuilder bf = new StringBuilder();
  48.         if(isNot()){
  49.             bf.append("( NOT ");
  50.         }
  51.         bf.append("( ");
  52.         bf.append(this.sqlFieldConverter.toColumn(this.getField(),true));
  53.         bf.append(" ");
  54.         bf.append(this.getComparator().getOperatore(this.sqlFieldConverter));
  55.         if(this.getObject()!=null){
  56.             bf.append(" ");
  57.             switch (mode) {
  58.             case STANDARD:
  59.                 try{
  60.                     bf.append(super.getObjectFormatter().toSQLString(this.getObject()));
  61.                 }catch(Exception e){
  62.                     return "ERROR: "+e.getMessage();
  63.                 }
  64.                 break;
  65.             case PREPARED_STATEMENT:
  66.                 bf.append("?");
  67.                 if(oggettiPreparedStatement!=null) {
  68.                     oggettiPreparedStatement.add(this.getObject());
  69.                 }
  70.                 break;
  71.             case JPA:
  72.                 String id = IDGenerator.getUniqueID(this.getField());
  73.                 bf.append(":"+id);
  74.                 if(oggettiJPA!=null) {
  75.                     oggettiJPA.put(id, this.getObject());
  76.                 }
  77.                 break;
  78.             }
  79.         }
  80.         bf.append(" )");
  81.         if(isNot()){
  82.             bf.append(" )");
  83.         }
  84.         return bf.toString();
  85.     }

  86.     private void toSqlEngine(ISQLQueryObject sqlQueryObject,SQLMode mode,List<Object> oggettiPreparedStatement,Map<String, Object> oggettiJPA)throws ExpressionException{
  87.         try{
  88.             String s = toSqlEngine(mode, oggettiPreparedStatement, oggettiJPA);
  89.             s = s.substring(1,s.length()-2);
  90.             sqlQueryObject.addWhereCondition(s);
  91.         }catch(Exception e){
  92.             throw new ExpressionException(e);
  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. }