Tenarai::DB

概要

  1. require 'tenarai/db'
  2. require 'tenarai/db/container'
  3. # DB へ格納するオブジェクトのクラス定義
  4. class Entry < Tenarai::DB::Row
  5. column Tenarai::DB::String.new('title', :length => 255)
  6. column Tenarai::DB::String.new('body')
  7. column Tenarai::DB::Date.new('created')
  8. column Tenarai::DB::Reference.new('comment', :multiple => true)
  9. column Tenarai::DB::Reference.new('tag', :multiple => true)
  10. end
  11. class Comment < Tenarai::DB::Row
  12. column Tenarai::DB::String.new('body')
  13. column Tenarai::DB::Reference.new('entry')
  14. end
  15. class Tag < Tenarai::DB::Row
  16. column Tenarai::DB::String.new('name')
  17. column Tenarai::DB::Reference.new('entry', :multiple => true)
  18. end
  19. # DB への接続
  20. Tenarai::DB.new(
  21. :engine => 'mysql',
  22. :db => 'test',
  23. :user => 'db_user',
  24. :password => 'db_password'
  25. ) do |db|
  26. table = Tenarai::DB::Container.new(
  27. db,
  28. # テーブル名と格納するオブジェクトのクラス名などを設定
  29. :table => [
  30. # 'テーブル名' => {:row => 'Row クラス名', :table => 'Table クラス名'}
  31. 'entry' => {:row => 'Entry' },
  32. 'comment' => {:row => 'Comment'},
  33. 'tag' => {:row => 'Tag' }
  34. ]
  35. # テーブル間のリレーションを設定
  36. :relation => [
  37. # ['テーブル名.カラム名', 'テーブル名.カラム名']
  38. ['entry.comment', 'comment.entry'],
  39. ['entry.tag', 'tag.entry' ]
  40. ]
  41. )
  42. # 全てのテーブルとリレーションをデータベースに作成
  43. # (create table や alter table ... add foreign key など)
  44. table.create
  45. # オブジェクトの作成とデータベースへの格納
  46. tag_climbing = Tag.new(tabl['tag'])
  47. tag_climbing.name = '山登り'
  48. tag_climbing.save
  49. tag_mt_iwate = Tag.new(tabl['tag'])
  50. tag_mt_iwate.name = '岩手山'
  51. tag_mt_iwate.save
  52. tag_mt_hayachine = Tag.new(tabl['tag'])
  53. tag_mt_hayachine.name = '早池峰山'
  54. tag_mt_hayachine.save
  55. entry1 = Entry.new(table['entry'])
  56. entry1.title = '岩手山に登ってきました'
  57. entry1.body = '頂上でお鉢めぐりです。頂上の地面が一部温かい。煙モクモク。さすが火山だけある。'
  58. entry1.created = Time.now
  59. entry1.save
  60. # リレーションの作成と保存
  61. entry1.tag << tag_climbing
  62. entry1.tag << tag_mt_iwate
  63. entry1.save
  64. tag_climbing.save
  65. tag_mt_iawte.save
  66. # オブジェクトの作成とデータベースへの格納
  67. entry2 = Entry.new(table['entry'])
  68. entry2.title = '早池峰山に登ってみたい'
  69. entry2.body = '実はまだ登ってません。岩場がゴロゴロしたところを登るらしい。実は子供のころに登っていたらしい?記憶なし。'
  70. entry2.created = Time.now
  71. entry2.save
  72. # リレーションの作成と保存
  73. tag_climbing.entry << entry2
  74. tag_mt_hayachine.entry << entry2
  75. tag_climbing.save
  76. tag_mt_hayachine.save
  77. entry2.save
  78. # オブジェクトの作成とデータベースへの格納
  79. comment1 = Comment.new(table['comment'])
  80. comment1.body = '御神坂から登って網張から下ったんだって?'
  81. comment1.save
  82. comment2 = Comment.new(table['comment'])
  83. comment2.body = '網張温泉の露天風呂はハエたたきでアブをバチンバチン。やるか、やられるか... 楽しいw'
  84. comment2.save
  85. # リレーションの作成と保存
  86. entry1.comment << comment1
  87. entry1.save
  88. comment1.save
  89. comment2.entry = entry1
  90. comment2.save
  91. entry1.save
  92. # 全てのテーブルとリレーションをデータベースから削除
  93. # (drop table や alter table ... drop foreign key など)
  94. table.drop
  95. end

クラスメソッド

メソッド


AdSense is disabled. Please check setting.