KiCad PCB EDA Suite
Loading...
Searching...
No Matches
sch_database_plugin.cpp
Go to the documentation of this file.
1/*
2 * This program source code file is part of KiCad, a free EDA CAD application.
3 *
4 * Copyright (C) 2022 Jon Evans <[email protected]>
5 * Copyright (C) 2022 KiCad Developers, see AUTHORS.txt for contributors.
6 *
7 * This program is free software: you can redistribute it and/or modify it
8 * under the terms of the GNU General Public License as published by the
9 * Free Software Foundation, either version 3 of the License, or (at your
10 * option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License along
18 * with this program. If not, see <http://www.gnu.org/licenses/>.
19 */
20
21#include <iostream>
22#include <unordered_set>
23#include <wx/log.h>
24
25#include <boost/algorithm/string.hpp>
26
29#include <fmt.h>
30#include <lib_symbol.h>
31#include <symbol_lib_table.h>
32
33#include "sch_database_plugin.h"
34
35
37 m_libTable( nullptr ),
38 m_settings(),
39 m_conn()
40{
41}
42
43
45{
46}
47
48
49void SCH_DATABASE_PLUGIN::EnumerateSymbolLib( wxArrayString& aSymbolNameList,
50 const wxString& aLibraryPath,
51 const STRING_UTF8_MAP* aProperties )
52{
53 std::vector<LIB_SYMBOL*> symbols;
54 EnumerateSymbolLib( symbols, aLibraryPath, aProperties );
55
56 for( LIB_SYMBOL* symbol : symbols )
57 aSymbolNameList.Add( symbol->GetName() );
58}
59
60
61void SCH_DATABASE_PLUGIN::EnumerateSymbolLib( std::vector<LIB_SYMBOL*>& aSymbolList,
62 const wxString& aLibraryPath,
63 const STRING_UTF8_MAP* aProperties )
64{
65 wxCHECK_RET( m_libTable, "Database plugin missing library table handle!" );
66 ensureSettings( aLibraryPath );
68
69 if( !m_conn )
71
72 bool powerSymbolsOnly = ( aProperties &&
73 aProperties->find( SYMBOL_LIB_TABLE::PropPowerSymsOnly ) !=
74 aProperties->end() );
75
76 for( const DATABASE_LIB_TABLE& table : m_settings->m_Tables )
77 {
78 std::vector<DATABASE_CONNECTION::ROW> results;
79
80 if( !m_conn->SelectAll( table.table, results ) )
81 {
82 if( !m_conn->GetLastError().empty() )
83 {
84 wxString msg = wxString::Format( _( "Error reading database table %s: %s" ),
85 table.table, m_conn->GetLastError() );
86 THROW_IO_ERROR( msg );
87 }
88
89 continue;
90 }
91
92 for( DATABASE_CONNECTION::ROW& result : results )
93 {
94 if( !result.count( table.key_col ) )
95 continue;
96
97 std::string prefix = table.name.empty() ? "" : fmt::format( "{}/", table.name );
98 wxString name( fmt::format( "{}{}", prefix,
99 std::any_cast<std::string>( result[table.key_col] ) ) );
100
101 LIB_SYMBOL* symbol = loadSymbolFromRow( name, table, result );
102
103 if( symbol && ( !powerSymbolsOnly || symbol->IsPower() ) )
104 aSymbolList.emplace_back( symbol );
105 }
106 }
107}
108
109
110LIB_SYMBOL* SCH_DATABASE_PLUGIN::LoadSymbol( const wxString& aLibraryPath,
111 const wxString& aAliasName,
112 const STRING_UTF8_MAP* aProperties )
113{
114 wxCHECK( m_libTable, nullptr );
115 ensureSettings( aLibraryPath );
117
118 if( !m_conn )
120
121 /*
122 * Table names are tricky, in order to allow maximum flexibility to the user.
123 * The slash character is used as a separator between a table name and symbol name, but symbol
124 * names may also contain slashes and table names may now also be empty (which results in the
125 * slash being dropped in the symbol name when placing a new symbol). So, if a slash is found,
126 * we check if the string before the slash is a valid table name. If not, we assume the table
127 * name is blank if our config has an entry for the null table.
128 */
129
130 std::string tableName = "";
131 std::string symbolName( aAliasName.ToUTF8() );
132
133 if( aAliasName.Contains( '/' ) )
134 {
135 tableName = std::string( aAliasName.BeforeFirst( '/' ).ToUTF8() );
136 symbolName = std::string( aAliasName.AfterFirst( '/' ).ToUTF8() );
137 }
138
139 std::vector<const DATABASE_LIB_TABLE*> tablesToTry;
140
141 for( const DATABASE_LIB_TABLE& tableIter : m_settings->m_Tables )
142 {
143 if( tableIter.name == tableName )
144 tablesToTry.emplace_back( &tableIter );
145 }
146
147 if( tablesToTry.empty() )
148 {
149 wxLogTrace( traceDatabase, wxT( "LoadSymbol: table '%s' not found in config" ), tableName );
150 return nullptr;
151 }
152
153 const DATABASE_LIB_TABLE* foundTable = nullptr;
155
156 for( const DATABASE_LIB_TABLE* table : tablesToTry )
157 {
158 if( m_conn->SelectOne( table->table, std::make_pair( table->key_col, symbolName ),
159 result ) )
160 {
161 foundTable = table;
162 wxLogTrace( traceDatabase, wxT( "LoadSymbol: SelectOne (%s, %s) found in %s" ),
163 table->key_col, symbolName, table->table );
164 }
165 else
166 {
167 wxLogTrace( traceDatabase, wxT( "LoadSymbol: SelectOne (%s, %s) failed for table %s" ),
168 table->key_col, symbolName, table->table );
169 }
170 }
171
172 wxCHECK( foundTable, nullptr );
173
174 return loadSymbolFromRow( aAliasName, *foundTable, result );
175}
176
177
178void SCH_DATABASE_PLUGIN::GetSubLibraryNames( std::vector<wxString>& aNames )
179{
180 ensureSettings( wxEmptyString );
181
182 aNames.clear();
183
184 std::set<wxString> tableNames;
185
186 for( const DATABASE_LIB_TABLE& tableIter : m_settings->m_Tables )
187 {
188 if( tableNames.count( tableIter.name ) )
189 continue;
190
191 aNames.emplace_back( tableIter.name );
192 tableNames.insert( tableIter.name );
193 }
194}
195
196
197void SCH_DATABASE_PLUGIN::GetAvailableSymbolFields( std::vector<wxString>& aNames )
198{
199 std::copy( m_customFields.begin(), m_customFields.end(), std::back_inserter( aNames ) );
200}
201
202
203void SCH_DATABASE_PLUGIN::GetDefaultSymbolFields( std::vector<wxString>& aNames )
204{
205 std::copy( m_defaultShownFields.begin(), m_defaultShownFields.end(),
206 std::back_inserter( aNames ) );
207}
208
209
210bool SCH_DATABASE_PLUGIN::CheckHeader( const wxString& aFileName )
211{
212 // TODO: Implement this sometime; but CheckHeader isn't even called...
213 return true;
214}
215
216
217bool SCH_DATABASE_PLUGIN::TestConnection( wxString* aErrorMsg )
218{
219 if( m_conn && m_conn->IsConnected() )
220 return true;
221
222 connect();
223
224 if( aErrorMsg && ( !m_conn || !m_conn->IsConnected() ) )
225 *aErrorMsg = m_lastError;
226
227 return m_conn && m_conn->IsConnected();
228}
229
230
231void SCH_DATABASE_PLUGIN::ensureSettings( const wxString& aSettingsPath )
232{
233 auto tryLoad =
234 [&]()
235 {
236 if( !m_settings->LoadFromFile() )
237 {
238 wxString msg = wxString::Format(
239 _( "Could not load database library: settings file %s missing or invalid" ),
240 aSettingsPath );
241
242 THROW_IO_ERROR( msg );
243 }
244 };
245
246 if( !m_settings && !aSettingsPath.IsEmpty() )
247 {
248 std::string path( aSettingsPath.ToUTF8() );
249 m_settings = std::make_unique<DATABASE_LIB_SETTINGS>( path );
250 m_settings->SetReadOnly( true );
251
252 tryLoad();
253 }
254 else if( !m_conn && m_settings )
255 {
256 // If we have valid settings but no connection yet; reload settings in case user is editing
257 tryLoad();
258 }
259 else if( m_conn && m_settings && !aSettingsPath.IsEmpty() )
260 {
261 wxASSERT_MSG( aSettingsPath == m_settings->GetFilename(),
262 "Path changed for database library without re-initializing plugin!" );
263 }
264 else if( !m_settings )
265 {
266 wxLogTrace( traceDatabase, wxT( "ensureSettings: no settings but no valid path!" ) );
267 }
268}
269
270
272{
273 wxCHECK_RET( m_settings, "Call ensureSettings before ensureConnection!" );
274
275 connect();
276
277 if( !m_conn || !m_conn->IsConnected() )
278 {
279 wxString msg = wxString::Format(
280 _( "Could not load database library: could not connect to database %s (%s)" ),
281 m_settings->m_Source.dsn, m_lastError );
282
283 THROW_IO_ERROR( msg );
284 }
285}
286
287
289{
290 wxCHECK_RET( m_settings, "Call ensureSettings before connect()!" );
291
292 if( m_conn && !m_conn->IsConnected() )
293 m_conn.reset();
294
295 if( !m_conn )
296 {
297 if( m_settings->m_Source.connection_string.empty() )
298 {
299 m_conn = std::make_unique<DATABASE_CONNECTION>( m_settings->m_Source.dsn,
300 m_settings->m_Source.username,
301 m_settings->m_Source.password,
302 m_settings->m_Source.timeout );
303 }
304 else
305 {
306 std::string cs = m_settings->m_Source.connection_string;
307 std::string basePath( wxFileName( m_settings->GetFilename() ).GetPath().ToUTF8() );
308
309 // Database drivers that use files operate on absolute paths, so provide a mechanism
310 // for specifying on-disk databases that live next to the kicad_dbl file
311 boost::replace_all( cs, "${CWD}", basePath );
312
313 m_conn = std::make_unique<DATABASE_CONNECTION>( cs, m_settings->m_Source.timeout );
314 }
315
316 if( !m_conn->IsConnected() )
317 {
318 m_lastError = m_conn->GetLastError();
319 m_conn.reset();
320 return;
321 }
322
323 m_conn->SetCacheParams( m_settings->m_Cache.max_size, m_settings->m_Cache.max_age );
324 }
325}
326
327
328std::optional<bool> SCH_DATABASE_PLUGIN::boolFromAny( const std::any& aVal )
329{
330 try
331 {
332 bool val = std::any_cast<bool>( aVal );
333 return val;
334 }
335 catch( const std::bad_any_cast& )
336 {
337 }
338
339 try
340 {
341 int val = std::any_cast<int>( aVal );
342 return static_cast<bool>( val );
343 }
344 catch( const std::bad_any_cast& )
345 {
346 }
347
348 try
349 {
350 wxString strval( std::any_cast<std::string>( aVal ).c_str(), wxConvUTF8 );
351
352 if( strval.IsEmpty() )
353 return std::nullopt;
354
355 strval.MakeLower();
356
357 for( const auto& trueVal : { wxS( "true" ), wxS( "yes" ), wxS( "y" ), wxS( "1" ) } )
358 {
359 if( strval.Matches( trueVal ) )
360 return true;
361 }
362
363 for( const auto& falseVal : { wxS( "false" ), wxS( "no" ), wxS( "n" ), wxS( "0" ) } )
364 {
365 if( strval.Matches( falseVal ) )
366 return false;
367 }
368 }
369 catch( const std::bad_any_cast& )
370 {
371 }
372
373 return std::nullopt;
374}
375
376
378 const DATABASE_LIB_TABLE& aTable,
379 const DATABASE_CONNECTION::ROW& aRow )
380{
381 LIB_SYMBOL* symbol = nullptr;
382
383 if( aRow.count( aTable.symbols_col ) )
384 {
385 LIB_SYMBOL* originalSymbol = nullptr;
386
387 // TODO: Support multiple options for symbol
388 std::string symbolIdStr = std::any_cast<std::string>( aRow.at( aTable.symbols_col ) );
389 LIB_ID symbolId;
390 symbolId.Parse( std::any_cast<std::string>( aRow.at( aTable.symbols_col ) ) );
391
392 if( symbolId.IsValid() )
393 originalSymbol = m_libTable->LoadSymbol( symbolId );
394
395 if( originalSymbol )
396 {
397 wxLogTrace( traceDatabase, wxT( "loadSymbolFromRow: found original symbol '%s'" ),
398 symbolIdStr );
399 symbol = originalSymbol->Duplicate();
400 symbol->SetSourceLibId( symbolId );
401 }
402 else if( !symbolId.IsValid() )
403 {
404 wxLogTrace( traceDatabase, wxT( "loadSymboFromRow: source symbol id '%s' is invalid, "
405 "will create empty symbol" ), symbolIdStr );
406 }
407 else
408 {
409 wxLogTrace( traceDatabase, wxT( "loadSymboFromRow: source symbol '%s' not found, "
410 "will create empty symbol" ), symbolIdStr );
411 }
412 }
413
414 if( !symbol )
415 {
416 // Actual symbol not found: return metadata only; error will be indicated in the
417 // symbol chooser
418 symbol = new LIB_SYMBOL( aSymbolName );
419 }
420 else
421 {
422 symbol->SetName( aSymbolName );
423 }
424
425 symbol->LibId().SetSubLibraryName( aTable.name );
426
427 if( aRow.count( aTable.footprints_col ) )
428 {
429 // TODO: Support multiple footprint choices
430 std::string footprints = std::any_cast<std::string>( aRow.at( aTable.footprints_col ) );
431 wxString footprint = wxString( footprints.c_str(), wxConvUTF8 ).BeforeFirst( ';' );
432 symbol->GetFootprintField().SetText( footprint );
433 }
434 else
435 {
436 wxLogTrace( traceDatabase, wxT( "loadSymboFromRow: footprint field %s not found." ),
437 aTable.footprints_col );
438 }
439
440 if( !aTable.properties.description.empty() && aRow.count( aTable.properties.description ) )
441 {
442 wxString value(
443 std::any_cast<std::string>( aRow.at( aTable.properties.description ) ).c_str(),
444 wxConvUTF8 );
445 symbol->SetDescription( value );
446 }
447
448 if( !aTable.properties.keywords.empty() && aRow.count( aTable.properties.keywords ) )
449 {
450 wxString value( std::any_cast<std::string>( aRow.at( aTable.properties.keywords ) ).c_str(),
451 wxConvUTF8 );
452 symbol->SetKeyWords( value );
453 }
454
455 if( !aTable.properties.footprint_filters.empty()
456 && aRow.count( aTable.properties.footprint_filters ) )
457 {
458 wxString value( std::any_cast<std::string>( aRow.at( aTable.properties.footprint_filters ) )
459 .c_str(),
460 wxConvUTF8 );
461 wxArrayString filters;
462 filters.push_back( value );
463 symbol->SetFPFilters( filters );
464 }
465
466 if( !aTable.properties.exclude_from_board.empty()
467 && aRow.count( aTable.properties.exclude_from_board ) )
468 {
469 std::optional<bool> val = boolFromAny( aRow.at( aTable.properties.exclude_from_board ) );
470
471 if( val )
472 {
473 symbol->SetIncludeOnBoard( !( *val ) );
474 }
475 else
476 {
477 wxLogTrace( traceDatabase, wxT( "loadSymbolFromRow: exclude_from_board value for %s "
478 "could not be cast to a boolean" ), aSymbolName );
479 }
480 }
481
482 if( !aTable.properties.exclude_from_bom.empty()
483 && aRow.count( aTable.properties.exclude_from_bom ) )
484 {
485 std::optional<bool> val = boolFromAny( aRow.at( aTable.properties.exclude_from_bom ) );
486
487 if( val )
488 {
489 symbol->SetIncludeInBom( !( *val ) );
490 }
491 else
492 {
493 wxLogTrace( traceDatabase, wxT( "loadSymbolFromRow: exclude_from_bom value for %s "
494 "could not be cast to a boolean" ), aSymbolName );
495 }
496 }
497
498 std::vector<LIB_FIELD*> fields;
499 symbol->GetFields( fields );
500
501 std::unordered_map<wxString, LIB_FIELD*> fieldsMap;
502
503 for( LIB_FIELD* field : fields )
504 fieldsMap[field->GetName()] = field;
505
506 for( const DATABASE_FIELD_MAPPING& mapping : aTable.fields )
507 {
508 if( !aRow.count( mapping.column ) )
509 {
510 wxLogTrace( traceDatabase, wxT( "loadSymbolFromRow: field %s not found in result" ),
511 mapping.column );
512 continue;
513 }
514
515 wxString value( std::any_cast<std::string>( aRow.at( mapping.column ) ).c_str(),
516 wxConvUTF8 );
517
518 if( mapping.name == wxT( "Value" ) )
519 {
520 LIB_FIELD& field = symbol->GetValueField();
521 field.SetText( value );
522
523 if( !mapping.inherit_properties )
524 {
525 field.SetVisible( mapping.visible_on_add );
526 field.SetNameShown( mapping.show_name );
527 }
528 continue;
529 }
530 else if( mapping.name == wxT( "Datasheet" ) )
531 {
532 LIB_FIELD& field = symbol->GetDatasheetField();
533 field.SetText( value );
534
535 if( !mapping.inherit_properties )
536 {
537 field.SetVisible( mapping.visible_on_add );
538 field.SetNameShown( mapping.show_name );
539
540 if( mapping.visible_on_add )
541 field.SetAutoAdded( true );
542 }
543
544 continue;
545 }
546
547 LIB_FIELD* field;
548 bool isNew = false;
549
550 if( fieldsMap.count( mapping.name ) )
551 {
552 field = fieldsMap[mapping.name];
553 }
554 else
555 {
556 field = new LIB_FIELD( symbol->GetNextAvailableFieldId() );
557 field->SetName( mapping.name );
558 isNew = true;
559 fieldsMap[mapping.name] = field;
560 }
561
562 if( !mapping.inherit_properties || isNew )
563 {
564 field->SetVisible( mapping.visible_on_add );
565 field->SetAutoAdded( true );
566 field->SetNameShown( mapping.show_name );
567 }
568
569 field->SetText( value );
570
571 if( isNew )
572 symbol->AddField( field );
573
574 m_customFields.insert( mapping.name );
575
576 if( mapping.visible_in_chooser )
577 m_defaultShownFields.insert( mapping.name );
578 }
579
580 return symbol;
581}
const char * name
Definition: DXF_plotter.cpp:56
std::map< std::string, std::any > ROW
virtual void SetVisible(bool aVisible)
Definition: eda_text.cpp:229
virtual void SetText(const wxString &aText)
Definition: eda_text.cpp:175
Field object used in symbol libraries.
Definition: lib_field.h:61
void SetAutoAdded(bool aAutoAdded)
Definition: lib_field.h:183
void SetName(const wxString &aName)
Set a user definable field name to aName.
Definition: lib_field.cpp:512
void SetNameShown(bool aShown=true)
Definition: lib_field.h:186
A logical library item identifier and consists of various portions much like a URI.
Definition: lib_id.h:49
int Parse(const UTF8 &aId, bool aFix=false)
Parse LIB_ID with the information from aId.
Definition: lib_id.cpp:50
bool IsValid() const
Check if this LID_ID is valid.
Definition: lib_id.h:172
void SetSubLibraryName(const UTF8 &aName)
Definition: lib_id.h:131
Define a library symbol object.
Definition: lib_symbol.h:99
void SetIncludeOnBoard(bool aIncludeOnBoard)
Set or clear include in board netlist flag.
Definition: lib_symbol.h:655
void SetSourceLibId(const LIB_ID &aLibId)
Definition: lib_symbol.h:150
int GetNextAvailableFieldId() const
virtual LIB_SYMBOL * Duplicate() const
Create a copy of a LIB_SYMBOL and assigns unique KIIDs to the copy and its children.
Definition: lib_symbol.h:115
bool IsPower() const
Definition: lib_symbol.cpp:684
LIB_FIELD & GetFootprintField()
Return reference to the footprint field.
void SetDescription(const wxString &aDescription)
Definition: lib_symbol.h:154
void SetKeyWords(const wxString &aKeyWords)
Definition: lib_symbol.h:167
void GetFields(std::vector< LIB_FIELD * > &aList)
Return a list of fields within this symbol.
LIB_FIELD & GetValueField()
Return reference to the value field.
void SetFPFilters(const wxArrayString &aFilters)
Definition: lib_symbol.h:202
void AddField(LIB_FIELD *aField)
Add a field.
LIB_ID & LibId()
Definition: lib_symbol.h:145
LIB_FIELD & GetDatasheetField()
Return reference to the datasheet field.
void SetIncludeInBom(bool aIncludeInBom)
Set or clear the include in schematic bill of materials flag.
Definition: lib_symbol.h:647
virtual void SetName(const wxString &aName)
Definition: lib_symbol.cpp:572
void GetAvailableSymbolFields(std::vector< wxString > &aNames) override
Retrieves a list of (custom) field names that are present on symbols in this library.
static std::optional< bool > boolFromAny(const std::any &aVal)
std::unique_ptr< DATABASE_CONNECTION > m_conn
Generally will be null if no valid connection is established.
void GetSubLibraryNames(std::vector< wxString > &aNames) override
Retrieves a list of sub-libraries in this library.
void EnumerateSymbolLib(wxArrayString &aSymbolNameList, const wxString &aLibraryPath, const STRING_UTF8_MAP *aProperties=nullptr) override
Populate a list of LIB_SYMBOL alias names contained within the library aLibraryPath.
void ensureSettings(const wxString &aSettingsPath)
bool TestConnection(wxString *aErrorMsg=nullptr)
bool CheckHeader(const wxString &aFileName) override
Return true if the first line in aFileName begins with the expected header.
std::set< wxString > m_defaultShownFields
std::set< wxString > m_customFields
LIB_SYMBOL * loadSymbolFromRow(const wxString &aSymbolName, const DATABASE_LIB_TABLE &aTable, const DATABASE_CONNECTION::ROW &aRow)
LIB_SYMBOL * LoadSymbol(const wxString &aLibraryPath, const wxString &aAliasName, const STRING_UTF8_MAP *aProperties=nullptr) override
Load a LIB_SYMBOL object having aPartName from the aLibraryPath containing a library format that this...
void GetDefaultSymbolFields(std::vector< wxString > &aNames) override
Retrieves a list of (custom) field names that should be shown by default for this library in the symb...
std::unique_ptr< DATABASE_LIB_SETTINGS > m_settings
SYMBOL_LIB_TABLE * m_libTable
A name/value tuple with unique names and optional values.
static const char * PropPowerSymsOnly
LIB_SYMBOL * LoadSymbol(const wxString &aNickname, const wxString &aName)
Load a LIB_SYMBOL having aName from the library given by aNickname.
const char *const traceDatabase
#define _(s)
#define THROW_IO_ERROR(msg)
Definition: ki_exception.h:38
bool visible_in_chooser
Whether the column is shown by default in the chooser.
std::string column
Database column name.
std::string name
KiCad field name.
bool inherit_properties
Whether or not to inherit properties from symbol field.
bool visible_on_add
Whether to show the field when placing the symbol.
bool show_name
Whether or not to show the field name as well as its value.
A database library table will be mapped to a sub-library provided by the database library entry in th...
std::string key_col
Unique key column name (will form part of the LIB_ID)
std::string name
KiCad library nickname (will form part of the LIB_ID)
std::string symbols_col
Column name containing KiCad symbol refs.
std::string footprints_col
Column name containing KiCad footprint refs.
std::vector< DATABASE_FIELD_MAPPING > fields
std::string table
Database table to pull content from.
MAPPABLE_SYMBOL_PROPERTIES properties