Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Initial commit

  • Loading branch information...
commit daa6d2ab589827624b6d96010e5cb41ae1fd02d1 0 parents
Ben Hood authored
3  .gitignore
... ...
@@ -0,0 +1,3 @@
  1
+.idea
  2
+*.iml
  3
+target/
186  pom.xml
... ...
@@ -0,0 +1,186 @@
  1
+<?xml version="1.0" encoding="UTF-8"?>
  2
+<project xmlns="http://maven.apache.org/POM/4.0.0"
  3
+         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4
+         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  5
+  <modelVersion>4.0.0</modelVersion>
  6
+
  7
+  <groupId>net.lshift</groupId>
  8
+  <artifactId>jooq-vernissage</artifactId>
  9
+  <version>1.0-SNAPSHOT</version>
  10
+
  11
+  <name>JOOQ Vernissage</name>
  12
+
  13
+  <packaging>jar</packaging>
  14
+
  15
+  <properties>
  16
+    <jooq.version>2.6.0</jooq.version>
  17
+    <jodatime.version>2.1</jodatime.version>
  18
+    <hsqldb.version>2.2.8</hsqldb.version>
  19
+    <flyway.version>2.0.3</flyway.version>
  20
+    <slf4j.version>1.7.2</slf4j.version>
  21
+    <logback.version>1.0.9</logback.version>
  22
+    <junit.version>4.11</junit.version>
  23
+    <bonecp.version>0.7.1.RELEASE</bonecp.version>
  24
+    <commons.lang.version>3.1</commons.lang.version>
  25
+
  26
+    <jdbcDriverClass>org.hsqldb.jdbc.JDBCDriver</jdbcDriverClass>
  27
+    <jdbcUrl>jdbc:hsqldb:target/temp;hsqldb.lock_file=false</jdbcUrl>
  28
+    <jdbcUser>SA</jdbcUser>
  29
+    <jdbcPass />
  30
+  </properties>
  31
+
  32
+  <dependencies>
  33
+
  34
+    <dependency>
  35
+      <groupId>org.hsqldb</groupId>
  36
+      <artifactId>hsqldb</artifactId>
  37
+      <version>${hsqldb.version}</version>
  38
+    </dependency>
  39
+
  40
+    <dependency>
  41
+      <groupId>org.jooq</groupId>
  42
+      <artifactId>jooq</artifactId>
  43
+      <version>${jooq.version}</version>
  44
+    </dependency>
  45
+
  46
+    <dependency>
  47
+      <groupId>joda-time</groupId>
  48
+      <artifactId>joda-time</artifactId>
  49
+      <version>${jodatime.version}</version>
  50
+    </dependency>
  51
+
  52
+    <dependency>
  53
+      <groupId>ch.qos.logback</groupId>
  54
+      <artifactId>logback-classic</artifactId>
  55
+      <version>${logback.version}</version>
  56
+    </dependency>
  57
+
  58
+    <dependency>
  59
+      <groupId>com.jolbox</groupId>
  60
+      <artifactId>bonecp</artifactId>
  61
+      <version>${bonecp.version}</version>
  62
+    </dependency>
  63
+
  64
+    <dependency>
  65
+      <groupId>org.apache.commons</groupId>
  66
+      <artifactId>commons-lang3</artifactId>
  67
+      <version>${commons.lang.version}</version>
  68
+    </dependency>
  69
+
  70
+    <dependency>
  71
+      <groupId>junit</groupId>
  72
+      <artifactId>junit</artifactId>
  73
+      <version>${junit.version}</version>
  74
+      <scope>test</scope>
  75
+    </dependency>
  76
+
  77
+  </dependencies>
  78
+
  79
+  <build>
  80
+    <plugins>
  81
+
  82
+      <plugin>
  83
+        <groupId>org.apache.maven.plugins</groupId>
  84
+        <artifactId>maven-surefire-plugin</artifactId>
  85
+        <version>2.13</version>
  86
+        <configuration>
  87
+          <systemPropertyVariables>
  88
+            <jdbcDriverClass>${jdbcDriverClass}</jdbcDriverClass>
  89
+            <jdbcUrl>${jdbcUrl}</jdbcUrl>
  90
+            <jdbcUser>${jdbcUser}</jdbcUser>
  91
