<commit_before>/**
 * The contents of this file are subject to the OpenMRS Public License
 * Version 1.0 (the "License"); you may not use this file except in
 * compliance with the License. You may obtain a copy of the License at
 * http://license.openmrs.org
 *
 * Software distributed under the License is distributed on an "AS IS"
 * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
 * License for the specific language governing rights and limitations
 * under the License.
 *
 * Copyright (C) OpenMRS, LLC.  All Rights Reserved.
 */
package org.openmrs.util;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.util.List;
import java.util.Vector;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.openmrs.api.db.DAOException;

/**
 * Utility class that provides database related methods
 * 
 * @since 1.6
 */
public class DatabaseUtil {
	
	private final static Log log = LogFactory.getLog(DatabaseUtil.class);
	
	/**
	 * Load the jdbc driver clas for the database which is specified by the connectionUrl parameter <br/>
	 * <br/>
	 * This is only needed when loading up a jdbc connection manually for the first time. This is
	 * not needed by most users and development practices with the openmrs API.
	 * 
	 * @param connectionUrl the connection url for the database, such as
	 *            "jdbc:mysql://localhost:3306/..."
	 * @throws ClassNotFoundException
	 */
	public static void loadDatabaseDriver(String connectionUrl) throws ClassNotFoundException {
		if (connectionUrl.contains("mysql"))
			Class.forName("com.mysql.jdbc.Driver");
		else if (connectionUrl.contains("hsqldb"))
			Class.forName("org.hsqldb.jdbcDriver");
		else if (connectionUrl.contains("postgresql"))
			Class.forName("org.postgresql.Driver");
		else if (connectionUrl.contains("oracle"))
			Class.forName("oracle.jdbc.driver.OracleDriver");
		else if (connectionUrl.contains("sqlserver"))
			Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver");
	}
	
	/**
	 * Executes the passed SQL query, enforcing select only if that parameter is set
	 */
	public static List<List<Object>> executeSQL(Connection conn, String sql, boolean selectOnly) throws DAOException {
		sql = sql.trim();
		boolean dataManipulation = false;<commit_msg>Adding support for JTDS database driver - TRUNK-1921<commit_after>/**
 * The contents of this file are subject to the OpenMRS Public License
 * Version 1.0 (the "License"); you may not use this file except in
 * compliance with the License. You may obtain a copy of the License at
 * http://license.openmrs.org
 *
 * Software distributed under the License is distributed on an "AS IS"
 * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
 * License for the specific language governing rights and limitations
 * under the License.
 *
 * Copyright (C) OpenMRS, LLC.  All Rights Reserved.
 */
package org.openmrs.util;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.util.List;
import java.util.Vector;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.openmrs.api.db.DAOException;

/**
 * Utility class that provides database related methods
 * 
 * @since 1.6
 */
public class DatabaseUtil {
	
	private final static Log log = LogFactory.getLog(DatabaseUtil.class);
	
	/**
	 * Load the jdbc driver clas for the database which is specified by the connectionUrl parameter <br/>
	 * <br/>
	 * This is only needed when loading up a jdbc connection manually for the first time. This is
	 * not needed by most users and development practices with the openmrs API.
	 * 
	 * @param connectionUrl the connection url for the database, such as
	 *            "jdbc:mysql://localhost:3306/..."
	 * @throws ClassNotFoundException
	 */
	public static void loadDatabaseDriver(String connectionUrl) throws ClassNotFoundException {
		if (connectionUrl.contains("mysql"))
			Class.forName("com.mysql.jdbc.Driver");
		else if (connectionUrl.contains("hsqldb"))
			Class.forName("org.hsqldb.jdbcDriver");
		else if (connectionUrl.contains("postgresql"))
			Class.forName("org.postgresql.Driver");
		else if (connectionUrl.contains("oracle"))
			Class.forName("oracle.jdbc.driver.OracleDriver");
		else if (connectionUrl.contains("jtds"))
			Class.forName("net.sourceforge.jtds.jdbc.Driver");
		else if (connectionUrl.contains("sqlserver"))
			Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver");
	}
	
	/**
	 * Executes the passed SQL query, enforcing select only if that parameter is set
	 */
	public static List<List<Object>> executeSQL(Connection conn, String sql, boolean selectOnly) throws DAOException {
		sql = sql.trim();
		boolean dataManipulation = false;
{'fraction_non_alphanumeric': 0.10052588996763755, 'fraction_numerical': 0.0040453074433656954, 'mean_word_length': 4.3344120819848975, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 11, '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': '6810687', 'n_tokens_mistral': 1462, 'n_tokens_neox': 1353, 'n_words': 479}