Class: ChefWorkflow::DatabaseSupport::Set

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

Direct Known Subclasses

Map

Instance Attribute Summary

Attributes inherited from Generic

#db

Instance Method Summary collapse

Methods inherited from Collection

#initialize

Methods inherited from Generic

#_dump, _load, #initialize, #post_marshal_init

Constructor Details

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

Instance Method Details

#add(key) ⇒ Object



137
138
139
# File 'lib/chef-workflow/support/db/basic.rb', line 137

def add(key)
  @db.execute("insert into #{@table_name} (name, key) values (?, ?)", [@object_name, key])
end

#clearObject



151
152
153
# File 'lib/chef-workflow/support/db/basic.rb', line 151

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

#create_tableObject



173
174
175
176
177
178
179
180
181
182
183
184
# File 'lib/chef-workflow/support/db/basic.rb', line 173

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

  @db.execute "create index if not exists #{@table_name}_name_idx on #{@table_name} (name)"
end

#delete(key) ⇒ Object



141
142
143
# File 'lib/chef-workflow/support/db/basic.rb', line 141

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

#eachObject



169
170
171
# File 'lib/chef-workflow/support/db/basic.rb', line 169

def each
  keys.each { |x| yield x }
end

#has_key?(key) ⇒ Boolean Also known as: include?

Returns:

  • (Boolean)


145
146
147
# File 'lib/chef-workflow/support/db/basic.rb', line 145

def has_key?(key)
  @db.execute("select count(*) from #{@table_name} where name=? and key=?", [@object_name, key]).first.first.to_i > 0
end

#keysObject



165
166
167
# File 'lib/chef-workflow/support/db/basic.rb', line 165

def keys
  @db.execute("select distinct key from #{@table_name} where name=?", [@object_name]).map(&:first)
end

#replace(set) ⇒ Object



155
156
157
158
159
160
161
162
163
# File 'lib/chef-workflow/support/db/basic.rb', line 155

def replace(set)
  clear

  return if set.empty?

  value_string = ("(?, ?)," * set.count).chop

  @db.execute("insert into #{@table_name} (name, key) values #{value_string}", set.map { |x| [@object_name, x] }.flatten)
end