Permalink
Browse files

Initial commit

  • Loading branch information...
0 parents commit daa6d2ab589827624b6d96010e5cb41ae1fd02d1 @0x6e6562 committed Jan 16, 2013
@@ -0,0 +1,3 @@
+.idea
+*.iml
+target/
186 pom.xml
@@ -0,0 +1,186 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<project xmlns="http://maven.apache.org/POM/4.0.0"
+ xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+ xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
+ <modelVersion>4.0.0</modelVersion>
+
+ <groupId>net.lshift</groupId>
+ <artifactId>jooq-vernissage</artifactId>
+ <version>1.0-SNAPSHOT</version>
+
+ <name>JOOQ Vernissage</name>
+
+ <packaging>jar</packaging>
+
+ <properties>
+ <jooq.version>2.6.0</jooq.version>
+ <jodatime.version>2.1</jodatime.version>
+ <hsqldb.version>2.2.8</hsqldb.version>
+ <flyway.version>2.0.3</flyway.version>
+ <slf4j.version>1.7.2</slf4j.version>
+ <logback.version>1.0.9</logback.version>
+ <junit.version>4.11</junit.version>
+ <bonecp.version>0.7.1.RELEASE</bonecp.version>
+ <commons.lang.version>3.1</commons.lang.version>
+
+ <jdbcDriverClass>org.hsqldb.jdbc.JDBCDriver</jdbcDriverClass>
+ <jdbcUrl>jdbc:hsqldb:target/temp;hsqldb.lock_file=false</jdbcUrl>
+ <jdbcUser>SA</jdbcUser>
+ <jdbcPass />
+ </properties>
+
+ <dependencies>
+
+ <dependency>
+ <groupId>org.hsqldb</groupId>
+ <artifactId>hsqldb</artifactId>
+ <version>${hsqldb.version}</version>
+ </dependency>
+
+ <dependency>
+ <groupId>org.jooq</groupId>
+ <artifactId>jooq</artifactId>
+ <version>${jooq.version}</version>
+ </dependency>
+
+ <dependency>
+ <groupId>joda-time</groupId>
+ <artifactId>joda-time</artifactId>
+ <version>${jodatime.version}</version>
+ </dependency>
+
+ <dependency>
+ <groupId>ch.qos.logback</groupId>
+ <artifactId>logback-classic</artifactId>
+ <version>${logback.version}</version>
+ </dependency>
+
+ <dependency>
+ <groupId>com.jolbox</groupId>
+ <artifactId>bonecp</artifactId>
+ <version>${bonecp.version}</version>
+ </dependency>
+
+ <dependency>
+ <groupId>org.apache.commons</groupId>
+ <artifactId>commons-lang3</artifactId>
+ <version>${commons.lang.version}</version>
+ </dependency>
+
+ <dependency>
+ <groupId>junit</groupId>
+ <artifactId>junit</artifactId>
+ <version>${junit.version}</version>
+ <scope>test</scope>
+ </dependency>
+
+ </dependencies>
+
+ <build>
+ <plugins>
+
+ <plugin>
+ <groupId>org.apache.maven.plugins</groupId>
+ <artifactId>maven-surefire-plugin</artifactId>
+ <version>2.13</version>
+ <configuration>
+ <systemPropertyVariables>
+ <jdbcDriverClass>${jdbcDriverClass}</jdbcDriverClass>
+ <jdbcUrl>${jdbcUrl}</jdbcUrl>
+ <jdbcUser>${jdbcUser}</jdbcUser>
+ <jdbcPass>${jdbcPass}</jdbcPass>
+ </systemPropertyVariables>
+ </configuration>
+ </plugin>
+
+ <plugin>
+ <groupId>com.googlecode.flyway</groupId>
+ <artifactId>flyway-maven-plugin</artifactId>
+ <version>${flyway.version}</version>
+ <dependencies>
+ <dependency>
+ <groupId>org.hsqldb</groupId>
+ <artifactId>hsqldb</artifactId>
+ <version>${hsqldb.version}</version>
+ </dependency>
+ </dependencies>
+ <configuration>
+ <url>${jdbcUrl}</url>
+ <driver>${jdbcDriverClass}</driver>
+ <user>${jdbcUser}</user>
+ <password>${jdbcPass}</password>
+ </configuration>
+ <executions>
+ <execution>
+ <id>migrate-db</id>
+ <phase>process-resources</phase>
+ <goals>
+ <goal>migrate</goal>
+ </goals>
+ </execution>
+ </executions>
+ </plugin>
+
+ <plugin>
+ <groupId>org.jooq</groupId>
+ <artifactId>jooq-codegen-maven</artifactId>
+ <version>${jooq.version}</version>
+ <dependencies>
+ <dependency>
+ <groupId>org.hsqldb</groupId>
+ <artifactId>hsqldb</artifactId>
+ <version>${hsqldb.version}</version>
+ </dependency>
+ <dependency>
+ <groupId>ch.qos.logback</groupId>
+ <artifactId>logback-classic</artifactId>
+ <version>${logback.version}</version>
+ </dependency>
+ <dependency>
+ <groupId>ch.qos.logback</groupId>
+ <artifactId>logback-core</artifactId>
+ <version>${logback.version}</version>
+ </dependency>
+ </dependencies>
+ <configuration>
+ <jdbc>
+ <driver>${jdbcDriverClass}</driver>
+ <url>${jdbcUrl}</url>
+ <user>${jdbcUser}</user>
+ <password>${jdbcPass}</password>
+ </jdbc>
+ <generator>
+ <name>org.jooq.util.DefaultGenerator</name>
+ <database>
+ <name>org.jooq.util.hsqldb.HSQLDBDatabase</name>
+ <includes>.*</includes>
+ <excludes />
+ <inputSchema>PUBLIC</inputSchema>
+ </database>
+ <generate>
+ <relations>true</relations>
+ <deprecated>false</deprecated>
+ <daos>true</daos>
+ </generate>
+ <target>
+ <packageName>net.lshift.jooq.schema</packageName>
+ <directory>${basedir}/target/generated-sources/jooq</directory>
+ </target>
+ </generator>
+ </configuration>
+ <executions>
+ <execution>
+ <id>codegen-jooq</id>
+ <phase>process-resources</phase>
+ <goals>
+ <goal>generate</goal>
+ </goals>
+ </execution>
+ </executions>
+ </plugin>
+
+ </plugins>
+ </build>
+
+
+</project>
@@ -0,0 +1,5 @@
+create table simple_table (
+ id varchar(32) primary key,
+ version bigint not null,
+ entry_date date
+);
@@ -0,0 +1,66 @@
+package net.lshift.jooq;
+
+import com.jolbox.bonecp.BoneCPConfig;
+import com.jolbox.bonecp.BoneCPDataSource;
+import org.jooq.SQLDialect;
+import org.jooq.impl.Factory;
+import org.junit.Before;
+
+import javax.sql.DataSource;
+import java.sql.Connection;
+import java.sql.SQLException;
+
+public abstract class AbstractDatabaseTest {
+
+ protected DataSource dataSource;
+ protected SQLDialect dialect;
+
+ @Before
+ public void setUp() {
+ dialect = getDialect();
+ dataSource = getDataSource();
+ }
+
+ protected DataSource getDataSource() {
+ BoneCPDataSource ds = new BoneCPDataSource();
+ ds.setDriverClass(System.getProperty("jdbcDriverClass", "org.hsqldb.jdbc.JDBCDriver"));
+ ds.setJdbcUrl(System.getProperty("jdbcUrl", "jdbc:hsqldb:target/temp;hsqldb.lock_file=false"));
+ ds.setUsername(System.getProperty("jdbcUser", "SA"));
+ ds.setPassword(System.getProperty("jdbcPass", ""));
+ return ds;
+ }
+
+ protected SQLDialect getDialect() {
+ return SQLDialect.valueOf("HSQLDB");
+ }
+
+ protected Factory getFactory(Connection c) {
+ return new Factory(c, dialect);
+ }
+
+ protected void closeConnection(Connection connection) {
+ closeConnection(connection, false);
+ }
+
+ protected void closeConnection(Connection connection, boolean shouldCommit) {
+ try {
+ if (shouldCommit) {
+ connection.commit();
+ }
+ connection.close();
+ } catch (SQLException e) {
+ throw new RuntimeException(e);
+ }
+ }
+
+ protected Connection getConnection() {
+ Connection c;
+
+ try {
+ c = dataSource.getConnection();
+ } catch (SQLException e) {
+ throw new RuntimeException(e);
+ }
+ return c;
+ }
+}
@@ -0,0 +1,120 @@
+package net.lshift.jooq;
+
+import static net.lshift.jooq.schema.tables.SimpleTable.SIMPLE_TABLE;
+
+import net.lshift.jooq.schema.tables.SimpleTable;
+import net.lshift.jooq.schema.tables.daos.SimpleTableDao;
+import net.lshift.jooq.schema.tables.records.SimpleTableRecord;
+import org.apache.commons.lang3.RandomStringUtils;
+import org.joda.time.DateTime;
+import org.jooq.*;
+import org.jooq.impl.Factory;
+
+import org.junit.Test;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.sql.Connection;
+import java.sql.Date;
+import java.util.List;
+
+public class JooqTest extends AbstractDatabaseTest {
+
+ static Logger log = LoggerFactory.getLogger(JooqTest.class);
+
+ @Test
+ public void simpleExample() throws Exception {
+ Connection connection = getConnection();
+ Factory db = getFactory(connection);
+
+ final DateTime startDate = new DateTime();
+ final long startVersion = System.currentTimeMillis();
+
+ int rows = 10;
+
+ for (int i = 0; i < rows; i++) {
+ db.insertInto(SIMPLE_TABLE).
+ set(SIMPLE_TABLE.ID, RandomStringUtils.randomAlphanumeric(10)).
+ set(SIMPLE_TABLE.VERSION, startVersion + i).
+ set(SIMPLE_TABLE.ENTRY_DATE, new Date(startDate.getMillis())).
+ execute();
+ }
+
+ // Run the same query twice to show the difference between the eager and lazy fetches
+
+ Result<Record> hydratedResults = db.select().from(SIMPLE_TABLE).fetch();
+
+ Cursor<Record> resultPointer = db.select().from(SIMPLE_TABLE).fetchLazy();
+
+ // Manual way of binding results
+
+ for (Record record : resultPointer) {
+ Long fetchedVersion = record.getValue(SIMPLE_TABLE.VERSION);
+ }
+
+ // A little bit less manual way of binding results
+
+ List<SimpleTableRecord> simpleTableRecords = db.fetch(SIMPLE_TABLE).into(SimpleTableRecord.class);
+
+ // Even more enterprisey (but not quite enterprise ready (TM))
+
+ SimpleTableDao dao = new SimpleTableDao(db);
+ List<net.lshift.jooq.schema.tables.pojos.SimpleTable> pojos = dao.findAll();
+
+ // JPA annotations are also possible, but let's look at some useful stuff instead .....
+
+ // Simple WHERE clause
+
+ final Date cutoff = new Date(startDate.plusWeeks(1).getMillis());
+
+ db.select().from(SIMPLE_TABLE).where(SIMPLE_TABLE.ENTRY_DATE.lt(cutoff)).execute();
+
+ // Add a second predicate to the where clause
+
+ Condition c1 = SIMPLE_TABLE.ENTRY_DATE.lt(cutoff);
+ Condition c2 = SIMPLE_TABLE.VERSION.eq(startVersion);
+
+ db.select().from(SIMPLE_TABLE).where(c1, c2).execute();
+
+ // Now do some aggregation
+
+ SelectHavingStep average =
+ db.select(SIMPLE_TABLE.VERSION.sum().as("Total"),
+ SIMPLE_TABLE.VERSION.avg().as("Some Common Name"), // USe this to control what the result set column is named
+ SIMPLE_TABLE.ENTRY_DATE).
+ from(SIMPLE_TABLE).
+ groupBy(SIMPLE_TABLE.ENTRY_DATE);
+
+ average.execute();
+
+ // Stack some query parts together in a union
+
+ SelectHavingStep stdDev =
+ db.select(SIMPLE_TABLE.VERSION.sum().as("Total"),
+ SIMPLE_TABLE.VERSION.stddevSamp().as("Some Common Name"), // USe this to control what the result set column is named
+ SIMPLE_TABLE.ENTRY_DATE).
+ from(SIMPLE_TABLE).
+ groupBy(SIMPLE_TABLE.ENTRY_DATE);
+
+ Select<Record> union = average.union(stdDev);
+
+ db.select(union.getFields()).from(union).execute();
+
+ // Server side merge
+
+ db.insertInto(SIMPLE_TABLE).
+ set(SIMPLE_TABLE.ID, "possible duplicate primary key").
+ set(SIMPLE_TABLE.VERSION, startVersion).
+ set(SIMPLE_TABLE.ENTRY_DATE, new Date(startDate.getMillis())).
+ onDuplicateKeyUpdate().
+ set(SIMPLE_TABLE.VERSION, startVersion).
+ set(SIMPLE_TABLE.ENTRY_DATE, new Date(startDate.getMillis())).
+ execute();
+
+ // JOOQ doesn't concern itself with commits or connection management -> you have to do this yourself
+
+ connection.commit();
+ connection.close();
+
+ }
+}

0 comments on commit daa6d2a

Please sign in to comment.