Skip to content

Commit

Permalink
Added id to each exported row
Browse files Browse the repository at this point in the history
  • Loading branch information
Meindert Kroese committed Oct 9, 2017
1 parent da8b82f commit 81f1b85
Showing 1 changed file with 8 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,20 +15,22 @@ public class SqlSerialization extends FlatTableSerialization {
private List<String> columns;
protected final PrintWriter writer;
protected boolean firstLine = true;
private int id = 0;

public SqlSerialization(OutputStream outputStream) throws IOException {
writer = new PrintWriter(outputStream);
}

protected void initialize(List<String> columnHeaders) throws IOException {
this.columnHeaders = "";
this.columnHeaders = "id";
columns = new ArrayList<String>();
for (String columnHeader : columnHeaders) {
columnHeader = columnHeader.replaceAll("\\.", "_");
columns.add(columnHeader);
this.columnHeaders += ", " + columnHeader;
this.columnHeaders += ", " + columnHeader + "_type";
}
this.columnHeaders = this.columnHeaders.substring(2);
// this.columnHeaders = this.columnHeaders.substring(2);
}

protected void setTableName(String tableName) {
Expand All @@ -42,6 +44,7 @@ protected void writeCreateTable() {
}
writer.println("DROP TABLE " + tableName + ";");
String createTableString = "CREATE TABLE " + tableName + " (\n";
createTableString += "id integer UNIQUE NOT NULL,\n";
for (String column: columns) {
createTableString += column + " text,\n";
createTableString += column + "_type text,\n";
Expand All @@ -56,7 +59,8 @@ protected void writeRow(List<Value> values) throws IOException {
writeCreateTable();
firstLine = false;
}
String columnValues = "";
id++;
String columnValues = id + "";
for (Value value : values) {
if (value == null) {
columnValues += ", DEFAULT";
Expand All @@ -67,7 +71,7 @@ protected void writeRow(List<Value> values) throws IOException {
}
}
writer.println("INSERT INTO " + this.tableName + " (" + columnHeaders +
") VALUES (" + columnValues.substring(2) + ");");
") VALUES (" + columnValues + ");");
}

@Override
Expand Down

0 comments on commit 81f1b85

Please sign in to comment.