<commit_before>package org.kered.dko.persistence;

import java.io.File;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.logging.Logger;

import javax.sql.DataSource;

import org.kered.dko.Constants;
import org.kered.dko.datasource.JDBCDriverDataSource;
import org.kered.dko.datasource.SingleThreadedDataSource;

public class Util {

    static final String CREATE_QS = "CREATE TABLE query_size (last_seen BIGINT, schema_name TEXT, table_name TEXT, id INTEGER PRIMARY KEY, hash_code int, row_count bigint);";
    static final String CREATE_CA = "CREATE TABLE column_access (id INTEGER PRIMARY KEY, query_execution_id long, table_name TEXT, column_name TEXT, last_seen bigint);";
    static final String CREATE_CA_I = "CREATE INDEX caqe ON column_access(query_execution_id ASC);";
    static final String CREATE_QE = "CREATE TABLE query_execution (id INTEGER PRIMARY KEY, query_hash int, stack_hash int, last_seen bigint, description text);";
    static final String CREATE_QE_I1 = "CREATE INDEX qeqh ON query_execution(query_hash ASC);";
    static final String CREATE_QE_I2 = "CREATE INDEX qesh ON query_execution(stack_hash ASC);";

	static DataSource ds = null;
	private static File dbPath = null;
	private static final Logger log = Logger.getLogger("org.kered.dko.persistence.Util");

	public static void setPersistenceDatabasePath(final File f) {
		dbPath = f;
		ds = null;
	}

	static boolean warnedNoSqlite3 = false;

	public static DataSource getDS() {
		if (ds == null) {
			final String[] drivers = {"org.sqlite.JDBC", "org.sqldroid.SQLDroidDriver"};
			Class driver = null;
			for (final String s : drivers) {
				try {
					driver = Class.forName(s);
				} catch (final ClassNotFoundException e) {
					log.fine("could not find "+ s);
				}
			}
			if (driver==null) {
				if (!warnedNoSqlite3) {
					log.warning("Could not find any Sqlite3 JDBC drivers.  All query optimization and snapshot functionality is turned off.");
					warnedNoSqlite3 = true;
				}
				return null;
			}
			final String path = System
					.getProperty(Constants.PROPERTY_PERSISTENCE_DB);
			File PERSISTENCE_DB = null;
			if (dbPath != null) {
				PERSISTENCE_DB = dbPath;
			} else if (path == null) {
				final File BASE_DIR = new File(
						System.getProperty("user.home"));
				PERSISTENCE_DB = new File(BASE_DIR, ".dko_persistence.db");
			} else {
				PERSISTENCE_DB = new File(path);
			}
			final String url = "jdbc:sqlite:" + PERSISTENCE_DB.getPath();
			Connection conn = null;
			try {
				conn = DriverManager.getConnection(url);
				checkQuerySize(conn);
				checkQueryExecution(conn);
				checkColumnAccess(conn);
			} catch (final SQLException e) {
				log.warning("I could not confirm the state of the persistence database ("+ PERSISTENCE_DB.getPath()
						+"), so the usage monitor will be diabled for this query.  This will not effect its output, "
						+ "only its speed.  This is the underlying error: "+ e);
				return null;
			} finally {
				try {
					conn.close();
				} catch (final SQLException e) {
					e.printStackTrace();
				}
			}
			ds = new SingleThreadedDataSource(new JDBCDriverDataSource(
					Constants.DB_TYPE.SQLITE3, url), 10000, true);

//			try {
//				Package pkg = Class.forName("org.kered.dko.persistence.QuerySize").getPackage();
//				Context[] contexts = {Context.getVMContext(), Context.getThreadContext(), Context.getThreadGroupContext() };
//				for (Context context : contexts) {
//					context.setDataSource(pkg, ds).setAutoUndo(false);
//				}
//			} catch (ClassNotFoundException e) {
//				// TODO Auto-generated catch block
//				e.printStackTrace();
//			}
		}
		return ds;
	}

