Guest User

Untitled

a guest
Feb 20th, 2018
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.81 KB | None | 0 0
  1. #load "extLib.cma"
  2.  
  3. open Printf
  4.  
  5. (* Record changes and report changes *)
  6. module HasChanged =
  7. struct
  8. type change_rec = { fn: string ; ov: string ; nv: string ; }
  9.  
  10. let create () = DynArray.create () ;;
  11. let to_list changes = DynArray.to_list changes ;;
  12.  
  13. let change fn ov nv = {fn=fn; ov=ov; nv=nv;}
  14. let add_maybe changes fn ov nv =
  15. if ov != nv then DynArray.add changes (change fn ov nv) ;;
  16. let field_names a b =
  17. if String.length a > 0
  18. then a ^ "," ^ b.fn
  19. else b.fn ;;
  20. let field_values a b =
  21. if String.length a > 0
  22. then a ^ ",'" ^ b.nv ^ "'"
  23. else "'" ^ b.nv ^ "'";;
  24. let field_name_value_pair a b =
  25. if String.length a > 0
  26. then a ^ "," ^ b.fn ^ "='" ^ b.nv ^ "'"
  27. else b.fn ^ "='" ^ b.nv ^ "'" ;;
  28. end ;;
  29.  
  30. let update_stmt table id changes =
  31. let vp = List.fold_left HasChanged.field_name_value_pair "" changes in
  32. sprintf "UPDATE %s SET %s WHERE id=%i" table vp id ;;
  33.  
  34. let add_callback v f = DynArray.add v f ;;
  35.  
  36. (* This entire module will be auto-generated based on table information *)
  37. module User =
  38. struct
  39. type user = {
  40. mutable id: int ;
  41. mutable code: string ;
  42. mutable password: string ;
  43. mutable first_name: string ;
  44. mutable last_name: string ;
  45. mutable version: int ;
  46. mutable original: user option ; }
  47.  
  48. let before_create = DynArray.create ()
  49. let before_update = DynArray.create ()
  50. let before_insert = DynArray.create ()
  51.  
  52. let create id code password first_name last_name version =
  53. { id=id; code=code; password=password; first_name=first_name;
  54. last_name=last_name; version=0; original=None; }
  55.  
  56. let empty = create 0 "" "" "" "" 0
  57.  
  58. let changes u =
  59. let do_change_compare u1 u2 =
  60. let changes = HasChanged.create () in
  61. HasChanged.add_maybe changes "id" (string_of_int u1.id) (string_of_int u2.id) ;
  62. HasChanged.add_maybe changes "code" u1.code u2.code ;
  63. HasChanged.add_maybe changes "password" u1.password u2.password ;
  64. HasChanged.add_maybe changes "first_name" u1.first_name u2.first_name ;
  65. HasChanged.add_maybe changes "last_name" u1.last_name u2.last_name ;
  66. HasChanged.add_maybe changes "version" (string_of_int u1.version) (string_of_int u2.version) ;
  67. HasChanged.to_list changes
  68. in
  69. match u.original with
  70. | Some orig -> do_change_compare orig u
  71. | None -> do_change_compare empty u ;;
  72.  
  73. let clear_changes u =
  74. u.original <- (Some (create u.id u.code u.password u.first_name u.last_name u.version))
  75.  
  76. let insert u =
  77. DynArray.iter (fun f -> f u) before_insert;
  78. let stmt = sprintf ("INSERT INTO users (code,password,first_name,last_name,version) VALUES ('%s','%s','%s','%s',%i)")
  79. u.code u.password u.first_name u.last_name u.version in
  80. u.id <- 10 ;
  81. clear_changes u ;
  82. stmt ;;
  83.  
  84. let update u =
  85. DynArray.iter (fun f -> f u) before_update;
  86. let stmt = update_stmt "users" u.id (changes u) in
  87. clear_changes u ;
  88. stmt ;;
  89.  
  90. let save u = if u.id == 0 then insert u else update u ;;
  91. end ;;
  92.  
  93. (* Methods that I use to extend the base functionality of the auto-generated
  94. User module *)
  95. let user_cb_set_defaults u = u.User.version <- 1 ;;
  96. let user_cb_increment_version u = u.User.version <- u.User.version + 1 ;;
  97. let user_full_name u = sprintf "%s %s" u.User.first_name u.User.last_name ;;
  98. let user_blinded_name u = sprintf "%s %c." u.User.first_name (String.get u.User.last_name 0) ;;
  99.  
  100. (* Simple callback interface *)
  101. add_callback User.before_insert user_cb_set_defaults ;;
  102. add_callback User.before_update user_cb_increment_version ;;
  103.  
  104. (* Play around a bit *)
  105. let u=User.create 0 "john" "doe" "John" "Doe" 0 in
  106. printf "%s\n" (User.save u) ;
  107. u.User.first_name <- "Jane" ;
  108. u.User.last_name <- "Smith" ;
  109. printf "%s\n" (user_blinded_name u) ;
  110. u.User.code <- "jsmith" ;
  111. printf "%s\n" (User.save u) ;;
Add Comment
Please, Sign In to add comment