Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Building an ORM with Arel

Building an ORM with Arel

This talk was presented at RubyConf Brasil 2014 with @vipulnsward

Prathamesh Sonpatki

August 28, 2014
Tweet

More Decks by Prathamesh Sonpatki

Other Decks in Technology

Transcript

  1. Object-relational mapping (ORM, O/RM, and O/R mapping) in computer science

    is a programming technique for converting data between incompatible type systems in object- oriented programming languages. This creates, in effect, a "virtual object database" that can be used from within the programming language.
  2. Active Record “An object that wraps a row in a

    database table or view, encapsulates the database access, and adds domain logic on that data.”
  3. SQL torm_development=# select * from posts; id | name |

    content | author | subject -----+-------------+---------+------------+--------- 332 | RGenGC| Restricted Generation Garbage Collection | GorbyPuff | How does RGenGC work? (1 row)
  4. Object Post: id: 332, name: ‘RGenGC’, subject: ‘How does RGenGC

    work?’ content: ‘Restricted Generation Garbage Collection’ author: ‘GorbyPuff’
  5. Object Post: id(int): 332, name(string): ‘RGenGC’, subject(string): ‘How does RGenGC

    work?’ content(string): ‘Restricted Generation Garbage Collection’ author(string): ‘GorbyPuff’
  6. What? post = Post.new subject: 'What is RGenCG?' post.save first_post

    = Post.find(1) puts first_post.subject #=> 'What is RGenCG?'
  7. ARel • Relational Algebra for Ruby • ARel is a

    SQL AST manager for Ruby • Can generate complex SQL queries • Adapts to multiple databases
  8. Engine • Provide information to ARel/ Attributes Access • Column

    / Table structure • Database Adapter • Data Type Mappings
  9. Attributes • Data Types • Casting from / to database

    types • Attribute Maps for table • Accessors for a model
  10. Summary • Database Connection • Creating columns/ Schema cache •

    Attribute Types/ Type casting • Attributes and accessors • Database statements • Querying