Skip to content

Commit

Permalink
Add live SQL preview to the Create Index dialog
Browse files Browse the repository at this point in the history
Add a fancy live preview of the SQL statement that is about to be
executed, just as we have one in the Edit Table dialog.
  • Loading branch information
MKleusberg committed Jan 16, 2017
1 parent 00e0108 commit 1bd1009
Show file tree
Hide file tree
Showing 4 changed files with 144 additions and 77 deletions.
59 changes: 40 additions & 19 deletions src/CreateIndexDialog.cpp
@@ -1,15 +1,15 @@
#include "CreateIndexDialog.h"
#include "ui_CreateIndexDialog.h"
#include "sqlitedb.h"
#include "sqlitetypes.h"

#include <QMessageBox>
#include <QPushButton>

CreateIndexDialog::CreateIndexDialog(DBBrowserDB& db, QWidget* parent)
: QDialog(parent),
pdb(db),
ui(new Ui::CreateIndexDialog)
ui(new Ui::CreateIndexDialog),
index(sqlb::Index(QString("")))
{
// Create UI
ui->setupUi(this);
Expand All @@ -24,6 +24,8 @@ CreateIndexDialog::CreateIndexDialog(DBBrowserDB& db, QWidget* parent)

QHeaderView *tableHeaderView = ui->tableIndexColumns->horizontalHeader();
tableHeaderView->setSectionResizeMode(0, QHeaderView::Stretch);

updateSqlText();
}

CreateIndexDialog::~CreateIndexDialog()
Expand All @@ -33,6 +35,10 @@ CreateIndexDialog::~CreateIndexDialog()

void CreateIndexDialog::tableChanged(const QString& new_table)
{
// Set the table name and clear all index columns
index.setTable(new_table);
index.clearColumns();

// And fill the table again
QStringList fields = pdb.getObjectByName(new_table).table.fieldNames();
ui->tableIndexColumns->setRowCount(fields.size());
Expand All @@ -55,45 +61,60 @@ void CreateIndexDialog::tableChanged(const QString& new_table)
order->addItem("ASC");
order->addItem("DESC");
ui->tableIndexColumns->setCellWidget(i, 2, order);
connect(order, static_cast<void(QComboBox::*)(const QString&)>(&QComboBox::currentTextChanged),
[=](QString new_order)
{
int colnum = index.findColumn(fields.at(i));
if(colnum != -1)
{
index.column(colnum)->setOrder(new_order);
updateSqlText();
}
});
}

updateSqlText();
}

void CreateIndexDialog::checkInput()
{
// Check if index name is set
bool valid = true;
if(ui->editIndexName->text().isEmpty())
valid = false;

int num_columns = 0;
// Check if index has any columns
index.clearColumns();
for(int i=0; i < ui->tableIndexColumns->rowCount(); ++i)
{
if(ui->tableIndexColumns->item(i, 1) && ui->tableIndexColumns->item(i, 1)->data(Qt::CheckStateRole) == Qt::Checked)
num_columns++;
{
index.addColumn(sqlb::IndexedColumnPtr(new sqlb::IndexedColumn(
ui->tableIndexColumns->item(i, 0)->text(),
qobject_cast<QComboBox*>(ui->tableIndexColumns->cellWidget(i, 2))->currentText())));
}
}
if(num_columns == 0)
if(index.columns().size() == 0)
valid = false;

// Only activate OK button if index data is valid
ui->buttonBox->button(QDialogButtonBox::Ok)->setEnabled(valid);

// Set the index name and the unique flag
index.setName(ui->editIndexName->text());
index.setUnique(ui->checkIndexUnique->isChecked());
updateSqlText();
}

void CreateIndexDialog::accept()
{
sqlb::Index index(ui->editIndexName->text());
index.setUnique(ui->checkIndexUnique->isChecked());
index.setTable(ui->comboTableName->currentText());

for(int i=0; i < ui->tableIndexColumns->rowCount(); ++i)
{
if(ui->tableIndexColumns->item(i, 1)->data(Qt::CheckStateRole) == Qt::Checked)
{
index.addColumn(sqlb::IndexedColumnPtr(new sqlb::IndexedColumn(
ui->tableIndexColumns->item(i, 0)->text(),
qobject_cast<QComboBox*>(ui->tableIndexColumns->cellWidget(i, 2))->currentText())));
}
}

if(pdb.executeSQL(index.sql()))
QDialog::accept();
else
QMessageBox::warning(this, QApplication::applicationName(), tr("Creating the index failed:\n%1").arg(pdb.lastError()));
}

void CreateIndexDialog::updateSqlText()
{
ui->sqlTextEdit->setText(index.sql());
}
5 changes: 5 additions & 0 deletions src/CreateIndexDialog.h
@@ -1,6 +1,8 @@
#ifndef CREATEINDEXDIALOG_H
#define CREATEINDEXDIALOG_H

#include "sqlitetypes.h"

#include <QDialog>

class DBBrowserDB;
Expand All @@ -25,6 +27,9 @@ private slots:
private:
DBBrowserDB& pdb;
Ui::CreateIndexDialog* ui;
sqlb::Index index;

void updateSqlText();
};

