mirror of
https://github.com/sqlitebrowser/sqlitebrowser.git
synced 2026-01-19 18:40:13 -06:00
SqliteTableModel: Inserting and deleting rows, clean up, bug fixes
Add some basic code to support inserting and deleting rows again. Fix a few bugs here and there. Do some general code clean up.
This commit is contained in:
@@ -27,7 +27,6 @@
|
||||
MainWindow::MainWindow(QWidget* parent)
|
||||
: QMainWindow(parent),
|
||||
ui(new Ui::MainWindow),
|
||||
browseTableModel(new QStandardItemModel(this)),
|
||||
m_browseTableModel(new SqliteTableModel(this, &db)),
|
||||
sqliteHighlighterTabSql(0),
|
||||
sqliteHighlighterLogUser(0),
|
||||
@@ -278,9 +277,10 @@ void MainWindow::populateTable( const QString & tablename)
|
||||
// Set new table
|
||||
if(!tablename.isEmpty())
|
||||
m_browseTableModel->setTable(tablename);
|
||||
ui->dataTable->setColumnHidden(0, true);
|
||||
|
||||
// Reset sorting
|
||||
curBrowseOrderByIndex = 1;
|
||||
curBrowseOrderByIndex = 0;
|
||||
curBrowseOrderByMode = Qt::AscendingOrder;
|
||||
m_browseTableModel->sort(curBrowseOrderByIndex, curBrowseOrderByMode);
|
||||
|
||||
@@ -320,7 +320,7 @@ void MainWindow::resetBrowser()
|
||||
int pos = ui->comboBrowseTable->findText(sCurrentTable);
|
||||
pos = pos == -1 ? 0 : pos;
|
||||
ui->comboBrowseTable->setCurrentIndex(pos);
|
||||
curBrowseOrderByIndex = 1;
|
||||
curBrowseOrderByIndex = 0;
|
||||
curBrowseOrderByMode = Qt::AscendingOrder;
|
||||
m_browseTableModel->sort(curBrowseOrderByIndex, curBrowseOrderByMode);
|
||||
populateTable(ui->comboBrowseTable->currentText());
|
||||
@@ -364,111 +364,34 @@ void MainWindow::closeEvent( QCloseEvent* event )
|
||||
|
||||
void MainWindow::addRecord()
|
||||
{
|
||||
if (db.addRecord(db.curBrowseTableName))
|
||||
int row = m_browseTableModel->rowCount();
|
||||
if(m_browseTableModel->insertRow(row))
|
||||
{
|
||||
populateTable(db.curBrowseTableName);
|
||||
//added record will be the last one in view
|
||||
updateTableView(db.getRecordCount()-1);
|
||||
}
|
||||
else
|
||||
{
|
||||
QMessageBox::information( this, QApplication::applicationName(),
|
||||
tr("Error adding record:\n") + db.lastErrorMessage);
|
||||
selectTableLine(row);
|
||||
} else {
|
||||
QMessageBox::warning( this, QApplication::applicationName(), tr("Error adding record:\n") + db.lastErrorMessage);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void MainWindow::deleteRecord()
|
||||
{
|
||||
if(ui->dataTable->currentIndex().row() != -1)
|
||||
if(ui->dataTable->currentIndex().isValid())
|
||||
{
|
||||
int lastselected = ui->dataTable->currentIndex().row();
|
||||
db.deleteRecord(lastselected);
|
||||
populateTable(db.curBrowseTableName);
|
||||
int nextselected = lastselected ;
|
||||
if (nextselected > db.getRecordCount()){
|
||||
nextselected = db.getRecordCount();
|
||||
}
|
||||
if (nextselected>0){
|
||||
selectTableLine(nextselected);
|
||||
int row = ui->dataTable->currentIndex().row();
|
||||
if(m_browseTableModel->removeRow(row))
|
||||
{
|
||||
populateTable(db.curBrowseTableName);
|
||||
if(row > m_browseTableModel->totalRowCount())
|
||||
row = m_browseTableModel->totalRowCount();
|
||||
selectTableLine(row);
|
||||
} else {
|
||||
QMessageBox::warning( this, QApplication::applicationName(), tr("Error deleting record:\n") + db.lastErrorMessage);
|
||||
}
|
||||
} else {
|
||||
QMessageBox::information( this, QApplication::applicationName(), tr("Please select a record first"));
|
||||
}
|
||||
}
|
||||
|
||||
#define WRAP_SIZE 80
|
||||
QString wrapText(const QString& text)
|
||||
{
|
||||
QString wrap;
|
||||
int textSize = text.size();
|
||||
|
||||
int cur = 0;
|
||||
while( wrap.size() < textSize)
|
||||
{
|
||||
wrap += text.mid(cur, WRAP_SIZE);
|
||||
cur += WRAP_SIZE;
|
||||
if( textSize - cur > WRAP_SIZE)
|
||||
wrap += '\n';
|
||||
}
|
||||
|
||||
return wrap;
|
||||
}
|
||||
|
||||
void MainWindow::updateTableView(int lineToSelect, bool keepColumnWidths)
|
||||
{
|
||||
QApplication::setOverrideCursor( Qt::WaitCursor );
|
||||
|
||||
// browseTableModel->setRowCount(db.getRecordCount());
|
||||
// browseTableModel->setColumnCount(db.browseFields.count());
|
||||
// browseTableModel->setHorizontalHeaderLabels(db.browseFields);
|
||||
|
||||
// rowList tab = db.browseRecs;
|
||||
// int maxRecs = db.getRecordCount();
|
||||
// gotoValidator->setRange(0, maxRecs);
|
||||
|
||||
// if ( maxRecs > 0 ) {
|
||||
|
||||
// int rowNum = 0;
|
||||
// int colNum = 0;
|
||||
// QString rowLabel;
|
||||
// for (int i = 0; i < tab.size(); ++i)
|
||||
// {
|
||||
// rowLabel.setNum(rowNum+1);
|
||||
// browseTableModel->setVerticalHeaderItem(rowNum, new QStandardItem(rowLabel));
|
||||
// colNum = 0;
|
||||
// QList<QByteArray> rt = tab[i];
|
||||
// for (int e = 1; e < rt.size(); ++e)
|
||||
// {
|
||||
// QString content = rt[e];
|
||||
|
||||
// QStandardItem* item = new QStandardItem(content);
|
||||
// item->setFlags(Qt::ItemIsSelectable | Qt::ItemIsEnabled);
|
||||
// item->setToolTip(wrapText(content));
|
||||
// browseTableModel->setItem( rowNum, colNum, item);
|
||||
// colNum++;
|
||||
// }
|
||||
// rowNum++;
|
||||
// if (rowNum==maxRecs) break;
|
||||
// }
|
||||
// }
|
||||
|
||||
// if(!keepColumnWidths) {
|
||||
// for(int i=0;i<browseTableModel->columnCount();++i)
|
||||
// {
|
||||
// ui->dataTable->resizeColumnToContents(i);
|
||||
// if( ui->dataTable->columnWidth(i) > 400 )
|
||||
// ui->dataTable->setColumnWidth(i, 400);
|
||||
// }
|
||||
// }
|
||||
// if (lineToSelect!=-1){
|
||||
// selectTableLine(lineToSelect);
|
||||
// }
|
||||
|
||||
setRecordsetLabel();
|
||||
QApplication::restoreOverrideCursor();
|
||||
}
|
||||
|
||||
void MainWindow::selectTableLine(int lineToSelect)
|
||||
{
|
||||
ui->dataTable->clearSelection();
|
||||
|
||||
@@ -50,7 +50,6 @@ private:
|
||||
|
||||
Ui::MainWindow* ui;
|
||||
|
||||
QStandardItemModel *browseTableModel;
|
||||
SqliteTableModel* m_browseTableModel;
|
||||
QStandardItemModel *queryResultListModel;
|
||||
QMenu *popupTableMenu;
|
||||
@@ -104,7 +103,6 @@ private slots:
|
||||
virtual void fileExit();
|
||||
virtual void addRecord();
|
||||
virtual void deleteRecord();
|
||||
virtual void updateTableView(int lineToSelect , bool keepColumnWidths = false);
|
||||
virtual void selectTableLine( int lineToSelect );
|
||||
virtual void navigatePrevious();
|
||||
virtual void navigateNext();
|
||||
|
||||
@@ -385,7 +385,7 @@ bool DBBrowserDB::executeMultiSQL(const QString& statement, bool dirty, bool log
|
||||
return true;
|
||||
}
|
||||
|
||||
bool DBBrowserDB::addRecord(const QString& sTableName)
|
||||
int DBBrowserDB::addRecord(const QString& sTableName)
|
||||
{
|
||||
char *errmsg;
|
||||
if (!isOpen()) return false;
|
||||
@@ -404,23 +404,21 @@ bool DBBrowserDB::addRecord(const QString& sTableName)
|
||||
{
|
||||
lastErrorMessage = QString::fromUtf8(errmsg);
|
||||
qCritical() << "addRecord: " << lastErrorMessage;
|
||||
return false;
|
||||
return -1;
|
||||
} else {
|
||||
return sqlite3_last_insert_rowid(_db);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
bool DBBrowserDB::deleteRecord( int wrow)
|
||||
bool DBBrowserDB::deleteRecord(const QString& table, int rowid)
|
||||
{
|
||||
char * errmsg;
|
||||
if (!hasValidBrowseSet) return false;
|
||||
if (!isOpen()) return false;
|
||||
bool ok = false;
|
||||
rowList tab = browseRecs;
|
||||
QList<QByteArray> rt = tab[wrow];
|
||||
QString rowid = rt[0];
|
||||
lastErrorMessage = QString("no error");
|
||||
|
||||
QString statement = QString("DELETE FROM `%1` WHERE rowid=%2;").arg(curBrowseTableName).arg(rowid);
|
||||
QString statement = QString("DELETE FROM `%1` WHERE rowid=%2;").arg(table).arg(rowid);
|
||||
|
||||
if (_db){
|
||||
logSQL(statement, kLogMsg_App);
|
||||
|
||||
@@ -99,8 +99,8 @@ public:
|
||||
*/
|
||||
QString getTableSQL(const QString& sTable);
|
||||
void updateSchema() ;
|
||||
bool addRecord(const QString& sTableName);
|
||||
bool deleteRecord(int wrow);
|
||||
int addRecord(const QString& sTableName);
|
||||
bool deleteRecord(const QString& table, int rowid);
|
||||
bool updateRecord(const QString& table, const QString& column, int row, const QByteArray& value);
|
||||
bool updateRecord(int wrow, int wcol, const QByteArray& wtext);
|
||||
bool browseTable( const QString & tablename, const QString& orderby = "rowid" );
|
||||
|
||||
@@ -70,14 +70,13 @@ void SqliteTableModel::setQuery(const QString& sQuery)
|
||||
for (int i = 0; i < m_columnCount; ++i)
|
||||
m_headers.append(QString::fromUtf8((const char *)sqlite3_column_name(stmt, i)));
|
||||
|
||||
int row = 0;
|
||||
// row data starts here
|
||||
do
|
||||
{
|
||||
QStringList rowdata;
|
||||
for (int i = 0; i < m_columnCount; ++i)
|
||||
rowdata.append(QString::fromUtf8((const char *)sqlite3_column_text(stmt, i)));
|
||||
m_data.insert(row++, rowdata);
|
||||
m_data.push_back(rowdata);
|
||||
} while(sqlite3_step(stmt) == SQLITE_ROW);
|
||||
}
|
||||
}
|
||||
@@ -124,7 +123,7 @@ QVariant SqliteTableModel::data(const QModelIndex &index, int role) const
|
||||
|
||||
if (role == Qt::DisplayRole)
|
||||
{
|
||||
return m_data[index.row()].at(index.column());
|
||||
return m_data.at(index.row()).at(index.column());
|
||||
}
|
||||
else
|
||||
return QVariant();
|
||||
@@ -136,7 +135,7 @@ bool SqliteTableModel::setData(const QModelIndex& index, const QVariant& value,
|
||||
{
|
||||
m_data[index.row()].replace(index.column(), value.toString());
|
||||
|
||||
if(m_db->updateRecord(m_sTable, m_headers.at(index.column()), index.sibling(index.row(), 0).data().toInt(), value.toByteArray()))
|
||||
if(m_db->updateRecord(m_sTable, m_headers.at(index.column()), m_data[index.row()].at(0).toInt(), value.toByteArray()))
|
||||
{
|
||||
emit(dataChanged(index, index));
|
||||
return true;
|
||||
@@ -175,7 +174,7 @@ void SqliteTableModel::fetchMore(const QModelIndex& parent)
|
||||
for (int i = 0; i < m_columnCount; ++i)
|
||||
rowdata.append(QString::fromUtf8((const char *)sqlite3_column_text(stmt, i)));
|
||||
Q_ASSERT(m_headers.size() == rowdata.size());
|
||||
m_data.insert(row++, rowdata);
|
||||
m_data.push_back(rowdata);
|
||||
} while(sqlite3_step(stmt) == SQLITE_ROW);
|
||||
}
|
||||
}
|
||||
@@ -201,10 +200,42 @@ void SqliteTableModel::sort(int column, Qt::SortOrder order)
|
||||
// Set the new query (but only if a table has already been set
|
||||
if(m_sTable != "")
|
||||
{
|
||||
setQuery(QString("SELECT * FROM `%1` ORDER BY `%2` %3")
|
||||
setQuery(QString("SELECT rowid,* FROM `%1` ORDER BY `%2` %3")
|
||||
.arg(m_sTable)
|
||||
.arg(m_headers.at(m_iSortColumn))
|
||||
.arg(m_sSortOrder)
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
bool SqliteTableModel::insertRows(int row, int count, const QModelIndex& parent)
|
||||
{
|
||||
beginInsertRows(parent, row, row + count - 1);
|
||||
|
||||
QStringList blank_data;
|
||||
for(int i=0;i<m_headers.size();i++)
|
||||
blank_data.push_back("");
|
||||
|
||||
for(int i=0;i<count;i++)
|
||||
{
|
||||
m_data.insert(row, blank_data);
|
||||
m_data[row].replace(0, QString::number(m_db->addRecord(m_sTable)));
|
||||
}
|
||||
|
||||
endInsertRows();
|
||||
return true;
|
||||
}
|
||||
|
||||
bool SqliteTableModel::removeRows(int row, int count, const QModelIndex& parent)
|
||||
{
|
||||
beginRemoveRows(parent, row, row + count - 1);
|
||||
|
||||
for(int i=count-1;i>=0;i--)
|
||||
{
|
||||
m_db->deleteRecord(m_sTable, m_data.at(row + i).at(0).toInt());
|
||||
m_data.removeAt(row + i);
|
||||
}
|
||||
|
||||
endRemoveRows();
|
||||
return true;
|
||||
}
|
||||
|
||||
@@ -12,7 +12,7 @@ class SqliteTableModel : public QAbstractTableModel
|
||||
public:
|
||||
explicit SqliteTableModel(QObject *parent = 0, DBBrowserDB* db = 0);
|
||||
|
||||
int rowCount(const QModelIndex &parent) const;
|
||||
int rowCount(const QModelIndex &parent = QModelIndex()) const;
|
||||
int totalRowCount() const;
|
||||
int columnCount(const QModelIndex &parent) const;
|
||||
QVariant headerData(int section, Qt::Orientation orientation, int role) const;
|
||||
@@ -22,6 +22,9 @@ public:
|
||||
void fetchMore(const QModelIndex &parent);
|
||||
size_t queryMore(size_t offset);
|
||||
|
||||
bool insertRows(int row, int count, const QModelIndex& parent = QModelIndex());
|
||||
bool removeRows(int row, int count, const QModelIndex& parent = QModelIndex());
|
||||
|
||||
void setQuery(const QString& sQuery);
|
||||
void setTable(const QString& table);
|
||||
void setChunkSize(size_t chunksize);
|
||||
@@ -38,7 +41,7 @@ private:
|
||||
int m_rowCount;
|
||||
int m_columnCount;
|
||||
QStringList m_headers;
|
||||
QMap<int, QStringList> m_data;
|
||||
QList<QStringList> m_data;
|
||||
|
||||
QString m_sQuery;
|
||||
QString m_sTable;
|
||||
|
||||
Reference in New Issue
Block a user