Skip to content

Commit 49b2027

Browse files
author
Jim Hamill
committed
Add button works
1 parent 327d4fd commit 49b2027

10 files changed

+91
-23
lines changed

BrowseWidget.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -86,8 +86,10 @@ void BrowseWidget::on_removeButton_clicked()
8686

8787
void BrowseWidget::on_addButton_clicked()
8888
{
89-
// Set the edit model to not save
90-
m_model->setEditStrategy(QSqlTableModel::OnManualSubmit);
89+
// Set the edit model to not save (only change if not already set, otherwise it crashes)
90+
if (m_model->editStrategy() != QSqlTableModel::OnManualSubmit) {
91+
m_model->setEditStrategy(QSqlTableModel::OnManualSubmit);
92+
}
9193

9294
// Create a new record
9395
QSqlRecord newRecord;

DatabaseConnectionWidget.cpp

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -93,17 +93,6 @@ bool DatabaseConnectionWidget::connectToDatabase(QString name, QString driver, Q
9393
return false;
9494
}
9595

96-
// Setup Explorer Widget
97-
ui->explorerTab->setDatabase(&m_database);
98-
99-
// Setup Query Widget
100-
ui->queryTab->setDatabase(&m_database);
101-
connect(ui->queryTab, SIGNAL(refreshNeeded()), this, SLOT(refresh()));
102-
103-
// Setup SQL Widget
104-
ui->sqlTab->setDatabase(m_database);
105-
connect(ui->sqlTab, SIGNAL(refreshNeeded()), this, SLOT(refresh()));
106-
10796
// If there's extra support for this driver, add it
10897
if (driver == "QMYSQL" || driver == "QMYSQL3") {
10998

@@ -123,6 +112,17 @@ bool DatabaseConnectionWidget::connectToDatabase(QString name, QString driver, Q
123112
}
124113
}
125114

115+
// Setup Explorer Widget
116+
ui->explorerTab->init(&m_database, m_extension);
117+
118+
// Setup Query Widget
119+
ui->queryTab->setDatabase(&m_database);
120+
connect(ui->queryTab, SIGNAL(refreshNeeded()), this, SLOT(refresh()));
121+
122+
// Setup SQL Widget
123+
ui->sqlTab->setDatabase(m_database);
124+
connect(ui->sqlTab, SIGNAL(refreshNeeded()), this, SLOT(refresh()));
125+
126126
return true;
127127
}
128128

ExplorerWidget.cpp

Lines changed: 31 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,12 @@
55
#include <QMessageBox>
66
#include <QSqlQuery>
77
#include <QSqlError>
8+
#include <QInputDialog>
89

910
ExplorerWidget::ExplorerWidget(QWidget *parent) :
1011
QWidget(parent),
11-
ui(new Ui::ExplorerWidget)
12+
ui(new Ui::ExplorerWidget),
13+
m_extension(NULL)
1214
{
1315
ui->setupUi(this);
1416
}
@@ -18,11 +20,14 @@ ExplorerWidget::~ExplorerWidget()
1820
delete ui;
1921
}
2022

21-
void ExplorerWidget::setDatabase(QSqlDatabase *database)
23+
void ExplorerWidget::init(QSqlDatabase *database, Extension *extension)
2224
{
2325
// Set the database
2426
m_database = database;
2527

28+
// Set the extension
29+
m_extension = extension;
30+
2631
// Set the database for child widgets
2732
ui->browseTab->setDatabase(m_database);
2833
ui->schemaTab->setDatabase(m_database);
@@ -58,15 +63,34 @@ void ExplorerWidget::on_removeButton_clicked()
5863
// Get table name
5964
QString table = ui->tableListWidget->currentItem()->text();
6065

61-
// Run the drop query
62-
QSqlQuery query = m_database->exec("DROP TABLE " + table);
66+
// Drop table if extension supports it
67+
if (m_extension) {
68+
m_extension->removeTable(table);
69+
}
6370

64-
// Check if there was an error and display it if there was
65-
if (query.lastError().type() != QSqlError::NoError) {
66-
QMessageBox::critical(this, "Operation failed", query.lastError().text());
71+
else {
72+
QMessageBox::warning(this, "Sorry", "This functionality is not available for this database type yet");
6773
}
6874

6975
// Refresh explorer
7076
refresh();
7177
}
7278
}
79+
80+
void ExplorerWidget::on_addButton_clicked()
81+
{
82+
// Get table name
83+
QString table = QInputDialog::getText(this, "Create Table", "Enter table name");
84+
85+
// Drop table if extension supports it
86+
if (m_extension) {
87+
m_extension->createTable(table);
88+
}
89+
90+
else {
91+
QMessageBox::warning(this, "Sorry", "This functionality is not available for this database type yet");
92+
}
93+
94+
// Refresh explorer
95+
refresh();
96+
}

