CSVWRITE line separator patch

74 views
Skip to first unread message

ILM

unread,
Feb 15, 2008, 5:57:14 AM2/15/08
to H2 Database
Hi,

I am using CSV files to transfer data between different database
systems. Although CSVREAD handles all line separator (\r or \n),
CSVWRITE only uses the default java "line.separator" property. So it
makes a difference whether your database is launched on Windows or
Linux, and some databases only support \n on import. I have thus added
a parameter to CSVWRITE to specify the line separator that should be
used.


Sylvain CUAZ
ILM Informatique




Index: src/main/org/h2/res/help.csv
===================================================================
--- src/main/org/h2/res/help.csv (revision 472)
+++ src/main/org/h2/res/help.csv (working copy)
@@ -2650,11 +2650,12 @@
"

"Functions (System)","CSVWRITE","
-CSVWRITE(fileNameString, queryString [, charsetString [,
fieldSeparatorString [, fieldDelimiterString [,
escapeCharacterString]]]]): int
+CSVWRITE(fileNameString, queryString [, charsetString [,
fieldSeparatorString [, fieldDelimiterString [, escapeCharacterString
[, lineSeparator]]]]]): int
","
Writes a CSV (comma separated values).
The file is overwritten if it exists.
The default charset is the default value for this system, and the
default field separator is a comma.
+The default line separator is the default value for this system
("line.separator" system property).
The returned value is the number or rows written.
Admin rights are required to execute this command.
","
Index: src/main/org/h2/expression/Function.java
===================================================================
--- src/main/org/h2/expression/Function.java (revision 472)
+++ src/main/org/h2/expression/Function.java (working copy)
@@ -1002,8 +1002,11 @@
String fieldSeparatorWrite = v3 == null ? null :
v3.getString();
String fieldDelimiter = v4 == null ? null :
v4.getString();
String escapeCharacter = v5 == null ? null :
v5.getString();
+ final Value v6 = getNullOrValue(session, args, 6);
+ final String lineSep = v6 == null ? null :
v6.getString();
Csv csv = Csv.getInstance();
setCsvDelimiterEscape(csv, fieldSeparatorWrite,
fieldDelimiter, escapeCharacter);
+ csv.setLineSeparator(lineSep);
int rows = csv.write(conn, v0.getString(),
v1.getString(), charset);
result = ValueInt.get(rows);
break;
Index: src/main/org/h2/tools/Csv.java
===================================================================
--- src/main/org/h2/tools/Csv.java (revision 472)
+++ src/main/org/h2/tools/Csv.java (working copy)
@@ -45,6 +45,7 @@
private String rowSeparatorWrite;
private char fieldDelimiter = '\"';
private char escapeCharacter = '\"';
+ private String lineSeparator;
private String fileName;
private Reader reader;
private PrintWriter writer;
@@ -209,6 +210,7 @@
}

private Csv() {
+ this.setLineSeparator(null);
}

private void init(String fileName, String charset) {
@@ -256,7 +258,7 @@
if (rowSeparatorWrite != null) {
writer.print(rowSeparatorWrite);
}
- writer.println();
+ writer.print(this.lineSeparator);
}

private String escape(String data) {
@@ -608,4 +610,13 @@
return escapeCharacter;
}

+ /**
+ * Set the line separator.
+ *
+ * @param lineSep the line separator, <code>null</code> for the
system default
+ * ("line.separator" property).
+ */
+ public void setLineSeparator(String lineSep) {
+ this.lineSeparator = lineSep != null ? lineSep :
System.getProperty("line.separator");
+ }
}

Thomas Mueller

unread,
Feb 16, 2008, 12:43:16 PM2/16/08
to h2-da...@googlegroups.com
Hi,

Thanks for the patch! This feature will be available in the next release.

Regards,
Thomas
Reply all
Reply to author
Forward
0 new messages