andcastillo / fdp2017

MIT License
1 stars 4 forks source link

Funcion de Seleccion #2

Open ArangoGutierrez opened 6 years ago

ArangoGutierrez commented 6 years ago

Funcion de seleccion

Caso :

SELECT * FROM table WHERE condition

Donde condition : columnId=value

La funcion que realiza la busqueda de la condicion es

         // Selection Scan
        String SelectionScan = "condition";
        Int Column = 2
        while (scanner.hasNext()) {
            List<String> line = parseLine(scanner.nextLine());
            String scanValue = line.get(Column);
              if ( SelectionScan.equals(scanValue) ) {
                printLine(line);
              }
        }

Su implementacion completa tomando los datos de un csv ubicado en data/example.csv con la estructura [id(int), string, Bool, Double]

import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

public class CSVUtils {

    private static final char DEFAULT_SEPARATOR = ',';
    private static final char DEFAULT_QUOTE = '"';

    public static void main(String[] args) throws Exception {

        String csvFile = "data/example.csv";

        Scanner scanner = new Scanner(new File(csvFile));

         // Selection Scan
        String indexScan = "true";
        while (scanner.hasNext()) {
            List<String> line = parseLine(scanner.nextLine());
            String scanValue = line.get(2);
              if ( indexScan.equals(scanValue) ) {
                printLine(line);
              }
        }
        scanner.close();

    }

    public static List<String> parseLine(String cvsLine) {
        return parseLine(cvsLine, DEFAULT_SEPARATOR, DEFAULT_QUOTE);
    }

    public static void printLine(List<String> line) {
        System.out.println("id="+ line.get(0)+ " b= " + line.get(1) + ", c= " + line.get(2) + " , x=" + line.get(3));
    }

    public static void selectFromLine(List<String> line, int[] t) {
      String select = "";
      //
      for (int i = 0; i < t.length; i++) {
        select = select + line.get(i) + " ";
      }
      System.out.println(select);
    }

    public static List<String> parseLine(String cvsLine, char separators) {
        return parseLine(cvsLine, separators, DEFAULT_QUOTE);
    }

    public static List<String> parseLine(String cvsLine, char separators, char customQuote) {

        List<String> result = new ArrayList<>();

        //if empty, return!
        if (cvsLine == null && cvsLine.isEmpty()) {
            return result;
        }

        if (customQuote == ' ') {
            customQuote = DEFAULT_QUOTE;
        }

        if (separators == ' ') {
            separators = DEFAULT_SEPARATOR;
        }

        StringBuffer curVal = new StringBuffer();
        boolean inQuotes = false;
        boolean startCollectChar = false;
        boolean doubleQuotesInColumn = false;

        char[] chars = cvsLine.toCharArray();

        for (char ch : chars) {

            if (inQuotes) {
                startCollectChar = true;
                if (ch == customQuote) {
                    inQuotes = false;
                    doubleQuotesInColumn = false;
                } else {

                    //Fixed : allow "" in custom quote enclosed
                    if (ch == '\"') {
                        if (!doubleQuotesInColumn) {
                            curVal.append(ch);
                            doubleQuotesInColumn = true;
                        }
                    } else {
                        curVal.append(ch);
                    }

                }
            } else {
                if (ch == customQuote) {

                    inQuotes = true;

                    //Fixed : allow "" in empty quote enclosed
                    if (chars[0] != '"' && customQuote == '\"') {
                        curVal.append('"');
                    }

                    //double quotes in column will hit this!
                    if (startCollectChar) {
                        curVal.append('"');
                    }

                } else if (ch == separators) {

                    result.add(curVal.toString());

                    curVal = new StringBuffer();
                    startCollectChar = false;

                } else if (ch == '\r') {
                    //ignore LF characters
                    continue;
                } else if (ch == '\n') {
                    //the end, break!
                    break;
                } else {
                    curVal.append(ch);
                }
            }

        }

        result.add(curVal.toString());

        return result;
    }

}
ArangoGutierrez commented 6 years ago

Esta implementacion tambien se puede reciclar para la funcion de proyeccion, usando

List<String> line = parseLine(scanner.nextLine());
line.get(X)

donde x sea la columna a proyectar (o las columnas en serie)

ArangoGutierrez commented 6 years ago

Funcion implementada en Commit # f6e7d1a TODO : Switch :Case para los operadores aritmeticos [=,>,<]