	private static void checkQueryExecution(final Connection conn) throws SQLException {
		final Statement stmt = conn.createStatement();
		try {
			final ResultSet rs = stmt.executeQuery("select count(1) from query_execution");
			rs.next();
			rs.getInt(1);<commit_msg>fix npe<commit_after>package org.kered.dko.persistence;

import java.io.File;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.logging.Logger;

import javax.sql.DataSource;

import org.kered.dko.Constants;
import org.kered.dko.datasource.JDBCDriverDataSource;
import org.kered.dko.datasource.SingleThreadedDataSource;

public class Util {

    static final String CREATE_QS = "CREATE TABLE query_size (last_seen BIGINT, schema_name TEXT, table_name TEXT, id INTEGER PRIMARY KEY, hash_code int, row_count bigint);";
    static final String CREATE_CA = "CREATE TABLE column_access (id INTEGER PRIMARY KEY, query_execution_id long, table_name TEXT, column_name TEXT, last_seen bigint);";
    static final String CREATE_CA_I = "CREATE INDEX caqe ON column_access(query_execution_id ASC);";
    static final String CREATE_QE = "CREATE TABLE query_execution (id INTEGER PRIMARY KEY, query_hash int, stack_hash int, last_seen bigint, description text);";
    static final String CREATE_QE_I1 = "CREATE INDEX qeqh ON query_execution(query_hash ASC);";
    static final String CREATE_QE_I2 = "CREATE INDEX qesh ON query_execution(stack_hash ASC);";

	static DataSource ds = null;
	private static File dbPath = null;
	private static final Logger log = Logger.getLogger("org.kered.dko.persistence.Util");

	public static void setPersistenceDatabasePath(final File f) {
		dbPath = f;
		ds = null;
	}

	static boolean warnedNoSqlite3 = false;

	public static DataSource getDS() {
		if (ds == null) {
			final String[] drivers = {"org.sqlite.JDBC", "org.sqldroid.SQLDroidDriver"};
			Class driver = null;
			for (final String s : drivers) {
				try {
					driver = Class.forName(s);
				} catch (final ClassNotFoundException e) {
					log.fine("could not find "+ s);
				}
			}
			if (driver==null) {
				if (!warnedNoSqlite3) {
					log.warning("Could not find any Sqlite3 JDBC drivers.  All query optimization and snapshot functionality is turned off.");
					warnedNoSqlite3 = true;
				}
				return null;
			}
			final String path = System
					.getProperty(Constants.PROPERTY_PERSISTENCE_DB);
			File PERSISTENCE_DB = null;
			if (dbPath != null) {
				PERSISTENCE_DB = dbPath;
			} else if (path == null) {
				final File BASE_DIR = new File(
						System.getProperty("user.home"));
				PERSISTENCE_DB = new File(BASE_DIR, ".dko_persistence.db");
			} else {
				PERSISTENCE_DB = new File(path);
			}
			final String url = "jdbc:sqlite:" + PERSISTENCE_DB.getPath();
			Connection conn = null;
			try {
				conn = DriverManager.getConnection(url);
				checkQuerySize(conn);
				checkQueryExecution(conn);
				checkColumnAccess(conn);
			} catch (final SQLException e) {
				log.warning("I could not confirm the state of the persistence database ("+ PERSISTENCE_DB.getPath()
						+"), so the usage monitor will be diabled for this query.  This will not effect its output, "
						+ "only its speed.  This is the underlying error: "+ e);
				return null;
			} finally {
				try {
					if (conn!=null) conn.close();
				} catch (final SQLException e) {
					e.printStackTrace();
				}
			}
			ds = new SingleThreadedDataSource(new JDBCDriverDataSource(
					Constants.DB_TYPE.SQLITE3, url), 10000, true);

//			try {
//				Package pkg = Class.forName("org.kered.dko.persistence.QuerySize").getPackage();
//				Context[] contexts = {Context.getVMContext(), Context.getThreadContext(), Context.getThreadGroupContext() };
//				for (Context context : contexts) {
//					context.setDataSource(pkg, ds).setAutoUndo(false);
//				}
//			} catch (ClassNotFoundException e) {
//				// TODO Auto-generated catch block
//				e.printStackTrace();
//			}
		}
		return ds;
	}

	private static void checkQueryExecution(final Connection conn) throws SQLException {
		final Statement stmt = conn.createStatement();
		try {
			final ResultSet rs = stmt.executeQuery("select count(1) from query_execution");
			rs.next();
			rs.getInt(1);
{'fraction_non_alphanumeric': 0.10859277708592777, 'fraction_numerical': 0.0034869240348692405, 'mean_word_length': 3.808982035928144, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, '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': '659026', 'n_tokens_mistral': 2923, 'n_tokens_neox': 2486, 'n_words': 702}