Csv csv = new Csv(); String options = argList.length < 3 ? null : argList[2].getValue(session).getString(); String charset = null; if (options != null && options.indexOf('=') >= 0) { charset = csv.setOptions(options); } else { charset = options; escapeCharacter); char fieldSeparator = csv.getFieldSeparatorRead(); String[] columns = StringUtils.arraySplit(columnList, fieldSeparator, true); ResultSet rs = null; ValueResultSet x; try { rs = csv.read(fileName, columns, charset); x = ValueResultSet.getCopy(rs, 0); } catch (SQLException e) { throw DbException.convert(e); } finally { csv.close(); JdbcUtils.closeSilently(rs);
String value = pair.substring(index + 1); char ch = value.length() == 0 ? 0 : value.charAt(0); if (isParam(key, "escape", "esc", "escapeCharacter")) { setEscapeCharacter(ch); } else if (isParam(key, "fieldDelimiter", "fieldDelim")) { setFieldDelimiter(ch); } else if (isParam(key, "fieldSeparator", "fieldSep")) { setFieldSeparatorRead(ch); setFieldSeparatorWrite(value); } else if (isParam(key, "lineComment", "lineCommentCharacter")) { setLineCommentCharacter(ch); } else if (isParam(key, "lineSeparator", "lineSep")) { setLineSeparator(value); } else if (isParam(key, "null", "nullString")) { setNullString(value); } else if (isParam(key, "charset", "characterSet")) { charset = value; } else if (isParam(key, "preserveWhitespace")) { setPreserveWhitespace(Utils.parseBoolean(value, false, false)); } else if (isParam(key, "writeColumnHeader")) { setWriteColumnHeader(Utils.parseBoolean(value, true, false)); } else if (isParam(key, "caseSensitiveColumnNames")) { setCaseSensitiveColumnNames(Utils.parseBoolean(value, false, false)); } else { throw DbException.getUnsupportedException(key);
/** * Writes the result set of a query to a file in the CSV format. * * @param conn the connection * @param outputFileName the file name * @param sql the query * @param charset the charset or null to use the system default charset * (see system property file.encoding) * @return the number of rows written */ public int write(Connection conn, String outputFileName, String sql, String charset) throws SQLException { Statement stat = conn.createStatement(); ResultSet rs = stat.executeQuery(sql); int rows = write(outputFileName, rs, charset); stat.close(); return rows; }
/** * Reads from the CSV file and returns a result set. The rows in the result * set are created on demand, that means the file is kept open until all * rows are read or the result set is closed. * <br /> * If the columns are read from the CSV file, then the following rules are * used: columns names that start with a letter or '_', and only * contain letters, '_', and digits, are considered case insensitive * and are converted to uppercase. Other column names are considered * case sensitive (that means they need to be quoted when accessed). * * @param inputFileName the file name * @param colNames or null if the column names should be read from the CSV * file * @param charset the charset or null to use the system default charset * (see system property file.encoding) * @return the result set */ public ResultSet read(String inputFileName, String[] colNames, String charset) throws SQLException { init(inputFileName, charset); try { return readResultSet(colNames); } catch (IOException e) { throw convertException("IOException reading " + inputFileName, e); } }
/** * Writes the result set to a file in the CSV format. The result set is read * using the following loop: * * <pre> * while (rs.next()) { * writeRow(row); * } * </pre> * * @param outputFileName the name of the csv file * @param rs the result set - the result set must be positioned before the * first row. * @param charset the charset or null to use the system default charset * (see system property file.encoding) * @return the number of rows written */ public int write(String outputFileName, ResultSet rs, String charset) throws SQLException { init(outputFileName, charset); try { initWrite(); return writeResultSet(rs); } catch (IOException e) { throw convertException("IOException writing " + outputFileName, e); } }
int i = 0; while (true) { String v = readValue(); if (v == null) { if (endOfLine) { throw convertException("IOException reading from " + fileName, e);
String fileName = v0.getString(); String columnList = v1 == null ? null : v1.getString(); Csv csv = new Csv(); String options = v2 == null ? null : v2.getString(); String charset = null; if (options != null && options.indexOf('=') >= 0) { charset = csv.setOptions(options); } else { charset = options; setCsvDelimiterEscape(csv, fieldSeparatorRead, fieldDelimiter, escapeCharacter); csv.setNullString(nullString); char fieldSeparator = csv.getFieldSeparatorRead(); String[] columns = StringUtils.arraySplit(columnList, fieldSeparator, true); try { result = ValueResultSet.get(csv.read(fileName, columns, charset)); } catch (SQLException e) { session.getUser().checkAdmin(); Connection conn = session.createConnection(false); Csv csv = new Csv(); String options = v2 == null ? null : v2.getString(); String charset = null; if (options != null && options.indexOf('=') >= 0) { charset = csv.setOptions(options);
int average_row_size = 0; Csv csv = new Csv(); csv.setFieldDelimiter('\t'); csv.setFieldSeparatorRead('\t'); ResultSet reader = csv.read(new BufferedReader(new InputStreamReader(fis)), null); ResultSetMetaData metadata = reader.getMetaData(); int columnCount = metadata.getColumnCount();
private static void setCsvDelimiterEscape(Csv csv, String fieldSeparator, String fieldDelimiter, String escapeCharacter) { if (fieldSeparator != null) { csv.setFieldSeparatorWrite(fieldSeparator); if (fieldSeparator.length() > 0) { char fs = fieldSeparator.charAt(0); csv.setFieldSeparatorRead(fs); } } if (fieldDelimiter != null) { char fd = fieldDelimiter.length() == 0 ? 0 : fieldDelimiter.charAt(0); csv.setFieldDelimiter(fd); } if (escapeCharacter != null) { char ec = escapeCharacter.length() == 0 ? 0 : escapeCharacter.charAt(0); csv.setEscapeCharacter(ec); } }
@Override public void exportTable(Connection connection, String tableReference, File fileName, ProgressVisitor progress) throws SQLException, IOException { if (FileUtil.isExtensionWellFormated(fileName, "tsv")) { final boolean isH2 = JDBCUtilities.isH2DataBase(connection.getMetaData()); TableLocation location = TableLocation.parse(tableReference, isH2); Statement st = null; try { st = connection.createStatement(); Csv csv = new Csv(); csv.setFieldDelimiter('\t'); csv.setFieldSeparatorWrite("\t"); csv.write(fileName.getPath(), st.executeQuery("SELECT * FROM " + location.toString()), null); } finally { if (st != null) { st.close(); } } } else { throw new SQLException("Only .tsv extension is supported"); } }
private void parse(Reader reader) throws SQLException, IOException { Rule functions = null; statements = New.arrayList(); Csv csv = new Csv(); csv.setLineCommentCharacter('#'); ResultSet rs = csv.read(reader, null); while (rs.next()) { String section = rs.getString("SECTION").trim();
/** * Export a resultset to a TSV file * * @param connection * @param res * @param fileName * @param progress * @param encoding * @throws java.sql.SQLException */ public void exportFromResultSet(Connection connection, ResultSet res, File fileName, ProgressVisitor progress, String encoding) throws SQLException { Csv csv = new Csv(); String csvOptions = "charset=UTF-8 fieldSeparator=\t fieldDelimiter=\t"; if (encoding != null) { csvOptions = String.format("charset=%s fieldSeparator=\t fieldDelimiter=\t", encoding); } csv.setOptions(csvOptions); csv.write(fileName.getPath(), res, null); }
Csv csv = new Csv(); if (csvOptions != null && csvOptions.indexOf('=') >= 0) { csv.setOptions(csvOptions); ResultSet reader = csv.read(new BufferedReader(new InputStreamReader(fis)), null); ResultSetMetaData metadata = reader.getMetaData(); int columnCount = metadata.getColumnCount();
Value v6 = getNullOrValue(session, args, 6); String nullString = v6 == null ? null : v6.getString(); Csv csv = Csv.getInstance(); setCsvDelimiterEscape(csv, fieldSeparatorRead, fieldDelimiter, escapeCharacter); csv.setNullString(nullString); char fieldSeparator = csv.getFieldSeparatorRead(); String[] columns = StringUtils.arraySplit(columnList, fieldSeparator, true); ValueResultSet vr = ValueResultSet.get(csv.read(fileName, columns, charset)); result = vr; break; Value v7 = getNullOrValue(session, args, 7); String lineSeparator = v7 == null ? null : v7.getString(); Csv csv = Csv.getInstance(); setCsvDelimiterEscape(csv, fieldSeparatorWrite, fieldDelimiter, escapeCharacter); csv.setNullString(nullString); if (lineSeparator != null) { csv.setLineSeparator(lineSeparator); int rows = csv.write(conn, v0.getString(), v1.getString(), charset); result = ValueInt.get(rows); break;
/** * Configure a Csv object based on the settings * * * @param csv * @return */ public Csv configure(Csv csv) { //csv.setLineSeparator("\n"); if(fieldDelimiter != null) { csv.setFieldDelimiter(fieldDelimiter); } if(fieldSeparator != null) { csv.setFieldSeparatorRead(fieldSeparator); } if(escapeCharacter != null) { csv.setEscapeCharacter(escapeCharacter); } return csv; } }
Csv csv = new Csv(); csv.setEscapeCharacter('\\'); ResultSet result = csv.read(reader, colNames);
@Override public void exportTable(Connection connection, String tableReference, File fileName, ProgressVisitor progress) throws SQLException, IOException { if(FileUtil.isExtensionWellFormated(fileName, "csv")){ final boolean isH2 = JDBCUtilities.isH2DataBase(connection.getMetaData()); TableLocation location = TableLocation.parse(tableReference, isH2); Statement st = null; try { st = connection.createStatement(); new Csv().write(fileName.getPath(), st.executeQuery("SELECT * FROM " + location.toString()), null); } finally { if (st != null) { st.close(); } } } else{ throw new SQLException("Only .csv extension is supported"); } }
private void initRead() throws IOException { if (input == null) { try { InputStream in = FileUtils.newInputStream(fileName); in = new BufferedInputStream(in, Constants.IO_BUFFER_SIZE); input = new InputStreamReader(in, characterSet); } catch (IOException e) { close(); throw e; } } if (!input.markSupported()) { input = new BufferedReader(input); } input.mark(1); int bom = input.read(); if (bom != 0xfeff) { // Microsoft Excel compatibility // ignore pseudo-BOM input.reset(); } inputBuffer = new char[Constants.IO_BUFFER_SIZE * 2]; if (columnNames == null) { readHeader(); } }