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.
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You 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 com.arakelian.jdbc.utils;
import java.io.Closeable;
import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.ParameterMetaData;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.sql.Statement;
import java.sql.Types;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
import java.util.NoSuchElementException;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.arakelian.jdbc.conn.ConnectionFactory;
import com.arakelian.jdbc.handler.ResultSetHandler;
import com.arakelian.jdbc.handler.ScalarHandler;
import com.arakelian.jdbc.model.Field;
import com.arakelian.jdbc.model.ImmutableField;
import com.arakelian.jdbc.model.ImmutableIndexField;
import com.arakelian.jdbc.model.Index;
import com.arakelian.jdbc.model.IndexField;
import com.arakelian.store.StoreException;
import com.github.rholder.retry.RetryException;
import com.github.rholder.retry.Retryer;
import com.github.rholder.retry.RetryerBuilder;
import com.github.rholder.retry.StopStrategies;
import com.github.rholder.retry.WaitStrategies;
import com.google.common.base.Preconditions;
public class DatabaseUtils {
public static class JdbcIterator implements Iterator, Closeable {
private final ConnectionFactory connectionFactory;
private final String sql;
private final Object[] params;
private final ResultSetHandler rowHandler;
private Connection conn;
private PreparedStatement stmt;
private ResultSet rs;
private ResultSetMetaData rsmd;
private boolean closed;
private T lookahead;
private JdbcIterator(
final ConnectionFactory connectionFactory,
final Connection conn,
final String sql,
final Object[] params,
final ResultSetHandler rowHandler) {
Preconditions.checkArgument(
connectionFactory != null || conn != null,
"connectionFactory or conn must be non-null");
Preconditions.checkArgument(!StringUtils.isEmpty(sql), "sql must be non-empty");
Preconditions.checkArgument(rowHandler != null, "rowHandler must be non-null");
this.connectionFactory = connectionFactory;
this.conn = conn;
this.sql = sql;
this.params = params;
this.rowHandler = rowHandler;
}
@Override
public void close() {
if (closed) {
return;
}
try {
closeQuietly(rs);
closeQuietly(stmt);
closeQuietly(conn);
} finally {
rs = null;
stmt = null;
conn = null;
closed = true;
}
}
@Override
public boolean hasNext() {
return !closed && (lookahead != null || (lookahead = peek()) != null);
}
@Override
public T next() {
if (lookahead != null) {
final T t = lookahead;
lookahead = null;
return t;
}
final T t = peek();
if (closed) {
throw new NoSuchElementException();
}
return t;
}
private T peek() {
if (lookahead != null) {
final T result = lookahead;
lookahead = null;
return result;
}
try {
if (stmt == null && !closed) {
if (conn == null) {
conn = connectionFactory.createConnection();
}
if (LOGGER.isDebugEnabled()) {
LOGGER.debug(DatabaseUtils.toString(null, sql, params));
}
stmt = conn.prepareStatement(sql);
fillStatement(stmt, params);
rs = stmt.executeQuery();
rsmd = rs.getMetaData();
}
final T row = rowHandler.handle(rs, rsmd);
if (rowHandler.wasLast(row)) {
close();
}
return row;
} catch (final SQLException e) {
throw new StoreException(createSqlException(e, sql, params));
}
}
}
/**
* Logger
*/
private static final Logger LOGGER = LoggerFactory.getLogger(DatabaseUtils.class);
/**
* Closes the given connection. This method will quietly log any SQLException that
* may occur but will otherwise ignore it.
*
* @param connection
* connection to close. If a null value is provided, this method has no effect.
*/
public static void closeQuietly(final Connection connection) {
try {
if (connection != null) {
connection.close();
}
} catch (final Throwable ignorable) {
if (LOGGER.isDebugEnabled()) {
LOGGER.debug(ignorable.getMessage(), ignorable);
}
}
}
/**
* Closes the given ResultSet. This method will quietly log any
* SQLException that may occur but will otherwise ignore it.
*
* Note: According to JDBC specification, this has the side-effect of closing the associated
* statement, unless the connection property "retainStatementAfterResultSetClose" is set to true
* (default is false).
*
* @param rs
* ResultSet to close. If a null value is provided, this method has no
* effect.
*/
public static void closeQuietly(final ResultSet rs) {
try {
if (rs != null) {
rs.close();
}
} catch (final Throwable ignorable) {
if (LOGGER.isDebugEnabled()) {
LOGGER.debug(ignorable.getMessage(), ignorable);
}
}
}
/**
* Closes the given statement. This method will quietly log any SQLException that
* may occur but will otherwise ignore it.
*
* @param statement
* statement to close. If a null value is provided, this method has no effect.
*/
public static void closeQuietly(final Statement statement) {
try {
if (statement instanceof PreparedStatement) {
((PreparedStatement) statement).clearParameters();
}
if (statement != null) {
statement.close();
}
} catch (final Throwable ignorable) {
if (LOGGER.isDebugEnabled()) {
LOGGER.debug(ignorable.getMessage(), ignorable);
}
}
}
/**
* Execute a batch INSERT, UPDATE, or DELETE query. The caller is responsible for closing the
* connection.
*
* @param conn
* The connection to the database
* @param sql
* The SQL to execute.
* @param batchParams
* The query replacement parameters.
* @return The number of rows updated.
* @throws SQLException
* if a database error occurs
*/
public static int[] executeBatchUpdate(
final Connection conn,
final String sql,
final Iterator