#endif
154 changes: 97 additions & 57 deletions src/CreateIndexDialog.ui
Expand Up @@ -7,7 +7,7 @@
<x>0</x>
<y>0</y>
<width>610</width>
<height>403</height>
<height>504</height>
</rect>
</property>
<property name="windowTitle">
Expand Down Expand Up @@ -39,62 +39,6 @@
<item row="0" column="1">
<widget class="QLineEdit" name="editIndexName"/>
</item>
<item row="3" column="0">
<widget class="QLabel" name="labelIndexColumns">
<property name="text">
<string>&amp;Columns</string>
</property>
<property name="buddy">
<cstring>tableIndexColumns</cstring>
</property>
</widget>
</item>
<item row="3" column="1">
<widget class="QTableWidget" name="tableIndexColumns">
<property name="sizePolicy">
<sizepolicy hsizetype="MinimumExpanding" vsizetype="MinimumExpanding">
<horstretch>0</horstretch>
<verstretch>1</verstretch>
</sizepolicy>
</property>
<property name="minimumSize">
<size>
<width>0</width>
<height>250</height>
</size>
</property>
<property name="editTriggers">
<set>QAbstractItemView::NoEditTriggers</set>
</property>
<property name="showDropIndicator" stdset="0">
<bool>false</bool>
</property>
<property name="alternatingRowColors">
<bool>true</bool>
</property>
<property name="selectionMode">
<enum>QAbstractItemView::NoSelection</enum>
</property>
<attribute name="verticalHeaderVisible">
<bool>false</bool>
</attribute>
<column>
<property name="text">
<string>Column</string>
</property>
</column>
<column>
<property name="text">
<string>Use in Index</string>
</property>
</column>
<column>
<property name="text">
<string>Order</string>
</property>
</column>
</widget>
</item>
<item row="1" column="0">
<widget class="QLabel" name="labelTableName">
<property name="text">
Expand Down Expand Up @@ -125,6 +69,78 @@
</property>
</widget>
</item>
<item row="3" column="0">
<widget class="QLabel" name="labelIndexColumns">
<property name="text">
<string>&amp;Columns</string>
</property>
<property name="buddy">
<cstring>tableIndexColumns</cstring>
</property>
</widget>
</item>
<item row="3" column="1">
<widget class="QSplitter" name="splitter">
<property name="orientation">
<enum>Qt::Vertical</enum>
</property>
<widget class="QTableWidget" name="tableIndexColumns">
<property name="sizePolicy">
<sizepolicy hsizetype="MinimumExpanding" vsizetype="MinimumExpanding">
<horstretch>0</horstretch>
<verstretch>1</verstretch>
</sizepolicy>
</property>
<property name="minimumSize">
<size>
<width>0</width>
<height>250</height>
</size>
</property>
<property name="editTriggers">
<set>QAbstractItemView::NoEditTriggers</set>
</property>
<property name="showDropIndicator" stdset="0">
<bool>false</bool>
</property>
<property name="alternatingRowColors">
<bool>true</bool>
</property>
<property name="selectionMode">
<enum>QAbstractItemView::NoSelection</enum>
</property>
<attribute name="verticalHeaderVisible">
<bool>false</bool>
</attribute>
<column>
<property name="text">
<string>Column</string>
</property>
</column>
<column>
<property name="text">
<string>Use in Index</string>
</property>
</column>
<column>
<property name="text">
<string>Order</string>
</property>
</column>
</widget>
<widget class="SqlTextEdit" name="sqlTextEdit">
<property name="minimumSize">
<size>
<width>0</width>
<height>100</height>
</size>
</property>
<property name="readOnly">
<bool>true</bool>
</property>
</widget>
</widget>
</item>
</layout>
</item>
<item>
Expand All @@ -139,6 +155,14 @@
</item>
</layout>
</widget>
<customwidgets>
<customwidget>
<class>SqlTextEdit</class>
<extends>QWidget</extends>
<header>sqltextedit.h</header>
<container>1</container>
</customwidget>
</customwidgets>
<tabstops>
<tabstop>editIndexName</tabstop>
<tabstop>comboTableName</tabstop>
Expand Down Expand Up @@ -230,6 +254,22 @@
</hint>
</hints>
</connection>
<connection>
<sender>checkIndexUnique</sender>
<signal>toggled(bool)</signal>
<receiver>CreateIndexDialog</receiver>
<slot>checkInput()</slot>
<hints>
<hint type="sourcelabel">
<x>332</x>
<y>90</y>
</hint>
<hint type="destinationlabel">
<x>304</x>
<y>251</y>
</hint>
</hints>
</connection>
</connections>
<slots>
<slot>tableChanged(QString)</slot>
Expand Down
3 changes: 2 additions & 1 deletion src/sqlitetypes.h
Expand Up @@ -306,10 +306,11 @@ class Index

void setColumns(const IndexedColumnVector& columns);
const IndexedColumnVector& columns() const { return m_columns; }
void clearColumns() { m_columns.clear(); }
void addColumn(const IndexedColumnPtr& c) { m_columns.append(c); }
bool removeColumn(const QString& name);
void setColumn(int index, IndexedColumnPtr c) { m_columns[index] = c; }
const IndexedColumnPtr& column(int index) const { return m_columns[index]; }
IndexedColumnPtr& column(int index) { return m_columns[index]; }
int findColumn(const QString& name) const;
QStringList columnSqlList() const;

Expand Down

0 comments on commit 1bd1009

Please sign in to comment.