Ver Mensaje Individual
  #3 (permalink)  
Antiguo 30/09/2016, 22:22
Avatar de detective_jd
detective_jd
 
Fecha de Ingreso: abril-2011
Ubicación: Salto
Mensajes: 437
Antigüedad: 13 años
Puntos: 6
Respuesta: Automatizar SQL con DatabaseMetaData

Mejoré un poco la cosa:

Código Java:
Ver original
  1. public String select(String[] cols, String[] tables, String[] style, String[] where, String[] conditions,String[] parts){
  2.         String Consulta = "select ";
  3.         if(cols == null){
  4.             Consulta += "* ";
  5.         } else {
  6.             Consulta += this.createColumns(cols, tables);
  7.         }
  8.         Consulta += "from " + this.getTable();        
  9.         if(tables != null){
  10.             String[] tab = this.prepareTables(tables);
  11.             Consulta += " " + this.getTable().charAt(0);            
  12.             Consulta += this.createJoin(style,tab);
  13.             if(where != null && conditions != null){
  14.                 Consulta += " " + this.createConditions(tab,where, conditions, parts);
  15.             }
  16.         }        
  17.         return Consulta;
  18.     }
  19.     private String createJoin(String[] style,String[] tables){
  20.         String join = " ";
  21.         if(tables != null && style != null){
  22.             for(int i = 0; i < tables.length; i++){
  23.                 if(!tables[i].equals(this.getTable())){
  24.                     String ps = "" + tables[i].charAt(0);
  25.                     String pp = "" + tables[i -1 ].charAt(0);
  26.                     if(ps.equals(pp)){
  27.                         ps += tables[i].charAt(1);
  28.                     }
  29.                     join += style[i-1] + " join " + tables[i] + " " + ps + " on" + this.createCombos(tables[i],tables[i -1]);
  30.                     if(!tables[tables.length -1].equals(tables[i])){
  31.                         join += " ";
  32.                     }
  33.                 }
  34.             }
  35.         }
  36.         return join;
  37.     }
  38.     private String createCombos(String emisor, String receptor){
  39.         String combo = " ";
  40.         String fk = "" + emisor.charAt(0);
  41.         String pk = "" + receptor.charAt(0);
  42.         if(fk.equals(pk)){
  43.             fk += receptor.charAt(1);
  44.         }
  45.         LinkedList<String> keys = this.objbd.getPrimaryKeys(emisor);
  46.         LinkedList<String> fays = this.objbd.getForeignKeys(receptor);
  47.         for(String f : fays){
  48.             for(String k : keys){
  49.                 if(k.equals(f)){
  50.                     combo += pk +"."+k +" = "+fk +"."+f;
  51.                     break;
  52.                 }                                    
  53.             }
  54.         }                        
  55.         return combo;
  56.     }
  57.     private String[] prepareTables(String[] tables){
  58.         String[] tab = new String[tables.length +1];
  59.         tab[0] = this.getTable();
  60.         for(int i = 0; i < tables.length; i++){
  61.             tab[i+1] = tables[i];
  62.         }
  63.         return tab;
  64.     }  
  65.     private String createColumns(String[] cols,String[] tables){
  66.         String columns = "";
  67.         for(String c : cols){
  68.             if(tables == null){
  69.                 columns += c;
  70.             } else {
  71.                 for(int i = 0; i < tables.length; i++){
  72.                     String ps = "" + tables[i].charAt(0);
  73.                     if(tables.length > 1 && i > 0){
  74.                         String pp = "" + tables[i-1].charAt(0);
  75.                          if(ps.equals(pp)){
  76.                             ps += tables[i].charAt(1);
  77.                         }
  78.                     }                    
  79.                     LinkedList<String>temp = this.objbd.getColumns(tables[i]);
  80.                     for(String c1 : temp){
  81.                         if(c1.equals(c)){
  82.                             columns += ps + "." + c;
  83.                         }
  84.                     }
  85.                 }                
  86.             }            
  87.             if(!cols[cols.length -1].equals(c)){
  88.                 columns += ",";
  89.             } else {
  90.                 columns += " ";
  91.             }
  92.         }
  93.         return columns;
  94.     }
  95.     private String createConditions(String[] tables, String[] where, String[] conditions, String[] parts){
  96.         String condition = "where ";
  97.         for(int i = 0 ; i < where.length; i++){
  98.             if(tables != null){
  99.                 for(int j = 0; j < tables.length; j++){
  100.                     String ps = "" + tables[j].charAt(0);
  101.                     if(tables.length > 1 && j > 0){
  102.                         String pp = "" + tables[j-1].charAt(0);
  103.                          if(ps.equals(pp)){
  104.                             ps += tables[j].charAt(1);
  105.                         }
  106.                     }
  107.                     LinkedList<String>temp = this.objbd.getColumns(tables[j]);
  108.                     for(String c1 : temp){
  109.                         if(c1.equals(where[i])){
  110.                             where[i] = ps + "." + where[i];
  111.                         }
  112.                     }
  113.                 }                
  114.             }
  115.         }
  116.         for(int i = 0 ; i < where.length; i++){
  117.             if(conditions[i].equals("between") || conditions[i].equals("not between")){
  118.                 condition += where[i] + " " + conditions[i] + " ? and ?";
  119.             }else if(conditions[i].equals("not null") || conditions[i].equals("null")){
  120.                 condition += where[i] + " " + conditions[i];
  121.             } else {
  122.                 condition += where[i] + " " + conditions[i] + " ?";            
  123.             }
  124.             if(!where[where.length -1].equals(where[i])){
  125.                 condition += " " + parts[i] + " ";
  126.             }
  127.         }        
  128.         return condition;
  129.     }

pero no me pasa que para el group by y order by es parecido a lo anterior, ¿quería preguntar cómo mejorar los renombramientos? ya que hice que para que no me duplique que en tabla que le haga el join me concatene su segunda letra pero eso no funcionará por mucho, además me quedé pensando cómo hacer si tengo subconsultas?? ahí no sé que hacer.

espero sus respuestas y saludos.