Skip to content

Commit

Permalink
Added Default annotation.
Browse files Browse the repository at this point in the history
  • Loading branch information
CollinAlpert committed Mar 7, 2020
1 parent 926c357 commit 4c59d7a
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 13 deletions.
3 changes: 1 addition & 2 deletions .github/workflows/post-push.yml
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,6 @@ jobs:
gpg_passphrase: ${{ secrets.gpg_passphrase }}
run: |
mkdir -p ~/.m2
export GPG_TTY=$(tty)
echo "${gpg_private_key}" | gpg --batch --import
echo "<settings><servers><server><id>github</id><username>CollinAlpert</username><password>${github_token}</password></server></servers></settings>" > ~/.m2/settings.xml
mvn clean deploy -B -e -Dmaven.wagon.http.pool=false -DaltDeploymentRepository=github::default::https://maven.pkg.github.com/CollinAlpert/Java2DB -Dgpg.passphrase=${gpg_passphrase}
Expand All @@ -71,7 +70,7 @@ jobs:

- name: Create Release
id: create_release
uses: actions/create-release@v1.0.0
uses: actions/create-release@latest
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
with:
Expand Down
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ Include the Maven artifact:
<dependency>
<groupId>com.github.collinalpert</groupId>
<artifactId>java2db</artifactId>
<version>5.2.1</version>
<version>5.3.0</version>
</dependency>
```
Or include the [JAR](https://github.com/CollinAlpert/Java2DB/releases/latest) in your project.
Expand Down Expand Up @@ -213,7 +213,8 @@ public class Person extends BaseEntity {
### Default database values
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.
You then have the additional option to specify if this behavior should occur on create or update statements, or both.
When the annotation is specified, per default the default database value is used on create statements, but not on update ones.
When the annotation is specified, per default the default database value is used on create statements, but not on update ones.\
If you __always__ want Java2DB to use your database-default value, regardless if `null` or not, simply use the `Default` annotation.

### Column name deviations
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.
Expand Down
8 changes: 4 additions & 4 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

<groupId>com.github.collinalpert</groupId>
<artifactId>java2db</artifactId>
<version>5.2.1</version>
<version>5.3.0</version>
<packaging>jar</packaging>

<name>Java2DB</name>
Expand Down Expand Up @@ -68,13 +68,13 @@
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.18</version>
<version>8.0.19</version>
</dependency>

<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<version>5.5.2</version>
<version>5.6.0</version>
<scope>test</scope>
</dependency>
</dependencies>
Expand All @@ -96,7 +96,7 @@
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-source-plugin</artifactId>
<version>3.2.0</version>
<version>3.2.1</version>
<executions>
<execution>
<id>attach-sources</id>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package com.github.collinalpert.java2db.annotations;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

/**
* 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.
*
* @author Collin Alpert
* @see DefaultIfNull
*/
@Target(ElementType.FIELD)
@Retention(RetentionPolicy.RUNTIME)
public @interface Default {
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.github.collinalpert.java2db.services;

import com.github.collinalpert.java2db.annotations.Default;
import com.github.collinalpert.java2db.annotations.DefaultIfNull;
import com.github.collinalpert.java2db.database.DBConnection;
import com.github.collinalpert.java2db.entities.BaseEntity;
Expand Down Expand Up @@ -94,7 +95,7 @@ public long create(E instance) throws SQLException {
var joiner = new StringJoiner(", ", "(", ");");
fieldModule.getEntityFields(instance.getClass(), BaseEntity.class).forEach(field -> {
field.setAccessible(true);
if (getFieldValue(field, instance) == null && annotationModule.hasAnnotation(field, DefaultIfNull.class, DefaultIfNull::onCreate)) {
if (annotationModule.hasAnnotation(field, Default.class) || (getFieldValue(field, instance) == null && annotationModule.hasAnnotation(field, DefaultIfNull.class, DefaultIfNull::onCreate))) {
joiner.add("default");
return;
}
Expand Down Expand Up @@ -148,7 +149,7 @@ public void create(List<E> instances) throws SQLException {
for (var entityField : entityFields) {
entityField.setAccessible(true);
var instance = instances.get(i);
if (getFieldValue(entityField, instance) == null && annotationModule.hasAnnotation(entityField, DefaultIfNull.class, DefaultIfNull::onCreate)) {
if (annotationModule.hasAnnotation(entityField, Default.class) || (getFieldValue(entityField, instance) == null && annotationModule.hasAnnotation(entityField, DefaultIfNull.class, DefaultIfNull::onCreate))) {
joiner.add("default");
return;
}
Expand Down Expand Up @@ -561,7 +562,7 @@ private String updateQuery(E instance) {
var fieldJoiner = new StringJoiner(", ");
fieldModule.getEntityFields(instance.getClass(), BaseEntity.class).forEach(field -> {
field.setAccessible(true);
var sqlValue = getFieldValue(field, instance) == null && annotationModule.hasAnnotation(field, DefaultIfNull.class, DefaultIfNull::onUpdate) ? "default" : getSqlValue(field, instance);
var sqlValue = annotationModule.hasAnnotation(field, Default.class) || (getFieldValue(field, instance) == null && annotationModule.hasAnnotation(field, DefaultIfNull.class, DefaultIfNull::onUpdate)) ? "default" : getSqlValue(field, instance);
fieldJoiner.add(String.format("`%s` = %s", tableModule.getColumnName(field), sqlValue));
});

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

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

/**
* Updates a specific column for records matching a condition in a table.
* This method is not affected by the {@link DefaultIfNull} annotation.
* This method is not affected by the {@link DefaultIfNull} or the {@link Default} annotation.
*
* @param condition The condition to update the column by.
* @param column The column to update.
Expand Down

0 comments on commit 4c59d7a

Please sign in to comment.