kicad: change from mysql to sqlite
This commit is contained in:
parent
55234f0d7b
commit
199ac09b69
22
kicad_lib.rb
22
kicad_lib.rb
|
@ -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
|
this script creates views from the partdb table so to create a database for KiCAD
|
||||||
if wil also output the KiCad database library file
|
if wil also output the KiCad database library file
|
||||||
=end
|
=end
|
||||||
require 'mysql2'
|
require 'sqlite3'
|
||||||
require 'json'
|
require 'json'
|
||||||
|
|
||||||
CREDENTIALS = "credentials.json"
|
# database file
|
||||||
raise "database information #{CREDENTIALS} do not exist" unless File.file? CREDENTIALS
|
DB_PATH = "partdb.db"
|
||||||
credentials = {}
|
raise "DB file #{DB_PATH} does not exist" unless File.file? DB_PATH
|
||||||
JSON.parse(IO.read(CREDENTIALS)).each {|key,value| credentials[key.to_sym] = value}
|
db = SQLite3::Database.new(DB_PATH)
|
||||||
Mysql2::Client.default_query_options.merge!(:as => :hash)
|
db.results_as_hash = true
|
||||||
db = Mysql2::Client.new(credentials)
|
|
||||||
KICAD_FILE = "partdb.kicad_dbl"
|
KICAD_FILE = "partdb.kicad_dbl"
|
||||||
DEBUG = false # print debug information
|
DEBUG = false # print debug information
|
||||||
|
|
||||||
|
@ -25,7 +24,8 @@ prop2view = [{property: "kicad_symbol"}, {property: "kicad_footprint"}, {propert
|
||||||
prop2view.each do |prop|
|
prop2view.each do |prop|
|
||||||
puts prop[:property]
|
puts prop[:property]
|
||||||
prop[:table] = "part_" + prop[:property].downcase.gsub(/[- \/]/,"_")
|
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
|
puts view if DEBUG
|
||||||
db.query(view)
|
db.query(view)
|
||||||
end
|
end
|
||||||
|
@ -37,7 +37,8 @@ dist2view = [{property: "LCSC"}, {property: "JLCPCB"}, {property: "DigiKey"}]
|
||||||
dist2view.each do |prop|
|
dist2view.each do |prop|
|
||||||
puts prop[:property]
|
puts prop[:property]
|
||||||
prop[:table] = "part_" + prop[:property].downcase.gsub(/[- \/]/,"_")
|
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
|
puts view if DEBUG
|
||||||
db.query(view)
|
db.query(view)
|
||||||
end
|
end
|
||||||
|
@ -57,7 +58,8 @@ categories.uniq!
|
||||||
categories.each do |category|
|
categories.each do |category|
|
||||||
puts category
|
puts category
|
||||||
table = "kicad_lib_" + category.downcase.gsub(/[- \/]/,"_")
|
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, "
|
view += "SELECT part.id, part.name, part.description, "
|
||||||
(prop2view + dist2view).each do |prop|
|
(prop2view + dist2view).each do |prop|
|
||||||
view += "COALESCE(#{prop[:table]}1.#{prop[:property]},#{prop[:table]}2.#{prop[:property]}) AS #{prop[:property]}, "
|
view += "COALESCE(#{prop[:table]}1.#{prop[:property]},#{prop[:table]}2.#{prop[:property]}) AS #{prop[:property]}, "
|
||||||
|
|
Loading…
Reference in New Issue