+            <jdbcPass>${jdbcPass}</jdbcPass>
  92
+          </systemPropertyVariables>
  93
+        </configuration>
  94
+      </plugin>
  95
+
  96
+      <plugin>
  97
+        <groupId>com.googlecode.flyway</groupId>
  98
+        <artifactId>flyway-maven-plugin</artifactId>
  99
+        <version>${flyway.version}</version>
  100
+        <dependencies>
  101
+          <dependency>
  102
+            <groupId>org.hsqldb</groupId>
  103
+            <artifactId>hsqldb</artifactId>
  104
+            <version>${hsqldb.version}</version>
  105
+          </dependency>
  106
+        </dependencies>
  107
+        <configuration>
  108
+          <url>${jdbcUrl}</url>
  109
+          <driver>${jdbcDriverClass}</driver>
  110
+          <user>${jdbcUser}</user>
  111
+          <password>${jdbcPass}</password>
  112
+        </configuration>
  113
+        <executions>
  114
+          <execution>
  115
+            <id>migrate-db</id>
  116
+            <phase>process-resources</phase>
  117
+            <goals>
  118
+              <goal>migrate</goal>
  119
+            </goals>
  120
+          </execution>
  121
+        </executions>
  122
+      </plugin>
  123
+
  124
+      <plugin>
  125
+        <groupId>org.jooq</groupId>
  126
+        <artifactId>jooq-codegen-maven</artifactId>
  127
+        <version>${jooq.version}</version>
  128
+        <dependencies>
  129
+          <dependency>
  130
+            <groupId>org.hsqldb</groupId>
  131
+            <artifactId>hsqldb</artifactId>
  132
+            <version>${hsqldb.version}</version>
  133
+          </dependency>
  134
+          <dependency>
  135
+            <groupId>ch.qos.logback</groupId>
  136
+            <artifactId>logback-classic</artifactId>
  137
+            <version>${logback.version}</version>
  138
+          </dependency>
  139
+          <dependency>
  140
+            <groupId>ch.qos.logback</groupId>
  141
+            <artifactId>logback-core</artifactId>
  142
+            <version>${logback.version}</version>
  143
+          </dependency>
  144
+        </dependencies>
  145
+        <configuration>
  146
+          <jdbc>
  147
+            <driver>${jdbcDriverClass}</driver>
  148
+            <url>${jdbcUrl}</url>
  149
+            <user>${jdbcUser}</user>
  150
+            <password>${jdbcPass}</password>
  151
+          </jdbc>
  152
+          <generator>
  153
+            <name>org.jooq.util.DefaultGenerator</name>
  154
+            <database>
  155
+              <name>org.jooq.util.hsqldb.HSQLDBDatabase</name>
  156
+              <includes>.*</includes>
  157
+              <excludes />
  158
+              <inputSchema>PUBLIC</inputSchema>
  159
+            </database>
  160
+            <generate>
  161
+              <relations>true</relations>
  162
+              <deprecated>false</deprecated>
  163
+              <daos>true</daos>
  164
+            </generate>
  165
+            <target>
  166
+              <packageName>net.lshift.jooq.schema</packageName>
  167
+              <directory>${basedir}/target/generated-sources/jooq</directory>
  168
+            </target>
  169
+          </generator>
  170
+        </configuration>
  171
+        <executions>
  172
+          <execution>
  173
+            <id>codegen-jooq</id>
  174
+            <phase>process-resources</phase>
  175
+            <goals>
  176
+              <goal>generate</goal>
  177
+            </goals>
  178
+          </execution>
  179
+        </executions>
  180
+      </plugin>
  181
+
  182
+    </plugins>
  183
+  </build>
  184
+
  185
+
  186
+</project>
5  src/main/resources/db/migration/V1_1__Initial_Schema.sql
... ...
@@ -0,0 +1,5 @@
  1
+create table simple_table (
  2
+  id varchar(32) primary key,
  3
+  version bigint not null,
  4
+  entry_date date
  5
+);
66  src/test/java/net/lshift/jooq/AbstractDatabaseTest.java
... ...
@@ -0,0 +1,66 @@
  1
+package net.lshift.jooq;
  2
+
  3
+import com.jolbox.bonecp.BoneCPConfig;
  4
