Search in sources :

Example 21 with DefaultConfiguration

use of org.jooq.impl.DefaultConfiguration in project curiostack by curioswitch.

the class DatabaseModule method dbContext.

@Provides
@Singleton
static DSLContext dbContext(DataSource dataSource, @ForDatabase ListeningExecutorService dbExecutor) {
    Configuration configuration = new DefaultConfiguration().set(dbExecutor).set(SQLDialect.MYSQL).set(new Settings().withRenderSchema(false)).set(new DataSourceConnectionProvider(dataSource)).set(DatabaseUtil.sfmRecordMapperProvider());
    DSLContext ctx = DSL.using(configuration);
    // Eagerly trigger JOOQ classinit for better startup performance.
    ctx.select().from("curio_server_framework_init").getSQL();
    return ctx;
}
Also used : DefaultConfiguration(org.jooq.impl.DefaultConfiguration) Configuration(org.jooq.Configuration) DataSourceConnectionProvider(org.jooq.impl.DataSourceConnectionProvider) DSLContext(org.jooq.DSLContext) DefaultConfiguration(org.jooq.impl.DefaultConfiguration) Settings(org.jooq.conf.Settings) Singleton(javax.inject.Singleton) Provides(dagger.Provides)

Example 22 with DefaultConfiguration

use of org.jooq.impl.DefaultConfiguration in project tutorials by eugenp.

the class PersistenceContextIntegrationTest method configuration.

@Bean
public DefaultConfiguration configuration() {
    DefaultConfiguration jooqConfiguration = new DefaultConfiguration();
    jooqConfiguration.set(connectionProvider());
    jooqConfiguration.set(new DefaultExecuteListenerProvider(exceptionTransformer()));
    String sqlDialectName = environment.getRequiredProperty("jooq.sql.dialect");
    SQLDialect dialect = SQLDialect.valueOf(sqlDialectName);
    jooqConfiguration.set(dialect);
    return jooqConfiguration;
}
Also used : DefaultExecuteListenerProvider(org.jooq.impl.DefaultExecuteListenerProvider) SQLDialect(org.jooq.SQLDialect) DefaultConfiguration(org.jooq.impl.DefaultConfiguration) Bean(org.springframework.context.annotation.Bean)

Example 23 with DefaultConfiguration

use of org.jooq.impl.DefaultConfiguration in project jOOQ by jOOQ.

the class SparkCRUD method main.

public static void main(String[] args) throws Exception {
    final BasicDataSource ds = new BasicDataSource();
    final Properties properties = new Properties();
    properties.load(SparkCRUD.class.getResourceAsStream("/config.properties"));
    ds.setDriverClassName(properties.getProperty("db.driver"));
    ds.setUrl(properties.getProperty("db.url"));
    ds.setUsername(properties.getProperty("db.username"));
    ds.setPassword(properties.getProperty("db.password"));
    final ConnectionProvider cp = new DataSourceConnectionProvider(ds);
    final Configuration configuration = new DefaultConfiguration().set(cp).set(SQLDialect.H2).set(new ThreadLocalTransactionProvider(cp, true));
    final DSLContext ctx = DSL.using(configuration);
    final JSONFormat format = new JSONFormat().format(true).header(false).recordFormat(RecordFormat.OBJECT);
    // Creates a new book resource, will return the ID to the created resource
    // author and title are sent as query parameters e.g. /books?author=Foo&title=Bar
    post("/books", (request, response) -> {
        return ctx.transactionResult(() -> {
            AuthorRecord author = upsertAuthor(ctx, request);
            BookRecord book = ctx.newRecord(BOOK);
            book.setAuthorId(author.getId());
            book.setTitle(request.queryParams("title"));
            book.store();
            // 201 Created
            response.status(201);
            return book.formatJSON(format);
        });
    });
    // Gets the book resource for the provided id
    get("/books/:id", (request, response) -> {
        Record2<String, String> book = ctx.select(BOOK.TITLE, AUTHOR.NAME).from(BOOK).join(AUTHOR).on(BOOK.AUTHOR_ID.eq(AUTHOR.ID)).where(BOOK.ID.eq(BOOK.ID.getDataType().convert(request.params(":id")))).fetchOne();
        if (book != null) {
            return book.formatJSON(format);
        } else {
            // 404 Not found
            response.status(404);
            return "{\"error\":\"Book not found\"}";
        }
    });
    // Updates the book resource for the provided id with new information
    // author and title are sent as query parameters e.g. /books/<id>?author=Foo&title=Bar
    put("/books/:id", (request, response) -> {
        return ctx.transactionResult(() -> {
            BookRecord book = ctx.selectFrom(BOOK).where(BOOK.ID.eq(BOOK.ID.getDataType().convert(request.params(":id")))).fetchOne();
            if (book != null) {
                AuthorRecord author = upsertAuthor(ctx, request);
                String newAuthor = request.queryParams("author");
                String newTitle = request.queryParams("title");
                if (newAuthor != null)
                    book.setAuthorId(author.getId());
                if (newTitle != null)
                    book.setTitle(newTitle);
                book.update();
                return book.formatJSON(format);
            } else {
                // 404 Not found
                response.status(404);
                return "{\"error\":\"Book not found\"}";
            }
        });
    });
    // Deletes the book resource for the provided id
    delete("/books/:id", (request, response) -> {
        return ctx.transactionResult(() -> {
            BookRecord book = ctx.deleteFrom(BOOK).where(BOOK.ID.eq(BOOK.ID.getDataType().convert(request.params(":id")))).returning().fetchOne();
            if (book != null) {
                return book.formatJSON(format);
            } else {
                // 404 Not found
                response.status(404);
                return "{\"error\":\"Book not found\"}";
            }
        });
    });
    // Gets all available book resources
    get("/books", (request, response) -> {
        return ctx.select(BOOK.ID, BOOK.TITLE).from(BOOK).fetch().formatJSON(format);
    });
}
Also used : DefaultConfiguration(org.jooq.impl.DefaultConfiguration) Configuration(org.jooq.Configuration) DataSourceConnectionProvider(org.jooq.impl.DataSourceConnectionProvider) ThreadLocalTransactionProvider(org.jooq.impl.ThreadLocalTransactionProvider) DSLContext(org.jooq.DSLContext) DefaultConfiguration(org.jooq.impl.DefaultConfiguration) Properties(java.util.Properties) JSONFormat(org.jooq.JSONFormat) ConnectionProvider(org.jooq.ConnectionProvider) DataSourceConnectionProvider(org.jooq.impl.DataSourceConnectionProvider) AuthorRecord(org.jooq.example.db.h2.tables.records.AuthorRecord) BookRecord(org.jooq.example.db.h2.tables.records.BookRecord) BasicDataSource(org.apache.commons.dbcp.BasicDataSource)

