[#1392] Add formatInsert() to render insert statements from a Result

This commit is contained in:
Lukas Eder 2014-08-26 10:46:56 +02:00
parent 68b6ec5b60
commit 915df76da3
4 changed files with 128 additions and 0 deletions

View File

@ -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<A, AP, B, S, B2S, BS, L, X, DATE, BOOL, D, T, U, UU, I, IPK, T7
testXML(doc, books);
}
public void testFormatInsert() throws Exception {
jOOQAbstractTest.reset = false;
String inserts =
create().select(TBookStore_NAME(), val(4), val(10))
.from(TBookStore())
.fetch()
.formatInsert(TBookToBookStore(), TBookToBookStore_BOOK_STORE_NAME(), TBookToBookStore_BOOK_ID(), TBookToBookStore_STOCK());
for (String insert : inserts.split(";"))
if (!StringUtils.isBlank(insert))
assertEquals(1, create().execute(insert));
assertEquals(9, create().fetchCount(TBookToBookStore()));
assertEquals(3, create().fetchCount(selectFrom(TBookToBookStore()).where(TBookToBookStore_BOOK_ID().eq(4))));
}
public void testIntoXML() throws Exception {
Result<B> books = create().selectFrom(TBook()).fetch();
testXML(books.intoXML(), books);

View File

@ -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();

View File

@ -446,6 +446,25 @@ public interface Result<R extends Record> extends List<R>, Attachable {
*/
String formatXML();
/**
* Get this result as a set of <code>INSERT</code> statements.
* <p>
* This uses the the first record's {@link TableRecord#getTable()}, if the
* first record is a {@link TableRecord}. Otherwise, this generates
* <code>INSERT</code> statements into an <code>"UNKNOWN_TABLE"</code>. In
* both cases, the {@link Result#fields()} are used for column names.
*/
String formatInsert();
/**
* Get this result as a set of <code>INSERT</code> statements.
* <p>
* This explicitly specifies the table (and optionally the fields) to insert
* into. If the <code>fields</code> 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<R extends Record> extends List<R>, 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<R extends Record> extends List<R>, 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.
*

View File

@ -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<R extends Record> implements Result<R>, 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 {