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

unix-ffi/sqlite3: Fix 64 bit support, statement finalization, and add UFI, commit, and rollback support. #905

Merged
merged 7 commits into from
Aug 22, 2024

Conversation

rhermanklink
Copy link
Contributor

@rhermanklink rhermanklink commented Jul 31, 2024

The current sqlite.py file only takes 4 bytes for pointers, disallowing 64 bit platforms, and also doesn't finalize statements except in the Cursor.close(), causing leaks and issues with Connections.close(). This PR fixes these issues by determining the pointer size dynamically for the platform, and finalizes the previous statements when a new one is made in execute().

Along with these fixes, this PR also adds the ability to use URI, and commit & rollback functionality to increase similarity with the CPython sqlite3 module. The addition of commit & rollback includes the addition of the autocommit and transaction posibilities as with the CPython sqlite3 module, with the same default mode enabled here.

@rhermanklink rhermanklink changed the title unix-ffi/sqlite3: Fix 64 bit support, statement finalization, and add UFI, commit support. unix-ffi/sqlite3: Fix 64 bit support, statement finalization, and add UFI, commit, and rollback support. Aug 6, 2024
@mattytrentini
Copy link
Contributor

This looks in a reasonable state to merge; thanks for the contribution!

I've ensured that the tests pass and, for convenience, have updated the micropython/unix container to include libffi-dev and libsqlite3-dev libraries to avoid the hassle of installing them if this module is to be used.

For reference, here's how one of the test files can be executed (running the command at the root of a micropython-lib clone):

> docker run -ti --rm -v $(pwd):/mplib -v $(pwd)/unix-ffi/sqlite3:/code -w /code micropython/unix
MicroPython v1.23.0 on 2024-08-20; linux [GCC 12.2.0] version
Use Ctrl-D to exit, Ctrl-E for paste mode
>>> import sys; sys.path.append("/mplib/unix-ffi/ffilib")
>>> import test_sqlite3
(1, 'foo', 3.0)
(3, 3, 3)
>>>

It could do with a README explaining how to use the library - but to be fair, the whole unix-ffi documentation should be improved!

Merging this as-is will mean that folks that want to use sqlite in the unix port have a good option.

rhermanklink and others added 7 commits August 22, 2024 13:09
Currently, the bytes object used to store the sqlite3 database pointer
is always 4 bytes, which causes segfaults on 64 bit platforms with 8
byte pointers. To address this, the size is now dynamically determined
using the uctypes modules pointer size.

Signed-off-by: Robert Klink <[email protected]>
Currently, statements are only finalized upon a call to Cursor.close().
However, in Cursor.execute() new statements get created without the
previous statements being finalized, causing those to get leaked,
preventing the database from being closed. The fix addresses this by
finalizing the previous statement if it exists.

Signed-off-by: Robert Klink <[email protected]>
This commit adds the ability to enable URI on the connect, as can be done
in the cpython sqlite3 module. URI allows, among other things, to create
a shared named in-memory database, which non URI filenames cannot create.

Signed-off-by: Robert Klink <[email protected]>
The sqlite3_prepare and sqlite3_close have been changed to use the v2
version. For the prepare this was done as the v1 version is "legacy",
and for close the documentation describes the v2 version to be used for
"host languages that are garbage collected, and where the order in
which destructors are called is arbitrary", which fits here.

Some clean-up to comments has also be done, and the tests now also
close the Cursor and Connections.

Signed-off-by: Robert Klink <[email protected]>
To increase the similarity between this module and CPythons sqlite3 module
the commit() and rollback() as defined in CPythons version have been
added, along with the different (auto)commit behaviors present there.
The defaults are also set to the same as in CPython, and can be changed
with the same parameters in connect(), as is showcased in the new test.

Signed-off-by: Robert Klink <[email protected]>
The previous commits fixed bugs and added new features.

Signed-off-by: Damien George <[email protected]>
@dpgeorge
Copy link
Member

Thanks for the contribution, this is a very nice improvement!

I have added commits to bump the manifest.py version, and include the sqlite3 tests in CI.

I also removed trailing whitespace in the last commit.

@dpgeorge dpgeorge merged commit 66fa62b into micropython:master Aug 22, 2024
4 checks passed
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.

3 participants