public TabelaSelect returnDados(List<Filtro> filtrosAND, Dictionary<string, List<string>> filtrosJoin, Metadados tabela) { try { Base.getInstance().desalocarBinarios(tabela.getNome()); } catch { } TabelaSelect ts = null; FileStream file = null; BinaryReader br = null; try { string arquivo = mem.getPath() + "\\" + tabela.getNome() + ".dat"; file = new FileStream(arquivo, FileMode.Open); using (br = new BinaryReader(file)) { int count; ts = new TabelaSelect(); Metadados meta = GerenciadorMemoria.getInstance().recuperarMetadados(tabela.getNome()); int colunas = meta.getNomesColunas().Count; ts.Campos = new string[colunas]; for (int i = 0; i < colunas; i++) { ts.Campos[i] = meta.getNome() + "." + meta.getNomesColunas()[i]; } //calcula o tamanho de cada registro int tamRegistro = 12; int posPrimary = 0;//posicao do primary key no registro foreach (DadosTabela dados in meta.getDados().Values) { if (dados.isPrimary()) posPrimary = tamRegistro + 2; tamRegistro += dados.getTamanho() + 2; } long posMax = br.BaseStream.Length; //posicao máxima para leitura do arquivo //organiza os filtros por coluna List<Filtro>[] filtrosCampo = new List<Filtro>[colunas]; for (int i = 0; i < colunas; i++) { ts.Campos[i] = meta.getNome() + "." + meta.getNomesColunas()[i]; filtrosCampo[i] = new List<Filtro>(); foreach (Filtro f in filtrosAND) { if (f.LValue.Equals(ts.Campos[i])) filtrosCampo[i].Add(f); } if (filtrosJoin.ContainsKey(ts.Campos[i])) { posMax = posicionaPonteiroArquivo(filtrosJoin[ts.Campos[i]], br, tamRegistro, posPrimary); } //se o campo for PrimaryKey organiza o filtro else if (filtrosCampo[i].Count > 0 && meta.getDados()[meta.getNomesColunas()[i]].isPrimary()) { ordenaFiltros(filtrosCampo[i]); //define o intervalo de consulta do arquivo caso exista filtro de chave primaria posMax = posicionaPonteiroArquivo(filtrosCampo[i], br, tamRegistro, posPrimary); } } //lê cada registro while (br.BaseStream.Position < posMax) { string[] registro = new string[colunas]; long posicao = br.ReadInt64(); count = br.ReadInt32(); bool insere = true; //Lê cada dado dentro do registro for (int i = 0; i < count && insere; i++) { string nomeColuna = meta.getNomesColunas()[i]; TipoDado tipo = meta.getDados()[nomeColuna].getTipoDado(); bool isPrimary = meta.getDados()[nomeColuna].isPrimary(); string valor = ""; string campo = meta.getNome() + "." + nomeColuna; if (tipo == TipoDado.Inteiro) { byte tamanho = br.ReadByte(); bool isValido = br.ReadBoolean(); int numero = br.ReadInt32(); valor = isValido ? numero + "" : "NULL"; foreach (Filtro f in filtrosCampo[i]) { switch (f.Op) { case OperadorRel.Igual: if (f.RValue.ToLower().Equals("null")) { if (isValido) insere = false; } else { if (numero != Convert.ToInt32(f.RValue)) insere = false; } break; case OperadorRel.MaiorQue: if (numero <= Convert.ToInt32(f.RValue)) insere = false; break; case OperadorRel.MenorQue: if (numero >= Convert.ToInt32(f.RValue)) insere = false; break; case OperadorRel.MaiorIgualA: if (numero < Convert.ToInt32(f.RValue)) insere = false; break; case OperadorRel.MenorIgualA: if (numero > Convert.ToInt32(f.RValue)) insere = false; break; case OperadorRel.Diferente: if (f.RValue.ToLower().Equals("null")) { if (!isValido) insere = false; } else { if (numero == Convert.ToInt32(f.RValue)) insere = false; } break; default: throw new SGDBException("Passou onde nao devia: GambiarraSelect.retornaDados.Inteiro.Default."); } } if(insere && filtrosJoin.ContainsKey(campo)) { insere = filtrosJoin[campo].Exists(s => s.Equals(valor)); } } else { byte tamanho = br.ReadByte(); bool isValido = br.ReadBoolean(); byte[] literal = br.ReadBytes(tamanho); string texto = new System.Text.ASCIIEncoding().GetString(literal); valor = isValido ? texto.TrimEnd() : "NULL"; foreach (Filtro f in filtrosCampo[i]) { switch (f.Op) { case OperadorRel.Igual: if (f.RValue.ToLower().Equals("null")) { if (isValido) insere = false; } else { byte[] filtro = new byte[tamanho]; new System.Text.ASCIIEncoding().GetBytes(f.RValue.PadRight(tamanho)).CopyTo(filtro, 0); string filtro2 = new System.Text.ASCIIEncoding().GetString(filtro).TrimEnd(); if (!valor.Equals(filtro2)) insere = false; } break; case OperadorRel.Diferente: if (f.RValue.ToLower().Equals("null")) { if (isValido) insere = false; } else { byte[] filtro = new byte[tamanho]; new System.Text.ASCIIEncoding().GetBytes(f.RValue.PadRight(tamanho)).CopyTo(filtro, 0); string filtro2 = new System.Text.ASCIIEncoding().GetString(filtro).TrimEnd(); if (valor.Equals(filtro2)) insere = false; } break; default: throw new SemanticError("Comparação de literais só pode ser igual ou diferente"); } } if (insere && filtrosJoin.ContainsKey(campo)) { insere = filtrosJoin[campo].Exists(s => s.Equals(valor)); } } registro[i] = valor; } if (insere) { ts.Registros.Add(registro); if (ts.Registros.Count >= Base.getInstance().qtd_max_registros) break; } if (br.BaseStream.Position % tamRegistro != 0) br.BaseStream.Position += tamRegistro - (br.BaseStream.Position % tamRegistro); } } } catch (Exception ex) { Console.WriteLine(ex.StackTrace); Console.ReadLine(); } finally { if (br != null) { br.Close(); } if (file != null) { file.Close(); } } return ts; }
public TabelaSelect returnDados(List <Filtro> filtrosAND, Dictionary <string, List <string> > filtrosJoin, Metadados tabela) { try { Base.getInstance().desalocarBinarios(tabela.getNome()); } catch { } TabelaSelect ts = null; FileStream file = null; BinaryReader br = null; try { string arquivo = mem.getPath() + "\\" + tabela.getNome() + ".dat"; file = new FileStream(arquivo, FileMode.Open); using (br = new BinaryReader(file)) { int count; ts = new TabelaSelect(); Metadados meta = GerenciadorMemoria.getInstance().recuperarMetadados(tabela.getNome()); int colunas = meta.getNomesColunas().Count; ts.Campos = new string[colunas]; for (int i = 0; i < colunas; i++) { ts.Campos[i] = meta.getNome() + "." + meta.getNomesColunas()[i]; } //calcula o tamanho de cada registro int tamRegistro = 12; int posPrimary = 0;//posicao do primary key no registro foreach (DadosTabela dados in meta.getDados().Values) { if (dados.isPrimary()) { posPrimary = tamRegistro + 2; } tamRegistro += dados.getTamanho() + 2; } long posMax = br.BaseStream.Length; //posicao máxima para leitura do arquivo //organiza os filtros por coluna List <Filtro>[] filtrosCampo = new List <Filtro> [colunas]; for (int i = 0; i < colunas; i++) { ts.Campos[i] = meta.getNome() + "." + meta.getNomesColunas()[i]; filtrosCampo[i] = new List <Filtro>(); foreach (Filtro f in filtrosAND) { if (f.LValue.Equals(ts.Campos[i])) { filtrosCampo[i].Add(f); } } if (filtrosJoin.ContainsKey(ts.Campos[i])) { posMax = posicionaPonteiroArquivo(filtrosJoin[ts.Campos[i]], br, tamRegistro, posPrimary); } //se o campo for PrimaryKey organiza o filtro else if (filtrosCampo[i].Count > 0 && meta.getDados()[meta.getNomesColunas()[i]].isPrimary()) { ordenaFiltros(filtrosCampo[i]); //define o intervalo de consulta do arquivo caso exista filtro de chave primaria posMax = posicionaPonteiroArquivo(filtrosCampo[i], br, tamRegistro, posPrimary); } } //lê cada registro while (br.BaseStream.Position < posMax) { string[] registro = new string[colunas]; long posicao = br.ReadInt64(); count = br.ReadInt32(); bool insere = true; //Lê cada dado dentro do registro for (int i = 0; i < count && insere; i++) { string nomeColuna = meta.getNomesColunas()[i]; TipoDado tipo = meta.getDados()[nomeColuna].getTipoDado(); bool isPrimary = meta.getDados()[nomeColuna].isPrimary(); string valor = ""; string campo = meta.getNome() + "." + nomeColuna; if (tipo == TipoDado.Inteiro) { byte tamanho = br.ReadByte(); bool isValido = br.ReadBoolean(); int numero = br.ReadInt32(); valor = isValido ? numero + "" : "NULL"; foreach (Filtro f in filtrosCampo[i]) { switch (f.Op) { case OperadorRel.Igual: if (f.RValue.ToLower().Equals("null")) { if (isValido) { insere = false; } } else { if (numero != Convert.ToInt32(f.RValue)) { insere = false; } } break; case OperadorRel.MaiorQue: if (numero <= Convert.ToInt32(f.RValue)) { insere = false; } break; case OperadorRel.MenorQue: if (numero >= Convert.ToInt32(f.RValue)) { insere = false; } break; case OperadorRel.MaiorIgualA: if (numero < Convert.ToInt32(f.RValue)) { insere = false; } break; case OperadorRel.MenorIgualA: if (numero > Convert.ToInt32(f.RValue)) { insere = false; } break; case OperadorRel.Diferente: if (f.RValue.ToLower().Equals("null")) { if (!isValido) { insere = false; } } else { if (numero == Convert.ToInt32(f.RValue)) { insere = false; } } break; default: throw new SGDBException("Passou onde nao devia: GambiarraSelect.retornaDados.Inteiro.Default."); } } if (insere && filtrosJoin.ContainsKey(campo)) { insere = filtrosJoin[campo].Exists(s => s.Equals(valor)); } } else { byte tamanho = br.ReadByte(); bool isValido = br.ReadBoolean(); byte[] literal = br.ReadBytes(tamanho); string texto = new System.Text.ASCIIEncoding().GetString(literal); valor = isValido ? texto.TrimEnd() : "NULL"; foreach (Filtro f in filtrosCampo[i]) { switch (f.Op) { case OperadorRel.Igual: if (f.RValue.ToLower().Equals("null")) { if (isValido) { insere = false; } } else { byte[] filtro = new byte[tamanho]; new System.Text.ASCIIEncoding().GetBytes(f.RValue.PadRight(tamanho)).CopyTo(filtro, 0); string filtro2 = new System.Text.ASCIIEncoding().GetString(filtro).TrimEnd(); if (!valor.Equals(filtro2)) { insere = false; } } break; case OperadorRel.Diferente: if (f.RValue.ToLower().Equals("null")) { if (isValido) { insere = false; } } else { byte[] filtro = new byte[tamanho]; new System.Text.ASCIIEncoding().GetBytes(f.RValue.PadRight(tamanho)).CopyTo(filtro, 0); string filtro2 = new System.Text.ASCIIEncoding().GetString(filtro).TrimEnd(); if (valor.Equals(filtro2)) { insere = false; } } break; default: throw new SemanticError("Comparação de literais só pode ser igual ou diferente"); } } if (insere && filtrosJoin.ContainsKey(campo)) { insere = filtrosJoin[campo].Exists(s => s.Equals(valor)); } } registro[i] = valor; } if (insere) { ts.Registros.Add(registro); if (ts.Registros.Count >= Base.getInstance().qtd_max_registros) { break; } } if (br.BaseStream.Position % tamRegistro != 0) { br.BaseStream.Position += tamRegistro - (br.BaseStream.Position % tamRegistro); } } } } catch (Exception ex) { Console.WriteLine(ex.StackTrace); Console.ReadLine(); } finally { if (br != null) { br.Close(); } if (file != null) { file.Close(); } } return(ts); }