Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
simasch committed Oct 31, 2024
1 parent 8d899d3 commit 6f3c040
Show file tree
Hide file tree
Showing 5 changed files with 352 additions and 0 deletions.
42 changes: 42 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
name: CI

on:
push:
branches: [ develop, main ]
pull_request:
branches: [ develop ]

workflow_dispatch:

jobs:
build:
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v2

- name: Set up JDK 17
uses: actions/setup-java@v2
with:
java-version: '17'
distribution: 'temurin'
server-id: ossrh
server-username: MAVEN_USERNAME
server-password: MAVEN_PASSWORD
gpg-private-key: ${{ secrets.MAVEN_GPG_PRIVATE_KEY }}
gpg-passphrase: MAVEN_GPG_PASSPHRASE

- name: Cache local Maven repository
uses: actions/cache@v2
with:
path: ~/.m2/repository
key: ${{ runner.os }}-maven-${{ hashFiles('**/pom.xml') }}
restore-keys: |
${{ runner.os }}-maven-
- name: Maven Deploy
run: mvn deploy -Pdeploy
env:
MAVEN_USERNAME: ${{ secrets.OSSRH_USERNAME }}
MAVEN_PASSWORD: ${{ secrets.OSSRH_TOKEN }}
MAVEN_GPG_PASSPHRASE: ${{ secrets.MAVEN_GPG_PASSPHRASE }}
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
/target/
/.idea/
/jooq-utilities.iml
20 changes: 20 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# jOOQ Spring Integration

