Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance. Project price only 1 $
You can buy this project and download/modify it how often you want.
/**
* Copyright 2015-2016 Knowm Inc. (http://knowm.org) and contributors.
* Copyright 2011-2015 Xeiam LLC (http://xeiam.com) and contributors.
*
* Licensed under the Apache License, Version 2.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://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.knowm.yank;
import java.sql.SQLException;
import java.util.List;
import java.util.Properties;
import org.apache.commons.dbutils.BasicRowProcessor;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.ResultSetHandler;
import org.apache.commons.dbutils.handlers.ArrayListHandler;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import org.apache.commons.dbutils.handlers.ColumnListHandler;
import org.apache.commons.dbutils.handlers.ScalarHandler;
import org.knowm.yank.exceptions.SQLStatementNotFoundException;
import org.knowm.yank.handlers.InsertedIDResultSetHandler;
import org.knowm.yank.processors.YankBeanProcessor;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.zaxxer.hikari.HikariDataSource;
/**
* A wrapper for DBUtils' QueryRunner's methods: update, query, and batch. Connections are retrieved from the connection pool in DBConnectionManager.
*
* @author timmolter
*/
public final class Yank {
private static final YankPoolManager YANK_POOL_MANAGER = YankPoolManager.INSTANCE;
/** slf4J logger wrapper */
private static Logger logger = LoggerFactory.getLogger(Yank.class);
private static final String BEAN_EXCEPTION_MESSAGE = "Error converting row to bean!! Make sure you have a default no-args constructor!";
private static final String QUERY_EXCEPTION_MESSAGE = "Error in SQL query!!!";
/**
* Prevent class instantiation with private constructor
*/
private Yank() {
}
// ////// INSERT //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/**
* Executes a given INSERT SQL prepared statement matching the sqlKey String in a properties file loaded via Yank.addSQLStatements(...) using the
* default connection pool. Returns the auto-increment id of the inserted row.
*
* @param sqlKey The SQL Key found in a properties file corresponding to the desired SQL statement value
* @param params The replacement parameters
* @return the auto-increment id of the inserted row, or null if no id is available
* @throws SQLStatementNotFoundException if an SQL statement could not be found for the given sqlKey String
*/
public static Long insertSQLKey(String sqlKey, Object[] params) {
return insertSQLKey(YankPoolManager.DEFAULT_POOL_NAME, sqlKey, params);
}
/**
* Executes a given INSERT SQL prepared statement matching the sqlKey String in a properties file loaded via Yank.addSQLStatements(...). Returns the
* auto-increment id of the inserted row.
*
* @param poolName The name of the connection pool to query against
* @param sqlKey The SQL Key found in a properties file corresponding to the desired SQL statement value
* @param params The replacement parameters
* @return the auto-increment id of the inserted row, or null if no id is available
* @throws SQLStatementNotFoundException if an SQL statement could not be found for the given sqlKey String
*/
public static Long insertSQLKey(String poolName, String sqlKey, Object[] params) {
String sql = YANK_POOL_MANAGER.getMergedSqlProperties().getProperty(sqlKey);
if (sql == null || sql.equalsIgnoreCase("")) {
throw new SQLStatementNotFoundException();
} else {
return insert(poolName, sql, params);
}
}
/**
* Executes a given INSERT SQL prepared statement. Returns the auto-increment id of the inserted row using the default connection pool.
*
* @param sql The query to execute
* @param params The replacement parameters
* @return the auto-increment id of the inserted row, or null if no id is available
*/
public static Long insert(String sql, Object[] params) {
return insert(YankPoolManager.DEFAULT_POOL_NAME, sql, params);
}
/**
* Executes a given INSERT SQL prepared statement. Returns the auto-increment id of the inserted row.
*
* @param poolName The name of the connection pool to query against
* @param sql The query to execute
* @param params The replacement parameters
* @return the auto-increment id of the inserted row, or null if no id is available
*/
public static Long insert(String poolName, String sql, Object[] params) {
Long returnLong = null;
try {
ResultSetHandler rsh = new InsertedIDResultSetHandler();
returnLong = new QueryRunner(YANK_POOL_MANAGER.getConnectionPool(poolName)).insert(sql, rsh, params);
} catch (SQLException e) {
handleSQLException(e);
}
return returnLong == null ? 0 : returnLong;
}
// ////// INSERT, UPDATE, DELETE, or UPSERT //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/**
* Executes the given INSERT, UPDATE, DELETE, REPLACE or UPSERT SQL statement matching the sqlKey String in a properties file loaded via
* Yank.addSQLStatements(...) using the default connection pool. Returns the number of rows affected.
*
* @param sqlKey The SQL Key found in a properties file corresponding to the desired SQL statement value
* @param params The replacement parameters
* @return The number of rows affected
* @throws SQLStatementNotFoundException if an SQL statement could not be found for the given sqlKey String
*/
public static int executeSQLKey(String sqlKey, Object[] params) {
return executeSQLKey(YankPoolManager.DEFAULT_POOL_NAME, sqlKey, params);
}
/**
* Executes the given INSERT, UPDATE, DELETE, REPLACE or UPSERT SQL statement matching the sqlKey String in a properties file loaded via
* Yank.addSQLStatements(...). Returns the number of rows affected.
*
* @param poolName The name of the connection pool to query against
* @param sqlKey The SQL Key found in a properties file corresponding to the desired SQL statement value
* @param params The replacement parameters
* @return The number of rows affected
* @throws SQLStatementNotFoundException if an SQL statement could not be found for the given sqlKey String
*/
public static int executeSQLKey(String poolName, String sqlKey, Object[] params) {
String sql = YANK_POOL_MANAGER.getMergedSqlProperties().getProperty(sqlKey);
if (sql == null || sql.equalsIgnoreCase("")) {
throw new SQLStatementNotFoundException();
} else {
return execute(poolName, sql, params);
}
}
/**
* Executes the given INSERT, UPDATE, DELETE, REPLACE or UPSERT SQL prepared statement. Returns the number of rows affected using the default
* connection pool.
*
* @param sql The query to execute
* @param params The replacement parameters
* @return The number of rows affected
*/
public static int execute(String sql, Object[] params) {
return execute(YankPoolManager.DEFAULT_POOL_NAME, sql, params);
}
/**
* Executes the given INSERT, UPDATE, DELETE, REPLACE or UPSERT SQL prepared statement. Returns the number of rows affected.
*
* @param poolName The name of the connection pool to query against
* @param sql The query to execute
* @param params The replacement parameters
* @return The number of rows affected
*/
public static int execute(String poolName, String sql, Object[] params) {
int returnInt = 0;
try {
returnInt = new QueryRunner(YANK_POOL_MANAGER.getConnectionPool(poolName)).update(sql, params);
} catch (SQLException e) {
handleSQLException(e);
}
return returnInt;
}
// ////// Single Scalar QUERY //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/**
* Return just one scalar given a SQL Key using an SQL statement matching the sqlKey String in a properties file loaded via
* Yank.addSQLStatements(...) using the default connection pool. If more than one row match the query, only the first row is returned.
*
* @param sqlKey The SQL Key found in a properties file corresponding to the desired SQL statement value
* @param scalarType The Class of the desired return scalar matching the table
* @param params The replacement parameters
* @return The Object
* @throws SQLStatementNotFoundException if an SQL statement could not be found for the given sqlKey String
*/
public static T queryScalarSQLKey(String sqlKey, Class scalarType, Object[] params) {
return queryScalarSQLKey(YankPoolManager.DEFAULT_POOL_NAME, sqlKey, scalarType, params);
}
/**
* Return just one scalar given a SQL Key using an SQL statement matching the sqlKey String in a properties file loaded via
* Yank.addSQLStatements(...). If more than one row match the query, only the first row is returned.
*
* @param poolName The name of the connection pool to query against
* @param sqlKey The SQL Key found in a properties file corresponding to the desired SQL statement value
* @param scalarType The Class of the desired return scalar matching the table
* @param params The replacement parameters
* @return The Object
* @throws SQLStatementNotFoundException if an SQL statement could not be found for the given sqlKey String
*/
public static T queryScalarSQLKey(String poolName, String sqlKey, Class scalarType, Object[] params) {
String sql = YANK_POOL_MANAGER.getMergedSqlProperties().getProperty(sqlKey);
if (sql == null || sql.equalsIgnoreCase("")) {
throw new SQLStatementNotFoundException();
} else {
return queryScalar(poolName, sql, scalarType, params);
}
}
/**
* Return just one scalar given a an SQL statement using the default connection pool.
*
* @param scalarType The Class of the desired return scalar matching the table
* @param params The replacement parameters
* @return The scalar Object
* @throws SQLStatementNotFoundException if an SQL statement could not be found for the given sqlKey String
*/
public static T queryScalar(String sql, Class scalarType, Object[] params) {
return queryScalar(YankPoolManager.DEFAULT_POOL_NAME, sql, scalarType, params);
}
/**
* Return just one scalar given a an SQL statement
*
* @param poolName The name of the connection pool to query against
* @param scalarType The Class of the desired return scalar matching the table
* @param params The replacement parameters
* @return The scalar Object
* @throws SQLStatementNotFoundException if an SQL statement could not be found for the given sqlKey String
*/
public static T queryScalar(String poolName, String sql, Class scalarType, Object[] params) {
T returnObject = null;
try {
ScalarHandler resultSetHandler = new ScalarHandler();
returnObject = new QueryRunner(YANK_POOL_MANAGER.getConnectionPool(poolName)).query(sql, resultSetHandler, params);
} catch (SQLException e) {
handleSQLException(e);
}
return returnObject;
}
// ////// Single Object QUERY //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/**
* Return just one Bean given a SQL Key using an SQL statement matching the sqlKey String in a properties file loaded via
* Yank.addSQLStatements(...). If more than one row match the query, only the first row is returned using the default connection pool.
*
* @param sqlKey The SQL Key found in a properties file corresponding to the desired SQL statement value
* @param params The replacement parameters
* @param beanType The Class of the desired return Object matching the table
* @return The Object
* @throws SQLStatementNotFoundException if an SQL statement could not be found for the given sqlKey String
*/
public static T queryBeanSQLKey(String sqlKey, Class beanType, Object[] params) {
return queryBeanSQLKey(YankPoolManager.DEFAULT_POOL_NAME, sqlKey, beanType, params);
}
/**
* Return just one Bean given a SQL Key using an SQL statement matching the sqlKey String in a properties file loaded via
* Yank.addSQLStatements(...). If more than one row match the query, only the first row is returned.
*
* @param poolName The name of the connection pool to query against
* @param sqlKey The SQL Key found in a properties file corresponding to the desired SQL statement value
* @param params The replacement parameters
* @param beanType The Class of the desired return Object matching the table
* @return The Object
* @throws SQLStatementNotFoundException if an SQL statement could not be found for the given sqlKey String
*/
public static T queryBeanSQLKey(String poolName, String sqlKey, Class beanType, Object[] params) {
String sql = YANK_POOL_MANAGER.getMergedSqlProperties().getProperty(sqlKey);
if (sql == null || sql.equalsIgnoreCase("")) {
throw new SQLStatementNotFoundException();
} else {
return queryBean(poolName, sql, beanType, params);
}
}
/**
* Return just one Bean given an SQL statement. If more than one row match the query, only the first row is returned using the default connection
* pool.
*
* @param sql The SQL statement
* @param params The replacement parameters
* @param beanType The Class of the desired return Object matching the table
* @return The Object
*/
public static T queryBean(String sql, Class beanType, Object[] params) {
return queryBean(YankPoolManager.DEFAULT_POOL_NAME, sql, beanType, params);
}
/**
* Return just one Bean given an SQL statement. If more than one row match the query, only the first row is returned.
*
* @param poolName The name of the connection pool to query against
* @param sql The SQL statement
* @param params The replacement parameters
* @param beanType The Class of the desired return Object matching the table
* @return The Object
*/
public static T queryBean(String poolName, String sql, Class beanType, Object[] params) {
T returnObject = null;
try {
BeanHandler resultSetHandler = new BeanHandler(beanType, new BasicRowProcessor(new YankBeanProcessor(beanType)));
returnObject = new QueryRunner(YANK_POOL_MANAGER.getConnectionPool(poolName)).query(sql, resultSetHandler, params);
} catch (SQLException e) {
handleSQLException(e);
}
return returnObject;
}
// ////// Object List QUERY //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/**
* Return a List of Beans given a SQL Key using an SQL statement matching the sqlKey String in a properties file loaded via
* Yank.addSQLStatements(...) using the default connection pool.
*
* @param sqlKey The SQL Key found in a properties file corresponding to the desired SQL statement value
* @param beanType The Class of the desired return Objects matching the table
* @param params The replacement parameters
* @return The List of Objects
* @throws SQLStatementNotFoundException if an SQL statement could not be found for the given sqlKey String
*/
public static List queryBeanListSQLKey(String sqlKey, Class beanType, Object[] params) {
return queryBeanListSQLKey(YankPoolManager.DEFAULT_POOL_NAME, sqlKey, beanType, params);
}
/**
* Return a List of Beans given a SQL Key using an SQL statement matching the sqlKey String in a properties file loaded via
* Yank.addSQLStatements(...).
*
* @param poolName The name of the connection pool to query against
* @param sqlKey The SQL Key found in a properties file corresponding to the desired SQL statement value
* @param beanType The Class of the desired return Objects matching the table
* @param params The replacement parameters
* @return The List of Objects
* @throws SQLStatementNotFoundException if an SQL statement could not be found for the given sqlKey String
*/
public static List queryBeanListSQLKey(String poolName, String sqlKey, Class beanType, Object[] params) {
String sql = YANK_POOL_MANAGER.getMergedSqlProperties().getProperty(sqlKey);
if (sql == null || sql.equalsIgnoreCase("")) {
throw new SQLStatementNotFoundException();
} else {
return queryBeanList(poolName, sql, beanType, params);
}
}
/**
* Return a List of Beans given an SQL statement using the default connection pool.
*
* @param sql The SQL statement
* @param beanType The Class of the desired return Objects matching the table
* @param params The replacement parameters
* @return The List of Objects
*/
public static List queryBeanList(String sql, Class beanType, Object[] params) {
return queryBeanList(YankPoolManager.DEFAULT_POOL_NAME, sql, beanType, params);
}
/**
* Return a List of Beans given an SQL statement
*
* @param poolName The name of the connection pool to query against
* @param sql The SQL statement
* @param beanType The Class of the desired return Objects matching the table
* @param params The replacement parameters
* @return The List of Objects
*/
public static List queryBeanList(String poolName, String sql, Class beanType, Object[] params) {
List returnList = null;
try {
BeanListHandler resultSetHandler = new BeanListHandler(beanType, new BasicRowProcessor(new YankBeanProcessor(beanType)));
returnList = new QueryRunner(YANK_POOL_MANAGER.getConnectionPool(poolName)).query(sql, resultSetHandler, params);
} catch (SQLException e) {
handleSQLException(e);
}
return returnList;
}
// ////// Column List QUERY //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/**
* Return a List of Objects from a single table column given a SQL Key using an SQL statement matching the sqlKey String in a properties file loaded
* via Yank.addSQLStatements(...) using the default connection pool.
*
* @param sqlKey The SQL Key found in a properties file corresponding to the desired SQL statement value
* @param params The replacement parameters
* @param columnType The Class of the desired return Objects matching the table
* @return The Column as a List
* @throws SQLStatementNotFoundException if an SQL statement could not be found for the given sqlKey String
*/
public static List queryColumnSQLKey(String sqlKey, String columnName, Class columnType, Object[] params) {
return queryColumnSQLKey(YankPoolManager.DEFAULT_POOL_NAME, sqlKey, columnName, columnType, params);
}
/**
* Return a List of Objects from a single table column given a SQL Key using an SQL statement matching the sqlKey String in a properties file loaded
* via Yank.addSQLStatements(...).
*
* @param poolName The name of the connection pool to query against
* @param sqlKey The SQL Key found in a properties file corresponding to the desired SQL statement value
* @param params The replacement parameters
* @param columnType The Class of the desired return Objects matching the table
* @return The Column as a List
* @throws SQLStatementNotFoundException if an SQL statement could not be found for the given sqlKey String
*/
public static List queryColumnSQLKey(String poolName, String sqlKey, String columnName, Class columnType, Object[] params) {
String sql = YANK_POOL_MANAGER.getMergedSqlProperties().getProperty(sqlKey);
if (sql == null || sql.equalsIgnoreCase("")) {
throw new SQLStatementNotFoundException();
} else {
return queryColumn(poolName, sql, columnName, columnType, params);
}
}
/**
* Return a List of Objects from a single table column given an SQL statement using the default connection pool.
*
* @param
* @param sql The SQL statement
* @param params The replacement parameters
* @param columnType The Class of the desired return Objects matching the table
* @return The Column as a List
*/
public static List queryColumn(String sql, String columnName, Class columnType, Object[] params) {
return queryColumn(YankPoolManager.DEFAULT_POOL_NAME, sql, columnName, columnType, params);
}
/**
* Return a List of Objects from a single table column given an SQL statement
*
* @param poolName The name of the connection pool to query against
* @param sql The SQL statement
* @param params The replacement parameters
* @param columnType The Class of the desired return Objects matching the table
* @return The Column as a List
*/
public static List queryColumn(String poolName, String sql, String columnName, Class columnType, Object[] params) {
List returnList = null;
try {
ColumnListHandler resultSetHandler = new ColumnListHandler(columnName);
returnList = new QueryRunner(YANK_POOL_MANAGER.getConnectionPool(poolName)).query(sql, resultSetHandler, params);
} catch (SQLException e) {
handleSQLException(e);
}
return returnList;
}
// ////// OBJECT[] LIST QUERY //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/**
* Return a List of generic Object[]s given a SQL Key using an SQL statement matching the sqlKey String in a properties file loaded via
* Yank.addSQLStatements(...) using the default connection pool.
*
* @param sqlKey The SQL Key found in a properties file corresponding to the desired SQL statement value
* @param params The replacement parameters
* @return The List of generic Object[]s
* @throws SQLStatementNotFoundException if an SQL statement could not be found for the given sqlKey String
*/
public static List