Skip to content

Commit

Permalink
Merge pull request #1245 from lukas-krecan/jooq
Browse files Browse the repository at this point in the history
Jooq
  • Loading branch information
lukas-krecan authored Nov 19, 2022
2 parents 902e33a + 2939161 commit 836d96b
Show file tree
Hide file tree
Showing 15 changed files with 551 additions and 4 deletions.
1 change: 1 addition & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
<module>providers/jdbc/shedlock-test-support-jdbc</module>
<module>providers/jdbc/shedlock-provider-jdbc-internal</module>
<module>providers/jdbc/shedlock-provider-jdbc</module>
<module>providers/jdbc/shedlock-provider-jooq</module>
<module>providers/jdbc/shedlock-provider-jdbc-template</module>
<module>providers/jdbc/shedlock-provider-jdbc-micronaut</module>
<module>providers/r2dbc/shedlock-provider-r2dbc</module>
Expand Down
58 changes: 58 additions & 0 deletions providers/jdbc/shedlock-provider-jooq/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
<?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">
<parent>
<artifactId>shedlock-parent</artifactId>
<groupId>net.javacrumbs.shedlock</groupId>
<version>5.0.0-SNAPSHOT</version>
<relativePath>../../../pom.xml</relativePath>
</parent>
<modelVersion>4.0.0</modelVersion>

<artifactId>shedlock-provider-jooq</artifactId>
<version>5.0.0-SNAPSHOT</version>

<dependencies>
<dependency>
<groupId>net.javacrumbs.shedlock</groupId>
<artifactId>shedlock-core</artifactId>
<version>${project.version}</version>
</dependency>

<dependency>
<groupId>org.jooq</groupId>
<artifactId>jooq</artifactId>
<version>3.17.5</version>
</dependency>