**jooq-spring** is a small open source library that provides inteegation of [jOOQ](https://www.jooq.org) with the [Spring Framework](https://spring.io/projects/spring-framework).

## Dependency

Add a dependency to the current version:

```xml
<dependency>
<groupId>ch.martinelli</groupId>
<artifactId>jooq-spring</artifactId>
<version>0.0.1</version>
</dependency>
```

## JooqRepository

## License
**jooq-spring** is open and free software under Apache License, Version 2: http://www.apache.org/licenses/LICENSE-2.0.html
134 changes: 134 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,134 @@
<?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>ch.martinelli</groupId>
<artifactId>jooq-spring</artifactId>
<version>0.0.1-SNAPSHOT</version>

<name>jOOQ Spring Integration</name>
<description>Helpful utilities when using jOOQ with Spring</description>
<url>https://github.com/martinellich/jooq-spring</url>
<licenses>
<license>
<name>The Apache Software License, Version 2.0</name>
<url>http://www.apache.org/licenses/LICENSE-2.0.txt</url>
<distribution>repo</distribution>
</license>
</licenses>

<developers>
<developer>
<id>simasch</id>
<name>Simon Martineli</name>
<organization>Martinelli LLC</organization>
<organizationUrl>https://martinell.ch</organizationUrl>
<email>[email protected]</email>
</developer>
</developers>

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>17</maven.compiler.source>
<maven.compiler.target>17</maven.compiler.target>
<project.scm.id>github</project.scm.id>
<jooq.version>3.19.14</jooq.version>
<spring.version>6.1.14</spring.version>
</properties>

<dependencies>
<dependency>
<groupId>org.jooq</groupId>
<artifactId>jooq</artifactId>
<version>${jooq.version}</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
<version>${spring.version}</version>
</dependency>
</dependencies>

<profiles>
<profile>
<id>deploy</id>
<build>
<plugins>
<plugin>
<groupId>com.amashchenko.maven.plugin</groupId>
<artifactId>gitflow-maven-plugin</artifactId>
<version>1.18.0</version>
<configuration>
<gitFlowConfig>
<productionBranch>main</productionBranch>
</gitFlowConfig>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-source-plugin</artifactId>
<version>2.2.1</version>
<executions>
<execution>
<id>attach-sources</id>
<goals>
<goal>jar-no-fork</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-javadoc-plugin</artifactId>
<version>3.2.0</version>
<configuration>
<source>17</source>
</configuration>
<executions>
<execution>
<id>attach-javadocs</id>
<goals>
<goal>jar</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-gpg-plugin</artifactId>
<version>3.0.1</version>
<executions>
<execution>
<id>sign-artifacts</id>
<phase>verify</phase>
<goals>
<goal>sign</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</profile>
</profiles>

<scm>
<connection>scm:git:[email protected]:martinellich/jooq-spring.git</connection>
<developerConnection>scm:git:[email protected]:martinellich/jooq-spring.git</developerConnection>
<url>[email protected]:maritnellich/jooq-spring.git</url>
</scm>

<distributionManagement>
<snapshotRepository>
<id>ossrh</id>
<url>https://oss.sonatype.org/content/repositories/snapshots</url>
</snapshotRepository>
<repository>
<id>ossrh</id>
<url>https://oss.sonatype.org/service/local/staging/deploy/maven2/</url>
</repository>
</distributionManagement>
</project>
153 changes: 153 additions & 0 deletions src/main/java/ch/martinelli/jooqspring/JooqRepository.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,153 @@
package ch.martinelli.jooqspring;

import org.jooq.Record;
import org.jooq.*;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Optional;

import static org.jooq.impl.DSL.row;

/**
* Abstract repository class for CRUD operations using jOOQ.
*
* @param <T> the type of the jOOQ Table
* @param <R> the type of the jOOQ UpdatableRecord
* @param <ID> the type of the primary key
*/
@Transactional(readOnly = true)
public abstract class JooqRepository<T extends Table<R>, R extends UpdatableRecord<R>, ID> {

protected final DSLContext dslContext;
protected final T table;

/**
* Constructs a new JooqRepository with the specified DSLContext and table.
*
* @param dslContext the DSLContext to be used for database operations
* @param table the table associated with this repository
*/
public JooqRepository(DSLContext dslContext, T table) {
this.dslContext = dslContext;
this.table = table;
}

/**
* Finds a record by its primary key.
*
* @param id the primary key value of the record to find
* @return an Optional containing the found record, or empty if no record was found
* @throws IllegalArgumentException if the table does not have a primary key
*/
public Optional<R> findById(ID id) {
if (table.getPrimaryKey() == null) {
throw new IllegalArgumentException("This method can only be called on tables with a primary key");
}
return dslContext
.selectFrom(table)
.where(eq(table.getPrimaryKey(), id))
.fetchOptional();
}

/**
* Retrieves a list of records from the database with pagination and sorting.
*
* @param offset the starting position of the first record
* @param limit the maximum number of records to retrieve
* @param orderBy the list of fields to order the result set by
* @return a List containing the fetched records
*/
public List<R> findAll(int offset, int limit, List<OrderField<?>> orderBy) {
return dslContext
.selectFrom(table)
.orderBy(orderBy)
.offset(offset)
.limit(limit)
.fetch();
}

/**
* Counts the total number of records in the associated table.
*
* @return the total number of records in the table
*/
public int count() {
return dslContext.fetchCount(table);
}

/**
* Saves the given record to the database. Attaches the record to the
* DSLContext and stores it.
*
* @param record the record to save
* @return the number of affected rows
*/
@Transactional
public int save(R record) {
dslContext.attach(record);
return record.store();
}

/**
* Merges the given record into the database. Attaches the record to the DSLContext
* and attempts to merge it.
*
* @param record the record to merge
* @return the number of affected rows
*/
@Transactional
public int merge(R record) {
dslContext.attach(record);
return record.merge();
}

/**
* Deletes the specified record from the database.
*
* @param record the record to delete
* @return the number of affected rows
*/
@Transactional
public int delete(R record) {
dslContext.attach(record);
return record.delete();
}

/**
* Deletes a record from the database identified by its primary key.
*
* @param id the primary key value of the record to delete
* @return the number of affected rows
* @throws IllegalArgumentException if the table does not have a primary key
*/
@Transactional
public int deleteById(ID id) {
if (table.getPrimaryKey() == null) {
throw new IllegalArgumentException("This method can only be called on tables with a primary key");
}
return dslContext.deleteFrom(table)
.where(eq(table.getPrimaryKey(), id))
.execute();
}

/**
* Generates a condition to be used in database queries to match a given primary key and its value.
* Inspired by {@link org.jooq.impl.DAOImpl}
*
* @param pk the primary key of the table
* @param id the value of the primary key to match
* @return a Condition used to match the specified primary key and its value
*/
@SuppressWarnings("unchecked")
private Condition eq(UniqueKey<R> pk, ID id) {
List<TableField<R, ?>> fields = pk.getFields();
if (fields.size() == 1) {
TableField<R, ?> first = fields.getFirst();
return ((Field<Object>) first).equal(first.getDataType().convert(id));
} else {
return row(pk.getFieldsArray()).equal((Record) id);
}
}

}

0 comments on commit 6f3c040

Please sign in to comment.