io.qt.sql.jdbc.QJdbcSqlResult Maven / Gradle / Ivy
The newest version!
/****************************************************************************
**
** Copyright (C) 2009-2024 Dr. Peter Droste, Omix Visualization GmbH & Co. KG. All rights reserved.
**
** This file is part of Qt Jambi.
**
** ** $BEGIN_LICENSE$
** GNU Lesser General Public License Usage
** This file may be used under the terms of the GNU Lesser
** General Public License version 2.1 as published by the Free Software
** Foundation and appearing in the file LICENSE.LGPL included in the
** packaging of this file. Please review the following information to
** ensure the GNU Lesser General Public License version 2.1 requirements
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
** General Public License version 3.0 as published by the Free Software
** Foundation and appearing in the file LICENSE.GPL included in the
** packaging of this file. Please review the following information to
** ensure the GNU General Public License version 3.0 requirements will be
** met: http://www.gnu.org/copyleft/gpl.html.
** $END_LICENSE$
**
** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
**
****************************************************************************/
package io.qt.sql.jdbc;
import static io.qt.core.QObject.tr;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.sql.Statement;
import io.qt.sql.QSql;
import io.qt.sql.QSqlDriver;
import io.qt.sql.QSqlError;
import io.qt.sql.QSqlField;
import io.qt.sql.QSqlRecord;
import io.qt.sql.QSqlResult;
class QJdbcSqlResult extends QSqlResult
{
public QJdbcSqlResult(QSqlDriver db, Connection c)
{
super(db);
this.connection = c;
try(Statement st = connection.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,
ResultSet.CONCUR_READ_ONLY)){
} catch (SQLException ex) {
if(ex.getMessage().contains("TYPE_FORWARD_ONLY")) {
this.setForwardOnly(true);
}
} catch (Throwable ex) {
}
}
public Object handle()
{
return statement;
}
@Override
protected Object data(int i)
{
try {
return QJdbcSqlUtil.javaToQt(resultSet.getObject(i + 1));
} catch (SQLException ex) {
setError(ex, tr("Unable to retrieve data"), QSqlError.ErrorType.StatementError);
}
return null;
}
@Override
protected boolean isNull(int i)
{
try {
resultSet.getObject(i + 1);
return resultSet.wasNull();
} catch (SQLException ex) {
setError(ex, tr("Unable to retrieve null status"), QSqlError.ErrorType.StatementError);
}
return false;
}
@Override
protected boolean fetchPrevious()
{
if (resultSet == null)
return false;
try {
if (!resultSet.previous())
return false;
} catch (SQLException ex) {
setError(ex, tr("Unable to fetch previous"), QSqlError.ErrorType.StatementError);
return false;
}
setAt(at() - 1);
return true;
}
@Override
protected boolean fetchNext()
{
if (resultSet == null)
return false;
try {
if (!resultSet.next()) {
return false;
}
} catch (SQLException ex) {
setError(ex, tr("Unable to fetch next"), QSqlError.ErrorType.StatementError);
return false;
}
setAt(at() + 1);
return true;
}
@Override
protected boolean fetch(int i)
{
if (resultSet == null)
return false;
try {
if (resultSet.absolute(i + 1)) {
setAt(i);
return true;
}
} catch (SQLException ex) {
setError(ex, tr("Unable to fetch row"), QSqlError.ErrorType.StatementError);
return false;
}
return false;
}
@Override
protected boolean fetchFirst()
{
if (resultSet == null)
return false;
try {
if (resultSet.first()) {
setAt(0);
return true;
}
} catch (SQLException ex) {
setError(ex, tr("Unable to fetch first"), QSqlError.ErrorType.StatementError);
return false;
}
return false;
}
protected boolean fetchLast()
{
if (resultSet == null)
return false;
try {
if (resultSet.last()) {
setAt(resultSet.getRow() - 1);
}
} catch (SQLException ex) {
setError(ex, tr("Unable to fetch last"), QSqlError.ErrorType.StatementError);
return false;
}
return false;
}
@Override
protected int size()
{
return -1;
}
@Override
protected int numRowsAffected()
{
return updateCount;
}
@Override
protected boolean reset(String query)
{
clearStatement();
// create a new statement
try {
if (isForwardOnly()) {
statement = connection.createStatement(ResultSet.TYPE_FORWARD_ONLY,
ResultSet.CONCUR_READ_ONLY);
} else {
try {
statement = connection.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,
ResultSet.CONCUR_READ_ONLY);
} catch (SQLException ex) {
if(ex.getMessage().contains("TYPE_FORWARD_ONLY")) {
setForwardOnly(true);
statement = connection.createStatement(ResultSet.TYPE_FORWARD_ONLY,
ResultSet.CONCUR_READ_ONLY);
}else {
throw ex;
}
}
}
} catch (SQLException ex) {
setError(ex, tr("Unable to create statement"), QSqlError.ErrorType.StatementError);
statement = null;
return false;
}
// execute the query
boolean executionResult;
try {
try {
if (connection.getMetaData().supportsGetGeneratedKeys())
executionResult = statement.execute(query, Statement.RETURN_GENERATED_KEYS);
else
executionResult = statement.execute(query, Statement.NO_GENERATED_KEYS);
} catch (SQLException ex) {
if(ex.getMessage().contains("not implemented")) {
executionResult = statement.execute(query);
}else {
throw ex;
}
}
} catch (SQLException ex) {
setError(ex, tr("Unable to execute query"), QSqlError.ErrorType.ConnectionError);
resultSet = null;
return false;
}
return getResultSet(executionResult);
}
@Override
protected boolean prepare(String query)
{
clearStatement();
// create a new prepared statement
try {
if (isForwardOnly()) {
try {
statement = connection.prepareStatement(query, ResultSet.TYPE_FORWARD_ONLY,
ResultSet.CONCUR_UPDATABLE);
} catch (SQLException ex) {
if(ex.getMessage().contains("CONCUR_READ_ONLY")) {
statement = connection.prepareStatement(query, ResultSet.TYPE_FORWARD_ONLY,
ResultSet.CONCUR_READ_ONLY);
}
}
} else {
try {
statement = connection.prepareStatement(query, ResultSet.TYPE_SCROLL_INSENSITIVE,
ResultSet.CONCUR_UPDATABLE);
} catch (SQLException ex) {
if(ex.getMessage().contains("CONCUR_READ_ONLY")) {
try {
statement = connection.prepareStatement(query, ResultSet.TYPE_SCROLL_INSENSITIVE,
ResultSet.CONCUR_READ_ONLY);
} catch (SQLException e) {
if(ex.getMessage().contains("TYPE_FORWARD_ONLY")) {
setForwardOnly(true);
statement = connection.prepareStatement(query, ResultSet.TYPE_FORWARD_ONLY,
ResultSet.CONCUR_READ_ONLY);
}else {
throw ex;
}
}
}else if(ex.getMessage().contains("TYPE_FORWARD_ONLY")) {
setForwardOnly(true);
statement = connection.prepareStatement(query, ResultSet.TYPE_FORWARD_ONLY,
ResultSet.CONCUR_READ_ONLY);
}else {
throw ex;
}
}
}
} catch (SQLException ex) {
setError(ex, tr("Unable to prepare statement"), QSqlError.ErrorType.StatementError);
statement = null;
return false;
}
return true;
}
@Override
protected boolean exec()
{
if ((statement == null) || !(statement instanceof PreparedStatement))
return false;
PreparedStatement ps = (PreparedStatement)statement;
try {
for (int i = 0; i