107 lines
2.8 KiB
C++
107 lines
2.8 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;
|
|
QString connections = settings.value("connectionList", QVariant("")).toString();
|
|
if (!connections.isEmpty())
|
|
{
|
|
QStringList connectionList = connections.split(",");
|
|
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()
|
|
{
|
|
QCursor oldCursor = cursor();
|
|
setCursor(Qt::BusyCursor);
|
|
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);
|
|
/*QSqlError error = m_QueryModel->lastError();
|
|
QString errorMessage;
|
|
if (error == QSqlError::ConnectionError)
|
|
{
|
|
errorMessage = "Connection Error: ";
|
|
if (!error.driverText().isEmpty())
|
|
{
|
|
errorMessage = errorMessage + error.driverText();
|
|
}
|
|
if (!error.databaseText().isEmpty())
|
|
{
|
|
errorMessage = errorMessage + error.databaseText();
|
|
}
|
|
|
|
}*/
|
|
ui->ResultsTableView->setModel(m_QueryModel);
|
|
ui->ResultsTableView->resizeColumnsToContents();
|
|
setCursor(oldCursor);
|
|
}
|