Skip to content

Commit 4c59d7a

Browse files
committed
Added Default annotation.
1 parent 926c357 commit 4c59d7a

File tree

5 files changed

+31
-13
lines changed

5 files changed

+31
-13
lines changed

.github/workflows/post-push.yml

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,6 @@ jobs:
4545
gpg_passphrase: ${{ secrets.gpg_passphrase }}
4646
run: |
4747
mkdir -p ~/.m2
48-
export GPG_TTY=$(tty)
4948
echo "${gpg_private_key}" | gpg --batch --import
5049
echo "<settings><servers><server><id>github</id><username>CollinAlpert</username><password>${github_token}</password></server></servers></settings>" > ~/.m2/settings.xml
5150
mvn clean deploy -B -e -Dmaven.wagon.http.pool=false -DaltDeploymentRepository=github::default::https://maven.pkg.github.com/CollinAlpert/Java2DB -Dgpg.passphrase=${gpg_passphrase}
@@ -71,7 +70,7 @@ jobs:
7170

7271
- name: Create Release
7372
id: create_release
74-
uses: actions/create-release@v1.0.0
73+
uses: actions/create-release@latest
7574
env:
7675
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
7776
with:

README.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ Include the Maven artifact:
2020
<dependency>
2121
<groupId>com.github.collinalpert</groupId>
2222
<artifactId>java2db</artifactId>
23-
<version>5.2.1</version>
23+
<version>5.3.0</version>
2424
</dependency>
2525
```
2626
Or include the [JAR](https://github.com/CollinAlpert/Java2DB/releases/latest) in your project.
@@ -213,7 +213,8 @@ public class Person extends BaseEntity {
213213
### Default database values
214214
If you would like to use the database-set default value for POJO fields which are null when creating or modifying data on the database, you need to annotate the specific field with the ```DefaultIfNull``` annotation.
215215
You then have the additional option to specify if this behavior should occur on create or update statements, or both.
216-
When the annotation is specified, per default the default database value is used on create statements, but not on update ones.
216+
When the annotation is specified, per default the default database value is used on create statements, but not on update ones.\
217+
If you __always__ want Java2DB to use your database-default value, regardless if `null` or not, simply use the `Default` annotation.
217218

218219
### Column name deviations
219220
To be able to target any column naming conventions, it is possible to explicitly tell Java2DB which table column a POJO field targets with the `@ColumnName` attribute. Simply apply the attribute to a field.

pom.xml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
<groupId>com.github.collinalpert</groupId>
88
<artifactId>java2db</artifactId>
9-
<version>5.2.1</version>
9+
<version>5.3.0</version>
1010
<packaging>jar</packaging>
1111

1212
<name>Java2DB</name>
@@ -68,13 +68,13 @@
6868
<dependency>
6969
<groupId>mysql</groupId>
7070
<artifactId>mysql-connector-java</artifactId>
71-
<version>8.0.18</version>
71+
<version>8.0.19</version>
7272
</dependency>
7373

7474
<dependency>
7575
<groupId>org.junit.jupiter</groupId>
7676
<artifactId>junit-jupiter-api</artifactId>
77-
<version>5.5.2</version>
77+
<version>5.6.0</version>
7878
<scope>test</scope>
7979
</dependency>
8080
</dependencies>
@@ -96,7 +96,7 @@
9696
<plugin>
9797
<groupId>org.apache.maven.plugins</groupId>
9898
<artifactId>maven-source-plugin</artifactId>
99-
<version>3.2.0</version>
99+
<version>3.2.1</version>
100100
<executions>
101101
<execution>
102102
<id>attach-sources</id>
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package com.github.collinalpert.java2db.annotations;
2+
3+
import java.lang.annotation.ElementType;
4+
import java.lang.annotation.Retention;
5+
import java.lang.annotation.RetentionPolicy;
6+
import java.lang.annotation.Target;
7+
8+
/**
9+
* This annotation tells Java2DB to always use the database-default for a column on create or update. Not to be confused with the {@link DefaultIfNull} annotation.
10+
*
11+
* @author Collin Alpert
12+
* @see DefaultIfNull
13+
*/
14+
@Target(ElementType.FIELD)
15+
@Retention(RetentionPolicy.RUNTIME)
16+
public @interface Default {
17+
}

src/main/java/com/github/collinalpert/java2db/services/BaseService.java

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.github.collinalpert.java2db.services;
22

3+
import com.github.collinalpert.java2db.annotations.Default;
34
import com.github.collinalpert.java2db.annotations.DefaultIfNull;
45
import com.github.collinalpert.java2db.database.DBConnection;
56
import com.github.collinalpert.java2db.entities.BaseEntity;
@@ -94,7 +95,7 @@ public long create(E instance) throws SQLException {
9495
var joiner = new StringJoiner(", ", "(", ");");
9596
fieldModule.getEntityFields(instance.getClass(), BaseEntity.class).forEach(field -> {
9697
field.setAccessible(true);
97-
if (getFieldValue(field, instance) == null && annotationModule.hasAnnotation(field, DefaultIfNull.class, DefaultIfNull::onCreate)) {
98+
if (annotationModule.hasAnnotation(field, Default.class) || (getFieldValue(field, instance) == null && annotationModule.hasAnnotation(field, DefaultIfNull.class, DefaultIfNull::onCreate))) {
9899
joiner.add("default");
99100
return;
100101
}
@@ -148,7 +149,7 @@ public void create(List<E> instances) throws SQLException {
148149
for (var entityField : entityFields) {
149150
entityField.setAccessible(true);
150151
var instance = instances.get(i);
151-
if (getFieldValue(entityField, instance) == null && annotationModule.hasAnnotation(entityField, DefaultIfNull.class, DefaultIfNull::onCreate)) {
152+
if (annotationModule.hasAnnotation(entityField, Default.class) || (getFieldValue(entityField, instance) == null && annotationModule.hasAnnotation(entityField, DefaultIfNull.class, DefaultIfNull::onCreate))) {
152153
joiner.add("default");
153154
return;
154155
}
@@ -561,7 +562,7 @@ private String updateQuery(E instance) {
561562
var fieldJoiner = new StringJoiner(", ");
562563
fieldModule.getEntityFields(instance.getClass(), BaseEntity.class).forEach(field -> {
563564
field.setAccessible(true);
564-
var sqlValue = getFieldValue(field, instance) == null && annotationModule.hasAnnotation(field, DefaultIfNull.class, DefaultIfNull::onUpdate) ? "default" : getSqlValue(field, instance);
565+
var sqlValue = annotationModule.hasAnnotation(field, Default.class) || (getFieldValue(field, instance) == null && annotationModule.hasAnnotation(field, DefaultIfNull.class, DefaultIfNull::onUpdate)) ? "default" : getSqlValue(field, instance);
565566
fieldJoiner.add(String.format("`%s` = %s", tableModule.getColumnName(field), sqlValue));
566567
});
567568

@@ -589,7 +590,7 @@ public <R> void update(long entityId, SqlFunction<E, R> column, SqlFunction<E, R
589590

590591
/**
591592
* Updates a specific column for a single record in a table.
592-
* This method is not affected by the {@link DefaultIfNull} annotation.
593+
* This method is not affected by the {@link DefaultIfNull} or the {@link Default} annotation.
593594
*
594595
* @param entityId The id of the record.
595596
* @param column The column to update.
@@ -604,7 +605,7 @@ public <R> void update(long entityId, SqlFunction<E, R> column, R newValue) thro
604605

605606
/**
606607
* Updates a specific column for records matching a condition in a table.
607-
* This method is not affected by the {@link DefaultIfNull} annotation.
608+
* This method is not affected by the {@link DefaultIfNull} or the {@link Default} annotation.
608609
*
609610
* @param condition The condition to update the column by.
610611
* @param column The column to update.

0 commit comments

Comments
 (0)