+import com.jolbox.bonecp.BoneCPDataSource;
  5
+import org.jooq.SQLDialect;
  6
+import org.jooq.impl.Factory;
  7
+import org.junit.Before;
  8
+
  9
+import javax.sql.DataSource;
  10
+import java.sql.Connection;
  11
+import java.sql.SQLException;
  12
+
  13
+public abstract class AbstractDatabaseTest {
  14
+
  15
+  protected DataSource dataSource;
  16
+  protected SQLDialect dialect;
  17
+
  18
+  @Before
  19
+  public void setUp() {
  20
+    dialect = getDialect();
  21
+    dataSource = getDataSource();
  22
+  }
  23
+
  24
+  protected DataSource getDataSource() {
  25
+    BoneCPDataSource ds = new BoneCPDataSource();
  26
+    ds.setDriverClass(System.getProperty("jdbcDriverClass", "org.hsqldb.jdbc.JDBCDriver"));
  27
+    ds.setJdbcUrl(System.getProperty("jdbcUrl", "jdbc:hsqldb:target/temp;hsqldb.lock_file=false"));
  28
+    ds.setUsername(System.getProperty("jdbcUser", "SA"));
  29
+    ds.setPassword(System.getProperty("jdbcPass", ""));
  30
+    return ds;
  31
+  }
  32
+
  33
+  protected SQLDialect getDialect() {
  34
+    return SQLDialect.valueOf("HSQLDB");
  35
+  }
  36
+
  37
+  protected Factory getFactory(Connection c) {
  38
+    return new Factory(c, dialect);
  39
+  }
  40
+
  41
+  protected void closeConnection(Connection connection) {
  42
+    closeConnection(connection, false);
  43
+  }
  44
+
  45
+  protected void closeConnection(Connection connection, boolean shouldCommit) {
  46
+    try {
  47
+      if (shouldCommit) {
  48
+        connection.commit();
  49
+      }
  50
+      connection.close();
  51
+    } catch (SQLException e) {
  52
+      throw new RuntimeException(e);
  53
+    }
  54
+  }
  55
+
  56
+  protected Connection getConnection() {
  57
+    Connection c;
  58
+
  59
+    try {
  60
+      c = dataSource.getConnection();
  61
+    } catch (SQLException e) {
  62
+      throw new RuntimeException(e);
  63
+    }
  64
+    return c;
  65
+  }
  66
+}
120  src/test/java/net/lshift/jooq/JooqTest.java
... ...
@@ -0,0 +1,120 @@
  1
+package net.lshift.jooq;
  2
+
  3
+import static net.lshift.jooq.schema.tables.SimpleTable.SIMPLE_TABLE;
  4
+
  5
+import net.lshift.jooq.schema.tables.SimpleTable;
  6
+import net.lshift.jooq.schema.tables.daos.SimpleTableDao;
  7
+import net.lshift.jooq.schema.tables.records.SimpleTableRecord;
  8
+import org.apache.commons.lang3.RandomStringUtils;
  9
+import org.joda.time.DateTime;
  10
+import org.jooq.*;
  11
+import org.jooq.impl.Factory;
  12
+
  13
+import org.junit.Test;
  14
+import org.slf4j.Logger;
  15
+import org.slf4j.LoggerFactory;
  16
+
  17
+import java.sql.Connection;
  18
+import java.sql.Date;
  19
+import java.util.List;
  20
+
  21
