Guest User

Untitled

a guest
Feb 20th, 2018
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.61 KB | None | 0 0
  1. #load "extLib.cma"
  2.  
  3. open Printf
  4.  
  5. type change = { fn: string; ov: string; nv: string; }
  6.  
  7. class change_log = object (self)
  8. val changes = DynArray.create ()
  9. method add_change fn ov nv = DynArray.add changes {fn=fn;ov=ov;nv=nv;}
  10. method add_maybe fn ov nv = if ov != nv then self#add_change fn ov nv
  11. method changes = changes
  12. method for_update =
  13. let do_f_eq_v x = x.fn ^ "='" ^ x.nv ^ "'" in
  14. let do_pair acc x =
  15. if String.length acc > 0 then acc ^ "," ^ (do_f_eq_v x)
  16. else (do_f_eq_v x)
  17. in
  18. DynArray.fold_left do_pair "" changes
  19. end ;;
  20.  
  21. class base_model = object (self)
  22. val changes = new change_log
  23.  
  24. method before_insert = ()
  25. method before_update = ()
  26. method after_insert = ()
  27. method after_update = ()
  28. end ;;
  29.  
  30. (* Dynamically generated base user class. Program reads the database structure
  31. and generates the model classes *)
  32. class base_user = object (self)
  33. inherit base_model
  34.  
  35. val mutable id = 0
  36. val mutable code = ""
  37. val mutable password = ""
  38. val mutable first_name = ""
  39. val mutable last_name = ""
  40. val mutable version = 0
  41.  
  42. method save =
  43. match id with
  44. | 0 ->
  45. self#before_insert ;
  46. sprintf "INSERT INTO users \
  47. (code,password,first_name,last_name,version) VALUES \
  48. ('%s','%s','%s','%s',%i)" code password first_name last_name
  49. version
  50. | n ->
  51. self#before_update ;
  52. "UPDATE users SET " ^ changes#for_update ^ " WHERE id=" ^
  53. (string_of_int id)
  54.  
  55. method id = id
  56. method set_id x =
  57. changes#add_maybe "id" (string_of_int id) (string_of_int x) ;
  58. id <- x
  59.  
  60. method code = code
  61. method set_code x =
  62. changes#add_maybe "code" code x ;
  63. code <- x
  64.  
  65. method password = password
  66. method set_password x =
  67. changes#add_maybe "password" password x ;
  68. password <- x
  69.  
  70. method first_name = first_name
  71. method set_first_name x =
  72. changes#add_maybe "first_name" first_name x ;
  73. first_name <- x
  74.  
  75. method last_name = last_name
  76. method set_last_name x =
  77. changes#add_maybe "last_name" last_name x ;
  78. last_name <- x
  79.  
  80. method version = version
  81. method set_version x =
  82. changes#add_maybe "version" (string_of_int version) (string_of_int x) ;
  83. version <- x
  84. end ;;
  85.  
  86. class user = object (self)
  87. inherit base_user
  88.  
  89. method before_insert = version <- 1
  90. method before_update = version <- version + 1
  91. method full_name = first_name ^ " " ^ last_name
  92. method blinded_name = sprintf "%s %c." first_name last_name.[0]
  93. end ;;
  94.  
  95. let u = new user ;;
  96. printf "%s\n" u#save ;;
  97. u#set_id 10 ;;
  98. u#set_code "john" ;;
  99. u#set_first_name "John" ;;
  100. u#set_last_name "Doe" ;;
  101. printf "%s\n" u#save ;;
  102. printf "%s\n" u#blinded_name ;;
Add Comment
Please, Sign In to add comment