Search in sources :

Example 56 with DSLContext

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();
    }
}
Also used : AuthorRecord(org.jooq.example.db.h2.tables.records.AuthorRecord) Connection(java.sql.Connection) DSLContext(org.jooq.DSLContext) Test(org.junit.Test)

Example 57 with DSLContext

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();
    }
}
Also used : DataChangedException(org.jooq.exception.DataChangedException) Connection(java.sql.Connection) DSLContext(org.jooq.DSLContext) BookRecord(org.jooq.example.db.h2.tables.records.BookRecord) Settings(org.jooq.conf.Settings) Test(org.junit.Test)

Example 58 with DSLContext

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;
}
Also used : Field(org.jooq.Field) TableField(org.jooq.TableField) ArrayList(java.util.ArrayList) DSLContext(org.jooq.DSLContext)

Example 59 with DSLContext

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;
    }
}
Also used : ResultSet(java.sql.ResultSet) DSLContext(org.jooq.DSLContext) ExecuteContext(org.jooq.ExecuteContext) ArrayList(java.util.ArrayList) Arrays.asList(java.util.Arrays.asList) List(java.util.List) ExecuteListener(org.jooq.ExecuteListener)

Example 60 with DSLContext

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;
}
Also used : Field(org.jooq.Field) UDTField(org.jooq.UDTField) ArrayList(java.util.ArrayList) DSLContext(org.jooq.DSLContext)

Aggregations

DSLContext (org.jooq.DSLContext)109 AnnotationConfigApplicationContext (org.springframework.context.annotation.AnnotationConfigApplicationContext)55 Connection (java.sql.Connection)23 SQLException (java.sql.SQLException)17 List (java.util.List)17 DIConfiguration (com.khartec.waltz.service.DIConfiguration)14 Collectors (java.util.stream.Collectors)14 EntityKind (com.khartec.waltz.model.EntityKind)11 ArrayList (java.util.ArrayList)9 DSL (org.jooq.impl.DSL)9 EntityReference (com.khartec.waltz.model.EntityReference)8 Timestamp (java.sql.Timestamp)8 Random (java.util.Random)8 IntStream (java.util.stream.IntStream)8 Application (com.khartec.waltz.model.application.Application)7 LogicalFlowDao (com.khartec.waltz.data.logical_flow.LogicalFlowDao)6 OrganisationalUnit (com.khartec.waltz.model.orgunit.OrganisationalUnit)6 Field (org.jooq.Field)6 Record1 (org.jooq.Record1)6 Test (org.junit.Test)6