From b606e054e17d1f23e080e6c77be8148cb2690ee0 Mon Sep 17 00:00:00 2001 From: Joe Date: Sat, 9 Mar 2024 23:21:58 -0500 Subject: [PATCH 1/2] Add method for retrieiving column names --- src/executor/query.rs | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/src/executor/query.rs b/src/executor/query.rs index 01c3a1d0eb..5e715deec5 100644 --- a/src/executor/query.rs +++ b/src/executor/query.rs @@ -115,6 +115,27 @@ impl QueryResult { { Ok(T::try_get_many_by_index(self)?) } + + /// Gets all column names in the order they were written. + #[cfg(feature = "sqlx-dep")] + pub fn columns(&self) -> Vec { + use sqlx::Column; + + match &self.row { + #[cfg(feature = "sqlx-mysql")] + QueryResultRow::SqlxMySql(row) => row.columns().iter().map(|c| c.name().to_string()).collect(), + #[cfg(feature = "sqlx-postgres")] + QueryResultRow::SqlxPostgres(row) => row.columns().iter().map(|c| c.name().to_string()).collect(), + #[cfg(feature = "sqlx-sqlite")] + QueryResultRow::SqlxSqlite(row) => row.columns().iter().map(|c| c.name().to_string()).collect(), + #[cfg(feature = "mock")] + QueryResultRow::Mock(row) => row.clone().into_column_value_tuples().map(|(c, _)| c.to_string()).collect(), + #[cfg(feature = "proxy")] + QueryResultRow::Proxy(row) => row.clone().into_column_value_tuples().map(|(c, _)| c.to_string()).collect(), + #[allow(unreachable_patterns)] + _ => unreachable!(), + } + } } #[allow(unused_variables)] From df06919fc34ef2f404bdb88b2dcf6e66fa4bc375 Mon Sep 17 00:00:00 2001 From: Joe Date: Mon, 11 Mar 2024 23:32:31 -0400 Subject: [PATCH 2/2] Address PR comments as well as add test --- src/executor/query.rs | 53 +++++++++++++++++++++++++++++++++++-------- 1 file changed, 44 insertions(+), 9 deletions(-) diff --git a/src/executor/query.rs b/src/executor/query.rs index 5e715deec5..712e33f580 100644 --- a/src/executor/query.rs +++ b/src/executor/query.rs @@ -116,22 +116,36 @@ impl QueryResult { Ok(T::try_get_many_by_index(self)?) } - /// Gets all column names in the order they were written. - #[cfg(feature = "sqlx-dep")] - pub fn columns(&self) -> Vec { + /// Retrieves the names of the columns in the result set + pub fn column_names(&self) -> Vec { + #[cfg(feature = "sqlx-dep")] use sqlx::Column; match &self.row { #[cfg(feature = "sqlx-mysql")] - QueryResultRow::SqlxMySql(row) => row.columns().iter().map(|c| c.name().to_string()).collect(), + QueryResultRow::SqlxMySql(row) => { + row.columns().iter().map(|c| c.name().to_string()).collect() + } #[cfg(feature = "sqlx-postgres")] - QueryResultRow::SqlxPostgres(row) => row.columns().iter().map(|c| c.name().to_string()).collect(), + QueryResultRow::SqlxPostgres(row) => { + row.columns().iter().map(|c| c.name().to_string()).collect() + } #[cfg(feature = "sqlx-sqlite")] - QueryResultRow::SqlxSqlite(row) => row.columns().iter().map(|c| c.name().to_string()).collect(), + QueryResultRow::SqlxSqlite(row) => { + row.columns().iter().map(|c| c.name().to_string()).collect() + } #[cfg(feature = "mock")] - QueryResultRow::Mock(row) => row.clone().into_column_value_tuples().map(|(c, _)| c.to_string()).collect(), + QueryResultRow::Mock(row) => row + .clone() + .into_column_value_tuples() + .map(|(c, _)| c.to_string()) + .collect(), #[cfg(feature = "proxy")] - QueryResultRow::Proxy(row) => row.clone().into_column_value_tuples().map(|(c, _)| c.to_string()).collect(), + QueryResultRow::Proxy(row) => row + .clone() + .into_column_value_tuples() + .map(|(c, _)| c.to_string()) + .collect(), #[allow(unreachable_patterns)] _ => unreachable!(), } @@ -1279,7 +1293,11 @@ try_from_u64_err!(uuid::Uuid); #[cfg(test)] mod tests { - use super::TryGetError; + use std::collections::BTreeMap; + + use sea_query::Value; + + use super::*; use crate::error::*; #[test] @@ -1368,4 +1386,21 @@ mod tests { ) ); } + + #[test] + fn column_names_from_query_result() { + let mut values = BTreeMap::new(); + values.insert("id".to_string(), Value::Int(Some(1))); + values.insert( + "name".to_string(), + Value::String(Some(Box::new("Abc".to_owned()))), + ); + let query_result = QueryResult { + row: QueryResultRow::Mock(crate::MockRow { values }), + }; + assert_eq!( + query_result.column_names(), + vec!["id".to_owned(), "name".to_owned()] + ); + } }