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

org.n3r.eql.util.BlackcatUtils Maven / Gradle / Ivy

There is a newer version: 0.1.19
Show newest version
package org.n3r.eql.util;

import com.github.bingoohuang.blackcat.instrument.callback.Blackcat;
import com.github.bingoohuang.blackcat.instrument.utils.Collections;
import com.github.bingoohuang.westjson.WestJson;
import lombok.experimental.UtilityClass;

import java.util.Collection;

@UtilityClass
@SuppressWarnings("unchecked")
public class BlackcatUtils {
    public boolean classExists(String className) {
        try {
            Class.forName(className);
            return true;
        } catch (Throwable e) { // including ClassNotFoundException
            return false;
        }
    }

    public final boolean HAS_BLACKCAT = classExists(
            "com.github.bingoohuang.blackcat.instrument.callback.Blackcat");

    public void trace(String sqlId, String printSql,
                             String traceParams, String evalSql, Object execRet) {
        if (!HAS_BLACKCAT) return;

        String paramsAndPrepared = "[]".equals(traceParams) ? ""
                : ", Params:" + traceParams + ", Prepared:" + printSql;
        Blackcat.trace("SQL",
                "ID:" + sqlId
                        + ", SQL:" + evalSql
                        + paramsAndPrepared
                        + ", Result:" + compressResult(execRet)
        );
    }

    private Object compressResult(Object execRet) {
        if (!(execRet instanceof Collection)) {
            return new WestJson().json(execRet, WestJson.UNQUOTED);
        }

        return Collections.compressResult((Collection) execRet);
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy