[#1686] Add UpdatableRecord.insert() and update()
This commit is contained in:
parent
304865bbdf
commit
66dd5f25e9
@ -35,6 +35,8 @@
|
||||
*/
|
||||
package org.jooq.test;
|
||||
|
||||
import static junit.framework.Assert.assertEquals;
|
||||
|
||||
import java.lang.reflect.Method;
|
||||
import java.math.BigDecimal;
|
||||
import java.math.BigInteger;
|
||||
@ -781,6 +783,14 @@ public abstract class BaseTest<
|
||||
.fetchOne();
|
||||
}
|
||||
|
||||
protected final void assertCountAuthors(int count) {
|
||||
assertEquals(count, (int) create().selectCount().from(TAuthor()).fetchOne(0, Integer.class));
|
||||
}
|
||||
|
||||
protected final void assertCountBooks(int count) {
|
||||
assertEquals(count, (int) create().selectCount().from(TBook()).fetchOne(0, Integer.class));
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
protected Sequence<? extends Number> SAuthorID() throws IllegalAccessException, NoSuchFieldException {
|
||||
return (Sequence<? extends Number>) cSequences().getField("S_AUTHOR_ID").get(cSequences());
|
||||
|
||||
@ -290,6 +290,60 @@ extends BaseTest<A, AP, B, S, B2S, BS, L, X, DATE, BOOL, D, T, U, UU, I, IPK, T7
|
||||
assertEquals(6, (int) bKey.value1());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testUpdatablesInsertUpdate() throws Exception {
|
||||
jOOQAbstractTest.reset = false;
|
||||
|
||||
A author = create().newRecord(TAuthor());
|
||||
|
||||
// No actions on unchanged authors
|
||||
assertFalse(author.changed());
|
||||
assertEquals(0, author.insert());
|
||||
assertCountAuthors(2);
|
||||
assertFalse(author.changed());
|
||||
assertEquals(0, author.update());
|
||||
assertCountAuthors(2);
|
||||
assertFalse(author.changed());
|
||||
assertEquals(0, author.store());
|
||||
assertCountAuthors(2);
|
||||
|
||||
author.setValue(TAuthor_ID(), 3);
|
||||
author.setValue(TAuthor_LAST_NAME(), "XX");
|
||||
assertTrue(author.changed());
|
||||
assertEquals(0, author.update());
|
||||
assertTrue(author.changed());
|
||||
assertCountAuthors(2);
|
||||
|
||||
assertEquals(1, author.insert());
|
||||
assertFalse(author.changed());
|
||||
assertCountAuthors(3);
|
||||
A test = getAuthor(3);
|
||||
assertEquals(3, (int) test.getValue(TAuthor_ID()));
|
||||
assertEquals("XX", test.getValue(TAuthor_LAST_NAME()));
|
||||
|
||||
assertEquals(0, author.insert());
|
||||
assertFalse(author.changed());
|
||||
assertCountAuthors(3);
|
||||
|
||||
assertEquals(0, author.update());
|
||||
assertFalse(author.changed());
|
||||
assertCountAuthors(3);
|
||||
|
||||
author.setValue(TAuthor_LAST_NAME(), "YY");
|
||||
assertTrue(author.changed());
|
||||
try {
|
||||
author.insert();
|
||||
fail();
|
||||
}
|
||||
catch (DataAccessException expected) {}
|
||||
|
||||
assertEquals(1, author.update());
|
||||
assertFalse(author.changed());
|
||||
test.refresh();
|
||||
assertEquals(3, (int) test.getValue(TAuthor_ID()));
|
||||
assertEquals("YY", test.getValue(TAuthor_LAST_NAME()));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testUpdatablesPK() throws Exception {
|
||||
jOOQAbstractTest.reset = false;
|
||||
|
||||
@ -1423,6 +1423,11 @@ public abstract class jOOQAbstractTest<
|
||||
new CRUDTests(this).testUpdatablesKeysMethod();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testUpdatablesInsertUpdate() throws Exception {
|
||||
new CRUDTests(this).testUpdatablesInsertUpdate();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testUpdatablesPK() throws Exception {
|
||||
new CRUDTests(this).testUpdatablesPK();
|
||||
|
||||
@ -102,7 +102,7 @@ public interface UpdatableRecord<R extends UpdatableRecord<R>> extends TableReco
|
||||
* Store this record back to the database.
|
||||
* <p>
|
||||
* Depending on the state of the primary key's or main unique key's value,
|
||||
* an <code>INSERT</code> or an <code>UPDATE</code> statement is executed.
|
||||
* an {@link #insert()} or an {@link #update()} statement is executed.
|
||||
* <p>
|
||||
* <h3>Statement type</h3>
|
||||
* <p>
|
||||
@ -126,6 +126,9 @@ public interface UpdatableRecord<R extends UpdatableRecord<R>> extends TableReco
|
||||
* <code>INSERT</code> will be executed.
|
||||
* <h3>Automatic value generation</h3>
|
||||
* <p>
|
||||
* Use {@link #insert()} or {@link #update()} to explicitly force either
|
||||
* statement type.
|
||||
* <p>
|
||||
* <ul>
|
||||
* <li><strong>IDENTITY columns</strong>
|
||||
* <p>
|
||||
@ -203,18 +206,47 @@ public interface UpdatableRecord<R extends UpdatableRecord<R>> extends TableReco
|
||||
* WHERE [key fields = key values]
|
||||
* AND [version/timestamp fields = version/timestamp values]</pre></code></li>
|
||||
* </ul>
|
||||
* <p>
|
||||
* This is in fact the same as calling
|
||||
* <code>store(getTable().getMainKey().getFieldsArray())</code>
|
||||
*
|
||||
* @return <code>1</code> if the record was stored to the database. <code>0
|
||||
* </code> if storing was not necessary.
|
||||
* @throws DataAccessException if something went wrong executing the query
|
||||
* @throws DataChangedException If optimistic locking is enabled and the
|
||||
* record has already been changed/deleted in the database
|
||||
* @see #insert()
|
||||
* @see #update()
|
||||
*/
|
||||
int store() throws DataAccessException, DataChangedException;
|
||||
|
||||
/**
|
||||
* Store this record back to the database using an <code>INSERT</code>
|
||||
* statement.
|
||||
* <p>
|
||||
* This is the same as {@link #store()}, except that an <code>INSERT</code>
|
||||
* statement (or no statement) will always be executed.
|
||||
*
|
||||
* @return <code>1</code> if the record was stored to the database. <code>0
|
||||
* </code> if storing was not necessary.
|
||||
* @throws DataAccessException if something went wrong executing the query
|
||||
* @see #store()
|
||||
*/
|
||||
int insert() throws DataAccessException;
|
||||
|
||||
/**
|
||||
* Store this record back to the database using an <code>UPDATE</code>
|
||||
* statement.
|
||||
* <p>
|
||||
* This is the same as {@link #store()}, except that an <code>UPDATE</code>
|
||||
* statement (or no statement) will always be executed.
|
||||
*
|
||||
* @return <code>1</code> if the record was stored to the database. <code>0
|
||||
* </code> if storing was not necessary.
|
||||
* @throws DataAccessException if something went wrong executing the query
|
||||
* @throws DataChangedException If optimistic locking is enabled and the
|
||||
* record has already been changed/deleted in the database
|
||||
* @see #store()
|
||||
*/
|
||||
int update() throws DataAccessException, DataChangedException;
|
||||
|
||||
/**
|
||||
* Deletes this record from the database, based on the value of the primary
|
||||
* key or main unique key.
|
||||
|
||||
@ -144,10 +144,19 @@ public class UpdatableRecordImpl<R extends UpdatableRecord<R>> extends TableReco
|
||||
result = storeInsert();
|
||||
}
|
||||
|
||||
setAllChanged(false);
|
||||
return result;
|
||||
}
|
||||
|
||||
@Override
|
||||
public final int insert() {
|
||||
return storeInsert();
|
||||
}
|
||||
|
||||
@Override
|
||||
public final int update() {
|
||||
return storeUpdate(getMainKey().getFieldsArray());
|
||||
}
|
||||
|
||||
private final int storeInsert() {
|
||||
Executor create = create();
|
||||
InsertQuery<R> insert = create.insertQuery(getTable());
|
||||
@ -184,6 +193,8 @@ public class UpdatableRecordImpl<R extends UpdatableRecord<R>> extends TableReco
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
setAllChanged(false);
|
||||
}
|
||||
|
||||
return result;
|
||||
@ -218,6 +229,11 @@ public class UpdatableRecordImpl<R extends UpdatableRecord<R>> extends TableReco
|
||||
// [#1596] Check if the record was really changed in the database
|
||||
int result = update.execute();
|
||||
checkIfChanged(result, version, timestamp);
|
||||
|
||||
if (result > 0) {
|
||||
setAllChanged(false);
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
|
||||
Loading…
Reference in New Issue
Block a user