mirror of
https://github.com/sqlitebrowser/sqlitebrowser.git
synced 2026-01-20 11:00:44 -06:00
Add live SQL preview to the Create Index dialog
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.
This commit is contained in:
@@ -1,7 +1,6 @@
|
||||
#include "CreateIndexDialog.h"
|
||||
#include "ui_CreateIndexDialog.h"
|
||||
#include "sqlitedb.h"
|
||||
#include "sqlitetypes.h"
|
||||
|
||||
#include <QMessageBox>
|
||||
#include <QPushButton>
|
||||
@@ -9,7 +8,8 @@
|
||||
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);
|
||||
@@ -24,6 +24,8 @@ CreateIndexDialog::CreateIndexDialog(DBBrowserDB& db, QWidget* parent)
|
||||
|
||||
QHeaderView *tableHeaderView = ui->tableIndexColumns->horizontalHeader();
|
||||
tableHeaderView->setSectionResizeMode(0, QHeaderView::Stretch);
|
||||
|
||||
updateSqlText();
|
||||
}
|
||||
|
||||
CreateIndexDialog::~CreateIndexDialog()
|
||||
@@ -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());
|
||||
@@ -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++;
|
||||
}
|
||||
if(num_columns == 0)
|
||||
valid = false;
|
||||
|
||||
ui->buttonBox->button(QDialogButtonBox::Ok)->setEnabled(valid);
|
||||
}
|
||||
|
||||
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(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()
|
||||
{
|
||||
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());
|
||||
}
|
||||
|
||||
@@ -1,6 +1,8 @@
|
||||
#ifndef CREATEINDEXDIALOG_H
|
||||
#define CREATEINDEXDIALOG_H
|
||||
|
||||
#include "sqlitetypes.h"
|
||||
|
||||
#include <QDialog>
|
||||
|
||||
class DBBrowserDB;
|
||||
@@ -25,6 +27,9 @@ private slots:
|
||||
private:
|
||||
DBBrowserDB& pdb;
|
||||
Ui::CreateIndexDialog* ui;
|
||||
sqlb::Index index;
|
||||
|
||||
void updateSqlText();
|
||||
};
|
||||
|
||||
#endif
|
||||
|
||||
@@ -7,7 +7,7 @@
|
||||
<x>0</x>
|
||||
<y>0</y>
|
||||
<width>610</width>
|
||||
<height>403</height>
|
||||
<height>504</height>
|
||||
</rect>
|
||||
</property>
|
||||
<property name="windowTitle">
|
||||
@@ -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>&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">
|
||||
@@ -125,6 +69,78 @@
|
||||
</property>
|
||||
</widget>
|
||||
</item>
|
||||
<item row="3" column="0">
|
||||
<widget class="QLabel" name="labelIndexColumns">
|
||||
<property name="text">
|
||||
<string>&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>
|
||||
@@ -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>
|
||||
@@ -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>
|
||||
|
||||
@@ -306,10 +306,11 @@ public:
|
||||
|
||||
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;
|
||||
|
||||
|
||||
Reference in New Issue
Block a user