Skip to content

Triying to get a DateTime<Utc> from a AnyRow. #1772

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

Closed
emuvi opened this issue Mar 31, 2022 · 7 comments
Closed

Triying to get a DateTime<Utc> from a AnyRow. #1772

emuvi opened this issue Mar 31, 2022 · 7 comments

Comments

@emuvi
Copy link

emuvi commented Mar 31, 2022

Hello,

I'm trying to get a DateTime from a AnyRow but did not found how it is done.

The function signature is:

pub fn csv_value(row: &AnyRow, index: usize, column: &AnyColumn) -> Result<String, sqlx::Error> {

The function where i'm trying is like:

let column_type = column.type_info().name();
let value = match column_type {
    "DATE" => {
        let value: Result<Option<DateTime<Utc>>, _> = row.try_get(index);

How can i get a DateTime from a AnyRow?

Thanks in advance.

@emuvi
Copy link
Author

emuvi commented Mar 31, 2022

The respective compilation unity has this imports:

use futures::TryStreamExt;
use sqlx::any::{AnyColumn, AnyConnection, AnyKind, AnyRow};
use sqlx::types::chrono::{DateTime, Utc};
use sqlx::{Column, Row, TypeInfo};

@abonander
Copy link
Collaborator

What error are you getting?

@emuvi
Copy link
Author

emuvi commented Apr 1, 2022

error[E0277]: the trait bound DateTime<Utc>: sqlx::Decode<'_, sqlx::Any> is not satisfied
--> src/utils.rs:65:63
|
65 | let value: Result<Option<DateTime>, _> = row.try_get(index);
| ^^^^^^^ the trait sqlx::Decode<'_, sqlx::Any> is not implemented for DateTime<Utc>
|
= help: the following implementations were found:
<DateTime as sqlx::Decode<'r, sqlx::Postgres>>
<DateTime as sqlx::Decode<'r, sqlx::Sqlite>>
<DateTime as sqlx::Decode<'r, sqlx::MySql>>
<DateTime as sqlx::Decode<'r, sqlx::Postgres>>
and 4 others
= note: required because of the requirements on the impl of sqlx::Decode<'_, sqlx::Any> for Option<DateTime<Utc>>
note: required by a bound in try_get
--> /home/pointel/.cargo/registry/src/github.1485827954.workers.dev-1ecc6299db9ec823/sqlx-core-0.5.11/src/row.rs:114:12
|
114 | T: Decode<'r, Self::Database> + TypeSelf::Database,
| ^^^^^^^^^^^^^^^^^^^^^^^^^^ required by this bound in try_get

error[E0277]: the trait bound DateTime<Utc>: Type<sqlx::Any> is not satisfied
--> src/utils.rs:65:63
|
65 | let value: Result<Option<DateTime>, _> = row.try_get(index);
| ^^^^^^^ the trait Type<sqlx::Any> is not implemented for DateTime<Utc>
|
= help: the following implementations were found:
<DateTime as Typesqlx::MySql>
<DateTime as Typesqlx::Postgres>
<DateTime as Typesqlx::Sqlite>
<DateTime as Typesqlx::MySql>
= note: required because of the requirements on the impl of Type<sqlx::Any> for Option<DateTime<Utc>>
note: required by a bound in try_get
--> /home/pointel/.cargo/registry/src/github.1485827954.workers.dev-1ecc6299db9ec823/sqlx-core-0.5.11/src/row.rs:114:41
|
114 | T: Decode<'r, Self::Database> + TypeSelf::Database,
| ^^^^^^^^^^^^^^^^^^^^ required by this bound in try_get

For more information about this error, try rustc --explain E0277.
error: could not compile datx due to 2 previous errors

@abonander
Copy link
Collaborator

Try enabling the chrono feature of sqlx.

@emuvi
Copy link
Author

emuvi commented Apr 1, 2022

It's enabled as:

sqlx = { version = "0.5", features = ["runtime-tokio-rustls", "all", "chrono"] }

@abonander
Copy link
Collaborator

Drop the all feature, it's confusing but it enables mssql which doesn't currently support DateTime<Utc> and so that disables that for the Any driver.

Try features = ["runtime-tokio-rustls", "all-types", "mysql", "postgres", "sqlite"]

@emuvi
Copy link
Author

emuvi commented Apr 1, 2022

It now compiles, thanks.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants