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();
}
}
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()
);
*/
}
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;
}
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));
}
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;
}
Aggregations