use of org.jooq.DSLContext in project jOOQ by jOOQ.
the class Example_2_1_ActiveRecords method run.
@Test
public void run() throws SQLException {
Connection connection = connection();
DSLContext dsl = DSL.using(connection);
AuthorRecord author;
try {
Tools.title("Loading and changing active records");
author = dsl.selectFrom(AUTHOR).where(AUTHOR.ID.eq(1)).fetchOne();
author.setDateOfBirth(Date.valueOf("2000-01-01"));
author.store();
Tools.print(author);
Tools.title("Creating a new active record");
author = dsl.newRecord(AUTHOR);
author.setId(3);
author.setFirstName("Alfred");
author.setLastName("Hitchcock");
author.store();
Tools.print(author);
Tools.title("Refreshing an active record");
author = dsl.newRecord(AUTHOR);
author.setId(3);
author.refresh();
Tools.print(author);
Tools.title("Updating an active record");
author.setDateOfBirth(Date.valueOf("1899-08-13"));
author.store();
Tools.print(author);
Tools.title("Deleting an active record");
author.delete();
Tools.print(dsl.selectFrom(AUTHOR).fetch());
} finally // Don't store the changes
{
connection.rollback();
}
}
use of org.jooq.DSLContext in project jOOQ by jOOQ.
the class Example_2_2_OptimisticLocking method run.
@Test
public void run() throws SQLException {
Connection connection = connection();
DSLContext dsl = DSL.using(connection, new Settings().withExecuteWithOptimisticLocking(true));
try {
Tools.title("Applying optimistic locking");
BookRecord book1 = dsl.selectFrom(BOOK).where(BOOK.ID.eq(1)).fetchOne();
BookRecord book2 = dsl.selectFrom(BOOK).where(BOOK.ID.eq(1)).fetchOne();
book1.setTitle("New Title");
book1.store();
book2.setTitle("Another Title");
book2.store();
} catch (DataChangedException expected) {
expected.printStackTrace();
} finally // Don't store the changes
{
connection.rollback();
}
}
use of org.jooq.DSLContext in project jOOQ by jOOQ.
the class DAOImpl method records.
private /* non-final */
List<R> records(Collection<P> objects, boolean forUpdate) {
List<R> result = new ArrayList<R>();
Field<?>[] pk = pk();
for (P object : objects) {
// [#2536] Upon store(), insert(), update(), delete(), returned values in the record
// are copied back to the relevant POJO using the RecordListener SPI
DSLContext ctx = using(!FALSE.equals(configuration.settings().isReturnRecordToPojo()) ? configuration.derive(providers(configuration.recordListenerProviders(), object)) : configuration);
R record = ctx.newRecord(table, object);
if (forUpdate && pk != null)
for (Field<?> field : pk) record.changed(field, false);
Tools.resetChangedOnNotNull(record);
result.add(record);
}
return result;
}
use of org.jooq.DSLContext in project jOOQ by jOOQ.
the class AbstractDMLQuery method execute.
@SuppressWarnings({ "rawtypes", "unchecked" })
@Override
protected final int execute(ExecuteContext ctx, ExecuteListener listener) throws SQLException {
if (returning.isEmpty()) {
return super.execute(ctx, listener);
} else {
int result = 1;
ResultSet rs;
switch(ctx.family()) {
// SQLite can select _rowid_ after the insert
case SQLITE:
{
listener.executeStart(ctx);
result = ctx.statement().executeUpdate();
ctx.rows(result);
listener.executeEnd(ctx);
DSLContext create = DSL.using(ctx.configuration());
returned = create.select(returning).from(table).where(rowid().equal(rowid().getDataType().convert(create.lastID()))).fetchInto(table);
return result;
}
case CUBRID:
{
listener.executeStart(ctx);
result = ctx.statement().executeUpdate();
ctx.rows(result);
listener.executeEnd(ctx);
selectReturning(ctx.configuration(), create(ctx.configuration()).lastID());
return result;
}
case DERBY:
case H2:
case MARIADB:
case MYSQL:
{
listener.executeStart(ctx);
result = ctx.statement().executeUpdate();
ctx.rows(result);
listener.executeEnd(ctx);
rs = ctx.statement().getGeneratedKeys();
try {
List<Object> list = new ArrayList<Object>();
// from getGeneratedKeys() sometimes
if (rs != null)
while (rs.next()) list.add(rs.getObject(1));
selectReturning(ctx.configuration(), list.toArray());
return result;
} finally {
JDBCUtils.safeClose(rs);
}
}
// in the Postgres JDBC driver
case FIREBIRD:
case POSTGRES:
{
listener.executeStart(ctx);
rs = ctx.statement().executeQuery();
listener.executeEnd(ctx);
break;
}
case HSQLDB:
default:
{
listener.executeStart(ctx);
result = ctx.statement().executeUpdate();
ctx.rows(result);
listener.executeEnd(ctx);
rs = ctx.statement().getGeneratedKeys();
break;
}
}
ExecuteContext ctx2 = new DefaultExecuteContext(ctx.configuration());
ExecuteListener listener2 = new ExecuteListeners(ctx2);
ctx2.resultSet(rs);
returned = new CursorImpl<R>(ctx2, listener2, fieldArray(returning), null, false, true).fetch();
// [#3682] Plain SQL tables do not have any fields
if (table.fields().length > 0)
returned = returned.into(table);
// execute this logic
if (returned.size() > 0 || ctx.family() != HSQLDB) {
result = returned.size();
ctx.rows(result);
}
return result;
}
}
use of org.jooq.DSLContext in project jOOQ by jOOQ.
the class AbstractRoutine method executeSelectFromPOSTGRES.
private final int executeSelectFromPOSTGRES() {
DSLContext create = create(configuration);
List<Field<?>> fields = new ArrayList<Field<?>>();
if (returnParameter != null)
fields.add(DSL.field(DSL.name(getName()), returnParameter.getDataType()));
for (Parameter<?> p : outParameters) fields.add(DSL.field(DSL.name(p.getName()), p.getDataType()));
Result<?> result = create.select(fields).from("{0}", asField()).fetch();
int i = 0;
if (returnParameter != null)
outValues.put(returnParameter, returnParameter.getDataType().convert(result.getValue(0, i++)));
for (Parameter<?> p : outParameters) outValues.put(p, p.getDataType().convert(result.getValue(0, i++)));
return 0;
}
Aggregations