Mark syntax errors in Execute SQL tab

If a syntax error is reported by SQLite during execution in the Execute
SQL tab of the main window try to locate the problematic SQL statement
and highlight it using a red squiggling line.
This commit is contained in:
Martin Kleusberg
2015-04-17 19:05:17 +02:00
parent 6fed5ec7c2
commit 649b1790e5
3 changed files with 31 additions and 4 deletions

View File

@@ -683,17 +683,24 @@ void MainWindow::executeQuery()
// Get SQL code to execute. This depends on the button that's been pressed // Get SQL code to execute. This depends on the button that's been pressed
QString query; QString query;
bool singleStep = false; bool singleStep = false;
int execution_start_line = 0;
int execution_start_index = 0;
if(sender()->objectName() == "actionSqlExecuteLine") if(sender()->objectName() == "actionSqlExecuteLine")
{ {
int cursor_line, cursor_index; int cursor_line, cursor_index;
sqlWidget->getEditor()->getCursorPosition(&cursor_line, &cursor_index); sqlWidget->getEditor()->getCursorPosition(&cursor_line, &cursor_index);
query = sqlWidget->getEditor()->text().mid(cursor_index); execution_start_line = cursor_line;
while(cursor_line < sqlWidget->getEditor()->lines())
query += sqlWidget->getEditor()->text(cursor_line++);
singleStep = true; singleStep = true;
} else { } else {
// if a part of the query is selected, we will only execute this part // if a part of the query is selected, we will only execute this part
query = sqlWidget->getSelectedSql(); query = sqlWidget->getSelectedSql();
int dummy;
if(query.isEmpty()) if(query.isEmpty())
query = sqlWidget->getSql(); query = sqlWidget->getSql();
else
sqlWidget->getEditor()->getSelection(&execution_start_line, &execution_start_index, &dummy, &dummy);
} }
if (query.isEmpty()) if (query.isEmpty())
return; return;
@@ -715,17 +722,25 @@ void MainWindow::executeQuery()
// gets finalized, see http://www.sqlite.org/lang_transaction.html // gets finalized, see http://www.sqlite.org/lang_transaction.html
db.setRestorePoint(); db.setRestorePoint();
// Remove any error indicators
sqlWidget->getEditor()->clearIndicatorRange(0, 0,
sqlWidget->getEditor()->lines(), sqlWidget->getEditor()->lineLength(sqlWidget->getEditor()->lines()),
sqlWidget->getEditor()->getErrorIndicatorNumber());
//Accept multi-line queries, by looping until the tail is empty //Accept multi-line queries, by looping until the tail is empty
QElapsedTimer timer; QElapsedTimer timer;
timer.start(); timer.start();
do do
{ {
int tail_length_before = tail_length;
const char* qbegin = tail; const char* qbegin = tail;
sql3status = sqlite3_prepare_v2(db._db,tail, tail_length, sql3status = sqlite3_prepare_v2(db._db,tail, tail_length, &vm, &tail);
&vm, &tail);
QString queryPart = QString::fromUtf8(qbegin, tail - qbegin); QString queryPart = QString::fromUtf8(qbegin, tail - qbegin);
tail_length -= (tail - qbegin); tail_length -= (tail - qbegin);
if (sql3status == SQLITE_OK){ int execution_end_index = execution_start_index + tail_length_before - tail_length;
if (sql3status == SQLITE_OK)
{
sql3status = sqlite3_step(vm); sql3status = sqlite3_step(vm);
sqlite3_finalize(vm); sqlite3_finalize(vm);
@@ -778,7 +793,11 @@ void MainWindow::executeQuery()
} else { } else {
statusMessage = QString::fromUtf8((const char*)sqlite3_errmsg(db._db)) + statusMessage = QString::fromUtf8((const char*)sqlite3_errmsg(db._db)) +
": " + queryPart; ": " + queryPart;
sqlWidget->getEditor()->fillIndicatorRange(execution_start_line, execution_start_index, execution_start_line, execution_end_index,
sqlWidget->getEditor()->getErrorIndicatorNumber());
} }
execution_start_index = execution_end_index;
} while( tail && *tail != 0 && (sql3status == SQLITE_OK || sql3status == SQLITE_DONE)); } while( tail && *tail != 0 && (sql3status == SQLITE_OK || sql3status == SQLITE_DONE));
sqlWidget->finishExecution(statusMessage); sqlWidget->finishExecution(statusMessage);
updatePlot(sqlWidget->getModel()); updatePlot(sqlWidget->getModel());

View File

@@ -82,6 +82,10 @@ SqlTextEdit::SqlTextEdit(QWidget* parent) :
// Enable folding // Enable folding
setFolding(QsciScintilla::BoxedTreeFoldStyle); setFolding(QsciScintilla::BoxedTreeFoldStyle);
// Create error indicator
errorIndicatorNumber = indicatorDefine(QsciScintilla::SquiggleIndicator);
setIndicatorForegroundColor(Qt::red, errorIndicatorNumber);
} }
SqlTextEdit::~SqlTextEdit() SqlTextEdit::~SqlTextEdit()

View File

@@ -17,6 +17,8 @@ public:
explicit SqlTextEdit(QWidget *parent = 0); explicit SqlTextEdit(QWidget *parent = 0);
virtual ~SqlTextEdit(); virtual ~SqlTextEdit();
int getErrorIndicatorNumber() const { return errorIndicatorNumber; }
static SqlUiLexer* sqlLexer; static SqlUiLexer* sqlLexer;
public slots: public slots:
@@ -28,6 +30,8 @@ protected:
private: private:
void setupSyntaxHighlightingFormat(const QString& settings_name, int style); void setupSyntaxHighlightingFormat(const QString& settings_name, int style);
int errorIndicatorNumber;
private slots: private slots:
void updateLineNumberAreaWidth(); void updateLineNumberAreaWidth();
}; };