#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "editdialog.h"
#include "finddialog.h"
#include <QtGui>
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
ui->passEdit->setEchoMode(QLineEdit::Password);
ui->splitter->setStretchFactor(1, 2);
ui->openButton->setEnabled(false);
ui->editButton->setEnabled(false);
ui->createButton->setEnabled(false);
ui->removeButton->setEnabled(false);
ui->findButton->setEnabled(false);
ui->resetButton->setEnabled(false);
ui->dsnEdit->setText(tr("ash"));
ui->passEdit->setText(tr("is"));
connect(ui->exitButton, SIGNAL(clicked()), this, SLOT(close()));
connect(ui->connectButton, SIGNAL(clicked()), this, SLOT(dbConnect()));
connect(ui->openButton, SIGNAL(clicked()), this, SLOT(dbOpen()));
connect(ui->findButton, SIGNAL(clicked()), this, SLOT(dbFind()));
connect(ui->resetButton, SIGNAL(clicked()), this, SLOT(dbReset()));
connect(ui->createButton, SIGNAL(clicked()), this, SLOT(dbCreate()));
connect(ui->editButton, SIGNAL(clicked()), this, SLOT(dbEdit()));
connect(ui->removeButton, SIGNAL(clicked()), this, SLOT(dbRemove()));
}
void MainWindow::dbConnect()
{
db = QSqlDatabase::addDatabase("QODBC3");
db.setDatabaseName(ui->dsnEdit->text());
db.setPassword(ui->passEdit->text());
if(!db.open())
QMessageBox::warning(this, tr("Ошибка соединения с базой данных"),
db.lastError().text());
else
{
QMessageBox::information(this, tr("Статус соединения"),
tr("Соединение установлено"));
}
ui->openButton->setEnabled(true);
ui->createButton->setEnabled(true);
ui->removeButton->setEnabled(true);
}
void MainWindow::dbOpen()
{
model = new QSqlTableModel(this, db);
model->setTable("books");
model->setSort(0, Qt::AscendingOrder);
model->setEditStrategy(QSqlTableModel::OnManualSubmit);
if(!model->select())
{
QMessageBox::warning(this, tr("Ошибка. Невозможно открыть таблицу"),
model->lastError().text());
return;
}
ui->dbTable->setModel(model);
ui->dbTable->setEditTriggers(QTableView::NoEditTriggers);
ui->dbTable->hideColumn(table_photo);
ui->dbTable->resizeColumnsToContents();
ui->dbTable->setCurrentIndex(model->index(0, 0));
ui->editButton->setEnabled(true);
ui->findButton->setEnabled(true);
ui->resetButton->setEnabled(true);
}
void MainWindow::dbCreate()
{
if(db.tables().contains("books") ||
db.tables().contains("BOOKS"))
{
QMessageBox::warning(this, tr("Ошибка создания"),
tr("Таблица уже создана. Отменено"));
return;
}
QSqlQuery query;
QString table_statement = "CREATE table \"BOOKS\" ("
"\"ID\" NUMBER NOT NULL,"
"\"NAME\" VARCHAR2(60) NOT NULL,"
"\"AUTH\" VARCHAR2(60) NOT NULL,"
"\"DILL\" VARCHAR2(60) NOT NULL,"
"\"DATE\" DATE NOT NULL,"
"\"HOWM\" VARCHAR2(60) NOT NULL,"
"\"HOWC\" VARCHAR2(60) NOT NULL,"
"\"COVER\" BLOB,"
"constraint \"BOOKS_PK\" primary key(\"ID\"))";
QString sequence_statement = "CREATE sequence \"BOOKS_SEQ\"";
QString trigger_statement = "CREATE trigger \"BI_BOOKS\" "
"before insert on \"BOOKS\" "
"for each row "
"begin "
"if :NEW.\"ID\" is null then "
"select \"BOOKS_SEQ\".nextval into :NEW.\"ID\" from dual; "
"end if; "
"end; ";
query.exec(table_statement);
query.exec(sequence_statement);
query.exec(trigger_statement);
QMessageBox::information(this, tr("Статус создания"),
tr("Таблица успешно создана"));
ui->createButton->setEnabled(false);
ui->removeButton->setEnabled(true);
}
void MainWindow::dbEdit()
{
if(db.isOpen() && ui->dbTable->model() != NULL)
{
QModelIndex id = ui->dbTable->currentIndex();
EditDialog *EditForm = new EditDialog(model, id.row(), this);
EditForm->exec();
ui->dbTable->resizeColumnsToContents();
delete EditForm;
}
else
{
QMessageBox::warning(this, tr("Ошибка редактирования"),
tr("Таблица не выбрана"));
}
}
void MainWindow::dbRemove()
{
if(!db.tables().contains("books") &&
!db.tables().contains("BOOKS"))
{
QMessageBox::warning(this, tr("Ошибка удаления"),
tr("Таблицы не существует. Отменено"));
return;
}
QSqlQuery query;
query.exec("DROP sequence \"BOOKS_SEQ\"");
query.exec("DROP trigger \"BI_BOOKS\"");
query.exec("DROP table \"BOOKS\"");
QMessageBox::information(this, tr("Статус удаления"),
tr("Таблица успешно удалена"));
ui->createButton->setEnabled(true);
ui->removeButton->setEnabled(false);
}
void MainWindow::dbFind()
{
if(db.isOpen() && ui->dbTable->model() != NULL)
{
FindDialog dialog(model, this);
dialog.exec();
}
else
{
QMessageBox::warning(this, tr("Ошибка поиска"),
tr("Таблица не выбрана"));
}
}
void MainWindow::dbReset()
{
model->setFilter("");
model->select();
}
MainWindow::~MainWindow()
{
delete ui;
}