create count opcode

This commit is contained in:
pedrocarlo 2025-05-02 22:03:04 -03:00
parent 14ef25ebb8
commit b69debb8c0
3 changed files with 35 additions and 0 deletions

View file

@ -4749,6 +4749,17 @@ pub fn op_affinity(
Ok(InsnFunctionStepResult::Step)
}
pub fn op_count(
program: &Program,
state: &mut ProgramState,
insn: &Insn,
pager: &Rc<Pager>,
mv_store: Option<&Rc<MvStore>>,
) -> Result<InsnFunctionStepResult> {
state.pc += 1;
Ok(InsnFunctionStepResult::Step)
}
fn exec_lower(reg: &OwnedValue) -> Option<OwnedValue> {
match reg {
OwnedValue::Text(t) => Some(OwnedValue::build_text(&t.as_str().to_lowercase())),

View file

@ -1448,6 +1448,19 @@ pub fn insn_to_str(
.join(", ")
),
),
Insn::Count {
cursor_id,
target_reg,
exact,
} => (
"Count",
*cursor_id as i32,
*target_reg as i32,
if *exact { 0 } else { 1 },
OwnedValue::build_text(""),
0,
"".to_string(),
),
};
format!(
"{:<4} {:<17} {:<4} {:<4} {:<4} {:<13} {:<2} {}",

View file

@ -857,6 +857,16 @@ pub enum Insn {
count: NonZeroUsize,
affinities: String,
},
/// Store the number of entries (an integer value) in the table or index opened by cursor P1 in register P2.
///
/// If P3==0, then an exact count is obtained, which involves visiting every btree page of the table.
/// But if P3 is non-zero, an estimate is returned based on the current cursor position.
Count {
cursor_id: CursorID,
target_reg: usize,
exact: bool,
},
}
impl Insn {
@ -977,6 +987,7 @@ impl Insn {
Insn::NotFound { .. } => execute::op_not_found,
Insn::Affinity { .. } => execute::op_affinity,
Insn::IdxDelete { .. } => execute::op_idx_delete,
Insn::Count { .. } => execute::op_count,
}
}
}