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

com.wizarius.orm.database.handlers.WritableHandlers Maven / Gradle / Ivy

There is a newer version: 0.0.27.6
Show newest version
package com.wizarius.orm.database.handlers;

import com.wizarius.orm.database.entityreader.DBParsedField;
import com.wizarius.orm.database.entityreader.DBSupportedTypes;

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

/**
 * Created by Vladyslav Shyshkin on 29.12.2017.
 * Hash map where key its type of object and value its handler
 */
public class WritableHandlers extends HashMap> {
    /**
     * Constructs an empty HashMap with the default initial capacity
     * (16) and the default load factor (0.75).
     */
    public WritableHandlers() {
        initializeDefaults();
    }

    public void initializeDefaults() {
        this.put(DBSupportedTypes.SHORT, new WritableHandler() {
            @Override
            public void set(Field field, Object entity, DBParsedField parsedField, int parameterIndex, PreparedStatement preparedStatement) throws Exception {
                short value = field.getShort(entity);
                set(value, parameterIndex, preparedStatement);
            }

            @Override
            public void set(Short value, int parameterIndex, PreparedStatement preparedStatement) throws SQLException {
                preparedStatement.setShort(parameterIndex, value);
            }
        });
        this.put(DBSupportedTypes.INTEGER, new WritableHandler() {
            @Override
            public void set(Field field, Object entity, DBParsedField parsedField, int parameterIndex, PreparedStatement preparedStatement) throws Exception {
                int value = field.getInt(entity);
                set(value, parameterIndex, preparedStatement);
            }

            @Override
            public void set(Integer value, int parameterIndex, PreparedStatement preparedStatement) throws SQLException {
                preparedStatement.setInt(parameterIndex, value);
            }
        });
        this.put(DBSupportedTypes.DOUBLE, new WritableHandler() {
            @Override
            public void set(Field field, Object entity, DBParsedField parsedField, int parameterIndex, PreparedStatement preparedStatement) throws Exception {
                double value = field.getDouble(entity);
                set(value, parameterIndex, preparedStatement);
            }

            @Override
            public void set(Double value, int parameterIndex, PreparedStatement preparedStatement) throws SQLException {
                preparedStatement.setDouble(parameterIndex, value);
            }
        });
        this.put(DBSupportedTypes.LONG, new WritableHandler() {
            @Override
            public void set(Field field, Object entity, DBParsedField parsedField, int parameterIndex, PreparedStatement preparedStatement) throws Exception {
                long value = field.getLong(entity);
                set(value, parameterIndex, preparedStatement);
            }

            @Override
            public void set(Long value, int parameterIndex, PreparedStatement preparedStatement) throws SQLException {
                preparedStatement.setLong(parameterIndex, value);
            }
        });
        this.put(DBSupportedTypes.FLOAT, new WritableHandler() {
            @Override
            public void set(Field field, Object entity, DBParsedField parsedField, int parameterIndex, PreparedStatement preparedStatement) throws Exception {
                float value = field.getFloat(entity);
                set(value, parameterIndex, preparedStatement);
            }

            @Override
            public void set(Float value, int parameterIndex, PreparedStatement preparedStatement) throws SQLException {
                preparedStatement.setFloat(parameterIndex, value);
            }
        });
        this.put(DBSupportedTypes.BOOLEAN, new WritableHandler() {
            @Override
            public void set(Field field, Object entity, DBParsedField parsedField, int parameterIndex, PreparedStatement preparedStatement) throws Exception {
                boolean value = field.getBoolean(entity);
                set(value, parameterIndex, preparedStatement);
            }

            @Override
            public void set(Boolean value, int parameterIndex, PreparedStatement preparedStatement) throws SQLException {
                preparedStatement.setBoolean(parameterIndex, value);
            }
        });
        this.put(DBSupportedTypes.BIGDECIMAL, new WritableHandler() {
            @Override
            public void set(Field field, Object entity, DBParsedField parsedField, int parameterIndex, PreparedStatement preparedStatement) throws Exception {
                BigDecimal value = (BigDecimal) field.get(entity);
                set(value, parameterIndex, preparedStatement);
            }

            @Override
            public void set(BigDecimal value, int parameterIndex, PreparedStatement preparedStatement) throws SQLException {
                preparedStatement.setBigDecimal(parameterIndex, value);
            }
        });
        this.put(DBSupportedTypes.STRING, new WritableHandler() {
            @Override
            public void set(Field field, Object entity, DBParsedField parsedField, int parameterIndex, PreparedStatement preparedStatement) throws Exception {
                String value = (String) field.get(entity);
                set(value, parameterIndex, preparedStatement);
            }

            @Override
            public void set(String value, int parameterIndex, PreparedStatement preparedStatement) throws SQLException {
                if (value == null) {
                    preparedStatement.setNull(parameterIndex, Types.VARCHAR);
                    return;
                }
                preparedStatement.setString(parameterIndex, value);
            }
        });
        this.put(DBSupportedTypes.BYTE, new WritableHandler() {
            @Override
            public void set(Field field, Object entity, DBParsedField parsedField, int parameterIndex, PreparedStatement preparedStatement) throws Exception {
                byte value = field.getByte(entity);
                set(value, parameterIndex, preparedStatement);
            }

            @Override
            public void set(Byte value, int parameterIndex, PreparedStatement preparedStatement) throws SQLException {
                preparedStatement.setByte(parameterIndex, value);
            }
        });
        this.put(DBSupportedTypes.BYTE_ARRAY, new WritableHandler() {
            @Override
            public void set(Field field, Object entity, DBParsedField parsedField, int parameterIndex, PreparedStatement preparedStatement) throws Exception {
                byte[] value = (byte[]) field.get(entity);
                set(value, parameterIndex, preparedStatement);
            }

            @Override
            public void set(byte[] value, int parameterIndex, PreparedStatement preparedStatement) throws SQLException {
                if (value == null) {
                    preparedStatement.setNull(parameterIndex, Types.BINARY);
                    return;
                }
                preparedStatement.setBytes(parameterIndex, value);
            }
        });
        this.put(DBSupportedTypes.CHAR, new WritableHandler() {
            @Override
            public void set(Field field, Object entity, DBParsedField parsedField, int parameterIndex, PreparedStatement preparedStatement) throws Exception {
                Character value = (Character) field.get(entity);
                set(value, parameterIndex, preparedStatement);
            }

            @Override
            public void set(Character value, int parameterIndex, PreparedStatement preparedStatement) throws SQLException {
                if (value == null || value == Character.MIN_VALUE) {
                    preparedStatement.setNull(parameterIndex, Types.CHAR);
                    return;
                }
                preparedStatement.setString(parameterIndex, String.valueOf(value));
            }
        });
        this.put(DBSupportedTypes.ENUM, new WritableHandler() {
            @Override
            public void set(Field field, Object entity, DBParsedField parsedField, int parameterIndex, PreparedStatement preparedStatement) throws Exception {
                Class c = entity.getClass();
                Field f = c.getDeclaredField(parsedField.getFieldName());
                f.setAccessible(true);
                Object result = f.get(entity);
                set(result, parameterIndex, preparedStatement);
            }

            @Override
            public void set(Object value, int parameterIndex, PreparedStatement preparedStatement) throws SQLException {
                if (value == null) {
                    preparedStatement.setNull(parameterIndex, Types.OTHER);
                    return;
                }
                preparedStatement.setObject(parameterIndex, value.toString(), Types.OTHER);
            }
        });
    }

    /**
     * Returns handler by type
     *
     * @param type database supported type
     * @return prepared handler
     * @throws UnknownHandlerException on unknown handler
     */
    public WritableHandler getHandlerByType(DBSupportedTypes type) {
        WritableHandler handler = get(type);
        if (handler == null) {
            throw new UnknownHandlerException("Unable to found handler for field where type = " + type);
        }
        return handler;
    }
}