-
-
Notifications
You must be signed in to change notification settings - Fork 2.3k
Fixed executing current SQL line not working for multiple lines SQLs #780
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fixed executing current SQL line not working for multiple lines SQLs #780
Conversation
@innermous @MKleusberg Would either of you have time to review/test this? My head is in configuration management learning at the moment, not C++, so I won't have time to look at it in the next day or two. 😦 |
And I would like for @chrisjlocke to test this, if you can, since you asked for the "execute current line" fix and it looks like you use it more often than I do (which is not at all 😆) |
No problem. I use it a lot as when creating tables, its handy (in the same tab, so you can see the fields) to create indexes, etc. Then add a test entry. Then go back and tweak that text field to hold bananas. Etc. |
Consider this. No allocations, all in-place. diff --git a/src/MainWindow.cpp b/src/MainWindow.cpp
index 8c30e1c..cdbb4a5 100644
--- a/src/MainWindow.cpp
+++ b/src/MainWindow.cpp
@@ -865,13 +865,10 @@ void MainWindow::executeQuery()
int position = editor->positionFromLineIndex(cursor_line, cursor_index);
QString entireSQL = editor->text();
- QString firstPartEntireSQL = entireSQL.left(position);
- QString secondPartEntireSQL = entireSQL.right(entireSQL.length() - position);
+ int begin = entireSQL.lastIndexOf(';', -(entireSQL.size() - position + 1)) + 1;
+ int end = entireSQL.indexOf(';', position);
- QString firstPartSQL = firstPartEntireSQL.split(";").last();
- QString lastPartSQL = secondPartEntireSQL.split(";").first();
-
- query = firstPartSQL + lastPartSQL;
+ query = entireSQL.mid(begin, end - begin);
} else {
// if a part of the query is selected, we will only execute this part
query = sqlWidget->getSelectedSql(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Look at my suggestion, or at least use QStringRefs.
It's a case of readability vs conciseness, and I find Aren't these kinds of things either: 1) not performance costly and/or 2) quite efficiently optimized out by the compiler? Having someone new come across this code, he might not understand what it does, and sometimes, the fact that a comment is needed for some code should raise some questions about the clarity of it. And this feels like premature optimization too.
so I'm better off to profile it before starting to optimize it. |
Interesting. Both ways seem ok to me... personally speaking, in this particular case the version with begin/end/query variables looks like it would be clearer. With some thoughts: 😄
|
Use stringrefs then. There's no need in QString's down here. |
@innermous, Could you please change it to use |
You guys don't value the performance. Two indexOf operators vs split everything for god's sake. |
Ok, you're right, rewrite it to use |
God, this Qt4 is awesome. |
Sorry for inconveniences. I should've known that split is not existing for QStringRef's in Qt4. |
Can you fix the Qt4 build? Also, you can change it to use |
@revolter I don't know why it failed. Merged commit doesn't have any QStringRef's. |
Actually, the merged code contains
so neither
nor
This is the Travis error:
|
@revolter my god... I screwed up... I tried to fall back to your first diff, but forgot about leftRefs... |
No problem, I'll try to push them straight from the website. |
@chrisjlocke When you get a chance, would you be ok to verify this works now? 😄 |
I certainly will once the Windows' builds are back up and running, which I
understand from your reply in another topic/thread/issue/doodah is
tomorrow. :)
|
Windows builds are back up and running. so just had a look at this. Works fine, but the obvious niggle is that this doesn't work at all:
The second statement isn't run, and no error is displayed. Obviously its not a showstopper, but 'unexpected', if you know what I mean. Just move the cursor back a character, and it'll run that line...
but if you don't know that, you'd wonder why it isn't working. Can't think of an easy workaround. Initially I could, but its big bloaty code... DB4S would be a 300 MB download... |
Well, I think it's pretty expected, as you don't have the cursor inside any SQL. It can't do both: run the current text line and run the current SQL.
Well, here you're contradicting yourself. Previously, you expected it to run the current SQL and not the "line of text", as having an SQL split on multiple lines was causing problems. |
No, I've never joined multiple SQL statements on the same line with a ; to join them, but have split SQL across multiple lines, so
So yes, guess I am contradicting myself. Sorry for the obvious confusion. |
From an end users's perspective, @chrisjlocke's first point is correct. It's extremely common to add Having to manually place the cursor before the Maybe we should check the position of the cursor before we run determine the query string? Then depending on the result of the position check, we figure out which part of the line to start working with. |
Well, I could detect if the right part from the cursor is empty (after trimming it) and select the previous SQL before the semicolon. |
That sounds workable. 😄 |
Hmmm, I can't see this getting into 3.9.1 as-is. @revolter What are the chances of getting the cursor detection position thing today? 😄 |
Why not? I think 0.0001% of people will put the cursor after the last semi-colon and start hating us for not working. It improves a lot the functionality that was in 3.8.0. I think I could start a VM and build the project there. |
It's going to be a lot more common than 0.001% of people. 😉 A very common pattern of use is to cut-n-paste a SQL command (with Just tested it here to be sure, and yeah... the execute current line button just "seems broken" from an end user perspective. eg pressing it does nothing The cursor position thing will fix that, so it works "properly" from an end user's point of view. 😄 |
Why wouldn't you cut-n-paste a SQL and then run it with the Execute action instead of Execute current line action? |
Either way should work. Having one just "not work" for no obvious reason is just not going to fly. 😉 It sounds like a fix for this by today isn't really working for you? If we delay 3.9.1 release until tomorrow would that be better? Note - It's not a bad idea anyway, as it will give some more time for translators to get things happening too. |
Yeah, but previously, it was executing the wrong SQL completely. If I manage to build the project I can fix it. And if not, I'll try to fix it blindly and send you a patch to build. But I was just trying to understand why are you seeing this corner case so important. I'll try to get it done tonight. |
Thanks @revolter. 😄 |
@chrisjlocke, I fixed the problem with the last character being a semicolon in 45affc9. |
Found another big bug in this while testing today. 😦 When the cursor is to the right of the For example: With the cursor to the right of the |
@justinclift, Fixed in 531eddb (hopefully the last commit that touches that code 😆) |
Awesome, I'll verify it here, then pull it into 3.9.1. 😄 |
Looks good here @revolter, well done. 😄 Added to 3.9.1. 🎉 |
This PR selects the text from the last semicolon (or the start) found before the cursor and up to the first semicolon (or the end) found after the cursor and executes it.
Examples:
runs the first query.
runs the second query.
runs the first query.
runs the second query.