Skip to content

Commit

Permalink
upgrade jpa, a start on attributeconverter
Browse files Browse the repository at this point in the history
  • Loading branch information
ccleve committed Dec 4, 2019
1 parent 26b40cf commit 21094a7
Show file tree
Hide file tree
Showing 5 changed files with 185 additions and 86 deletions.
30 changes: 7 additions & 23 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -78,28 +78,12 @@
</execution>
</executions>
</plugin>

<!--

<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-javadoc-plugin</artifactId>
<version>3.1.0</version>
<configuration>
<javadocExecutable>${java.home}/bin/javadoc</javadocExecutable>
<additionalparam>-Xdoclint:none</additionalparam>
<additionalOptions>-Xdoclint:none</additionalOptions>
</configuration>
<executions>
<execution>
<id>attach-javadocs</id>
<goals>
<goal>jar</goal>
</goals>
</execution>
</executions>
</plugin>
-->
<!-- <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-javadoc-plugin</artifactId>
<version>3.1.0</version> <configuration> <javadocExecutable>${java.home}/bin/javadoc</javadocExecutable>
<additionalparam>-Xdoclint:none</additionalparam> <additionalOptions>-Xdoclint:none</additionalOptions>
</configuration> <executions> <execution> <id>attach-javadocs</id> <goals>
<goal>jar</goal> </goals> </execution> </executions> </plugin> -->

<plugin>
<groupId>org.apache.maven.plugins</groupId>
Expand Down Expand Up @@ -135,8 +119,8 @@

<dependency>
<groupId>javax.persistence</groupId>
<artifactId>persistence-api</artifactId>
<version>1.0.2</version>
<artifactId>javax.persistence-api</artifactId>
<version>2.2</version>
</dependency>

<dependency>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package com.dieselpoint.norm.converter;

import java.sql.Array;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import javax.persistence.AttributeConverter;
import javax.persistence.Converter;

import com.dieselpoint.norm.DbException;

@Converter
public class IntArrayToListConverter implements AttributeConverter<List<Integer>, java.sql.Array> {

@Override
public Array convertToDatabaseColumn(List<Integer> attribute) {
return new SimpleArray(java.sql.Types.INTEGER, attribute.toArray());
}

@Override
public List<Integer> convertToEntityAttribute(Array dbData) {

try {
if (dbData.getBaseType() != java.sql.Types.INTEGER) {
throw new DbException("Database is not returning an integer array");
}
int[] arr = (int[]) dbData.getArray();
List<Integer> out = new ArrayList<>();
for (int i = 0; i < arr.length; i++) {
out.add(arr[i]);
}
return out;

} catch (SQLException e) {
throw new DbException(e);
}
}

}
71 changes: 71 additions & 0 deletions src/main/java/com/dieselpoint/norm/converter/SimpleArray.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
package com.dieselpoint.norm.converter;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Map;

public class SimpleArray implements java.sql.Array {

private int baseType;
private Object [] arr;

public SimpleArray(int baseType, Object [] arr) {
this.baseType = baseType;
this.arr = arr;
}

@Override
public String getBaseTypeName() throws SQLException {
throw new UnsupportedOperationException();
}

@Override
public int getBaseType() throws SQLException {
return baseType;
}

@Override
public Object getArray() throws SQLException {
return arr;
}

@Override
public Object getArray(Map<String, Class<?>> map) throws SQLException {
throw new UnsupportedOperationException();
}

@Override
public Object getArray(long index, int count) throws SQLException {
throw new UnsupportedOperationException();
}

@Override
public Object getArray(long index, int count, Map<String, Class<?>> map) throws SQLException {
throw new UnsupportedOperationException();
}

@Override
public ResultSet getResultSet() throws SQLException {
throw new UnsupportedOperationException();
}

@Override
public ResultSet getResultSet(Map<String, Class<?>> map) throws SQLException {
throw new UnsupportedOperationException();
}

@Override
public ResultSet getResultSet(long index, int count) throws SQLException {
throw new UnsupportedOperationException();
}

@Override
public ResultSet getResultSet(long index, int count, Map<String, Class<?>> map) throws SQLException {
throw new UnsupportedOperationException();
}

@Override
public void free() throws SQLException {
}

}
2 changes: 2 additions & 0 deletions src/main/java/com/dieselpoint/norm/sqlmakers/Property.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import java.lang.reflect.Field;
import java.lang.reflect.Method;

import javax.persistence.AttributeConverter;
import javax.persistence.Column;
import javax.persistence.EnumType;

Expand All @@ -22,4 +23,5 @@ public class Property {
public EnumType enumType;
public Column columnAnnotation;
public DbSerializable serializer;
public AttributeConverter converter;
}
Loading

0 comments on commit 21094a7

Please sign in to comment.