+public class JooqTest extends AbstractDatabaseTest {
  22
+
  23
+  static Logger log = LoggerFactory.getLogger(JooqTest.class);
  24
+
  25
+  @Test
  26
+  public void simpleExample() throws Exception {
  27
+    Connection connection = getConnection();
  28
+    Factory db = getFactory(connection);
  29
+
  30
+    final DateTime startDate = new DateTime();
  31
+    final long startVersion = System.currentTimeMillis();
  32
+
  33
+    int rows = 10;
  34
+
  35
+    for (int i = 0; i < rows; i++) {
  36
+      db.insertInto(SIMPLE_TABLE).
  37
+          set(SIMPLE_TABLE.ID, RandomStringUtils.randomAlphanumeric(10)).
  38
+          set(SIMPLE_TABLE.VERSION, startVersion + i).
  39
+          set(SIMPLE_TABLE.ENTRY_DATE, new Date(startDate.getMillis())).
  40
+          execute();
  41
+    }
  42
+
  43
+    // Run the same query twice to show the difference between the eager and lazy fetches
  44
+
  45
+    Result<Record> hydratedResults = db.select().from(SIMPLE_TABLE).fetch();
  46
+
  47
+    Cursor<Record> resultPointer = db.select().from(SIMPLE_TABLE).fetchLazy();
  48
+
  49
+    // Manual way of binding results
  50
+
  51
+    for (Record record : resultPointer) {
  52
+      Long fetchedVersion = record.getValue(SIMPLE_TABLE.VERSION);
  53
+    }
  54
+
  55
+    // A little bit less manual way of binding results
  56
+
  57
+    List<SimpleTableRecord> simpleTableRecords = db.fetch(SIMPLE_TABLE).into(SimpleTableRecord.class);
  58
+
  59
+    // Even more enterprisey (but not quite enterprise ready (TM))
  60
+
  61
+    SimpleTableDao dao = new SimpleTableDao(db);
  62
+    List<net.lshift.jooq.schema.tables.pojos.SimpleTable> pojos = dao.findAll();
  63
+
  64
+    // JPA annotations are also possible, but let's look at some useful stuff instead .....
  65
+
  66
+    // Simple WHERE clause
  67
+
  68
+    final Date cutoff = new Date(startDate.plusWeeks(1).getMillis());
  69
+
  70
+    db.select().from(SIMPLE_TABLE).where(SIMPLE_TABLE.ENTRY_DATE.lt(cutoff)).execute();
  71
+
  72
+    // Add a second predicate to the where clause
  73
+
  74
+    Condition c1 = SIMPLE_TABLE.ENTRY_DATE.lt(cutoff);
  75
+    Condition c2 = SIMPLE_TABLE.VERSION.eq(startVersion);
  76
+
  77
+    db.select().from(SIMPLE_TABLE).where(c1, c2).execute();
  78
+
  79
+    // Now do some aggregation
  80
+
  81
+    SelectHavingStep average =
  82
+        db.select(SIMPLE_TABLE.VERSION.sum().as("Total"),
  83
+                SIMPLE_TABLE.VERSION.avg().as("Some Common Name"), // USe this to control what the result set column is named
  84
+                SIMPLE_TABLE.ENTRY_DATE).
  85
+           from(SIMPLE_TABLE).
  86
+           groupBy(SIMPLE_TABLE.ENTRY_DATE);
  87
+
  88
+    average.execute();
  89
+
  90
+    // Stack some query parts together in a union
  91
+
  92
+    SelectHavingStep stdDev =
  93
+        db.select(SIMPLE_TABLE.VERSION.sum().as("Total"),
  94
+                  SIMPLE_TABLE.VERSION.stddevSamp().as("Some Common Name"), // USe this to control what the result set column is named
  95
+                  SIMPLE_TABLE.ENTRY_DATE).
  96
+           from(SIMPLE_TABLE).
  97
+           groupBy(SIMPLE_TABLE.ENTRY_DATE);
  98
+
  99
+    Select<Record> union = average.union(stdDev);
  100
+
  101
+    db.select(union.getFields()).from(union).execute();
  102
+
  103
+    // Server side merge
  104
+
  105
+    db.insertInto(SIMPLE_TABLE).
  106
+          set(SIMPLE_TABLE.ID, "possible duplicate primary key").
  107
+          set(SIMPLE_TABLE.VERSION, startVersion).
  108
+          set(SIMPLE_TABLE.ENTRY_DATE, new Date(startDate.getMillis())).
  109
+       onDuplicateKeyUpdate().
  110
+          set(SIMPLE_TABLE.VERSION, startVersion).
  111
+          set(SIMPLE_TABLE.ENTRY_DATE, new Date(startDate.getMillis())).
  112
+       execute();
  113
+
  114
+    // JOOQ doesn't concern itself with commits or connection management -> you have to do this yourself
  115
+
  116
+    connection.commit();
  117
+    connection.close();
  118
+
  119
+  }
  120
+}

0 notes on commit daa6d2a

Please sign in to comment.
Something went wrong with that request. Please try again.