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 upAdd an example of FromSql/ToSql for custom types #1783
Comments
This comment has been minimized.
|
https://github.com/diesel-rs/diesel/blob/master/diesel_tests/tests/custom_types.rs You can have a look here |
This comment has been minimized.
RazrFalcon
commented
Jul 9, 2018
|
@Eijebong Looks like it is postgres only. I'm using mysql. |
This comment has been minimized.
mbilker
commented
Jul 16, 2018
•
|
Disregard my comment. I didn't notice the |
This comment has been minimized.
|
For mysql it is basically the same. Just use |
This comment has been minimized.
RazrFalcon
commented
Aug 12, 2018
|
@weiznich but the example uses |
This comment has been minimized.
|
For the SQL side simply see the mysql documentation. |
This comment has been minimized.
RazrFalcon
commented
Aug 12, 2018
|
@weiznich I don't need an MySql enum. I simply want to represent a random Sql type with my own. |
This comment has been minimized.
|
Then you only need the |
This comment has been minimized.
RazrFalcon
commented
Aug 12, 2018
|
This comment has been minimized.
My mistake it is |
This comment has been minimized.
RazrFalcon
commented
Aug 13, 2018
|
It doesn't work either. Do you have a complete example? |
This comment has been minimized.
|
Something like this: (basically the same as the example above) #[derive(AsExpression, SqlRow, Debug, Clone)]
#[sql_type = "Text"]
enum MyEnum {
Foo,
Bar
}
impl ToSql<MyType, Mysql> for MyEnum {
fn to_sql<W: Write>(&self, out: &mut Output<W, Mysql>) -> serialize::Result {
let t = match *self {
MyEnum::Foo => "foo",
MyEnum::Bar => "bar"
}
<&str as ToSql<Text, Mysql>>::to_sql(t, out)
}
}
impl FromSql<MyType, Mysql> for MyEnum {
fn from_sql(bytes: Option<&[u8]>) -> deserialize::Result<Self> {
match <String as FromSql<Text, Mysql>>::from_sql(bytes)? {
"foo" => Ok(MyEnum::Foo),
"bar" => Ok(MyEnum::Bar),
_ => Err("Unrecognized enum variant".into()),
}
}
}For defining own sql types: Just see the definition of types inside of diesel |
This comment has been minimized.
RazrFalcon
commented
Aug 13, 2018
|
Finally! Here is a complete example: #[macro_use] extern crate diesel;
use diesel::*;
use diesel::deserialize::{self, FromSql};
use diesel::serialize::{self, Output, ToSql};
use diesel::mysql::{Mysql, MysqlConnection};
use diesel::sql_types::{Unsigned, Smallint};
use std::io::Write;
table! {
custom_types {
id -> Integer,
action -> Unsigned<Smallint>,
}
}
#[derive(AsExpression, FromSqlRow, PartialEq, Debug, Clone)]
#[sql_type = "Unsigned<Smallint>"]
pub enum Action {
Added = 0,
Updated = 1,
Removed = 2,
}
impl ToSql<Unsigned<Smallint>, Mysql> for Action {
fn to_sql<W: Write>(&self, out: &mut Output<W, Mysql>) -> serialize::Result {
let t = match *self {
Action::Added => 0,
Action::Updated => 1,
Action::Removed => 2,
};
<u16 as ToSql<Unsigned<Smallint>, Mysql>>::to_sql(&t, out)
}
}
impl FromSql<Unsigned<Smallint>, Mysql> for Action {
fn from_sql(bytes: Option<&[u8]>) -> deserialize::Result<Self> {
match <u16 as FromSql<Unsigned<Smallint>, Mysql>>::from_sql(bytes)? {
0 => Ok(Action::Added),
1 => Ok(Action::Updated),
2 => Ok(Action::Removed),
_ => Err("Unrecognized enum variant".into()),
}
}
}
#[derive(Insertable, Queryable, Identifiable, Debug, PartialEq)]
#[table_name = "custom_types"]
struct HasCustomTypes {
id: i32,
action: Action,
}
pub fn select() {
let conn = MysqlConnection::establish("test").unwrap();
let rows: Vec<HasCustomTypes> = custom_types::table.load(&conn).unwrap();
} |
This comment has been minimized.
0xcaff
commented
Aug 15, 2018
•
|
Here's a complete example of storing a custom type in a SQL |
RazrFalcon commentedJul 9, 2018
I've spent a few hours trying to understand how to use custom types instead of one from
sql_types, but not results.Let's say I have a
SmallIntcolumn that I want to represent asenum. How can I do this?