From 408fbcf8b422ba443d01780c103354d8a16468b7 Mon Sep 17 00:00:00 2001 From: Martin Kleusberg Date: Fri, 6 Feb 2015 14:08:59 +0100 Subject: [PATCH] Use same data type as SQLite for primary keys This improves commit c20c58595a8093622a8928ae539962433eabcbec to definitely make sure the primary key values are stored and handled correctly by simply using the same data type as SQLite itself does. --- src/sqlitedb.cpp | 9 ++++----- src/sqlitedb.h | 10 +++++----- src/sqlitetablemodel.cpp | 4 ++-- 3 files changed, 11 insertions(+), 12 deletions(-) diff --git a/src/sqlitedb.cpp b/src/sqlitedb.cpp index 1a880971..343a912f 100644 --- a/src/sqlitedb.cpp +++ b/src/sqlitedb.cpp @@ -1,6 +1,5 @@ #include "sqlitedb.h" #include "sqlitetablemodel.h" -#include "sqlite.h" #include "CipherDialog.h" #include @@ -578,7 +577,7 @@ bool DBBrowserDB::executeMultiSQL(const QString& statement, bool dirty, bool log return true; } -bool DBBrowserDB::getRow(const QString& sTableName, long rowid, QList& rowdata) +bool DBBrowserDB::getRow(const QString& sTableName, sqlite3_int64 rowid, QList& rowdata) { QString sQuery = QString("SELECT * FROM `%1` WHERE `%2`=%3;").arg(sTableName).arg(getObjectByName(sTableName).table.rowidColumn()).arg(rowid); QByteArray utf8Query = sQuery.toUtf8(); @@ -625,7 +624,7 @@ int64_t DBBrowserDB::max(const sqlb::Table& t, sqlb::FieldPtr field) const return ret; } -QString DBBrowserDB::emptyInsertStmt(const sqlb::Table& t, long pk_value) const +QString DBBrowserDB::emptyInsertStmt(const sqlb::Table& t, sqlite3_int64 pk_value) const { QString stmt = QString("INSERT INTO `%1`").arg(t.name()); @@ -727,7 +726,7 @@ long DBBrowserDB::addRecord(const QString& sTableName) } } -bool DBBrowserDB::deleteRecord(const QString& table, long rowid) +bool DBBrowserDB::deleteRecord(const QString& table, sqlite3_int64 rowid) { char * errmsg; if (!isOpen()) return false; @@ -750,7 +749,7 @@ bool DBBrowserDB::deleteRecord(const QString& table, long rowid) return ok; } -bool DBBrowserDB::updateRecord(const QString& table, const QString& column, long row, const QByteArray& value) +bool DBBrowserDB::updateRecord(const QString& table, const QString& column, sqlite3_int64 row, const QByteArray& value) { if (!isOpen()) return false; diff --git a/src/sqlitedb.h b/src/sqlitedb.h index 7abc3798..ecd6b305 100644 --- a/src/sqlitedb.h +++ b/src/sqlitedb.h @@ -2,13 +2,13 @@ #define SQLITEDB_H #include "sqlitetypes.h" +#include "sqlite.h" #include #include #include class CipherDialog; -class sqlite3; enum { @@ -66,7 +66,7 @@ public: * @param rowdata A list of QByteArray containing the row data. * @return true if statement execution was ok, else false. */ - bool getRow(const QString& sTableName, long rowid, QList& rowdata); + bool getRow(const QString& sTableName, sqlite3_int64 rowid, QList& rowdata); /** * @brief max Queries the table t for the max value of field. @@ -84,9 +84,9 @@ public: * @param pk_value This optional parameter can be used to manually set a specific value for the primary key column * @return An sqlite conform INSERT INTO statement with empty values. (NULL,'',0) */ - QString emptyInsertStmt(const sqlb::Table& t, long pk_value = -1) const; - bool deleteRecord(const QString& table, long rowid); - bool updateRecord(const QString& table, const QString& column, long row, const QByteArray& value); + QString emptyInsertStmt(const sqlb::Table& t, sqlite3_int64 pk_value = -1) const; + bool deleteRecord(const QString& table, sqlite3_int64 rowid); + bool updateRecord(const QString& table, const QString& column, sqlite3_int64 row, const QByteArray& value); bool createTable(const QString& name, const sqlb::FieldVector& structure); bool renameTable(const QString& from_table, const QString& to_table); diff --git a/src/sqlitetablemodel.cpp b/src/sqlitetablemodel.cpp index 03ee0cfc..e50891be 100644 --- a/src/sqlitetablemodel.cpp +++ b/src/sqlitetablemodel.cpp @@ -256,7 +256,7 @@ bool SqliteTableModel::setData(const QModelIndex& index, const QVariant& value, if(m_data.at(index.row()).at(index.column()) == value) return true; - if(m_db->updateRecord(m_sTable, m_headers.at(index.column()), m_data[index.row()].at(0).toLong(), value.toByteArray())) + if(m_db->updateRecord(m_sTable, m_headers.at(index.column()), m_data[index.row()].at(0).toLongLong(), value.toByteArray())) { // Only update the cache if this row has already been read, if not there's no need to do any changes to the cache if(index.row() < m_data.size()) @@ -351,7 +351,7 @@ bool SqliteTableModel::removeRows(int row, int count, const QModelIndex& parent) for(int i=count-1;i>=0;i--) { - m_db->deleteRecord(m_sTable, m_data.at(row + i).at(0).toLong()); + m_db->deleteRecord(m_sTable, m_data.at(row + i).at(0).toLongLong()); m_data.removeAt(row + i); }