Skip to content

Commit 02c9e74

Browse files
authored
Merge pull request #9 from rainlanguage/multiple-sql-commands
2 parents 6e3850a + 0e13611 commit 02c9e74

8 files changed

Lines changed: 678 additions & 90 deletions

File tree

‎Cargo.lock‎

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎packages/sqlite-web-core/src/coordination.rs‎

Lines changed: 24 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ use crate::util::sanitize_identifier;
1515
pub struct WorkerState {
1616
pub worker_id: String,
1717
pub is_leader: Rc<RefCell<bool>>,
18-
pub db: Rc<RefCell<Option<Rc<SQLiteDatabase>>>>,
18+
pub db: Rc<RefCell<Option<SQLiteDatabase>>>,
1919
pub channel: BroadcastChannel,
2020
pub db_name: String,
2121
pub pending_queries: Rc<RefCell<HashMap<String, PendingQuery>>>,
@@ -75,11 +75,18 @@ impl WorkerState {
7575
let channel = channel.clone();
7676

7777
spawn_local(async move {
78-
let database = db.borrow().clone();
79-
let result = if let Some(database) = database {
80-
database.exec(&sql).await
81-
} else {
82-
Err("Database not initialized".to_string())
78+
let result = {
79+
// Extract database temporarily to avoid holding RefCell across await
80+
let db_opt = db.borrow_mut().take();
81+
match db_opt {
82+
Some(mut database) => {
83+
let result = database.exec(&sql).await;
84+
// Put the database back
85+
*db.borrow_mut() = Some(database);
86+
result
87+
}
88+
None => Err("Database not initialized".to_string()),
89+
}
8390
};
8491

8592
let response = match result {
@@ -158,7 +165,7 @@ impl WorkerState {
158165
spawn_local(async move {
159166
match SQLiteDatabase::initialize_opfs(&db_name).await {
160167
Ok(database) => {
161-
*db.borrow_mut() = Some(Rc::new(database));
168+
*db.borrow_mut() = Some(database);
162169

163170
let msg = ChannelMessage::NewLeader {
164171
leader_id: worker_id.clone(),
@@ -190,11 +197,16 @@ impl WorkerState {
190197

191198
pub async fn execute_query(&self, sql: String) -> Result<String, String> {
192199
if *self.is_leader.borrow() {
193-
let database = self.db.borrow().clone();
194-
if let Some(database) = database {
195-
database.exec(&sql).await
196-
} else {
197-
Err("Database not initialized".to_string())
200+
// Extract database temporarily to avoid holding RefCell across await
201+
let db_opt = self.db.borrow_mut().take();
202+
match db_opt {
203+
Some(mut database) => {
204+
let result = database.exec(&sql).await;
205+
// Put the database back
206+
*self.db.borrow_mut() = Some(database);
207+
result
208+
}
209+
None => Err("Database not initialized".to_string()),
198210
}
199211
} else {
200212
let query_id = Uuid::new_v4().to_string();

0 commit comments

Comments
 (0)