kicad: change from mysql to sqlite

master
King Kévin 2023-03-09 11:27:35 +01:00
parent 55234f0d7b
commit 199ac09b69
1 changed files with 12 additions and 10 deletions

View File

@ -7,15 +7,14 @@ https://docs.kicad.org/master/en/eeschema/eeschema.html#database_libraries
this script creates views from the partdb table so to create a database for KiCAD
if wil also output the KiCad database library file
=end
require 'mysql2'
require 'sqlite3'
require 'json'
CREDENTIALS = "credentials.json"
raise "database information #{CREDENTIALS} do not exist" unless File.file? CREDENTIALS
credentials = {}
JSON.parse(IO.read(CREDENTIALS)).each {|key,value| credentials[key.to_sym] = value}
Mysql2::Client.default_query_options.merge!(:as => :hash)
db = Mysql2::Client.new(credentials)
# database file
DB_PATH = "partdb.db"
raise "DB file #{DB_PATH} does not exist" unless File.file? DB_PATH
db = SQLite3::Database.new(DB_PATH)
db.results_as_hash = true
KICAD_FILE = "partdb.kicad_dbl"
DEBUG = false # print debug information
@ -25,7 +24,8 @@ prop2view = [{property: "kicad_symbol"}, {property: "kicad_footprint"}, {propert
prop2view.each do |prop|
puts prop[:property]
prop[:table] = "part_" + prop[:property].downcase.gsub(/[- \/]/,"_")
view = "CREATE OR REPLACE VIEW #{prop[:table]} AS SELECT properties.part AS part, properties.value AS #{prop[:property]} FROM properties JOIN property ON property.id = properties.property WHERE property.name = '#{prop[:property]}'"
db.query("DROP VIEW IF EXISTS #{prop[:table]}")
view = "CREATE VIEW #{prop[:table]} AS SELECT properties.part AS part, properties.value AS #{prop[:property]} FROM properties JOIN property ON property.id = properties.property WHERE property.name = '#{prop[:property]}'"
puts view if DEBUG
db.query(view)
end
@ -37,7 +37,8 @@ dist2view = [{property: "LCSC"}, {property: "JLCPCB"}, {property: "DigiKey"}]
dist2view.each do |prop|
puts prop[:property]
prop[:table] = "part_" + prop[:property].downcase.gsub(/[- \/]/,"_")
view = "CREATE OR REPLACE VIEW #{prop[:table]} AS SELECT distribution.part AS part, distribution.sku AS #{prop[:property]} FROM distribution JOIN distributor ON distributor.id = distribution.distributor WHERE distributor.name = '#{prop[:property]}'"
db.query("DROP VIEW IF EXISTS #{prop[:table]}")
view = "CREATE VIEW #{prop[:table]} AS SELECT distribution.part AS part, distribution.sku AS #{prop[:property]} FROM distribution JOIN distributor ON distributor.id = distribution.distributor WHERE distributor.name = '#{prop[:property]}'"
puts view if DEBUG
db.query(view)
end
@ -57,7 +58,8 @@ categories.uniq!
categories.each do |category|
puts category
table = "kicad_lib_" + category.downcase.gsub(/[- \/]/,"_")
view = "CREATE OR REPLACE VIEW #{table} AS "
db.query("DROP VIEW IF EXISTS #{table}")
view = "CREATE VIEW #{table} AS "
view += "SELECT part.id, part.name, part.description, "
(prop2view + dist2view).each do |prop|
view += "COALESCE(#{prop[:table]}1.#{prop[:property]},#{prop[:table]}2.#{prop[:property]}) AS #{prop[:property]}, "