Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

Underlaying Technology of Modern O/R Mapper

6,119 views

Published on

Presentation slides at RubyKaigi2011

Published in: Technology

Underlaying Technology of Modern O/R Mapper

  1. 1. RubyKaigi2011Underlaying Technology ofModern O/R Mapptermakoto kuwatahttp://www.kuwata-lab.com/
  2. 2. Table of Contents The Goal of Modern ORM Query Object View-layer Cacing Ruby-to-SQL Transformation How to Prevent SQL Injection
  3. 3. The Goal of Modern ORM
  4. 4. What is the Problem?
  5. 5. By the way...
  6. 6. Refactoring
  7. 7. Key Points Programming language can break large part into small pieces construct large part from small pieces add name to pieces in order to abstract them
  8. 8. Again, What is the Problem? • SQL cant break large one into pieces • SQL cant be constructed from pieces • SQL cant name or abstract pieces
  9. 9. Does ORM Solve it? Not abstracted
  10. 10. Abstract by Named Scope
  11. 11. History of Evolution OOP, Modern ORM Functional C, Pascal DBI, ORM Assembler SQL
  12. 12. Conclusion of this section SQL is just Assembler It cant be broken into pieces It cant be constructed from pieces It cant name or abstract pieces Modern ORM provides abstraction for SQL ex. named_scope
  13. 13. Query Object
  14. 14. Kwd arg vs. Method Chain
  15. 15. Query Object (1)
  16. 16. Query Object (2)
  17. 17. Method Call vs. Query Op Just a method call Operation to Query object
  18. 18. Query Operation (1) Just a method call
  19. 19. Query Operation (2) Just a method call
  20. 20. Query Operation (3) Just a method call
  21. 21. Query Operation (4) Just a method call
  22. 22. Query == Collection Array ActiveRecord::Relation
  23. 23. Action to Collection condition condition
  24. 24. SQL Abstraction (1)
  25. 25. SQL Abstraction (2)
  26. 26. OT: Partial Application
  27. 27. Conslution of this section Query object is the key concept for modern ORM behaves like a collection abstracts SQL
  28. 28. ORM and View-layer Cache
  29. 29. Example: Fragment Cache
  30. 30. Example: Controller Submits SQL everytime
  31. 31. Move find() from C to V? MVC broken
  32. 32. Check Cache Existence? C depends on V tightly
  33. 33. What is the Problem? View Cache: Pull-styleMVC: Push-style Context Data Controller
  34. 34. Sol: Proc or Lambda (C) find(:all) is not called yet
  35. 35. Sol: Proc or Lambda (V) Call find(:all) only when cache is expired
  36. 36. Sol: Proc or Lambda Pros. Easy to understand & implement Cons. Caching on V affects to C (C must be changed according to V)
  37. 37. Sol: Lazy Loading (C) SQL is not executed yet
  38. 38. Sol: Lazy Loading (V) Nothing to be changed!
  39. 39. Sol: Lazy Loading Pros. Caching on V doesnt affect to C Cons. Availables only on modern ORM (not available on Rails2)
  40. 40. Conclusion of this section MVC and fragment cache are not good friends MVC: push-style Fragment cache: pull-style Laziness solves them Proc or lambda Lazy loading
  41. 41. N+1 Problem
  42. 42. What is N+1 Problem? (1) Team : Member = 1 : N
  43. 43. What is N+1 Problem? (2) }1 }N
  44. 44. Sol: Eager Loading
  45. 45. Sol: Eager Loading Pros. Easy to understand Cons. Not adaptive to changes Easy to forget Hard to notice Unused eager loading is very waste
  46. 46. Sol: Strategic Eager Loading
  47. 47. Sol: Strategic Eager Loading • Each item knows its collection • Collection resolves relationships
  48. 48. Sol: Strategic Eager Loading Pros. Adaptive to changes (No need to specify relationship for eager loading) Cons. Not supported in Rails (2 & 3)
  49. 49. Conclusion of this section Eager loading Easy to forget, hard to notice Strategic Eager Loading No need to specify relation names Adaptive to changes
  50. 50. Ruby-to-SQL Translation
  51. 51. Evaluation x=1 x+1 2
  52. 52. AST by Evaluation x = Column.new(:x) x+1 + x 1
  53. 53. Operator Override
  54. 54. Ruby to AST to SQL == Operator x 1Override
  55. 55. Other Examples
  56. 56. Sequel Example
  57. 57. Pitfalls 1.8 doesnt allow to override != operator Use 1.9 Impossible to override && nor || Use & and | instead, or AND(), OR()
  58. 58. OT: Operast Library to convert Ruby expression into other language (such as SQL). https://github.com/kwatch/operast
  59. 59. OT: Oktest New-style testing library for Python Apply Operator Override to Testing http://packages.python.org/Oktest/
  60. 60. Conclusion of this section How to Ruby to AST (by Operator Override) AST to SQL
  61. 61. Any Questions?

×