-
Notifications
You must be signed in to change notification settings - Fork 31
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
0e37b15
commit ed231d2
Showing
3 changed files
with
126 additions
and
5 deletions.
There are no files selected for viewing
113 changes: 110 additions & 3 deletions
113
horreum-backend/src/main/java/io/hyperfoil/tools/horreum/hibernate/IntArrayType.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,117 @@ | ||
package io.hyperfoil.tools.horreum.hibernate; | ||
|
||
|
||
import org.hibernate.type.BasicTypeReference; | ||
import org.hibernate.HibernateException; | ||
import org.hibernate.engine.spi.SharedSessionContractImplementor; | ||
import org.hibernate.type.CustomType; | ||
import org.hibernate.type.SqlTypes; | ||
import org.hibernate.type.spi.TypeConfiguration; | ||
import org.hibernate.usertype.UserType; | ||
|
||
public class IntArrayType { | ||
import java.io.Serializable; | ||
import java.sql.Array; | ||
import java.sql.PreparedStatement; | ||
import java.sql.ResultSet; | ||
import java.sql.SQLException; | ||
import java.sql.Types; | ||
import java.util.Arrays; | ||
import java.util.Objects; | ||
import java.util.stream.IntStream; | ||
|
||
public static final BasicTypeReference<int[]> INT_ARRAY = new BasicTypeReference("int-array", int[].class, 666); | ||
import static java.lang.String.format; | ||
|
||
public class IntArrayType implements UserType<int[]> { | ||
|
||
public static final CustomType INSTANCE = new CustomType<>(new IntArrayType(), new TypeConfiguration()); | ||
@Override | ||
public int getSqlType() { | ||
return SqlTypes.ARRAY; | ||
} | ||
|
||
@Override | ||
public Class<int[]> returnedClass() { | ||
return int[].class; | ||
} | ||
|
||
@Override | ||
public boolean equals(int[] x, int[] y) { | ||
return Arrays.equals(x, y); | ||
} | ||
|
||
@Override | ||
public int hashCode(int[] x) { | ||
return Objects.hashCode(x); | ||
} | ||
|
||
@Override | ||
public int[] nullSafeGet(ResultSet rs, int position, SharedSessionContractImplementor session, Object owner) | ||
throws SQLException { | ||
if(rs.wasNull()) | ||
return null; | ||
Array array = rs.getArray(position); | ||
if (array == null) { | ||
return null; | ||
} | ||
try { | ||
return (int[]) array.getArray(); | ||
} catch (final Exception ex) { | ||
throw new RuntimeException("Failed to convert ResultSet to int[]: " + ex.getMessage(), ex); | ||
} | ||
} | ||
|
||
@Override | ||
public void nullSafeSet(PreparedStatement ps, int[] value, int index, SharedSessionContractImplementor session) | ||
throws SQLException { | ||
if (value == null) { | ||
ps.setNull(index, Types.ARRAY); | ||
return; | ||
} | ||
try { | ||
Integer[] castArray = IntStream.of(value).boxed().toArray( Integer[]::new ); | ||
Array array = ps.getConnection().createArrayOf("integer", castArray); | ||
ps.setArray(index, array); | ||
} catch (final Exception ex) { | ||
throw new RuntimeException(format("Failed to convert JSON to String: %s", ex.getMessage()), ex); | ||
} | ||
} | ||
|
||
@Override | ||
public int[] deepCopy(int[] value) throws HibernateException { | ||
if (value == null) { | ||
return null; | ||
} | ||
return Arrays.copyOf(value, value.length); | ||
} | ||
|
||
@Override | ||
public boolean isMutable() { | ||
return true; | ||
} | ||
|
||
@Override | ||
public Serializable disassemble(int[] value) throws HibernateException { | ||
return (Serializable) this.deepCopy(value); | ||
} | ||
|
||
@Override | ||
public int[] assemble(Serializable cached, Object owner) throws HibernateException { | ||
String stringArray = cached.toString().replaceAll("[\\[\\]]", ""); | ||
String[] tokens = stringArray.split(","); | ||
|
||
int length = tokens.length; | ||
int[] array = new int[length]; | ||
for (int i = 0; i < tokens.length; i++) { | ||
array[i] = Integer.valueOf(tokens[i]); | ||
} | ||
return array; | ||
} | ||
|
||
@Override | ||
public int[] replace(int[] original, int[] target, Object owner) throws HibernateException { | ||
return original; | ||
} | ||
|
||
public String getName() { | ||
return "int-array"; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters