Class: Wakame::StatusDB::SequelAdapter

Inherits:
Object
  • Object
show all
Defined in:
lib/wakame/status_db.rb

Constant Summary collapse

DATA_FORMAT_VERSION =
'0.4'

Instance Method Summary collapse

Constructor Details

#initializeSequelAdapter

Returns a new instance of SequelAdapter.



88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
# File 'lib/wakame/status_db.rb', line 88

def initialize
  require 'sequel/core'
  require 'sequel/model'

  #@db = Sequel.connect(Wakame.config.status_db_dsn, {:logger=>Wakame.log})
  @db = Sequel.connect(Wakame.config.status_db_dsn)
  
  if [:metadata, :model_stores].all?{ |i| @db.table_exists?(i) }
    m = @db[:metadata].where(:id=>1).first

    unless m && m[:version] == DATA_FORMAT_VERSION
      setup_store
    end

  else
    setup_store
  end

  # Generate Sequel::Model class dynamically.
  # This is same as below:
  # class ModelStore < Sequel::Model
  #    unrestrict_primary_key
  # end
  @model_class = Class.new(Sequel::Model(:model_stores)) { |klass|
    klass.unrestrict_primary_key
  }
  @model_class.plugin :schema
  @model_class.plugin :hook_class_methods
  @model_class.class_eval {
    before_create(:set_created_at) do
      self.updated_at = self.created_at = Time.now
    end
    before_update(:set_updated_at) do
      self.updated_at = Time.now
    end
  }
  # @model_class.plugin :caching, store
end

Instance Method Details

#clear_storeObject



181
182
183
# File 'lib/wakame/status_db.rb', line 181

def clear_store
  setup_store
end

#delete(id) ⇒ Object



177
178
179
# File 'lib/wakame/status_db.rb', line 177

def delete(id)
  @model_class[id].destroy
end

#exists?(id) ⇒ Boolean

Returns:

  • (Boolean)


162
163
164
# File 'lib/wakame/status_db.rb', line 162

def exists?(id)
  !@model_class[id].nil?
end

#find(id, &blk) ⇒ Object



147
148
149
150
151
152
153
# File 'lib/wakame/status_db.rb', line 147

def find(id, &blk)
  m = @model_class[id]
  if m
    hash = eval(m[:dump])
    blk.call(id, hash)
  end
end

#find_all(klass) ⇒ Object

Find all rows belong to given klass name. Returns id list which matches class_type == klass



157
158
159
160
# File 'lib/wakame/status_db.rb', line 157

def find_all(klass)
  ds = @model_class.where(:class_type=>klass.to_s)
  ds.all.map {|r| r[:id] }
end

#save(id, hash) ⇒ Object



166
167
168
169
170
171
172
173
174
175
# File 'lib/wakame/status_db.rb', line 166

def save(id, hash)
  m = @model_class[id]
  if m.nil? 
    m = @model_class.new
    m.id = id
    m.class_type = hash[AttributeHelper::CLASS_TYPE_KEY]
 end 
  m.dump = hash.inspect
  m.save
end

#setup_storeObject



127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
# File 'lib/wakame/status_db.rb', line 127

def setup_store
  @db.drop_table :metadata rescue nil
  @db.create_table? :metadata do
    primary_key :id
    column :version, :string
    column :created_at, :datetime
  end

  @db[:metadata].insert(:version=>DATA_FORMAT_VERSION, :created_at=>Time.now)

  @db.drop_table :model_stores rescue nil
  @db.create_table? :model_stores do
    primary_key :id, :string, :size=>50, :auto_increment=>false
    column :class_type, :string
    column :dump, :text
    column :created_at, :datetime
    column :updated_at, :datetime
  end
end