<commit_before>/*
 * Copyright (c) 2000  <NAME> <<EMAIL>>
 *
 * $Id: CachePreparedStatementStub.java,v 1.3 2003/03/28 20:30:11 knitterb Exp $
 */

package net.spy.db;

import java.math.BigDecimal;

import java.sql.Date;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Time;
import java.sql.Timestamp;
import java.sql.Types;

import net.spy.SpyDB;

import net.spy.cache.SpyCache;

/**
 * Prepared statement for executing cached queries
 */
public class CachePreparedStatementStub extends Object {

	// Stored DB handle
	private SpyDB db=null;

	// Where we store the prepared query
	private String queryStr=null;

	// Where we can put arguments.
	private Object args[]=null;
	// What are our argument types
	private int types[]=null;

	// How long the results of this statement should be cached
	private long cacheTime=60*60*1000;

	// Query timeout
	private int timeout=0;

	/**
	 * Create a CachePreparedStatement object for the given query (you
	 * probably don't want to do this directly).
	 */
	public CachePreparedStatementStub(SpyDB db, String query, long cacheTime) {
		super();
		this.db=db;
		this.queryStr=query;
		this.cacheTime=cacheTime;

		// Figure out how many arguments may be used.
		int ntokens=countQs(query);
		args=new Object[ntokens];
		types=new int[ntokens];
	}

	// Count the number of question marks
	private int countQs(String query) {
		int qs=0;
		int currentIndex=-1;

		do {
			currentIndex=query.indexOf("?", currentIndex+1);
			if(currentIndex>=0) {
				qs++;
			}
		} while(currentIndex>=0);

		return(qs);
	}

	// Set a given argument
	private void setArg(int index, Object what, int type)
		throws SQLException {
		// Our base is 0, JDBC base is 1
		index--;

		if(index<0) {
			throw new SQLException("Illegal index, they start at 1, G");
		}
		if(index>=args.length) {
			throw new SQLException("Illegal index, this statement takes a "
				+ "maximum of " + args.length + " arguments.");
		}

		// Set the vars
		args[index]=what;
		types[index]=type;
	}

	/**
	 * Get an integer hash code to uniquely identify this object.
	 */
	public int hashCode() {
		int hc=0;

		hc+=queryStr.hashCode();
		StringBuffer sb=new StringBuffer(256);
		for(int i=0; i<args.length; i++) {
			try {
				sb.append(args[i]);
				sb.append((char)0x00);
			} catch(Exception e) {
				// Ignore it, we'll get close.
			}
		}
		// Hashcode of all of the args run together.
		hc+=sb.toString().hashCode();

		return(hc);
	}

	// Implemented
	public ResultSet executeQuery() 
		throws SQLException {

		int hc=hashCode();
		String key="dbcache_prepared_" + hc;
		SpyCache cache=SpyCache.getInstance();
		CachedResultSet crs=(CachedResultSet)cache.get(key);
		if(crs==null) {
			crs=realExecuteQuery();
			cache.store(key, crs, cacheTime*1000);
		}
		ResultSet crsret=(ResultSet)crs.newCopy();
		return(crsret);
	}

	/** 
	 * Set the query timeout.
	 * 
	 * @param to the maximum number of seconds
	 * @throws SQLException if the number is not greater than or equal to 0
	 */
	public void setQueryTimeout(int to) throws SQLException {
		timeout=to;
	}

