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.
Sequelpure Ruby DB Toolkit
http://sequel.rubyforge.org/
Ruby Toolbox• Database Abstraction - Adapters• DSL - Ruby• ORM - Models
Adapters• SQLite• MySQL (2)• PostgreSQL• DBI
gem install sequel     gem install sqlite3     gem install mysql2
Connect to DB• DB = Sequel.sqlite   (in-memory)
Connect to DB• DB = Sequel.sqlite (in-memory)• DB = Sequel.connect (‘sqlite://test.db’)
Connect to DB• DB = Sequel.sqlite (in-memory)• DB = Sequel.connect (‘sqlite://test.db’)• DB = Sequel.connect (‘mysql://......
Ruby Toolboxsequel -E sqlite://test.db
Database Object
DB - Database Object• DB.tables• DB.table_exists?(:items)• DB.schema(:items)• DB.add_index :items, :name
SQL QUIZ !Rename table
SQL QUIZ !DB.rename_table :items , :mono
SQL QUIZ !ALTER TABLE `items`RENAME TO `mono`
DB.run(‘ALTER TABLE ’)   DB.rename_table
Dataset Object
Unique for Sequel
Dataset objectSQL Query      as  Object
DB[:items]
Immutable   Dataset       ReturnDataset (copy)
ChainableDB[:Items]             SELECT * FROM `items`
ChainableDB[:Items]    where(‘price > 10’)             SELECT * FROM `items`              WHERE (price > 100)
ChainableDB[:Items]     where(‘price > 10’)   order(:name)   limit(10)              SELECT * FROM `items`               WH...
Two type of returns Dataset   return   Dataset’                    id   name Dataset   return                    1     a  ...
require ‘sequel_core’
Model
require ‘sequel’class Item < Sequel::Model          Datasetend
Associationsclass Item < Sequel::Model   many_to_one :usersendclass User < Sequel::Model   one_to_many :itemsend
Associations   ActiveRecord            Sequel     belongs_to         many_to_one     has_many           one_to_manyhas_and...
Other features• Model validations - validate_*• Model hooks - before, after• Migrations• Transactions
Questions?
Sequel
Upcoming SlideShare
Loading in …5
×

Sequel

2,578 views

Published on

pure Ruby DB toolkit

Published in: Technology
  • Be the first to comment

Sequel

  1. 1. Sequelpure Ruby DB Toolkit
  2. 2. http://sequel.rubyforge.org/
  3. 3. Ruby Toolbox• Database Abstraction - Adapters• DSL - Ruby• ORM - Models
  4. 4. Adapters• SQLite• MySQL (2)• PostgreSQL• DBI
  5. 5. gem install sequel gem install sqlite3 gem install mysql2
  6. 6. Connect to DB• DB = Sequel.sqlite (in-memory)
  7. 7. Connect to DB• DB = Sequel.sqlite (in-memory)• DB = Sequel.connect (‘sqlite://test.db’)
  8. 8. Connect to DB• DB = Sequel.sqlite (in-memory)• DB = Sequel.connect (‘sqlite://test.db’)• DB = Sequel.connect (‘mysql://...’)• DB = Sequel.connect (‘postgres://...’)
  9. 9. Ruby Toolboxsequel -E sqlite://test.db
  10. 10. Database Object
  11. 11. DB - Database Object• DB.tables• DB.table_exists?(:items)• DB.schema(:items)• DB.add_index :items, :name
  12. 12. SQL QUIZ !Rename table
  13. 13. SQL QUIZ !DB.rename_table :items , :mono
  14. 14. SQL QUIZ !ALTER TABLE `items`RENAME TO `mono`
  15. 15. DB.run(‘ALTER TABLE ’) DB.rename_table
  16. 16. Dataset Object
  17. 17. Unique for Sequel
  18. 18. Dataset objectSQL Query as Object
  19. 19. DB[:items]
  20. 20. Immutable Dataset ReturnDataset (copy)
  21. 21. ChainableDB[:Items] SELECT * FROM `items`
  22. 22. ChainableDB[:Items] where(‘price > 10’) SELECT * FROM `items` WHERE (price > 100)
  23. 23. ChainableDB[:Items] where(‘price > 10’) order(:name) limit(10) SELECT * FROM `items` WHERE (price > 100) ORDER BY `name` LIMIT 10
  24. 24. Two type of returns Dataset return Dataset’ id name Dataset return 1 a 2 bDB[:items].limit(10).all
  25. 25. require ‘sequel_core’
  26. 26. Model
  27. 27. require ‘sequel’class Item < Sequel::Model Datasetend
  28. 28. Associationsclass Item < Sequel::Model many_to_one :usersendclass User < Sequel::Model one_to_many :itemsend
  29. 29. Associations ActiveRecord Sequel belongs_to many_to_one has_many one_to_manyhas_and_belongs_to... many_to_many has_one one_to_one
  30. 30. Other features• Model validations - validate_*• Model hooks - before, after• Migrations• Transactions
  31. 31. Questions?

×