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

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

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

import com.wizarius.orm.database.DBSupportedTypes;

import java.math.BigDecimal;
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 ReadableHandlers extends HashMap {
    /**
     * Initialize default handlers
     */
    public void initializeDefaults() {
        //no floating point
        this.put(DBSupportedTypes.BYTE, (field, instance, resultSet, resultSetFieldName, parsedField) -> {
            field.setByte(instance, resultSet.getByte(resultSetFieldName));
        });
        this.put(DBSupportedTypes.SHORT, (field, instance, resultSet, resultSetFieldName, parsedField) -> {
            field.setShort(instance, resultSet.getShort(resultSetFieldName));
        });
        this.put(DBSupportedTypes.INTEGER, (field, instance, resultSet, resultSetFieldName, parsedField) -> {
            field.setInt(instance, resultSet.getInt(resultSetFieldName));
        });
        this.put(DBSupportedTypes.LONG, (field, instance, rs, resultSetFieldName, parsedField) -> {
            field.setLong(instance, rs.getLong(resultSetFieldName));
        });
        // floating point
        this.put(DBSupportedTypes.FLOAT, (field, instance, resultSet, resultSetFieldName, parsedField) -> {
            field.setFloat(instance, resultSet.getFloat(resultSetFieldName));
        });
        this.put(DBSupportedTypes.DOUBLE, (field, instance, rs, resultSetFieldName, parsedField) -> {
            field.set(instance, rs.getDouble(resultSetFieldName));
        });
        this.put(DBSupportedTypes.BIGDECIMAL, (field, instance, resultSet, resultSetFieldName, parsedField) -> {
            BigDecimal value = resultSet.getBigDecimal(resultSetFieldName);
            field.set(instance, value);
        });
        // characters
        this.put(DBSupportedTypes.CHAR, (field, instance, rs, resultSetFieldName, parsedField) -> {
            String string = rs.getString(resultSetFieldName);
            if (string != null && string.length() != 0) {
                field.setChar(instance, string.charAt(0));
            }
        });
        this.put(DBSupportedTypes.STRING, (field, instance, rs, resultSetFieldName, parsedField) -> {
            field.set(instance, rs.getString(resultSetFieldName));
        });
        // other
        this.put(DBSupportedTypes.BOOLEAN, (field, instance, rs, resultSetFieldName, parsedField) -> {
            field.setBoolean(instance, rs.getBoolean(resultSetFieldName));
        });
        this.put(DBSupportedTypes.BYTE_ARRAY, (field, instance, rs, resultSetFieldName, parsedField) -> {
            field.set(instance, rs.getBytes(resultSetFieldName));
        });
        this.put(DBSupportedTypes.ENUM, (field, instance, rs, resultSetFieldName, parsedField) -> {
            String value = rs.getString(resultSetFieldName);
            if (value != null) {
                Enum enumValue = Enum.valueOf((Class) field.getType(), value);
                field.set(instance, enumValue);
            }
        });
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy