All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.wizarius.orm.database.postgres.helpers.PostgresWritableHelper Maven / Gradle / Ivy

There is a newer version: 0.0.27.3
Show newest version
package com.wizarius.orm.database.postgres.helpers;

import com.wizarius.orm.database.DBParsedField;
import com.wizarius.orm.database.DBSupportedTypes;
import com.wizarius.orm.database.exceptions.DBException;
import com.wizarius.orm.database.handlers.WritableHandler;
import com.wizarius.orm.database.handlers.WritableHandlers;
import com.wizarius.orm.database.interfaces.DBEntity;
import com.wizarius.orm.database.postgres.annotations.CustomType;
import com.wizarius.orm.database.postgres.annotations.CustomTypeField;

import java.lang.reflect.Field;
import java.math.BigDecimal;
import java.sql.Array;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.sql.Types;
import java.util.Arrays;

/**
 * Created by Vladyslav Shyshkin on 12.11.17.
 * 

* TypeInfoCache - supported types in postgres */ public class PostgresWritableHelper { /** * Get list of writable handlers * * @return prepared type handlers */ public static WritableHandlers getWritableHandlers() { WritableHandlers handlers = new WritableHandlers(); handlers.put(DBSupportedTypes.CUSTOM_TYPE, getCustomTypeHandler()); handlers.put(DBSupportedTypes.SHORT_ARRAY, getShortArrayHandler()); handlers.put(DBSupportedTypes.INTEGER_ARRAY, getIntegerArrayHandler()); handlers.put(DBSupportedTypes.LONG_ARRAY, getLongArrayHandler()); handlers.put(DBSupportedTypes.FLOAT_ARRAY, getFloatArrayHandler()); handlers.put(DBSupportedTypes.DOUBLE_ARRAY, getDoubleArrayHandler()); handlers.put(DBSupportedTypes.STRING_ARRAY, getStringArrayHandler()); handlers.put(DBSupportedTypes.CHAR_ARRAY, getChartArrayHandler()); handlers.put(DBSupportedTypes.ENUM_ARRAY, getEnumArrayHandler()); handlers.put(DBSupportedTypes.BOOLEAN_ARRAY, getBooleanArrayHandler()); handlers.put(DBSupportedTypes.BIGDECIMAL_ARRAY, getBigDecimalArrayHandler()); return handlers; } /** * Get custom type handler * * @return prepared type handler */ private static WritableHandler getCustomTypeHandler() { return new WritableHandler() { @Override public void set(Field field, DBEntity entity, DBParsedField parsedField, int parameterIndex, PreparedStatement preparedStatement) throws Exception { String value = generateCustomTypeUpdateString(field, entity); set(value, parameterIndex, preparedStatement); } @Override public void set(Object value, int parameterIndex, PreparedStatement preparedStatement) throws SQLException { if (value == null) { preparedStatement.setNull(parameterIndex, Types.OTHER); } else { preparedStatement.setObject( parameterIndex, value, Types.OTHER ); } } }; } /** * Get integer array handler * * @return writable int handler */ private static WritableHandler getIntegerArrayHandler() { return new WritableHandler() { @Override public void set(Field field, DBEntity entity, DBParsedField parsedField, int parameterIndex, PreparedStatement preparedStatement) throws Exception { Object obj = field.get(entity); if (obj == null) { set(null, parameterIndex, preparedStatement); } else { int[] value = (int[]) field.get(entity); set(value, parameterIndex, preparedStatement); } } @Override public void set(int[] value, int parameterIndex, PreparedStatement preparedStatement) throws SQLException { if (value == null) { preparedStatement.setNull(parameterIndex, Types.ARRAY); } else { Integer[] integers = Arrays.stream(value).boxed().toArray(Integer[]::new); Array array = preparedStatement.getConnection().createArrayOf("integer", integers); preparedStatement.setArray(parameterIndex, array); } } }; } /** * Get short array handler * * @return writable short handler */ private static WritableHandler getShortArrayHandler() { return new WritableHandler() { @Override public void set(Field field, DBEntity entity, DBParsedField parsedField, int parameterIndex, PreparedStatement preparedStatement) throws Exception { Object obj = field.get(entity); if (obj == null) { set(null, parameterIndex, preparedStatement); } else { short[] value = (short[]) field.get(entity); set(value, parameterIndex, preparedStatement); } } @Override public void set(short[] value, int parameterIndex, PreparedStatement preparedStatement) throws SQLException { if (value == null) { preparedStatement.setNull(parameterIndex, Types.ARRAY); } else { Short[] shorts = new Short[value.length]; for (int i = 0; i < value.length; i++) { shorts[i] = value[i]; } Array array = preparedStatement.getConnection().createArrayOf("smallint", shorts); preparedStatement.setArray(parameterIndex, array); } } }; } /** * Get long array handler * * @return writable long handler */ private static WritableHandler getLongArrayHandler() { return new WritableHandler() { @Override public void set(Field field, DBEntity entity, DBParsedField parsedField, int parameterIndex, PreparedStatement preparedStatement) throws Exception { Object obj = field.get(entity); if (obj == null) { set(null, parameterIndex, preparedStatement); } else { long[] value = (long[]) field.get(entity); set(value, parameterIndex, preparedStatement); } } @Override public void set(long[] value, int parameterIndex, PreparedStatement preparedStatement) throws SQLException { if (value == null) { preparedStatement.setNull(parameterIndex, Types.ARRAY); } else { Long[] longs = Arrays.stream(value).boxed().toArray(Long[]::new); Array array = preparedStatement.getConnection().createArrayOf("BIGINT", longs); preparedStatement.setArray(parameterIndex, array); } } }; } /** * Get long array handler * * @return writable float handler */ private static WritableHandler getFloatArrayHandler() { return new WritableHandler() { @Override public void set(Field field, DBEntity entity, DBParsedField parsedField, int parameterIndex, PreparedStatement preparedStatement) throws Exception { Object obj = field.get(entity); if (obj == null) { set(null, parameterIndex, preparedStatement); } else { float[] value = (float[]) field.get(entity); set(value, parameterIndex, preparedStatement); } } @Override public void set(float[] value, int parameterIndex, PreparedStatement preparedStatement) throws SQLException { if (value == null) { preparedStatement.setNull(parameterIndex, Types.ARRAY); } else { Float[] floats = new Float[value.length]; for (int i = 0; i < value.length; i++) { floats[i] = value[i]; } Array array = preparedStatement.getConnection().createArrayOf("float4", floats); preparedStatement.setArray(parameterIndex, array); } } }; } /** * Get double array handler * * @return writable double handler */ private static WritableHandler getDoubleArrayHandler() { return new WritableHandler() { @Override public void set(Field field, DBEntity entity, DBParsedField parsedField, int parameterIndex, PreparedStatement preparedStatement) throws Exception { Object obj = field.get(entity); if (obj == null) { set(null, parameterIndex, preparedStatement); } else { double[] value = (double[]) field.get(entity); set(value, parameterIndex, preparedStatement); } } @Override public void set(double[] value, int parameterIndex, PreparedStatement preparedStatement) throws SQLException { if (value == null) { preparedStatement.setNull(parameterIndex, Types.ARRAY); } else { Double[] doubles = new Double[value.length]; for (int i = 0; i < value.length; i++) { doubles[i] = value[i]; } Array array = preparedStatement.getConnection().createArrayOf("float8", doubles); preparedStatement.setArray(parameterIndex, array); } } }; } /** * Get string array handler * * @return writable string handler */ private static WritableHandler getStringArrayHandler() { return new WritableHandler() { @Override public void set(Field field, DBEntity entity, DBParsedField parsedField, int parameterIndex, PreparedStatement preparedStatement) throws Exception { Object obj = field.get(entity); if (obj == null) { set(null, parameterIndex, preparedStatement); } else { String[] value = (String[]) obj; set(value, parameterIndex, preparedStatement); } } @Override public void set(String[] value, int parameterIndex, PreparedStatement preparedStatement) throws SQLException { if (value == null) { preparedStatement.setNull(parameterIndex, Types.ARRAY); } else { Array array = preparedStatement.getConnection().createArrayOf("varchar", value); preparedStatement.setArray(parameterIndex, array); } } }; } /** * Get char array handler * * @return writable char handler */ private static WritableHandler getChartArrayHandler() { return new WritableHandler() { @Override public void set(Field field, DBEntity entity, DBParsedField parsedField, int parameterIndex, PreparedStatement preparedStatement) throws Exception { Object obj = field.get(entity); if (obj == null) { set(null, parameterIndex, preparedStatement); } else { char[] value = (char[]) obj; set(value, parameterIndex, preparedStatement); } } @Override public void set(char[] value, int parameterIndex, PreparedStatement preparedStatement) throws SQLException { if (value == null) { preparedStatement.setNull(parameterIndex, Types.ARRAY); } else { Character[] chars = new Character[value.length]; for (int i = 0; i < value.length; i++) { chars[i] = value[i]; } Array array = preparedStatement.getConnection().createArrayOf("varchar", chars); preparedStatement.setArray(parameterIndex, array); } } }; } /** * Get char array handler * * @return writable char handler */ private static WritableHandler getBooleanArrayHandler() { return new WritableHandler() { @Override public void set(Field field, DBEntity entity, DBParsedField parsedField, int parameterIndex, PreparedStatement preparedStatement) throws Exception { Object obj = field.get(entity); if (obj == null) { set(null, parameterIndex, preparedStatement); } else { boolean[] value = (boolean[]) obj; set(value, parameterIndex, preparedStatement); } } @Override public void set(boolean[] value, int parameterIndex, PreparedStatement preparedStatement) throws SQLException { if (value == null) { preparedStatement.setNull(parameterIndex, Types.ARRAY); } else { Boolean[] booleans = new Boolean[value.length]; for (int i = 0; i < value.length; i++) { booleans[i] = value[i]; } Array array = preparedStatement.getConnection().createArrayOf("boolean", booleans); preparedStatement.setArray(parameterIndex, array); } } }; } /** * Get bigdecimal array handler * * @return writable bigdecimal handler */ private static WritableHandler getBigDecimalArrayHandler() { return new WritableHandler() { @Override public void set(Field field, DBEntity entity, DBParsedField parsedField, int parameterIndex, PreparedStatement preparedStatement) throws Exception { Object obj = field.get(entity); if (obj == null) { set(null, parameterIndex, preparedStatement); } else { BigDecimal[] value = (BigDecimal[]) obj; set(value, parameterIndex, preparedStatement); } } @Override public void set(BigDecimal[] value, int parameterIndex, PreparedStatement preparedStatement) throws SQLException { if (value == null) { preparedStatement.setNull(parameterIndex, Types.ARRAY); } else { Array array = preparedStatement.getConnection().createArrayOf("numeric", value); preparedStatement.setArray(parameterIndex, array); } } }; } /** * Get enum array handler * * @return writable enum handler */ private static WritableHandler getEnumArrayHandler() { return new WritableHandler() { @Override public void set(Field field, DBEntity entity, DBParsedField parsedField, int parameterIndex, PreparedStatement preparedStatement) throws Exception { Object obj = field.get(entity); if (obj == null) { set(null, parameterIndex, preparedStatement); } else { Enum[] value = (Enum[]) obj; set(value, parameterIndex, preparedStatement); } } @Override public void set(Enum[] value, int parameterIndex, PreparedStatement preparedStatement) throws SQLException { if (value == null) { preparedStatement.setNull(parameterIndex, Types.ARRAY); } else { Array array = preparedStatement.getConnection().createArrayOf("varchar", value); preparedStatement.setObject(parameterIndex, array, Types.OTHER); } } }; } /** * Generate custom type insert string value * * @param entityField source field * @param entity object entity * @return value string * @throws IllegalAccessException impossible to access * @throws DBException on incorrect field */ private static String generateCustomTypeUpdateString(Field entityField, DBEntity entity) throws IllegalAccessException, DBException { entityField.setAccessible(true); Object entityTypeObject = entityField.get(entity); Class clazz = entityField.get(entity).getClass(); if (!clazz.isAnnotationPresent(CustomType.class)) { throw new DBException("Class not marked with annotation custom type"); } StringBuilder sb = new StringBuilder(); Field[] fields = clazz.getDeclaredFields(); if (fields.length == 0) { throw new DBException("Fields size cannot be zero"); } sb.append("("); for (Field field : fields) { field.setAccessible(true); if (!field.isAnnotationPresent(CustomTypeField.class)) { throw new DBException("Field not marked with annotation custom field type"); } sb.append(field.get(entityTypeObject)).append(","); } sb.setLength(sb.length() - 1); sb.append(")"); return sb.toString(); } }





© 2015 - 2024 Weber Informatics LLC | Privacy Policy