diff --git a/timbuctoo-instancev4/src/main/java/nl/knaw/huygens/timbuctoo/server/TimbuctooV4.java b/timbuctoo-instancev4/src/main/java/nl/knaw/huygens/timbuctoo/server/TimbuctooV4.java index 8800ce62d3..d830b66281 100644 --- a/timbuctoo-instancev4/src/main/java/nl/knaw/huygens/timbuctoo/server/TimbuctooV4.java +++ b/timbuctoo-instancev4/src/main/java/nl/knaw/huygens/timbuctoo/server/TimbuctooV4.java @@ -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; @@ -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()); diff --git a/timbuctoo-instancev4/src/main/java/nl/knaw/huygens/timbuctoo/v5/dropwizard/contenttypes/SqlWriter.java b/timbuctoo-instancev4/src/main/java/nl/knaw/huygens/timbuctoo/v5/dropwizard/contenttypes/SqlWriter.java new file mode 100644 index 0000000000..6479ef28d2 --- /dev/null +++ b/timbuctoo-instancev4/src/main/java/nl/knaw/huygens/timbuctoo/v5/dropwizard/contenttypes/SqlWriter.java @@ -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; + } +} diff --git a/timbuctoo-instancev4/src/main/java/nl/knaw/huygens/timbuctoo/v5/serializable/serializations/SqlSerialization.java b/timbuctoo-instancev4/src/main/java/nl/knaw/huygens/timbuctoo/v5/serializable/serializations/SqlSerialization.java new file mode 100644 index 0000000000..6f95365b3e --- /dev/null +++ b/timbuctoo-instancev4/src/main/java/nl/knaw/huygens/timbuctoo/v5/serializable/serializations/SqlSerialization.java @@ -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 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 columnHeaders) throws IOException { + this.columnHeaders = "id"; + columns = new ArrayList(); + 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 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(); + } + +}