Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Sql export #189

Open
wants to merge 14 commits into
base: master
Choose a base branch
from
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@
import nl.knaw.huygens.timbuctoo.v5.dropwizard.contenttypes.JsonLdWriter;
import nl.knaw.huygens.timbuctoo.v5.dropwizard.contenttypes.JsonWriter;
import nl.knaw.huygens.timbuctoo.v5.dropwizard.contenttypes.SerializerWriterRegistry;
import nl.knaw.huygens.timbuctoo.v5.dropwizard.contenttypes.SqlWriter;
import nl.knaw.huygens.timbuctoo.v5.dropwizard.endpoints.CreateDataSet;
import nl.knaw.huygens.timbuctoo.v5.dropwizard.endpoints.DataSet;
import nl.knaw.huygens.timbuctoo.v5.dropwizard.endpoints.ErrorResponseHelper;
Expand Down Expand Up @@ -282,7 +283,8 @@ public void run(TimbuctooConfiguration configuration, Environment environment) t
new CsvWriter(),
new JsonLdWriter(),
new JsonWriter(),
new GraphVizWriter()
new GraphVizWriter(),
new SqlWriter()
);

final PaginationArgumentsHelper argHelper = new PaginationArgumentsHelper(configuration.getCollectionFilters());
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package nl.knaw.huygens.timbuctoo.v5.dropwizard.contenttypes;

import nl.knaw.huygens.timbuctoo.v5.serializable.serializations.SqlSerialization;

import javax.ws.rs.Produces;

@Produces(SqlWriter.MIME_TYPE)
public class SqlWriter extends SerializerWriter {

public static final String MIME_TYPE = "text/sql";

public SqlWriter() {
super(SqlSerialization::new);
}

@Override
public String getMimeType() {
return MIME_TYPE;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
package nl.knaw.huygens.timbuctoo.v5.serializable.serializations;

import java.io.IOException;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;
import nl.knaw.huygens.timbuctoo.v5.serializable.dto.Value;
import nl.knaw.huygens.timbuctoo.v5.serializable.serializations.base.FlatTableSerialization;

public class SqlSerialization extends FlatTableSerialization {

private String tableName = "graphqlexport";
private String columnHeaders;
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 = "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);
}

protected void setTableName(String tableName) {
this.tableName = tableName;
}

protected void writeCreateTable() {
if (tableName.isEmpty()) {
// System.err.println("writeCreateTable: tableName is empty!");
tableName = "tableName";
}
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";
}
createTableString = createTableString.substring(0, createTableString.length() - 2);
createTableString += ");";
writer.println(createTableString);
}

protected void writeRow(List<Value> values) throws IOException {
if (firstLine) {
writeCreateTable();
firstLine = false;
}
id++;
String columnValues = id + "";
for (Value value : values) {
if (value == null) {
columnValues += ", DEFAULT";
columnValues += ", 'text'";
} else {
columnValues += ", '" + value.getValue().toString() + "'";
columnValues += ", '" + value.getType() + "'";
}
}
writer.println("INSERT INTO " + this.tableName + " (" + columnHeaders +
") VALUES (" + columnValues + ");");
}

@Override
protected void finish() throws IOException {
writer.flush();
writer.close();
}

}