Package x.sql2

Source Code of x.sql2.SQLUtils

package x.sql2;

import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.sql.Timestamp;

import java.util.Arrays;
import java.util.Date;
import java.util.regex.Pattern;
import javax.persistence.Column;

public class SQLUtils {

    public static final String VALUE_DELIMITER = "|";
    public static final Pattern VALUES_PATTERN = Pattern.compile("\\|[a-zA-Z0-9_-]*\\|");
    public static final Pattern PATTERNS_PATTERN = Pattern.compile("#(\\s|[a-zA-Z0-9_-])*#");
    public static final Pattern CONSTANSTS_PATTERN = Pattern.compile("@[a-zA-Z0-9_-]*@");

    public static boolean setValueByAnnotation(String columnName, Object obj, Object value) throws IllegalArgumentException, IllegalAccessException {

        Method setter = getSetterMethod(columnName, obj.getClass());

        if (setter != null) {
            try {
                setter.invoke(obj, value);
            } catch (InvocationTargetException e) {
                return false;
            }
            return true;
        }

        return false;
    }

    public static Object getValueByAnnotation(String columnName, Object fromObj) throws IllegalArgumentException, IllegalAccessException {
        try {
            Method m = getGetterMethod(columnName, fromObj.getClass());
            if (m == null) {
                return null;
            }
            return m.invoke(fromObj);
        } catch (InvocationTargetException ex) {
            ex.printStackTrace();
            return null;
        }


    }

    public static Method getSetterMethod(String columnName, Class c) {

        for (Field f : c.getDeclaredFields()) {
            Column a = f.getAnnotation(Column.class);
            if (a == null) {
                continue;
            }

            if (a.name().equals(columnName)) {
                //proveriaem esli li setter
                String ff = f.getName();
                ff = ff.substring(0, 1).toUpperCase() + ff.substring(1);
                Method setter = null;
                try {
                    setter = c.getMethod("set" + ff, f.getType());
                } catch (Exception e) {
                }

                return setter;
            }


        }
        return null;
    }

    public static Method getGetterMethod(String columnName, Class c) {
        for (Field f : c.getDeclaredFields()) {
            Column a = f.getAnnotation(Column.class);
            if (a == null) {
                continue;
            }

            if (a.name().equals(columnName)) {
                //proveriaem esli li setter
                String ff = f.getName();
                ff = ff.substring(0, 1).toUpperCase() + ff.substring(1);
                Method getter = null;
                try {
                    getter = c.getMethod("get" + ff);
                    if (getter.getReturnType() == f.getType()) {
                        return getter;
                    }
                } catch (Exception e) {
                    return null;
                }
            }
        }
        return null;
    }

    public static boolean isGetter(Method method) {
        if (!method.getName().startsWith("get")) {
            return false;
        }
        if (method.getParameterTypes().length != 0) {
            return false;
        }
        if (void.class.equals(method.getReturnType())) {
            return false;
        }
        return true;
    }

    public static boolean isSetter(Method method) {
        if (!method.getName().startsWith("set")) {
            return false;
        }
        if (method.getParameterTypes().length != 1) {
            return false;
        }
        return true;
    }

    public static String formatColumnValue(Object o) {
        String colValue = "";
        if (o == null) {
            colValue = "null";
        } else if (o.getClass().isArray()) {
            if (!o.getClass().isPrimitive()) {
                colValue = "'" + Arrays.toString((Object[]) o).replace("[", "{").replace("]", "}") + "'";
            } else {

               
            }
        } else if (o instanceof Date) {
            Timestamp ts = new Timestamp(((Date) o).getTime());
            colValue = "'" + ts.toString() + "'";
        } else if (o instanceof Integer || o instanceof Double) {
            colValue = o.toString();
        } else {
            colValue = "'" + String.valueOf(o).replace("'", "''") + "'";
        }
        return colValue;
    }
}
TOP

Related Classes of x.sql2.SQLUtils

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.