	// OK, here's what happens when we determine that we really don't have
	// the data and need to come up with it.
	private CachedResultSet realExecuteQuery() throws SQLException {
		PreparedStatement pst=db.prepareStatement(queryStr);
		pst.setQueryTimeout(timeout);

		// Set allllllll the types
		for(int i=0; i<args.length; i++) {
			try {
				switch(types[i]) {
					case Types.BIT:
						pst.setBoolean(i+1, ((Boolean)args[i]).booleanValue());
						break;
					case Types.DATE:
						pst.setDate(i+1, (Date)args[i]);
						break;
					case Types.DOUBLE:
						pst.setDouble(i+1, ((Double)args[i]).doubleValue());
						break;
					case Types.FLOAT:
						pst.setFloat(i+1, ((Float)args[i]).floatValue());
						break;
					case Types.INTEGER:
						pst.setInt(i+1, ((Integer)args[i]).intValue());
						break;
					case Types.BIGINT:
						pst.setLong(i+1, ((Long)args[i]).longValue());
						break;
					case Types.TINYINT:
						pst.setShort(i+1, (short)((Integer)args[i]).intValue());
						break;
					case Types.NULL:
						pst.setNull(i+1, ((Integer)args[i]).intValue());
						break;
					case Types.OTHER:
						pst.setObject(i+1, args[i]);
						break;
					case Types.VARCHAR:
						pst.setString(i+1, (String)args[i]);
						break;
					case Types.TIME:
						pst.setTime(i+1, (Time)args[i]);
						break;
					case Types.TIMESTAMP:
						pst.setTimestamp(i+1, (Timestamp)args[i]);
						break;
					case Types.DECIMAL:
						pst.setBigDecimal(i+1, (BigDecimal)args[i]);
						break;
					default:
						throw new SQLException("Whoops, type "
							+ types[i] + " (" + TypeNames.getTypeName(types[i])
							+ ") seems to have been overlooked.");
				}
			} catch (NullPointerException ex) {
				System.err.println("error with "+args[i]+" in type "+
					types[i]+" at param postition "+i);
				throw ex;
			}
		}

		// OK, at this point, all the arguments should be set, proceed to
		// execute the query.
		return(new CachedResultSet(pst.executeQuery()));

	}

