85 lines
2.2 KiB
C++
85 lines
2.2 KiB
C++
#include "sqlform.h"
|
|
#include "ui_sqlform.h"
|
|
#include <QComboBox>
|
|
#include <QSqlDatabase>
|
|
#include <QSqlTableModel>
|
|
#include <QSqlQueryModel>
|
|
#include <QSettings>
|
|
#include <Qsci/qscilexersql.h>
|
|
|
|
SqlForm::SqlForm(QWidget *parent) :
|
|
QDialog(parent),
|
|
ui(new Ui::SqlForm)
|
|
{
|
|
QsciLexerSQL *lexer = new QsciLexerSQL();
|
|
lexer->setDefaultPaper(QColor(255, 255, 255));
|
|
lexer->setDefaultColor(QColor(0, 0, 0));
|
|
lexer->setDefaultFont(QFont("monospace"));
|
|
for (int i = -1; i < 23; i++)
|
|
{
|
|
lexer->setPaper(QColor(255, 255, 255), i);
|
|
//lexer->setDefaultColor(QColor(0, 0, 0));
|
|
lexer->setFont(QFont("monospace"), i);
|
|
}
|
|
ui->setupUi(this);
|
|
ui->QueryTextEdit->setLexer(lexer);
|
|
m_ConnectionComboBox = new QComboBox(this);
|
|
reloadConnections();
|
|
ui->QueryToolBar->addWidget(m_ConnectionComboBox);
|
|
m_QueryModel = 0;
|
|
}
|
|
|
|
SqlForm::~SqlForm()
|
|
{
|
|
delete m_ConnectionComboBox;
|
|
delete m_QueryModel;
|
|
delete ui;
|
|
}
|
|
|
|
void SqlForm::reloadConnections()
|
|
{
|
|
QSettings settings;
|
|
QStringList connectionList = settings.value("connectionList").toStringList();
|
|
m_ConnectionComboBox->clear();
|
|
for (int i = 0; i < connectionList.count(); i++)
|
|
{
|
|
QMap<QString, QVariant> connectionMap = settings.value("connection " + connectionList.at(i)).toMap();
|
|
m_ConnectionComboBox->addItem(connectionMap.value("connectionName").toString());
|
|
}
|
|
}
|
|
|
|
void SqlForm::changeEvent(QEvent *e)
|
|
{
|
|
QWidget::changeEvent(e);
|
|
switch (e->type()) {
|
|
case QEvent::LanguageChange:
|
|
ui->retranslateUi(this);
|
|
break;
|
|
default:
|
|
break;
|
|
}
|
|
}
|
|
|
|
void SqlForm::on_RunQueryAction_triggered()
|
|
{
|
|
m_Database = QSqlDatabase::database(m_ConnectionComboBox->currentText());
|
|
m_Database.open();
|
|
QCoreApplication::processEvents();
|
|
QString query = "";
|
|
if (m_QueryModel == 0)
|
|
{
|
|
m_QueryModel = new QSqlQueryModel;
|
|
}
|
|
if (ui->QueryTextEdit->hasSelectedText())
|
|
{
|
|
query = ui->QueryTextEdit->selectedText();
|
|
}
|
|
else
|
|
{
|
|
query = ui->QueryTextEdit->text();
|
|
}
|
|
m_QueryModel->setQuery(query, m_Database);
|
|
ui->ResultsTableView->setModel(m_QueryModel);
|
|
ui->ResultsTableView->resizeColumnsToContents();
|
|
}
|