From 25b8fc7c04134f09ce4f68cf3c39e2ce9bd97705 Mon Sep 17 00:00:00 2001 From: Ryan Leckey Date: Wed, 15 Jul 2020 02:05:00 -0700 Subject: [PATCH] fix(sqlite): support column types changing per row --- sqlx-core/src/sqlite/connection/executor.rs | 8 ++++++ tests/sqlite/sqlite.rs | 28 +++++++++++++++++++++ 2 files changed, 36 insertions(+) diff --git a/sqlx-core/src/sqlite/connection/executor.rs b/sqlx-core/src/sqlite/connection/executor.rs index 0ab0c52f..72990770 100644 --- a/sqlx-core/src/sqlite/connection/executor.rs +++ b/sqlx-core/src/sqlite/connection/executor.rs @@ -89,6 +89,12 @@ fn emplace_row_metadata( Ok(()) } +fn update_column_type_metadata(statement: &StatementHandle, columns: &mut Vec) { + for col in columns.iter_mut() { + col.type_info = statement.column_type_info(col.ordinal); + } +} + impl<'c> Executor<'c> for &'c mut SqliteConnection { type Database = Sqlite; @@ -144,6 +150,8 @@ impl<'c> Executor<'c> for &'c mut SqliteConnection { Arc::make_mut(columns), Arc::make_mut(scratch_row_column_names), )?; + } else { + update_column_type_metadata(handle, Arc::make_mut(columns)); } match s { diff --git a/tests/sqlite/sqlite.rs b/tests/sqlite/sqlite.rs index 4c3fb9f3..e62057b8 100644 --- a/tests/sqlite/sqlite.rs +++ b/tests/sqlite/sqlite.rs @@ -92,6 +92,34 @@ async fn it_maths() -> anyhow::Result<()> { Ok(()) } +#[sqlx_macros::test] +async fn it_can_describe_with_pragma() -> anyhow::Result<()> { + use sqlx::{ValueRef, TypeInfo, Decode}; + + let mut conn = new::().await?; + + let defaults = sqlx::query("pragma table_info (tweet)") + .try_map(|row: SqliteRow| { + let val = row.try_get_raw("dflt_value")?; + let ty = val.type_info().clone(); + + let val: Option = Decode::decode(val).map_err(sqlx::Error::Decode)?; + + if val.is_some() { + assert_eq!(ty.name(), "TEXT"); + } + + Ok(val) + }) + .fetch_all(&mut conn) + .await?; + + assert_eq!(defaults[0], None); + assert_eq!(defaults[2], Some(0)); + + Ok(()) +} + #[sqlx_macros::test] async fn it_binds_positional_parameters_issue_467() -> anyhow::Result<()> { let mut conn = new::().await?;