LikeExpressionSQL.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.LikeMode;
  26. import org.openspcoop2.generic_project.expression.impl.LikeExpressionImpl;
  27. import org.openspcoop2.generic_project.expression.impl.formatter.IObjectFormatter;
  28. import org.openspcoop2.utils.TipiDatabase;
  29. import org.openspcoop2.utils.sql.EscapeSQLPattern;
  30. import org.openspcoop2.utils.sql.ISQLQueryObject;
  31. import org.openspcoop2.utils.sql.SQLObjectFactory;

  32. /**
  33.  * LikeExpressionSQL
  34.  *
  35.  * @author Poli Andrea (apoli@link.it)
  36.  * @author $Author$
  37.  * @version $Rev$, $Date$
  38.  */
  39. public class LikeExpressionSQL extends LikeExpressionImpl implements ISQLExpression {

  40.     private ISQLFieldConverter sqlFieldConverter;
  41.    
  42.     public LikeExpressionSQL(ISQLFieldConverter sqlFieldConverter,IObjectFormatter objectFormatter, IField field,
  43.             String value, LikeMode mode, boolean caseInsensitive) {
  44.         super(objectFormatter, field, value, mode, caseInsensitive);
  45.         this.sqlFieldConverter = sqlFieldConverter;
  46.     }

  47.     private String toSqlEngine(SQLMode mode,ISQLQueryObject sqlQueryObject,List<?> oggettiPreparedStatement,Map<String, ?> oggettiJPA)throws ExpressionException{
  48.        
  49.         if(mode!=null) {
  50.             // nop
  51.         }
  52.         if(oggettiPreparedStatement!=null) {
  53.             // nop
  54.         }
  55.         if(oggettiJPA!=null) {
  56.             // nop
  57.         }
  58.        
  59.         StringBuilder bf = new StringBuilder();
  60.         if(isNot()){
  61.             bf.append("( NOT ");
  62.         }
  63.         bf.append("( ");
  64.         if(this.isCaseInsensitive()){
  65.             bf.append("lower(");
  66.         }
  67.         bf.append(this.sqlFieldConverter.toColumn(this.getField(),true));
  68.         if(this.isCaseInsensitive()){
  69.             bf.append(")");
  70.         }
  71.         bf.append(" like '");
  72.         String sqlValue = null;
  73.         try{
  74.             sqlValue = super.getObjectFormatter().toString(this.getValue());
  75.         }catch(Exception e){
  76.             return "ERROR: "+e.getMessage();
  77.         }
  78.            
  79.         // escape
  80.         String escapeClausole = "";
  81.         try{
  82.             ISQLQueryObject sqlQueryObjectForEscape = null;
  83.             if(sqlQueryObject!=null){
  84.                 sqlQueryObjectForEscape = sqlQueryObject.newSQLQueryObject();
  85.             }
  86.             else{
  87.                 sqlQueryObjectForEscape = SQLObjectFactory.createSQLQueryObject(TipiDatabase.POSTGRESQL); // lo uso come default per produrre sql
  88.             }
  89.             /**System.out.println("DATABASE: "+sqlQueryObjectForEscape.getClass().getName());*/
  90.             EscapeSQLPattern escapePattern = sqlQueryObjectForEscape.escapePatternValue(sqlValue);
  91.             /**System.out.println("ESCAPE PATTERN: "+escapePattern.isUseEscapeClausole());*/
  92.             if(escapePattern.isUseEscapeClausole()){
  93.                 escapeClausole = " ESCAPE '"+escapePattern.getEscapeClausole()+"'";
  94.             }
  95.             sqlValue = escapePattern.getEscapeValue();
  96.         }catch(Exception e){
  97.             throw new ExpressionException(e);
  98.         }
  99.        
  100.         // mode
  101.         String likeParam = getLikeParamSql(sqlValue);
  102.         bf.append(likeParam);
  103.         bf.append("'");
  104.         bf.append(escapeClausole);
  105.         bf.append(" )");
  106.         if(isNot()){
  107.             bf.append(" )");
  108.         }
  109.         return bf.toString();
  110.     }
  111.     private String getLikeParamSql(String sqlValue) {
  112.         String likeParam = null;
  113.         switch (this.getMode()) {
  114.         case EXACT:
  115.             if(this.isCaseInsensitive()){
  116.                 likeParam = sqlValue.toLowerCase();
  117.             }else{
  118.                 likeParam = sqlValue;
  119.             }
  120.             break;
  121.         case ANYWHERE:
  122.             if(this.isCaseInsensitive()){
  123.                 likeParam = "%"+sqlValue.toLowerCase()+"%";
  124.             }else{
  125.                 likeParam = "%"+sqlValue+"%";
  126.             }
  127.             break;
  128.         case END:
  129.             if(this.isCaseInsensitive()){
  130.                 likeParam = "%"+sqlValue.toLowerCase();
  131.             }else{
  132.                 likeParam = "%"+sqlValue;
  133.             }
  134.             break;
  135.         case START:
  136.             if(this.isCaseInsensitive()){
  137.                 likeParam = sqlValue.toLowerCase()+"%";
  138.             }else{
  139.                 likeParam = sqlValue+"%";
  140.             }
  141.             break;
  142.         default:
  143.             break;
  144.         }
  145.         return likeParam;
  146.     }
  147.    
  148.     private void toSqlEngine(ISQLQueryObject sqlQueryObject,SQLMode mode,List<Object> oggettiPreparedStatement,Map<String, Object> oggettiJPA)throws ExpressionException{
  149.         try{
  150.             String s = toSqlEngine(mode, sqlQueryObject, oggettiPreparedStatement, oggettiJPA);
  151.             s = s.substring(1,s.length()-2);
  152.             sqlQueryObject.addWhereCondition(s);
  153.         }catch(Exception e){
  154.             throw new ExpressionException(e);
  155.         }
  156.     }
  157.    
  158.     @Override
  159.     public String toSql() throws ExpressionException {
  160.         return toSqlEngine(SQLMode.STANDARD, null, null, null);
  161.     }

  162.     @Override
  163.     public String toSqlPreparedStatement(List<Object> oggetti)
  164.             throws ExpressionException {
  165.         return toSqlEngine(SQLMode.PREPARED_STATEMENT, null, oggetti, null);
  166.     }

  167.     @Override
  168.     public String toSqlJPA(Map<String, Object> oggetti)
  169.             throws ExpressionException {
  170.         return toSqlEngine(SQLMode.JPA, null, null, oggetti);
  171.     }
  172.    
  173.     @Override
  174.     public void toSql(ISQLQueryObject sqlQueryObject) throws ExpressionException {
  175.         toSqlEngine(sqlQueryObject,SQLMode.STANDARD, null, null);
  176.     }

  177.     @Override
  178.     public void toSqlPreparedStatement(ISQLQueryObject sqlQueryObject,List<Object> oggetti)
  179.             throws ExpressionException {
  180.         toSqlEngine(sqlQueryObject,SQLMode.PREPARED_STATEMENT, oggetti, null);
  181.     }

  182.     @Override
  183.     public void toSqlJPA(ISQLQueryObject sqlQueryObject,Map<String, Object> oggetti)
  184.             throws ExpressionException {
  185.         toSqlEngine(sqlQueryObject,SQLMode.JPA, null, oggetti);
  186.     }
  187.    
  188. }