tdf#103685: "Commands out of sync" when connecting to MySQL using direct

Thanks to Lionel for his great help

Change-Id: Ifcc1d72cca29c031f31da203cd1e3302ea0ea3e3
Reviewed-on: https://gerrit.libreoffice.org/42688
Tested-by: Jenkins <ci@libreoffice.org>
Reviewed-by: Lionel Elie Mamane <lionel@mamane.lu>
This commit is contained in:
Julien Nabet
2017-09-23 13:26:58 +02:00
committed by Lionel Elie Mamane
parent 722161e264
commit 444730a67d
3 changed files with 68 additions and 39 deletions

View File

@@ -29,6 +29,7 @@
#include <rtl/strbuf.hxx> #include <rtl/strbuf.hxx>
#include <com/sun/star/sdbc/SQLException.hpp> #include <com/sun/star/sdbc/SQLException.hpp>
#include <com/sun/star/sdbc/XRow.hpp> #include <com/sun/star/sdbc/XRow.hpp>
#include <com/sun/star/sdbc/XMultipleResults.hpp>
namespace dbaui namespace dbaui
{ {
@@ -183,25 +184,75 @@ namespace dbaui
::osl::MutexGuard aGuard(m_aMutex); ::osl::MutexGuard aGuard(m_aMutex);
OUString sStatus; OUString sStatus;
css::uno::Reference< css::sdbc::XResultSet > xResultSet;
// clear the output box
m_pOutput->SetText(OUString());
try try
{ {
// create a statement // create a statement
Reference< XStatement > xStatement = m_xConnection->createStatement(); Reference< XStatement > xStatement = m_xConnection->createStatement();
OSL_ENSURE(xStatement.is(), "DirectSQLDialog::implExecuteStatement: no statement returned by the connection!"); css::uno::Reference< css::sdbc::XMultipleResults > xMR ( xStatement, UNO_QUERY );
// clear the output box if (xMR.is())
m_pOutput->SetText(OUString());
if (xStatement.is())
{ {
if (_rStatement.toAsciiUpperCase().startsWith("SELECT") && m_pShowOutput->IsChecked()) bool hasRS = xStatement->execute(_rStatement);
if(hasRS)
{
css::uno::Reference< css::sdbc::XResultSet > xRS (xMR->getResultSet());
if (m_pShowOutput->IsChecked())
display(xRS);
}
else
addOutputText(OUString::number(xMR->getUpdateCount()) + " rows updated\n");
while ((hasRS=xMR->getMoreResults()) || (xMR->getUpdateCount() != -1))
{
if(hasRS)
{
css::uno::Reference< css::sdbc::XResultSet > xRS (xMR->getResultSet());
if (m_pShowOutput->IsChecked())
display(xRS);
}
}
}
else
{
if (_rStatement.toAsciiUpperCase().startsWith("SELECT"))
{
css::uno::Reference< css::sdbc::XResultSet > xRS = xStatement->executeQuery(_rStatement);
if(m_pShowOutput->IsChecked())
display(xRS);
}
else
{
sal_Int32 resultCount = xStatement->executeUpdate(_rStatement);
addOutputText(OUString::number(resultCount) + " rows updated\n");
}
}
// successful
sStatus = DBA_RES(STR_COMMAND_EXECUTED_SUCCESSFULLY);
// dispose the statement
::comphelper::disposeComponent(xStatement);
}
catch(const SQLException& e)
{
sStatus = e.Message;
}
catch( const Exception& )
{
DBG_UNHANDLED_EXCEPTION();
}
// add the status text
addStatusText(sStatus);
}
void DirectSQLDialog::display(css::uno::Reference< css::sdbc::XResultSet > xRS)
{ {
// execute it as a query
xResultSet = xStatement->executeQuery(_rStatement);
// get a handle for the rows // get a handle for the rows
css::uno::Reference< css::sdbc::XRow > xRow( xResultSet, css::uno::UNO_QUERY ); css::uno::Reference< css::sdbc::XRow > xRow( xRS, css::uno::UNO_QUERY );
// work through each of the rows // work through each of the rows
while (xResultSet->next()) while (xRS->next())
{ {
// initialise the output line for each row // initialise the output line for each row
OUString out(""); OUString out("");
@@ -223,29 +274,6 @@ namespace dbaui
// report the output // report the output
addOutputText(out); addOutputText(out);
} }
} else {
// execute it
xStatement->execute(_rStatement);
}
}
// successful
sStatus = DBA_RES(STR_COMMAND_EXECUTED_SUCCESSFULLY);
// dispose the statement
::comphelper::disposeComponent(xStatement);
}
catch(const SQLException& e)
{
sStatus = e.Message;
}
catch( const Exception& )
{
DBG_UNHANDLED_EXCEPTION();
}
// add the status text
addStatusText(sStatus);
} }
void DirectSQLDialog::addStatusText(const OUString& _rMessage) void DirectSQLDialog::addStatusText(const OUString& _rMessage)

View File

@@ -106,6 +106,9 @@ namespace dbaui
/// adds a status text to the output list /// adds a status text to the output list
void addOutputText(const OUString& _rMessage); void addOutputText(const OUString& _rMessage);
/// displays resultset
void display(css::uno::Reference< css::sdbc::XResultSet > xRS);
#ifdef DBG_UTIL #ifdef DBG_UTIL
const sal_Char* impl_CheckInvariants() const; const sal_Char* impl_CheckInvariants() const;
#endif #endif

View File

@@ -175,7 +175,7 @@ Reference< XConnection > SAL_CALL OCommonStatement::getConnection()
sal_Int32 SAL_CALL OCommonStatement::getUpdateCount() sal_Int32 SAL_CALL OCommonStatement::getUpdateCount()
{ {
return 0; return cppStatement->getUpdateCount();
} }
Any SAL_CALL OStatement::queryInterface(const Type & rType) Any SAL_CALL OStatement::queryInterface(const Type & rType)
@@ -238,9 +238,7 @@ sal_Bool SAL_CALL OCommonStatement::getMoreResults()
MutexGuard aGuard(m_aMutex); MutexGuard aGuard(m_aMutex);
checkDisposed(rBHelper.bDisposed); checkDisposed(rBHelper.bDisposed);
// if your driver supports more than only one resultset return cppStatement->getMoreResults();
// and has one more at this moment return true
return false;
} }
Any SAL_CALL OCommonStatement::getWarnings() Any SAL_CALL OCommonStatement::getWarnings()