From 12b6efb189f9eabe4727c429f289b7aef10d3a46 Mon Sep 17 00:00:00 2001 From: Bill Somerville Date: Mon, 21 Jan 2019 13:35:18 +0000 Subject: [PATCH] Make sure database models are synchronized before using QSqlTableModel::rowCount() This is necessary as the cached model will not reflect a correct row count if there are un-fetched rows. --- models/CabrilloLog.cpp | 21 +++++++++++++++++++-- models/FoxLog.cpp | 22 ++++++++++++++++++++-- 2 files changed, 39 insertions(+), 4 deletions(-) diff --git a/models/CabrilloLog.cpp b/models/CabrilloLog.cpp index aaedd3bd1..3a30380e3 100644 --- a/models/CabrilloLog.cpp +++ b/models/CabrilloLog.cpp @@ -46,10 +46,25 @@ CabrilloLog::impl::impl (Configuration const * configuration) } SQL_error_check (dupe_query_, &QSqlQuery::prepare, - "SELECT COUNT(*) FROM cabrillo_log WHERE call = :call AND band = :band"); + "SELECT " + " COUNT(*) " + " FROM " + " cabrillo_log " + " WHERE " + " call = :call " + " AND band = :band"); SQL_error_check (export_query_, &QSqlQuery::prepare, - "SELECT frequency, \"when\", exchange_sent, call, exchange_rcvd FROM cabrillo_log ORDER BY \"when\""); + "SELECT " + " frequency" + " , \"when\"" + " , exchange_sent" + " , call" + " , exchange_rcvd" + " FROM " + " cabrillo_log " + " ORDER BY " + " \"when\""); setEditStrategy (QSqlTableModel::OnFieldChange); setTable ("cabrillo_log"); @@ -138,6 +153,8 @@ bool CabrilloLog::dupe (Frequency frequency, QString const& call) const void CabrilloLog::reset () { + // synchronize model + while (m_->canFetchMore ()) m_->fetchMore (); if (m_->rowCount ()) { m_->setEditStrategy (QSqlTableModel::OnManualSubmit); diff --git a/models/FoxLog.cpp b/models/FoxLog.cpp index 504ddd2a8..773a5308f 100644 --- a/models/FoxLog.cpp +++ b/models/FoxLog.cpp @@ -46,10 +46,26 @@ FoxLog::impl::impl (Configuration const * configuration) } SQL_error_check (dupe_query_, &QSqlQuery::prepare, - "SELECT COUNT(*) FROM fox_log WHERE call = :call AND band = :band"); + "SELECT " + " COUNT(*) " + " FROM " + " fox_log " + " WHERE " + " call = :call " + " AND band = :band"); SQL_error_check (export_query_, &QSqlQuery::prepare, - "SELECT band, \"when\", call, grid, report_sent, report_rcvd FROM fox_log ORDER BY \"when\""); + "SELECT " + " band" + " , \"when\"" + " , call" + " , grid" + " , report_sent" + " , report_rcvd " + " FROM " + " fox_log " + " ORDER BY " + " \"when\""); setEditStrategy (QSqlTableModel::OnFieldChange); setTable ("fox_log"); @@ -141,6 +157,8 @@ bool FoxLog::dupe (QString const& call, QString const& band) const void FoxLog::reset () { + // synchronize model + while (m_->canFetchMore ()) m_->fetchMore (); if (m_->rowCount ()) { m_->setEditStrategy (QSqlTableModel::OnManualSubmit);