ExplorerWidget.h

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
#include <QSqlDatabase>
66
#include <QListWidgetItem>
77

8+
#include "Extension/Extension.h"
9+
810
namespace Ui {
911
class ExplorerWidget;
1012
}
@@ -17,7 +19,7 @@ class ExplorerWidget : public QWidget
1719
explicit ExplorerWidget(QWidget *parent = 0);
1820
~ExplorerWidget();
1921

20-
void setDatabase(QSqlDatabase *database);
22+
void init(QSqlDatabase *database, Extension *extension = NULL);
2123

2224
public slots:
2325
void refresh();
@@ -27,11 +29,16 @@ private slots:
2729

2830
void on_removeButton_clicked();
2931

32+
void on_addButton_clicked();
33+
3034
private:
3135
Ui::ExplorerWidget *ui;
3236

3337
// Database
3438
QSqlDatabase *m_database;
39+
40+
// Extension
41+
Extension *m_extension;
3542
};
3643

3744
#endif // EXPLORERWIDGET_H

ExplorerWidget.ui

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@
3939
<item>
4040
<layout class="QHBoxLayout" name="horizontalLayout">
4141
<item>
42-
<widget class="QPushButton" name="pushButton_2">
42+
<widget class="QPushButton" name="addButton">
4343
<property name="text">
4444
<string>Add</string>
4545
</property>

Extension/Extension.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,16 @@ Extension::~Extension()
3939

4040
}
4141

42+
int Extension::createTable(QString table)
43+
{
44+
return false;
45+
}
46+
47+
int Extension::removeTable(QString table)
48+
{
49+
return false;
50+
}
51+
4252
QList<ExtensionTab*> Extension::getTabs()
4353
{
4454
return m_tabs;

Extension/Extension.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,9 @@ Q_OBJECT
3131
explicit Extension(QObject *parent = 0, QSqlDatabase *database = new QSqlDatabase());
3232
~Extension();
3333

34+
virtual int createTable(QString table);
35+
virtual int removeTable(QString table);
36+
3437
virtual QList<ExtensionTab*> getTabs();
3538
virtual QList<QToolBar*> getToolBars();
3639

Extension/MySQLExtension.cpp

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@
77
#include <QDir>
88
#include <QProcess>
99
#include <QMessageBox>
10+
#include <QInputDialog>
11+
#include <QSqlQuery>
12+
#include <QSqlError>
1013

1114
MySQLExtension::MySQLExtension(QObject *parent, QSqlDatabase *database) :
1215
Extension(parent, database)
@@ -50,3 +53,19 @@ MySQLExtension::~MySQLExtension()
5053
delete m_backupAction;
5154
delete m_restoreAction;
5255
}
56+
57+
int MySQLExtension::createTable(QString table)
58+
{
59+
// Run the drop query
60+
QSqlQuery query = m_database->exec("CREATE TABLE " + table + "(id INT NOT NULL AUTO_INCREMENT, PRIMARY KEY(id))");
61+
62+
return true;
63+
}
64+
65+
int MySQLExtension::removeTable(QString table)
66+
{
67+
// Run the drop query
68+
QSqlQuery query = m_database->exec("DROP TABLE " + table);
69+
70+
return true;
71+
}

Extension/MySQLExtension.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,9 @@ Q_OBJECT
1616
explicit MySQLExtension(QObject *parent = 0, QSqlDatabase *database = new QSqlDatabase());
1717
~MySQLExtension();
1818

19+
int createTable(QString table);
20+
int removeTable(QString table);
21+
1922
private:
2023
ExtensionTab *m_toolsTab;
2124
QToolBar m_toolsToolBar;

SqlWidget.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ void SqlWidget::on_runSqlButton_clicked()
8888
}
8989

9090
// Let the user know the code has been run
91-
QMessageBox::information(this, "SQL Executed Successfully", "SQL Code has been executed successfully.");
91+
QMessageBox::information(this, "SQL Executed Successfully", "SQL has been executed successfully.");
9292

9393
// Emit a signal that the database needs refreshing
9494
emit refreshNeeded();

0 commit comments

Comments
 (0)