	// Implemented (sorta)
	public int executeUpdate() 
		throws SQLException {
		throw new SQLException("Illegal?  This operation makes no sense!");<commit_msg>Use the logger. Keywords: cscvs:MAIN-193<commit_after>/*
 * Copyright (c) 2000  <NAME> <<EMAIL>>
 *
 * $Id: CachePreparedStatementStub.java,v 1.4 2003/04/02 05:55:02 dustin Exp $
 */

package net.spy.db;

import java.math.BigDecimal;

import java.sql.Date;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Time;
import java.sql.Timestamp;
import java.sql.Types;

import net.spy.SpyDB;
import net.spy.SpyObject;

import net.spy.cache.SpyCache;

/**
 * Prepared statement for executing cached queries
 */
public class CachePreparedStatementStub extends SpyObject {

	// Stored DB handle
	private SpyDB db=null;

	// Where we store the prepared query
	private String queryStr=null;

	// Where we can put arguments.
	private Object args[]=null;
	// What are our argument types
	private int types[]=null;

	// How long the results of this statement should be cached
	private long cacheTime=60*60*1000;

	// Query timeout
	private int timeout=0;

	/**
	 * Create a CachePreparedStatement object for the given query (you
	 * probably don't want to do this directly).
	 */
	public CachePreparedStatementStub(SpyDB db, String query, long cacheTime) {
		super();
		this.db=db;
		this.queryStr=query;
		this.cacheTime=cacheTime;

		// Figure out how many arguments may be used.
		int ntokens=countQs(query);
		args=new Object[ntokens];
		types=new int[ntokens];
	}

	// Count the number of question marks
	private int countQs(String query) {
		int qs=0;
		int currentIndex=-1;

		do {
			currentIndex=query.indexOf("?", currentIndex+1);
			if(currentIndex>=0) {
				qs++;
			}
		} while(currentIndex>=0);

		return(qs);
	}

	// Set a given argument
	private void setArg(int index, Object what, int type)
		throws SQLException {
		// Our base is 0, JDBC base is 1
		index--;

		if(index<0) {
			throw new SQLException("Illegal index, they start at 1, G");
		}
		if(index>=args.length) {
			throw new SQLException("Illegal index, this statement takes a "
				+ "maximum of " + args.length + " arguments.");
		}

		// Set the vars
		args[index]=what;
		types[index]=type;
	}

	/**
	 * Get an integer hash code to uniquely identify this object.
	 */
	public int hashCode() {
		int hc=0;

		hc+=queryStr.hashCode();
		StringBuffer sb=new StringBuffer(256);
		for(int i=0; i<args.length; i++) {
			try {
				sb.append(args[i]);
				sb.append((char)0x00);
			} catch(Exception e) {
				// Ignore it, we'll get close.
			}
		}
		// Hashcode of all of the args run together.
		hc+=sb.toString().hashCode();

		return(hc);
	}

	// Implemented
	public ResultSet executeQuery() 
		throws SQLException {

		int hc=hashCode();
		String key="dbcache_prepared_" + hc;
		SpyCache cache=SpyCache.getInstance();
		CachedResultSet crs=(CachedResultSet)cache.get(key);
		if(crs==null) {
			crs=realExecuteQuery();
			cache.store(key, crs, cacheTime*1000);
		}
		ResultSet crsret=(ResultSet)crs.newCopy();
		return(crsret);
	}

	/** 
	 * Set the query timeout.
	 * 
	 * @param to the maximum number of seconds
	 * @throws SQLException if the number is not greater than or equal to 0
	 */
	public void setQueryTimeout(int to) throws SQLException {
		timeout=to;
	}

	// OK, here's what happens when we determine that we really don't have
	// the data and need to come up with it.
	private CachedResultSet realExecuteQuery() throws SQLException {
		PreparedStatement pst=db.prepareStatement(queryStr);
		pst.setQueryTimeout(timeout);

		// Set allllllll the types
		for(int i=0; i<args.length; i++) {
			try {
				switch(types[i]) {
					case Types.BIT:
						pst.setBoolean(i+1, ((Boolean)args[i]).booleanValue());
						break;
					case Types.DATE:
						pst.setDate(i+1, (Date)args[i]);
						break;
					case Types.DOUBLE:
						pst.setDouble(i+1, ((Double)args[i]).doubleValue());
						break;
					case Types.FLOAT:
						pst.setFloat(i+1, ((Float)args[i]).floatValue());
						break;
					case Types.INTEGER:
						pst.setInt(i+1, ((Integer)args[i]).intValue());
						break;
					case Types.BIGINT:
						pst.setLong(i+1, ((Long)args[i]).longValue());
						break;
					case Types.TINYINT:
						pst.setShort(i+1, (short)((Integer)args[i]).intValue());
						break;
					case Types.NULL:
						pst.setNull(i+1, ((Integer)args[i]).intValue());
						break;
					case Types.OTHER:
						pst.setObject(i+1, args[i]);
						break;
					case Types.VARCHAR:
						pst.setString(i+1, (String)args[i]);
						break;
					case Types.TIME:
						pst.setTime(i+1, (Time)args[i]);
						break;
					case Types.TIMESTAMP:
						pst.setTimestamp(i+1, (Timestamp)args[i]);
						break;
					case Types.DECIMAL:
						pst.setBigDecimal(i+1, (BigDecimal)args[i]);
						break;
					default:
						throw new SQLException("Whoops, type "
							+ types[i] + " (" + TypeNames.getTypeName(types[i])
							+ ") seems to have been overlooked.");
				}
			} catch (NullPointerException ex) {
				getLogger().error("error with "+args[i]+" in type "+
					types[i]+" at param postition "+i);
				throw ex;
			}
		}

		// OK, at this point, all the arguments should be set, proceed to
		// execute the query.
		return(new CachedResultSet(pst.executeQuery()));

	}

	// Implemented (sorta)
	public int executeUpdate() 
		throws SQLException {
		throw new SQLException("Illegal?  This operation makes no sense!");
{'fraction_non_alphanumeric': 0.1353369246763066, 'fraction_numerical': 0.01256970040638881, 'mean_word_length': 3.260064412238325, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 15, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 2, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1875451', 'n_tokens_mistral': 4337, 'n_tokens_neox': 3557, 'n_words': 941}