Skip to content

Commit c20c585

Browse files
committed
Support 64bit primary keys
Primary keys in SQLite are 64bit in size. We, however, used the int datatype which often is 32bit only. Also the conversion from QString and other Qt datatypes to numbers was done by the toInt() method which fails on these large numbers. These issues are fixed by this commit, adding support for databases with big primary key values. See issue sqlitebrowser#172.
1 parent f63b119 commit c20c585

File tree

3 files changed

+14
-14
lines changed

3 files changed

+14
-14
lines changed

src/sqlitedb.cpp

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -578,7 +578,7 @@ bool DBBrowserDB::executeMultiSQL(const QString& statement, bool dirty, bool log
578578
return true;
579579
}
580580

581-
bool DBBrowserDB::getRow(const QString& sTableName, int rowid, QList<QByteArray>& rowdata)
581+
bool DBBrowserDB::getRow(const QString& sTableName, long rowid, QList<QByteArray>& rowdata)
582582
{
583583
QString sQuery = QString("SELECT * FROM `%1` WHERE `%2`=%3;").arg(sTableName).arg(getObjectByName(sTableName).table.rowidColumn()).arg(rowid);
584584
QByteArray utf8Query = sQuery.toUtf8();
@@ -625,7 +625,7 @@ int64_t DBBrowserDB::max(const sqlb::Table& t, sqlb::FieldPtr field) const
625625
return ret;
626626
}
627627

628-
QString DBBrowserDB::emptyInsertStmt(const sqlb::Table& t, int pk_value) const
628+
QString DBBrowserDB::emptyInsertStmt(const sqlb::Table& t, long pk_value) const
629629
{
630630
QString stmt = QString("INSERT INTO `%1`").arg(t.name());
631631

@@ -689,7 +689,7 @@ QString DBBrowserDB::emptyInsertStmt(const sqlb::Table& t, int pk_value) const
689689
return stmt;
690690
}
691691

692-
int DBBrowserDB::addRecord(const QString& sTableName)
692+
long DBBrowserDB::addRecord(const QString& sTableName)
693693
{
694694
char *errmsg;
695695
if (!isOpen()) return false;
@@ -699,12 +699,12 @@ int DBBrowserDB::addRecord(const QString& sTableName)
699699
// For tables without rowid we have to set the primary key by ourselves. We do so by querying for the largest value in the PK column
700700
// and adding one to it.
701701
QString sInsertstmt;
702-
int pk_value;
702+
long pk_value;
703703
if(table.isWithoutRowidTable())
704704
{
705705
SqliteTableModel m(this, this);
706706
m.setQuery(QString("SELECT MAX(`%1`) FROM `%2`;").arg(table.rowidColumn()).arg(sTableName));
707-
pk_value = m.data(m.index(0, 0)).toInt() + 1;
707+
pk_value = m.data(m.index(0, 0)).toLongLong() + 1;
708708
sInsertstmt = emptyInsertStmt(table, pk_value);
709709
} else {
710710
sInsertstmt = emptyInsertStmt(table);
@@ -727,7 +727,7 @@ int DBBrowserDB::addRecord(const QString& sTableName)
727727
}
728728
}
729729

730-
bool DBBrowserDB::deleteRecord(const QString& table, int rowid)
730+
bool DBBrowserDB::deleteRecord(const QString& table, long rowid)
731731
{
732732
char * errmsg;
733733
if (!isOpen()) return false;
@@ -750,7 +750,7 @@ bool DBBrowserDB::deleteRecord(const QString& table, int rowid)
750750
return ok;
751751
}
752752

753-
bool DBBrowserDB::updateRecord(const QString& table, const QString& column, int row, const QByteArray& value)
753+
bool DBBrowserDB::updateRecord(const QString& table, const QString& column, long row, const QByteArray& value)
754754
{
755755
if (!isOpen()) return false;
756756

src/sqlitedb.h

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ class DBBrowserDB : public QObject
6666
* @param rowdata A list of QByteArray containing the row data.
6767
* @return true if statement execution was ok, else false.
6868
*/
69-
bool getRow(const QString& sTableName, int rowid, QList<QByteArray>& rowdata);
69+
bool getRow(const QString& sTableName, long rowid, QList<QByteArray>& rowdata);
7070

7171
/**
7272
* @brief max Queries the table t for the max value of field.
@@ -77,16 +77,16 @@ class DBBrowserDB : public QObject
7777
int64_t max(const sqlb::Table& t, sqlb::FieldPtr field) const;
7878

7979
void updateSchema();
80-
int addRecord(const QString& sTableName);
80+
long addRecord(const QString& sTableName);
8181

8282
/**
8383
* @brief Creates an empty insert statement.
8484
* @param pk_value This optional parameter can be used to manually set a specific value for the primary key column
8585
* @return An sqlite conform INSERT INTO statement with empty values. (NULL,'',0)
8686
*/
87-
QString emptyInsertStmt(const sqlb::Table& t, int pk_value = -1) const;
88-
bool deleteRecord(const QString& table, int rowid);
89-
bool updateRecord(const QString& table, const QString& column, int row, const QByteArray& value);
87+
QString emptyInsertStmt(const sqlb::Table& t, long pk_value = -1) const;
88+
bool deleteRecord(const QString& table, long rowid);
89+
bool updateRecord(const QString& table, const QString& column, long row, const QByteArray& value);
9090

9191
bool createTable(const QString& name, const sqlb::FieldVector& structure);
9292
bool renameTable(const QString& from_table, const QString& to_table);

src/sqlitetablemodel.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -256,7 +256,7 @@ bool SqliteTableModel::setData(const QModelIndex& index, const QVariant& value,
256256
if(m_data.at(index.row()).at(index.column()) == value)
257257
return true;
258258

259-
if(m_db->updateRecord(m_sTable, m_headers.at(index.column()), m_data[index.row()].at(0).toInt(), value.toByteArray()))
259+
if(m_db->updateRecord(m_sTable, m_headers.at(index.column()), m_data[index.row()].at(0).toLong(), value.toByteArray()))
260260
{
261261
// Only update the cache if this row has already been read, if not there's no need to do any changes to the cache
262262
if(index.row() < m_data.size())
@@ -351,7 +351,7 @@ bool SqliteTableModel::removeRows(int row, int count, const QModelIndex& parent)
351351

352352
for(int i=count-1;i>=0;i--)
353353
{
354-
m_db->deleteRecord(m_sTable, m_data.at(row + i).at(0).toInt());
354+
m_db->deleteRecord(m_sTable, m_data.at(row + i).at(0).toLong());
355355
m_data.removeAt(row + i);
356356
}
357357

0 commit comments

Comments
 (0)