Example 24 with DefaultConfiguration

use of org.jooq.impl.DefaultConfiguration in project jOOQ by jOOQ.

the class Example_4_4_ExecuteListener method run.

@Test
public void run() {
    Tools.title("Displaying execution time using a custom ExecuteListener");
    ExecuteListener listener = new CallbackExecuteListener().onStart(ctx -> {
        // Register the start time to the current context
        ctx.data("time", System.nanoTime());
    }).onEnd(ctx -> {
        // Extract the start time from the current context
        Long time = (Long) ctx.data("time");
        System.out.println("Execution time : " + ((System.nanoTime() - time) / 1000 / 1000.0) + "ms. Query : " + ctx.sql());
    });
    DSL.using(new DefaultConfiguration().set(SQLDialect.H2).set(new DefaultConnectionProvider(connection())).set(new DefaultExecuteListenerProvider(listener))).select(AUTHOR.ID).from(AUTHOR).fetch();
}
Also used : DSL(org.jooq.impl.DSL) DefaultConfiguration(org.jooq.impl.DefaultConfiguration) CallbackExecuteListener(org.jooq.impl.CallbackExecuteListener) ExecuteListener(org.jooq.ExecuteListener) Tools(org.jooq.academy.tools.Tools) DefaultConnectionProvider(org.jooq.impl.DefaultConnectionProvider) Test(org.junit.Test) SQLDialect(org.jooq.SQLDialect) Tools.connection(org.jooq.academy.tools.Tools.connection) AUTHOR(org.jooq.example.db.h2.Tables.AUTHOR) DefaultExecuteListenerProvider(org.jooq.impl.DefaultExecuteListenerProvider) CallbackExecuteListener(org.jooq.impl.CallbackExecuteListener) DefaultConnectionProvider(org.jooq.impl.DefaultConnectionProvider) DefaultExecuteListenerProvider(org.jooq.impl.DefaultExecuteListenerProvider) DefaultConfiguration(org.jooq.impl.DefaultConfiguration) CallbackExecuteListener(org.jooq.impl.CallbackExecuteListener) ExecuteListener(org.jooq.ExecuteListener) Test(org.junit.Test)

Example 25 with DefaultConfiguration

use of org.jooq.impl.DefaultConfiguration in project jOOQ by jOOQ.

the class MockResultSet method get0.

@SuppressWarnings("unchecked")
private <T> T get0(Object value, Class<T> type) {
    Converter<Object, T> converter = (record.configuration() == null ? new DefaultConfiguration() : record.configuration()).converterProvider().provide(value == null ? Object.class : (Class<Object>) value.getClass(), type);
    T converted = converter == null ? null : converter.from(value);
    wasNull = (converted == null);
    return converted;
}
Also used : DEFAULT(org.jooq.SQLDialect.DEFAULT) DefaultConfiguration(org.jooq.impl.DefaultConfiguration)

Aggregations

DefaultConfiguration (org.jooq.impl.DefaultConfiguration)27 Settings (org.jooq.conf.Settings)7 Connection (java.sql.Connection)6 Configuration (org.jooq.Configuration)6 DSLContext (org.jooq.DSLContext)6 ConnectionParam (org.simpleflatmapper.db.ConnectionParam)6 Test (org.junit.Test)5 DefaultExecuteListenerProvider (org.jooq.impl.DefaultExecuteListenerProvider)4 SfmRecordMapperProvider (org.simpleflatmapper.jooq.SfmRecordMapperProvider)4 Bean (org.springframework.context.annotation.Bean)4 Properties (java.util.Properties)3 SQLDialect (org.jooq.SQLDialect)3 DataSourceConnectionProvider (org.jooq.impl.DataSourceConnectionProvider)3 DefaultConnectionProvider (org.jooq.impl.DefaultConnectionProvider)3 Provides (dagger.Provides)2 List (java.util.List)2 Singleton (javax.inject.Singleton)2 BasicDataSource (org.apache.commons.dbcp.BasicDataSource)2 ConnectionProvider (org.jooq.ConnectionProvider)2 DbObject (org.simpleflatmapper.test.beans.DbObject)2