<dependency>
<groupId>net.javacrumbs.shedlock</groupId>
<artifactId>shedlock-test-support-jdbc</artifactId>
<version>${project.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<version>${logback.ver}</version>
<scope>test</scope>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<configuration>
<archive>
<manifestEntries>
<Automatic-Module-Name>
net.javacrumbs.shedlock.provider.jooq
</Automatic-Module-Name>
</manifestEntries>
</archive>
</configuration>
</plugin>
</plugins>
</build>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/**
* Copyright 2009 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package net.javacrumbs.shedlock.provider.jooq;

import net.javacrumbs.shedlock.support.StorageBasedLockProvider;
import net.javacrumbs.shedlock.support.annotation.NonNull;
import org.jooq.DSLContext;


public class JooqLockProvider extends StorageBasedLockProvider {
public JooqLockProvider(@NonNull DSLContext dslContext) {
super(new JooqStorageAccessor(dslContext));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
package net.javacrumbs.shedlock.provider.jooq;

import net.javacrumbs.shedlock.core.LockConfiguration;
import net.javacrumbs.shedlock.support.AbstractStorageAccessor;
import net.javacrumbs.shedlock.support.annotation.NonNull;
import org.jooq.DSLContext;
import org.jooq.Field;
import org.jooq.Record;
import org.jooq.TableField;
import org.jooq.types.DayToSecond;

import java.io.Serializable;
import java.time.Duration;
import java.time.LocalDateTime;
import java.util.Map;

import static net.javacrumbs.shedlock.provider.jooq.Shedlock.SHEDLOCK;
import static org.jooq.impl.DSL.currentLocalDateTime;
import static org.jooq.impl.DSL.inline;
import static org.jooq.impl.DSL.localDateTimeAdd;
import static org.jooq.impl.DSL.when;

class JooqStorageAccessor extends AbstractStorageAccessor {
private final DSLContext dslContext;
private final Shedlock t = SHEDLOCK;

JooqStorageAccessor(DSLContext dslContext) {
this.dslContext = dslContext;
}

@Override
public boolean insertRecord(@NonNull LockConfiguration lockConfiguration) {
return dslContext.transactionResult(tx -> tx.dsl().insertInto(t)
.set(data(lockConfiguration))
.onConflictDoNothing()
.execute() > 0);
}

@Override
public boolean updateRecord(@NonNull LockConfiguration lockConfiguration) {
return dslContext.transactionResult(tx -> tx.dsl().update(t)
.set(data(lockConfiguration))
.where(t.NAME.eq(lockConfiguration.getName()).and(t.LOCK_UNTIL.le(now())))
.execute() > 0);
}

@Override
public void unlock(LockConfiguration lockConfiguration) {
Field<LocalDateTime> lockAtLeastFor = t.LOCKED_AT.add(DayToSecond.valueOf(lockConfiguration.getLockAtLeastFor()));
dslContext.transaction(tx -> tx.dsl().update(t).set(t.LOCK_UNTIL, when(lockAtLeastFor.gt(now()), lockAtLeastFor).otherwise(now()))
.where(t.NAME.eq(lockConfiguration.getName()).and(t.LOCKED_BY.eq(getHostname())))
.execute());
}

@Override
public boolean extend(@NonNull LockConfiguration lockConfiguration) {
return dslContext.transactionResult(tx -> tx.dsl().update(t).set(t.LOCK_UNTIL, nowPlus(lockConfiguration.getLockAtMostFor()))
.where(t.NAME.eq(lockConfiguration.getName()).and(t.LOCKED_BY.eq(getHostname())).and(t.LOCK_UNTIL.gt(now())))
.execute() > 0);
}


private Map<? extends TableField<Record, ? extends Serializable>, Serializable> data(LockConfiguration lockConfiguration) {
return Map.of(
t.NAME, lockConfiguration.getName(),
t.LOCK_UNTIL, nowPlus(lockConfiguration.getLockAtMostFor()),
t.LOCKED_AT, now(),
t.LOCKED_BY, getHostname()
);
}

private Field<LocalDateTime> now() {
return currentLocalDateTime(inline(6));
}

private Field<LocalDateTime> nowPlus(Duration duration) {
return localDateTimeAdd(now(), DayToSecond.valueOf(duration));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,142 @@
package net.javacrumbs.shedlock.provider.jooq;


import org.jooq.Field;
import org.jooq.ForeignKey;
import org.jooq.Name;
import org.jooq.Record;
import org.jooq.Table;
import org.jooq.TableField;
import org.jooq.TableOptions;
import org.jooq.UniqueKey;
import org.jooq.impl.DSL;
import org.jooq.impl.Internal;
import org.jooq.impl.SQLDataType;
import org.jooq.impl.TableImpl;

import java.time.LocalDateTime;


/**
* This class is generated by jOOQ.
*/
@SuppressWarnings({ "all", "unchecked", "rawtypes" })
class Shedlock extends TableImpl<Record> {

private static final long serialVersionUID = 1L;

/**
* The reference instance of <code>public.shedlock</code>
*/
public static final Shedlock SHEDLOCK = new Shedlock();

public static final UniqueKey<Record> SHEDLOCK_PKEY = Internal.createUniqueKey(Shedlock.SHEDLOCK, DSL.name("shedlock_pkey"), new TableField[] { Shedlock.SHEDLOCK.NAME }, true);


/**
* The class holding records for this type
*/
@Override
public Class<Record> getRecordType() {
return Record.class;
}

/**
* The column <code>public.shedlock.name</code>.
*/
public final TableField<Record, String> NAME = createField(DSL.name("name"), SQLDataType.VARCHAR(64).nullable(false), this, "");

/**
* The column <code>public.shedlock.lock_until</code>.
*/
public final TableField<Record, LocalDateTime> LOCK_UNTIL = createField(DSL.name("lock_until"), SQLDataType.LOCALDATETIME(6).nullable(false), this, "");

/**
* The column <code>public.shedlock.locked_at</code>.
*/
public final TableField<Record, LocalDateTime> LOCKED_AT = createField(DSL.name("locked_at"), SQLDataType.LOCALDATETIME(6).nullable(false), this, "");

/**
* The column <code>public.shedlock.locked_by</code>.
*/
public final TableField<Record, String> LOCKED_BY = createField(DSL.name("locked_by"), SQLDataType.VARCHAR(255).nullable(false), this, "");

private Shedlock(Name alias, Table<Record> aliased) {
this(alias, aliased, null);
}

private Shedlock(Name alias, Table<Record> aliased, Field<?>[] parameters) {
super(alias, null, aliased, parameters, DSL.comment(""), TableOptions.table());
}

/**
* Create an aliased <code>public.shedlock</code> table reference
*/
public Shedlock(String alias) {
this(DSL.name(alias), SHEDLOCK);
}

/**
* Create an aliased <code>public.shedlock</code> table reference
*/
public Shedlock(Name alias) {
this(alias, SHEDLOCK);
}

/**
* Create a <code>public.shedlock</code> table reference
*/
public Shedlock() {
this(DSL.name("shedlock"), null);
}

public <O extends Record> Shedlock(Table<O> child, ForeignKey<O, Record> key) {
super(child, key, SHEDLOCK);
}


@Override
public Shedlock as(String alias) {
return new Shedlock(DSL.name(alias), this);
}

@Override
public Shedlock as(Name alias) {
return new Shedlock(alias, this);
}

@Override
public Shedlock as(Table<?> alias) {
return new Shedlock(alias.getQualifiedName(), this);
}
@Override
public UniqueKey<Record> getPrimaryKey() {
return SHEDLOCK_PKEY;
}

/**
* Rename this table
*/
@Override
public Shedlock rename(String name) {
return new Shedlock(DSL.name(name), null);
}

/**
* Rename this table
*/
@Override
public Shedlock rename(Name name) {
return new Shedlock(name, null);
}

/**
* Rename this table
*/
@Override
public Shedlock rename(Table<?> name) {
return new Shedlock(name.getQualifiedName(), null);
}
}


Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
/**
* Copyright 2009 the original author or authors.
* <p>
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* <p>
* http://www.apache.org/licenses/LICENSE-2.0
* <p>
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package net.javacrumbs.shedlock.provider.jooq;

import net.javacrumbs.shedlock.support.StorageBasedLockProvider;
import net.javacrumbs.shedlock.test.support.jdbc.AbstractJdbcLockProviderIntegrationTest;
import net.javacrumbs.shedlock.test.support.jdbc.DbConfig;
import org.jetbrains.annotations.NotNull;
import org.jooq.DSLContext;

public abstract class AbstractJooqLockProviderIntegrationTest extends AbstractJdbcLockProviderIntegrationTest {
private final DbConfig dbConfig;

private final DSLContext dslContext;

public AbstractJooqLockProviderIntegrationTest(DbConfig dbConfig, @NotNull DSLContext dslContext) {
this.dbConfig = dbConfig;
this.dslContext = dslContext;
}

@Override
protected DbConfig getDbConfig() {
return dbConfig;
}

@Override
protected StorageBasedLockProvider getLockProvider() {
return new JooqLockProvider(dslContext);
}

@Override
protected boolean useDbTime() {
return true;
}
}


Loading

0 comments on commit 836d96b

Please sign in to comment.