server: improve parent merging

This commit is contained in:
King Kévin 2023-01-26 07:17:20 +01:00
parent 904724cbce
commit 8336dc70df
1 changed files with 13 additions and 19 deletions

View File

@ -63,17 +63,15 @@ get '/' do
end
def get_part_by_id(id)
return nil unless id
statement = @db.prepare("SELECT part.id, part.name, part.description, part.datasheet, manufacturer.name AS manufacturer, package.name AS package, part.pincount AS pincount, part.page AS page, part.family AS parent FROM part LEFT JOIN package ON package.id = part.package LEFT JOIN manufacturer ON manufacturer.id = part.manufacturer WHERE part.id = ?")
part = statement.execute(id).to_a[0]
return nil unless part
# merge with family info
while part and part["parent"] do
family = statement.execute(part["parent"]).to_a[0]
part["parent"] = nil # reset info
break unless family
part["family"] ||= family["name"]
family.each do |k,v|
part[k] ||= v
parent = get_part_by_id(part["parent"])
# merge parent info
if parent then
part.each do |k,v|
part[k] ||= parent[k]
end
end
# add all distributors
@ -101,18 +99,14 @@ def get_part_by_id(id)
end
# add properties
part["properties"] = {}
family = id
statement = @db.prepare("SELECT property.name AS name, property_value.value AS value FROM property_value JOIN property ON property.id = property_value.property WHERE property_value.part = ?")
family_stmt = @db.prepare("SELECT family FROM part WHERE id = ?")
while family do
statement.execute(family).each do |row|
part["properties"][row["name"]] ||= []
part["properties"][row["name"]] << row["value"]
end
results = family_stmt.execute(family)
family = nil
results.each do |row|
family = row["id"]
statement.execute(id).each do |row|
part["properties"][row["name"]] ||= []
part["properties"][row["name"]] << row["value"]
end
if parent then
parent["properties"].each do |k,v|
part["properties"][k] ||= v
end
end
# clean up