Repository: denoland/deno
File: ext/node/ops/sqlite/statement.rs (Rust)

Code before:
        anon_start += 1;
      }

      // SAFETY: `raw` is a valid pointer to a sqlite3_stmt.
      let sql_param_count = unsafe { ffi::sqlite3_bind_parameter_count(raw) };

      let mut positional_idx = 1;
      for i in anon_start..params.length() {
        // Find the next positional parameter slot.
        // Skip named parameters (:name, $name, @name) but include anonymous (?)
        // and numbered (?NNN) parameters.
        while positional_idx <= sql_param_count {
          // SAFETY: `raw` is a valid pointer to a sqlite3_stmt.
          let name_ptr =
            unsafe { ffi::sqlite3_bind_parameter_name(raw, positional_idx) };
          if name_ptr.is_null() {
            // Anonymous parameter - use this slot
            break;
          }
          // Check if it's a numbered parameter (starts with '?')
          // SAFETY: name_ptr is not null, checked above.
          let first_byte = unsafe { *name_ptr as u8 };
          if first_byte == b'?' {
            // Numbered parameter like ?1, ?2 - use this slot
            break;
          }
          // Named parameter (:name, $name, @name) - skip it
          positional_idx += 1;
        }

        let value = params.get(i);

        self.bind_value(scope, value, positional_idx)?;

        positional_idx += 1;
      }
    }

    Ok(())
  }
}

struct ResetGuard<'a>(&'a StatementSync);

impl Drop for ResetGuard<'_> {
  fn drop(&mut self) {
    let _ = self.0.reset();
  }
}

// Represents a single prepared statement. Cannot be initialized directly via constructor.

Review comment (suggestion):
nit, can be single if:
```suggestion
          if name_ptr.is_null()
              // SAFETY: short-circuiting guarantees name_ptr is non-null here
              || unsafe { *name_ptr as u8 == b'?' }
          {
              break;
          }
```

Code after:
        anon_start += 1;
      }

      // SAFETY: `raw` is a valid pointer to a sqlite3_stmt.
      let sql_param_count = unsafe { ffi::sqlite3_bind_parameter_count(raw) };

      let mut positional_idx = 1;
      for i in anon_start..params.length() {
        // Find the next positional parameter slot.
        // Skip named parameters (:name, $name, @name) but include anonymous (?)
        // and numbered (?NNN) parameters.
        while positional_idx <= sql_param_count {
          // SAFETY: `raw` is a valid pointer to a sqlite3_stmt.
          let name_ptr =
            unsafe { ffi::sqlite3_bind_parameter_name(raw, positional_idx) };
          if name_ptr.is_null()
            // SAFETY: short-circuiting guarantees name_ptr is non-null here
            || unsafe { *name_ptr as u8 == b'?' }
          {
            break;
          }
          // Named parameter (:name, $name, @name) - skip it
          positional_idx += 1;
        }

        let value = params.get(i);

        self.bind_value(scope, value, positional_idx)?;

        positional_idx += 1;
      }
    }

    Ok(())
  }
}

struct ResetGuard<'a>(&'a StatementSync);

impl Drop for ResetGuard<'_> {
  fn drop(&mut self) {
    let _ = self.0.reset();
  }
}

// Represents a single prepared statement. Cannot be initialized directly via constructor.
// Instances are created using `DatabaseSync#prepare`.
//
// A prepared statement is an efficient binary representation of the SQL used to create it.
#[op2]
impl StatementSync {