Ver Mensaje Individual
  #2 (permalink)  
Antiguo 29/09/2016, 22:58
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

Hola a todos, les cuento que estuve trabajando en el select genérico y este es el resultado:

Código Java:
Ver original
  1. public String select(String[] cols, String[] tables, String style, String[] where){
  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. }
  14. if(where != null){
  15. Consulta += " where ";
  16. for(String w : where){
  17. Consulta += w + " = ?";
  18. if(!where[where.length -1].equals(w)){
  19. Consulta += " and ";
  20. }
  21. }
  22. }
  23. return Consulta;
  24. }
  25. private String createJoin(String style,String[] tables){
  26. String join = " ";
  27. if(tables != null){
  28. for(String t : tables){
  29. if(!t.equals(this.getTable())){
  30. char ps = t.charAt(0);
  31. join += style + " join " + t + " " + ps + " on" + this.createCombos(tables);
  32. }
  33. }
  34. }
  35. return join;
  36. }
  37. private String[] prepareTables(String[] tables){
  38. String[] tab = new String[tables.length +1];
  39. tab[0] = this.getTable();
  40. for(int i = 0; i < tables.length; i++){
  41. tab[i+1] = tables[i];
  42. }
  43. return tab;
  44. }
  45. private String createCombos(String[] tables){
  46. String combo = " ";
  47. for(int i = 0; i < tables.length -1; i++){
  48. char fk = tables[i+1].charAt(0);
  49. char pk = tables[i].charAt(0);
  50. LinkedList<String> keys = this.objbd.getPrimaryKeys(tables[i+1]);
  51. LinkedList<String> fays = this.objbd.getForeignKeys(tables[i]);
  52. for(String f : fays){
  53. for(String k : keys){
  54. if(k.equals(f)){
  55. combo += fk +"."+f +" = "+pk +"."+k;
  56. }
  57. }
  58. }
  59. }
  60. return combo;
  61. }
  62. private String createColumns(String[] cols,String[] tables){
  63. String columns = "";
  64. for(String c : cols){
  65. if(tables == null){
  66. columns += c;
  67. } else {
  68. for(String t : tables){
  69. char s = t.charAt(0);
  70. LinkedList<String>temp = this.objbd.getColumns(t);
  71. for(String c1 : temp){
  72. if(c1.equals(c)){
  73. columns += s + "." + c;
  74. }
  75. }
  76. }
  77. }
  78. if(!cols[cols.length -1].equals(c)){
  79. columns += ",";
  80. } else {
  81. columns += " ";
  82. }
  83. }
  84. return columns;
  85. }

lo que me faltaría es ver bien las condiciones, los order y group by, además debería probar con los renombramientos de sql xq de momento esta sólo obteniendo la primer letra cosa que no es muy buena idea, ya que si hay otra tabla con la misma letra inicial, la consulta queda fastidiada, no sé que me sugieren?