-
-
Notifications
You must be signed in to change notification settings - Fork 2.2k
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
sqlite: Functions thats returns the values resulting from a 'SELECT' query and name of column - Feature ISSUE #20565 #20650
base: master
Are you sure you want to change the base?
Conversation
mut select_header := regex_opt(r'select((\s)+(all)|(distinct)(\s)+)|(\s)+')! | ||
mut from := regex_opt(r'(\s)+from(\s)+')! |
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.
hm, why is regex matching needed at all?
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.
I use the string query itself to obtain the names of the columns if they have been specified, as well as the name of the table! Since there are several ways to initiate the 'SELECT' command doc, I constructed a regex based on the syntax to facilitate obtaining the column names and the table name!
vlib/db/sqlite/sqlite.c.v
Outdated
if select_header.matches_string(query_lower) && query_lower.contains(r'from') { | ||
// The execution of this function indicates that the passed | ||
// query is syntactically correct, which is why no additional verification | ||
rows := db.exec(query)! |
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.
rows := db.exec(query)!
will return an error itself, if the query has invalid syntax.
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.
Correct, that's why I don't perform any verification other than simply checking if the query is a "select" statement beforehand. If the query isn't a select statement, I don't execute it at all!
That way, I save execution time for a query that isn't even a 'select'.
if rows.len == 0 { | ||
return []QuerySet{} | ||
} else { | ||
// Finding final index of select((\s)+(all)|(distinct)(\s)+)|(\s)+ inside query_lower string | ||
_, end_select := select_header.match_string(query_lower) |
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.
As an aspect of readability, I would mention avoiding unnecessary nested code, with a reference to the book 100 Go Mistakes and How to Avoid Them.
When an if block returns, we should omit the else block [...]
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.
That should be common sense for any language.
This pull request addresses the feature mentioned in issue #20565 and implements a function that returns the values resulting from a 'SELECT' query and the name of each column. If an alias is provided either through the 'as' command or not, the returned value becomes the alias.
'SELECT SYNTAX' - Documentation
'JOIN SYNTAX' - Documentation
Source code
Examples:
All fields
All names with explicit names
All fields with alias
Specific field
Specific field with alias and 'as'
Specific field with alias without 'as'
All tests are in:
vlib/db/sqlite/sqlite_test.v
OUTPUT TESTS