Skip to content

Support named tuples as return values #1

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

Merged
merged 1 commit into from
Jan 27, 2023
Merged

Conversation

cmd410
Copy link
Contributor

@cmd410 cmd410 commented Jan 27, 2023

So, to be frank, this PR adds the ability to define tuples for DB models beforehand and use them as return types: consider the following example:

import easy_sqlite3

type
  User = tuple[id: int, username: string, email: string]

proc createUsersTable*() {.importdb: staticRead("sql/create_users_table.sql").}
proc insertUser*(username, email: string) {.importdb: staticRead("sql/insert_user.sql").}
proc selectUserById*(id: int): User {.importdb: staticRead("sql/select_user_by_id.sql").}

selectUserById has return type annotated as User. It wasn't possible before because importdb expected only int as identifier there, so the user had no choice but to copy and paste the whole tuple definition.

Now by contrast it can be any identifier which is either int or evaluates to tuple[...] type.

It is achieved by making body parameter in importdb be typed, so that compiler binds type annotations for the proc, which we can get implementation of later.

I ran the tests with creating tables, inserting, selecting and updating data. All work as intended, should be backwards compatible, I guess.

@codehz
Copy link
Owner

codehz commented Jan 27, 2023

Cool! thanks

@codehz codehz merged commit 23e4596 into codehz:develop Jan 27, 2023
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.

2 participants