7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
|
# File 'lib/houcho/outerrole/save.rb', line 7
def save_outer_role(outerrole, datasource)
db = Houcho::Database.new.handle
db.transaction do
delete_ids = db.execute("SELECT id FROM outerrole WHERE data_source = ?", datasource).flatten
outerrole.each do |outerrole, hosts|
begin
db.execute("INSERT INTO outerrole(name, data_source) VALUES(?,?)", outerrole, datasource)
rescue SQLite3::ConstraintException, "column name is not unique"
ensure
outerrole_id = db.execute("SELECT id FROM outerrole WHERE name = ?", outerrole).flatten.first
end
delete_ids.delete(outerrole_id)
db.execute("DELETE FROM outerrole_host WHERE outerrole_id = ?", outerrole_id)
hosts.each do |host|
begin
db.execute("INSERT INTO host(name) VALUES(?)", host)
rescue SQLite3::ConstraintException, "column name is not unique"
ensure
begin
db.execute(
"INSERT INTO outerrole_host(outerrole_id, host_id) VALUES(?,?)",
outerrole_id,
db.execute("SELECT id FROM host WHERE name = ?", host).flatten.first
)
rescue SQLite3::ConstraintException, "column name is not unique"
end
end
end
end
delete_ids.each do |id|
db.execute("DELETE FROM outerrole_host WHERE outerrole_id = ?", id)
end
end end
|