Skip to content

Instantly share code, notes, and snippets.

@brookman
Last active October 23, 2022 09:32
Show Gist options
  • Select an option

  • Save brookman/6fa007b46f87c71cb5ca4eca6073b428 to your computer and use it in GitHub Desktop.

Select an option

Save brookman/6fa007b46f87c71cb5ca4eca6073b428 to your computer and use it in GitHub Desktop.

Revisions

  1. brookman revised this gist Oct 23, 2022. 1 changed file with 6 additions and 3 deletions.
    9 changes: 6 additions & 3 deletions uuid_sqlite_diesel.rs
    Original file line number Diff line number Diff line change
    @@ -17,6 +17,10 @@ impl UUID {
    pub fn random() -> Self {
    Self(uuid::Uuid::new_v4())
    }

    pub fn parse_str(string: &str) -> Result<Self, uuid::Error> {
    Uuid::parse_str(string).map(Self)
    }
    }

    impl From<UUID> for uuid::Uuid {
    @@ -35,14 +39,13 @@ impl FromSql<Text, Sqlite> for UUID {
    fn from_sql(bytes: RawValue<'_, Sqlite>) -> deserialize::Result<Self> {
    let value = <Vec<u8>>::from_sql(bytes)?;
    let string = str::from_utf8(&value)?;
    let uuid = Uuid::parse_str(string)?;
    return Ok(UUID(uuid));
    Ok(UUID::parse_str(string)?)
    }
    }

    impl ToSql<Text, Sqlite> for UUID {
    fn to_sql<'b>(&'b self, out: &mut Output<'b, '_, Sqlite>) -> serialize::Result {
    out.set_value(self.0.to_string();
    out.set_value(self.0.to_string());
    Ok(IsNull::No)
    }
    }
  2. brookman revised this gist Oct 23, 2022. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion uuid_sqlite_diesel.rs
    Original file line number Diff line number Diff line change
    @@ -42,7 +42,7 @@ impl FromSql<Text, Sqlite> for UUID {

    impl ToSql<Text, Sqlite> for UUID {
    fn to_sql<'b>(&'b self, out: &mut Output<'b, '_, Sqlite>) -> serialize::Result {
    out.set_value(format!("{}", self.0).to_string());
    out.set_value(self.0.to_string();
    Ok(IsNull::No)
    }
    }
  3. brookman created this gist Oct 23, 2022.
    48 changes: 48 additions & 0 deletions uuid_sqlite_diesel.rs
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,48 @@
    use diesel::backend::RawValue;
    use diesel::deserialize::{self, FromSql, FromSqlRow};
    use diesel::serialize::{self, IsNull, Output, ToSql};
    use diesel::sql_types::Text;
    use diesel::sqlite::Sqlite;
    use diesel::AsExpression;
    use std::fmt;
    use std::fmt::{Display, Formatter};
    use std::str;
    use uuid::{self, Uuid};

    #[derive(Debug, Clone, Copy, FromSqlRow, AsExpression, Hash, Eq, PartialEq)]
    #[sql_type = "Text"]
    pub struct UUID(pub uuid::Uuid);

    impl UUID {
    pub fn random() -> Self {
    Self(uuid::Uuid::new_v4())
    }
    }

    impl From<UUID> for uuid::Uuid {
    fn from(s: UUID) -> Self {
    s.0
    }
    }

    impl Display for UUID {
    fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
    write!(f, "{}", self.0)
    }
    }

    impl FromSql<Text, Sqlite> for UUID {
    fn from_sql(bytes: RawValue<'_, Sqlite>) -> deserialize::Result<Self> {
    let value = <Vec<u8>>::from_sql(bytes)?;
    let string = str::from_utf8(&value)?;
    let uuid = Uuid::parse_str(string)?;
    return Ok(UUID(uuid));
    }
    }

    impl ToSql<Text, Sqlite> for UUID {
    fn to_sql<'b>(&'b self, out: &mut Output<'b, '_, Sqlite>) -> serialize::Result {
    out.set_value(format!("{}", self.0).to_string());
    Ok(IsNull::No)
    }
    }