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.*;
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 = (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.SMALLINT);
                } else {
                    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 {
                Integer value = (Integer) field.get(entity);
                set(value, parameterIndex, preparedStatement);
            }

            @Override
            public void set(Integer value, int parameterIndex, PreparedStatement preparedStatement) throws SQLException {
                if (value == null) {
                    preparedStatement.setNull(parameterIndex, Types.INTEGER);
                } else {
                    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 = (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.DOUBLE);
                } else {
                    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 = (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.BIGINT);
                } else {
                    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 = (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.FLOAT);
                } else {
                    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 = (Boolean) field.get(entity);
                set(value, parameterIndex, preparedStatement);
            }

            @Override
            public void set(Boolean value, int parameterIndex, PreparedStatement preparedStatement) throws SQLException {
                if (value == null) {
                    preparedStatement.setNull(parameterIndex, Types.BOOLEAN);
                } else {
                    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 {
                if (value == null) {
                    preparedStatement.setNull(parameterIndex, Types.NUMERIC);
                } else {
                    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);
                } else {
                    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 = (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.NULL);
                } else {
                    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);
                } else {
                    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);
                } else {
                    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);
                } else {
                    preparedStatement.setObject(parameterIndex, value.toString(), Types.OTHER);
                }
            }
        });
        this.put(DBSupportedTypes.DATE, new WritableHandler() {
            @Override
            public void set(Field field, Object entity, DBParsedField parsedField, int parameterIndex, PreparedStatement preparedStatement) throws Exception {
                Date value = (Date) field.get(entity);
                set(value, parameterIndex, preparedStatement);
            }

            @Override
            public void set(Date value, int parameterIndex, PreparedStatement preparedStatement) throws SQLException {
                if (value == null) {
                    preparedStatement.setNull(parameterIndex, Types.DATE);
                } else {
                    preparedStatement.setDate(parameterIndex, value);
                }
            }
        });

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

            @Override
            public void set(Timestamp value, int parameterIndex, PreparedStatement preparedStatement) throws SQLException {
                if (value == null) {
                    preparedStatement.setNull(parameterIndex, Types.TIMESTAMP);
                } else {
                    preparedStatement.setTimestamp(parameterIndex, value);
                }
            }
        });
    }

    /**
     * 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;
    }
}