Skip to content
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

Open
wants to merge 5 commits into
base: master
Choose a base branch
from

Conversation

viniciusfdasilva
Copy link
Contributor

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

r := db.get_queryset('SELECT * FROM repository')! 

query1

All names with explicit names

r := db.get_queryset('SELECT name, id FROM repository')!

query2

All fields with alias

r := db.get_queryset('SELECT name as n, id i FROM repository')!

query3

Specific field

r := db.get_queryset('SELECT name FROM repository')! 

query4

Specific field with alias and 'as'

r := db.get_queryset('SELECT name as n FROM repository')! 

query5e6

Specific field with alias without 'as'

r := db.get_queryset('SELECT name n FROM repository')! 

query5e6

All tests are in: vlib/db/sqlite/sqlite_test.v

OUTPUT TESTS

test

Comment on lines +264 to +265
mut select_header := regex_opt(r'select((\s)+(all)|(distinct)(\s)+)|(\s)+')!
mut from := regex_opt(r'(\s)+from(\s)+')!
Copy link
Member

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?

Copy link
Contributor Author

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!

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)!
Copy link
Member

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.

Copy link
Contributor Author

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'.

@viniciusfdasilva viniciusfdasilva marked this pull request as draft February 16, 2024 22:44
@viniciusfdasilva viniciusfdasilva marked this pull request as ready for review February 16, 2024 22:44
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants