Advertisement
Guest User

Untitled

a guest
Apr 11th, 2019
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.90 KB | None | 0 0
  1. #[macro_use]
  2. extern crate diesel;
  3.  
  4.  
  5. //use diesel::prelude::*;
  6. use diesel::mysql::MysqlConnection;
  7. use std::env;
  8. use diesel::deserialize::{self, FromSql};
  9. use diesel::serialize::{self, IsNull, Output, ToSql};
  10.  
  11. use std::io::Write;
  12.  
  13. #[macro_use]
  14. extern crate diesel_derive_newtype;
  15. use mytype::ProductID;
  16. mod mytype{
  17. use diesel::sql_types::Integer;
  18. use diesel::mysql::Mysql;
  19. use diesel::deserialize::{self, FromSql};
  20. use diesel::serialize::{self, IsNull, Output, ToSql};
  21. use std::io::Write;
  22.  
  23.  
  24. #[derive(Debug, AsExpression, FromSqlRow, PartialEq, Eq)]
  25. #[sql_type = "Integer"]
  26. pub struct ProductID(pub i32);
  27. impl ToSql<i32, Mysql> for ProductID{
  28. fn to_sql<W: Write>(&self, out: &mut Output<W, Mysql>) -> serialize::Result{
  29. //return self.0.to_sql(out);
  30. unimplemented!();
  31.  
  32. }
  33. }
  34. impl FromSql<i32, Mysql> for ProductID{
  35. fn from_sql(bytes: Option<&[u8]>) -> deserialize::Result<Self>{
  36. use diesel::sql_types::*;
  37. let res:deserialize::Result<i32> = i32::from_sql(bytes);
  38.  
  39. match res{
  40. Ok(i) => Ok(ProductID(i)),
  41. Err(e) => Err(e)
  42. }
  43. }
  44. }
  45. }
  46.  
  47.  
  48. mod models {
  49. use super::mytype::ProductID;
  50.  
  51. table! {
  52. products (product_id) {
  53. product_id -> Integer,
  54. name -> Text,
  55. weight -> Nullable<Integer>,
  56. sold -> Integer,
  57. asdf -> Integer,
  58. }
  59. }
  60.  
  61.  
  62. #[derive(Queryable)]
  63. pub struct Product {
  64. pub product_id: i32,
  65. pub name: String,
  66. pub weight: Option<i32>,
  67. pub sold: i32,
  68. pub asdf: ProductID
  69. }
  70.  
  71.  
  72.  
  73. #[derive(Insertable)]
  74. #[table_name = "products"]
  75. pub struct NewProduct<'a> {
  76. pub name: &'a String,
  77. pub weight: Option<&'a i32>,
  78. pub sold: &'a i32,
  79. pub asdf: &'a ProductID
  80. }
  81.  
  82. }
  83.  
  84.  
  85. fn main(){
  86. let prod = ProductID(5);
  87.  
  88. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement