-
-
Notifications
You must be signed in to change notification settings - Fork 520
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
Add Support For PostgreSQL Arrays In FromQueryResult Implementation Of JsonValue #1598
Changes from all commits
6a1b14b
7304c0f
75ee167
0163814
d426576
93229ce
391780b
6fe62fd
fe69df0
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -77,20 +77,33 @@ impl FromQueryResult for JsonValue { | |
crate::QueryResultRow::SqlxPostgres(row) => { | ||
use serde_json::json; | ||
use sqlx::{postgres::types::Oid, Column, Postgres, Row, Type}; | ||
|
||
for column in row.columns() { | ||
let col = if !column.name().starts_with(pre) { | ||
continue; | ||
} else { | ||
column.name().replacen(pre, "", 1) | ||
}; | ||
let col_type = column.type_info(); | ||
|
||
macro_rules! match_postgres_type { | ||
( $type: ty ) => { | ||
if <$type as Type<Postgres>>::type_info().eq(col_type) { | ||
try_get_type!($type, col) | ||
match col_type.kind() { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We try to conditionally match if the column is of type array, or not. |
||
#[cfg(feature = "postgres-array")] | ||
sqlx::postgres::PgTypeKind::Array(_) => { | ||
if <Vec<$type> as Type<Postgres>>::type_info().eq(col_type) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If column is indeed of type array, and postgres-array feature is enabled, match the column type against Vec< |
||
try_get_type!(Vec<$type>, col); | ||
} | ||
} | ||
_ => { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Otherwise, match column with given primitive type (original logic) |
||
if <$type as Type<Postgres>>::type_info().eq(col_type) { | ||
try_get_type!($type, col); | ||
} | ||
} | ||
} | ||
}; | ||
} | ||
|
||
match_postgres_type!(bool); | ||
match_postgres_type!(i8); | ||
match_postgres_type!(i16); | ||
|
@@ -126,9 +139,15 @@ impl FromQueryResult for JsonValue { | |
match_postgres_type!(rust_decimal::Decimal); | ||
#[cfg(feature = "with-json")] | ||
try_get_type!(serde_json::Value, col); | ||
#[cfg(all(feature = "with-json", feature = "postgres-array"))] | ||
try_get_type!(Vec<serde_json::Value>, col); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Since postgres also supports arrays of json or jsonb, we need to add explicit matching for it in addition to primitive arrays. |
||
try_get_type!(String, col); | ||
#[cfg(feature = "postgres-array")] | ||
try_get_type!(Vec<String>, col); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Add explicit check for String array, since it's not a primitive |
||
#[cfg(feature = "with-uuid")] | ||
try_get_type!(uuid::Uuid, col); | ||
#[cfg(all(feature = "with-uuid", feature = "postgres-array"))] | ||
try_get_type!(Vec<uuid::Uuid>, col); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Add explicit check for UUID array, since it's not a primitive |
||
try_get_type!(Vec<u8>, col); | ||
} | ||
Ok(JsonValue::Object(map)) | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The macro is used for matching primitives against column types, but doesn't consider the fact that postgres columns can be of type array (of a primitive).