1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282
|
/***************************************************************************
* SPDX-FileCopyrightText: 2024 S. MANKOWSKI stephane@mankowski.fr
* SPDX-FileCopyrightText: 2024 G. DE BURE support@mankowski.fr
* SPDX-License-Identifier: GPL-3.0-or-later
***************************************************************************/
/** @file
* This file is a plugin for debug.
*
* @author Stephane MANKOWSKI / Guillaume DE BURE
*/
#include "skgdebugpluginwidget.h"
#include <qdom.h>
#include <qjsengine.h>
#include "skgdocument.h"
#include "skgmainpanel.h"
#include "skgservices.h"
#include "skgtraces.h"
#include "skgtransactionmng.h"
const auto EXECUTE = QStringLiteral("EXECUTE");
const auto BENCHMARK = QStringLiteral("BENCHMARK");
const auto EXECUTE_MULTI = QStringLiteral("EXECUTE_MULTI");
const auto EXPLAIN = QStringLiteral("EXPLAIN");
const auto EXPLAIN_PLAN = QStringLiteral("EXPLAIN_PLAN");
const auto RUN_SCRIPT = QStringLiteral("RUN_SCRIPT");
SKGDebugPluginWidget::SKGDebugPluginWidget(QWidget *iParent, SKGDocument *iDocument)
: SKGTabPage(iParent, iDocument)
{
SKGTRACEINFUNC(10)
if (iDocument == nullptr) {
return;
}
ui.setupUi(this);
// Set icons
ui.kSQLPushButton->setIcon(SKGServices::fromTheme(QStringLiteral("system-run")));
ui.kSQLTransactionPushButton->setIcon(SKGServices::fromTheme(QStringLiteral("system-run")));
ui.kRefreshViewsAndIndexes->setIcon(SKGServices::fromTheme(QStringLiteral("view-refresh")));
// Fill combo box
ui.kExplainCmb->addItem(SKGServices::fromTheme(QStringLiteral("system-run")), i18nc("Execute an SQL query", "Execute"), EXECUTE);
ui.kExplainCmb->addItem(SKGServices::fromTheme(QStringLiteral("system-run")), i18nc("Execute an SQL query (Benchmark)", "Execute benchmark"), BENCHMARK);
ui.kExplainCmb->addItem(SKGServices::fromTheme(QStringLiteral("system-run")),
i18nc("Execute an SQL queries (one per line)", "Execute multi queries"),
EXECUTE_MULTI);
ui.kExplainCmb->addItem(SKGServices::fromTheme(QStringLiteral("help-hint")), i18nc("Explain an SQL query", "Explain"), EXPLAIN);
ui.kExplainCmb->addItem(SKGServices::fromTheme(QStringLiteral("games-hint")), i18nc("Explain the SQL query plan", "Explain query plan"), EXPLAIN_PLAN);
ui.kExplainCmb->addItem(SKGServices::fromTheme(QStringLiteral("media-playback-start")),
i18nc("Execute script", "Execute script [%1]", "javascript"),
RUN_SCRIPT);
ui.kInput->setVisible(false);
// Set level trace
ui.kTraceLevel->setValue(SKGTraces::SKGLevelTrace);
// Set profiling mode
ui.kEnableProfilingChk->setCheckState(SKGTraces::SKGPerfo ? Qt::Checked : Qt::Unchecked);
// Init debug page
QStringList tables;
ui.kSQLInput->addItem(QStringLiteral("SELECT * FROM sqlite_master;"));
iDocument->getDistinctValues(QStringLiteral("sqlite_master"), QStringLiteral("name"), QStringLiteral("type in ('table', 'view')"), tables);
int nb = tables.count();
for (int i = 0; i < nb; ++i) {
ui.kSQLInput->addItem("SELECT * FROM " % tables.at(i) % u';');
}
ui.kSQLInput->addItem(QStringLiteral("ANALYZE;"));
ui.kSQLInput->addItem(QStringLiteral("PRAGMA integrity_check;"));
for (int i = 0; i < nb; ++i) {
ui.kSQLInput->addItem("PRAGMA table_info(" % tables.at(i) % ");");
ui.kSQLInput->addItem("PRAGMA index_list(" % tables.at(i) % ");");
}
iDocument->getDistinctValues(QStringLiteral("sqlite_master"), QStringLiteral("name"), QStringLiteral("type='index'"), tables);
nb = tables.count();
for (int i = 0; i < nb; ++i) {
ui.kSQLInput->addItem("PRAGMA index_info(" % tables.at(i) % ");");
}
connect(ui.kTraceLevel, &QSlider::valueChanged, this, &SKGDebugPluginWidget::onTraceLevelModified);
connect(ui.kEnableProfilingChk, &QCheckBox::checkStateChanged, this, &SKGDebugPluginWidget::onProfilingModeChanged);
connect(ui.kExplainCmb, static_cast<void (SKGComboBox::*)(int)>(&SKGComboBox::currentIndexChanged), this, &SKGDebugPluginWidget::onModeChanged);
connect(ui.kSQLPushButton, &QPushButton::clicked, this, &SKGDebugPluginWidget::onExecuteSqlOrder);
connect(ui.kSQLTransactionPushButton, &QPushButton::clicked, this, &SKGDebugPluginWidget::onExecuteSqlOrderInTransaction);
connect(ui.kRefreshViewsAndIndexes, &QPushButton::clicked, this, &SKGDebugPluginWidget::onRefreshViewsAndIndexes);
}
SKGDebugPluginWidget::~SKGDebugPluginWidget(){SKGTRACEINFUNC(10)}
QString SKGDebugPluginWidget::getState()
{
SKGTRACEINFUNC(10)
QDomDocument doc(QStringLiteral("SKGML"));
QDomElement root = doc.createElement(QStringLiteral("parameters"));
doc.appendChild(root);
root.setAttribute(QStringLiteral("explain"), ui.kExplainCmb->currentIndex());
root.setAttribute(QStringLiteral("enableProfiling"), ui.kEnableProfilingChk->checkState() == Qt::Checked ? QStringLiteral("Y") : QStringLiteral("N"));
root.setAttribute(QStringLiteral("levelTraces"), ui.kTraceLevel->value());
root.setAttribute(QStringLiteral("sqlOrder"), ui.kSQLInput->currentText());
return doc.toString();
}
void SKGDebugPluginWidget::setState(const QString &iState)
{
SKGTRACEINFUNC(10)
QDomDocument doc(QStringLiteral("SKGML"));
doc.setContent(iState);
QDomElement root = doc.documentElement();
QString explain = root.attribute(QStringLiteral("explain"));
QString enableProfiling = root.attribute(QStringLiteral("enableProfiling"));
QString levelTraces = root.attribute(QStringLiteral("levelTraces"));
QString sqlOrder = root.attribute(QStringLiteral("sqlOrder"));
QString sqlResult = root.attribute(QStringLiteral("sqlResult"));
if (!explain.isEmpty()) {
ui.kExplainCmb->setCurrentIndex(SKGServices::stringToInt(explain == QStringLiteral("Y") ? QStringLiteral("1") : std::move(explain)));
}
if (!enableProfiling.isEmpty()) {
ui.kEnableProfilingChk->setCheckState(enableProfiling == QStringLiteral("Y") ? Qt::Checked : Qt::Unchecked);
}
if (!levelTraces.isEmpty()) {
ui.kTraceLevel->setValue(SKGServices::stringToInt(levelTraces));
}
ui.kSQLInput->setText(sqlOrder);
ui.kSQLResult->setPlainText(sqlResult);
}
void SKGDebugPluginWidget::onExecuteSqlOrderInTransaction()
{
onExecuteSqlOrder(true);
}
SKGError SKGDebugPluginWidget::executeSqlOrders(const QStringList &iSQL, QString &oOutput)
{
SKGError err;
int nb = iSQL.count();
for (int i = 0; i < nb; ++i) {
auto sql = iSQL[i].trimmed();
if (!sql.isEmpty()) {
oOutput += sql + '\n';
QString oResult;
double time = SKGServices::getMicroTime();
err = getDocument()->dumpSelectSqliteOrder(sql, oResult);
time = SKGServices::getMicroTime() - time;
oOutput += oResult;
oOutput += i18nc("Display the execution time needed by an SQL query", "\nExecution time: %1 ms", SKGServices::doubleToString(time));
oOutput += QStringLiteral("\n\n");
}
}
IFKO(err)
{
oOutput += err.getFullMessageWithHistorical();
}
return err;
}
void SKGDebugPluginWidget::onExecuteSqlOrder(bool iInTransaction)
{
SKGTRACEINFUNC(10)
SKGError err;
auto exp = ui.kExplainCmb->currentData().toString();
if (exp == RUN_SCRIPT) {
// Script execution
ui.kSQLResult->clear();
QJSEngine myEngine;
// skgresult.setText(skgdocument.getUniqueIdentifier())
// skgerror=skgdocument.sendMessage(QStringLiteral("Hello"))
// skgerror=skgdocument.sendMessage(QStringLiteral("Hello"))
// skgmainpanel.closeAllOtherPages(skgmainpanel.currentPage())
auto t = myEngine.globalObject();
t.setProperty(QStringLiteral("skgresult"), myEngine.newQObject(ui.kSQLResult));
t.setProperty(QStringLiteral("skgdocument"), myEngine.newQObject(getDocument()));
// t.setProperty(QStringLiteral("skgerror"), myEngine.newQObject(&err));
t.setProperty(QStringLiteral("skgmainpanel"), myEngine.newQObject(SKGMainPanel::getMainPanel()));
// Finally execute the scripting code.
myEngine.evaluate(ui.kInput->toPlainText());
} else if (exp == EXECUTE_MULTI) {
// SQL multi lines
auto sqls = ui.kInput->toPlainText().split('\n');
QString oResultGlobal;
if (iInTransaction) {
SKGBEGINTRANSACTION(*getDocument(), i18nc("Display an SQL command from the debug plugin", "SQL command from debug plugin"), err)
IFOKDO(err, err = SKGDebugPluginWidget::executeSqlOrders(sqls, oResultGlobal))
} else {
QApplication::setOverrideCursor(QCursor(Qt::WaitCursor));
err = SKGDebugPluginWidget::executeSqlOrders(sqls, oResultGlobal);
QApplication::restoreOverrideCursor();
}
IFKO(err)
{
oResultGlobal += err.getFullMessageWithHistorical();
}
ui.kSQLResult->setPlainText(oResultGlobal);
} else {
// SQL execution
QString text = ui.kSQLInput->currentText();
if (exp == EXPLAIN) {
text = "EXPLAIN " % text;
} else if (exp == EXPLAIN_PLAN) {
text = "EXPLAIN QUERY PLAN " % text;
}
QString oResult;
double time = SKGServices::getMicroTime();
int max_nb = exp == BENCHMARK ? 1000 : 1; // If benchmark, execute 10 times
int max_time = 10000; // 10 seconds max
int nb_run = 0;
for (int i = 0; i < max_nb; ++i) {
if (iInTransaction) {
SKGBEGINTRANSACTION(*getDocument(), i18nc("Display an SQL command from the debug plugin", "SQL command from debug plugin"), err)
IFOKDO(err, getDocument()->dumpSelectSqliteOrder(text, oResult))
} else {
QApplication::setOverrideCursor(QCursor(Qt::WaitCursor));
err = getDocument()->dumpSelectSqliteOrder(text, oResult);
QApplication::restoreOverrideCursor();
}
nb_run++;
if (SKGServices::getMicroTime() - time > max_time) {
break; // Stop if we reach the max time
}
}
time = (SKGServices::getMicroTime() - time) / nb_run;
oResult +=
i18nc("Display the execution time needed by an SQL query",
"\nExecution time: %1 ms %2",
SKGServices::doubleToString(time),
nb_run > 1 ? "(" % i18nc("Display the average execution time of an SQL query", "Average for %1 runs", nb_run) % ")" : QStringLiteral(""));
IFOK(err)
{
ui.kSQLResult->setPlainText(oResult);
}
else
{
ui.kSQLResult->setPlainText(err.getFullMessageWithHistorical());
}
}
}
void SKGDebugPluginWidget::onTraceLevelModified()
{
SKGTRACEINFUNC(10)
SKGTraces::SKGLevelTrace = ui.kTraceLevel->value();
}
void SKGDebugPluginWidget::onModeChanged()
{
SKGTRACEINFUNC(10)
auto exp = ui.kExplainCmb->currentData().toString();
ui.kInput->setVisible(exp == RUN_SCRIPT || exp == EXECUTE_MULTI);
ui.kSQLInput->setVisible(!ui.kInput->isVisible());
}
void SKGDebugPluginWidget::onProfilingModeChanged()
{
SKGTRACEINFUNC(10)
SKGTraces::SKGPerfo = (ui.kEnableProfilingChk->checkState() == Qt::Checked);
}
void SKGDebugPluginWidget::onRefreshViewsAndIndexes()
{
SKGTRACEINFUNC(10)
QApplication::setOverrideCursor(QCursor(Qt::WaitCursor));
SKGError err;
err = getDocument()->refreshViewsIndexesAndTriggers();
IFKO(err)
{
ui.kSQLResult->setPlainText(err.getFullMessageWithHistorical());
}
QApplication::restoreOverrideCursor();
}
|