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

io.hypersistence.utils.logging.InlineQueryLogEntryCreator Maven / Gradle / Ivy

There is a newer version: 3.8.3
Show newest version
package io.hypersistence.utils.logging;

import net.ttddyy.dsproxy.ExecutionInfo;
import net.ttddyy.dsproxy.QueryInfo;
import net.ttddyy.dsproxy.listener.logging.DefaultQueryLogEntryCreator;

import java.util.*;

/**
 * @author Vlad Mihalcea
 */
public class InlineQueryLogEntryCreator extends DefaultQueryLogEntryCreator {
    @Override
    protected void writeParamsEntry(StringBuilder sb, ExecutionInfo execInfo, List queryInfoList) {
        sb.append("Params:[");
        for (QueryInfo queryInfo : queryInfoList) {
            boolean firstArg = true;
            for (Map paramMap : queryInfo.getQueryArgsList()) {

                if (!firstArg) {
                    sb.append(", ");
                } else {
                    firstArg = false;
                }

                SortedMap sortedParamMap = new TreeMap(new CustomStringAsIntegerComparator());
                sortedParamMap.putAll(paramMap);

                sb.append("(");
                boolean firstParam = true;
                for (Map.Entry paramEntry : sortedParamMap.entrySet()) {
                    if (!firstParam) {
                        sb.append(", ");
                    } else {
                        firstParam = false;
                    }
                    Object parameter = paramEntry.getValue();
                    if (parameter != null && parameter.getClass().isArray()) {
                        sb.append(arrayToString(parameter));
                    } else {
                        sb.append(parameter);
                    }
                }
                sb.append(")");
            }
        }
        sb.append("]");
    }

    private String arrayToString(Object object) {
        if (object.getClass().isArray()) {
            if (object instanceof byte[]) {
                return Arrays.toString((byte[]) object);
            }
            if (object instanceof short[]) {
                return Arrays.toString((short[]) object);
            }
            if (object instanceof char[]) {
                return Arrays.toString((char[]) object);
            }
            if (object instanceof int[]) {
                return Arrays.toString((int[]) object);
            }
            if (object instanceof long[]) {
                return Arrays.toString((long[]) object);
            }
            if (object instanceof float[]) {
                return Arrays.toString((float[]) object);
            }
            if (object instanceof double[]) {
                return Arrays.toString((double[]) object);
            }
            if (object instanceof boolean[]) {
                return Arrays.toString((boolean[]) object);
            }
            if (object instanceof Object[]) {
                return Arrays.toString((Object[]) object);
            }
        }
        throw new UnsupportedOperationException("Array type not supported: " + object.getClass());
    }

    private static class CustomStringAsIntegerComparator extends StringAsIntegerComparator {
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy