-
Notifications
You must be signed in to change notification settings - Fork 5.5k
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
fix(ext/node): implement SQLite Session API #27909
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,124 @@ | ||
// Copyright 2018-2025 the Deno authors. MIT license. | ||
|
||
use std::cell::Cell; | ||
use std::cell::RefCell; | ||
use std::ffi::c_void; | ||
use std::rc::Rc; | ||
|
||
use deno_core::op2; | ||
use deno_core::GarbageCollected; | ||
use libsqlite3_sys as ffi; | ||
use serde::Deserialize; | ||
|
||
use super::SqliteError; | ||
|
||
#[derive(Deserialize)] | ||
#[serde(rename_all = "camelCase")] | ||
pub struct SessionOptions { | ||
pub table: Option<String>, | ||
pub db: Option<String>, | ||
} | ||
|
||
pub struct Session { | ||
pub(crate) inner: *mut ffi::sqlite3_session, | ||
pub(crate) freed: Cell<bool>, | ||
|
||
// Hold a strong reference to the database. | ||
pub(crate) _db: Rc<RefCell<Option<rusqlite::Connection>>>, | ||
} | ||
|
||
impl GarbageCollected for Session {} | ||
|
||
impl Drop for Session { | ||
fn drop(&mut self) { | ||
let _ = self.delete(); | ||
} | ||
} | ||
|
||
impl Session { | ||
fn delete(&self) -> Result<(), SqliteError> { | ||
if self.freed.get() { | ||
return Err(SqliteError::SessionClosed); | ||
} | ||
|
||
self.freed.set(true); | ||
// Safety: `self.inner` is a valid session. double free is | ||
// prevented by `freed` flag. | ||
unsafe { | ||
ffi::sqlite3session_delete(self.inner); | ||
} | ||
|
||
Ok(()) | ||
} | ||
} | ||
|
||
#[op2] | ||
impl Session { | ||
// Closes the session. | ||
#[fast] | ||
fn close(&self) -> Result<(), SqliteError> { | ||
self.delete() | ||
} | ||
|
||
// Retrieves a changeset containing all changes since the changeset | ||
// was created. Can be called multiple times. | ||
// | ||
// This method is a wrapper around `sqlite3session_changeset()`. | ||
#[buffer] | ||
fn changeset(&self) -> Result<Box<[u8]>, SqliteError> { | ||
if self.freed.get() { | ||
return Err(SqliteError::SessionClosed); | ||
} | ||
|
||
session_buffer_op(self.inner, ffi::sqlite3session_changeset) | ||
} | ||
|
||
// Similar to the method above, but generates a more compact patchset. | ||
// | ||
// This method is a wrapper around `sqlite3session_patchset()`. | ||
#[buffer] | ||
fn patchset(&self) -> Result<Box<[u8]>, SqliteError> { | ||
if self.freed.get() { | ||
return Err(SqliteError::SessionClosed); | ||
} | ||
|
||
session_buffer_op(self.inner, ffi::sqlite3session_patchset) | ||
} | ||
} | ||
|
||
fn session_buffer_op( | ||
s: *mut ffi::sqlite3_session, | ||
f: unsafe extern "C" fn( | ||
*mut ffi::sqlite3_session, | ||
*mut i32, | ||
*mut *mut c_void, | ||
) -> i32, | ||
) -> Result<Box<[u8]>, SqliteError> { | ||
let mut n_buffer = 0; | ||
let mut p_buffer = std::ptr::null_mut(); | ||
|
||
// Safety: `s` is a valid session and the buffer is allocated | ||
// by sqlite3 and will be freed later. | ||
let r = unsafe { f(s, &mut n_buffer, &mut p_buffer) }; | ||
if r != ffi::SQLITE_OK { | ||
return Err(SqliteError::SessionChangesetFailed); | ||
} | ||
|
||
if n_buffer == 0 { | ||
return Ok(Default::default()); | ||
} | ||
|
||
// Safety: n_buffer is the size of the buffer. | ||
let buffer = unsafe { | ||
std::slice::from_raw_parts(p_buffer as *const u8, n_buffer as usize) | ||
} | ||
.to_vec() | ||
.into_boxed_slice(); | ||
|
||
// Safety: free sqlite allocated buffer, we copied it into the JS buffer. | ||
unsafe { | ||
ffi::sqlite3_free(p_buffer); | ||
} | ||
|
||
Ok(buffer) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Do we have to create these from scratch? Can't we use tests from Node's suite?
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.
Yep we should also enable the Node.js tests.
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 guess not right now because the sqlite API is not fully on par with Node.js for the tests to pass.