Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(#2407): respect the HaltIfNull opcode when determining nullability #2409

Merged
merged 1 commit into from
Mar 20, 2023
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
11 changes: 11 additions & 0 deletions sqlx-sqlite/src/connection/explain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,7 @@ const OP_CONCAT: &str = "Concat";
const OP_OFFSET_LIMIT: &str = "OffsetLimit";
const OP_RESULT_ROW: &str = "ResultRow";
const OP_HALT: &str = "Halt";
const OP_HALT_IF_NULL: &str = "HaltIfNull";

const MAX_LOOP_COUNT: u8 = 2;

Expand Down Expand Up @@ -942,6 +943,16 @@ pub(super) fn explain(
state.r.insert(p2, RegDataType::Single(ColumnType::null()));
}

// if there is a value in p3, and the query passes, then
// we know that it is not nullable
OP_HALT_IF_NULL => {
if let Some(RegDataType::Single(ColumnType::Single { nullable, .. })) =
state.r.get_mut(&p3)
{
*nullable = Some(false);
}
}

OP_FUNCTION => {
// r[p1] = func( _ )
match from_utf8(p4).map_err(Error::protocol)? {
Expand Down
16 changes: 16 additions & 0 deletions tests/sqlite/describe.rs
Original file line number Diff line number Diff line change
Expand Up @@ -250,6 +250,22 @@ async fn it_describes_insert_with_returning() -> anyhow::Result<()> {
Ok(())
}

#[sqlx_macros::test]
async fn it_describes_bound_columns_non_null() -> anyhow::Result<()> {
let mut conn = new::<Sqlite>().await?;
let d = conn
.describe("INSERT INTO tweet (id, text) VALUES ($1, $2) returning *")
.await?;

assert_eq!(d.columns().len(), 4);
assert_eq!(d.column(0).type_info().name(), "INTEGER");
assert_eq!(d.nullable(0), Some(false));
assert_eq!(d.column(1).type_info().name(), "TEXT");
assert_eq!(d.nullable(1), Some(false));

Ok(())
}

#[sqlx_macros::test]
async fn it_describes_update_with_returning() -> anyhow::Result<()> {
let mut conn = new::<Sqlite>().await?;
Expand Down