Skip to content

Commit

Permalink
Fixes to avoid crash with duplicate ids when adding userpoints or log…
Browse files Browse the repository at this point in the history
…book entries.

albar965/littlenavmap#985
  • Loading branch information
albar965 committed Jan 15, 2023
1 parent 81f81d6 commit d8cac42
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 29 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.txt
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ This text is partially Markdown, hence sometimes strange formatting.
turn anticipation display in Little Navmap.
* Corrected NAT track download address to notams.aim.faa.gov/nat.html to avoid error messages on
download.
* Fixes to avoid crash with duplicate ids when adding userpoints or logbook entries. #985

===============================================================================

Expand Down
7 changes: 7 additions & 0 deletions src/sql/datamanagerbase.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,8 @@ void DataManagerBase::setActions(QAction *undoActionParam, QAction *redoActionPa

void DataManagerBase::insertOneRecord(SqlRecord record, int id)
{
initCurrentId();

if(id == -1)
// Use generated id if not given
id = getNextId();
Expand All @@ -194,6 +196,8 @@ void DataManagerBase::insertOneRecord(SqlRecord record, int id)

void DataManagerBase::insertRecords(sql::SqlRecordList records)
{
initCurrentId();

// Insert id in records if id column is 0 or missing
for(SqlRecord& record : records)
updateIdColumn(record, getNextId());
Expand Down Expand Up @@ -559,6 +563,9 @@ void DataManagerBase::postUndoBulkInsert()
syncCurrentUndoGroupToDb();
updateUndoRedoActions();
}

// get current (max) id from table
initCurrentId();
}

void DataManagerBase::preUndoDeleteAll()
Expand Down
44 changes: 15 additions & 29 deletions src/sql/sqlrecord.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*****************************************************************************
* Copyright 2015-2020 Alexander Barthel [email protected]
* Copyright 2015-2023 Alexander Barthel [email protected]
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
Expand Down Expand Up @@ -34,17 +34,15 @@ QVariant SqlRecord::value(int i) const
{
QVariant retval = sqlRecord.value(i);
if(!retval.isValid())
throw SqlException("SqlRecord::value(): Value index " + QString::number(
i) + " does not exist in query \"" + queryString + "\"");
throw SqlException("SqlRecord::value(): Value index " + QString::number(i) + " does not exist in query \"" + queryString + "\"");
return retval;
}

QVariant SqlRecord::value(const QString& name) const
{
QVariant retval = sqlRecord.value(name);
if(!retval.isValid())
throw SqlException("SqlRecord::value(): Value name \"" +
name + "\" does not exist in query \"" + queryString + "\"");
throw SqlException("SqlRecord::value(): Value name \"" + name + "\" does not exist in query \"" + queryString + "\"");
return retval;

}
Expand All @@ -67,71 +65,63 @@ QDateTime SqlRecord::valueDateTime(const QString& name, const QDateTime& default
bool SqlRecord::isNull(int i) const
{
if(sqlRecord.fieldName(i).isEmpty())
throw SqlException("SqlRecord::isNull(): Field index " + QString::number(i) +
" does not exist in query \"" + queryString + "\"");
throw SqlException("SqlRecord::isNull(): Field index " + QString::number(i) + " does not exist in query \"" + queryString + "\"");

return sqlRecord.isNull(i);
}

bool SqlRecord::isNull(const QString& name) const
{
if(sqlRecord.indexOf(name) == -1)
throw SqlException("SqlRecord::indexOf(): Field name \"" +
name + "\" does not exist in query \"" + queryString + "\"");
throw SqlException("SqlRecord::indexOf(): Field name \"" + name + "\" does not exist in query \"" + queryString + "\"");
return sqlRecord.isNull(name);
}

int SqlRecord::indexOf(const QString& name) const
{
int retval = sqlRecord.indexOf(name);
if(retval == -1)
throw SqlException("SqlRecord::indexOf(): Field name \"" +
name + "\" does not exist in query \"" + queryString + "\"");
throw SqlException("SqlRecord::indexOf(): Field name \"" + name + "\" does not exist in query \"" + queryString + "\"");
return retval;
}

QString SqlRecord::fieldName(int i) const
{
QString retval = sqlRecord.fieldName(i);
if(retval.isEmpty())
throw SqlException("SqlRecord::fieldName(): Field index " + QString::number(i) +
" does not exist in query \"" + queryString + "\"");
throw SqlException("SqlRecord::fieldName(): Field index " + QString::number(i) + " does not exist in query \"" + queryString + "\"");
return retval;
}

QVariant::Type SqlRecord::fieldType(int i) const
{
QSqlField retval = sqlRecord.field(i);
if(sqlRecord.fieldName(i).isEmpty())
throw SqlException("SqlRecord::fieldType(): Field index " + QString::number(i) +
" does not exist in query \"" + queryString + "\"");
throw SqlException("SqlRecord::fieldType(): Field index " + QString::number(i) + " does not exist in query \"" + queryString + "\"");
return retval.type();
}

QVariant::Type SqlRecord::fieldType(const QString& name) const
{
QSqlField retval = sqlRecord.field(name);
if(!contains(name))
throw SqlException("SqlRecord::fieldType(): Field name \"" +
name + "\" does not exist in query \"" + queryString + "\"");
throw SqlException("SqlRecord::fieldType(): Field name \"" + name + "\" does not exist in query \"" + queryString + "\"");

return retval.type();
}

bool SqlRecord::isGenerated(int i) const
{
if(sqlRecord.fieldName(i).isEmpty())
throw SqlException("SqlRecord::isGenerated(): Field index " + QString::number(i) +
" does not exist in query \"" + queryString + "\"");
throw SqlException("SqlRecord::isGenerated(): Field index " + QString::number(i) + " does not exist in query \"" + queryString + "\"");

return sqlRecord.isGenerated(i);
}

bool SqlRecord::isGenerated(const QString& name) const
{
if(!contains(name))
throw SqlException("SqlRecord::isGenerated(): Field name \"" +
name + "\" does not exist in query \"" + queryString + "\"");
throw SqlException("SqlRecord::isGenerated(): Field name \"" + name + "\" does not exist in query \"" + queryString + "\"");
return sqlRecord.isGenerated(name);
}

Expand Down Expand Up @@ -243,32 +233,28 @@ QVariantList SqlRecord::values() const
void SqlRecord::setValue(int i, const QVariant& val)
{
if(sqlRecord.fieldName(i).isEmpty())
throw SqlException("SqlRecord::setValue(): Field index " + QString::number(i) +
" does not exist in query \"" + queryString + "\"");
throw SqlException("SqlRecord::setValue(): Field index " + QString::number(i) + " does not exist in query \"" + queryString + "\"");
sqlRecord.setValue(i, val);
}

void SqlRecord::setValue(const QString& name, const QVariant& val)
{
if(sqlRecord.indexOf(name) == -1)
throw SqlException("SqlRecord::setValue(): Field name \"" +
name + "\" does not exist in query \"" + queryString + "\"");
throw SqlException("SqlRecord::setValue(): Field name \"" + name + "\" does not exist in query \"" + queryString + "\"");
sqlRecord.setValue(name, val);
}

void SqlRecord::setNull(int i)
{
if(sqlRecord.fieldName(i).isEmpty())
throw SqlException("SqlRecord::setNull(): Field index " + QString::number(i) +
" does not exist in query \"" + queryString + "\"");
throw SqlException("SqlRecord::setNull(): Field index " + QString::number(i) + " does not exist in query \"" + queryString + "\"");
sqlRecord.setNull(i);
}

void SqlRecord::setNull(const QString& name)
{
if(sqlRecord.indexOf(name) == -1)
throw SqlException("SqlRecord::setNull(): Field name \"" +
name + "\" does not exist in query \"" + queryString + "\"");
throw SqlException("SqlRecord::setNull(): Field name \"" + name + "\" does not exist in query \"" + queryString + "\"");
sqlRecord.setNull(name);
}

Expand Down

0 comments on commit d8cac42

Please sign in to comment.