Class: ChefWorkflow::DatabaseSupport::Object

Inherits:
Generic
  • Object
show all
Defined in:
lib/chef-workflow/support/db/basic.rb

Instance Attribute Summary

Attributes inherited from Generic

#db

Instance Method Summary collapse

Methods inherited from Generic

#_dump, _load, #initialize, #post_marshal_init

Constructor Details

This class inherits a constructor from ChefWorkflow::DatabaseSupport::Generic

Instance Method Details

#[](key) ⇒ Object



39
40
41
42
# File 'lib/chef-workflow/support/db/basic.rb', line 39

def [](key)
  value = @db.execute("select value from #{@table_name} where key=?", [key]).first.first rescue nil
  return value && Marshal.load(value)
end

#[]=(key, value) ⇒ Object



44
45
46
47
48
# File 'lib/chef-workflow/support/db/basic.rb', line 44

def []=(key, value)
  delete(key)
  @db.execute("insert into #{@table_name} (key, value) values (?, ?)", [key, Marshal.dump(value)])
  value
end

#create_tableObject



54
55
56
57
58
59
60
61
62
63
# File 'lib/chef-workflow/support/db/basic.rb', line 54

def create_table
  @db.execute <<-EOF
  create table if not exists #{@table_name} (
    id integer not null primary key autoincrement,
    key varchar(255) not null,
    value text not null,
    UNIQUE(key)
  )
  EOF
end

#delete(key) ⇒ Object



50
51
52
# File 'lib/chef-workflow/support/db/basic.rb', line 50

def delete(key)
  @db.execute("delete from #{@table_name} where key=?", [key])
end