CSC/ECE 517 Fall 2010/ch3 3j KS: Difference between revisions
No edit summary |
(Initial Draft from Scott's document) |
||
Line 1: | Line 1: | ||
='''Object-relational Mapping for Ruby'''= | ='''Object-relational Mapping for Ruby'''= | ||
__TOC__ | |||
=Introduction= | |||
[http://en.wikipedia.org/wiki/Object-relational_mapping Object-relational mapping (ORM)] provides developers with a set of tools that ease management of the relationships between objects and relational databases, thus allowing applications to be easily extended to add data persistence. For Ruby, several object-relational mapping options are available in addition to ActiveRecord, the ORM layer supplied with Rails. | |||
=Overview= | =Overview= | ||
Object-relational Mapping (ORM) frameworks unburden the designer of the complex translation between database and object space. | |||
Typical ORM features: | |||
* Automatic mapping from classes to database tables | |||
** Class instance variables to database columns | |||
** Class instances to table rows | |||
* Aggregation and association relationships between mapped classes are managed | |||
** Example, :has_many, :belongs_to associations in ActiveRecord | |||
** Inheritance cases are mapped to tables | |||
* Validation of data prior to table storage | |||
* Class extensions to enable search, as well as creation, read, update, and deletion (CRUD) of instances/records | |||
* Usually abstracts the database from program space in such a way that alternate database types can be easily chosen (SQLite, Oracle, etc) | |||
The diagram below depicts a simple mapping of an object to a database table…. | |||
[[Image:3j_ks_pic1.jpg]] | |||
When applied to Ruby, implementations of ORM often leverage the language’s [http://en.wikipedia.org/wiki/Metaprogramming metaprogramming] strengths to create intuitive application-specific methods and otherwise extend classes to support database functionality. | |||
Perhaps add section w/ examples of typical Ruby ORM techniques here else just cover in each product… | |||
=Comparison of ORM products for Ruby= | =Comparison of ORM products for Ruby= | ||
=''' | ==''' ActiveRecord '''== | ||
[http://ar.rubyonrails.org/ ActiveRecord], originally created by David Heinemeier and released in 2003, became the de-facto ORM for Ruby since it was integrated in the widely-used Rails framework, however alternate ORMs for Ruby have been developed and Rails 3.x is ORM independent. ActiveRecord is an implementation of the active record design pattern, where a table is wrapped into a class and this class implements accessor methods for each column in the table. [http://en.wikipedia.org/wiki/Active_record_pattern] | |||
ActiveRecord uses a single table inheritance scheme, which trades off some storage efficiency for simplicity in the database design. Add a pic for to describe… | |||
ActiveRecord manages associations between table elements and provides the means to define such associations in the model definition. For example, the User class shown below defines a ‘’to_many’’ association with both the cheers and posts tables. Also note ActiveRecord’s integrated support for validation of table information when attempting an update. | |||
<pre> | |||
class User < ActiveRecord::Base | |||
has_many :cheers | |||
has_many :posts | |||
validates_presence_of :name | |||
validates_uniqueness_of :name | |||
validates_length_of :name, :within => 3..20 | |||
end | |||
</pre> | |||
'''Pros''' - | '''Pros''' - | ||
* | * Integrated with Rails development framework | ||
* Dynamically created database search methods (eg User.find_by_address) ease db queries and make queries database syntax independent | |||
'''Cons''' - | '''Cons''' - | ||
* | * DB creation/management is decoupled from the model, requiring a separate utility (rake/migrate) that must be kept in sync with application | ||
=='''Sequel'''== | |||
[http://sequel.rubyforge.org] | |||
* table definition and mapping to object structure explicitly made in ruby vs external tool (rake) as in RoR | |||
prod description | prod description | ||
http://sequel.rubyforge.org/rdoc/files/README_rdoc.html | |||
Sequel: The Database Toolkit for Ruby | |||
Sequel is a simple, flexible, and powerful SQL database access toolkit for Ruby. | |||
Sequel provides thread safety, connection pooling and a concise DSL for constructing SQL queries and table schemas. | |||
Sequel includes a comprehensive ORM layer for mapping records to Ruby objects and handling associated records. | |||
Sequel supports advanced database features such as prepared statements, bound variables, stored procedures, savepoints, two-phase commit, transaction isolation, master/slave configurations, and database sharding. | |||
Sequel currently has adapters for ADO, Amalgalite, DataObjects, DB2, DBI, Firebird, Informix, JDBC, MySQL, Mysql2, ODBC, OpenBase, Oracle, PostgreSQL and SQLite3. | |||
'''Pros''' - | '''Pros''' - | ||
* zzz | * zzz | ||
Line 36: | Line 81: | ||
'''Cons''' - | '''Cons''' - | ||
* zzz | * zzz | ||
==''' DataMapper'''== | |||
http://www.gelens.org/2007/12/13/ruby_orm_datamapper/ | |||
Ruby ORM: DataMapper | |||
There is a relative new ORM (Object Relational Mapper) for Ruby called “DataMapper”. Its goal is to create a fast, feature rich and thread-safe(!) ORM. DataMapper can be used standalone or as plugin for example Rails or Merb. | |||
Finally some competition in the Ruby ORM world. Rails’ ActiveRecord is no longer the only usable production ORM. DataMapper’s developers made some radical different fundamental design decisions regarding to migrations and property declarations. Property declarations are now done in the Model itself and not in separate “migration”-files. Which is more elegant in my opinion. | |||
Lately DataMapper is growing fast, it gets more attention and its feature list gets longer and is already superior to AR in terms of speed. A few weeks ago I started a new project using the new ruby framework Merb. Merb is also thread-safe and is in combination with DataMapper a really cool framework. By the way, did you know Rails is not thread-safe? Kinda sucky for a web framework to handle only one connection at a time. That’s the reason why people use multiple Mongrel servers when deploying Rails application. Not everyone I know seems to realize that. The bad thing is, there is NO plan to make it thread-safe :-/. | |||
[http://blog.mattwynne.net/2008/05/23/datamapper-a-better-orm-for-ruby/] | |||
DataMapper: A Better ORM for Ruby | |||
One of the things that’s always irritated my about rails’ ActiveRecord framework is the way that the domain model lives in the database.Don’t get me wrong: it’s very clever, and a great showcase for ruby’s metaprogramming features, which will blow average C# / Java mind the mind when they first see it. | |||
In rails, you build a database of your domain model, and create empty classes with the names of the domain entities (conventionally the singular of a database table name) which inherit from ActiveRecord. ActiveRecord then looks at your database, and using the magic of metaprogramming, hydrates your object with a bunch of properties that map to the database fields. | |||
But I prefer to write my models in the code, and if you do too, you might want to take a look at DataMapper. | |||
==Chart example== | ==Chart example== | ||
Line 71: | Line 128: | ||
|} | |} | ||
< | <pre> | ||
Code example | |||
</pre> | |||
< | |||
= | =Conclusion= | ||
Ruby ORM bla. | Ruby ORM bla. | ||
<br /> | <br /> | ||
= | =References= | ||
[1] [http://ruby-doc.org/ Ruby language] | [1] [http://ruby-doc.org/ Ruby language] | ||
Line 90: | Line 143: | ||
[2] Fowler, C., The Ruby FAQ, www.rubygarden.org/faq | [2] Fowler, C., The Ruby FAQ, www.rubygarden.org/faq | ||
[3] , Active Record — Object-relation mapping put on rails, http://ar.rubyonrails.org/ | |||
==External Links== | |||
== | |||
#[http://wxruby.rubyforge.org/wiki/wiki.pl WxRuby] | #[http://wxruby.rubyforge.org/wiki/wiki.pl WxRuby] | ||
#[http://www.activestate.com/activetcl ActiveTcl for Windows] | #[http://www.activestate.com/activetcl ActiveTcl for Windows] |
Revision as of 01:32, 4 October 2010
Object-relational Mapping for Ruby
Introduction
Object-relational mapping (ORM) provides developers with a set of tools that ease management of the relationships between objects and relational databases, thus allowing applications to be easily extended to add data persistence. For Ruby, several object-relational mapping options are available in addition to ActiveRecord, the ORM layer supplied with Rails.
Overview
Object-relational Mapping (ORM) frameworks unburden the designer of the complex translation between database and object space.
Typical ORM features:
- Automatic mapping from classes to database tables
- Class instance variables to database columns
- Class instances to table rows
- Aggregation and association relationships between mapped classes are managed
- Example, :has_many, :belongs_to associations in ActiveRecord
- Inheritance cases are mapped to tables
- Validation of data prior to table storage
- Class extensions to enable search, as well as creation, read, update, and deletion (CRUD) of instances/records
- Usually abstracts the database from program space in such a way that alternate database types can be easily chosen (SQLite, Oracle, etc)
The diagram below depicts a simple mapping of an object to a database table….
When applied to Ruby, implementations of ORM often leverage the language’s metaprogramming strengths to create intuitive application-specific methods and otherwise extend classes to support database functionality.
Perhaps add section w/ examples of typical Ruby ORM techniques here else just cover in each product…
Comparison of ORM products for Ruby
ActiveRecord
ActiveRecord, originally created by David Heinemeier and released in 2003, became the de-facto ORM for Ruby since it was integrated in the widely-used Rails framework, however alternate ORMs for Ruby have been developed and Rails 3.x is ORM independent. ActiveRecord is an implementation of the active record design pattern, where a table is wrapped into a class and this class implements accessor methods for each column in the table. [1]
ActiveRecord uses a single table inheritance scheme, which trades off some storage efficiency for simplicity in the database design. Add a pic for to describe…
ActiveRecord manages associations between table elements and provides the means to define such associations in the model definition. For example, the User class shown below defines a ‘’to_many’’ association with both the cheers and posts tables. Also note ActiveRecord’s integrated support for validation of table information when attempting an update.
class User < ActiveRecord::Base has_many :cheers has_many :posts validates_presence_of :name validates_uniqueness_of :name validates_length_of :name, :within => 3..20 end
Pros -
- Integrated with Rails development framework
- Dynamically created database search methods (eg User.find_by_address) ease db queries and make queries database syntax independent
Cons -
- DB creation/management is decoupled from the model, requiring a separate utility (rake/migrate) that must be kept in sync with application
Sequel
- table definition and mapping to object structure explicitly made in ruby vs external tool (rake) as in RoR
prod description http://sequel.rubyforge.org/rdoc/files/README_rdoc.html
Sequel: The Database Toolkit for Ruby Sequel is a simple, flexible, and powerful SQL database access toolkit for Ruby. Sequel provides thread safety, connection pooling and a concise DSL for constructing SQL queries and table schemas. Sequel includes a comprehensive ORM layer for mapping records to Ruby objects and handling associated records. Sequel supports advanced database features such as prepared statements, bound variables, stored procedures, savepoints, two-phase commit, transaction isolation, master/slave configurations, and database sharding. Sequel currently has adapters for ADO, Amalgalite, DataObjects, DB2, DBI, Firebird, Informix, JDBC, MySQL, Mysql2, ODBC, OpenBase, Oracle, PostgreSQL and SQLite3.
Pros -
- zzz
Cons -
- zzz
DataMapper
http://www.gelens.org/2007/12/13/ruby_orm_datamapper/ Ruby ORM: DataMapper There is a relative new ORM (Object Relational Mapper) for Ruby called “DataMapper”. Its goal is to create a fast, feature rich and thread-safe(!) ORM. DataMapper can be used standalone or as plugin for example Rails or Merb. Finally some competition in the Ruby ORM world. Rails’ ActiveRecord is no longer the only usable production ORM. DataMapper’s developers made some radical different fundamental design decisions regarding to migrations and property declarations. Property declarations are now done in the Model itself and not in separate “migration”-files. Which is more elegant in my opinion. Lately DataMapper is growing fast, it gets more attention and its feature list gets longer and is already superior to AR in terms of speed. A few weeks ago I started a new project using the new ruby framework Merb. Merb is also thread-safe and is in combination with DataMapper a really cool framework. By the way, did you know Rails is not thread-safe? Kinda sucky for a web framework to handle only one connection at a time. That’s the reason why people use multiple Mongrel servers when deploying Rails application. Not everyone I know seems to realize that. The bad thing is, there is NO plan to make it thread-safe :-/. [3] DataMapper: A Better ORM for Ruby One of the things that’s always irritated my about rails’ ActiveRecord framework is the way that the domain model lives in the database.Don’t get me wrong: it’s very clever, and a great showcase for ruby’s metaprogramming features, which will blow average C# / Java mind the mind when they first see it. In rails, you build a database of your domain model, and create empty classes with the names of the domain entities (conventionally the singular of a database table name) which inherit from ActiveRecord. ActiveRecord then looks at your database, and using the magic of metaprogramming, hydrates your object with a bunch of properties that map to the database fields. But I prefer to write my models in the code, and if you do too, you might want to take a look at DataMapper.
Chart example
aaaaaa | bbbbbbb | cccc | ddddddddd |
---|---|---|---|
AA | yes | yes | yes |
BBBBB | yes | yes | yes |
CCCC | yes | yes | yes |
DDDD | yes | yes | yes |
EEEE | yes | yes | yes |
Code example
Conclusion
Ruby ORM bla.
References
[1] Ruby language
[2] Fowler, C., The Ruby FAQ, www.rubygarden.org/faq
[3] , Active Record — Object-relation mapping put on rails, http://ar.rubyonrails.org/