Commit 5f389e6c authored by Hugo Hörnquist's avatar Hugo Hörnquist

Add simple SQL interface from inside admin.

Adds a new table to the database.
parent 63975d2e
......@@ -344,6 +344,21 @@ MainWindow::MainWindow(QWidget *parent) :
transHistoryModel->setEditStrategy(QSqlTableModel::OnManualSubmit);
ui->moneyTransferHistory->header()->setSectionResizeMode(QHeaderView::ResizeToContents);
// ==================================================
QSqlQueryModel* sqlQueryModel = new QSqlQueryModel;
ui->sqlOutput->setModel(sqlQueryModel);
QSqlTableModel* sqlQueryListModel = new QSqlTableModel;
sqlQueryListModel->setTable("sql_queries");
sqlQueryListModel->select();
sqlQueryListModel->setHeaderData(0, Qt::Horizontal, "id");
sqlQueryListModel->setHeaderData(1, Qt::Horizontal, "Namn");
sqlQueryListModel->setHeaderData(2, Qt::Horizontal, "Query");
ui->sqlBufferList->setModel(sqlQueryListModel);
ui->sqlBufferList->hideColumn(0);
ui->sqlBufferList->hideColumn(2);
}
MainWindow::~MainWindow()
......@@ -936,3 +951,72 @@ void MainWindow::on_accNewAccBtn_clicked()
setMoneyAccountValues();
setAccTransferCombo();
}
void MainWindow::setSqlQuery(qlonglong id)
{
QSqlQuery query;
query.prepare("SELECT name, query FROM sql_queries "
"WHERE id = :id");
query.bindValue(":id", id);
query.exec();
if (query.next()) {
ui->sqlBufferName->setText(query.value(0).toString());
ui->sqlEdit->setPlainText(query.value(1).toString());
}
}
void MainWindow::setSqlQuery(QString name)
{
QSqlQuery query;
query.prepare("SELECT name, query FROM sql_queries "
"WHERE name = :name");
query.bindValue(":name", name);
query.exec();
if (query.next()) {
ui->sqlBufferName->setText(query.value(0).toString());
ui->sqlEdit->setPlainText(query.value(1).toString());
}
}
void MainWindow::on_sqlBufferList_activated(const QModelIndex &index)
{
QAbstractItemModel* model = ui->sqlBufferList->model();
qlonglong id = model->data(model->index(index.row(), 0)).toLongLong();
setSqlQuery(id);
}
void MainWindow::on_sqlBufferList_clicked(const QModelIndex &index)
{
QAbstractItemModel* model = ui->sqlBufferList->model();
QString name = model->data(model->index(index.row(), 1)).toString();
ui->sqlBufferName->setText(name);
}
void MainWindow::on_sqlLoad_clicked()
{
setSqlQuery(ui->sqlBufferName->text());
}
void MainWindow::on_sqlSave_clicked()
{
// QSqlTableModel* model = (QSqlTableModel*) ui->sqlBufferList->model();
QSqlQuery query;
query.prepare("INSERT OR REPLACE "
"INTO sql_queries (name, query) "
"VALUES (:name, :query)");
query.bindValue(":name", ui->sqlBufferName->text());
query.bindValue(":query", ui->sqlEdit->toPlainText());
query.exec();
((QSqlTableModel*) ui->sqlBufferList->model())->select();
}
void MainWindow::on_sqlOutReload_clicked()
{
QSqlQueryModel* model = (QSqlQueryModel*) ui->sqlOutput->model();
model->setQuery(ui->sqlEdit->toPlainText());
ui->tabWidget_2->setCurrentIndex(0);
}
......@@ -85,6 +85,16 @@ private slots:
void on_accNewAccBtn_clicked();
void on_sqlBufferList_activated(const QModelIndex &index);
void on_sqlBufferList_clicked(const QModelIndex &index);
void on_sqlLoad_clicked();
void on_sqlSave_clicked();
void on_sqlOutReload_clicked();
private:
Ui::MainWindow *ui;
QSqlTableModel* model;
......@@ -106,6 +116,9 @@ private:
void setProductFilter();
void setMoneyAccountValues();
void setSqlQuery(qlonglong id);
void setSqlQuery(QString name);
QVariant get_projected_stock_value();
QVariant get_acquistion_stock_value();
void setStockValues();
......
......@@ -14,8 +14,8 @@
<string>MainWindow</string>
</property>
<widget class="QWidget" name="centralWidget">
<layout class="QVBoxLayout" name="verticalLayout">
<item>
<layout class="QGridLayout" name="gridLayout_2">
<item row="0" column="0">
<widget class="QTabWidget" name="tabWidget">
<property name="tabShape">
<enum>QTabWidget::Rounded</enum>
......@@ -960,6 +960,136 @@
</item>
</layout>
</widget>
<widget class="QWidget" name="sqlTab">
<attribute name="title">
<string>SQL</string>
</attribute>
<layout class="QVBoxLayout" name="verticalLayout_12">
<item>
<widget class="QSplitter" name="splitter_3">
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
<widget class="QTabWidget" name="tabWidget_2">
<property name="currentIndex">
<number>1</number>
</property>
<widget class="QWidget" name="tab_6">
<attribute name="title">
<string>Output</string>
</attribute>
<layout class="QGridLayout" name="gridLayout_3">
<item row="0" column="0" colspan="3">
<widget class="QTableView" name="sqlOutput">
<property name="editTriggers">
<set>QAbstractItemView::NoEditTriggers</set>
</property>
</widget>
</item>
<item row="1" column="0">
<spacer name="horizontalSpacer_7">
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>40</width>
<height>20</height>
</size>
</property>
</spacer>
</item>
<item row="1" column="2">
<widget class="QPushButton" name="sqlPrint">
<property name="text">
<string>Skriv ut</string>
</property>
</widget>
</item>
</layout>
</widget>
<widget class="QWidget" name="tab_7">
<attribute name="title">
<string>Sparade</string>
</attribute>
<layout class="QVBoxLayout" name="verticalLayout">
<item>
<widget class="QTreeView" name="sqlBufferList">
<property name="editTriggers">
<set>QAbstractItemView::NoEditTriggers</set>
</property>
</widget>
</item>
</layout>
</widget>
</widget>
<widget class="QWidget" name="gridLayoutWidget">
<layout class="QGridLayout" name="gridLayout_5">
<item row="1" column="3">
<widget class="QPushButton" name="sqlSave">
<property name="text">
<string>Spara</string>
</property>
</widget>
</item>
<item row="1" column="2">
<widget class="QPushButton" name="sqlLoad">
<property name="text">
<string>Ladda</string>
</property>
</widget>
</item>
<item row="1" column="1">
<spacer name="horizontalSpacer_9">
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>40</width>
<height>20</height>
</size>
</property>
</spacer>
</item>
<item row="2" column="1" colspan="3">
<widget class="QPlainTextEdit" name="sqlEdit">
<property name="font">
<font>
<family>Monospace</family>
</font>
</property>
</widget>
</item>
<item row="3" column="1">
<widget class="QPushButton" name="sqlOutReload">
<property name="text">
<string>Kör</string>
</property>
</widget>
</item>
<item row="3" column="2" colspan="2">
<spacer name="horizontalSpacer_8">
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>40</width>
<height>20</height>
</size>
</property>
</spacer>
</item>
<item row="0" column="1" colspan="3">
<widget class="QLineEdit" name="sqlBufferName"/>
</item>
</layout>
</widget>
</widget>
</item>
</layout>
</widget>
<widget class="QWidget" name="tab_3">
<attribute name="title">
<string>Övrigt</string>
......
......@@ -125,6 +125,12 @@ CREATE TABLE my_db_settings (
value
);
CREATE TABLE sql_queries (
id INTEGER PRIMARY KEY NOT NULL,
name TEXT NOT NULL UNIQUE,
query TEXT DEFAULT "" NOT NULL
);
-- ============================== Views ==================================
CREATE VIEW drainage_change AS -- how prices have changed due to the drainage, or null
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment