Skip to content

Commit

Permalink
copy-edit unsupported/feature-gated type errors in query!()
Browse files Browse the repository at this point in the history
  • Loading branch information
abonander committed Feb 14, 2020
1 parent 7d234e4 commit 4feb50e
Show file tree
Hide file tree
Showing 18 changed files with 111 additions and 50 deletions.
2 changes: 1 addition & 1 deletion sqlx-core/src/mysql/types/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ impl MySqlTypeInfo {
impl Display for MySqlTypeInfo {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
if self.id.type_name() != "<unknown>" {
write!(f, "{}", self.id)
write!(f, "{}", self.id.type_name())
} else {
write!(f, "ID {:#x}", self.id.0)
}
Expand Down
3 changes: 2 additions & 1 deletion sqlx-core/src/postgres/executor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -325,7 +325,8 @@ impl super::PgConnection {
return Ok(HashMap::new());
}

let mut query = "select types.type_id, pg_type.typname from (VALUES ".to_string();
// uppercase type names are easier to visually identify
let mut query = "select types.type_id, UPPER(pg_type.typname) from (VALUES ".to_string();
let mut args = PgArguments::default();
let mut pushed = false;

Expand Down
4 changes: 2 additions & 2 deletions sqlx-core/src/postgres/types/bool.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,13 @@ use crate::types::HasSqlType;

impl HasSqlType<bool> for Postgres {
fn type_info() -> PgTypeInfo {
PgTypeInfo::new(TypeId::BOOL, "bool")
PgTypeInfo::new(TypeId::BOOL, "BOOL")
}
}

impl HasSqlType<[bool]> for Postgres {
fn type_info() -> PgTypeInfo {
PgTypeInfo::new(TypeId::ARRAY_BOOL, "bool[]")
PgTypeInfo::new(TypeId::ARRAY_BOOL, "BOOL[]")
}
}

Expand Down
4 changes: 2 additions & 2 deletions sqlx-core/src/postgres/types/bytes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,13 @@ use crate::types::HasSqlType;

impl HasSqlType<[u8]> for Postgres {
fn type_info() -> PgTypeInfo {
PgTypeInfo::new(TypeId::BYTEA, "bytea")
PgTypeInfo::new(TypeId::BYTEA, "BYTEA")
}
}

impl HasSqlType<[&'_ [u8]]> for Postgres {
fn type_info() -> PgTypeInfo {
PgTypeInfo::new(TypeId::ARRAY_BYTEA, "bytea[]")
PgTypeInfo::new(TypeId::ARRAY_BYTEA, "BYTEA[]")
}
}

Expand Down
16 changes: 8 additions & 8 deletions sqlx-core/src/postgres/types/chrono.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,19 +12,19 @@ use crate::types::HasSqlType;

impl HasSqlType<NaiveTime> for Postgres {
fn type_info() -> PgTypeInfo {
PgTypeInfo::new(TypeId::TIME, "time")
PgTypeInfo::new(TypeId::TIME, "TIME")
}
}

impl HasSqlType<NaiveDate> for Postgres {
fn type_info() -> PgTypeInfo {
PgTypeInfo::new(TypeId::DATE, "date")
PgTypeInfo::new(TypeId::DATE, "DATE")
}
}

impl HasSqlType<NaiveDateTime> for Postgres {
fn type_info() -> PgTypeInfo {
PgTypeInfo::new(TypeId::TIMESTAMP, "timestamp")
PgTypeInfo::new(TypeId::TIMESTAMP, "TIMESTAMP")
}
}

Expand All @@ -33,25 +33,25 @@ where
Tz: TimeZone,
{
fn type_info() -> PgTypeInfo {
PgTypeInfo::new(TypeId::TIMESTAMPTZ, "timestamptz")
PgTypeInfo::new(TypeId::TIMESTAMPTZ, "TIMESTAMPTZ")
}
}

impl HasSqlType<[NaiveTime]> for Postgres {
fn type_info() -> PgTypeInfo {
PgTypeInfo::new(TypeId::ARRAY_TIME, "time[]")
PgTypeInfo::new(TypeId::ARRAY_TIME, "TIME[]")
}
}

impl HasSqlType<[NaiveDate]> for Postgres {
fn type_info() -> PgTypeInfo {
PgTypeInfo::new(TypeId::ARRAY_DATE, "date[]")
PgTypeInfo::new(TypeId::ARRAY_DATE, "DATE[]")
}
}

impl HasSqlType<[NaiveDateTime]> for Postgres {
fn type_info() -> PgTypeInfo {
PgTypeInfo::new(TypeId::ARRAY_TIMESTAMP, "timestamp[]")
PgTypeInfo::new(TypeId::ARRAY_TIMESTAMP, "TIMESTAMP[]")
}
}

Expand All @@ -60,7 +60,7 @@ where
Tz: TimeZone,
{
fn type_info() -> PgTypeInfo {
PgTypeInfo::new(TypeId::ARRAY_TIMESTAMPTZ, "timestamp[]")
PgTypeInfo::new(TypeId::ARRAY_TIMESTAMPTZ, "TIMESTAMP[]")
}
}

Expand Down
8 changes: 4 additions & 4 deletions sqlx-core/src/postgres/types/float.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,13 @@ use crate::types::HasSqlType;

impl HasSqlType<f32> for Postgres {
fn type_info() -> PgTypeInfo {
PgTypeInfo::new(TypeId::FLOAT4, "float4")
PgTypeInfo::new(TypeId::FLOAT4, "FLOAT4")
}
}

impl HasSqlType<[f32]> for Postgres {
fn type_info() -> PgTypeInfo {
PgTypeInfo::new(TypeId::ARRAY_FLOAT4, "float4[]")
PgTypeInfo::new(TypeId::ARRAY_FLOAT4, "FLOAT4[]")
}
}

Expand All @@ -33,13 +33,13 @@ impl Decode<Postgres> for f32 {

impl HasSqlType<f64> for Postgres {
fn type_info() -> PgTypeInfo {
PgTypeInfo::new(TypeId::FLOAT8, "float8")
PgTypeInfo::new(TypeId::FLOAT8, "FLOAT8")
}
}

impl HasSqlType<[f64]> for Postgres {
fn type_info() -> PgTypeInfo {
PgTypeInfo::new(TypeId::ARRAY_FLOAT8, "float8[]")
PgTypeInfo::new(TypeId::ARRAY_FLOAT8, "FLOAT8[]")
}
}

Expand Down
12 changes: 6 additions & 6 deletions sqlx-core/src/postgres/types/int.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,13 @@ use crate::types::HasSqlType;

impl HasSqlType<i16> for Postgres {
fn type_info() -> PgTypeInfo {
PgTypeInfo::new(TypeId::INT2, "int2")
PgTypeInfo::new(TypeId::INT2, "INT2")
}
}

impl HasSqlType<[i16]> for Postgres {
fn type_info() -> PgTypeInfo {
PgTypeInfo::new(TypeId::ARRAY_INT2, "int2[]")
PgTypeInfo::new(TypeId::ARRAY_INT2, "INT2[]")
}
}

Expand All @@ -33,13 +33,13 @@ impl Decode<Postgres> for i16 {

impl HasSqlType<i32> for Postgres {
fn type_info() -> PgTypeInfo {
PgTypeInfo::new(TypeId::INT4, "int4")
PgTypeInfo::new(TypeId::INT4, "INT4")
}
}

impl HasSqlType<[i32]> for Postgres {
fn type_info() -> PgTypeInfo {
PgTypeInfo::new(TypeId::ARRAY_INT4, "int4[]")
PgTypeInfo::new(TypeId::ARRAY_INT4, "INT4[]")
}
}

Expand All @@ -57,13 +57,13 @@ impl Decode<Postgres> for i32 {

impl HasSqlType<i64> for Postgres {
fn type_info() -> PgTypeInfo {
PgTypeInfo::new(TypeId::INT8, "int8")
PgTypeInfo::new(TypeId::INT8, "INT8")
}
}

impl HasSqlType<[i64]> for Postgres {
fn type_info() -> PgTypeInfo {
PgTypeInfo::new(TypeId::ARRAY_INT8, "int8[]")
PgTypeInfo::new(TypeId::ARRAY_INT8, "INT8[]")
}
}

Expand Down
4 changes: 2 additions & 2 deletions sqlx-core/src/postgres/types/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,9 +60,9 @@ impl PgTypeInfo {
impl Display for PgTypeInfo {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
if let Some(ref name) = self.name {
write!(f, "{} (type OID {})", *name, self.id.0)
write!(f, "{}", *name)
} else {
write!(f, "{}", self.id.0)
write!(f, "OID {}", self.id.0)
}
}
}
Expand Down
4 changes: 2 additions & 2 deletions sqlx-core/src/postgres/types/str.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,13 @@ use crate::Postgres;

impl HasSqlType<str> for Postgres {
fn type_info() -> PgTypeInfo {
PgTypeInfo::new(TypeId::TEXT, "text")
PgTypeInfo::new(TypeId::TEXT, "TEXT")
}
}

impl HasSqlType<[&'_ str]> for Postgres {
fn type_info() -> PgTypeInfo {
PgTypeInfo::new(TypeId::ARRAY_TEXT, "text")
PgTypeInfo::new(TypeId::ARRAY_TEXT, "TEXT")
}
}

Expand Down
4 changes: 2 additions & 2 deletions sqlx-core/src/postgres/types/uuid.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,13 @@ use crate::types::HasSqlType;

impl HasSqlType<Uuid> for Postgres {
fn type_info() -> PgTypeInfo {
PgTypeInfo::new(TypeId::UUID, "uuid")
PgTypeInfo::new(TypeId::UUID, "UUID")
}
}

impl HasSqlType<[Uuid]> for Postgres {
fn type_info() -> PgTypeInfo {
PgTypeInfo::new(TypeId::ARRAY_UUID, "uuid[]")
PgTypeInfo::new(TypeId::ARRAY_UUID, "UUID[]")
}
}

Expand Down
6 changes: 3 additions & 3 deletions sqlx-macros/src/query_macros/args.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,14 +35,14 @@ pub fn quote_args<DB: DatabaseExt>(
.ok_or_else(|| {
if let Some(feature_gate) = <DB as DatabaseExt>::get_feature_gate(&type_) {
format!(
"support for type {} of param #{} requires optional feature `{}`",
"optional feature `{}` required for type {} of param #{}",
feature_gate,
type_,
i + 1,
feature_gate
)
.into()
} else {
format!("unknown param type {} for param #{}", type_, i + 1).into()
format!("unsupported type {} for param #{}", type_, i + 1).into()
}
})
})
Expand Down
41 changes: 36 additions & 5 deletions sqlx-macros/src/query_macros/output.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,31 @@ use sqlx::describe::Describe;

use crate::database::DatabaseExt;

use std::fmt::{self, Display, Formatter};

pub struct RustColumn {
pub(super) ident: Ident,
pub(super) type_: TokenStream,
}

struct DisplayColumn<'a> {
// zero-based index, converted to 1-based number
idx: usize,
name: Option<&'a str>,
}

impl Display for DisplayColumn<'_> {
fn fmt(&self, f: &mut Formatter) -> fmt::Result {
let num = self.idx + 1;

if let Some(name) = self.name {
write!(f, "column #{} ({:?})", num, name)
} else {
write!(f, "column #{}", num)
}
}
}

pub fn columns_to_rust<DB: DatabaseExt>(describe: &Describe<DB>) -> crate::Result<Vec<RustColumn>> {
describe
.result_columns
Expand All @@ -26,15 +46,26 @@ pub fn columns_to_rust<DB: DatabaseExt>(describe: &Describe<DB>) -> crate::Resul

let type_ = <DB as DatabaseExt>::return_type_for_id(&column.type_info)
.ok_or_else(|| {
if let Some(feature_gate) = <DB as DatabaseExt>::get_feature_gate(&column.type_info) {
if let Some(feature_gate) =
<DB as DatabaseExt>::get_feature_gate(&column.type_info)
{
format!(
"support for column type {} at position {} (name: {:?}) requires optional feature `{}`",
&column.type_info, i, column.name, feature_gate
"optional feature `{feat}` required for type {ty} of {col}",
ty = &column.type_info,
feat = feature_gate,
col = DisplayColumn {
idx: i,
name: column.name.as_deref()
}
)
} else {
format!(
"unknown output type {} for column at position {} (name: {:?})",
column.type_info, i, column.name
"unsupported type {ty} of {col}",
ty = column.type_info,
col = DisplayColumn {
idx: i,
name: column.name.as_deref()
}
)
}
})?
Expand Down
6 changes: 3 additions & 3 deletions tests/ui/mysql/gated/chrono.stderr
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
error: support for column type DATE (0xa) at position 0 (name: Some("date")) requires optional feature `chrono`
error: optional feature `chrono` required for type DATE of column #1 ("date")
--> $DIR/chrono.rs:2:13
|
2 | let _ = sqlx::query!("select CONVERT(now(), DATE) date");
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
= note: this error originates in a macro outside of the current crate (in Nightly builds, run with -Z external-macro-backtrace for more info)

error: support for column type TIME (0xb) at position 0 (name: Some("time")) requires optional feature `chrono`
error: optional feature `chrono` required for type TIME of column #1 ("time")
--> $DIR/chrono.rs:4:13
|
4 | let _ = sqlx::query!("select CONVERT(now(), TIME) time");
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
= note: this error originates in a macro outside of the current crate (in Nightly builds, run with -Z external-macro-backtrace for more info)

error: support for column type DATETIME (0xc) at position 0 (name: Some("datetime")) requires optional feature `chrono`
error: optional feature `chrono` required for type DATETIME of column #1 ("datetime")
--> $DIR/chrono.rs:6:13
|
6 | let _ = sqlx::query!("select CONVERT(now(), DATETIME) datetime");
Expand Down
Loading

0 comments on commit 4feb50e

Please sign in to comment.