mirror of
https://github.com/sqlitebrowser/sqlitebrowser.git
synced 2026-01-21 03:21:43 -06:00
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 #172.
This commit is contained in:
@@ -578,7 +578,7 @@ bool DBBrowserDB::executeMultiSQL(const QString& statement, bool dirty, bool log
|
||||
return true;
|
||||
}
|
||||
|
||||
bool DBBrowserDB::getRow(const QString& sTableName, int rowid, QList<QByteArray>& rowdata)
|
||||
bool DBBrowserDB::getRow(const QString& sTableName, long rowid, QList<QByteArray>& 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 +625,7 @@ int64_t DBBrowserDB::max(const sqlb::Table& t, sqlb::FieldPtr field) const
|
||||
return ret;
|
||||
}
|
||||
|
||||
QString DBBrowserDB::emptyInsertStmt(const sqlb::Table& t, int pk_value) const
|
||||
QString DBBrowserDB::emptyInsertStmt(const sqlb::Table& t, long pk_value) const
|
||||
{
|
||||
QString stmt = QString("INSERT INTO `%1`").arg(t.name());
|
||||
|
||||
@@ -689,7 +689,7 @@ QString DBBrowserDB::emptyInsertStmt(const sqlb::Table& t, int pk_value) const
|
||||
return stmt;
|
||||
}
|
||||
|
||||
int DBBrowserDB::addRecord(const QString& sTableName)
|
||||
long DBBrowserDB::addRecord(const QString& sTableName)
|
||||
{
|
||||
char *errmsg;
|
||||
if (!isOpen()) return false;
|
||||
@@ -699,12 +699,12 @@ int DBBrowserDB::addRecord(const QString& sTableName)
|
||||
// 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
|
||||
// and adding one to it.
|
||||
QString sInsertstmt;
|
||||
int pk_value;
|
||||
long pk_value;
|
||||
if(table.isWithoutRowidTable())
|
||||
{
|
||||
SqliteTableModel m(this, this);
|
||||
m.setQuery(QString("SELECT MAX(`%1`) FROM `%2`;").arg(table.rowidColumn()).arg(sTableName));
|
||||
pk_value = m.data(m.index(0, 0)).toInt() + 1;
|
||||
pk_value = m.data(m.index(0, 0)).toLongLong() + 1;
|
||||
sInsertstmt = emptyInsertStmt(table, pk_value);
|
||||
} else {
|
||||
sInsertstmt = emptyInsertStmt(table);
|
||||
@@ -727,7 +727,7 @@ int DBBrowserDB::addRecord(const QString& sTableName)
|
||||
}
|
||||
}
|
||||
|
||||
bool DBBrowserDB::deleteRecord(const QString& table, int rowid)
|
||||
bool DBBrowserDB::deleteRecord(const QString& table, long rowid)
|
||||
{
|
||||
char * errmsg;
|
||||
if (!isOpen()) return false;
|
||||
@@ -750,7 +750,7 @@ bool DBBrowserDB::deleteRecord(const QString& table, int rowid)
|
||||
return ok;
|
||||
}
|
||||
|
||||
bool DBBrowserDB::updateRecord(const QString& table, const QString& column, int row, const QByteArray& value)
|
||||
bool DBBrowserDB::updateRecord(const QString& table, const QString& column, long row, const QByteArray& value)
|
||||
{
|
||||
if (!isOpen()) return false;
|
||||
|
||||
|
||||
@@ -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, int rowid, QList<QByteArray>& rowdata);
|
||||
bool getRow(const QString& sTableName, long rowid, QList<QByteArray>& rowdata);
|
||||
|
||||
/**
|
||||
* @brief max Queries the table t for the max value of field.
|
||||
@@ -77,16 +77,16 @@ public:
|
||||
int64_t max(const sqlb::Table& t, sqlb::FieldPtr field) const;
|
||||
|
||||
void updateSchema();
|
||||
int addRecord(const QString& sTableName);
|
||||
long addRecord(const QString& sTableName);
|
||||
|
||||
/**
|
||||
* @brief Creates an empty insert statement.
|
||||
* @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, int pk_value = -1) const;
|
||||
bool deleteRecord(const QString& table, int rowid);
|
||||
bool updateRecord(const QString& table, const QString& column, int row, const QByteArray& value);
|
||||
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);
|
||||
|
||||
bool createTable(const QString& name, const sqlb::FieldVector& structure);
|
||||
bool renameTable(const QString& from_table, const QString& to_table);
|
||||
|
||||
@@ -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).toInt(), value.toByteArray()))
|
||||
if(m_db->updateRecord(m_sTable, m_headers.at(index.column()), m_data[index.row()].at(0).toLong(), 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).toInt());
|
||||
m_db->deleteRecord(m_sTable, m_data.at(row + i).at(0).toLong());
|
||||
m_data.removeAt(row + i);
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user