BetweenExpressionSQL.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.BetweenExpressionImpl;
  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.  * BetweenExpressionSQL
  31.  *
  32.  * @author Poli Andrea (apoli@link.it)
  33.  * @author $Author$
  34.  * @version $Rev$, $Date$
  35.  */
  36. public class BetweenExpressionSQL extends BetweenExpressionImpl implements ISQLExpression {

  37.     private ISQLFieldConverter sqlFieldConverter;
  38.    
  39.     public BetweenExpressionSQL(ISQLFieldConverter sqlFieldConverter,IObjectFormatter objectFormatter, IField field,
  40.             Object lower, Object high) {
  41.         super(objectFormatter, field, lower, high);
  42.         this.sqlFieldConverter = sqlFieldConverter;
  43.     }

  44.     private String toSqlEngine(SQLMode mode,List<Object> oggettiPreparedStatement,Map<String, Object> oggettiJPA)throws ExpressionException{
  45.         StringBuilder bf = new StringBuilder();
  46.         if(isNot()){
  47.             bf.append("( NOT ");
  48.         }
  49.         bf.append("( ");
  50.         bf.append(this.sqlFieldConverter.toColumn(this.getField(),true));
  51.         bf.append(" BETWEEN ");
  52.         bf.append(" ");
  53.         switch (mode) {
  54.         case STANDARD:
  55.             try{
  56.                 bf.append(super.getObjectFormatter().toSQLString(this.getLower()));
  57.             }catch(Exception e){
  58.                 return "ERROR-LOWER: "+e.getMessage();
  59.             }
  60.             break;
  61.         case PREPARED_STATEMENT:
  62.             bf.append("?");
  63.             if(oggettiPreparedStatement!=null) {
  64.                 oggettiPreparedStatement.add(this.getLower());
  65.             }
  66.             break;
  67.         case JPA:
  68.             String id = "Lower_"+IDGenerator.getUniqueID(this.getField());
  69.             bf.append(":"+id);
  70.             if(oggettiJPA!=null) {
  71.                 oggettiJPA.put(id, this.getLower());
  72.             }
  73.             break;
  74.         }
  75.         bf.append(" AND ");
  76.         switch (mode) {
  77.         case STANDARD:
  78.             try{
  79.                 bf.append(super.getObjectFormatter().toSQLString(this.getHigh()));
  80.             }catch(Exception e){
  81.                 return "ERROR-HIGH: "+e.getMessage();
  82.             }
  83.             break;
  84.         case PREPARED_STATEMENT:
  85.             bf.append("?");
  86.             if(oggettiPreparedStatement!=null) {
  87.                 oggettiPreparedStatement.add(this.getHigh());
  88.             }
  89.             break;
  90.         case JPA:
  91.             String id = "High_"+IDGenerator.getUniqueID(this.getField());
  92.             bf.append(":"+id);
  93.             if(oggettiJPA!=null) {
  94.                 oggettiJPA.put(id, this.getHigh());
  95.             }
  96.             break;
  97.         }
  98.         bf.append(" )");
  99.         if(isNot()){
  100.             bf.append(" )");
  101.         }
  102.         return bf.toString();
  103.     }
  104.    
  105.     private void toSqlEngine(ISQLQueryObject sqlQueryObject,SQLMode mode,List<Object> oggettiPreparedStatement,Map<String, Object> oggettiJPA)throws ExpressionException{
  106.         try{
  107.             String s = toSqlEngine(mode, oggettiPreparedStatement, oggettiJPA);
  108.             s = s.substring(1,s.length()-2);
  109.             sqlQueryObject.addWhereCondition(s);
  110.         }catch(Exception e){
  111.             throw new ExpressionException(e);
  112.         }
  113.     }
  114.    
  115.     @Override
  116.     public String toSql() throws ExpressionException {
  117.         return toSqlEngine(SQLMode.STANDARD, null, null);
  118.     }

  119.     @Override
  120.     public String toSqlPreparedStatement(List<Object> oggetti)
  121.             throws ExpressionException {
  122.         return toSqlEngine(SQLMode.PREPARED_STATEMENT, oggetti, null);
  123.     }

  124.     @Override
  125.     public String toSqlJPA(Map<String, Object> oggetti)
  126.             throws ExpressionException {
  127.         return toSqlEngine(SQLMode.JPA, null, oggetti);
  128.     }
  129.    
  130.     @Override
  131.     public void toSql(ISQLQueryObject sqlQueryObject) throws ExpressionException {
  132.         toSqlEngine(sqlQueryObject,SQLMode.STANDARD, null, null);
  133.     }

  134.     @Override
  135.     public void toSqlPreparedStatement(ISQLQueryObject sqlQueryObject,List<Object> oggetti)
  136.             throws ExpressionException {
  137.         toSqlEngine(sqlQueryObject,SQLMode.PREPARED_STATEMENT, oggetti, null);
  138.     }

  139.     @Override
  140.     public void toSqlJPA(ISQLQueryObject sqlQueryObject,Map<String, Object> oggetti)
  141.             throws ExpressionException {
  142.         toSqlEngine(sqlQueryObject,SQLMode.JPA, null, oggetti);
  143.     }
  144. }