32#include <bs_thread_pool.hpp>
33#include <wx/datetime.h>
34#include <wx/filename.h>
36#include <wx/tokenzr.h>
38#include <boost/algorithm/string.hpp>
61 SCH_IO( wxS(
"Database library" ) ),
78 const wxString& aLibraryPath,
79 const std::map<std::string, UTF8>* aProperties )
81 std::vector<LIB_SYMBOL*> symbols;
86 aSymbolNameList.Add( symbol->GetName() );
112 const wxString& aLibraryPath,
113 const std::map<std::string, UTF8>* aProperties )
115 wxCHECK_RET(
m_adapter,
"Database plugin missing library manager adapter handle!" );
127 if( !powerSymbolsOnly || symbol->
IsPower() )
128 aSymbolList.emplace_back( symbol->
Duplicate() );
134 const std::map<std::string, UTF8>* aProperties )
142 const wxString& aAliasName,
143 const std::map<std::string, UTF8>* aProperties )
145 wxCHECK_MSG(
m_adapter,
nullptr,
"Database plugin missing library manager adapter handle!" );
151 std::string tableName;
152 std::string symbolName( aAliasName.ToUTF8() );
167 tableName = sanitizedIt->second.first;
168 symbolName = sanitizedIt->second.second;
181 if( tableName.empty() && aAliasName.Contains(
'/' ) )
183 tableName = std::string( aAliasName.BeforeFirst(
'/' ).ToUTF8() );
184 symbolName = std::string( aAliasName.AfterFirst(
'/' ).ToUTF8() );
187 std::vector<const DATABASE_LIB_TABLE*> tablesToTry;
192 if( tableName.empty() || tableIter.
name == tableName )
193 tablesToTry.emplace_back( &tableIter );
196 if( tablesToTry.empty() )
198 wxLogTrace(
traceDatabase, wxT(
"LoadSymbol: table '%s' not found in config" ), tableName );
207 if( conn->SelectOne(
table->table, std::make_pair(
table->key_col, symbolName ),
result ) )
210 wxLogTrace(
traceDatabase, wxT(
"LoadSymbol: SelectOne (%s, %s) found in %s" ),
215 wxLogTrace(
traceDatabase, wxT(
"LoadSymbol: SelectOne (%s, %s) failed for table %s" ),
233 std::set<wxString> tableNames;
237 if( tableNames.count( tableIter.
name ) )
240 aNames.emplace_back( tableIter.
name );
241 tableNames.insert( tableIter.
name );
264 size_t signature = 0;
266 for(
const auto& [
table, results] : aTableResults )
270 size_t rowSignature = 0;
272 for(
const auto& [column, value] :
result )
276 if(
const std::string* str = std::any_cast<std::string>( &value ) )
284 if(
const std::string* str = std::any_cast<std::string>( &it->second ) )
287 symbolId.
Parse( *str );
294 if( std::optional<int> libHash =
m_adapter->GetLibraryModifyHash( nickname ) )
320 wxString::Format(
_(
"Symbol '%s' is in a database library and is not previewed." ),
332 wxString::Format(
_(
"Symbol '%s' was not found." ), aId.
Format().
wx_str() ),
343 return aBaseDir.
IsEmpty() ? wxFileName( aSettings.
GetFilename() ).GetPath() : aBaseDir;
348 bool aConnectNow,
const wxString& aBaseDir )
357 if( !aBackend.backend )
358 return {
nullptr, aBackend.failure, std::move( aBackend.error ) };
360 return { std::make_unique<DATABASE_CONNECTION>( std::move( aBackend.backend ), aConnectNow ),
DB_FAILURE::UNKNOWN,
368 auto it = aRow.find( aTable.
key_col );
370 if( it == aRow.end() )
371 return wxEmptyString;
373 std::string rawName = std::any_cast<std::string>( it->second );
375 std::string prefix = ( aGloballyUniqueKeys || aTable.
name.empty() ) ?
"" : fmt::format(
"{}/", aTable.
name );
380 return wxString::FromUTF8( fmt::format(
"{}{}", prefix, sanitizedName.
c_str() ) );
384std::vector<std::unique_ptr<LIB_SYMBOL>>
386 bool aGloballyUniqueKeys,
const std::vector<DATABASE_CONNECTION::ROW>& aRows,
394 std::vector<std::unique_ptr<LIB_SYMBOL>> symbols;
395 std::map<wxString, std::pair<size_t, std::string>> listed;
413 if(
auto it = listed.find(
name ); it != listed.end() )
415 if( it->second.second == rawKey )
417 preview.
reportIssue( PREVIEW_ISSUE::FIELD::REPEATED_KEY,
418 wxString::Format(
_(
"Key '%s' appears in more than one row; only the last "
420 wxString::FromUTF8( rawKey ) ),
426 wxString::Format(
_(
"Keys '%s' and '%s' are both listed as '%s'; only the "
428 wxString::FromUTF8( it->second.second ),
429 wxString::FromUTF8( rawKey ),
name ),
433 symbols[it->second.first] = std::move( symbol );
434 it->second.second = rawKey;
438 listed[
name] = { symbols.size(), rawKey };
439 symbols.push_back( std::move( symbol ) );
456 Report( aMessage, aSeverity );
461 std::map<wxString, std::unique_ptr<LIB_SYMBOL>>& aSymbolCache,
462 std::map<wxString, std::pair<std::string, std::string>>& aSanitizedNameMap )
464 for(
const auto& [
table, results] : aTableResults )
474 aSanitizedNameMap[
name] = std::make_pair(
table->name, rawName );
479 aSymbolCache[symbol->GetName()] = std::move( symbol );
483 return !aSymbolCache.empty();
495 long long currentTimestampSeconds = wxDateTime::Now().GetValue().GetValue() / 1000;
507 struct CACHE_LIB_GUARD
510 ~CACHE_LIB_GUARD() { *
flag =
false; }
532 std::vector<DATABASE_CONNECTION::ROW> results;
535 if( !aConn->SelectAll(
table.table,
table.key_col, results, &status ) )
541 if( !status.
error.empty() )
547 tableResults.emplace_back( &
table, std::move( results ) );
566 std::map<wxString, std::unique_ptr<LIB_SYMBOL>> newSymbolCache;
567 std::map<wxString, std::pair<std::string, std::string>> newSanitizedNameMap;
598 wxLogTrace(
traceDatabase, wxT(
"Starting background refresh thread" ) );
618 if( worker.joinable() )
624 std::minstd_rand& aRng )
627 static constexpr long long FIRST_RETRY_MS = 3000;
629 const long long ceiling =
630 std::min( aMaxAgeSeconds * 1000, FIRST_RETRY_MS << std::clamp( aAttempt, 0,
MAX_BUSY_ATTEMPT ) );
633 std::uniform_int_distribution<long long> spread( ceiling / 2, ceiling );
635 return std::chrono::milliseconds( spread( aRng ) );
647 BS::this_thread::set_os_thread_name(
"dblib bg" );
649 std::minstd_rand jitter( std::random_device{}() );
654 long long maxAge = 0;
668 bool dropped =
m_cachePopulated.load() && ( !conn || !conn->IsConnected() );
681 wxLogTrace(
traceDatabase, wxT(
"Initiating background refresh" ) );
686 bool querySuccess =
true;
700 querySuccess =
false;
704 conn->ClearCache(
table.table );
706 std::vector<DATABASE_CONNECTION::ROW> results;
709 if( !conn->SelectAll(
table.table,
table.key_col, results, &status ) )
711 wxLogTrace(
traceDatabase, wxT(
"Background refresh: SelectAll failed for table %s" ),
713 querySuccess =
false;
720 tableResults.emplace_back( &
table, std::move( results ) );
726 querySuccess =
false;
732 bool dataChanged =
false;
745 std::map<wxString, std::unique_ptr<LIB_SYMBOL>> newSymbolCache;
746 std::map<wxString, std::pair<std::string, std::string>> newSanitizedNameMap;
750 wxLogTrace(
traceDatabase, wxT(
"Background refresh: new data" ) );
766 wxLogTrace(
traceDatabase, wxT(
"Background refresh: no new data" ) );
772 wxLogTrace(
traceDatabase, wxT(
"Background refresh failed: %s; cache preserved" ), e.
What() );
774 catch(
const std::exception& e )
776 wxLogTrace(
traceDatabase, wxT(
"Background refresh failed: %s; cache preserved" ), e.
what() );
783 std::chrono::milliseconds wait = std::chrono::seconds( maxAge );
811 std::string
path( aSettingsPath.ToUTF8() );
827 wxASSERT_MSG( aSettingsPath ==
m_settings->GetFilename(),
828 "Path changed for database library without re-initializing plugin!" );
832 wxLogTrace(
traceDatabase, wxT(
"ensureSettings: no settings but no valid path!" ) );
843 std::condition_variable cv;
851struct SETTINGS_READ_REGISTRY
854 std::map<std::string, std::weak_ptr<SETTINGS_READ>> reads;
858SETTINGS_READ_REGISTRY& settingsReadRegistry()
860 static SETTINGS_READ_REGISTRY* instance =
new SETTINGS_READ_REGISTRY;
867void readSettingsFile(
const std::shared_ptr<SETTINGS_READ>& aRead,
const std::filesystem::path& aPath )
874 std::ifstream in( aPath, std::ios::binary );
875 std::vector<char> chunk( 1 << 16 );
879 in.read( chunk.data(), chunk.size() );
880 bytes.append( chunk.data(),
static_cast<size_t>( in.gcount() ) );
883 ok = in.eof() && !in.bad();
891 std::lock_guard lock( aRead->m );
892 aRead->bytes = std::move(
bytes );
895 aRead->cv.notify_all();
907 std::shared_ptr<SETTINGS_READ> read;
910 SETTINGS_READ_REGISTRY& registry = settingsReadRegistry();
911 std::lock_guard registryLock( registry.m );
913 for(
auto it = registry.reads.begin(); it != registry.reads.end(); )
915 it = it->second.expired() ? registry.reads.erase( it ) : std::next( it );
919 wxFileName file(
path );
920 file.Normalize( wxPATH_NORM_DOTS | wxPATH_NORM_ABSOLUTE );
922 const std::filesystem::path nativePath =
ToFsPath( file.GetFullPath() );
924 file.Normalize( wxPATH_NORM_CASE );
926 const std::string key( file.GetFullPath().ToUTF8() );
928 if( std::shared_ptr<SETTINGS_READ> pending = registry.reads[key].lock() )
930 std::lock_guard readLock( pending->m );
938 read = std::make_shared<SETTINGS_READ>();
939 registry.reads[key] = read;
943 std::thread( readSettingsFile, read, nativePath ).detach();
947 registry.reads.erase( key );
953 const auto deadline =
956 std::unique_lock lock( read->m );
960 if(
m_loadCancelled.load() || std::chrono::steady_clock::now() >= deadline )
963 read->cv.wait_for( lock, std::chrono::milliseconds( 50 ) );
966 const bool ok = read->ok;
967 const std::string
bytes = read->bytes;
972 THROW_IO_ERRORF(
_(
"Could not load database library: settings file %s missing or invalid" ),
path );
987 wxCHECK_MSG(
m_settings,
nullptr,
"Call ensureSettings before ensureConnection!" );
993 if( !conn || !conn->IsConnected() )
1001 const bool configured = !
source.dsn.empty() || !
source.connection_string.empty();
1004 const wxString shown = !
name.empty() ? wxString::FromUTF8(
name )
1005 : configured ?
_(
"connection string" )
1006 :
_(
"(not configured)" );
1008 THROW_IO_ERRORF(
_(
"Could not load database library: could not connect to database %s (%s)" ), shown,
1024 auto now = std::chrono::steady_clock::now;
1026 const auto start = now();
1028 std::minstd_rand jitter( std::random_device{}() );
1029 bool firstOpen =
false;
1030 std::chrono::milliseconds lastAttempt( 0 );
1033 for(
int attempt = 0;; ++attempt )
1041 if( existing && existing->IsConnected() )
1045 firstOpen = !existing;
1051 const auto attemptStart = now();
1054 lastAttempt = std::chrono::duration_cast<std::chrono::milliseconds>( now() - attemptStart );
1062 const auto remaining = std::chrono::duration_cast<std::chrono::milliseconds>( deadline - now() ) - lastAttempt;
1064 if( remaining <= std::chrono::milliseconds( 0 ) )
1067 const std::chrono::milliseconds wait = std::min(
FileInUseBackoff( attempt, jitter ), remaining );
1091 std::shared_ptr<DATABASE_CONNECTION> newConn = std::move( created.
connection );
1094 return { created.
failure, wxString::FromUTF8( created.
error ) };
1096 if( !newConn->IsConnected() )
1097 return { newConn->GetLastFailure(), newConn->GetLastError() };
1105 requiredColumns.insert( field.
column );
1108 requiredColumns.insert( tableIter.
pins_col );
1111 std::set<std::string> optionalColumns{
1117 if( newConn->CacheTableInfo( tableIter.
table, requiredColumns, optionalColumns ) )
1120 std::optional<DATABASE_CONNECTION::TABLE_FAILURE> failed = newConn->GetTableFailure( tableIter.
table );
1125 return { failed->failure,
1126 wxString::Format(
_(
"Error reading database table %s: %s" ),
1127 wxString::FromUTF8( tableIter.
table ), wxString::FromUTF8( failed->error ) ) };
1134 m_conn = std::move( newConn );
1144 bool val = std::any_cast<bool>( aVal );
1147 catch(
const std::bad_any_cast& )
1153 int val = std::any_cast<int>( aVal );
1154 return static_cast<bool>( val );
1156 catch(
const std::bad_any_cast& )
1162 wxString strval( std::any_cast<std::string>( aVal ).c_str(), wxConvUTF8 );
1164 if( strval.IsEmpty() )
1165 return std::nullopt;
1169 for(
const auto& trueVal : { wxS(
"true" ), wxS(
"yes" ), wxS(
"y" ), wxS(
"1" ) } )
1171 if( strval.Matches( trueVal ) )
1175 for(
const auto& falseVal : { wxS(
"false" ), wxS(
"no" ), wxS(
"n" ), wxS(
"0" ) } )
1177 if( strval.Matches( falseVal ) )
1181 catch(
const std::bad_any_cast& )
1185 return std::nullopt;
1193 std::unique_ptr<LIB_SYMBOL> symbol =
nullptr;
1197 std::string symbols = std::any_cast<std::string>( aRow.at( aTable.
symbols_col ) );
1198 wxString symbolsStr = wxString( symbols.c_str(), wxConvUTF8 );
1199 wxStringTokenizer tokenizer( symbolsStr,
";\t\r\n", wxTOKEN_STRTOK );
1201 std::vector<LIB_ID> symbolIds;
1203 while( tokenizer.HasMoreTokens() )
1205 wxString token = tokenizer.GetNextToken();
1207 id.
Parse( std::string( token.ToUTF8() ) );
1211 symbolIds.push_back(
id );
1216 wxString::Format(
_(
"'%s' is not a symbol; expected Library:Symbol." ), token ),
1225 std::vector<LIB_SYMBOL*> sourceSymbols;
1226 std::vector<wxString> sourceSymbolNames;
1228 for(
const LIB_ID& symbolId : symbolIds )
1230 wxString symbolIdStr = symbolId.Format().wx_str();
1234 wxLogTrace(
traceDatabase, wxT(
"loadSymbolFromRow: cycle detected resolving '%s' "
1235 "(row '%s' in table '%s'); skipping recursive load" ),
1236 symbolIdStr, aSymbolName, aTable.
name );
1242 std::unordered_set<wxString>* set;
1244 ~CYCLE_GUARD() { set->erase( key ); }
1256 sourceSymbols.push_back( src );
1257 sourceSymbolNames.push_back( src->
GetName() );
1261 wxLogTrace(
traceDatabase, wxT(
"loadSymbolFromRow: source symbol '%s' not found" ), symbolIdStr );
1265 if( sourceSymbols.empty() )
1269 symbol.reset(
new LIB_SYMBOL( aSymbolName ) );
1271 else if( sourceSymbols.size() == 1 )
1273 symbol.reset( sourceSymbols[0]->Duplicate() );
1274 symbol->SetSourceLibId( symbolIds[0] );
1275 symbol->SetName( aSymbolName );
1284 symbol.reset(
new LIB_SYMBOL( aSymbolName ) );
1285 symbol->SetSourceLibId( symbolIds[0] );
1287 symbol->SetUnitCount( sourceSymbols[0]->GetUnitCount(),
false );
1288 symbol->SetPowerSymbolProp( sourceSymbols[0]->IsPower() );
1289 symbol->SetShowPinNames( sourceSymbols[0]->GetShowPinNames() );
1290 symbol->SetShowPinNumbers( sourceSymbols[0]->GetShowPinNumbers() );
1291 symbol->SetPinNameOffset( sourceSymbols[0]->GetPinNameOffset() );
1295 if(
SCH_FIELD* srcField = sourceSymbols[0]->GetField( fieldId ) )
1297 SCH_FIELD* dstField = symbol->GetField( fieldId );
1298 *dstField = *srcField;
1303 std::vector<wxString> bodyStyleNames;
1304 std::vector<int> sourceBodyStyleCounts;
1305 std::vector<int> compositeBodyStyleBase;
1307 int nextBodyStyle = 1;
1309 for(
size_t i = 0; i < sourceSymbols.size(); ++i )
1311 int srcCount = std::max( 1, sourceSymbols[i]->GetBodyStyleCount() );
1312 sourceBodyStyleCounts.push_back( srcCount );
1313 compositeBodyStyleBase.push_back( nextBodyStyle );
1317 bodyStyleNames.push_back( sourceSymbolNames[i] );
1321 for(
int style = 1; style <= srcCount; ++style )
1323 wxString styleName = sourceSymbols[i]->GetBodyStyleDescription( style,
false );
1324 bodyStyleNames.push_back( sourceSymbolNames[i] + wxT(
" (" ) + styleName + wxT(
")" ) );
1328 nextBodyStyle += srcCount;
1331 int totalBodyStyles = nextBodyStyle - 1;
1333 symbol->SetHasDeMorganBodyStyles(
false );
1334 symbol->SetBodyStyleNames( bodyStyleNames );
1336 std::set<wxString> mergedFieldNames;
1338 for(
size_t i = 0; i < sourceSymbols.size(); ++i )
1340 std::unique_ptr<LIB_SYMBOL> srcSymbol = sourceSymbols[i]->Flatten();
1341 int srcCount = sourceBodyStyleCounts[i];
1342 int base = compositeBodyStyleBase[i];
1344 for(
SCH_ITEM& item : srcSymbol->GetDrawItems() )
1358 targetStyle = base + ( item.GetBodyStyle() - 1 );
1360 if( targetStyle > totalBodyStyles )
1366 symbol->AddDrawItem( newItem,
false );
1370 for(
int bodyStyle = 0; bodyStyle < srcCount; ++bodyStyle )
1375 symbol->AddDrawItem( newItem,
false );
1380 int targetStyle = base + ( item.GetBodyStyle() - 1 );
1382 if( targetStyle > totalBodyStyles )
1388 symbol->AddDrawItem( newItem,
false );
1393 symbol->GetDrawItems().sort();
1398 symbol.reset(
new LIB_SYMBOL( aSymbolName ) );
1401 LIB_ID libId = symbol->GetLibId();
1403 symbol->SetLibId( libId );
1405 wxArrayString footprintsList;
1409 std::string footprints = std::any_cast<std::string>( aRow.at( aTable.
footprints_col ) );
1411 wxString footprintsStr = wxString( footprints.c_str(), wxConvUTF8 );
1412 wxStringTokenizer tokenizer( footprintsStr,
";\t\r\n", wxTOKEN_STRTOK );
1414 while( tokenizer.HasMoreTokens() )
1415 footprintsList.Add( tokenizer.GetNextToken() );
1417 if( footprintsList.size() > 0 )
1418 symbol->GetFootprintField().SetText( footprintsList[0] );
1432 std::string jsonStr = std::any_cast<std::string>( aRow.at( aTable.
pins_col ) );
1434 if( !jsonStr.empty() )
1436 nlohmann::json
json = nlohmann::json::parse( jsonStr );
1438 if(
json.is_object() &&
json.contains(
"pin_maps" ) )
1443 else if( !footprintsList.IsEmpty() )
1447 if( !assignments.empty() )
1449 const wxString mapName = wxS(
"Database" );
1451 symbol->PinMaps().AddOrReplace(
MakeLegacyPinMap( mapName, assignments ) );
1455 std::vector<ASSOCIATED_FOOTPRINT> associations;
1457 for(
const wxString& footprint : footprintsList )
1460 fpId.
Parse( footprint );
1461 associations.push_back( { fpId, mapName } );
1464 symbol->SetAssociatedFootprints( std::move( associations ) );
1469 catch(
const std::exception& e )
1474 wxString::Format(
_(
"Error parsing pin map for database symbol '%s': %s" ), aSymbolName,
1485 symbol->SetDescription( value );
1490 wxString value( std::any_cast<std::string>( aRow.at( aTable.
properties.
keywords ) ).c_str(),
1492 symbol->SetKeyWords( value );
1501 footprintsList.push_back( value );
1504 symbol->SetFPFilters( footprintsList );
1513 symbol->SetExcludedFromSim( *val );
1517 wxLogTrace(
traceDatabase, wxT(
"loadSymbolFromRow: exclude_from_sim value for %s "
1518 "could not be cast to a boolean" ), aSymbolName );
1529 symbol->SetExcludedFromBoard( *val );
1533 wxLogTrace(
traceDatabase, wxT(
"loadSymbolFromRow: exclude_from_board value for %s "
1534 "could not be cast to a boolean" ), aSymbolName );
1545 symbol->SetExcludedFromBOM( *val );
1549 wxLogTrace(
traceDatabase, wxT(
"loadSymbolFromRow: exclude_from_bom value for %s "
1550 "could not be cast to a boolean" ), aSymbolName );
1554 std::vector<SCH_FIELD*> fields;
1555 symbol->GetFields( fields );
1557 std::unordered_map<wxString, SCH_FIELD*> fieldsMap;
1560 fieldsMap[field->GetName()] = field;
1562 static const wxString c_valueFieldName( wxS(
"Value" ) );
1563 static const wxString c_datasheetFieldName( wxS(
"Datasheet" ) );
1564 static const wxString c_footprintFieldName( wxS(
"Footprint" ) );
1570 int dbFieldOrdinal = symbol->GetNextFieldOrdinal();
1574 if( !aRow.count( mapping.
column ) )
1576 wxLogTrace(
traceDatabase, wxT(
"loadSymbolFromRow: field %s not found in result" ), mapping.
column );
1589 strValue = std::any_cast<std::string>( aRow.at( mapping.
column ) );
1591 catch( std::bad_any_cast& )
1595 wxString value(
strValue.c_str(), wxConvUTF8 );
1597 if( mapping.
name_wx == c_valueFieldName )
1599 SCH_FIELD& field = symbol->GetValueField();
1610 else if( mapping.
name_wx == c_datasheetFieldName )
1612 SCH_FIELD& field = symbol->GetDatasheetField();
1630 if( fieldsMap.count( mapping.
name_wx ) )
1632 field = fieldsMap[mapping.
name_wx];
1639 fieldsMap[mapping.
name_wx] = field;
1659 symbol->AddDrawItem( field,
false );
1667 symbol->GetDrawItems().sort();
1672 symbol->RefreshLibraryTreeCaches();
static DB_BACKEND_RESULT Create(const DATABASE_SOURCE &aSource, const wxString &aBasePath)
Builds the backend for a source.
Brackets the reads of one library load or refresh, so the backend can keep what it opens for them.
std::map< std::string, std::any > ROW
Edits every setting of a .kicad_dbl file and previews the resulting symbols.
Dialog helper object to sit in the inheritance tree between wxDialog and any class written by wxFormB...
virtual void SetParent(EDA_ITEM *aParent)
virtual void SetVisible(bool aVisible)
virtual void Report(const wxString &aText, SEVERITY aSeverity=RPT_SEVERITY_UNDEFINED) const
Hold an error message and may be used when throwing exceptions containing meaningful error messages.
virtual const wxString What() const
A composite of Problem() and Where()
virtual const char * what() const override
std::exception interface, returned as UTF-8
wxString GetFilename() const
A logical library item identifier and consists of various portions much like a URI.
int Parse(const UTF8 &aId, bool aFix=false)
Parse LIB_ID with the information from aId.
bool IsValid() const
Check if this LID_ID is valid.
void SetSubLibraryName(const UTF8 &aName)
const UTF8 & GetLibNickname() const
Return the logical library name portion of a LIB_ID.
static UTF8 FixIllegalChars(const UTF8 &aLibItemName, bool aLib)
Replace illegal LIB_ID item name characters with underscores '_'.
Define a library symbol object.
bool IsPower() const override
virtual LIB_SYMBOL * Duplicate() const
Create a copy of a LIB_SYMBOL and assigns unique KIIDs to the copy and its children.
wxString GetName() const override
void SetOrdinal(int aOrdinal, FIELD_T aType)
void SetAutoAdded(bool aAutoAdded)
wxString GetName(bool aUseDefaultName=true) const
Return the field name (not translated).
void SetName(const wxString &aName)
void SetText(const wxString &aText) override
void SetNameShown(bool aShown=true)
void reportIssue(PREVIEW_ISSUE::FIELD aField, const wxString &aMessage, SEVERITY aSeverity) const
Reports to the preview's issue list when there is one, otherwise to the reporter.
void EnumerateSymbolLib(wxArrayString &aSymbolNameList, const wxString &aLibraryPath, const std::map< std::string, UTF8 > *aProperties=nullptr) override
Populate a list of LIB_SYMBOL alias names contained within the library aLibraryPath.
CONNECT_STATUS connect()
Connects, and waits out a file another instance is opening at the same moment when the library never ...
DIALOG_SHIM * CreateConfigurationDialog(wxWindow *aParent, const wxString &aLibraryPath) override
std::shared_mutex m_cacheMutex
PREVIEW_ISSUES * m_previewIssues
Collects the problems found while building a preview; null outside BuildPreviewSymbols()
std::mutex m_connectMutex
std::shared_ptr< DATABASE_CONNECTION > m_conn
Generally will be null if no valid connection is established.
void CancelLoad() override
Ends the waits of a load in progress and of any that starts before ResumeLoad(), so closing a project...
static wxString SymbolNameForRow(const DATABASE_LIB_TABLE &aTable, bool aGloballyUniqueKeys, const DATABASE_CONNECTION::ROW &aRow, std::string *aRawKey=nullptr)
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...
void loadSettingsBounded(int aTimeoutSeconds)
Loads the settings file on a thread that can be abandoned, so a share that went away cannot hold the ...
void ensureSettings(const wxString &aSettingsPath)
static thread_local std::unordered_set< wxString > s_inProgressLoads
LIB_IDs whose resolution is in flight on the calling thread, used to break self-referential cycles wh...
std::shared_ptr< DATABASE_CONNECTION > currentConnection()
Copies the connection under the cache lock so the caller keeps it alive across a reconnect.
void backgroundRefreshWorker()
std::atomic< bool > m_loadCancelled
Set by CancelLoad(), cleared by ResumeLoad()
static std::vector< std::unique_ptr< LIB_SYMBOL > > BuildPreviewSymbols(SYMBOL_LIBRARY_ADAPTER *aLibs, const DATABASE_LIB_TABLE &aTable, bool aGloballyUniqueKeys, const std::vector< DATABASE_CONNECTION::ROW > &aRows, PREVIEW_ISSUES *aIssues)
Builds symbols from already-fetched rows exactly as the library would name and load them,...
std::atomic< bool > m_cachePopulated
True once the LIB_SYMBOL cache has been materialized at least once.
void stopBackgroundRefresh()
bool m_inCacheLib
Re-entrancy guard for cacheLib(), tripped when a self-referential load routes back through the adapte...
std::map< wxString, std::unique_ptr< LIB_SYMBOL > > m_nameToSymbolcache
std::map< wxString, std::pair< std::string, std::string > > m_sanitizedNameMap
CONNECT_STATUS connectOnce()
One attempt at connect(), with m_connectMutex held.
std::set< wxString > m_defaultShownFields
virtual ~SCH_IO_DATABASE()
void cacheLib(const std::shared_ptr< DATABASE_CONNECTION > &aConn)
size_t m_cacheSignature
Signature of the raw database rows at last materialization; used to skip rebuilding the LIB_SYMBOL ca...
std::vector< std::pair< const DATABASE_LIB_TABLE *, std::vector< DATABASE_CONNECTION::ROW > > > TABLE_RESULT_LIST
bool connectionStopped() const
static CONNECTION_RESULT CreateConnection(const DATABASE_LIB_SETTINGS &aSettings, bool aConnectNow=true, const wxString &aBaseDir=wxEmptyString)
Opens a connection described by aSettings, expanding ${CWD} in a connection string to the directory h...
void GetSubLibraryNames(std::vector< wxString > &aNames) override
Retrieves a list of sub-libraries in this library.
static std::chrono::milliseconds busyRetryWait(long long aMaxAgeSeconds, int aAttempt, std::minstd_rand &aRng)
std::unique_ptr< DATABASE_LIB_SETTINGS > m_settings
static std::optional< bool > boolFromAny(const std::any &aVal)
size_t computeSignature(const TABLE_RESULT_LIST &aTableResults) const
void CheckLibrary(const wxString &aLibraryPath, const std::map< std::string, UTF8 > *aProperties=nullptr) override
Validate that the library at aLibraryPath is reachable and well-formed, without necessarily loading s...
SYMBOL_LIBRARY_ADAPTER * m_adapter
void startBackgroundRefresh()
std::thread m_refreshThread
static constexpr int MAX_BUSY_ATTEMPT
Busy retries past this attempt stop doubling the window.
long long m_cacheTimestamp
std::condition_variable m_refreshCV
void GetAvailableSymbolFields(std::vector< wxString > &aNames) override
Retrieves a list of (custom) field names that are present on symbols in this library.
std::atomic< bool > m_refreshRunning
bool m_previewMode
Set on the throwaway instance behind BuildPreviewSymbols()
std::atomic< bool > m_stopping
bool materializeCache(const TABLE_RESULT_LIST &aTableResults, std::map< wxString, std::unique_ptr< LIB_SYMBOL > > &aSymbolCache, std::map< wxString, std::pair< std::string, std::string > > &aSanitizedNameMap)
std::unique_ptr< LIB_SYMBOL > loadSymbolFromRow(const wxString &aSymbolName, const DATABASE_LIB_TABLE &aTable, const DATABASE_CONNECTION::ROW &aRow)
static constexpr std::chrono::seconds FIRST_CONNECT_BUDGET
The race this covers lasts milliseconds, so a file in use for longer has a real holder and is reporte...
static CONNECTION_RESULT connectionFor(DB_BACKEND_RESULT &&aBackend, bool aConnectNow)
Wraps a backend in a connection, or reports why there is none.
void ResumeLoad() noexcept override
Called after the loads that were cancelled have ended, to clear what CancelLoad() left behind.
std::set< wxString > m_customFields
std::mutex m_symbolLoadMutex
LIB_SYMBOL * resolveSourceSymbol(const LIB_ID &aId)
Resolves one entry of a row's symbols column through the library adapter.
std::shared_ptr< DATABASE_CONNECTION > ensureConnection()
Returns a live connection or throws.
std::vector< PREVIEW_ISSUE > PREVIEW_ISSUES
LIB_SYMBOL * LoadSymbol(const wxString &aLibraryPath, const wxString &aAliasName, const std::map< std::string, UTF8 > *aProperties=nullptr) override
Load a LIB_SYMBOL object having aPartName from the aLibraryPath containing a library format that this...
std::mutex m_refreshMutex
static SCH_FILE_T EnumFromStr(const wxString &aFileType)
Return the file type from the corresponding plugin type name: "kicad", "legacy", etc.
SCH_IO(const wxString &aName)
Base class for any item which can be embedded within the SCHEMATIC container class,...
SCH_ITEM * Duplicate(bool addToParentGroup, SCH_COMMIT *aCommit=nullptr, bool aDoClone=false) const
Routine to create a new copy of given item.
virtual void SetBodyStyle(int aBodyStyle)
An interface to the global shared library manager that is schematic-specific and linked to one projec...
static const char * PropPowerSymsOnly
An 8 bit string that is assuredly encoded in UTF8, and supplies special conversion support to and fro...
const char * c_str() const
@ INVALID_TYPE
The source type is not one KiCad knows.
@ UNREACHABLE
Nothing answered at the server address, or it refused the connection.
@ NOT_SUPPORTED_ON_PLATFORM
No backend for the source type exists on this platform.
@ FILE_IN_USE
The database file is locked by another process.
int DatabaseFileWaitSeconds(int aTimeoutSeconds)
#define IGNORE_PARENT_GROUP
std::chrono::milliseconds FileInUseBackoff(int aAttempt, std::minstd_rand &aRng)
std::filesystem::path ToFsPath(const wxString &aPath)
Give aPath to std::filesystem without a detour through the C locale.
#define THROW_IO_ERROR(msg)
macro which captures the "call site" values of FILE_, __FUNCTION & LINE
#define THROW_IO_ERRORF(msg,...)
const wxChar *const traceDatabase
Flag to enable database library tracing.
static constexpr void hash_combine(std::size_t &seed)
This is a dummy function to take the final case of hash_combine below.
PIN_MAP MakeLegacyPinMap(const wxString &aName, const std::unordered_map< wxString, std::vector< wxString > > &aAssignments)
Build a single named PIN_MAP from a legacy symbol-pin to footprint-pad(s) assignment table (the flat ...
std::vector< ASSOCIATED_FOOTPRINT > ParseAssociatedFootprints(const nlohmann::json &aParent)
Parse the spec-form footprint associations from aParent["associated_footprints"] (issue #2282).
std::unordered_map< wxString, std::vector< wxString > > ParseLegacyPinAssignments(const nlohmann::json &aArray)
Parse the legacy flat pin-assignment JSON array (MR !2540 form) into a symbol-pin to footprint-pad(s)...
PIN_MAP_SET ParsePinMapSet(const nlohmann::json &aParent)
Parse the spec-form named maps from aParent["pin_maps"] into a PIN_MAP_SET (issue #2282).
static std::string strValue(double aValue)
static wxString cwdDirFor(const DATABASE_LIB_SETTINGS &aSettings, const wxString &aBaseDir)
The directory ${CWD} stands for, which is the settings file's unless the caller supplies one.
What a failed query left behind, copied before another thread's query can replace it.
bool visible_in_chooser
Whether the column is shown by default in the chooser.
std::string column
Database column 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.
wxString name_wx
KiCad field name (converted)
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
std::string pins_col
Column name containing JSON pin assignments (optional)
The outcome of building a backend; backend is null when failure and error say why.
std::string footprint_filters
std::string exclude_from_sim
std::string exclude_from_board
std::string exclude_from_bom
A connection built from settings, or the reason none could be built.
std::unique_ptr< DATABASE_CONNECTION > connection
Null when no backend exists for the source type.
DB_FAILURE failure
Why connection is null.
std::string error
The message naming the problem when connection is null.
Why the last connect() attempt failed, for the caller to report from its own copy.
FIELD_T
The set of all field indices assuming an array like sequence that a SCH_COMPONENT or LIB_PART can hol...
@ USER
The field ID hasn't been set yet; field is invalid.
static std::vector< uint8_t > bytes(const std::string &aText)
wxString result
Test unit parsing edge cases and error handling.