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.
package com.github.davidmoten.rx.jdbc;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.Reader;
import java.io.StringReader;
import java.io.Writer;
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.sql.Blob;
import java.sql.Clob;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.sql.Time;
import java.sql.Timestamp;
import java.sql.Types;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Collections;
import java.util.List;
import org.apache.commons.io.IOUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import rx.functions.Func1;
import com.github.davidmoten.rx.jdbc.QuerySelect.Builder;
/**
* Utility methods.
*/
public final class Util {
/**
* Private constructor to prevent instantiation.
*/
private Util() {
// prevent instantiation
}
/**
* Logger.
*/
private static final Logger log = LoggerFactory.getLogger(Util.class);
/**
* Count the number of JDBC parameters in a sql statement.
*
* @param sql
* @return
*/
static int parametersCount(String sql) {
// TODO account for ? characters in string constants
return countOccurrences(sql, '?');
}
/**
* Returns the number of occurrences of a character in a string.
*
* @param haystack
* @param needle
* @return
*/
private static int countOccurrences(String haystack, char needle) {
int count = 0;
for (int i = 0; i < haystack.length(); i++) {
if (haystack.charAt(i) == needle)
count++;
}
return count;
}
/**
* Cancels then closes a {@link PreparedStatement} and logs exceptions
* without throwing. Does nothing if ps is null.
*
* @param ps
*/
static void closeQuietly(PreparedStatement ps) {
try {
boolean isClosed;
try {
if (ps != null)
isClosed = ps.isClosed();
else
isClosed = true;
} catch (SQLException e) {
log.debug(e.getMessage());
isClosed = true;
}
if (ps != null && !isClosed) {
try {
ps.cancel();
log.debug("cancelled {}", ps);
} catch (SQLException e) {
log.debug(e.getMessage());
}
ps.close();
log.debug("closed {}", ps);
}
} catch (SQLException e) {
log.debug(e.getMessage(), e);
} catch (RuntimeException e) {
log.debug(e.getMessage(), e);
}
}
/**
* Closes a {@link Connection} and logs exceptions without throwing. Does
* nothing if connection is null.
*
* @param connection
*/
static void closeQuietly(Connection connection) {
try {
if (connection != null && !connection.isClosed()) {
connection.close();
log.debug("closed {}", connection);
}
} catch (SQLException e) {
log.debug(e.getMessage(), e);
} catch (RuntimeException e) {
log.debug(e.getMessage(), e);
}
}
/**
* Closes a {@link Connection} only if the connection is in auto commit mode
* and logs exceptions without throwing. Does nothing if connection is null.
*
* @param connection
*/
static void closeQuietlyIfAutoCommit(Connection connection) {
try {
if (connection != null && !connection.isClosed() && connection.getAutoCommit())
closeQuietly(connection);
} catch (SQLException e) {
throw new RuntimeException(e);
}
}
/**
* Commits a {@link Connection} and logs exceptions without throwing.
*
* @param connection
*/
static void commit(Connection connection) {
if (connection != null)
try {
connection.commit();
log.debug("committed");
} catch (SQLException e) {
throw new RuntimeException(e);
}
}
/**
* Rolls back a {@link Connection} and logs exceptions without throwing.
*
* @param connection
*/
static void rollback(Connection connection) {
if (connection != null)
try {
connection.rollback();
log.debug("rolled back");
} catch (SQLException e) {
throw new RuntimeException(e);
}
}
/**
* Closes a {@link ResultSet} and logs exceptions without throwing.
*
* @param rs
*/
static void closeQuietly(ResultSet rs) {
try {
if (rs != null && !rs.isClosed()) {
rs.close();
log.debug("closed {}", rs);
}
} catch (SQLException e) {
log.debug(e.getMessage(), e);
} catch (RuntimeException e) {
log.debug(e.getMessage(), e);
}
}
/**
* Returns true if and only if {@link Connection} is in auto commit mode.
*
* @param con
* @return
*/
static boolean isAutoCommit(Connection con) {
try {
return con.getAutoCommit();
} catch (SQLException e) {
throw new RuntimeException(e);
}
}
/**
* Returns the empty list whenever called.
*/
static Func1> TO_EMPTY_PARAMETER_LIST = new Func1>() {
@Override
public List call(Integer n) {
return Collections.emptyList();
};
};
/**
* Returns a function that converts the ResultSet column values into
* parameters to the constructor (with number of parameters equals the
* number of columns) of type cls then returns an instance of
* type cls. See {@link Builder#autoMap(Class)}.
*
* @param cls
* @return
*/
static Func1 autoMap(final Class cls) {
return new Func1() {
@Override
public T call(ResultSet rs) {
return autoMap(rs, cls);
}
};
}
/**
* Converts the ResultSet column values into parameters to the constructor
* (with number of parameters equals the number of columns) of type
* T then returns an instance of type T. See See
* {@link Builder#autoMap(Class)}.
*
* @param cls
* the class of the resultant instance
* @return an automapped instance
*/
@SuppressWarnings("unchecked")
static T autoMap(ResultSet rs, Class cls) {
try {
int n = rs.getMetaData().getColumnCount();
for (Constructor> c : cls.getDeclaredConstructors()) {
if (n == c.getParameterTypes().length) {
return autoMap(rs, (Constructor) c);
}
}
throw new RuntimeException("constructor with number of parameters=" + n
+ " not found in " + cls);
} catch (SQLException e) {
throw new RuntimeException(e);
}
}
/**
* Converts the ResultSet column values into parameters to the given
* constructor (with number of parameters equals the number of columns) of
* type T then returns an instance of type T. See
* See {@link Builder#autoMap(Class)}.
*
* @param rs
* the result set row
* @param c
* constructor to use for instantiation
* @return automapped instance
*/
private static T autoMap(ResultSet rs, Constructor c) {
Class>[] types = c.getParameterTypes();
List