public static CSVFormat setFormat()

in spanner/jdbc/src/main/java/com/example/spanner/jdbc/LoadCsvExample.java [165:209]


  public static CSVFormat setFormat(CommandLine cmd) {
    CSVFormat parseFormat;
    // Set file format type
    if (cmd.hasOption("f")) {
      switch (cmd.getOptionValue("f").toUpperCase()) {
        case EXCEL:
          parseFormat = CSVFormat.EXCEL;
          break;
        case POSTGRESQL_CSV:
          parseFormat = CSVFormat.POSTGRESQL_CSV;
          break;
        case POSTGRESQL_TEXT:
          parseFormat = CSVFormat.POSTGRESQL_TEXT;
          break;
        default:
          parseFormat = CSVFormat.DEFAULT;
      }
    } else {
      parseFormat = CSVFormat.DEFAULT;
    }
    // Set null string representation
    if (cmd.hasOption("n")) {
      parseFormat = parseFormat.withNullString(cmd.getOptionValue("n"));
    }
    // Set delimiter character
    if (cmd.hasOption("d")) {
      if (cmd.getOptionValue("d").length() != 1) {
        throw new IllegalArgumentException("Invalid delimiter character entered.");
      }
      parseFormat = parseFormat.withDelimiter(cmd.getOptionValue("d").charAt(0));
    }
    // Set escape character
    if (cmd.hasOption("e")) {
      if (cmd.getOptionValue("e").length() != 1) {
        throw new IllegalArgumentException("Invalid escape character entered.");
      }
      parseFormat = parseFormat.withEscape(cmd.getOptionValue("e").charAt(0));
    }
    // Set parser to parse first row as headers
    if (cmd.hasOption("h") && cmd.getOptionValue("h").equalsIgnoreCase("True")) {
      parseFormat = parseFormat.withFirstRecordAsHeader();
      hasHeader = true;
    }
    return parseFormat;
  }