Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- esta es la parte del código en Model.java, lo que respectan los select automatizados:
- public String select(String[] cols, String[][] tables, String[][] where,String[] group,String[][] having,String[][] order){
- String Consulta = "select ";
- if(cols == null){
- Consulta += "* ";
- } else {
- Consulta += this.createColumns(cols, tables);
- }
- LinkedList<String[]> tab = this.prepareTables(tables);
- Consulta += this.createFrom(tab);
- if(where != null){
- Consulta += " " + this.createConditions(tab,where);
- }
- if(group != null){
- Consulta += " " + this.createGroup(tab,group);
- }
- if(having != null){
- Consulta += " " + this.createHaving(tab,having);
- }
- if(order != null){
- Consulta += " " + this.createOrder(tab,order);
- }
- return Consulta;
- }
- private LinkedList<String[]> prepareTables(String[][] tables){
- LinkedList<String[]> tab = new LinkedList();
- String[] temp = { this.generateKey(tab, this.getTable()), this.getTable(),null};
- tab.add(temp);
- if(tables != null){
- for (String[] table : tables) {
- String[] temp2 = {this.generateKey(tab, table[0]), table[0], table[1]};
- tab.add(temp2);
- }
- }
- return tab;
- }
- private String generateKey(LinkedList<String[]> alias,String now){
- String d = "" + now.charAt(0);
- if(alias.isEmpty()){
- return d;
- } else {
- for (String[] alia : alias) {
- for (int x = 0; x < now.length(); x++) {
- if (alia[0].equals(d)) {
- x++;
- d += now.charAt(x);
- }
- }
- }
- return d;
- }
- }
- private String createColumns(String[] cols,String[][] tables){
- String columns = "";
- for(String c : cols){
- if(tables == null){
- columns += c;
- } else {
- LinkedList<String[]> tab = this.prepareTables(tables);
- for (String[] tab1 : tab) {
- LinkedList<String> temp = this.objbd.getColumns(tab1[1]);
- for (String c1 : temp) {
- if (c1.equals(c)) {
- columns += tab1[0] + "." + c;
- }
- }
- }
- }
- if(!cols[cols.length -1].equals(c)){
- columns += ",";
- } else {
- columns += " ";
- }
- }
- return columns;
- }
- private String createFrom(LinkedList<String[]> tables){
- String from = "from " + tables.getFirst()[1] + " " + tables.getFirst()[0] + " ";
- for(int i = 1; i < tables.size(); i++){
- from += tables.get(i)[2] + " join " + tables.get(i)[1] + " " + tables.get(i)[0] + " on";
- from += this.createCombos(
- new String[]{tables.get(i)[0],tables.get(i)[1]},
- new String[]{tables.get(i-1)[0],tables.get(i-1)[1]}
- );
- if(!tables.getLast()[1].equals(tables.get(i)[1])){
- from += " ";
- }
- }
- return from;
- }
- private String createCombos(String[] emisor, String[] receptor){
- String combo = " ";
- LinkedList<String> keys = this.objbd.getPrimaryKeys(emisor[1]);
- LinkedList<String> fays = this.objbd.getForeignKeys(receptor[1]);
- for(String f : fays){
- for(String k : keys){
- if(k.equals(f)){
- combo += receptor[0] +"."+k +" = "+ emisor[0] +"."+f;
- break;
- }
- }
- }
- return combo;
- }
- private String createConditions(LinkedList<String[]> tables, String[][] where){
- String condition = "where ";
- for (String[] where1 : where) {
- for (String[] table : tables) {
- LinkedList<String> temp = this.objbd.getColumns(table[1]);
- for (String c1 : temp) {
- if (c1.equals(where1[0])) {
- where1[0] = table[0] + "." + where1[0];
- }
- }
- }
- }
- for (String[] where1 : where) {
- if (where1[1].equals("between") || where1[1].equals("not between")) {
- condition += where1[0] + " " + where1[1] + " ? and ?";
- } else if (where1[1].equals("not null") || where1[1].equals("null")) {
- condition += where1[0] + " " + where1[1];
- } else {
- condition += where1[0] + " " + where1[1] + " ?";
- }
- if (!where[where.length -1][0].equals(where1[0])) {
- condition += " " + where1[2] + " ";
- }
- }
- return condition;
- }
- private String createGroup(LinkedList<String[]> tables,String[] group){
- String groupby = "group by ";
- for (int i= 0; i< group.length; i++) {
- for (String[] table : tables) {
- LinkedList<String> temp = this.objbd.getColumns(table[1]);
- for (String c1 : temp) {
- if (c1.equals(group[i])) {
- group[i] = table[0] + "." + group[i];
- }
- }
- }
- }
- for (String group1 : group) {
- groupby += group1;
- if(!group[group.length -1].equals(group1)){
- groupby += ",";
- }
- }
- return groupby;
- }
- private String createHaving(LinkedList<String[]> tables,String[][] having){
- String hav = "having ";
- return hav;
- }
- private String createOrder(LinkedList<String[]> tables,String[][] order){
- String orderby = "order by ";
- for (String[] order1 : order) {
- for (String[] table : tables) {
- LinkedList<String> temp = this.objbd.getColumns(table[1]);
- for (String c1 : temp) {
- if (c1.equals(order1[0])) {
- order1[0] = table[0] + "." + order1[0];
- }
- }
- }
- }
- for (String[] order1 : order) {
- orderby += order1[0] + " " + order1[1];
- if(!order[order.length -1][0].equals(order1[0])){
- orderby += ",";
- }
- }
- return orderby;
- }
- pero no sé cómo armar los parámetros del array en el having, en el main:
- //ejemplo para el having
- System.out.println("");
- System.out.println(new CompradorModel().select(
- new String[]{"usuCI","usuNom1"},
- new String[][]{{"usuarios","left"},{"roles","inner"}},
- new String[][]{{"usuCI","like","and"},{"usuFecNac","between"}},
- new String[]{"usuCI"},
- null,
- new String[][]{{"usuCI","asc"},{"usuFecNac","desc"}}
- ));
- Me falta pulir bien el tema de los havings automatizados, las serias subconsultas xq no sé cómo hacerlo, necesitaría una idea de cómo hacerlas automatizadas, y me falta pulir el createColumns en caso de renombrar campos y también si quiero usar funciones agregadas.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement