Join GitHub today
GitHub is home to over 28 million developers working together to host and review code, manage projects, and build software together.
Sign upHow can I store `chrono::Duration` in the sqlite 3 database with diesel? #1542
Comments
This comment has been minimized.
|
You will need to add a new type wrapper for the |
This comment has been minimized.
vityafx
commented
Feb 6, 2018
|
@weiznich Can't I simply implement |
This comment has been minimized.
|
No you can't because you own neither |
This comment has been minimized.
vityafx
commented
Feb 6, 2018
|
Oh, well, I was not clear enough :) I wanted this to be done in the |
This comment has been minimized.
vityafx
commented
Feb 6, 2018
•
|
@weiznich Could you help me with implementing this in my fork? It seems to me it does not work even so:
How can I implement it? I thought this way enough: impl FromSql<BigInt, Sqlite> for chrono::Duration {
fn from_sql(value: Option<&<Sqlite as Backend>::RawValue>) -> deserialize::Result<Self> {
let i64_value = <i64 as FromSql<BigInt, Sqlite>>::from_sql(value)?;
Ok(chrono::Duration::nanoseconds(i64_value))
}
}
impl ToSql<BigInt, Sqlite> for chrono::Duration {
fn to_sql<W: Write>(&self, out: &mut Output<W, Sqlite>) -> serialize::Result {
if let Some(num_nanoseconds) = self.num_nanoseconds() {
ToSql::<BigInt, Sqlite>::to_sql(&num_nanoseconds, out)
} else {
Err(format!("{:?} as nanoseconds is too larg to fit in an i64", self).into())
}
}
} |
This comment has been minimized.
|
You need to add impl for (I'm not sure if |
This comment has been minimized.
vityafx
commented
Feb 6, 2018
|
@weiznich looks like it worked, thanks. |
This comment has been minimized.
|
I don't think we would support duration support for SQLite. |
sgrif
closed this
Feb 6, 2018
This comment has been minimized.
vityafx
commented
Feb 7, 2018
|
@sgrif could you explain please? Why not? |
This comment has been minimized.
|
We don't typically support types that don't have a direct database mapping to a SQL type. Even if we did, there's no obvious way to store it, and you wouldn't be able to do anything useful with it. |
This comment has been minimized.
vityafx
commented
Feb 7, 2018
|
@sgrif This is sad. I have implemented |
This comment has been minimized.
vityafx
commented
Feb 15, 2018
|
@sgrif So, what is wrong with |
This comment has been minimized.
|
SQLite has no corresponding interval type to store it as. I'd expect |
This comment has been minimized.
vityafx
commented
Feb 16, 2018
|
@sgrif what do you think about that? https://github.com/vityafx/diesel-chrono-duration Could you review it please? :-[ |
vityafx commentedFeb 6, 2018
•
edited
I wanna have a duration field in my table. However, I don't know what type to use for it, because, looking at chrono types implementation for sqlite I think it is impossible.
According to the [
chrono::Durationdocumentation](https://docs.rs/chrono/0.4.0/chrono/struct.Duration.html), it can be stored as
BIGINTEGERin the sqlite3 table, since it is:So, my guess is to implement that via storing in sqlite3 table with
BIGINTEGERtype and implementingFromSqlandToSqltraits for thechrono::Durationby calling thechrono::Duration::nanoseconds()static method.