add base
This commit is contained in:
		
							
								
								
									
										
											BIN
										
									
								
								__pycache__/sql.cpython-310.pyc
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										
											BIN
										
									
								
								__pycache__/sql.cpython-310.pyc
									
									
									
									
									
										Normal file
									
								
							
										
											Binary file not shown.
										
									
								
							
							
								
								
									
										
											BIN
										
									
								
								__pycache__/sql.cpython-311.pyc
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										
											BIN
										
									
								
								__pycache__/sql.cpython-311.pyc
									
									
									
									
									
										Normal file
									
								
							
										
											Binary file not shown.
										
									
								
							
							
								
								
									
										
											BIN
										
									
								
								__pycache__/table.cpython-310.pyc
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										
											BIN
										
									
								
								__pycache__/table.cpython-310.pyc
									
									
									
									
									
										Normal file
									
								
							
										
											Binary file not shown.
										
									
								
							
							
								
								
									
										
											BIN
										
									
								
								__pycache__/table.cpython-311.pyc
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										
											BIN
										
									
								
								__pycache__/table.cpython-311.pyc
									
									
									
									
									
										Normal file
									
								
							
										
											Binary file not shown.
										
									
								
							
							
								
								
									
										54
									
								
								java/BancoDeDados.java
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										54
									
								
								java/BancoDeDados.java
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,54 @@ | ||||
| /* | ||||
|  * Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license | ||||
|  * Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template | ||||
|  */ | ||||
| package trabalhobd; | ||||
|  | ||||
| import java.util.ArrayList; | ||||
| import java.util.List; | ||||
| import java.util.Queue; | ||||
|  | ||||
| /** | ||||
|  * | ||||
|  * @author julioc7r | ||||
|  */ | ||||
| public class BancoDeDados { | ||||
|     List <Tabela> lista_tabelas; | ||||
|     int n_tabelas; | ||||
|     SQL pesquisa; | ||||
|  | ||||
|     public BancoDeDados() { | ||||
|         this.lista_tabelas = new ArrayList<Tabela>(); | ||||
|         this.n_tabelas = 0; | ||||
|     } | ||||
|  | ||||
|     public List<Tabela> getLista_tabelas() { | ||||
|         return lista_tabelas; | ||||
|     } | ||||
|  | ||||
|     public void setLista_tabelas(List<Tabela> lista_tabelas) { | ||||
|         this.lista_tabelas = lista_tabelas; | ||||
|     } | ||||
|  | ||||
|     public int getN_tabelas() { | ||||
|         return n_tabelas; | ||||
|     } | ||||
|  | ||||
|     public void setN_tabelas(int n_tabelas) { | ||||
|         this.n_tabelas = n_tabelas; | ||||
|     } | ||||
|  | ||||
|     public SQL getPesquisa() { | ||||
|         return pesquisa; | ||||
|     } | ||||
|  | ||||
|     public void setPesquisa(Queue<String> query,List<Tabela> lista_tabelas) { | ||||
|         this.pesquisa = new SQL(query,lista_tabelas); | ||||
|     } | ||||
|  | ||||
|     public void executaSQL(Queue<String> query){ | ||||
|         setPesquisa(query, this.lista_tabelas); | ||||
|         pesquisa.resultadoQuery(); | ||||
|     } | ||||
|      | ||||
| } | ||||
							
								
								
									
										12
									
								
								java/README.md
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										12
									
								
								java/README.md
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,12 @@ | ||||
| # jcSQL | ||||
|  | ||||
| Tentativa de criar um Banco de Dados que aceita alguns comandos SQL em Java. | ||||
|  | ||||
| A proposta do Trabalho 1 da disciplina de Banco de Dados, foi a de codificar um Banco de Dados em memória para que tenhamos melhor compreensão dos processos que acontencem dentro de um DataBase. | ||||
|  | ||||
| Comandos Suportados: | ||||
|  | ||||
| Select, From, Where, Order By | ||||
|  | ||||
| *os filtros do Where( < , > , = , != ) | ||||
|  | ||||
							
								
								
									
										588
									
								
								java/SQL.java
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										588
									
								
								java/SQL.java
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,588 @@ | ||||
| /* | ||||
|  * Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license | ||||
|  * Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template | ||||
|  */ | ||||
| package trabalhobd; | ||||
|  | ||||
| import java.util.ArrayList; | ||||
| import java.util.LinkedList; | ||||
| import java.util.List; | ||||
| import java.util.Queue; | ||||
|  | ||||
| /** | ||||
|  * | ||||
|  * @author julioc7r | ||||
|  */ | ||||
| public class SQL { | ||||
|     Queue <String> query; | ||||
|     List<Tabela> lista_tabelas; | ||||
|     List<String> selectList; | ||||
|     List<String> fromList; | ||||
|     List<String> whereList; | ||||
|     List<String> joinList; | ||||
|     Boolean whereFlag; | ||||
|     Boolean orderBy; | ||||
|     String chaveOrder; | ||||
|  | ||||
|     public SQL(Queue<String> query,List<Tabela> lista_tabelas) { | ||||
|         this.query = query; | ||||
|         this.selectList = new LinkedList(); | ||||
|         this.fromList = new LinkedList(); | ||||
|         this.whereList = new LinkedList(); | ||||
|         this.joinList = new LinkedList(); | ||||
|         this.orderBy = false; | ||||
|         this.whereFlag = false; | ||||
|         this.chaveOrder = null; | ||||
|         this.lista_tabelas = lista_tabelas; | ||||
|          | ||||
|     } | ||||
|  | ||||
|     public Queue<String> getQuery() { | ||||
|         return query; | ||||
|     } | ||||
|  | ||||
|     public void setQuery(Queue<String> query) { | ||||
|         this.query = query; | ||||
|     } | ||||
|  | ||||
|     public Boolean getOrderBy() { | ||||
|         return orderBy; | ||||
|     } | ||||
|  | ||||
|     public void setOrderBy(Boolean orderBy) { | ||||
|         this.orderBy = orderBy; | ||||
|     } | ||||
|      | ||||
|     public void processamentoQuery() { | ||||
|          | ||||
|         for(int i = 0;this.query.isEmpty() == false; i++) | ||||
|         { | ||||
|             System.out.println("Fila["+i +"]  = "+ this.query.peek()); | ||||
|             if(this.query.peek().equals("select")){ //SE ENTROU salva na selectList os atributos requiridos | ||||
|                 i++;//para printar a ordem dos elementos | ||||
|                 this.query.poll(); | ||||
|                 int j=0; | ||||
|                 while(isCommand(query.peek())== false){ | ||||
|                     System.out.println(this.query.peek()+" - "+ "LISTA VAZIA" +j); | ||||
|                     System.out.println("Fila["+i +"]  = "+ this.query.peek()); | ||||
|                     i++; | ||||
|                     this.selectList.add(this.query.poll()); | ||||
|                     System.out.println(this.query.peek()+" - "+this.selectList.get(j)); | ||||
|                     j++; | ||||
|                 } | ||||
|             } | ||||
|             if(this.query.peek().equals("from")){ //SE ENTROU salva na fromList os atributos requiridos | ||||
|                 i++; | ||||
|                 this.query.poll(); | ||||
|                 int j=0; | ||||
|                 while(isCommand(query.peek())== false){ | ||||
|                     System.out.println(this.query.peek()+" - "+"LISTA VAZIA" +j); | ||||
|                     System.out.println("Fila["+i +"]  = "+ this.query.peek()); | ||||
|                     i++; | ||||
|                     this.fromList.add(this.query.poll()); | ||||
|                     System.out.println(this.query.peek()+" - "+this.fromList.get(j)); | ||||
|                     j++; | ||||
|                 } | ||||
|             } | ||||
|              | ||||
|             if(this.query.peek()!=null && this.query.peek().equals("on")){ //SE ENTROU salva na fromList os atributos requiridos | ||||
|                 i++; | ||||
|                 this.query.poll(); | ||||
|                 int j=0; | ||||
|                 while(isCommand(query.peek())== false){ | ||||
|                     System.out.println(this.query.peek()+" - "+"LISTA VAZIA" +j); | ||||
|                     System.out.println("Fila["+i +"]  = "+ this.query.peek()); | ||||
|                     i++; | ||||
|                     this.joinList.add(this.query.poll()); | ||||
|                     System.out.println(this.query.peek()+" - "+this.joinList.get(j)); | ||||
|                     j++; | ||||
|                 } | ||||
|             } | ||||
|              | ||||
|             if(this.query.peek()!=null && this.query.peek().equals("where")){  //SE ENTROU salva na whereList os atributos requiridos | ||||
|                 this.whereFlag = true; | ||||
|                 i++; | ||||
|                 this.query.poll(); | ||||
|                 int j=0; | ||||
|                 while(isCommand(query.peek())== false){ | ||||
|                     System.out.println(this.query.peek()+" - "+"LISTA VAZIA" +j); | ||||
|                     System.out.println("Fila["+i +"]  = "+ this.query.peek()); | ||||
|                     i++; | ||||
|                     this.whereList.add(this.query.poll()); | ||||
|                     //System.out.println(this.query.peek()+" - "+this.whereList.get(j)); | ||||
|                     j++; | ||||
|                     if(query.isEmpty()){ | ||||
|                         return;} | ||||
|                     } | ||||
|             }               | ||||
|             if(this.query.peek()!=null && this.query.peek().equals("order")){  //SE ENTROU define o elemento pelo qual a lista será ordenada | ||||
|                 i+=2; | ||||
|                 this.query.poll();// elimina o "ORDER" da fila | ||||
|                 this.query.poll();// elimina o "BY" da fila | ||||
|                 this.orderBy = true; | ||||
|                 this.chaveOrder = this.query.poll(); | ||||
|                 System.out.println("Fila["+i +"]  = "+"CHAVE  = "+ this.chaveOrder); | ||||
|             } | ||||
|         } | ||||
|     } | ||||
|     | ||||
|     public void resultadoQuery(){ | ||||
|         //Pensar em como fazer | ||||
|         processamentoQuery(); | ||||
|         int whereTabelaPos = -1; | ||||
|          | ||||
|          | ||||
|         //ENCONTRA AS TABELAS USADAS | ||||
|         List<Tabela> tabelas = new LinkedList(); | ||||
|         System.out.println("LAOSLKDOAKSDOAKS"+this.lista_tabelas.size()); | ||||
|         for(int i = 0; i<this.lista_tabelas.size();i++){ | ||||
|             int j = 0; | ||||
|             //FAZ A COMPARAÇÃO ENCONTRANDO AS TABELAS ESCOLHIDAS NO FROM E ADICIONANDO NA LISTA TABELAS | ||||
|             if(this.fromList.isEmpty() && this.fromList.get(j).equals(this.lista_tabelas.get(i).getNomeTabela())){ | ||||
|                 tabelas.add(this.lista_tabelas.get(i)); | ||||
|                 j++; | ||||
|             } | ||||
|         } | ||||
|         System.out.println("LAOSLKDOAKSDOAKS"+this.lista_tabelas.size()); | ||||
|         //FAZ A SELECTION(WHERE),ENCONTRA EM PRIMEIRA PARTE OS ATRIBUTOS QUE CORRESPONDE A CONDIÇÃO REQUIRIDA | ||||
|         //Busca em qual tabela está o elemento | ||||
|         for(int i = 0; i<tabelas.size();i++){ | ||||
|             if(whereFlag && tabelas.get(i).getColunaPeloNome(this.whereList.get(i))<99){ | ||||
|                 whereTabelaPos = i; | ||||
|             } | ||||
|         } | ||||
|  | ||||
|  | ||||
|         int n_elementos = 0; // número de colunas que a tabela resultado terá | ||||
|         int[] pos_tabela = new int[this.fromList.size()]; // guarda a posição da tabela usada na lista_tabela  | ||||
|         | ||||
|         int n_linhas = 0; | ||||
|         int n_colunas_total = 0; | ||||
|         int m; | ||||
|         boolean encontrouTabela = false; | ||||
|          | ||||
|         int[] quantidadePorTabela = new int[this.fromList.size()];//Guarda a quantidade de colunas usadas por tabela | ||||
|         for(int i = 0 ; i < this.fromList.size();i++){ | ||||
|             quantidadePorTabela[i] = 0; | ||||
|         } | ||||
|          | ||||
|         if(this.selectList.get(0).contains("*")){ | ||||
|             for(int i = 0; i< this.fromList.size();i++){ | ||||
|                 for( m = 0; m < this.lista_tabelas.size(); m++ ){ | ||||
|                     System.out.println(this.fromList.get(i)+" COMPARAÇÃO "+ this.lista_tabelas.get(m).getNomeTabela()); | ||||
|                     //System.out.println(lista_tabelas.toString()); | ||||
|                     if(this.fromList.get(i).equals(this.lista_tabelas.get(m).getNomeTabela())){ | ||||
|                         | ||||
|                         pos_tabela[i] = m;  | ||||
|                         encontrouTabela = true; | ||||
|                     } | ||||
|                 } | ||||
|                 if( encontrouTabela == false && m == this.lista_tabelas.size()){ | ||||
|                     System.out.println("TABELA NÃO ENCONTRADA"); | ||||
|                     return; | ||||
|                 } | ||||
|                 System.out.println("JO"+this.fromList.get(i)+this.lista_tabelas.indexOf(this.fromList.get(i))+ pos_tabela[i]); | ||||
|                 n_elementos += this.lista_tabelas.get(pos_tabela[i]).getColunas(); | ||||
|                 n_linhas += this.lista_tabelas.get(pos_tabela[i]).getLinhas(); | ||||
|                 n_colunas_total += this.lista_tabelas.get(pos_tabela[i]).getColunas(); | ||||
|                 quantidadePorTabela[i] =  this.lista_tabelas.get(pos_tabela[i]).getColunas();  | ||||
|             } | ||||
|         }else{ | ||||
|             n_elementos = this.selectList.size(); // número de colunas que a tabela resultado terá | ||||
|             for(int i = 0; i< this.fromList.size();i++){ | ||||
|                 for(int j = 0; j <this.lista_tabelas.size();j++){ | ||||
|                     if(this.lista_tabelas.get(j).nomeTabela.contentEquals(this.fromList.get(i))){ | ||||
|                     pos_tabela[i] = j; //FALTA IMPLEMENTAR CASO OCORRA O JOIN | ||||
|                      System.out.println("entrei"); | ||||
|                     quantidadePorTabela[i] += 1; | ||||
|                     } | ||||
|                     System.out.println("JO"+this.fromList.get(i) + this.lista_tabelas.get(j).nomeTabela); | ||||
|                 } | ||||
|                 n_linhas += this.lista_tabelas.get(pos_tabela[i]).getLinhas(); | ||||
|                 n_colunas_total += this.lista_tabelas.get(pos_tabela[i]).getColunas(); // FAZER IGUAL o tam[] e  o pos tabela[] pra dar o JOINs | ||||
|                  | ||||
|             } | ||||
|         } | ||||
|  | ||||
|         List<String> tabelaResultado = new LinkedList<>();//n_elementos*n_linhas); | ||||
|                          | ||||
|          | ||||
|         int[] tam = new int[n_elementos]; | ||||
|          | ||||
|         System.out.println( "QQQQ "+ n_elementos); | ||||
|         //n_elementos--; | ||||
|          | ||||
|          | ||||
|         boolean joinONList = false; | ||||
|         if(this.selectList.get(0).contains("*") && fromList.size() == 1){ | ||||
|             for(int j = 0; j < n_elementos ; j++){ | ||||
|                 tam[j] = j; | ||||
|             } | ||||
|         } | ||||
|         else if(this.selectList.get(0).contains("*") && fromList.size() > 1){ | ||||
|             int k = 0; | ||||
|             for(int i = 0; i < fromList.size();i++){ | ||||
|                 for(int j = 0; j < this.lista_tabelas.get(pos_tabela[i]).getColunas() ; j++){ | ||||
|                     tam[k] = j; | ||||
|                     k++; | ||||
|                 } | ||||
|             } | ||||
|         } | ||||
|         else{ | ||||
|             for (int i = 0; i < pos_tabela.length ; i++){ | ||||
|                 for(int j = 0; j < n_elementos ; j++){ | ||||
|                     tam[j] = this.lista_tabelas.get(pos_tabela[i]).getColunaPeloNome(this.selectList.get(j)); | ||||
|                     System.out.println(tam[j] + "QQQ "+ n_elementos); | ||||
|                             if(tam[j]==99){// PERCORREU A TABELA E NÃO ACHOU | ||||
|                                 System.out.println(" ELEMENTO NÃO ENCONTRADO!!"); | ||||
|                                 return; | ||||
|                             } | ||||
|                     if((whereFlag==true) && (this.selectList.get(j).contains(whereList.get(0)))){ | ||||
|                         whereTabelaPos = j; | ||||
|                     } | ||||
|                 } | ||||
|             } | ||||
|         } | ||||
|          | ||||
|          | ||||
|         if(fromList.size()>1){ // FAZ O JOIN SE A FROMLIST POSSUIR MAIS DE DOIS ELEMENTOS(IMPLEMENTAMOS PARA FUNCIONAR COM 2 ELEMENTOS)          | ||||
|             tabelaResultado = nestedLoopJoin(tabelaResultado,pos_tabela,tam,quantidadePorTabela); | ||||
|             if(whereFlag==true){ | ||||
|                 for(int k = 0; k < pos_tabela.length ; k++){ | ||||
|                     for(int i = 0 ; i < n_linhas;i++){ | ||||
|                         for(int j = 0 ; j < n_elementos ; j++){ | ||||
|                             if(comparaWhere(this.lista_tabelas.get(pos_tabela[k]).getElemento(tam[whereTabelaPos]))==true){ | ||||
|                                 tabelaResultado.add(this.lista_tabelas.get(pos_tabela[k]).getElemento(tam[j])); | ||||
|                             } | ||||
|                             else if(tabelaResultado.size()<n_elementos){//ADICIONA OS PRIMEIROS ELEMENTOS | ||||
|                                 tabelaResultado.add(this.lista_tabelas.get(pos_tabela[k]).getElemento(tam[j])); | ||||
|                             } | ||||
|                             tam[j] += n_colunas_total; //Faz com que os elementos das colunas desejadas sejam achados. | ||||
|                         } | ||||
|                     } | ||||
|                 }    | ||||
|             } | ||||
|             else if(joinONList == true){ | ||||
|                  | ||||
|                 for(int k = 0; k < pos_tabela.length ; k++){ | ||||
|                     for(int i = 0 ; i < n_linhas;i++){ | ||||
|                         for(int j = 0 ; j < n_elementos ; j++){ | ||||
|                             tabelaResultado.add(this.lista_tabelas.get(pos_tabela[k]).getElemento(tam[j])); | ||||
|                             tam[j] += n_colunas_total; //Faz com que os elementos das colunas desejadas sejam achados. | ||||
|                         } | ||||
|                     } | ||||
|                 }    | ||||
|             } | ||||
|             tabelaResultado.add("fim"); | ||||
|         } | ||||
|          | ||||
|         else{  | ||||
|             if(whereFlag==true){ | ||||
|                 for(int k = 0; k < pos_tabela.length ; k++){ | ||||
|                     for(int i = 0 ; i < n_linhas;i++){ | ||||
|                         for(int j = 0 ; j < n_elementos ; j++){ | ||||
|                             if(comparaWhere(this.lista_tabelas.get(pos_tabela[k]).getElemento(tam[whereTabelaPos]))==true){ | ||||
|                                 tabelaResultado.add(this.lista_tabelas.get(pos_tabela[k]).getElemento(tam[j])); | ||||
|                             } | ||||
|                             else if(tabelaResultado.size()<n_elementos){//ADICIONA OS PRIMEIROS ELEMENTOS | ||||
|                                 tabelaResultado.add(this.lista_tabelas.get(pos_tabela[k]).getElemento(tam[j])); | ||||
|                             } | ||||
|                             tam[j] += n_colunas_total; //Faz com que os elementos das colunas desejadas sejam achados. | ||||
|                         } | ||||
|                     } | ||||
|                 }    | ||||
|             } | ||||
|             else{ | ||||
|                 for(int k = 0; k < pos_tabela.length ; k++){ | ||||
|                     for(int i = 0 ; i < n_linhas;i++){ | ||||
|                         for(int j = 0 ; j < n_elementos ; j++){ | ||||
|                             tabelaResultado.add(this.lista_tabelas.get(pos_tabela[k]).getElemento(tam[j])); | ||||
|                             tam[j] += n_colunas_total; //Faz com que os elementos das colunas desejadas sejam achados. | ||||
|                         } | ||||
|                     } | ||||
|                 }  | ||||
|             } | ||||
|              | ||||
|             tabelaResultado.add("fim"); | ||||
|         } | ||||
|          | ||||
|         // select * from employees1000,livros-db | ||||
|         // select first_name gender from employees1000, livros-db | ||||
|         // select first_name gender from employees1000 where gender = F _ FUNCIONA | ||||
|         // select first_name gender from employees1000 where first_name = Mary NAO FUNCIONA | ||||
|          | ||||
|         if(this.orderBy == true){ | ||||
|             orderBy(this.chaveOrder,tabelaResultado,n_elementos); | ||||
|         } | ||||
|             System.out.println("aaaaaaaaa  "+tabelaResultado.size()); | ||||
|         printTabelaResultado(tabelaResultado,n_elementos,tabelaResultado.size()); | ||||
|     }  | ||||
|      | ||||
|     public void orderBy(String elemento, List<String> tabela, int tam_colunas){ | ||||
|         int tam = tam_colunas; | ||||
|         int ordenado = 0; | ||||
|         while(ordenado==1){ | ||||
|             ordenado = 1; | ||||
|             for(int i = 0 ; i < tabela.size();i = i + tam_colunas){ | ||||
|                 if(tabela.get(i).compareTo(tabela.get(i+tam_colunas))<0){ | ||||
|                     for(int j = 0 ; j < tam_colunas ; j++){ | ||||
|                         String swap = tabela.get(i+j); | ||||
|                         tabela.set(i+j,tabela.get(i+j+tam_colunas)); | ||||
|                         tabela.set(i+j+tam_colunas,swap); | ||||
|                     } | ||||
|                     ordenado = 0; | ||||
|                 } | ||||
|             } | ||||
|         } | ||||
|     } | ||||
|      | ||||
|     private void printTabelaResultado(List<String> tabelaResultado, int tcolunas, int tlinhas) { | ||||
|         int tam = 0; | ||||
|         System.out.println("  --------------------------------------------------------------"); | ||||
|         for(int i = 0; i < tlinhas ; i++){ | ||||
|          | ||||
|             for(int j = 0; j < tcolunas ; j++){ | ||||
|                 System.out.print(" | " + tabelaResultado.get(tam)); | ||||
|                 tam++; | ||||
|             } | ||||
|             System.out.print(" | "); | ||||
|             System.out.println("\n  --------------------------------------------------------------"); | ||||
|             if(tabelaResultado.get(tam).contains("fim")){ | ||||
|                 return; | ||||
|             } | ||||
|         } | ||||
|     } | ||||
|      | ||||
|     private int[] maiorTabela(int[] pos_tabela){ //VERIFICA A MENOR TABELA E COLOCA NA POSIÇÃO CENTRAL DO LOOP | ||||
|          | ||||
|         if(this.lista_tabelas.get(pos_tabela[0]).getLinhas() > this.lista_tabelas.get(pos_tabela[1]).getLinhas()){ | ||||
|             int aux = pos_tabela[0]; | ||||
|             pos_tabela[0] = pos_tabela[1]; | ||||
|             pos_tabela[1] = aux; | ||||
|         } | ||||
|          | ||||
|         return pos_tabela; | ||||
|          | ||||
|     } | ||||
|      | ||||
|     public List<String> nestedLoopJoin(List<String> tabelaResultado, int[] pos_tabela,int[] select_pos,int[] coluna_select){ | ||||
|         //TRABALHO 2 | ||||
|         pos_tabela = maiorTabela(pos_tabela); // A MAIOR TABELA RODA DENTRO DO LOOP DA MENOR TABELA | ||||
|        /* for each tuple t.r in r do begin | ||||
|             for each tuple t.s in s do begin | ||||
|                 test pair (t.r,t.s) to see if they satisfy the join condition θ | ||||
|                 if they do, add t.r • t.s to the result. | ||||
|             end | ||||
|         end | ||||
|         r is called the outer relation and s the inner relation of the join. | ||||
|         */ | ||||
|         | ||||
|         //select name from pessoa trabalho on cpf | ||||
|                 | ||||
|         for(int i = 0; i< this.joinList.size() ; i++){ | ||||
|         | ||||
|             System.out.println("JOIN ON" +this.joinList.get(i)); | ||||
|         } | ||||
|         | ||||
|         int[] pos_join = new int[2]; // GUARDA A POSIÇÃ) DOS ELEMENTO CHAVE EM SUAS RESPECTIVAS TABELAS [0] = tabela1 ||||| [1] = tabela2 | ||||
|          | ||||
|         // pos_tabela -> posição da tabela na lista tabela | ||||
|          | ||||
|         for (int i = 0; i < pos_tabela.length ; i++){ | ||||
|             for(int j = 0; j < 2 ; j++){ | ||||
|                  | ||||
|                 pos_join[j] = this.lista_tabelas.get(pos_tabela[i]).getColunaPeloNome(this.joinList.get(0)); | ||||
|                  | ||||
|                 System.out.println(pos_join[j] + "LOOPcompareID TABELA - "+ i); | ||||
|                  | ||||
|                         if(pos_join[j]==99){// PERCORREU A TABELA E NÃO ACHOU | ||||
|                             System.out.println(" ELEMENTO NÃO ENCONTRADO!!"); | ||||
|                             return null; | ||||
|                         } | ||||
|             } | ||||
|         } | ||||
|          | ||||
|          | ||||
|         //ADICIONANDO OS HEADERS | ||||
|         int p = 0; | ||||
|         for(int k = 0; k < select_pos.length; k++){ | ||||
|             System.out.println(pos_join[p] + "DENTRO DO LOOP K - " + k + " COLUNA "+ select_pos[k]+" P - " +p + this.lista_tabelas.get(pos_tabela[p]).getElemento(select_pos[k])); | ||||
|                 if(k == (coluna_select[p]-1)){ | ||||
|                                  | ||||
|                     p = 1; | ||||
|                 } | ||||
|             tabelaResultado.add(this.lista_tabelas.get(pos_tabela[p]).getElemento(select_pos[k])); | ||||
|             select_pos[k] = select_pos[k] + this.lista_tabelas.get(pos_tabela[p]).getColunas(); | ||||
|         } | ||||
|          | ||||
|          | ||||
|         pos_join[0] = pos_join[0] + this.lista_tabelas.get(pos_tabela[0]).getColunas(); | ||||
|         pos_join[1] = pos_join[1] + this.lista_tabelas.get(pos_tabela[1]).getColunas(); | ||||
|         int aux = pos_join[0]; | ||||
|         int auxb = pos_join[1]; | ||||
|          | ||||
|          | ||||
|         int[] auxSelect = select_pos; | ||||
|          | ||||
|         for (int i = 0; i < select_pos.length ; i++){ | ||||
|            System.out.println(i + " DENTRO DO SELECT - "+ select_pos[i]); | ||||
|         } | ||||
|          | ||||
|         for(int i = 0 ; i < this.lista_tabelas.get(pos_tabela[0]).getLinhas();i++){ | ||||
|             //System.out.println( " i " + i ); | ||||
|             if(pos_join[0] > this.lista_tabelas.get(pos_tabela[0]).getTamanho()){ | ||||
|                 break; | ||||
|             } | ||||
|             if(i>=1){ | ||||
|                 pos_join[0] = aux +( i * this.lista_tabelas.get(pos_tabela[0]).getColunas()); | ||||
|             } | ||||
|             //System.out.println( " POSIÇAO DO JOIN i " + pos_join[0] ); | ||||
|              | ||||
|                 for(int j = 0 ; j < this.lista_tabelas.get(pos_tabela[1]).getLinhas() ; j++){ | ||||
|                     //System.out.println( " i " + i + " j " + j); | ||||
|                     // System.out.println( " POSIÇAO DO JOIN j " + pos_join[1] ); | ||||
|                     if(this.lista_tabelas.get(pos_tabela[0]).getElemento(pos_join[0]).contentEquals(this.lista_tabelas.get(pos_tabela[1]).getElemento(pos_join[1]))){ | ||||
|                         p = 0; | ||||
|                          | ||||
|                         for(int k = 0; k < select_pos.length; k++){ | ||||
|                             System.out.println(pos_join[p] + "DENTRO DO LOOP K - " + k + " COLUNA "+ select_pos[k]+" P - " +p + this.lista_tabelas.get(pos_tabela[p]).getElemento(select_pos[k])); | ||||
|                             if(k == (coluna_select[p]-1)){ | ||||
|                                  | ||||
|                                 p = 1; | ||||
|                                 //System.out.println(pos_join[j] + "DENTRO DO LOOP K - " + k + " COLUNA "+ select_pos[k]+" P - " +p); | ||||
|                             } | ||||
|                             tabelaResultado.add(this.lista_tabelas.get(pos_tabela[p]).getElemento(select_pos[k])); | ||||
|                             if(p==1){ | ||||
|                                 select_pos[k] = select_pos[k] + this.lista_tabelas.get(pos_tabela[p]).getColunas(); | ||||
|                             } | ||||
|                              | ||||
|                         } | ||||
|                     } | ||||
|                     else{ | ||||
|                         for(int k = 0; k < select_pos.length; k++){ | ||||
|                             select_pos[k] = select_pos[k] + this.lista_tabelas.get(pos_tabela[p]).getColunas(); | ||||
|                         } | ||||
|                     } | ||||
|                      | ||||
|                     //Faz com que os elementos das colunas desejadas sejam achados. | ||||
|                     pos_join[1] = pos_join[1] + this.lista_tabelas.get(pos_tabela[1]).getColunas(); | ||||
|                     if(pos_join[1] > this.lista_tabelas.get(pos_tabela[1]).getTamanho()){ | ||||
|                         break; | ||||
|                     } | ||||
|                      | ||||
|                 } | ||||
|             for(int k = 0; k < coluna_select[0]; k++){ | ||||
|                 select_pos[k] = select_pos[k] + this.lista_tabelas.get(pos_tabela[p]).getColunas(); | ||||
|             } | ||||
|             for(int k = coluna_select[0]; k < select_pos.length; k++){ | ||||
|                 select_pos[k] = auxSelect[k]; | ||||
|             } | ||||
|             pos_join[1] = auxb; | ||||
|         } | ||||
|         return tabelaResultado; | ||||
|          | ||||
|        /* select * from trabalho pessoa on cpf TESTE  | ||||
|      | ||||
|     DUAS FORMAS DE LER  | ||||
|      | ||||
|     SELECT * | ||||
|     FROM tabela1, tabela2  | ||||
|     WHERE tabela1.id=tabela2.id | ||||
|      | ||||
|     SELECT * | ||||
|     FROM tabela1 | ||||
|     JOIN tabela2 | ||||
|     ON tabela1.Key = tabela2.Key | ||||
|     */ | ||||
|     } | ||||
|      | ||||
|     /* | ||||
|  | ||||
|     public void selection(List<String> whereList,Tabela tabela){//PROCESSAMENTO DO WHERE | ||||
|         //{=, <, ≤, >, ≥, ≠} OPERADORES PARA TABELAS ORDENADAS | ||||
|         //{=, ≠} OPERADORES PARA VALORES DESORDENADOS | ||||
|         int i = 0; | ||||
|  | ||||
|         if(whereList.get(i).contentEquals("=")){ | ||||
|             tabela = tabela.comparaIgual(tabela,elemento,chave); | ||||
|         } | ||||
|         else if(whereList.get(i).contentEquals("!=")){ | ||||
|             tabela = tabela.comparaDiferente(tabela,elemento,chave); | ||||
|         } | ||||
|         else if(whereList.get(i).contentEquals(">")){ | ||||
|             tabela = tabela.comparaMaior(tabela,elemento,chave); | ||||
|         } | ||||
|         else if(whereList.get(i).contentEquals("<")){ | ||||
|             tabela = tabela.comparaMenor(tabela,elemento,chave); | ||||
|         } | ||||
|  | ||||
|          | ||||
|     } | ||||
|     */ | ||||
|  | ||||
|     private boolean isCommand(String elemento){ | ||||
|         if(elemento == null){ | ||||
|             return true; | ||||
|         } | ||||
|         else if(elemento.equals("where")){ | ||||
|             return true; | ||||
|         } | ||||
|         else if(elemento.equals("order")){ | ||||
|             return true; | ||||
|         } | ||||
|         else if(elemento.equals("on")){ | ||||
|             return true; | ||||
|         } | ||||
|         else if(elemento.equals("from")){ | ||||
|             return true; | ||||
|         } | ||||
|         else{return false;} | ||||
|     } | ||||
|      | ||||
|      | ||||
|      | ||||
|     private boolean executaWhere(String elemento){ | ||||
|         if(whereList.get(1).contains("=")){ | ||||
|             if(elemento.equals(this.whereList.get(2))){ //IGUAL | ||||
|             return true;} | ||||
|         } | ||||
|         else if(whereList.get(1).contains("!=")){ | ||||
|             if(!elemento.equals(this.whereList.get(2))){ //DIFERENTE | ||||
|             return true;} | ||||
|         } | ||||
|         else if(whereList.get(1).contains(">")){ | ||||
|             if(elemento.compareTo(this.whereList.get(2))>0){ //MAIOR | ||||
|             return true;} | ||||
|         } | ||||
|         else if(whereList.get(1).contains("<")){ | ||||
|             if(elemento.compareTo(this.whereList.get(2))<0){ //MENOR | ||||
|             return true;} | ||||
|         } | ||||
|         else{return false;} | ||||
|          | ||||
|         //boolean resultado = comparaWhere(); | ||||
|          | ||||
|         return false; | ||||
|     } | ||||
|      | ||||
|     private boolean comparaWhere(String elemento){ | ||||
|         if(whereList.get(1).contains("=")){ | ||||
|             if(elemento.equals(this.whereList.get(2))){ //IGUAL | ||||
|             return true;} | ||||
|         } | ||||
|         else if(whereList.get(1).contains("!=")){ | ||||
|             if(!elemento.equals(this.whereList.get(2))){ //DIFERENTE | ||||
|             return true;} | ||||
|         } | ||||
|         else if(whereList.get(1).contains(">")){ | ||||
|             if(elemento.compareTo(this.whereList.get(2))>0){ //MAIOR | ||||
|             return true;} | ||||
|         } | ||||
|         else if(whereList.get(1).contains("<")){ | ||||
|             if(elemento.compareTo(this.whereList.get(2))<0){ //MENOR | ||||
|             return true;} | ||||
|         } | ||||
|         else{return false;} | ||||
|         return false; | ||||
|     } | ||||
| } | ||||
							
								
								
									
										197
									
								
								java/Tabela.java
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										197
									
								
								java/Tabela.java
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,197 @@ | ||||
| /* | ||||
|  * Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license | ||||
|  * Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template | ||||
|  */ | ||||
| package trabalhobd; | ||||
|  | ||||
|  | ||||
| import java.util.ArrayList; | ||||
| import java.util.List; | ||||
|  | ||||
| /* | ||||
|  * Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license | ||||
|  * Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template | ||||
|  */ | ||||
|  | ||||
| /** | ||||
|  * | ||||
|  * @author julioc7r | ||||
|  */ | ||||
| public class Tabela { | ||||
|      | ||||
|     String nomeTabela; | ||||
|      | ||||
|     int tamanho; | ||||
|      | ||||
|     private final int linhas; | ||||
|     private final int colunas; | ||||
|     private  List<String> elementos; // mudar para qualquer tipo de variavel | ||||
|      | ||||
|      | ||||
|     public Tabela(String nomeTabela,int linhas, int colunas) { | ||||
|         this.nomeTabela = nomeTabela; | ||||
|         this.tamanho = 0; | ||||
|         this.linhas = linhas; | ||||
|         this.colunas = colunas; | ||||
|         elementos = new ArrayList<>(linhas * colunas); | ||||
|     } | ||||
|  | ||||
|     public int getTamanho() { | ||||
|         return tamanho; | ||||
|     } | ||||
|  | ||||
|     public void setTamanho(Integer tamanho) { | ||||
|         this.tamanho = tamanho; | ||||
|     } | ||||
|  | ||||
|     public int getColunas() { | ||||
|         return colunas; | ||||
|     } | ||||
|  | ||||
|     public int getLinhas() { | ||||
|         return linhas; | ||||
|     } | ||||
|  | ||||
|     public String getNomeTabela() { | ||||
|         return nomeTabela; | ||||
|     } | ||||
|  | ||||
|     public void setNomeTabela(String nomeTabela) { | ||||
|         this.nomeTabela = nomeTabela; | ||||
|     } | ||||
|  | ||||
|     public String getElemento(int posicao) { | ||||
|         return elementos.get(posicao); | ||||
|     } | ||||
|      | ||||
|     public void projecao(String elemento){ | ||||
|          | ||||
|         if(elemento.compareTo("*")==0){ | ||||
|             printTabela(); | ||||
|         } | ||||
|         else{ | ||||
|             int i = 0; | ||||
|             while(i < colunas){ | ||||
|                 System.out.println(i + " - "+ elemento + "=" + elementos.get(i)); | ||||
|                 if(elemento.compareTo(elementos.get(i))==0){ | ||||
|                     break; | ||||
|                 } | ||||
|                 i++; | ||||
|             } | ||||
|             if(i == colunas){ | ||||
|                 System.out.println(" não foi encontrado "); | ||||
|                 return;} | ||||
|             else{ | ||||
|                 printTabelaColuna(i); | ||||
|             } | ||||
|         } | ||||
|     } | ||||
|      | ||||
|     public int getColunaPeloNome(String elemento){ | ||||
|         int i = 0; | ||||
|             while(i < colunas){ | ||||
|                 System.out.println(i + " - "+ elemento + "=" + elementos.get(i)); | ||||
|                 if(elemento.compareTo(elementos.get(i))==0){ | ||||
|                     return i; | ||||
|                 } | ||||
|                 i++; | ||||
|             } | ||||
|         return 99; | ||||
|     } | ||||
|  | ||||
|     /* | ||||
|     public String get(int linha, int coluna) { | ||||
|         if (!posicaoValida(linha, coluna)) throw new IllegalArgumentException(); | ||||
|         return elementos.get(posicaoNaLista(linha, coluna)); | ||||
|     }*/ | ||||
|      | ||||
|     public void adicionaElemento(String[] elemento) { | ||||
|         for(int i = 0; i<elemento.length ;i++){ | ||||
|             elementos.add(elemento[i]); | ||||
|             this.tamanho = this.tamanho + 1; | ||||
|         } | ||||
|     } | ||||
|      | ||||
|     public void printTabela(){ | ||||
|         System.out.println("  --------------------------------------------------------------"); | ||||
|         int tam = 0; | ||||
|         for(int i = 0; i < linhas ; i++){ | ||||
|              | ||||
|             for(int j = 0; j<colunas ; j++){ | ||||
|                 System.out.print(" | " + elementos.get(tam)); | ||||
|                 tam++; | ||||
|             } | ||||
|             System.out.print(" | "); | ||||
|             System.out.println("\n  --------------------------------------------------------------"); | ||||
|          | ||||
|         } | ||||
|          | ||||
|     } | ||||
|      | ||||
|     public void printTabelaColuna(int coluna){ | ||||
|         System.out.println("  ------------------------"); | ||||
|         int tam = coluna; | ||||
|         for(int i = 0; i < linhas ; i++){ | ||||
|              | ||||
|             System.out.print(" | " + elementos.get(tam)); | ||||
|             tam = tam + this.colunas; | ||||
|             System.out.print(" | "); | ||||
|             System.out.println("\n  --------------------------"); | ||||
|          | ||||
|         } | ||||
|          | ||||
|     } | ||||
|      | ||||
|     @Override | ||||
|     public String toString() { | ||||
|         return "Tabela{" + "nomeTabela=" + nomeTabela + ", linhas=" + linhas + ", tamanho=" + tamanho + ", colunas=" + colunas + '}'; | ||||
|     } | ||||
|  | ||||
|     private void printTabelaResultado(List<String> tabelaResultado, int tcolunas, int tlinhas) { | ||||
|         int tam = 0; | ||||
|         System.out.println("  --------------------------------------------------------------"); | ||||
|         for(int i = 0; i < tlinhas ; i++){ | ||||
|              | ||||
|             for(int j = 0; j<tcolunas ; j++){ | ||||
|                 System.out.print(" | " + tabelaResultado.get(tam)); | ||||
|                 tam++; | ||||
|             } | ||||
|             System.out.print(" | "); | ||||
|             System.out.println("\n  --------------------------------------------------------------"); | ||||
|          | ||||
|         } | ||||
|     } | ||||
|     /* | ||||
|      | ||||
|     private void ordenaTabela(List<String> tabelaResultado, int tcolunas, int tlinhas) { // IMPLEMENTAR O MERGE SORT | ||||
|         int tam = 0; | ||||
|         System.out.println("  --------------------------------------------------------------"); | ||||
|         for(int i = 0; i < tlinhas ; i++){ | ||||
|              | ||||
|             for(int j = 0; j<tcolunas ; j++){ | ||||
|                 System.out.print(" | " + tabelaResultado.get(tam)); | ||||
|                 tam++; | ||||
|             } | ||||
|             System.out.print(" | "); | ||||
|             System.out.println("\n  --------------------------------------------------------------"); | ||||
|          | ||||
|         } | ||||
|     } | ||||
|      | ||||
|     public List<String> comparaIgual(List<String> tabela,String elemento,String chave){ | ||||
|         int n_elementos = selectFila.length; | ||||
|         int n_linhas = this.linhas; | ||||
|         List<String> tabelaResultado = new ArrayList<>(n_elementos*n_linhas); | ||||
|         return tabela; | ||||
|     } | ||||
|     public List<String> comparaDiferente(List<String> tabela,String elemento,String chave){ | ||||
|         return tabela; | ||||
|     } | ||||
|     public List<String> comparaMaior(List<String> tabela,String elemento,String chave){ | ||||
|         return tabela; | ||||
|     } | ||||
|     public boolean comparaMenor(List<String> tabela,String elemento,String chave){ | ||||
|         return false; | ||||
|     }*/ | ||||
|      | ||||
| } | ||||
							
								
								
									
										237
									
								
								java/TrabalhoBD.java
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										237
									
								
								java/TrabalhoBD.java
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,237 @@ | ||||
| /* | ||||
|  * Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license | ||||
|  * Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Main.java to edit this template | ||||
|  */ | ||||
| package trabalhobd; | ||||
|  | ||||
| import java.io.BufferedReader; | ||||
| import java.io.File; | ||||
| import java.io.FileNotFoundException; | ||||
| import java.io.FileReader; | ||||
| import java.io.IOException; | ||||
| import java.util.Arrays; | ||||
| import java.util.LinkedList; | ||||
| import java.util.Queue; | ||||
| import java.util.Scanner; | ||||
|  | ||||
| /** | ||||
|  * | ||||
|  * @author julioc7r | ||||
|  */ | ||||
| public class TrabalhoBD { | ||||
|  | ||||
|     /** | ||||
|      * @param args the command line arguments | ||||
|      */ | ||||
|     public static void main(String[] args) throws FileNotFoundException, IOException { | ||||
|  | ||||
|         // String[] ma = new String[1]; | ||||
|         Scanner input = new Scanner(System.in); | ||||
|  | ||||
|         // EscolheArquivo tela1 = new EscolheArquivo(); Tentativa de fazer pela | ||||
|         // interface gráfica | ||||
|  | ||||
|         BancoDeDados Banco = new BancoDeDados(); // Cria um Banco de Dados | ||||
|  | ||||
|         System.out.println("Digite o local onde está o arquivo?"); // TESTE /Users/julioc7r/testecsv | ||||
|         String caminhoArquivo = "/Users/julioc7r/testecsv";// input.nextLine(); // Fazer a busca por um | ||||
|                                                            // repositório************ | ||||
|  | ||||
|         File arquivoCSV = new File(caminhoArquivo); | ||||
|         File[] lista_arquivosCSV = arquivoCSV.listFiles(); | ||||
|         System.out.println("Digite o local onde está o arquivo?" + lista_arquivosCSV.length | ||||
|                 + Arrays.toString(arquivoCSV.listFiles())); | ||||
|         if (arquivoCSV.exists() == false) { | ||||
|             System.out.println("Arquivo não encontrado/Verifique novamento o caminho"); | ||||
|             return; | ||||
|         } | ||||
|         int n_tabelas = 0; | ||||
|         for (int t = 0; t < lista_arquivosCSV.length; t++) { | ||||
|             if (lista_arquivosCSV[t].getName().contains(".csv")) { | ||||
|                 BufferedReader br = new BufferedReader(new FileReader(lista_arquivosCSV[t])); | ||||
|  | ||||
|                 int qtd_linhas; | ||||
|                 String line = "not null"; | ||||
|                 for (qtd_linhas = -1; line != null; qtd_linhas++) {// conta quantas linhas tem o arquivo | ||||
|                     line = br.readLine(); | ||||
|                 } | ||||
|                 // overwrite old buffered reader/ reseta o arquivo para a primeira posição | ||||
|                 br = new BufferedReader(new FileReader(lista_arquivosCSV[t])); | ||||
|  | ||||
|                 line = br.readLine(); | ||||
|  | ||||
|                 String fator_divisor = ","; | ||||
|                 if (line.contains(",") == true) { | ||||
|                     fator_divisor = ","; | ||||
|                 } else if (line.contains(";") == true) { | ||||
|                     fator_divisor = ";"; | ||||
|                 } | ||||
|  | ||||
|                 String[] vect = line.split(fator_divisor); // Divide pela as colunas da tabela | ||||
|                 String primeiroHeader = vect[0]; | ||||
|  | ||||
|                 System.out.println( | ||||
|                         "nome:" + lista_arquivosCSV[t].getName() + " linhas:" + qtd_linhas + " colunas:" + vect.length); | ||||
|                 Banco.lista_tabelas | ||||
|                         .add(new Tabela(lista_arquivosCSV[t].getName().replace(".csv", ""), qtd_linhas, vect.length)); | ||||
|  | ||||
|                 Banco.setN_tabelas(n_tabelas);// atualiza o numero de tabelas no banco | ||||
|  | ||||
|                 // Banco.lista_tabelas.get(t).toString(); | ||||
|                 // System.out.println(Banco.lista_tabelas.get(t).toString()); | ||||
|  | ||||
|                 while (line != null) { | ||||
|                     System.out.println(line); | ||||
|  | ||||
|                     vect = line.split(fator_divisor); // divide no vetor apartir da virgula | ||||
|                     if (vect[0].equals(primeiroHeader)) { | ||||
|                         for (int i = 0; i < vect.length; i++) { | ||||
|                             vect[i].toLowerCase();// NAOFUNCINOANSdkm | ||||
|                         } | ||||
|                     } | ||||
|                     Banco.lista_tabelas.get(n_tabelas).adicionaElemento(vect); | ||||
|                     line = br.readLine(); | ||||
|                 } | ||||
|  | ||||
|                 System.out.println("Tabela:"); | ||||
|                 Banco.lista_tabelas.get(n_tabelas).printTabela(); | ||||
|                 n_tabelas++; | ||||
|  | ||||
|             } | ||||
|         } | ||||
|         int opção = 0; | ||||
|         String pesquisa; | ||||
|         char confirma = 'N'; | ||||
|         String elemento; | ||||
|  | ||||
|         do { | ||||
|             System.out.println("TABELAS:"); | ||||
|             for (int i = 0; i < Banco.getN_tabelas(); i++) { | ||||
|                 System.out.println("nome:" + Banco.lista_tabelas.get(i).toString()); | ||||
|             } | ||||
|             System.out.println("Bem-vindo!"); | ||||
|             System.out.println("=========="); | ||||
|             System.out.println("1 - Query"); | ||||
|             System.out.println("9 - Sair"); | ||||
|             System.out.println(":"); | ||||
|             opção = input.nextInt(); | ||||
|  | ||||
|             switch (opção) { | ||||
|                 case 1: | ||||
|                     System.out.println("\nDIGITE A SQL QUEUE"); | ||||
|                     elemento = input.nextLine(); | ||||
|                     elemento = input.nextLine(); | ||||
|  | ||||
|                     String[] vect = elemento.split(" "); | ||||
|  | ||||
|                     Queue<String> query = new LinkedList(); | ||||
|                     int orderBy = 0; // Flag para se possuir o orderby | ||||
|  | ||||
|                     // QUEUE TEST 1 | select emp_no birth_date from employees where // FUNCIONA | ||||
|                     // QUEUE TEST 2 | select first_name gender from employees1000 order by gender // | ||||
|                     // FUNCIONA | ||||
|  | ||||
|                     // CAMINHOTESTE /Users/julioc7r/testecsv | ||||
|  | ||||
|                     // emp_no,birth_date,first_name,last_name,gender,hire_date | ||||
|  | ||||
|                     for (int i = 0; i < vect.length; i++) { // coloca tudo em letra minuscula e retira a virgula e o | ||||
|                         System.out.println(vect[i]); // ponto e virgula que fica na hora que a string é dividida no | ||||
|                                                      // vetor vect[]. | ||||
|                         vect[i].toLowerCase(); | ||||
|  | ||||
|                         if (vect[i].contains(",")) { | ||||
|                             vect[i].replace(",", ""); | ||||
|                         } | ||||
|                         if (vect[i].contains(";")) { | ||||
|                             vect[i].replace(";", ""); | ||||
|                         } | ||||
|                         query.add(vect[i]); // Adiciona na fila Query | ||||
|                     } | ||||
|  | ||||
|                     Banco.executaSQL(query); | ||||
|                     /* | ||||
|                      * | ||||
|                      * for(int i = 0 ; i < vect.length ;i ++) | ||||
|                      * { | ||||
|                      * System.out.println("entrei"+ i); | ||||
|                      * if(i == 0 && vect[i].equals("select")){ | ||||
|                      * i++; | ||||
|                      * query.poll(); | ||||
|                      * int j = 1; | ||||
|                      * while(vect[i].equals("from")== false){//conta elementos do SELECT | ||||
|                      * i++; | ||||
|                      * j++; | ||||
|                      * } | ||||
|                      * i--; | ||||
|                      * selectList = new String[j]; | ||||
|                      * j = 0; | ||||
|                      * do{ | ||||
|                      * System.out.println(vect[j+1]+" - "+selectList[j]+j); | ||||
|                      * //selectList[j] = vect[j+1]; | ||||
|                      * selectList[j] = query.poll(); | ||||
|                      * System.out.println(vect[j+1]+" - "+selectList[j]+j); | ||||
|                      * j++; | ||||
|                      * }while(vect[j+1].equals("from")== false); | ||||
|                      * } | ||||
|                      * | ||||
|                      * if(vect[i].equals("from")){ | ||||
|                      * //i++; | ||||
|                      * int j = 0; | ||||
|                      * while((vect[i+1].equals("where")== false)){//||(vect[i+1].equals("order")== | ||||
|                      * false)){//conta elementos do SELECT | ||||
|                      * i++; | ||||
|                      * j++; | ||||
|                      * System.out.println("entrei"+ i + j); | ||||
|                      * } | ||||
|                      * i-=j-1; | ||||
|                      * fromList = new String[j]; | ||||
|                      * j = 0; | ||||
|                      * do{ | ||||
|                      * System.out.println(vect[i]+" - "+fromList[j]+j); | ||||
|                      * fromList[j] = vect[i]; | ||||
|                      * System.out.println(vect[i]+" - "+fromList[j]+j+"i "+i); | ||||
|                      * j++; | ||||
|                      * i++; | ||||
|                      * }while((vect[i].equals("where")== false));//||(vect[i].equals("order")== | ||||
|                      * false)); | ||||
|                      * } | ||||
|                      * | ||||
|                      * if(vect[i].equals("order") && vect[i+1].equals("by")){ | ||||
|                      * orderBy=1; | ||||
|                      * i = i + 2; | ||||
|                      * System.out.println("oi"+i); | ||||
|                      * int k; // posição do atributo que vai ser usado para ordenar a tabela no | ||||
|                      * selectList | ||||
|                      * if(vect[i].equals(selectList[0])== false){ | ||||
|                      * for(k = 1; k < selectList.length; k++){ | ||||
|                      * if(vect[i].equals(selectList[k])) | ||||
|                      * break; | ||||
|                      * } | ||||
|                      * String swap = selectList[0]; | ||||
|                      * selectList[0] = selectList [k]; | ||||
|                      * selectList[k] = swap; | ||||
|                      * System.out.println("oi"); | ||||
|                      * } | ||||
|                      * } | ||||
|                      * | ||||
|                      * } | ||||
|                      * | ||||
|                      * int i; | ||||
|                      * | ||||
|                      * Tabela t = lista_tabelas.get(0); | ||||
|                      * t.resultadoQuery(selectList,orderBy); | ||||
|                      */ | ||||
|  | ||||
|                 case 9: | ||||
|                     System.out.println("Tem certeza que deseja sair?(S)para sair"); | ||||
|                     confirma = input.next().toUpperCase().charAt(0); | ||||
|                     break; | ||||
|  | ||||
|             } | ||||
|         } while (confirma != 'S'); | ||||
|  | ||||
|         input.close(); | ||||
|     } | ||||
|  | ||||
| } | ||||
							
								
								
									
										1
									
								
								java/sourcecsv/teste.csv
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										1
									
								
								java/sourcecsv/teste.csv
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1 @@ | ||||
|  | ||||
|  | 
							
								
								
									
										36
									
								
								main.py
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										36
									
								
								main.py
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,36 @@ | ||||
| import os | ||||
|  | ||||
| from table import Table | ||||
| from sql import SQL | ||||
|  | ||||
| def main(): | ||||
|     print("Bem vindo ao SGBD") | ||||
|     print("Digite a pasta onde está os arquivos CSV:") | ||||
|     #csv_folder = input() | ||||
|     csv_folder = "/mnt/c/Users/jose/Desktop/jcSQL-main/source" | ||||
|  | ||||
|     tables = {} | ||||
|  | ||||
|     # get all csv files in the folder | ||||
|     for file in os.listdir(csv_folder): | ||||
|         if file.endswith(".csv"): | ||||
|             table_name = file.split(".")[0] | ||||
|             table = Table(table_name) | ||||
|             table.load_from_csv(f"{csv_folder}/{file}") | ||||
|             tables[table_name] = table | ||||
|  | ||||
|     print("Tabelas carregadas:") | ||||
|     print(tables.keys()) | ||||
|  | ||||
|     while True: | ||||
|         print("Digite a query [0 para sair]:") | ||||
|         query = input() | ||||
|  | ||||
|         if query == "0": | ||||
|             break | ||||
|  | ||||
|         sql = SQL(tables, query) | ||||
|         sql.execute() | ||||
|  | ||||
| if __name__ == "__main__": | ||||
|     main() | ||||
							
								
								
									
										4
									
								
								source/city.csv
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										4
									
								
								source/city.csv
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,4 @@ | ||||
| city,state,country | ||||
| London,England,United Kingdom | ||||
| Kansas City,Kansas,United States | ||||
| Rio de Janeiro,Rio de Janeiro,Brazil | ||||
| 
 | 
							
								
								
									
										4
									
								
								source/person.csv
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										4
									
								
								source/person.csv
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,4 @@ | ||||
| name,surname,age,city | ||||
| Bob,Smith,32,London | ||||
| Angelina,Jolie,90,Kansas City | ||||
| Wellerton,Abigail,4,Rio de Janeiro | ||||
| 
 | 
							
								
								
									
										41
									
								
								sql.py
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										41
									
								
								sql.py
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,41 @@ | ||||
| from table import Table | ||||
|  | ||||
| class SQL: | ||||
|     def __init__(self, tables: dict, query: str) -> None: | ||||
|         self.tables = tables | ||||
|         self.query = query.replace(";", "") | ||||
|  | ||||
|     def __str__(self) -> str: | ||||
|         pass | ||||
|  | ||||
|     # SELECIONAR * DE tabela | ||||
|     def execute(self): | ||||
|         query_parts = self.query.split(" ") | ||||
|  | ||||
|         if query_parts[0] == "SELECIONAR": | ||||
|             self.select(query_parts) | ||||
|         else: | ||||
|             pass | ||||
|  | ||||
|     # SELECIONAR * DE tabela ONDE coluna = valor JUNTAR tabela2 coluna | ||||
|     def select(self, query_parts: list) -> None: | ||||
|         where_filter = None | ||||
|         join_stmt = None | ||||
|  | ||||
|         if "ONDE" in query_parts: | ||||
|             index = query_parts.index("ONDE") | ||||
|             where_filter_column = query_parts[index + 1] | ||||
|             where_filter_operator = query_parts[index + 2] | ||||
|             where_filter_value = query_parts[index + 3] | ||||
|             where_filter = [where_filter_column, where_filter_operator, where_filter_value] | ||||
|  | ||||
|         if "JUNTAR" in query_parts: | ||||
|             index = query_parts.index("JUNTAR") | ||||
|             join_table = query_parts[index + 1] | ||||
|             join_column = query_parts[index + 2] | ||||
|             join_stmt = [self.tables[join_table], join_column] | ||||
|  | ||||
|         table = self.tables[query_parts[3]] | ||||
|         table.print_columns(table.columns, | ||||
|                             where_filter=where_filter, | ||||
|                             join_stmt=join_stmt) | ||||
							
								
								
									
										52
									
								
								table.py
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										52
									
								
								table.py
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,52 @@ | ||||
| class Table: | ||||
|     def __init__(self, name): | ||||
|         self.name = name | ||||
|         self.columns = [] | ||||
|         self.rows = [] | ||||
|  | ||||
|     def __str__(self): | ||||
|         return self.name | ||||
|  | ||||
|     def __repr__(self): | ||||
|         return self.name | ||||
|  | ||||
|     def load_from_csv(self, csv_file): | ||||
|         with open(csv_file, 'r') as f: | ||||
|             reader = f.read().splitlines() | ||||
|  | ||||
|             # First line is the column names | ||||
|             self.columns = reader[0].split(',') | ||||
|             reader.pop(0) | ||||
|  | ||||
|             for row in reader: | ||||
|                 row = row.split(',') | ||||
|                 self.rows.append(row) | ||||
|  | ||||
|     def print_columns(self, column_list: list, where_filter: list = None, join_stmt: list = None): | ||||
|         if column_list[0] == '*': | ||||
|             column_list = self.columns | ||||
|  | ||||
|         print(', '.join(column_list)) | ||||
|         print('-' * 20) | ||||
|  | ||||
|         for row in self.rows: | ||||
|             if where_filter is not None: | ||||
|                 column_index = self.columns.index(where_filter[0]) | ||||
|                 row_value = row[column_index] | ||||
|  | ||||
|                 if where_filter[1] == '=': | ||||
|                     if row_value != where_filter[2]: | ||||
|                         continue | ||||
|                 elif where_filter[1] == '>': | ||||
|                     if row_value <= where_filter[2]: | ||||
|                         continue | ||||
|                 elif where_filter[1] == '<': | ||||
|                     if row_value >= where_filter[2]: | ||||
|                         continue | ||||
|  | ||||
|             row_values = [] | ||||
|             for column in column_list: | ||||
|                 column_index = self.columns.index(column) | ||||
|                 row_values.append(row[column_index]) | ||||
|  | ||||
|             print(', '.join(row_values)) | ||||
		Reference in New Issue
	
	Block a user