Skip to content

Commit

Permalink
Add changes tracking to the 'Connection' struct + 'Insn::InsertAwait'…
Browse files Browse the repository at this point in the history
… now affects changes counter
  • Loading branch information
Lemon-Peppermint committed Jan 2, 2025
1 parent abd8e6a commit 9109dbf
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 10 deletions.
6 changes: 6 additions & 0 deletions core/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,8 @@ impl Database {
header,
transaction_state: RefCell::new(TransactionState::None),
last_insert_rowid: Cell::new(0),
last_change: Cell::new(0),
total_changes: Cell::new(0),
});
let rows = conn.query("SELECT * FROM sqlite_schema")?;
let mut schema = schema.borrow_mut();
Expand All @@ -156,6 +158,8 @@ impl Database {
header: self.header.clone(),
last_insert_rowid: Cell::new(0),
transaction_state: RefCell::new(TransactionState::None),
last_change: Cell::new(0),
total_changes: Cell::new(0),
})
}

Expand Down Expand Up @@ -231,6 +235,8 @@ pub struct Connection {
header: Rc<RefCell<DatabaseHeader>>,
transaction_state: RefCell<TransactionState>,
last_insert_rowid: Cell<u64>,
last_change: Cell<i64>,
total_changes: Cell<i64>,
}

impl Connection {
Expand Down
18 changes: 12 additions & 6 deletions core/translate/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1012,11 +1012,14 @@ pub fn translate_expr(
unreachable!("this is always ast::Expr::Cast")
}
ScalarFunc::Changes => {
if let Some(args) = args{
crate::bail_parse_error!("{} fucntion with more than 0 arguments", srf);
if let Some(_) = args {
crate::bail_parse_error!(
"{} fucntion with more than 0 arguments",
srf
);
}
let start_reg = program.alloc_register();
program.emit_insn(Insn::Function{
program.emit_insn(Insn::Function {
constant_mask: 0,
start_reg,
dest: target_register,
Expand Down Expand Up @@ -1515,11 +1518,14 @@ pub fn translate_expr(
Ok(target_register)
}
ScalarFunc::TotalChanges => {
if let Some(args) = args {
crate::bail_parse_error!("{} fucntion with more than 0 arguments", srf.to_string());
if let Some(_) = args {
crate::bail_parse_error!(
"{} fucntion with more than 0 arguments",
srf.to_string()
);
}
let start_reg = program.alloc_register();
program.emit_insn(Insn::Function{
program.emit_insn(Insn::Function {
constant_mask: 0,
start_reg,
dest: target_register,
Expand Down
13 changes: 9 additions & 4 deletions core/vdbe/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1322,8 +1322,9 @@ impl Program {
state.registers[*dest] = result;
}
ScalarFunc::Changes => {
//placeholder
state.registers[*dest] = OwnedValue::Integer(0);
let res = &self.connection.upgrade().unwrap().last_change;
let changes = res.get();
state.registers[*dest] = OwnedValue::Integer(changes);
}
ScalarFunc::Char => {
let reg_values =
Expand Down Expand Up @@ -1534,8 +1535,9 @@ impl Program {
state.registers[*dest] = result;
}
ScalarFunc::TotalChanges => {
//placeholder
state.registers[*dest] = OwnedValue::Integer(0);
let res = &self.connection.upgrade().unwrap().total_changes;
let total_changes = res.get();
state.registers[*dest] = OwnedValue::Integer(total_changes);
}
ScalarFunc::UnixEpoch => {
if *start_reg == 0 {
Expand Down Expand Up @@ -1739,6 +1741,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);
}
}
}
Expand Down

0 comments on commit 9109dbf

Please sign in to comment.