SQL Browser▲
Sélectionnez
/**
**************************************************************************
**
** Copyright (C) 2016 The Qt Company Ltd.
** Contact: https://www.qt.io/licensing/
**
** This file is part of the demonstration applications of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:BSD$
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and The Qt Company. For licensing terms
** and conditions see https://www.qt.io/terms-conditions. For further
** information use the contact form at https://www.qt.io/contact-us.
**
** BSD License Usage
** Alternatively, you may use this file under the terms of the BSD license
** as follows:
**
** "Redistribution and use in source and binary forms, with or without
** modification, are permitted provided that the following conditions are
** met:
** * Redistributions of source code must retain the above copyright
** notice, this list of conditions and the following disclaimer.
** * Redistributions in binary form must reproduce the above copyright
** notice, this list of conditions and the following disclaimer in
** the documentation and/or other materials provided with the
** distribution.
** * Neither the name of The Qt Company Ltd nor the names of its
** contributors may be used to endorse or promote products derived
** from this software without specific prior written permission.
**
**
** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
**
** $QT_END_LICENSE$
**
***************************************************************************
*/
#include
"browser.h"
#include
"qsqlconnectiondialog.h"
#include <QtWidgets>
#include <QtSql>
Browser::
Browser(QWidget *
parent)
:
QWidget(parent)
{
setupUi(this
);
table-&
gt;addAction(insertRowAction);
table-&
gt;addAction(deleteRowAction);
table-&
gt;addAction(fieldStrategyAction);
table-&
gt;addAction(rowStrategyAction);
table-&
gt;addAction(manualStrategyAction);
table-&
gt;addAction(submitAction);
table-&
gt;addAction(revertAction);
table-&
gt;addAction(selectAction);
if
(QSqlDatabase::
drivers().isEmpty())
QMessageBox::
information(this
, tr("No database drivers found"
),
tr("This demo requires at least one Qt database driver. "
"Please check the documentation how to build the "
"Qt SQL plugins."
));
emit statusMessage(tr("Ready."
));
}
Browser::
~
Browser()
{
}
void
Browser::
exec()
{
QSqlQueryModel *
model =
new
QSqlQueryModel(table);
model-&
gt;setQuery(QSqlQuery(sqlEdit-&
gt;toPlainText(), connectionWidget-&
gt;currentDatabase()));
table-&
gt;setModel(model);
if
(model-&
gt;lastError().type() !=
QSqlError::
NoError)
emit statusMessage(model-&
gt;lastError().text());
else
if
(model-&
gt;query().isSelect())
emit statusMessage(tr("Query OK."
));
else
emit statusMessage(tr("Query OK, number of affected rows: %1"
).arg(
model-&
gt;query().numRowsAffected()));
updateActions();
}
QSqlError Browser::
addConnection(const
QString &
amp;driver, const
QString &
amp;dbName, const
QString &
amp;host,
const
QString &
amp;user, const
QString &
amp;passwd, int
port)
{
static
int
cCount =
0
;
QSqlError err;
QSqlDatabase db =
QSqlDatabase::
addDatabase(driver, QString("Browser%1"
).arg(++
cCount));
db.setDatabaseName(dbName);
db.setHostName(host);
db.setPort(port);
if
(!
db.open(user, passwd)) {
err =
db.lastError();
db =
QSqlDatabase();
QSqlDatabase::
removeDatabase(QString("Browser%1"
).arg(cCount));
}
connectionWidget-&
gt;refresh();
return
err;
}
void
Browser::
addConnection()
{
QSqlConnectionDialog dialog(this
);
if
(dialog.exec() !=
QDialog::
Accepted)
return
;
if
(dialog.useInMemoryDatabase()) {
QSqlDatabase::
database("in_mem_db"
, false
).close();
QSqlDatabase::
removeDatabase("in_mem_db"
);
QSqlDatabase db =
QSqlDatabase::
addDatabase("QSQLITE"
, "in_mem_db"
);
db.setDatabaseName(":memory:"
);
if
(!
db.open())
QMessageBox::
warning(this
, tr("Unable to open database"
), tr("An error occurred while "
"opening the connection: "
) +
db.lastError().text());
QSqlQuery q(""
, db);
q.exec("drop table Movies"
);
q.exec("drop table Names"
);
q.exec("create table Movies (id integer primary key, Title varchar, Director varchar, Rating number)"
);
q.exec("insert into Movies values (0, 'Metropolis', 'Fritz Lang', '8.4')"
);
q.exec("insert into Movies values (1, 'Nosferatu, eine Symphonie des Grauens', 'F.W. Murnau', '8.1')"
);
q.exec("insert into Movies values (2, 'Bis ans Ende der Welt', 'Wim Wenders', '6.5')"
);
q.exec("insert into Movies values (3, 'Hardware', 'Richard Stanley', '5.2')"
);
q.exec("insert into Movies values (4, 'Mitchell', 'Andrew V. McLaglen', '2.1')"
);
q.exec("create table Names (id integer primary key, Firstname varchar, Lastname varchar, City varchar)"
);
q.exec("insert into Names values (0, 'Sala', 'Palmer', 'Morristown')"
);
q.exec("insert into Names values (1, 'Christopher', 'Walker', 'Morristown')"
);
q.exec("insert into Names values (2, 'Donald', 'Duck', 'Andeby')"
);
q.exec("insert into Names values (3, 'Buck', 'Rogers', 'Paris')"
);
q.exec("insert into Names values (4, 'Sherlock', 'Holmes', 'London')"
);
connectionWidget-&
gt;refresh();
}
else
{
QSqlError err =
addConnection(dialog.driverName(), dialog.databaseName(), dialog.hostName(),
dialog.userName(), dialog.password(), dialog.port());
if
(err.type() !=
QSqlError::
NoError)
QMessageBox::
warning(this
, tr("Unable to open database"
), tr("An error occurred while "
"opening the connection: "
) +
err.text());
}
}
void
Browser::
showTable(const
QString &
amp;t)
{
QSqlTableModel *
model =
new
CustomModel(table, connectionWidget-&
gt;currentDatabase());
model-&
gt;setEditStrategy(QSqlTableModel::
OnRowChange);
model-&
gt;setTable(connectionWidget-&
gt;currentDatabase().driver()-&
gt;escapeIdentifier(t, QSqlDriver::
TableName));
model-&
gt;select();
if
(model-&
gt;lastError().type() !=
QSqlError::
NoError)
emit statusMessage(model-&
gt;lastError().text());
table-&
gt;setModel(model);
table-&
gt;setEditTriggers(QAbstractItemView::
DoubleClicked|
QAbstractItemView::
EditKeyPressed);
connect(table-&
gt;selectionModel(), &
amp;QItemSelectionModel::
currentRowChanged,
this
, &
amp;Browser::
currentChanged);
updateActions();
}
void
Browser::
showMetaData(const
QString &
amp;t)
{
QSqlRecord rec =
connectionWidget-&
gt;currentDatabase().record(t);
QStandardItemModel *
model =
new
QStandardItemModel(table);
model-&
gt;insertRows(0
, rec.count());
model-&
gt;insertColumns(0
, 7
);
model-&
gt;setHeaderData(0
, Qt::
Horizontal, "Fieldname"
);
model-&
gt;setHeaderData(1
, Qt::
Horizontal, "Type"
);
model-&
gt;setHeaderData(2
, Qt::
Horizontal, "Length"
);
model-&
gt;setHeaderData(3
, Qt::
Horizontal, "Precision"
);
model-&
gt;setHeaderData(4
, Qt::
Horizontal, "Required"
);
model-&
gt;setHeaderData(5
, Qt::
Horizontal, "AutoValue"
);
model-&
gt;setHeaderData(6
, Qt::
Horizontal, "DefaultValue"
);
for
(int
i =
0
; i &
lt; rec.count(); ++
i) {
QSqlField fld =
rec.field(i);
model-&
gt;setData(model-&
gt;index(i, 0
), fld.name());
model-&
gt;setData(model-&
gt;index(i, 1
), fld.typeID() ==
-
1
? QString(QMetaType::
typeName(fld.type()))
:
QString("%1 (%2)"
).arg(QMetaType::
typeName(fld.type())).arg(fld.typeID()));
model-&
gt;setData(model-&
gt;index(i, 2
), fld.length());
model-&
gt;setData(model-&
gt;index(i, 3
), fld.precision());
model-&
gt;setData(model-&
gt;index(i, 4
), fld.requiredStatus() ==
-
1
? QVariant("?"
)
:
QVariant(bool
(fld.requiredStatus())));
model-&
gt;setData(model-&
gt;index(i, 5
), fld.isAutoValue());
model-&
gt;setData(model-&
gt;index(i, 6
), fld.defaultValue());
}
table-&
gt;setModel(model);
table-&
gt;setEditTriggers(QAbstractItemView::
NoEditTriggers);
updateActions();
}
void
Browser::
insertRow()
{
QSqlTableModel *
model =
qobject_cast&
lt;QSqlTableModel *&
gt;(table-&
gt;model());
if
(!
model)
return
;
QModelIndex insertIndex =
table-&
gt;currentIndex();
int
row =
insertIndex.row() ==
-
1
? 0
: insertIndex.row();
model-&
gt;insertRow(row);
insertIndex =
model-&
gt;index(row, 0
);
table-&
gt;setCurrentIndex(insertIndex);
table-&
gt;edit(insertIndex);
}
void
Browser::
deleteRow()
{
QSqlTableModel *
model =
qobject_cast&
lt;QSqlTableModel *&
gt;(table-&
gt;model());
if
(!
model)
return
;
QModelIndexList currentSelection =
table-&
gt;selectionModel()-&
gt;selectedIndexes();
for
(int
i =
0
; i &
lt; currentSelection.count(); ++
i) {
if
(currentSelection.at(i).column() !=
0
)
continue
;
model-&
gt;removeRow(currentSelection.at(i).row());
}
updateActions();
}
void
Browser::
updateActions()
{
QSqlTableModel *
tm =
qobject_cast&
lt;QSqlTableModel *&
gt;(table-&
gt;model());
bool
enableIns =
tm;
bool
enableDel =
enableIns &
amp;&
amp; table-&
gt;currentIndex().isValid();
insertRowAction-&
gt;setEnabled(enableIns);
deleteRowAction-&
gt;setEnabled(enableDel);
fieldStrategyAction-&
gt;setEnabled(tm);
rowStrategyAction-&
gt;setEnabled(tm);
manualStrategyAction-&
gt;setEnabled(tm);
submitAction-&
gt;setEnabled(tm);
revertAction-&
gt;setEnabled(tm);
selectAction-&
gt;setEnabled(tm);
if
(tm) {
QSqlTableModel::
EditStrategy es =
tm-&
gt;editStrategy();
fieldStrategyAction-&
gt;setChecked(es ==
QSqlTableModel::
OnFieldChange);
rowStrategyAction-&
gt;setChecked(es ==
QSqlTableModel::
OnRowChange);
manualStrategyAction-&
gt;setChecked(es ==
QSqlTableModel::
OnManualSubmit);
}
}
void
Browser::
about()
{
QMessageBox::
about(this
, tr("About"
), tr("The SQL Browser demonstration "
"shows how a data browser can be used to visualize the results of SQL"
"statements on a live database"
));
}
void
Browser::
on_fieldStrategyAction_triggered()
{
QSqlTableModel *
tm =
qobject_cast&
lt;QSqlTableModel *&
gt;(table-&
gt;model());
if
(tm)
tm-&
gt;setEditStrategy(QSqlTableModel::
OnFieldChange);
}
void
Browser::
on_rowStrategyAction_triggered()
{
QSqlTableModel *
tm =
qobject_cast&
lt;QSqlTableModel *&
gt;(table-&
gt;model());
if
(tm)
tm-&
gt;setEditStrategy(QSqlTableModel::
OnRowChange);
}
void
Browser::
on_manualStrategyAction_triggered()
{
QSqlTableModel *
tm =
qobject_cast&
lt;QSqlTableModel *&
gt;(table-&
gt;model());
if
(tm)
tm-&
gt;setEditStrategy(QSqlTableModel::
OnManualSubmit);
}
void
Browser::
on_submitAction_triggered()
{
QSqlTableModel *
tm =
qobject_cast&
lt;QSqlTableModel *&
gt;(table-&
gt;model());
if
(tm)
tm-&
gt;submitAll();
}
void
Browser::
on_revertAction_triggered()
{
QSqlTableModel *
tm =
qobject_cast&
lt;QSqlTableModel *&
gt;(table-&
gt;model());
if
(tm)
tm-&
gt;revertAll();
}
void
Browser::
on_selectAction_triggered()
{
QSqlTableModel *
tm =
qobject_cast&
lt;QSqlTableModel *&
gt;(table-&
gt;model());
if
(tm)
tm-&
gt;select();
}