Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 19 additions & 11 deletions crates/sqlx-cloudflare-core/src/result.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@
#[derive(Debug, Default, Clone, PartialEq, Eq)]
pub struct QueryResult {
rows_affected: u64,
last_insert_rowid: Option<i64>,
last_insert_rowid: i64,
}

impl QueryResult {
/// A result reporting `rows_affected` and, when the backend knows one,
/// `last_insert_rowid`.
/// A result reporting `rows_affected` and `last_insert_rowid`, 0 when the
/// backend reports none.
#[must_use]
pub fn new(rows_affected: u64, last_insert_rowid: Option<i64>) -> Self {
pub fn new(rows_affected: u64, last_insert_rowid: i64) -> Self {
Self {
rows_affected,
last_insert_rowid,
Expand All @@ -22,9 +22,15 @@ impl QueryResult {
self.rows_affected
}

/// The rowid of the last row inserted, when the backend reports one.
/// The rowid of the last row inserted, or 0 when the statement inserted
/// none.
///
/// An `i64` rather than an `Option`, as sqlx-sqlite reports it, so code
/// written against `SqliteQueryResult` compiles unchanged. Rowids start
/// at 1 unless a row is given 0 explicitly, so 0 is unambiguous in
/// practice; `RETURNING` is the way to be certain.
#[must_use]
pub fn last_insert_rowid(&self) -> Option<i64> {
pub fn last_insert_rowid(&self) -> i64 {
self.last_insert_rowid
}
}
Expand All @@ -36,7 +42,9 @@ impl Extend<QueryResult> for QueryResult {
// The last statement that reported a rowid, not the last
// statement: a trailing SELECT reporting none should not erase
// the INSERT before it.
self.last_insert_rowid = result.last_insert_rowid.or(self.last_insert_rowid);
if result.last_insert_rowid != 0 {
self.last_insert_rowid = result.last_insert_rowid;
}
}
}
}
Expand Down Expand Up @@ -93,12 +101,12 @@ mod tests {
let mut result = QueryResult::default();

result.extend([
QueryResult::new(2, Some(7)),
QueryResult::new(1, Some(9)),
QueryResult::new(0, None),
QueryResult::new(2, 7),
QueryResult::new(1, 9),
QueryResult::new(0, 0),
]);

assert_eq!(result.rows_affected(), 3);
assert_eq!(result.last_insert_rowid(), Some(9));
assert_eq!(result.last_insert_rowid(), 9);
}
}
2 changes: 1 addition & 1 deletion crates/sqlx-cloudflare-d1/src/js.rs
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,7 @@ fn query_result(result: &D1ResultSys) -> Result<D1QueryResult, Error> {

Ok(QueryResult::new(
number("changes").map_or(0, whole_number_u64),
number("last_row_id").map(whole_number_i64),
number("last_row_id").map_or(0, whole_number_i64),
))
}

Expand Down
2 changes: 1 addition & 1 deletion crates/sqlx-cloudflare-d1/tests/worker/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -299,7 +299,7 @@ async fn execute_reports_changes(conn: &mut D1Connection) -> Outcome {

ensure(inserted.rows_affected() == 1, "insert rows_affected")?;
ensure(
inserted.last_insert_rowid() == Some(id),
inserted.last_insert_rowid() == id,
format!(
"last_insert_rowid {:?}, id {id}",
inserted.last_insert_rowid()
Expand Down
4 changes: 2 additions & 2 deletions crates/sqlx-cloudflare-do/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -146,8 +146,8 @@ let names: Vec<String> = conn
returning the error.
- **`rows_affected` comes from `changes()`.** Like SQLite's own count, it
covers the rows the statement itself inserted, updated or deleted, not those
its triggers touched. A statement that wrote nothing reports 0 and no
`last_insert_rowid`.
its triggers touched. A statement that wrote nothing reports 0 for both
`rows_affected` and `last_insert_rowid`.
- **No `sqlx::migrate!`.** Create the schema from the object's constructor.
- **No derived TEXT enums.** `#[derive(sqlx::Type)]` works for
`#[sqlx(transparent)]` newtypes and `#[repr(i32)]` enums, but sqlx only
Expand Down
2 changes: 1 addition & 1 deletion crates/sqlx-cloudflare-do/src/js.rs
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ pub(crate) fn execute(

let result = QueryResult::new(
u64::try_from(changes).map_err(|error| Error::Decode(error.into()))?,
Some(rowid),
rowid,
);

Ok((rows, result))
Expand Down
4 changes: 2 additions & 2 deletions crates/sqlx-cloudflare-do/tests/worker/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -360,7 +360,7 @@ async fn execute_reports_changes(conn: &mut DoConnection) -> Outcome {

ensure(inserted.rows_affected() == 1, "insert rows_affected")?;
ensure(
inserted.last_insert_rowid() == Some(id),
inserted.last_insert_rowid() == id,
format!(
"last_insert_rowid {:?}, id {id}",
inserted.last_insert_rowid()
Expand Down Expand Up @@ -735,7 +735,7 @@ async fn select_reports_no_changes(conn: &mut DoConnection) -> Outcome {
format!("SELECT rows_affected {}", selected.rows_affected()),
)?;
ensure(
selected.last_insert_rowid().is_none(),
selected.last_insert_rowid() == 0,
format!(
"SELECT last_insert_rowid {:?}",
selected.last_insert_rowid()
Expand Down
Loading