Skip to content

Commit

Permalink
Added handling for null values in foreign keys
Browse files Browse the repository at this point in the history
  • Loading branch information
CollinAlpert committed Oct 9, 2018
1 parent f2956f9 commit 31f4210
Show file tree
Hide file tree
Showing 5 changed files with 98 additions and 7 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ First, include the Maven artifact:
<dependency>
<groupId>com.github.collinalpert</groupId>
<artifactId>java2db</artifactId>
<version>2.2.4</version>
<version>2.2.5</version>
</dependency>
```
Or include the [JAR](https://github.com/CollinAlpert/Java2DB/releases/latest) in your project. To begin using this library, you need to do two things on program start:
Expand Down
19 changes: 15 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>2.2.4</version>
<version>2.2.5</version>
<packaging>jar</packaging>

<name>Java2DB</name>
Expand Down Expand Up @@ -50,9 +50,10 @@
</distributionManagement>

<properties>
<jdk.version>10</jdk.version>
<maven.compiler.source>10</maven.compiler.source>
<maven.compiler.target>10</maven.compiler.target>
<java-version>10</java-version>
<jdk.version>${java-version}</jdk.version>
<maven.compiler.source>${java-version}</maven.compiler.source>
<maven.compiler.target>${java-version}</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>

Expand Down Expand Up @@ -84,6 +85,16 @@

<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.0</version>
<configuration>
<source>${java-version}</source>
<target>${java-version}</target>
</configuration>
</plugin>

<plugin>
<groupId>org.sonatype.plugins</groupId>
<artifactId>nexus-staging-maven-plugin</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Optional;

Expand Down Expand Up @@ -88,23 +89,46 @@ private <E extends BaseEntity> void setFields(ResultSet set, E entity) throws SQ
private <E extends BaseEntity> void setFields(ResultSet set, E entity, String identifier) throws SQLException {
var fields = Utilities.getEntityFields(entity.getClass(), true);
var tableName = Utilities.getTableName(entity.getClass());
var foreignKeyFields = new LinkedList<Field>();
for (Field field : fields) {
field.setAccessible(true);
try {
if (field.getAnnotation(ForeignKeyObject.class) != null) {
if (!BaseEntity.class.isAssignableFrom(field.getType())) {
throw new IllegalArgumentException(String.format("Type %s which is annotated as a foreign key, does not extend BaseEntity", field.getType().getSimpleName()));
}
foreignKeyFields.add(field);
var foreignKeyObject = IoC.resolve((Class<? extends BaseEntity>) field.getType());
setFields(set, foreignKeyObject, UniqueIdentifier.getIdentifier(field.getName()));
field.set(entity, foreignKeyObject);
continue;
}
var value = set.getObject((identifier == null ? tableName : identifier) + "_" + field.getName(), field.getType());
var value = set.getObject((identifier == null ? tableName : identifier) + "_" + field.getName());
if (value == null) {
continue;
}
field.set(entity, value);
} catch (IllegalAccessException e) {
e.printStackTrace();
}
}
validateEntityForNull(entity, foreignKeyFields);
}

private <E extends BaseEntity> void validateEntityForNull(E entity, List<Field> foreignKeyFields) {
foreignKeyFields.forEach(field -> {
var foreignKeyText = field.getAnnotation(ForeignKeyObject.class).value();
try {
field.setAccessible(true);
var foreignKeyField = entity.getClass().getDeclaredField(foreignKeyText);
foreignKeyField.setAccessible(true);
var value = foreignKeyField.get(entity);
if (value == null) {
field.set(entity, null);
}
} catch (NoSuchFieldException | IllegalAccessException e) {
e.printStackTrace();
}
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ private String buildQuery() {
}
builder.append(String.join(", ", fieldList)).append(" from ").append(tableName);
for (var foreignKey : foreignKeyList) {
builder.append(" inner join `").append(foreignKey.getChildTable()).append("` ").append(foreignKey.getAlias()).append(" on `").append(foreignKey.getParentClass()).append("`.").append(foreignKey.getParentForeignKey()).append(" = ").append(foreignKey.getAlias()).append(".id");
builder.append(" left join `").append(foreignKey.getChildTable()).append("` ").append(foreignKey.getAlias()).append(" on `").append(foreignKey.getParentClass()).append("`.").append(foreignKey.getParentForeignKey()).append(" = ").append(foreignKey.getAlias()).append(".id");
}
var constraints = QueryConstraints.getConstraints(this.type);
if (this.whereClause == null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@
import com.github.collinalpert.java2db.database.DBConnection;
import com.github.collinalpert.java2db.entities.BaseEntity;
import com.github.collinalpert.java2db.mappers.BaseMapper;
import com.github.collinalpert.java2db.queries.OrderTypes;
import com.github.collinalpert.java2db.queries.Query;
import com.github.collinalpert.java2db.utilities.IoC;
import com.github.collinalpert.java2db.utilities.Utilities;
import com.github.collinalpert.lambda2sql.functions.SqlFunction;
import com.github.collinalpert.lambda2sql.functions.SqlPredicate;

import java.lang.reflect.ParameterizedType;
Expand Down Expand Up @@ -152,6 +154,60 @@ public Optional<T> getById(long id) {
public List<T> getAll() {
return getMultiple(x -> true).get();
}

/**
* Gets all values from the table but limits the result.
*
* @param limit The maximum of records to return.
* @return A list with the maximum size of the parameter specified.
*/
public List<T> getAll(int limit) {
return getMultiple(x -> true).limit(limit).get();
}

/**
* Gets all values from the table and orders them in an ascending order.
*
* @param orderBy The property to order by.
* @return A list of all records ordered by a specific property in an ascending order.
*/
public List<T> getAll(SqlFunction<T, ?> orderBy) {
return getAll(orderBy, OrderTypes.ASCENDING);
}

/**
* Gets all values from the table and orders them in the specified order.
*
* @param orderBy The property to order by.
* @param sortingType The order direction. Can be either ascending or descending.
* @return A list of all records ordered by a specific property in the specified order.
*/
public List<T> getAll(SqlFunction<T, ?> orderBy, OrderTypes sortingType) {
return getMultiple(x -> true).orderBy(orderBy, sortingType).get();
}

/**
* Gets all values from the table, orders them in an ascending order and limits the result.
*
* @param orderBy The property to order by.
* @param limit The maximum records to return.
* @return A list with the maximum size of the parameter specified and in an ascending order.
*/
public List<T> getAll(SqlFunction<T, ?> orderBy, int limit) {
return getAll(orderBy, OrderTypes.ASCENDING, limit);
}

/**
* Gets all values from the table, orders them in a specific order and limits the result.
*
* @param orderBy The property to order by.
* @param sortingType The order direction. Can be either ascending or descending.
* @param limit The maximum records to return.
* @return A list with the maximum size of the parameter specified and in an ascending order.
*/
public List<T> getAll(SqlFunction<T, ?> orderBy, OrderTypes sortingType, int limit) {
return getMultiple(x -> true).orderBy(orderBy, sortingType).limit(limit).get();
}
//endregion

//region Update
Expand Down

0 comments on commit 31f4210

Please sign in to comment.