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

Add support for prepared statements #13

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

Conversation

yerTools
Copy link

Hello :)

I added support for prepared query statements to improve overall performance when queries are reused.
Since both libraries (Erlang and JavaScript) use prepared statements under the hood when calling query, I split this function into prepare and query_prepared.

I also updated every test that uses sqlight.query by introducing a helper function that ensures both functions return the same errors and results.

I had to wrap the database connection in sqlight_ffi.js to keep track of the created prepared statements since the library requires that they be finalized before closing the connection. I think this isn't the most elegant solution, but I'm not aware of a better approach.

I also updated the README example, the CHANGELOG, and bumped the version to 0.10.0.

Thank you very much for the great work, and I'm hoping I didn't miss anything this time. :)

Copy link
Owner

@lpil lpil left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks great! Thank you.

Could you write a test for what happens when the connection has been closed and then the statement is run. Thank you

src/sqlight.gleam Outdated Show resolved Hide resolved
src/sqlight_ffi.erl Outdated Show resolved Hide resolved
test/sqlight_test.gleam Outdated Show resolved Hide resolved
@yerTools
Copy link
Author

Thank you very much for the code review. I wrote some tests and got inconsistent results depending on the target platform.
Since you have more experience, I wanted to ask for your opinion before addressing these missing tests.


1. Running a normal query after the connection was closed:

  • Erlang panics with the failure badarg.
  • JavaScript returns an error: SqlightError(code: GenericError, message: "Database was closed.", offset: -1)

This inconsistency existed before this PR.
Can this behavior be tolerated, or is it a bug? If it's the latter, how should it be fixed?
I think it's better to return an error than to panic, but I currently have no idea how to prevent the panic.


2. Running a prepared statement after the connection was closed:

  • Erlang returns the expected result (perhaps because the query is very simple?).
  • JavaScript returns an error: SqlightError(code: GenericError, message: "Query is finalized.", offset: -1)

Since Gleam is immutable, I don't know how to keep track of whether the connection has been closed, other than wrapping the whole thing inside a process—which either breaks target support or introduces overhead.
I think returning an error would be the right way to handle it, but I'm not sure of the best approach.


I'm looking forward to your opinion on this topic. The best solution I currently have in mind is to document it as undefined behavior on the close function. You probably have a better solution. :)

Thank you

@yerTools
Copy link
Author

yerTools commented Nov 26, 2024

Hello,

I used this version in a private project of mine, and tests failed. After a few hours of debugging, I found the problem. If you don't reset the prepared statement after an error occurs, this error will reappear when the prepared statement is used again.

This is now solved, and I added the test case which triggered this bug by not resetting the prepared statement after each use. I also set a flag to indicate that these prepared statements are persistent.

Thank you very much! :)

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