OracleQueryObject.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.sql;


  21. import java.text.SimpleDateFormat;
  22. import java.util.ArrayList;
  23. import java.util.Date;
  24. import java.util.Iterator;
  25. import java.util.List;

  26. import org.openspcoop2.utils.TipiDatabase;
  27. import org.openspcoop2.utils.date.DateUtils;



  28. /**
  29.  * Classe dove vengono forniti utility per la conversione di comandi SQL per il database oracle
  30.  *
  31.  *
  32.  * @author Poli Andrea (apoli@link.it)
  33.  * @author $Author$
  34.  * @version $Rev$, $Date$
  35.  */
  36. public class OracleQueryObject extends SQLQueryObjectCore{
  37.    
  38.     public OracleQueryObject(TipiDatabase tipoDatabase) {
  39.         super(tipoDatabase);
  40.     }
  41.    
  42.    
  43.     // GESTIONE LIMIT SENZA OFFSET
  44.     // Se viene impostato OrderBy, e l'offset non e' impostato, deve essere impostato, altrimenti il limit funziona, pero' non ritorna un risultato ordinato.
  45.     @Override
  46.     public ISQLQueryObject addOrderBy(String orderByNomeField) throws SQLQueryObjectException{
  47.         if(this.offset<0 && this.limit>=0){
  48.             super.setOffset(0);
  49.         }
  50.         return super.addOrderBy(orderByNomeField);
  51.     }
  52.     @Override
  53.     public void setLimit(int limit) throws SQLQueryObjectException{
  54.         if(this.offset<0 && !this.orderBy.isEmpty()){
  55.             this.offset = 0;
  56.         }
  57.         super.setLimit(limit);
  58.     }
  59.    
  60.    
  61.    
  62.     // GESTIONE ALIAS 'as'
  63.     // Non tutti i database supportano l'alias 'as', ad esempio Oracle genera un errore se viene usato l'alias con le tabelle.
  64.     @Override
  65.     public String getDefaultAliasTableKeyword(){
  66.         return this.getSupportedAliasesTable().get(0);
  67.     }
  68.     @Override
  69.     public List<String> getSupportedAliasesTable(){
  70.         List<String> lista = new ArrayList<>();
  71.         lista.add(" ");
  72.         return lista;
  73.     }
  74.    
  75.    
  76.    
  77.    
  78.     /**
  79.      * Ritorna una costante  di tipo 'timestamp'
  80.      *
  81.      * @param date Costante
  82.      */
  83.     @Override
  84.     public String getSelectTimestampConstantField(Date date) throws SQLQueryObjectException{
  85.         SimpleDateFormat sqlDateformat = DateUtils.getDefaultDateTimeFormatter("yyyy-MM-dd HH:mm:ss.SSS");
  86.         return "TO_TIMESTAMP('"+sqlDateformat.format(date)+"', 'YYYY-MM-DD HH24:MI:SS.FF3')";
  87.     }
  88.    
  89.    
  90.    
  91.     @Override
  92.     public String getUnixTimestampConversion(String column){
  93.         String timeZone = null;/**"Europe/Rome";*/
  94.         timeZone = java.util.TimeZone.getDefault().getID(); // fix: uso il timeZone di sistema
  95.         return "("+
  96.                 "(((cast("+column+" as date) - to_date('19700101','YYYYMMDD')) * 86400 * 1000) + to_number(to_char("+column+",'FF3'))) - "+            
  97.                 "( (EXTRACT (timezone_hour from FROM_TZ ("+column+",(TZ_OFFSET('"+timeZone+"'))))) *60*60*1000 ) "+
  98.                 ")";
  99.                
  100.     }
  101.    
  102.     @Override
  103.     public String getDiffUnixTimestamp(String columnMax,String columnMin){
  104.         return "("+
  105.                 "(extract( day from  ("+columnMax+" - "+columnMin+"))*24*60*60*1000)"+
  106.                 "+"+
  107.                 "(extract( hour from ("+columnMax+" - "+columnMin+"))*60*60*1000)"+
  108.                 "+"+
  109.                 "(extract( minute from ("+columnMax+" - "+columnMin+"))*60*1000)"+
  110.                 "+"+
  111.                 "round(extract( second from ("+columnMax+" - "+columnMin+"))*1000)"+
  112.                 ")";
  113.     }

  114.    
  115.    
  116.    
  117.     /**
  118.      * Aggiunge un field count alla select, con un alias, del campo <var>fieldAvg</var> (di tipo Timestamp)
  119.      * es: SELECT avg(fieldAvg) as alias FROM ....
  120.      *
  121.       * @param field Nome del Field
  122.      * @param alias Alias
  123.      */
  124.     @Override
  125.     public ISQLQueryObject addSelectAvgTimestampField(String field,String alias) throws SQLQueryObjectException{
  126.         if(field==null)
  127.             throw new SQLQueryObjectException(SQLQueryObjectCore.FIELD_DEVE_ESSERE_DIVERSO_NULL);
  128.         // Trasformo in UNIX_TIMESTAMP
  129.         String fieldSQL = "avg("+this.getUnixTimestampConversion(field)+")";
  130.         if(alias != null){
  131.             /**fieldSQL = fieldSQL + " as "+alias;*/
  132.             fieldSQL = fieldSQL + this.getDefaultAliasFieldKeyword() + alias;
  133.         }
  134.         this.engineAddSelectField(null,fieldSQL,null,false,true);
  135.         this.fieldNames.add(alias);
  136.         return this;
  137.     }
  138.    
  139.    
  140.     /**
  141.      * Aggiunge un field count alla select, con un alias, del campo <var>field</var> (di tipo Timestamp)
  142.      * es: SELECT max(field) as alias FROM ....
  143.      *
  144.       * @param field Nome del Field
  145.      * @param alias Alias
  146.      */
  147.     @Override
  148.     public ISQLQueryObject addSelectMaxTimestampField(String field,String alias) throws SQLQueryObjectException{
  149.         if(field==null)
  150.             throw new SQLQueryObjectException(SQLQueryObjectCore.FIELD_DEVE_ESSERE_DIVERSO_NULL);
  151.         // Trasformo in UNIX_TIMESTAMP
  152.         String fieldSQL = "max("+this.getUnixTimestampConversion(field)+")";
  153.         if(alias != null){
  154.             /**fieldSQL = fieldSQL + " as "+alias;*/
  155.             fieldSQL = fieldSQL + this.getDefaultAliasFieldKeyword() + alias;
  156.         }
  157.         this.engineAddSelectField(null,fieldSQL,null,false,true);
  158.         this.fieldNames.add(alias);
  159.         return this;
  160.     }
  161.    
  162.     /**
  163.      * Aggiunge un field count alla select, con un alias, del campo <var>field</var> (di tipo Timestamp)
  164.      * es: SELECT min(field) as alias FROM ....
  165.      *
  166.      * @param field Nome del Field
  167.      * @param alias
  168.      */
  169.     @Override
  170.     public ISQLQueryObject addSelectMinTimestampField(String field,String alias) throws SQLQueryObjectException{
  171.         if(field==null)
  172.             throw new SQLQueryObjectException(SQLQueryObjectCore.FIELD_DEVE_ESSERE_DIVERSO_NULL);
  173.         // Trasformo in UNIX_TIMESTAMP
  174.         String fieldSQL = "min("+this.getUnixTimestampConversion(field)+")";
  175.         if(alias != null){
  176.             /**fieldSQL = fieldSQL + " as "+alias;*/
  177.             fieldSQL = fieldSQL + this.getDefaultAliasFieldKeyword() + alias;
  178.         }
  179.         this.engineAddSelectField(null,fieldSQL,null,false,true);
  180.         this.fieldNames.add(alias);
  181.         return this;
  182.     }
  183.    
  184.     /**
  185.      * Aggiunge un field count alla select, con un alias, del campo <var>field</var> (di tipo Timestamp)
  186.      * es: SELECT sum(field) as alias FROM ....
  187.      *
  188.      * @param field Nome del Field
  189.      * @param alias
  190.      */
  191.     @Override
  192.     public ISQLQueryObject addSelectSumTimestampField(String field,String alias) throws SQLQueryObjectException{
  193.         if(field==null)
  194.             throw new SQLQueryObjectException(SQLQueryObjectCore.FIELD_DEVE_ESSERE_DIVERSO_NULL);
  195.         // Trasformo in UNIX_TIMESTAMP
  196.         String fieldSQL = "sum("+this.getUnixTimestampConversion(field)+")";
  197.         if(alias != null){
  198.             /**fieldSQL = fieldSQL + " as "+alias;*/
  199.             fieldSQL = fieldSQL + this.getDefaultAliasFieldKeyword() + alias;
  200.         }
  201.         this.engineAddSelectField(null,fieldSQL,null,false,true);
  202.         this.fieldNames.add(alias);
  203.         return this;
  204.     }
  205.    
  206.    
  207.     /**
  208.      * Aggiunge una tabella di ricerca del from
  209.      * es: SELECT * from ( subSelect )
  210.      *
  211.      * @param subSelect subSelect
  212.      */
  213.     @Override
  214.     public ISQLQueryObject addFromTable(ISQLQueryObject subSelect) throws SQLQueryObjectException{
  215.         StringBuilder bf = new StringBuilder();
  216.         bf.append(" ( ");
  217.         bf.append(subSelect.createSQLQuery());
  218.         bf.append(" ) ");
  219.         this.addFromTable(bf.toString());
  220.         return this;
  221.     }
  222.    
  223.    
  224.    
  225.    
  226.    
  227.     @Override
  228.     protected EscapeSQLConfiguration getEscapeSQLConfiguration(){
  229.        
  230.         EscapeSQLConfiguration config = new EscapeSQLConfiguration();
  231.         config.addCharacter('_');
  232.         config.addCharacter('%');
  233.         config.addCharacter('\\');
  234.         config.setUseEscapeClausole(true);
  235.         config.setEscape('\\');
  236.        
  237.         // special
  238.         config.addCharacterWithOtherEscapeChar('\'','\'');
  239.        
  240.         return config;
  241.     }

  242.    
  243.    
  244.     @Override
  245.     public ISQLQueryObject addSelectForceIndex(String nomeTabella,String indexName) throws SQLQueryObjectException{
  246.         if(nomeTabella==null || "".equals(nomeTabella))
  247.             throw new SQLQueryObjectException("Nome tabela is null or empty string");
  248.         if(indexName==null || "".equals(indexName))
  249.             throw new SQLQueryObjectException("Nome indice is null or empty string");
  250.         String forceIndex = "/*+ index("+nomeTabella+" "+indexName+") */";
  251.         if(this.forceIndexTableNames.contains(forceIndex)){
  252.             throw new SQLQueryObjectException("Forzatura all'utilizzo dell'indice ("+forceIndex+") gia inserito tra le forzature");
  253.         }
  254.         this.forceIndexTableNames.add(forceIndex);
  255.         return this;
  256.     }
  257.    

  258.    
  259.    
  260.    
  261.    
  262.     @Override
  263.     public String getExtractDayFormatFromTimestampFieldPrefix(DayFormatEnum dayFormat) throws SQLQueryObjectException {
  264.         if(dayFormat==null) {
  265.             throw new SQLQueryObjectException("dayFormat undefined");
  266.         }
  267.         if(DayFormatEnum.FULL_DAY_NAME.equals(dayFormat)) {
  268.             return "TRIM(BOTH FROM TO_CHAR(";
  269.         }
  270.         else {
  271.             return super.getExtractDayFormatFromTimestampFieldPrefix(dayFormat);
  272.         }
  273.     }
  274.     @Override
  275.     public String getExtractDayFormatFromTimestampFieldSuffix(DayFormatEnum dayFormat) throws SQLQueryObjectException {
  276.         if(DayFormatEnum.FULL_DAY_NAME.equals(dayFormat)) {
  277.             return super.getExtractDayFormatFromTimestampFieldSuffix(dayFormat) + ")"; // chiusura trim
  278.         }
  279.         else {
  280.             return super.getExtractDayFormatFromTimestampFieldSuffix(dayFormat);
  281.         }
  282.     }
  283.    
  284.    
  285.    
  286.    
  287.    
  288.    
  289.    
  290.    
  291.    
  292.    
  293.     @Override
  294.     public ISQLQueryObject addWhereIsEmptyCondition(String field) throws SQLQueryObjectException{
  295.         // In oracle le stringhe vuote equivalgono a null
  296.         return this.addWhereIsNullCondition(field);
  297.     }
  298.    
  299.     @Override
  300.     public ISQLQueryObject addWhereIsNotEmptyCondition(String field) throws SQLQueryObjectException{
  301.         // In oracle le stringhe vuote equivalgono a null
  302.         return this.addWhereIsNotNullCondition(field);
  303.     }
  304.    
  305.    
  306.    
  307.    
  308.     /**
  309.      * Crea una SQL Query con i dati dell'oggetto
  310.      *
  311.      * @return SQL Query
  312.      */
  313.     @Override
  314.     public String createSQLQuery() throws SQLQueryObjectException{
  315.         return this.createSQLQuery(false);
  316.     }
  317.     private String createSQLQuery(boolean union) throws SQLQueryObjectException{
  318.        
  319.         this.precheckBuildQuery();
  320.        
  321.         StringBuilder bf = new StringBuilder();
  322.        
  323.         bf.append("SELECT ");
  324.        
  325.         // forzatura di indici
  326.         if( !(this.offset>=0 || this.limit>=0) ){
  327.             Iterator<String> itForceIndex = this.forceIndexTableNames.iterator();
  328.             while(itForceIndex.hasNext()){
  329.                 bf.append(" "+itForceIndex.next()+" ");
  330.             }
  331.         }
  332.        
  333.         if(this.isSelectDistinct())
  334.             bf.append(" DISTINCT ");
  335.        
  336.         // select field
  337.         addSQLQuerySelectField(bf);
  338.        
  339.         bf.append(getSQL(false,false,false,union));
  340.        
  341.         /**if( this.offset>=0 || this.limit>=0)
  342.         //  System.out.println("SQL ["+bf.toString()+"]");*/
  343.        
  344.         return bf.toString();
  345.     }
  346.     private void addSQLQuerySelectField(StringBuilder bf) {
  347.         if(this.fields.isEmpty()){
  348.             bf.append("*");
  349.         }else{
  350.             Iterator<String> it = this.fields.iterator();
  351.             boolean first = true;
  352.             while(it.hasNext()){
  353.                 if(!first)
  354.                     bf.append(",");
  355.                 else
  356.                     first = false;

  357.                 String field = it.next();
  358.                 /**if( this.offset>=0 || this.limit>=0){*/
  359.                 // Rilascio vincolo di order by in caso di limit impostato.
  360.                 // Il vincolo rimane per l'offset, per gestire le select annidate di qualche implementazioni come Oracle,SQLServer ...
  361.                 if( this.offset>=0 ){
  362.                    
  363.                     field = this.normalizeField(field, false);

  364.                 }
  365.                 bf.append(field);
  366.             }
  367.         }
  368.     }
  369.    

  370.     /**
  371.      * Crea una SQL per una operazione di Delete con i dati dell'oggetto
  372.      *
  373.      * @return SQL per una operazione di Delete
  374.      */
  375.     @Override
  376.     public String createSQLDeleteEngine() throws SQLQueryObjectException{
  377.        
  378.         StringBuilder bf = new StringBuilder();
  379.                
  380.         bf.append("DELETE ");
  381.        
  382.         bf.append(getSQL(true,false,false,false));
  383.         return bf.toString();
  384.     }
  385.    
  386.    
  387.     /**
  388.      * @return SQL
  389.      * @throws SQLQueryObjectException
  390.      */
  391.     private String getSQL(boolean delete,boolean update,boolean conditions,boolean union) throws SQLQueryObjectException {
  392.         StringBuilder bf = new StringBuilder();
  393.        
  394.         if(this.selectForUpdate){
  395.             this.checkSelectForUpdate(update, delete, union);
  396.         }
  397.        
  398.         if(!update && !conditions){
  399.             // From
  400.             bf.append(SQLQueryObjectCore.FROM_SEPARATOR);
  401.            
  402.            
  403.             // Se e' presente Offset
  404.             /**if( (this.offset>=0 || this.limit>=0) && (delete==false)){*/
  405.             // Rilascio vincolo di order by in caso di limit impostato.
  406.             // Il vincolo rimane per l'offset, per gestire le select annidate di qualche implementazioni come Oracle,SQLServer ...
  407.             if( (this.offset>=0) && (!delete)){
  408.                
  409.                 /**java.util.List<String> aliasOrderByDistinct = new java.util.ArrayList<>();*/
  410.                                
  411.                 if(!this.isSelectDistinct()){
  412.                
  413.                     bf.append(SQLQueryObjectCore.SELECT_SEPARATOR_CON_INIZIO_APERTURA);
  414.                    
  415.                     Iterator<String> itForceIndex = this.forceIndexTableNames.iterator();
  416.                     while(itForceIndex.hasNext()){
  417.                         bf.append(" "+itForceIndex.next()+" ");
  418.                     }
  419.                                    
  420.                     if(this.fields.isEmpty()){
  421.                         Iterator<String> it = this.tableNames.iterator();
  422.                         boolean first = true;
  423.                         while(it.hasNext()){
  424.                             if(!first)
  425.                                 bf.append(",");
  426.                             else
  427.                                 first = false;
  428.                             String t = it.next();
  429.                             bf.append(t+".*");
  430.                         }
  431.                     }else{
  432.                         Iterator<String> it = this.fields.iterator();
  433.                         boolean first = true;
  434.                         while(it.hasNext()){
  435.                             if(!first)
  436.                                 bf.append(",");
  437.                             else
  438.                                 first = false;
  439.                             String f = it.next();
  440.                             bf.append(f);
  441.                         }
  442.                     }
  443.                    
  444.                    
  445.                     bf.append(" , ROW_NUMBER() OVER ( ORDER BY ");
  446.                    
  447.                     // Condizione OrderBy
  448.                     if(this.orderBy.isEmpty()){
  449.                         throw new SQLQueryObjectException(SQLQueryObjectCore.CONDIZIONI_ORDER_BY_RICHESTE);
  450.                     }
  451.                     if(!this.orderBy.isEmpty()){
  452.                         Iterator<String> it = this.orderBy.iterator();
  453.                         boolean first = true;
  454.                         while(it.hasNext()){
  455.                             if(!first)
  456.                                 bf.append(",");
  457.                             else
  458.                                 first = false;
  459.                             String condizione = it.next();
  460.                             if(this.alias.containsKey(condizione)){
  461.                                 bf.append(this.alias.get(condizione));
  462.                             }else{
  463.                                 bf.append(condizione);
  464.                             }
  465.                             boolean sortTypeAsc = this.sortTypeAsc;
  466.                             if(this.orderBySortType.containsKey(condizione)){
  467.                                 sortTypeAsc = this.orderBySortType.get(condizione);
  468.                             }
  469.                             if(sortTypeAsc){
  470.                                 bf.append(SQLQueryObjectCore.ASC_SEPARATOR);
  471.                             }else{
  472.                                 bf.append(SQLQueryObjectCore.DESC_SEPARATOR);
  473.                             }
  474.                         }
  475.                     }
  476.                
  477.                     bf.append(" ) AS rowNumber ");
  478.                    
  479.                
  480.                     bf.append(SQLQueryObjectCore.FROM_SEPARATOR);
  481.                
  482.                                
  483.                     // Table dove effettuare la ricerca 'FromTable'
  484.                     if(this.tables.isEmpty()){
  485.                         throw new SQLQueryObjectException(SQLQueryObjectCore.TABELLA_RICERCA_FROM_NON_DEFINITA);
  486.                     }else{
  487.                         Iterator<String> it = this.tables.iterator();
  488.                         boolean first = true;
  489.                         while(it.hasNext()){
  490.                             if(!first)
  491.                                 bf.append(",");
  492.                             else
  493.                                 first = false;
  494.                             bf.append(it.next());
  495.                         }
  496.                     }
  497.                
  498.                     // Condizioni di Where
  499.                     if(!this.conditions.isEmpty()){
  500.                    
  501.                         bf.append(SQLQueryObjectCore.WHERE_SEPARATOR);
  502.                        
  503.                         if(this.notBeforeConditions){
  504.                             bf.append(SQLQueryObjectCore.NOT_SEPARATOR_APERTURA);
  505.                         }
  506.                        
  507.                         for(int i=0; i<this.conditions.size(); i++){
  508.                            
  509.                             if(i>0){
  510.                                 if(this.andLogicOperator){
  511.                                     bf.append(SQLQueryObjectCore.AND_SEPARATOR);
  512.                                 }else{
  513.                                     bf.append(SQLQueryObjectCore.OR_SEPARATOR);
  514.                                 }
  515.                             }
  516.                             String cond = this.conditions.get(i);              
  517.                             bf.append(cond);
  518.                         }
  519.                        
  520.                         if(this.notBeforeConditions){
  521.                             bf.append(" )");
  522.                         }
  523.                     }
  524.                    
  525.                     // Condizione GroupBy
  526.                     if((!this.getGroupByConditions().isEmpty()) && (!delete)){
  527.                         bf.append(SQLQueryObjectCore.GROUP_BY_SEPARATOR);
  528.                         Iterator<String> it = this.getGroupByConditions().iterator();
  529.                         boolean first = true;
  530.                         while(it.hasNext()){
  531.                             if(!first)
  532.                                 bf.append(",");
  533.                             else
  534.                                 first = false;
  535.                             bf.append(it.next());
  536.                         }
  537.                     }
  538.                    
  539.                     // ForUpdate (Non si può utilizzarlo con offset o limit in oracle)
  540. /**                 if(this.selectForUpdate){
  541. //                      bf.append(" FOR UPDATE ");
  542. //                  }*/
  543.                
  544.                 }else{
  545.                    
  546.                     bf.append(SQLQueryObjectCore.SELECT_SEPARATOR_CON_INIZIO_APERTURA);
  547.                    
  548.                     Iterator<String> itForceIndex = this.forceIndexTableNames.iterator();
  549.                     while(itForceIndex.hasNext()){
  550.                         bf.append(" "+itForceIndex.next()+" ");
  551.                     }
  552.                    
  553.                     if(this.fields.isEmpty()){
  554.                         Iterator<String> it = this.tableNames.iterator();
  555.                         boolean first = true;
  556.                         while(it.hasNext()){
  557.                             if(!first)
  558.                                 bf.append(",");
  559.                             else
  560.                                 first = false;
  561.                             String t = it.next();
  562.                             bf.append(t+".*");
  563.                         }
  564.                     }else{
  565.                         Iterator<String> it = this.fields.iterator();
  566.                         boolean first = true;
  567.                         while(it.hasNext()){
  568.                             if(!first)
  569.                                 bf.append(",");
  570.                             else
  571.                                 first = false;
  572.                             String field = it.next();
  573.                            
  574.                             field = this.normalizeField(field, false);
  575.                            
  576.                             bf.append(field);
  577.                         }
  578.                     }
  579.                    
  580.                     // Cerco inoltre se ci sono delle condizioni di ORDER BY, ed in tal caso le metto come Alias
  581.                     /** NON FUNZIONA
  582.                     int contatoreAlias = 0;
  583.                     if(this.orderBy.size()>0){
  584.                         Iterator<String> it = this.orderBy.iterator();
  585.                         while(it.hasNext()){
  586.                             String fieldOrder = it.next();
  587.                             contatoreAlias++;
  588.                             String a = "ALIASFIELD"+contatoreAlias;
  589.                             aliasOrderByDistinct.add(a);
  590.                             // In questa lista di select field inoltre aggiunto l'alias.
  591.                             if(this.fields.size()==0)
  592.                                 bf.append(a);
  593.                             else
  594.                                 bf.append(","+a);
  595.                             // Verra' aggiunto nelle select piu' interna.
  596.                             //this.fields.add(fieldOrder +" as "+a);
  597.                             this.fields.add(fieldOrder +this.getAliasFieldKeyword()+a);
  598.                         }
  599.                     }
  600.                     */
  601.                    
  602.                     bf.append(" , ROWNUM AS rowNumber");
  603.                
  604.                     bf.append(SQLQueryObjectCore.FROM_SEPARATOR);
  605.                    
  606.                     bf.append(" ( SELECT DISTINCT ");
  607.                    
  608.                         if(this.fields.isEmpty()){
  609.                             bf.append("*");
  610.                         }else{
  611.                             Iterator<String> it = this.fields.iterator();
  612.                             boolean first = true;
  613.                             while(it.hasNext()){
  614.                                 if(!first)
  615.                                     bf.append(",");
  616.                                 else
  617.                                     first = false;
  618.                                 String f = it.next();
  619.                                 bf.append(f);
  620.                             }
  621.                         }
  622.                        
  623.                         bf.append(SQLQueryObjectCore.FROM_SEPARATOR);
  624.                    
  625.                         // Table dove effettuare la ricerca 'FromTable'
  626.                         if(this.tables.isEmpty()){
  627.                             throw new SQLQueryObjectException(SQLQueryObjectCore.TABELLA_RICERCA_FROM_NON_DEFINITA);
  628.                         }else{
  629.                             Iterator<String> it = this.tables.iterator();
  630.                             boolean first = true;
  631.                             while(it.hasNext()){
  632.                                 if(!first)
  633.                                     bf.append(",");
  634.                                 else
  635.                                     first = false;
  636.                                 bf.append(it.next());
  637.                             }
  638.                         }
  639.                    
  640.                         bf.append(" ");
  641.                        
  642.                         // Condizioni di Where
  643.                         if(!this.conditions.isEmpty()){
  644.                        
  645.                             bf.append(SQLQueryObjectCore.WHERE_SEPARATOR);
  646.                            
  647.                             if(this.notBeforeConditions){
  648.                                 bf.append(SQLQueryObjectCore.NOT_SEPARATOR_APERTURA);
  649.                             }
  650.                            
  651.                             for(int i=0; i<this.conditions.size(); i++){
  652.                                
  653.                                 if(i>0){
  654.                                     if(this.andLogicOperator){
  655.                                         bf.append(SQLQueryObjectCore.AND_SEPARATOR);
  656.                                     }else{
  657.                                         bf.append(SQLQueryObjectCore.OR_SEPARATOR);
  658.                                     }
  659.                                 }
  660.                                 String cond = this.conditions.get(i);              
  661.                                 bf.append(cond);
  662.                             }
  663.                            
  664.                             if(this.notBeforeConditions){
  665.                                 bf.append(" )");
  666.                             }
  667.                         }
  668.                        
  669.                         // Condizione GroupBy
  670.                         if((!this.getGroupByConditions().isEmpty()) && (!delete)){
  671.                             bf.append(SQLQueryObjectCore.GROUP_BY_SEPARATOR);
  672.                             Iterator<String> it = this.getGroupByConditions().iterator();
  673.                             boolean first = true;
  674.                             while(it.hasNext()){
  675.                                 if(!first)
  676.                                     bf.append(",");
  677.                                 else
  678.                                     first = false;
  679.                                 bf.append(it.next());
  680.                             }
  681.                         }
  682.                        
  683.                         // Condizione OrderBy
  684.                         if(this.orderBy.isEmpty()){
  685.                             throw new SQLQueryObjectException(SQLQueryObjectCore.CONDIZIONI_ORDER_BY_RICHESTE);
  686.                         }
  687.                         if(!this.orderBy.isEmpty()){
  688.                             bf.append(SQLQueryObjectCore.ORDER_BY_SEPARATOR);
  689.                             Iterator<String> it = this.orderBy.iterator();
  690.                             boolean first = true;
  691.                             while(it.hasNext()){
  692.                                 String column = it.next();
  693.                                 if(!first)
  694.                                     bf.append(",");
  695.                                 else
  696.                                     first = false;
  697.                                 bf.append(column);
  698.                                 boolean sortTypeAsc = this.sortTypeAsc;
  699.                                 if(this.orderBySortType.containsKey(column)){
  700.                                     sortTypeAsc = this.orderBySortType.get(column);
  701.                                 }
  702.                                 if(sortTypeAsc){
  703.                                     bf.append(SQLQueryObjectCore.ASC_SEPARATOR);
  704.                                 }else{
  705.                                     bf.append(SQLQueryObjectCore.DESC_SEPARATOR);
  706.                                 }
  707.                             }
  708.                         }
  709.                        
  710.                     bf.append(" ) ");
  711.                    
  712.                 }
  713.                
  714.                 bf.append(" ) WHERE ( ");
  715.                 if(this.offset>=0){
  716.                     bf.append(" rowNumber > ");
  717.                     bf.append(this.offset);
  718.                 }
  719.                 if(this.limit>=0){
  720.                     if(this.offset>=0)
  721.                         bf.append(" AND");
  722.                     bf.append(" rowNumber <=  ");
  723.                     if(this.offset>=0)
  724.                         bf.append(this.offset+this.limit);
  725.                     else
  726.                         bf.append(this.limit);
  727.                 }
  728.                 bf.append(" )");
  729.                
  730.                
  731.                 // ORDER BY FINALE
  732.                 if(!union &&
  733.                     (!this.orderBy.isEmpty())
  734.                     ){
  735.                     bf.append(SQLQueryObjectCore.ORDER_BY_SEPARATOR);
  736.                     Iterator<String> it = this.orderBy.iterator();
  737.                     boolean first = true;
  738.                     while(it.hasNext()){
  739.                         if(!first)
  740.                             bf.append(",");
  741.                         else
  742.                             first = false;
  743.                         String originalField = it.next();
  744.                        
  745.                         String field = this.normalizeField(originalField);
  746.                        
  747.                         bf.append(field);
  748.                        
  749.                         boolean sortTypeAsc = this.sortTypeAsc;
  750.                         if(this.orderBySortType.containsKey(originalField)){
  751.                             sortTypeAsc = this.orderBySortType.get(originalField);
  752.                         }
  753.                         if(sortTypeAsc){
  754.                             bf.append(SQLQueryObjectCore.ASC_SEPARATOR);
  755.                         }else{
  756.                             bf.append(SQLQueryObjectCore.DESC_SEPARATOR);
  757.                         }
  758.                     }
  759.                 }
  760.                
  761.                 /**
  762.                  * OLD ALIAS
  763.                 if(aliasOrderByDistinct.size()>0){
  764.                     bf.append(SQLQueryObjectCore.ORDER_BY_SEPARATOR);
  765.                     Iterator<String> it = aliasOrderByDistinct.iterator();
  766.                     boolean first = true;
  767.                     while(it.hasNext()){
  768.                         if(!first)
  769.                             bf.append(",");
  770.                         else
  771.                             first = false;
  772.                         bf.append(it.next());
  773.                     }
  774.                     if(this.sortTypeAsc){
  775.                         bf.append(SQLQueryObjectCore.ASC_SEPARATOR);
  776.                     }else{
  777.                         bf.append(SQLQueryObjectCore.DESC_SEPARATOR);
  778.                     }
  779.                 }
  780.                 */
  781.                
  782.             }else{
  783.                
  784.                 // Offset non presente
  785.                
  786.                 // Table dove effettuare la ricerca 'FromTable'
  787.                 if(this.tables.isEmpty()){
  788.                     throw new SQLQueryObjectException(SQLQueryObjectCore.TABELLA_RICERCA_FROM_NON_DEFINITA);
  789.                 }else{
  790.                    
  791.                     if(delete && this.tables.size()>2)
  792.                         throw new SQLQueryObjectException("Non e' possibile effettuare una delete con piu' di una tabella alla volta");
  793.                    
  794.                     Iterator<String> it = this.tables.iterator();
  795.                     boolean first = true;
  796.                     while(it.hasNext()){
  797.                         if(!first)
  798.                             bf.append(",");
  799.                         else
  800.                             first = false;
  801.                         bf.append(it.next());
  802.                     }
  803.                 }
  804.                
  805.                 // Condizioni di Where
  806.                 if(!this.conditions.isEmpty()){
  807.                     bf.append(SQLQueryObjectCore.WHERE_SEPARATOR);
  808.                    
  809.                     if(this.notBeforeConditions){
  810.                         bf.append("NOT (");
  811.                     }
  812.                    
  813.                     for(int i=0; i<this.conditions.size(); i++){
  814.                         if(i>0){
  815.                             if(this.andLogicOperator){
  816.                                 bf.append(SQLQueryObjectCore.AND_SEPARATOR);
  817.                             }else{
  818.                                 bf.append(SQLQueryObjectCore.OR_SEPARATOR);
  819.                             }
  820.                         }
  821.                         bf.append(this.conditions.get(i));
  822.                     }
  823.                    
  824.                     if(this.notBeforeConditions){
  825.                         bf.append(")");
  826.                     }
  827.                    
  828.                 }
  829.                        
  830.                 // LIMIT
  831.                 if(this.limit>=0){
  832.                     if(this.conditions.isEmpty()){
  833.                         bf.append(SQLQueryObjectCore.WHERE_SEPARATOR);
  834.                     }else{
  835.                         bf.append(SQLQueryObjectCore.AND_SEPARATOR);
  836.                     }
  837.                     bf.append(" ( rownum <= ");
  838.                     bf.append(this.limit);
  839.                     bf.append(" ) ");
  840.                 }
  841.                
  842.                 // Condizione GroupBy
  843.                 if((!this.getGroupByConditions().isEmpty()) && (!delete)){
  844.                     bf.append(SQLQueryObjectCore.GROUP_BY_SEPARATOR);
  845.                     Iterator<String> it = this.getGroupByConditions().iterator();
  846.                     boolean first = true;
  847.                     while(it.hasNext()){
  848.                         if(!first)
  849.                             bf.append(",");
  850.                         else
  851.                             first = false;
  852.                         bf.append(it.next());
  853.                     }
  854.                 }
  855.            
  856.                 // Condizione OrderBy
  857.                 if(!union &&
  858.                     ((!this.orderBy.isEmpty()) && (!delete))
  859.                     ){
  860.                     bf.append(SQLQueryObjectCore.ORDER_BY_SEPARATOR);
  861.                     Iterator<String> it = this.orderBy.iterator();
  862.                     boolean first = true;
  863.                     while(it.hasNext()){
  864.                         String column = it.next();
  865.                         if(!first)
  866.                             bf.append(",");
  867.                         else
  868.                             first = false;
  869.                         bf.append(column);
  870.                         boolean sortTypeAsc = this.sortTypeAsc;
  871.                         if(this.orderBySortType.containsKey(column)){
  872.                             sortTypeAsc = this.orderBySortType.get(column);
  873.                         }
  874.                         if(sortTypeAsc){
  875.                             bf.append(SQLQueryObjectCore.ASC_SEPARATOR);
  876.                         }else{
  877.                             bf.append(SQLQueryObjectCore.DESC_SEPARATOR);
  878.                         }
  879.                     }
  880.                 }
  881.                
  882.                 // ForUpdate
  883.                 if(this.selectForUpdate){
  884.                     bf.append(" FOR UPDATE ");
  885.                 }
  886.             }
  887.            
  888.         }else{
  889.            
  890.             // UPDATE, conditions
  891.            
  892.             // Condizioni di Where
  893.             if(!this.conditions.isEmpty()){
  894.                
  895.                 if(!conditions)
  896.                     bf.append(SQLQueryObjectCore.WHERE_SEPARATOR);
  897.                
  898.                 if(this.notBeforeConditions){
  899.                     bf.append("NOT (");
  900.                 }
  901.                
  902.                 for(int i=0; i<this.conditions.size(); i++){
  903.                     if(i>0){
  904.                         if(this.andLogicOperator){
  905.                             bf.append(SQLQueryObjectCore.AND_SEPARATOR);
  906.                         }else{
  907.                             bf.append(SQLQueryObjectCore.OR_SEPARATOR);
  908.                         }
  909.                     }
  910.                     bf.append(this.conditions.get(i));
  911.                 }
  912.                
  913.                 if(this.notBeforeConditions){
  914.                     bf.append(")");
  915.                 }
  916.                
  917.             }
  918.            
  919.             // Non genero per le condizioni, per update viene sollevata eccezione prima
  920.             // ForUpdate
  921. /**         if(this.selectForUpdate){
  922. //              bf.append(" FOR UPDATE ");
  923. //          }*/
  924.         }
  925.        
  926.         return bf.toString();
  927.     }
  928.    
  929.     /**
  930.      * Crea la stringa SQL per l'unione dei parametri presi in sqlQueryObject
  931.      * Il booleano 'unionAll' indica se i vari parametri devono essere uniti con 'UNION'  o con 'UNION ALL'
  932.      *
  933.      * In caso nell'oggetto viene impostato il LIMIT o OFFSET, questo viene utilizzato per effettuare LIMIT e/o OFFSET dell'unione
  934.      *
  935.      * In caso l'ORDER by viene impostato nell'oggetto, questo viene utilizzato per effettuare ORDER BY dell'unione
  936.      *
  937.      *
  938.      * Sintassi OracleSQL:
  939.      *
  940.      * SELECT * from
  941.      * (
  942.      * (  SELECT fields... FROM tabella WHERE conditions  )
  943.      * UNION [ALL]
  944.      * (  SELECT fields... FROM tabella WHERE conditions  )
  945.      * )
  946.      * WHERE ROWNUM&lt;=LIMIT+OFFSET AND ROWNUM&gt;OFFSET
  947.      * ORDER BY gdo,tipo_fruitore,fruitore,tipo_erogatore,erogatore,tipo_servizio,servizio DESC  
  948.      *
  949.      *
  950.      * @param unionAll
  951.      * @param sqlQueryObject
  952.      * @return stringa SQL per l'unione dei parametri presi in sqlQueryObject
  953.      * @throws SQLQueryObjectException
  954.      */
  955.     @Override
  956.     public String createSQLUnion(boolean unionAll,ISQLQueryObject... sqlQueryObject) throws SQLQueryObjectException{
  957.                
  958.         // Controllo parametro su cui effettuare la UNION
  959.         this.checkUnionField(false,sqlQueryObject);
  960.        
  961.         if(this.selectForUpdate){
  962.             this.checkSelectForUpdate(false, false, true);
  963.         }
  964.        
  965.         StringBuilder bf = new StringBuilder();
  966.        
  967.         bf.append("SELECT ");
  968.        
  969. //      // Non ha senso, la union fa gia la distinct, a meno di usare la unionAll ma in quel caso non si vuole la distinct
  970. /**     // if(this.isSelectDistinct())
  971. //      //  bf.append(" DISTINCT ");*/
  972.        
  973.        
  974.         if( this.offset<0 ){
  975.            
  976.             Iterator<String> itForceIndex = this.forceIndexTableNames.iterator();
  977.             while(itForceIndex.hasNext()){
  978.                 bf.append(" "+itForceIndex.next()+" ");
  979.             }
  980.            
  981.             if(this.fields.isEmpty()){
  982.                 bf.append("* ");
  983.             }else{
  984.                 Iterator<String> it = this.fields.iterator();
  985.                 boolean first = true;
  986.                 while(it.hasNext()){
  987.                     if(!first)
  988.                         bf.append(",");
  989.                     else
  990.                         first = false;
  991.                     String f = it.next();
  992.                     bf.append(f);
  993.                 }
  994.                 bf.append(" ");
  995.             }
  996.    
  997.         }
  998.         else{
  999.             bf.append("* ");
  1000.         }
  1001.    
  1002.         bf.append(SQLQueryObjectCore.FROM_SEPARATOR);
  1003.        
  1004.         // Se e' presente Offset
  1005.         /**if( (this.offset>=0 || this.limit>=0) ){*/
  1006.         // Rilascio vincolo di order by in caso di limit impostato.
  1007.         // Il vincolo rimane per l'offset, per gestire le select annidate di qualche implementazioni come Oracle,SQLServer ...
  1008.         if( this.offset>=0 ){
  1009.            
  1010.             bf.append(SQLQueryObjectCore.SELECT_SEPARATOR_CON_INIZIO_APERTURA);
  1011.            
  1012.             Iterator<String> itForceIndex = this.forceIndexTableNames.iterator();
  1013.             while(itForceIndex.hasNext()){
  1014.                 bf.append(" "+itForceIndex.next()+" ");
  1015.             }
  1016.            
  1017.             if(this.fields.isEmpty()){
  1018.                 Iterator<String> it = this.tableNames.iterator();
  1019.                 boolean first = true;
  1020.                 while(it.hasNext()){
  1021.                     if(!first)
  1022.                         bf.append(",");
  1023.                     else
  1024.                         first = false;
  1025.                     String t = it.next();
  1026.                     bf.append(t+".*");
  1027.                 }
  1028.             }else{
  1029.                 Iterator<String> it = this.fields.iterator();
  1030.                 boolean first = true;
  1031.                 while(it.hasNext()){
  1032.                     if(!first)
  1033.                         bf.append(",");
  1034.                     else
  1035.                         first = false;
  1036.                     String f = it.next();
  1037.                     bf.append(f);
  1038.                 }
  1039.             }
  1040.            
  1041.             bf.append(" , ROW_NUMBER() OVER ( ORDER BY ");
  1042.            
  1043.             // Condizione OrderBy
  1044.             if(this.orderBy.isEmpty()){
  1045.                 throw new SQLQueryObjectException(SQLQueryObjectCore.CONDIZIONI_ORDER_BY_RICHESTE);
  1046.             }
  1047.             if(!this.orderBy.isEmpty()){
  1048.                 Iterator<String> it = this.orderBy.iterator();
  1049.                 boolean first = true;
  1050.                 while(it.hasNext()){
  1051.                     if(!first)
  1052.                         bf.append(",");
  1053.                     else
  1054.                         first = false;
  1055.                     String condizione = it.next();
  1056.                     if(this.alias.containsKey(condizione)){
  1057.                         bf.append(this.alias.get(condizione));
  1058.                     }else{
  1059.                         bf.append(condizione);
  1060.                     }
  1061.                     boolean sortTypeAsc = this.sortTypeAsc;
  1062.                     if(this.orderBySortType.containsKey(condizione)){
  1063.                         sortTypeAsc = this.orderBySortType.get(condizione);
  1064.                     }
  1065.                     if(sortTypeAsc){
  1066.                         bf.append(SQLQueryObjectCore.ASC_SEPARATOR);
  1067.                     }else{
  1068.                         bf.append(SQLQueryObjectCore.DESC_SEPARATOR);
  1069.                     }
  1070.                 }
  1071.             }
  1072.            
  1073.             bf.append(" ) AS rowNumber ");
  1074.            
  1075.            
  1076.             // Table dove effettuare la ricerca 'FromTable'
  1077.             bf.append(SQLQueryObjectCore.FROM_SEPARATOR_APERTURA);
  1078.            
  1079.             for(int i=0; i<sqlQueryObject.length; i++){
  1080.                
  1081.                 if(((OracleQueryObject)sqlQueryObject[i]).selectForUpdate){
  1082.                     try{
  1083.                         ((OracleQueryObject)sqlQueryObject[i]).checkSelectForUpdate(false, false, true);
  1084.                     }catch(Exception e){
  1085.                         throw new SQLQueryObjectException("Parametro SqlQueryObject["+i+"] non valido: "+e.getMessage());
  1086.                     }
  1087.                 }
  1088.                
  1089.                 if(i>0){
  1090.                     bf.append(" UNION ");
  1091.                     if(unionAll){
  1092.                         bf.append(" ALL ");
  1093.                     }
  1094.                 }
  1095.                
  1096.                 bf.append("( ");
  1097.                
  1098.                 bf.append(((OracleQueryObject)sqlQueryObject[i]).createSQLQuery(true));
  1099.                
  1100.                 bf.append(") ");
  1101.             }
  1102.            
  1103.             bf.append(" ) ");
  1104.            
  1105.             // Condizioni di Where
  1106.             if(!this.conditions.isEmpty()){
  1107.            
  1108.                 bf.append(SQLQueryObjectCore.WHERE_SEPARATOR);
  1109.                
  1110.                 if(this.notBeforeConditions){
  1111.                     bf.append(SQLQueryObjectCore.NOT_SEPARATOR_APERTURA);
  1112.                 }
  1113.                
  1114.                 for(int i=0; i<this.conditions.size(); i++){
  1115.                    
  1116.                     if(i>0){
  1117.                         if(this.andLogicOperator){
  1118.                             bf.append(SQLQueryObjectCore.AND_SEPARATOR);
  1119.                         }else{
  1120.                             bf.append(SQLQueryObjectCore.OR_SEPARATOR);
  1121.                         }
  1122.                     }
  1123.                     String cond = this.conditions.get(i);
  1124.                     bf.append(cond);
  1125.                 }
  1126.                
  1127.                 if(this.notBeforeConditions){
  1128.                     bf.append(" )");
  1129.                 }
  1130.             }
  1131.            
  1132.             // Condizione GroupBy
  1133.             if((!this.getGroupByConditions().isEmpty())){
  1134.                 bf.append(SQLQueryObjectCore.GROUP_BY_SEPARATOR);
  1135.                 Iterator<String> it = this.getGroupByConditions().iterator();
  1136.                 boolean first = true;
  1137.                 while(it.hasNext()){
  1138.                     if(!first)
  1139.                         bf.append(",");
  1140.                     else
  1141.                         first = false;
  1142.                     bf.append(it.next());
  1143.                 }
  1144.             }
  1145.            
  1146.             bf.append(" ) WHERE ( ");
  1147.             if(this.offset>=0){
  1148.                 bf.append(" rowNumber > ");
  1149.                 bf.append(this.offset);
  1150.             }
  1151.             if(this.limit>=0){
  1152.                 if(this.offset>=0)
  1153.                     bf.append(" AND");
  1154.                 bf.append(" rowNumber <=  ");
  1155.                 if(this.offset>=0)
  1156.                     bf.append(this.offset+this.limit);
  1157.                 else
  1158.                     bf.append(this.limit);
  1159.             }
  1160.             bf.append(" )");
  1161.        
  1162.        
  1163.         }else{
  1164.            
  1165.             // no offset
  1166.            
  1167.             bf.append(" ( ");
  1168.            
  1169.             for(int i=0; i<sqlQueryObject.length; i++){
  1170.                
  1171.                 if(((OracleQueryObject)sqlQueryObject[i]).selectForUpdate){
  1172.                     try{
  1173.                         ((OracleQueryObject)sqlQueryObject[i]).checkSelectForUpdate(false, false, true);
  1174.                     }catch(Exception e){
  1175.                         throw new SQLQueryObjectException("Parametro SqlQueryObject["+i+"] non valido: "+e.getMessage());
  1176.                     }
  1177.                 }
  1178.                
  1179.                 if(i>0){
  1180.                     bf.append(" UNION ");
  1181.                     if(unionAll){
  1182.                         bf.append(" ALL ");
  1183.                     }
  1184.                 }
  1185.                
  1186.                 bf.append("( ");
  1187.                
  1188.                 bf.append(((OracleQueryObject)sqlQueryObject[i]).createSQLQuery(true));
  1189.                
  1190.                 bf.append(") ");
  1191.             }
  1192.            
  1193.             bf.append(") ");

  1194.             // LIMIT
  1195.             if(this.limit>=0){
  1196.                 bf.append(SQLQueryObjectCore.WHERE_SEPARATOR);
  1197.                 bf.append(" ( rownum <= ");
  1198.                 bf.append(this.limit);
  1199.                 bf.append(" ) ");
  1200.             }  
  1201.            
  1202.             // Condizione GroupBy
  1203.             if((!this.getGroupByConditions().isEmpty())){
  1204.                 bf.append(SQLQueryObjectCore.GROUP_BY_SEPARATOR);
  1205.                 Iterator<String> it = this.getGroupByConditions().iterator();
  1206.                 boolean first = true;
  1207.                 while(it.hasNext()){
  1208.                     if(!first)
  1209.                         bf.append(",");
  1210.                     else
  1211.                         first = false;
  1212.                     bf.append(it.next());
  1213.                 }
  1214.             }
  1215.            
  1216.             // Condizione OrderBy
  1217.             if(!this.orderBy.isEmpty()){
  1218.                 bf.append(SQLQueryObjectCore.ORDER_BY_SEPARATOR);
  1219.                 Iterator<String> it = this.orderBy.iterator();
  1220.                 boolean first = true;
  1221.                 while(it.hasNext()){
  1222.                     String column = it.next();
  1223.                     if(!first)
  1224.                         bf.append(",");
  1225.                     else
  1226.                         first = false;
  1227.                     bf.append(column);
  1228.                     boolean sortTypeAsc = this.sortTypeAsc;
  1229.                     if(this.orderBySortType.containsKey(column)){
  1230.                         sortTypeAsc = this.orderBySortType.get(column);
  1231.                     }
  1232.                     if(sortTypeAsc){
  1233.                         bf.append(SQLQueryObjectCore.ASC_SEPARATOR);
  1234.                     }else{
  1235.                         bf.append(SQLQueryObjectCore.DESC_SEPARATOR);
  1236.                     }
  1237.                 }
  1238.             }
  1239.         }
  1240.        
  1241. /**     bf.append(" ) ");*/
  1242.        
  1243.         return bf.toString();
  1244.     }
  1245.    
  1246.    
  1247.     /**
  1248.      * Crea la stringa SQL per l'unione dei parametri presi in sqlQueryObject (viene effettuato il count)
  1249.      * Il booleano 'unionAll' indica se i vari parametri devono essere uniti con 'UNION'  o con 'UNION ALL'
  1250.      *
  1251.      * Sintassi OracleSQL:
  1252.      *
  1253.      * select count(*) as aliasCount FROM (
  1254.      *
  1255.      * (  SELECT fields... FROM tabella WHERE conditions  )
  1256.      * UNION [ALL]
  1257.      * (  SELECT fields... FROM tabella WHERE conditions  )
  1258.      *
  1259.      * )
  1260.      *
  1261.      * @param unionAll
  1262.      * @param sqlQueryObject
  1263.      * @return stringa SQL per l'unione dei parametri presi in sqlQueryObject (viene effettuato il count)
  1264.      * @throws SQLQueryObjectException
  1265.      */
  1266.     @Override
  1267.     public String createSQLUnionCount(boolean unionAll,String aliasCount,ISQLQueryObject... sqlQueryObject) throws SQLQueryObjectException{
  1268.        
  1269.         // Controllo parametro su cui effettuare la UNION
  1270.         this.checkUnionField(true,sqlQueryObject);
  1271.        
  1272.         if(aliasCount==null){
  1273.             throw new SQLQueryObjectException("Alias per il count non definito");
  1274.         }
  1275.        
  1276.         StringBuilder bf = new StringBuilder();
  1277.        
  1278.         bf.append("SELECT count(*) "+this.getDefaultAliasFieldKeyword()+" ");
  1279.         bf.append(aliasCount);
  1280.         bf.append(SQLQueryObjectCore.FROM_SEPARATOR_APERTURA);
  1281.        
  1282.         bf.append( this.createSQLUnion(unionAll, sqlQueryObject) );
  1283.        
  1284.         bf.append(") ");
  1285.        
  1286.         return bf.toString();
  1287.        
  1288.     }
  1289.    
  1290.    
  1291.    
  1292.     /**
  1293.      * Crea una SQL per una operazione di Update con i dati dell'oggetto
  1294.      *
  1295.      * @return SQL per una operazione di Update
  1296.      */
  1297.     @Override
  1298.     public String createSQLUpdateEngine() throws SQLQueryObjectException{
  1299.        
  1300.         StringBuilder bf = new StringBuilder();
  1301.         bf.append("UPDATE ");
  1302.         bf.append(this.updateTable);
  1303.         bf.append(" SET ");
  1304.         for(int i=0; i<this.updateFieldsName.size(); i++){
  1305.             if(i>0)
  1306.                 bf.append(" , ");
  1307.             bf.append(this.updateFieldsName.get(i));
  1308.             bf.append(" = ");
  1309.             bf.append(this.updateFieldsValue.get(i));
  1310.         }
  1311.         bf.append(getSQL(false,true,false,false));
  1312.         return bf.toString();
  1313.     }

  1314.    
  1315.    
  1316.    
  1317.    
  1318.    
  1319.    
  1320.     /* ---------------- WHERE CONDITIONS ------------------ */
  1321.    
  1322.     /**
  1323.      * Crea le condizioni presenti senza anteporre il comando (INSERT,CREATE,UPDATE), le tabelle interessate e il 'WHERE'
  1324.      *
  1325.      * @return SQL Query
  1326.      */
  1327.     @Override
  1328.     public String createSQLConditionsEngine() throws SQLQueryObjectException{
  1329.        
  1330.         StringBuilder bf = new StringBuilder();
  1331.         bf.append(getSQL(false,false,true,false));
  1332.         return bf.toString();
  1333.     }
  1334.    
  1335.    
  1336.    
  1337. }