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

com.smallaswater.easysql.mysql.data.SqlData Maven / Gradle / Ivy

There is a newer version: 3.0.7
Show newest version
package com.smallaswater.easysql.mysql.data;


import java.util.LinkedHashMap;
import java.util.LinkedList;
import java.util.List;

/**
 * @author SmallasWater
 */
public class SqlData {

    private final LinkedHashMap data = new LinkedHashMap<>();

    public SqlData() {
    }

    public SqlData(String column, Object object) {
        data.put(column.toLowerCase(), object);
    }

    public LinkedHashMap getData() {
        return data;
    }

    /**
     * 添加数据
     *
     * @param column 字段名
     * @param object 数据
     */
    public SqlData put(String column, Object object) {
        data.put(column.toLowerCase(), object);
        return this;
    }



    /**
     * 获取数据
     *
     * @param column 字段名
     * @param     数据类型
     */
    public  Object get(String column, T defaultValue) {
        return data.getOrDefault(column.toLowerCase(), defaultValue);
    }

    public int getInt(String key) {
        return this.getInt(key, 0);
    }

    public int getInt(String key, int defaultValue) {
        return Integer.parseInt(this.get(key, defaultValue).toString());
    }

    /**
     * 这个似乎没啥用
     * */
    @Deprecated
    public boolean isInt(String key) {
        Object val = data.get(key);
        return val instanceof Integer;
    }


    public double getDouble(String key) {
        return this.getDouble(key, 0.0D);
    }

    public double getDouble(String key, double defaultValue) {
        return Double.parseDouble(this.get(key, defaultValue).toString());
    }

    /**
     * 这个似乎没啥用
     * */
    @Deprecated
    public boolean isDouble(String key) {
        Object val = data.get(key);
        return val instanceof Double;
    }

    public String getString(String key) {
        return this.getString(key, "");
    }

    public String getString(String key, String defaultValue) {
        Object result = this.get(key, defaultValue);
        return String.valueOf(result);
    }

    /**
     * 这个似乎没啥用
     * */
    @Deprecated
    public boolean isString(String key) {
        Object val = data.get(key);
        return val instanceof String;
    }

    public boolean getBoolean(String key) {
        return this.getBoolean(key, false);
    }

    public boolean getBoolean(String key, boolean defaultValue) {
        return Boolean.parseBoolean(this.get(key, defaultValue).toString());
    }

    /**
     * 这个似乎没啥用
     * */
    @Deprecated
    public boolean isBoolean(String key) {
        Object val = data.get(key);
        return val instanceof Boolean;
    }


    /**
     * 获取所有字段名
     *
     * @return 字段名
     */
    public List getColumns() {
        return new LinkedList<>(data.keySet());
    }

    public String getColumn() {
        return getColumn(0);
    }

    public Object getValue() {
        return getValue(0);
    }

    public String getColumn(int index) {
        LinkedList names = new LinkedList<>(data.keySet());
        if (names.size() > index) {
            return names.get(index);
        }
        return null;
    }


    /**
     * 根据索引位置获取返回的数据
     */
    public Object getValue(int index) {
        LinkedList names = new LinkedList<>(data.values());
        if (names.size() > index) {
            return names.get(index);
        }
        return null;
    }


    public List getObjects() {
        return new LinkedList<>(data.values());
    }

    /**
     * 将获取的所有字段转换为字符串
     */
    public String getColumnToString() {
        return getDefaultToString(getColumns(), false);
    }

    /**
     * 将获取的所有数值转换为字符串
     */
    public String getObjectToString() {
        return getDefaultToString(getObjects(), true);
    }

    private String getDefaultToString(List objects, boolean key) {
        StringBuilder builder = new StringBuilder();
        for (Object s : objects) {
            if (key) {
                builder.append("'").append(s).append("'").append(",");
            } else {
                builder.append(s).append(",");
            }

        }
        String str = builder.toString();
        return str.substring(0, str.length() - 1);
    }

    @Override
    public String toString() {
        StringBuilder builder = new StringBuilder("[");

        for (String name : data.keySet()) {
            Object o = data.get(name);
            if (o == null) {
                o = "null";
            }
            builder.append("column: ").append(name).append(" value: ").append(o);
        }
        builder.append("]");
        return builder.toString();
    }
}