Search in sources :

Example 16 with Settings

use of org.jooq.conf.Settings 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<>(objects.size());
    Field<?>[] pk = pk();
    DSLContext ctx;
    // [#7731] Create a Record -> POJO mapping to allow for reusing the below
    // derived Configuration for improved reflection caching.
    IdentityHashMap<R, Object> mapping = !FALSE.equals(settings().isReturnRecordToPojo()) ? new IdentityHashMap<>() : null;
    // are copied back to the relevant POJO using the RecordListener SPI
    if (mapping != null) {
        Consumer<? super RecordContext> end = c -> {
            Record record = c.record();
            // TODO: [#2536] Use mapper()
            if (record != null)
                record.into(mapping.get(record));
        };
        ctx = configuration().deriveAppending(onStoreEnd(end).onInsertEnd(end).onUpdateEnd(end).onDeleteEnd(end)).dsl();
    } else
        ctx = ctx();
    for (P object : objects) {
        R record = ctx.newRecord(table, object);
        if (mapping != null)
            mapping.put(record, object);
        if (forUpdate && pk != null)
            for (Field<?> field : pk) record.changed(field, false);
        Tools.resetChangedOnNotNull(record);
        result.add(record);
    }
    return result;
}
Also used : UniqueKey(org.jooq.UniqueKey) Arrays(java.util.Arrays) RecordMapper(org.jooq.RecordMapper) RecordListener.onStoreEnd(org.jooq.RecordListener.onStoreEnd) Table(org.jooq.Table) DAO(org.jooq.DAO) DSL.row(org.jooq.impl.DSL.row) RecordContext(org.jooq.RecordContext) Condition(org.jooq.Condition) Collections.singletonList(java.util.Collections.singletonList) ArrayList(java.util.ArrayList) Arrays.asList(java.util.Arrays.asList) DSLContext(org.jooq.DSLContext) SQLDialect(org.jooq.SQLDialect) RecordListenerProvider(org.jooq.RecordListenerProvider) UpdatableRecord(org.jooq.UpdatableRecord) FALSE(java.lang.Boolean.FALSE) Record(org.jooq.Record) IdentityHashMap(java.util.IdentityHashMap) Collection(java.util.Collection) Settings(org.jooq.conf.Settings) Field(org.jooq.Field) Consumer(java.util.function.Consumer) Configuration(org.jooq.Configuration) List(java.util.List) TableField(org.jooq.TableField) DSL.noCondition(org.jooq.impl.DSL.noCondition) EMPTY_RECORD(org.jooq.impl.Tools.EMPTY_RECORD) Optional(java.util.Optional) TRUE(java.lang.Boolean.TRUE) ArrayList(java.util.ArrayList) DSLContext(org.jooq.DSLContext) Field(org.jooq.Field) TableField(org.jooq.TableField) UpdatableRecord(org.jooq.UpdatableRecord) Record(org.jooq.Record)

Example 17 with Settings

use of org.jooq.conf.Settings in project jOOQ by jOOQ.

the class ParserCLI method main.

public static final void main(String... args) throws Exception {
    CLIUtil.main("https://www.jooq.org/doc/latest/manual/sql-building/sql-parser/sql-parser-cli/", () -> {
        Args a;
        Settings settings = new Settings();
        DSLContext ctx;
        a = parse(args);
        settings(a, settings);
        ctx = ctx(a, settings);
        if (a.interactive || args == null || args.length == 0) {
            interactiveMode(ctx, a);
        } else if (a.done) {
        } else if (a.toDialect == null || a.sql == null) {
            System.out.println("Mandatory arguments: -T and -s. Use -h for help");
            throw new RuntimeException();
        } else
            render(ctx, a);
    });
}
Also used : Settings(org.jooq.conf.Settings)

Example 18 with Settings

use of org.jooq.conf.Settings in project jOOQ by jOOQ.

the class DiffCLI method main.

public static final void main(String... args) throws Exception {
    CLIUtil.main("https://www.jooq.org/doc/latest/manual/sql-building/schema-diff-cli/", () -> {
        Args a;
        Settings settings = new Settings();
        DSLContext ctx;
        a = parse(args);
        settings(a, settings);
        ctx = ctx(a, settings);
        if (a.done) {
        } else if (a.toDialect == null || a.sql1 == null || a.sql2 == null) {
            System.out.println("Mandatory arguments: -T and -1, -2. Use -h for help");
            throw new RuntimeException();
        } else
            render(ctx, a);
    });
}
Also used : Settings(org.jooq.conf.Settings)

Example 19 with Settings

use of org.jooq.conf.Settings in project jOOQ by jOOQ.

the class Example_4_2_Settings method run.

@Test
public void run() {
    Select<?> select = DSL.select().from(AUTHOR).where(AUTHOR.ID.eq(3));
    Tools.title("A couple of settings at work - Formatting");
    out.println(using(H2, new Settings().withRenderFormatted(false)).render(select));
    out.println(using(H2, new Settings().withRenderFormatted(true)).render(select));
    Tools.title("A couple of settings at work - Schema");
    out.println(using(H2, new Settings().withRenderSchema(false)).render(select));
    out.println(using(H2, new Settings().withRenderSchema(true)).render(select));
    Tools.title("A couple of settings at work - Name case");
    out.println(using(H2, new Settings().withRenderNameCase(RenderNameCase.AS_IS)).render(select));
    out.println(using(H2, new Settings().withRenderNameCase(RenderNameCase.UPPER)).render(select));
    out.println(using(H2, new Settings().withRenderNameCase(RenderNameCase.LOWER)).render(select));
    Tools.title("A couple of settings at work - Name quoting");
    out.println(using(H2, new Settings().withRenderQuotedNames(RenderQuotedNames.ALWAYS)).render(select));
    out.println(using(H2, new Settings().withRenderQuotedNames(RenderQuotedNames.EXPLICIT_DEFAULT_QUOTED)).render(select));
    out.println(using(H2, new Settings().withRenderQuotedNames(RenderQuotedNames.EXPLICIT_DEFAULT_UNQUOTED)).render(select));
    out.println(using(H2, new Settings().withRenderQuotedNames(RenderQuotedNames.NEVER)).render(select));
    Tools.title("A couple of settings at work - Keyword case");
    out.println(using(H2, new Settings().withRenderKeywordCase(RenderKeywordCase.AS_IS)).render(select));
    out.println(using(H2, new Settings().withRenderKeywordCase(RenderKeywordCase.LOWER)).render(select));
    out.println(using(H2, new Settings().withRenderKeywordCase(RenderKeywordCase.UPPER)).render(select));
    out.println(using(H2, new Settings().withRenderKeywordCase(RenderKeywordCase.PASCAL)).render(select));
    Tools.title("A couple of settings at work - Mapping");
    out.println(using(H2, new Settings().withRenderMapping(new RenderMapping().withSchemata(new MappedSchema().withInput("PUBLIC").withOutput("test").withTables(new MappedTable().withInput("AUTHOR").withOutput("test-author"))))).render(select));
}
Also used : MappedSchema(org.jooq.conf.MappedSchema) MappedTable(org.jooq.conf.MappedTable) RenderMapping(org.jooq.conf.RenderMapping) Settings(org.jooq.conf.Settings) Test(org.junit.Test)

Example 20 with Settings

use of org.jooq.conf.Settings 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)

Aggregations

Settings (org.jooq.conf.Settings)22 DSLContext (org.jooq.DSLContext)10 DefaultConfiguration (org.jooq.impl.DefaultConfiguration)7 Connection (java.sql.Connection)5 List (java.util.List)5 Configuration (org.jooq.Configuration)5 SQLDialect (org.jooq.SQLDialect)4 ArrayList (java.util.ArrayList)3 Test (org.junit.Test)3 PreparedStatement (java.sql.PreparedStatement)2 SQLException (java.sql.SQLException)2 Statement (java.sql.Statement)2 HashMap (java.util.HashMap)2 Map (java.util.Map)2 MappedSchema (org.jooq.conf.MappedSchema)2 MappedTable (org.jooq.conf.MappedTable)2 RenderMapping (org.jooq.conf.RenderMapping)2 DataAccessException (org.jooq.exception.DataAccessException)2 DSL (org.jooq.impl.DSL)2 DataSourceConnectionProvider (org.jooq.impl.DataSourceConnectionProvider)2