Package x.sql2

Source Code of x.sql2.SQLRow

package x.sql2;

import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.HashMap;
import javax.persistence.Column;

public abstract class SQLRow {

    private HashMap<String, Object> data = new HashMap<String, Object>();

    public SQLRow() {
    }

    protected abstract SQLRow getInstance();

    public void set(String columnName, Object value) {
        set(new SQLColumn(columnName), value);

    }

    public Object get(String columnName) {
        return get(new SQLColumn(columnName));

    }

    public <E> E get(SQLColumn<E> col) {
        if (col.getFieldName() != null) {
            try {
                Class c = getInstance().getClass();
                String getter = col.getFieldGetter();
                for (Method m : c.getMethods()) {
                    if (m.getName().equals(getter)) {
                        return (E) m.invoke(getInstance());
                    }
                }
            } catch (Exception e) {
                e.printStackTrace();
            }

        }
        return (E) data.get(col.getColumnName());
    }

    public void set(SQLColumn col, Object value) {
        if (col.getFieldName() != null) {
            try {
                Class c = getInstance().getClass();
                String setter = col.getFieldSetter();
                for (Method m : c.getMethods()) {
                    if (m.getName().equals(setter) && m.getParameterTypes().length == 1) {
                        Class paramType = m.getParameterTypes()[0];
                        if (value == null) {
                            m.invoke(getInstance(), value);
                            return;
                        } else if (paramType == value.getClass()) {
                            m.invoke(getInstance(), value);
                            return;
                        } else if (paramType.isArray() && value.getClass().isArray() && paramType.getComponentType().equals(value.getClass().getComponentType())) {
                            m.invoke(getInstance(), value);
                            return;
                        }

                    }
                }
            } catch (Exception e) {
                if (e instanceof java.lang.IllegalArgumentException) {
                    System.out.println("Column " + col.getColumnName() + " class=" + value.getClass() + " value=" + value);
                }
                e.printStackTrace();
            }
        }
        this.data.put(col.getColumnName(), value);
    }

    public <E> E get(SQLColumn<E> col, E ifnull) {
        return (E) (get(col) != null ? get(col) : ifnull);
    }

    public Object get2(SQLColumn col, Object ifnull) {
        return (get(col) != null ? get(col) : ifnull);
    }

    public void setIfNull(SQLColumn col, Object value) {
        if (data.get(col.getColumnName()) == null) {
            data.put(col.getColumnName(), value);
        }
    }

    public boolean isFieldExist(SQLColumn col) {
        return data.get(col.getColumnName()) != null;
    }

    public void setValueByAnnotations(String columnName, Object value) throws Exception {
        SQLUtils.setValueByAnnotation(columnName, getInstance(), value);
    }

    public Object getValueByAnnotations(String columnName) throws Exception {
        return SQLUtils.getValueByAnnotation(columnName, getInstance());
    }

    public String toStringParams() {
        StringBuilder sb = new StringBuilder();
        for (Field f : this.getClass().getDeclaredFields()) {
            if (f.getAnnotation(Column.class) != null) {
                Column anot = f.getAnnotation(Column.class);
                Object value = get(new SQLColumn(anot.name(), f.getName()));
                //%[flags][width]conversion

                sb.append(String.format("%-20s %-20s %-20s", anot.name(), f.getName(), (value)));
                sb.append("\n");
            }
        }

        for (String key : data.keySet()) {
            sb.append(String.format("%-20s %-20s %-20s", key, key, (data.get(key))));
            sb.append("\n");
        }

        return sb.toString();
    }
}
TOP

Related Classes of x.sql2.SQLRow

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.