-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfillDB.rb
84 lines (67 loc) · 2.24 KB
/
fillDB.rb
1
2
3
4
5
6
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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
require 'json'
require "sqlite3"
File.delete("nutrition.db") if File.exist?("nutrition.db")
database = SQLite3::Database.new "nutrition.db"
File.readlines('schema.sql').each do |line|
database.execute(line)
end
@allItems = JSON.parse(File.read("items.js"))
puts "item count " + @allItems.count.to_s
@nutrientCount = 0
@nutrients = Hash.new
@categories = Hash.new
for item in @allItems
if item["nutrients"] == nil
item["nutrients"] = Array.new #For the case where not all the data exists
end
if item["cat"] == nil
item["cat"] = "unknown"
end
item["nutrients"] = item["nutrients"].select { |nut| nut["name"] != "Energy" || nut["unit"] != "kcal"}
@nutrientCount += item["nutrients"].count
cat = item["cat"]
if @categories[cat] == nil
@categories[cat] = Hash.new
@categories[cat]["name"] = cat
@categories[cat]["id"] = @categories.count
end
for nut in item["nutrients"]
name = nut["name"]
unit = nut["unit"]
if @nutrients[name] == nil
@nutrients[name] = Hash.new
@nutrients[name]["group"] = nut["group"]
@nutrients[name]["unit"] = unit
@nutrients[name]["name"] = name
@nutrients[name]["id"] = @nutrients.count
else
if @nutrients[name]["unit"] != unit
puts "missmatch " + unit + " != " + @nutrients[name]["unit"]
end
end
end
end
puts "nutrient count " + @nutrientCount.to_s
puts "unique " + @nutrients.count.to_s
@nutrients.each do |key,nut|
database.execute 'INSERT INTO "nutrient" (id,name,nutrientGroup,baseUnit) VALUES (?,?,?,?)',[nut["id"],nut["name"],nut["group"],nut["unit"]]
end
@categories.each do |key,cat|
database.execute 'INSERT INTO "food_category" (id,name) VALUES (?,?)',[cat["id"],cat["name"]]
end
@createdNutrientCount = 0
@allItems.each_with_index { |food,index|
database.transaction do |db|
cat = @categories[food["cat"]]
db.execute 'INSERT INTO "food" (id,name,category_id) VALUES (?,?,?)',[index+1,food["name"],cat["id"]]
for nut in food["nutrients"]
nutDB = @nutrients[nut["name"]]
if nut["value"] > 0
db.execute 'INSERT INTO "food_nutrient" (food_id,nutrient_id,quantity) VALUES (?,?,?)',[index+1,nutDB["id"],nut["value"]]
@createdNutrientCount = @createdNutrientCount + 1
end
end
end
}
puts "created " + @createdNutrientCount.to_s + " nutrient entries"
database.close