mirror of
https://github.com/aljazceru/turso.git
synced 2026-01-27 20:04:23 +01:00
Modified changes and total_changes scalarfuncs to be more like sqlite
Fmt and clippy Remove print
This commit is contained in:
@@ -422,6 +422,12 @@ impl Connection {
|
||||
fn update_last_rowid(&self, rowid: u64) {
|
||||
self.last_insert_rowid.set(rowid);
|
||||
}
|
||||
|
||||
pub fn set_changes(&self, nchange: i64) {
|
||||
self.last_change.set(nchange);
|
||||
let prev_total_changes = self.total_changes.get();
|
||||
self.total_changes.set(prev_total_changes + nchange);
|
||||
}
|
||||
}
|
||||
|
||||
pub struct Statement {
|
||||
|
||||
@@ -48,6 +48,7 @@ pub fn translate(
|
||||
syms: &SymbolTable,
|
||||
) -> Result<Program> {
|
||||
let mut program = ProgramBuilder::new();
|
||||
let mut change_cnt_on = false;
|
||||
|
||||
match stmt {
|
||||
ast::Stmt::AlterTable(_, _) => bail_parse_error!("ALTER TABLE not supported yet"),
|
||||
@@ -79,6 +80,7 @@ pub fn translate(
|
||||
limit,
|
||||
..
|
||||
} => {
|
||||
change_cnt_on = true;
|
||||
translate_delete(&mut program, schema, &tbl_name, where_clause, limit, syms)?;
|
||||
}
|
||||
ast::Stmt::Detach(_) => bail_parse_error!("DETACH not supported yet"),
|
||||
@@ -106,6 +108,7 @@ pub fn translate(
|
||||
body,
|
||||
returning,
|
||||
} => {
|
||||
change_cnt_on = true;
|
||||
translate_insert(
|
||||
&mut program,
|
||||
schema,
|
||||
@@ -120,7 +123,7 @@ pub fn translate(
|
||||
}
|
||||
}
|
||||
|
||||
Ok(program.build(database_header, connection))
|
||||
Ok(program.build(database_header, connection, change_cnt_on))
|
||||
}
|
||||
|
||||
/* Example:
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
use std::{
|
||||
cell::RefCell,
|
||||
cell::{Cell, RefCell},
|
||||
collections::HashMap,
|
||||
rc::{Rc, Weak},
|
||||
};
|
||||
@@ -327,6 +327,7 @@ impl ProgramBuilder {
|
||||
mut self,
|
||||
database_header: Rc<RefCell<DatabaseHeader>>,
|
||||
connection: Weak<Connection>,
|
||||
change_cnt_on: bool,
|
||||
) -> Program {
|
||||
self.resolve_labels();
|
||||
assert!(
|
||||
@@ -343,6 +344,8 @@ impl ProgramBuilder {
|
||||
connection,
|
||||
auto_commit: true,
|
||||
parameters: self.parameters,
|
||||
n_change: Cell::new(0),
|
||||
change_cnt_on,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -55,7 +55,7 @@ use rand::{thread_rng, Rng};
|
||||
use regex::{Regex, RegexBuilder};
|
||||
use sorter::Sorter;
|
||||
use std::borrow::BorrowMut;
|
||||
use std::cell::RefCell;
|
||||
use std::cell::{Cell, RefCell};
|
||||
use std::collections::{BTreeMap, HashMap};
|
||||
use std::num::NonZero;
|
||||
use std::rc::{Rc, Weak};
|
||||
@@ -282,6 +282,8 @@ pub struct Program {
|
||||
pub parameters: crate::parameters::Parameters,
|
||||
pub connection: Weak<Connection>,
|
||||
pub auto_commit: bool,
|
||||
pub n_change: Cell<i64>,
|
||||
pub change_cnt_on: bool,
|
||||
}
|
||||
|
||||
impl Program {
|
||||
@@ -892,11 +894,24 @@ impl Program {
|
||||
return if self.auto_commit {
|
||||
match pager.end_tx() {
|
||||
Ok(crate::storage::wal::CheckpointStatus::IO) => Ok(StepResult::IO),
|
||||
Ok(crate::storage::wal::CheckpointStatus::Done) => Ok(StepResult::Done),
|
||||
Ok(crate::storage::wal::CheckpointStatus::Done) => {
|
||||
if self.change_cnt_on {
|
||||
self.connection
|
||||
.upgrade()
|
||||
.unwrap()
|
||||
.set_changes(self.n_change.get());
|
||||
}
|
||||
Ok(StepResult::Done)
|
||||
}
|
||||
Err(e) => Err(e),
|
||||
}
|
||||
} else {
|
||||
Ok(StepResult::Done)
|
||||
if self.change_cnt_on {
|
||||
if let Some(conn) = self.connection.upgrade() {
|
||||
conn.set_changes(self.n_change.get());
|
||||
}
|
||||
}
|
||||
return Ok(StepResult::Done);
|
||||
};
|
||||
}
|
||||
Insn::Transaction { write } => {
|
||||
@@ -2076,10 +2091,9 @@ impl Program {
|
||||
if let Some(rowid) = cursor.rowid()? {
|
||||
if let Some(conn) = self.connection.upgrade() {
|
||||
conn.update_last_rowid(rowid);
|
||||
let prev_total_changes = conn.total_changes.get();
|
||||
conn.last_change.set(1);
|
||||
conn.total_changes.set(prev_total_changes + 1);
|
||||
}
|
||||
let prev_changes = self.n_change.get();
|
||||
self.n_change.set(prev_changes + 1);
|
||||
}
|
||||
}
|
||||
state.pc += 1;
|
||||
|
||||
Reference in New Issue
Block a user