Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #[macro_use]
- extern crate diesel;
- //use diesel::prelude::*;
- use diesel::mysql::MysqlConnection;
- use std::env;
- use diesel::deserialize::{self, FromSql};
- use diesel::serialize::{self, IsNull, Output, ToSql};
- use std::io::Write;
- #[macro_use]
- extern crate diesel_derive_newtype;
- use mytype::ProductID;
- mod mytype{
- use diesel::sql_types::Integer;
- use diesel::mysql::Mysql;
- use diesel::deserialize::{self, FromSql};
- use diesel::serialize::{self, IsNull, Output, ToSql};
- use std::io::Write;
- #[derive(Debug, AsExpression, FromSqlRow, PartialEq, Eq)]
- #[sql_type = "Integer"]
- pub struct ProductID(pub i32);
- impl ToSql<i32, Mysql> for ProductID{
- fn to_sql<W: Write>(&self, out: &mut Output<W, Mysql>) -> serialize::Result{
- //return self.0.to_sql(out);
- unimplemented!();
- }
- }
- impl FromSql<i32, Mysql> for ProductID{
- fn from_sql(bytes: Option<&[u8]>) -> deserialize::Result<Self>{
- use diesel::sql_types::*;
- let res:deserialize::Result<i32> = i32::from_sql(bytes);
- match res{
- Ok(i) => Ok(ProductID(i)),
- Err(e) => Err(e)
- }
- }
- }
- }
- mod models {
- use super::mytype::ProductID;
- table! {
- products (product_id) {
- product_id -> Integer,
- name -> Text,
- weight -> Nullable<Integer>,
- sold -> Integer,
- asdf -> Integer,
- }
- }
- #[derive(Queryable)]
- pub struct Product {
- pub product_id: i32,
- pub name: String,
- pub weight: Option<i32>,
- pub sold: i32,
- pub asdf: ProductID
- }
- #[derive(Insertable)]
- #[table_name = "products"]
- pub struct NewProduct<'a> {
- pub name: &'a String,
- pub weight: Option<&'a i32>,
- pub sold: &'a i32,
- pub asdf: &'a ProductID
- }
- }
- fn main(){
- let prod = ProductID(5);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement