Search in sources :

Example 6 with DSLContext

use of org.jooq.DSLContext in project jOOQ by jOOQ.

the class Example_1_3_DMLStatements method run.

@Test
public void run() throws SQLException {
    Connection connection = connection();
    DSLContext dsl = DSL.using(connection);
    try {
        // Inserting is just as easy as selecting
        Tools.title("Inserting a new AUTHOR");
        Tools.print(dsl.insertInto(AUTHOR, AUTHOR.ID, AUTHOR.FIRST_NAME, AUTHOR.LAST_NAME).values(3, "Alfred", "Hitchcock").execute());
        // But the Java compiler will actively check your statements. The
        // following statements will not compile:
        /*
            Tools.title("Not enough arguments to the values() method!");
            Tools.print(
                DSL.using(connection())
                   .insertInto(AUTHOR, AUTHOR.ID, AUTHOR.FIRST_NAME, AUTHOR.LAST_NAME)
                   .values(4, "Alfred")
                   .execute()
            );
            */
        /*
            Tools.title("Wrong order of types of arguments to the values() method!");
            Tools.print(
                DSL.using(connection())
                   .insertInto(AUTHOR, AUTHOR.FIRST_NAME, AUTHOR.LAST_NAME, AUTHOR.ID)
                   .values(4, "Alfred", "Hitchcock")
                   .execute()
            );
            */
        Tools.title("Check if our latest record was really created");
        Tools.print(dsl.select().from(AUTHOR).where(AUTHOR.ID.eq(3)).fetch());
        Tools.title("Update the DATE_OF_BIRTH column");
        Tools.print(dsl.update(AUTHOR).set(AUTHOR.DATE_OF_BIRTH, Date.valueOf("1899-08-13")).where(AUTHOR.ID.eq(3)).execute());
        Tools.title("Check if our latest record was really updated");
        Tools.print(dsl.select().from(AUTHOR).where(AUTHOR.ID.eq(3)).fetch());
        Tools.title("Delete the new record again");
        Tools.print(dsl.delete(AUTHOR).where(AUTHOR.ID.eq(3)).execute());
        Tools.title("Check if the record was really deleted");
        Tools.print(dsl.select().from(AUTHOR).fetch());
    } finally // Don't keep the new data
    {
        connection.rollback();
    }
}
Also used : Connection(java.sql.Connection) DSLContext(org.jooq.DSLContext) Test(org.junit.Test)

Example 7 with DSLContext

use of org.jooq.DSLContext in project jOOQ by jOOQ.

the class Example_1_4_Predicates method run.

@Test
public void run() {
    DSLContext dsl = DSL.using(connection());
    Tools.title("Combine predicates using AND");
    Tools.print(dsl.select().from(BOOK).where(BOOK.TITLE.like("%a%").and(BOOK.AUTHOR_ID.eq(1))).fetch());
    /*
        Tools.title("Wrong types in comparison predicate");
        Tools.print(
            dsl.select()
               .from(BOOK)
               .where(BOOK.ID.eq("abc"))
               .fetch()
        );
        */
    Tools.title("Use an IN-predicate");
    Tools.print(dsl.select().from(AUTHOR).where(AUTHOR.ID.in(select(BOOK.AUTHOR_ID).from(BOOK))).fetch());
/*
        Tools.title("Wrong type of columns in subquery");
        Tools.print(
            dsl.select()
               .from(AUTHOR)
               .where(AUTHOR.ID.in(select(BOOK.TITLE).from(BOOK)))
               .fetch()
        );
        */
/*
        Tools.title("Wrong number of columns in subquery");
        Tools.print(
            dsl.select()
               .from(AUTHOR)
               .where(AUTHOR.ID.in(select(BOOK.AUTHOR_ID, BOOK.TITLE).from(BOOK)))
               .fetch()
        );
        */
}
Also used : DSLContext(org.jooq.DSLContext) Test(org.junit.Test)

Example 8 with DSLContext

use of org.jooq.DSLContext in project keywhiz by square.

the class ServiceModule method readonlyJooqContext.

@Provides
@Singleton
@Readonly
DSLContext readonlyJooqContext(@Readonly ManagedDataSource dataSource) throws SQLException {
    DSLContext dslContext = DSLContexts.databaseAgnostic(dataSource);
    org.jooq.Configuration configuration = dslContext.configuration();
    // Disable support for nested transactions via savepoints (required for MySQL)
    // See: https://groups.google.com/forum/#!topic/jooq-user/zG0U6CkxI5o
    configuration.set(new DefaultTransactionProvider(configuration.connectionProvider(), false));
    return dslContext;
}
Also used : DefaultTransactionProvider(org.jooq.impl.DefaultTransactionProvider) DSLContext(org.jooq.DSLContext) Readonly(keywhiz.service.config.Readonly) Singleton(com.google.inject.Singleton) Provides(com.google.inject.Provides)

Example 9 with DSLContext

use of org.jooq.DSLContext in project keywhiz by square.

the class AddUserCommand method run.

@Override
protected void run(Bootstrap<KeywhizConfig> bootstrap, Namespace namespace, KeywhizConfig config) throws Exception {
    DataSource dataSource = config.getDataSourceFactory().build(new MetricRegistry(), "add-user-datasource");
    Console console = System.console();
    System.out.format("New username:");
    String user = console.readLine();
    System.out.format("password for '%s': ", user);
    char[] password = console.readPassword();
    DSLContext dslContext = DSLContexts.databaseAgnostic(dataSource);
    new UserDAO(dslContext).createUser(user, new String(password));
}
Also used : UserDAO(keywhiz.service.daos.UserDAO) MetricRegistry(com.codahale.metrics.MetricRegistry) Console(java.io.Console) DSLContext(org.jooq.DSLContext) DataSource(javax.sql.DataSource)

Example 10 with DSLContext

use of org.jooq.DSLContext in project keywhiz by square.

the class KeywhizTestRunner method createTest.

@Override
protected Object createTest() throws Exception {
    // Reset database. Sometimes, the truncate command fails. I don't know why?
    DSLContext jooqContext = injector.getInstance(DSLContext.class);
    try {
        jooqContext.truncate(Users.USERS).execute();
    } catch (DataAccessException e) {
    }
    try {
        jooqContext.truncate(SecretsContent.SECRETS_CONTENT).execute();
    } catch (DataAccessException e) {
    }
    try {
        jooqContext.truncate(Memberships.MEMBERSHIPS).execute();
    } catch (DataAccessException e) {
    }
    try {
        jooqContext.truncate(Accessgrants.ACCESSGRANTS).execute();
    } catch (DataAccessException e) {
    }
    try {
        jooqContext.truncate(Clients.CLIENTS).execute();
    } catch (DataAccessException e) {
    }
    try {
        jooqContext.truncate(Groups.GROUPS).execute();
    } catch (DataAccessException e) {
    }
    try {
        jooqContext.truncate(Secrets.SECRETS).execute();
    } catch (DataAccessException e) {
    }
    Object object = injector.getInstance(getTestClass().getJavaClass());
    MockitoAnnotations.initMocks(object);
    return object;
}
Also used : DSLContext(org.jooq.DSLContext) DataAccessException(org.jooq.exception.DataAccessException)

Aggregations

DSLContext (org.jooq.DSLContext)47 Connection (java.sql.Connection)23 SQLException (java.sql.SQLException)17 Test (org.junit.Test)8 Timestamp (java.sql.Timestamp)6 ArrayList (java.util.ArrayList)5 Field (org.jooq.Field)5 Listener (org.spongepowered.api.event.Listener)4 MetricRegistry (com.codahale.metrics.MetricRegistry)3 JooqContainer (com.intel.mtwilson.jooq.util.JooqContainer)3 Record (org.jooq.Record)3 Settings (org.jooq.conf.Settings)3 Context (com.torodb.backend.ErrorHandler.Context)2 ResultSet (java.sql.ResultSet)2 List (java.util.List)2 UUID (java.util.UUID)2 DataSource (javax.sql.DataSource)2 Record1 (org.jooq.Record1)2 SelectQuery (org.jooq.SelectQuery)2 TableField (org.jooq.TableField)2