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

org.postgresql.core.CachedQuery Maven / Gradle / Ivy

There is a newer version: 42.7.3
Show newest version
/*-------------------------------------------------------------------------
*
* Copyright (c) 2015, PostgreSQL Global Development Group
*
*
*-------------------------------------------------------------------------
*/
package org.postgresql.core;

import org.postgresql.util.CanEstimateSize;

/**
 * Stores information on the parsed JDBC query.
 * It is used to cut parsing overhead when executing the same query through {@link java.sql.Connection#prepareStatement(String)}.
 */
public class CachedQuery implements CanEstimateSize {
    /**
     * Cache key. {@link String} or {@link org.postgresql.jdbc2.CallableQueryKey}.
     * It is assumed that {@code String.valueOf(key)*2} would give reasonable estimate of the number of retained bytes
     * by given key (see {@link #getSize}).
     */
    public final Object key;
    public final Query query;
    public final boolean isFunction;
    public final boolean outParmBeforeFunc;

    private int executeCount;

    public CachedQuery(Object key, Query query, boolean isFunction, boolean outParmBeforeFunc)
    {
        this.key = key;
        this.query = query;
        this.isFunction = isFunction;
        this.outParmBeforeFunc = outParmBeforeFunc;
    }

    public void increaseExecuteCount() {
        if (executeCount < Integer.MAX_VALUE)
            executeCount++;
    }

    public void increaseExecuteCount(int inc) {
        int newValue = executeCount + inc;
        if (newValue > 0) // if overflows, just ignore the update
            executeCount = newValue;
    }

    /**
     * Number of times this statement has been used
     * @return number of times this statement has been used
     */
    public int getExecuteCount() {
        return executeCount;
    }

    @Override
    public long getSize()
    {
        int queryLength = String.valueOf(key).length() * 2 /* 2 bytes per char */;
        return queryLength * 2 /* original query and native sql */ + 100 /* entry in hash map, CachedQuery wrapper, etc */;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy