diff --git a/jOOQ-test/src/test/java/org/jooq/test/all/testcases/FormatTests.java b/jOOQ-test/src/test/java/org/jooq/test/all/testcases/FormatTests.java index 5447a2bc3c..3c6700c9da 100644 --- a/jOOQ-test/src/test/java/org/jooq/test/all/testcases/FormatTests.java +++ b/jOOQ-test/src/test/java/org/jooq/test/all/testcases/FormatTests.java @@ -41,6 +41,8 @@ package org.jooq.test.all.testcases; import static java.util.Arrays.asList; +import static org.jooq.impl.DSL.selectFrom; +import static org.jooq.impl.DSL.val; import static org.jooq.tools.StringUtils.defaultString; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertTrue; @@ -73,6 +75,7 @@ import org.jooq.UpdatableRecord; import org.jooq.test.BaseTest; import org.jooq.test.jOOQAbstractTest; import org.jooq.test.all.tools.DOMBuilder; +import org.jooq.tools.StringUtils; import org.w3c.dom.Document; @@ -422,6 +425,23 @@ extends BaseTest books = create().selectFrom(TBook()).fetch(); testXML(books.intoXML(), books); diff --git a/jOOQ-test/src/test/java/org/jooq/test/jOOQAbstractTest.java b/jOOQ-test/src/test/java/org/jooq/test/jOOQAbstractTest.java index 72c2293f46..9facd98786 100644 --- a/jOOQ-test/src/test/java/org/jooq/test/jOOQAbstractTest.java +++ b/jOOQ-test/src/test/java/org/jooq/test/jOOQAbstractTest.java @@ -2217,6 +2217,11 @@ public abstract class jOOQAbstractTest< new FormatTests(this).testFormatXML(); } + @Test + public void testFormatInsert() throws Exception { + new FormatTests(this).testFormatInsert(); + } + @Test public void testIntoXML() throws Exception { new FormatTests(this).testIntoXML(); diff --git a/jOOQ/src/main/java/org/jooq/Result.java b/jOOQ/src/main/java/org/jooq/Result.java index ffc5dce6de..581d81076d 100644 --- a/jOOQ/src/main/java/org/jooq/Result.java +++ b/jOOQ/src/main/java/org/jooq/Result.java @@ -446,6 +446,25 @@ public interface Result extends List, Attachable { */ String formatXML(); + /** + * Get this result as a set of INSERT statements. + *

+ * This uses the the first record's {@link TableRecord#getTable()}, if the + * first record is a {@link TableRecord}. Otherwise, this generates + * INSERT statements into an "UNKNOWN_TABLE". In + * both cases, the {@link Result#fields()} are used for column names. + */ + String formatInsert(); + + /** + * Get this result as a set of INSERT statements. + *

+ * This explicitly specifies the table (and optionally the fields) to insert + * into. If the fields argument is left empty, the + * {@link Result#fields()} are used, instead. + */ + String formatInsert(Table table, Field... fields); + /** * Like {@link #format()}, but the data is output onto an {@link OutputStream}. * @@ -502,6 +521,20 @@ public interface Result extends List, Attachable { */ void formatXML(OutputStream stream) throws IOException; + /** + * Like {@link #formatInsert()}, but the data is output onto an {@link OutputStream}. + * + * @throws IOException - an unchecked wrapper for {@link java.io.IOException}, if anything goes wrong. + */ + void formatInsert(OutputStream stream) throws IOException; + + /** + * Like {@link #formatInsert(Table, Field...)}, but the data is output onto an {@link OutputStream}. + * + * @throws IOException - an unchecked wrapper for {@link java.io.IOException}, if anything goes wrong. + */ + void formatInsert(OutputStream stream, Table table, Field... fields) throws IOException; + /** * Like {@link #format()}, but the data is output onto a {@link Writer}. * @@ -558,6 +591,20 @@ public interface Result extends List, Attachable { */ void formatXML(Writer writer) throws IOException; + /** + * Like {@link #formatInsert()}, but the data is output onto a {@link Writer}. + * + * @throws IOException - an unchecked wrapper for {@link java.io.IOException}, if anything goes wrong. + */ + void formatInsert(Writer writer) throws IOException; + + /** + * Like {@link #formatInsert(Table, Field...)}, but the data is output onto an {@link Writer}. + * + * @throws IOException - an unchecked wrapper for {@link java.io.IOException}, if anything goes wrong. + */ + void formatInsert(Writer writer, Table table, Field... fields) throws IOException; + /** * Get this result as XML. * diff --git a/jOOQ/src/main/java/org/jooq/impl/ResultImpl.java b/jOOQ/src/main/java/org/jooq/impl/ResultImpl.java index 95373826ff..cae35757c2 100644 --- a/jOOQ/src/main/java/org/jooq/impl/ResultImpl.java +++ b/jOOQ/src/main/java/org/jooq/impl/ResultImpl.java @@ -43,6 +43,8 @@ package org.jooq.impl; import static java.lang.Math.max; import static java.lang.Math.min; +import static org.jooq.impl.DSL.insertInto; +import static org.jooq.impl.DSL.tableByName; import static org.jooq.impl.Utils.indexOrFail; import static org.jooq.tools.StringUtils.abbreviate; import static org.jooq.tools.StringUtils.leftPad; @@ -74,6 +76,7 @@ import javax.xml.parsers.ParserConfigurationException; import org.jooq.AttachableInternal; import org.jooq.Configuration; import org.jooq.Converter; +import org.jooq.DSLContext; import org.jooq.EnumType; import org.jooq.Field; import org.jooq.Record; @@ -105,6 +108,7 @@ import org.jooq.RecordType; import org.jooq.Result; import org.jooq.Row; import org.jooq.Table; +import org.jooq.TableRecord; import org.jooq.exception.IOException; import org.jooq.exception.InvalidResultException; import org.jooq.tools.Convert; @@ -789,6 +793,58 @@ class ResultImpl implements Result, AttachableInternal { } } + @Override + public final String formatInsert() { + StringWriter writer = new StringWriter(); + formatInsert(writer); + return writer.toString(); + } + + @Override + public final void formatInsert(OutputStream stream) { + formatInsert(new OutputStreamWriter(stream)); + } + + @Override + public final void formatInsert(Writer writer) { + Table table = null; + + if (records.size() > 0 && records.get(0) instanceof TableRecord) + table = ((TableRecord) records.get(0)).getTable(); + + if (table == null) + table = tableByName("UNKNOWN_TABLE"); + + formatInsert(writer, table, fields()); + } + + @Override + public final String formatInsert(Table table, Field... f) { + StringWriter writer = new StringWriter(); + formatInsert(writer, table, f); + return writer.toString(); + } + + @Override + public final void formatInsert(OutputStream stream, Table table, Field... f) { + formatInsert(new OutputStreamWriter(stream), table, f); + } + + @Override + public final void formatInsert(Writer writer, Table table, Field... f) { + DSLContext ctx = DSL.using(configuration()); + + try { + for (R record : this) { + writer.append(ctx.renderInlined(insertInto(table, f).values(record.intoArray()))); + writer.append(";\n"); + } + } + catch (java.io.IOException e) { + throw new IOException("Exception while writing INSERTs", e); + } + } + @Override public final Document intoXML() { try {