2022-05-29 19:29:32 +00:00
|
|
|
/*
|
|
|
|
* This program source code file is part of KiCad, a free EDA CAD application.
|
|
|
|
*
|
|
|
|
* Copyright (C) 2022 Jon Evans <jon@craftyjon.com>
|
|
|
|
* Copyright (C) 2022 KiCad Developers, see AUTHORS.txt for contributors.
|
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify it
|
|
|
|
* under the terms of the GNU General Public License as published by the
|
|
|
|
* Free Software Foundation, either version 3 of the License, or (at your
|
|
|
|
* option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful, but
|
|
|
|
* WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License along
|
|
|
|
* with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <iostream>
|
2022-11-10 02:24:31 +00:00
|
|
|
#include <unordered_set>
|
2023-03-29 16:53:28 +00:00
|
|
|
#include <wx/log.h>
|
2022-05-29 19:29:32 +00:00
|
|
|
|
|
|
|
#include <boost/algorithm/string.hpp>
|
|
|
|
|
|
|
|
#include <database/database_connection.h>
|
|
|
|
#include <database/database_lib_settings.h>
|
|
|
|
#include <fmt.h>
|
|
|
|
#include <lib_symbol.h>
|
|
|
|
#include <symbol_lib_table.h>
|
|
|
|
|
|
|
|
#include "sch_database_plugin.h"
|
|
|
|
|
|
|
|
|
|
|
|
SCH_DATABASE_PLUGIN::SCH_DATABASE_PLUGIN() :
|
|
|
|
m_libTable( nullptr ),
|
|
|
|
m_settings(),
|
|
|
|
m_conn()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
SCH_DATABASE_PLUGIN::~SCH_DATABASE_PLUGIN()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void SCH_DATABASE_PLUGIN::EnumerateSymbolLib( wxArrayString& aSymbolNameList,
|
|
|
|
const wxString& aLibraryPath,
|
2022-11-06 16:51:52 +00:00
|
|
|
const STRING_UTF8_MAP* aProperties )
|
2022-05-29 19:29:32 +00:00
|
|
|
{
|
|
|
|
std::vector<LIB_SYMBOL*> symbols;
|
|
|
|
EnumerateSymbolLib( symbols, aLibraryPath, aProperties );
|
|
|
|
|
|
|
|
for( LIB_SYMBOL* symbol : symbols )
|
|
|
|
aSymbolNameList.Add( symbol->GetName() );
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void SCH_DATABASE_PLUGIN::EnumerateSymbolLib( std::vector<LIB_SYMBOL*>& aSymbolList,
|
|
|
|
const wxString& aLibraryPath,
|
2022-11-06 16:51:52 +00:00
|
|
|
const STRING_UTF8_MAP* aProperties )
|
2022-05-29 19:29:32 +00:00
|
|
|
{
|
|
|
|
wxCHECK_RET( m_libTable, "Database plugin missing library table handle!" );
|
|
|
|
ensureSettings( aLibraryPath );
|
|
|
|
ensureConnection();
|
|
|
|
|
2023-05-07 13:06:23 +00:00
|
|
|
if( !m_conn )
|
|
|
|
THROW_IO_ERROR( m_lastError );
|
|
|
|
|
2022-05-29 19:29:32 +00:00
|
|
|
bool powerSymbolsOnly = ( aProperties &&
|
|
|
|
aProperties->find( SYMBOL_LIB_TABLE::PropPowerSymsOnly ) !=
|
|
|
|
aProperties->end() );
|
|
|
|
|
|
|
|
for( const DATABASE_LIB_TABLE& table : m_settings->m_Tables )
|
|
|
|
{
|
|
|
|
std::vector<DATABASE_CONNECTION::ROW> results;
|
|
|
|
|
2023-06-22 02:47:41 +00:00
|
|
|
if( !m_conn->SelectAll( table.table, table.key_col, results ) )
|
2022-05-29 19:29:32 +00:00
|
|
|
{
|
|
|
|
if( !m_conn->GetLastError().empty() )
|
|
|
|
{
|
|
|
|
wxString msg = wxString::Format( _( "Error reading database table %s: %s" ),
|
|
|
|
table.table, m_conn->GetLastError() );
|
|
|
|
THROW_IO_ERROR( msg );
|
|
|
|
}
|
|
|
|
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
for( DATABASE_CONNECTION::ROW& result : results )
|
|
|
|
{
|
|
|
|
if( !result.count( table.key_col ) )
|
|
|
|
continue;
|
|
|
|
|
2022-12-10 21:35:11 +00:00
|
|
|
std::string prefix = table.name.empty() ? "" : fmt::format( "{}/", table.name );
|
|
|
|
wxString name( fmt::format( "{}{}", prefix,
|
2022-05-29 19:29:32 +00:00
|
|
|
std::any_cast<std::string>( result[table.key_col] ) ) );
|
|
|
|
|
|
|
|
LIB_SYMBOL* symbol = loadSymbolFromRow( name, table, result );
|
|
|
|
|
|
|
|
if( symbol && ( !powerSymbolsOnly || symbol->IsPower() ) )
|
|
|
|
aSymbolList.emplace_back( symbol );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
LIB_SYMBOL* SCH_DATABASE_PLUGIN::LoadSymbol( const wxString& aLibraryPath,
|
|
|
|
const wxString& aAliasName,
|
2022-11-06 16:51:52 +00:00
|
|
|
const STRING_UTF8_MAP* aProperties )
|
2022-05-29 19:29:32 +00:00
|
|
|
{
|
|
|
|
wxCHECK( m_libTable, nullptr );
|
|
|
|
ensureSettings( aLibraryPath );
|
|
|
|
ensureConnection();
|
|
|
|
|
2023-05-07 13:06:23 +00:00
|
|
|
if( !m_conn )
|
|
|
|
THROW_IO_ERROR( m_lastError );
|
|
|
|
|
2022-12-10 21:35:11 +00:00
|
|
|
/*
|
|
|
|
* Table names are tricky, in order to allow maximum flexibility to the user.
|
|
|
|
* The slash character is used as a separator between a table name and symbol name, but symbol
|
|
|
|
* names may also contain slashes and table names may now also be empty (which results in the
|
|
|
|
* slash being dropped in the symbol name when placing a new symbol). So, if a slash is found,
|
|
|
|
* we check if the string before the slash is a valid table name. If not, we assume the table
|
|
|
|
* name is blank if our config has an entry for the null table.
|
|
|
|
*/
|
2022-05-29 19:29:32 +00:00
|
|
|
|
2022-12-10 21:35:11 +00:00
|
|
|
std::string tableName = "";
|
|
|
|
std::string symbolName( aAliasName.ToUTF8() );
|
|
|
|
|
|
|
|
if( aAliasName.Contains( '/' ) )
|
|
|
|
{
|
|
|
|
tableName = std::string( aAliasName.BeforeFirst( '/' ).ToUTF8() );
|
|
|
|
symbolName = std::string( aAliasName.AfterFirst( '/' ).ToUTF8() );
|
|
|
|
}
|
|
|
|
|
|
|
|
std::vector<const DATABASE_LIB_TABLE*> tablesToTry;
|
2022-05-29 19:29:32 +00:00
|
|
|
|
|
|
|
for( const DATABASE_LIB_TABLE& tableIter : m_settings->m_Tables )
|
|
|
|
{
|
2022-08-26 23:09:59 +00:00
|
|
|
if( tableIter.name == tableName )
|
2022-12-10 21:35:11 +00:00
|
|
|
tablesToTry.emplace_back( &tableIter );
|
2022-05-29 19:29:32 +00:00
|
|
|
}
|
|
|
|
|
2022-12-10 21:35:11 +00:00
|
|
|
if( tablesToTry.empty() )
|
2022-05-29 19:29:32 +00:00
|
|
|
{
|
2022-12-10 21:35:11 +00:00
|
|
|
wxLogTrace( traceDatabase, wxT( "LoadSymbol: table '%s' not found in config" ), tableName );
|
2022-05-29 19:29:32 +00:00
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2022-12-10 21:35:11 +00:00
|
|
|
const DATABASE_LIB_TABLE* foundTable = nullptr;
|
2022-05-29 19:29:32 +00:00
|
|
|
DATABASE_CONNECTION::ROW result;
|
|
|
|
|
2022-12-10 21:35:11 +00:00
|
|
|
for( const DATABASE_LIB_TABLE* table : tablesToTry )
|
2022-05-29 19:29:32 +00:00
|
|
|
{
|
2022-12-10 21:35:11 +00:00
|
|
|
if( m_conn->SelectOne( table->table, std::make_pair( table->key_col, symbolName ),
|
|
|
|
result ) )
|
|
|
|
{
|
|
|
|
foundTable = table;
|
|
|
|
wxLogTrace( traceDatabase, wxT( "LoadSymbol: SelectOne (%s, %s) found in %s" ),
|
|
|
|
table->key_col, symbolName, table->table );
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
wxLogTrace( traceDatabase, wxT( "LoadSymbol: SelectOne (%s, %s) failed for table %s" ),
|
|
|
|
table->key_col, symbolName, table->table );
|
|
|
|
}
|
2022-05-29 19:29:32 +00:00
|
|
|
}
|
|
|
|
|
2022-12-10 21:35:11 +00:00
|
|
|
wxCHECK( foundTable, nullptr );
|
|
|
|
|
|
|
|
return loadSymbolFromRow( aAliasName, *foundTable, result );
|
2022-05-29 19:29:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2022-08-27 03:52:56 +00:00
|
|
|
void SCH_DATABASE_PLUGIN::GetSubLibraryNames( std::vector<wxString>& aNames )
|
|
|
|
{
|
|
|
|
ensureSettings( wxEmptyString );
|
|
|
|
|
|
|
|
aNames.clear();
|
|
|
|
|
2022-12-10 21:35:11 +00:00
|
|
|
std::set<wxString> tableNames;
|
|
|
|
|
2022-08-27 03:52:56 +00:00
|
|
|
for( const DATABASE_LIB_TABLE& tableIter : m_settings->m_Tables )
|
2022-12-10 21:35:11 +00:00
|
|
|
{
|
|
|
|
if( tableNames.count( tableIter.name ) )
|
|
|
|
continue;
|
|
|
|
|
2022-08-27 03:52:56 +00:00
|
|
|
aNames.emplace_back( tableIter.name );
|
2022-12-10 21:35:11 +00:00
|
|
|
tableNames.insert( tableIter.name );
|
|
|
|
}
|
2022-08-27 03:52:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2022-08-28 23:02:12 +00:00
|
|
|
void SCH_DATABASE_PLUGIN::GetAvailableSymbolFields( std::vector<wxString>& aNames )
|
|
|
|
{
|
|
|
|
std::copy( m_customFields.begin(), m_customFields.end(), std::back_inserter( aNames ) );
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void SCH_DATABASE_PLUGIN::GetDefaultSymbolFields( std::vector<wxString>& aNames )
|
|
|
|
{
|
|
|
|
std::copy( m_defaultShownFields.begin(), m_defaultShownFields.end(),
|
|
|
|
std::back_inserter( aNames ) );
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2023-02-26 23:39:31 +00:00
|
|
|
bool SCH_DATABASE_PLUGIN::TestConnection( wxString* aErrorMsg )
|
|
|
|
{
|
|
|
|
if( m_conn && m_conn->IsConnected() )
|
|
|
|
return true;
|
|
|
|
|
|
|
|
connect();
|
|
|
|
|
2023-05-07 13:06:23 +00:00
|
|
|
if( aErrorMsg && ( !m_conn || !m_conn->IsConnected() ) )
|
|
|
|
*aErrorMsg = m_lastError;
|
2023-02-26 23:39:31 +00:00
|
|
|
|
|
|
|
return m_conn && m_conn->IsConnected();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2022-05-29 19:29:32 +00:00
|
|
|
void SCH_DATABASE_PLUGIN::ensureSettings( const wxString& aSettingsPath )
|
|
|
|
{
|
2022-08-31 00:58:49 +00:00
|
|
|
auto tryLoad =
|
|
|
|
[&]()
|
|
|
|
{
|
|
|
|
if( !m_settings->LoadFromFile() )
|
|
|
|
{
|
|
|
|
wxString msg = wxString::Format(
|
|
|
|
_( "Could not load database library: settings file %s missing or invalid" ),
|
|
|
|
aSettingsPath );
|
|
|
|
|
|
|
|
THROW_IO_ERROR( msg );
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2022-08-27 03:52:56 +00:00
|
|
|
if( !m_settings && !aSettingsPath.IsEmpty() )
|
2022-05-29 19:29:32 +00:00
|
|
|
{
|
|
|
|
std::string path( aSettingsPath.ToUTF8() );
|
|
|
|
m_settings = std::make_unique<DATABASE_LIB_SETTINGS>( path );
|
|
|
|
m_settings->SetReadOnly( true );
|
|
|
|
|
2022-08-31 00:58:49 +00:00
|
|
|
tryLoad();
|
2022-05-29 19:29:32 +00:00
|
|
|
}
|
2022-08-31 00:58:49 +00:00
|
|
|
else if( !m_conn && m_settings )
|
|
|
|
{
|
|
|
|
// If we have valid settings but no connection yet; reload settings in case user is editing
|
|
|
|
tryLoad();
|
|
|
|
}
|
|
|
|
else if( m_conn && m_settings && !aSettingsPath.IsEmpty() )
|
2022-05-29 19:29:32 +00:00
|
|
|
{
|
|
|
|
wxASSERT_MSG( aSettingsPath == m_settings->GetFilename(),
|
|
|
|
"Path changed for database library without re-initializing plugin!" );
|
|
|
|
}
|
2022-08-31 00:58:49 +00:00
|
|
|
else if( !m_settings )
|
|
|
|
{
|
|
|
|
wxLogTrace( traceDatabase, wxT( "ensureSettings: no settings but no valid path!" ) );
|
|
|
|
}
|
2022-05-29 19:29:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void SCH_DATABASE_PLUGIN::ensureConnection()
|
|
|
|
{
|
|
|
|
wxCHECK_RET( m_settings, "Call ensureSettings before ensureConnection!" );
|
|
|
|
|
2023-02-26 23:39:31 +00:00
|
|
|
connect();
|
|
|
|
|
|
|
|
if( !m_conn || !m_conn->IsConnected() )
|
|
|
|
{
|
|
|
|
wxString msg = wxString::Format(
|
|
|
|
_( "Could not load database library: could not connect to database %s (%s)" ),
|
2023-05-07 13:06:23 +00:00
|
|
|
m_settings->m_Source.dsn, m_lastError );
|
2023-02-26 23:39:31 +00:00
|
|
|
|
|
|
|
THROW_IO_ERROR( msg );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void SCH_DATABASE_PLUGIN::connect()
|
|
|
|
{
|
|
|
|
wxCHECK_RET( m_settings, "Call ensureSettings before connect()!" );
|
|
|
|
|
2022-12-22 17:14:16 +00:00
|
|
|
if( m_conn && !m_conn->IsConnected() )
|
|
|
|
m_conn.reset();
|
|
|
|
|
2022-05-29 19:29:32 +00:00
|
|
|
if( !m_conn )
|
|
|
|
{
|
|
|
|
if( m_settings->m_Source.connection_string.empty() )
|
|
|
|
{
|
|
|
|
m_conn = std::make_unique<DATABASE_CONNECTION>( m_settings->m_Source.dsn,
|
|
|
|
m_settings->m_Source.username,
|
|
|
|
m_settings->m_Source.password,
|
|
|
|
m_settings->m_Source.timeout );
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
std::string cs = m_settings->m_Source.connection_string;
|
|
|
|
std::string basePath( wxFileName( m_settings->GetFilename() ).GetPath().ToUTF8() );
|
|
|
|
|
|
|
|
// Database drivers that use files operate on absolute paths, so provide a mechanism
|
2023-05-07 13:06:23 +00:00
|
|
|
// for specifying on-disk databases that live next to the kicad_dbl file
|
2022-05-29 19:29:32 +00:00
|
|
|
boost::replace_all( cs, "${CWD}", basePath );
|
|
|
|
|
|
|
|
m_conn = std::make_unique<DATABASE_CONNECTION>( cs, m_settings->m_Source.timeout );
|
|
|
|
}
|
|
|
|
|
|
|
|
if( !m_conn->IsConnected() )
|
|
|
|
{
|
2023-05-07 13:06:23 +00:00
|
|
|
m_lastError = m_conn->GetLastError();
|
2022-08-31 00:58:49 +00:00
|
|
|
m_conn.reset();
|
2023-02-26 23:39:31 +00:00
|
|
|
return;
|
2022-05-29 19:29:32 +00:00
|
|
|
}
|
2022-08-31 02:18:21 +00:00
|
|
|
|
2023-06-22 02:47:41 +00:00
|
|
|
for( const DATABASE_LIB_TABLE& tableIter : m_settings->m_Tables )
|
|
|
|
{
|
|
|
|
std::set<std::string> columns;
|
|
|
|
|
|
|
|
columns.insert( tableIter.key_col );
|
|
|
|
columns.insert( tableIter.footprints_col );
|
|
|
|
columns.insert( tableIter.symbols_col );
|
|
|
|
|
|
|
|
columns.insert( tableIter.properties.description );
|
|
|
|
columns.insert( tableIter.properties.footprint_filters );
|
|
|
|
columns.insert( tableIter.properties.keywords );
|
2023-08-08 16:32:08 +00:00
|
|
|
columns.insert( tableIter.properties.exclude_from_sim );
|
2023-06-22 02:47:41 +00:00
|
|
|
columns.insert( tableIter.properties.exclude_from_bom );
|
|
|
|
columns.insert( tableIter.properties.exclude_from_board );
|
|
|
|
|
|
|
|
for( const DATABASE_FIELD_MAPPING& field : tableIter.fields )
|
|
|
|
columns.insert( field.column );
|
|
|
|
|
|
|
|
m_conn->CacheTableInfo( tableIter.table, columns );
|
|
|
|
}
|
|
|
|
|
2022-08-31 02:18:21 +00:00
|
|
|
m_conn->SetCacheParams( m_settings->m_Cache.max_size, m_settings->m_Cache.max_age );
|
2022-05-29 19:29:32 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2023-03-23 01:24:24 +00:00
|
|
|
std::optional<bool> SCH_DATABASE_PLUGIN::boolFromAny( const std::any& aVal )
|
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
|
|
|
bool val = std::any_cast<bool>( aVal );
|
|
|
|
return val;
|
|
|
|
}
|
|
|
|
catch( const std::bad_any_cast& )
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
try
|
|
|
|
{
|
|
|
|
int val = std::any_cast<int>( aVal );
|
|
|
|
return static_cast<bool>( val );
|
|
|
|
}
|
|
|
|
catch( const std::bad_any_cast& )
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
try
|
|
|
|
{
|
|
|
|
wxString strval( std::any_cast<std::string>( aVal ).c_str(), wxConvUTF8 );
|
|
|
|
|
|
|
|
if( strval.IsEmpty() )
|
|
|
|
return std::nullopt;
|
|
|
|
|
|
|
|
strval.MakeLower();
|
|
|
|
|
2023-03-26 23:43:33 +00:00
|
|
|
for( const auto& trueVal : { wxS( "true" ), wxS( "yes" ), wxS( "y" ), wxS( "1" ) } )
|
2023-03-23 01:24:24 +00:00
|
|
|
{
|
|
|
|
if( strval.Matches( trueVal ) )
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2023-03-26 23:43:33 +00:00
|
|
|
for( const auto& falseVal : { wxS( "false" ), wxS( "no" ), wxS( "n" ), wxS( "0" ) } )
|
2023-03-23 01:24:24 +00:00
|
|
|
{
|
|
|
|
if( strval.Matches( falseVal ) )
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
catch( const std::bad_any_cast& )
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
return std::nullopt;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2022-05-29 19:29:32 +00:00
|
|
|
LIB_SYMBOL* SCH_DATABASE_PLUGIN::loadSymbolFromRow( const wxString& aSymbolName,
|
|
|
|
const DATABASE_LIB_TABLE& aTable,
|
|
|
|
const DATABASE_CONNECTION::ROW& aRow )
|
|
|
|
{
|
|
|
|
LIB_SYMBOL* symbol = nullptr;
|
|
|
|
|
|
|
|
if( aRow.count( aTable.symbols_col ) )
|
|
|
|
{
|
2023-03-23 01:24:24 +00:00
|
|
|
LIB_SYMBOL* originalSymbol = nullptr;
|
|
|
|
|
2022-05-29 19:29:32 +00:00
|
|
|
// TODO: Support multiple options for symbol
|
|
|
|
std::string symbolIdStr = std::any_cast<std::string>( aRow.at( aTable.symbols_col ) );
|
|
|
|
LIB_ID symbolId;
|
|
|
|
symbolId.Parse( std::any_cast<std::string>( aRow.at( aTable.symbols_col ) ) );
|
|
|
|
|
2023-03-23 01:24:24 +00:00
|
|
|
if( symbolId.IsValid() )
|
|
|
|
originalSymbol = m_libTable->LoadSymbol( symbolId );
|
2022-05-29 19:29:32 +00:00
|
|
|
|
|
|
|
if( originalSymbol )
|
|
|
|
{
|
|
|
|
wxLogTrace( traceDatabase, wxT( "loadSymbolFromRow: found original symbol '%s'" ),
|
|
|
|
symbolIdStr );
|
|
|
|
symbol = originalSymbol->Duplicate();
|
2023-01-20 03:45:46 +00:00
|
|
|
symbol->SetSourceLibId( symbolId );
|
2022-05-29 19:29:32 +00:00
|
|
|
}
|
2023-03-23 01:24:24 +00:00
|
|
|
else if( !symbolId.IsValid() )
|
|
|
|
{
|
|
|
|
wxLogTrace( traceDatabase, wxT( "loadSymboFromRow: source symbol id '%s' is invalid, "
|
|
|
|
"will create empty symbol" ), symbolIdStr );
|
|
|
|
}
|
2022-05-29 19:29:32 +00:00
|
|
|
else
|
|
|
|
{
|
|
|
|
wxLogTrace( traceDatabase, wxT( "loadSymboFromRow: source symbol '%s' not found, "
|
|
|
|
"will create empty symbol" ), symbolIdStr );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if( !symbol )
|
|
|
|
{
|
|
|
|
// Actual symbol not found: return metadata only; error will be indicated in the
|
|
|
|
// symbol chooser
|
|
|
|
symbol = new LIB_SYMBOL( aSymbolName );
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
symbol->SetName( aSymbolName );
|
|
|
|
}
|
|
|
|
|
2022-08-27 03:52:56 +00:00
|
|
|
symbol->LibId().SetSubLibraryName( aTable.name );
|
|
|
|
|
2022-05-29 19:29:32 +00:00
|
|
|
if( aRow.count( aTable.footprints_col ) )
|
|
|
|
{
|
|
|
|
std::string footprints = std::any_cast<std::string>( aRow.at( aTable.footprints_col ) );
|
2023-08-23 23:16:49 +00:00
|
|
|
|
|
|
|
wxString footprintsStr = wxString( footprints.c_str(), wxConvUTF8 );
|
|
|
|
wxArrayString footprintsList;
|
|
|
|
wxStringTokenizer tokenizer( footprintsStr, ';' );
|
|
|
|
|
|
|
|
while( tokenizer.HasMoreTokens() )
|
|
|
|
footprintsList.Add( tokenizer.GetNextToken() );
|
|
|
|
|
|
|
|
if( footprintsList.size() > 0 )
|
|
|
|
symbol->GetFootprintField().SetText( footprintsList[0] );
|
|
|
|
|
|
|
|
symbol->SetFPFilters( footprintsList );
|
2022-05-29 19:29:32 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
wxLogTrace( traceDatabase, wxT( "loadSymboFromRow: footprint field %s not found." ),
|
|
|
|
aTable.footprints_col );
|
|
|
|
}
|
|
|
|
|
2022-11-09 03:18:29 +00:00
|
|
|
if( !aTable.properties.description.empty() && aRow.count( aTable.properties.description ) )
|
|
|
|
{
|
|
|
|
wxString value(
|
|
|
|
std::any_cast<std::string>( aRow.at( aTable.properties.description ) ).c_str(),
|
|
|
|
wxConvUTF8 );
|
|
|
|
symbol->SetDescription( value );
|
|
|
|
}
|
|
|
|
|
2022-11-10 01:50:01 +00:00
|
|
|
if( !aTable.properties.keywords.empty() && aRow.count( aTable.properties.keywords ) )
|
|
|
|
{
|
|
|
|
wxString value( std::any_cast<std::string>( aRow.at( aTable.properties.keywords ) ).c_str(),
|
|
|
|
wxConvUTF8 );
|
|
|
|
symbol->SetKeyWords( value );
|
|
|
|
}
|
|
|
|
|
2022-11-09 03:18:29 +00:00
|
|
|
if( !aTable.properties.footprint_filters.empty()
|
|
|
|
&& aRow.count( aTable.properties.footprint_filters ) )
|
|
|
|
{
|
|
|
|
wxString value( std::any_cast<std::string>( aRow.at( aTable.properties.footprint_filters ) )
|
|
|
|
.c_str(),
|
|
|
|
wxConvUTF8 );
|
|
|
|
wxArrayString filters;
|
|
|
|
filters.push_back( value );
|
|
|
|
symbol->SetFPFilters( filters );
|
|
|
|
}
|
|
|
|
|
2023-08-08 16:32:08 +00:00
|
|
|
if( !aTable.properties.exclude_from_sim.empty()
|
|
|
|
&& aRow.count( aTable.properties.exclude_from_sim ) )
|
|
|
|
{
|
|
|
|
std::optional<bool> val = boolFromAny( aRow.at( aTable.properties.exclude_from_sim ) );
|
|
|
|
|
|
|
|
if( val )
|
|
|
|
{
|
|
|
|
symbol->SetExcludedFromSim( *val );
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
wxLogTrace( traceDatabase, wxT( "loadSymbolFromRow: exclude_from_sim value for %s "
|
|
|
|
"could not be cast to a boolean" ), aSymbolName );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-11-09 03:18:29 +00:00
|
|
|
if( !aTable.properties.exclude_from_board.empty()
|
|
|
|
&& aRow.count( aTable.properties.exclude_from_board ) )
|
|
|
|
{
|
2023-03-23 01:24:24 +00:00
|
|
|
std::optional<bool> val = boolFromAny( aRow.at( aTable.properties.exclude_from_board ) );
|
|
|
|
|
|
|
|
if( val )
|
|
|
|
{
|
2023-06-25 02:03:06 +00:00
|
|
|
symbol->SetExcludedFromBoard( *val );
|
2023-03-23 01:24:24 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
wxLogTrace( traceDatabase, wxT( "loadSymbolFromRow: exclude_from_board value for %s "
|
|
|
|
"could not be cast to a boolean" ), aSymbolName );
|
|
|
|
}
|
2022-11-09 03:18:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if( !aTable.properties.exclude_from_bom.empty()
|
|
|
|
&& aRow.count( aTable.properties.exclude_from_bom ) )
|
|
|
|
{
|
2023-03-23 01:24:24 +00:00
|
|
|
std::optional<bool> val = boolFromAny( aRow.at( aTable.properties.exclude_from_bom ) );
|
|
|
|
|
|
|
|
if( val )
|
|
|
|
{
|
2023-06-25 02:03:06 +00:00
|
|
|
symbol->SetExcludedFromBOM( *val );
|
2023-03-23 01:24:24 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
wxLogTrace( traceDatabase, wxT( "loadSymbolFromRow: exclude_from_bom value for %s "
|
|
|
|
"could not be cast to a boolean" ), aSymbolName );
|
|
|
|
}
|
2022-11-09 03:18:29 +00:00
|
|
|
}
|
|
|
|
|
2022-11-10 02:24:31 +00:00
|
|
|
std::vector<LIB_FIELD*> fields;
|
|
|
|
symbol->GetFields( fields );
|
|
|
|
|
|
|
|
std::unordered_map<wxString, LIB_FIELD*> fieldsMap;
|
|
|
|
|
|
|
|
for( LIB_FIELD* field : fields )
|
|
|
|
fieldsMap[field->GetName()] = field;
|
|
|
|
|
2022-05-29 19:29:32 +00:00
|
|
|
for( const DATABASE_FIELD_MAPPING& mapping : aTable.fields )
|
|
|
|
{
|
|
|
|
if( !aRow.count( mapping.column ) )
|
|
|
|
{
|
2023-03-23 01:24:24 +00:00
|
|
|
wxLogTrace( traceDatabase, wxT( "loadSymbolFromRow: field %s not found in result" ),
|
2022-05-29 19:29:32 +00:00
|
|
|
mapping.column );
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2023-11-15 17:27:25 +00:00
|
|
|
std:: string strValue;
|
|
|
|
|
|
|
|
try
|
|
|
|
{
|
|
|
|
strValue = std::any_cast<std::string>( aRow.at( mapping.column ) );
|
|
|
|
}
|
|
|
|
catch( std::bad_any_cast& e )
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
wxString value( strValue.c_str(), wxConvUTF8 );
|
2022-05-29 19:29:32 +00:00
|
|
|
|
2022-11-09 03:18:29 +00:00
|
|
|
if( mapping.name == wxT( "Value" ) )
|
2022-05-29 19:29:32 +00:00
|
|
|
{
|
|
|
|
LIB_FIELD& field = symbol->GetValueField();
|
|
|
|
field.SetText( value );
|
2022-11-16 02:27:43 +00:00
|
|
|
|
|
|
|
if( !mapping.inherit_properties )
|
|
|
|
{
|
|
|
|
field.SetVisible( mapping.visible_on_add );
|
|
|
|
field.SetNameShown( mapping.show_name );
|
|
|
|
}
|
2022-05-29 19:29:32 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
else if( mapping.name == wxT( "Datasheet" ) )
|
|
|
|
{
|
|
|
|
LIB_FIELD& field = symbol->GetDatasheetField();
|
|
|
|
field.SetText( value );
|
|
|
|
|
2022-11-16 02:27:43 +00:00
|
|
|
if( !mapping.inherit_properties )
|
|
|
|
{
|
|
|
|
field.SetVisible( mapping.visible_on_add );
|
|
|
|
field.SetNameShown( mapping.show_name );
|
|
|
|
|
|
|
|
if( mapping.visible_on_add )
|
|
|
|
field.SetAutoAdded( true );
|
|
|
|
}
|
2022-05-29 19:29:32 +00:00
|
|
|
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2022-11-10 02:24:31 +00:00
|
|
|
LIB_FIELD* field;
|
|
|
|
bool isNew = false;
|
|
|
|
|
|
|
|
if( fieldsMap.count( mapping.name ) )
|
|
|
|
{
|
|
|
|
field = fieldsMap[mapping.name];
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
field = new LIB_FIELD( symbol->GetNextAvailableFieldId() );
|
|
|
|
field->SetName( mapping.name );
|
|
|
|
isNew = true;
|
|
|
|
fieldsMap[mapping.name] = field;
|
|
|
|
}
|
|
|
|
|
|
|
|
if( !mapping.inherit_properties || isNew )
|
|
|
|
{
|
|
|
|
field->SetVisible( mapping.visible_on_add );
|
|
|
|
field->SetAutoAdded( true );
|
|
|
|
field->SetNameShown( mapping.show_name );
|
|
|
|
}
|
|
|
|
|
2022-05-29 19:29:32 +00:00
|
|
|
field->SetText( value );
|
|
|
|
|
2022-11-10 02:24:31 +00:00
|
|
|
if( isNew )
|
|
|
|
symbol->AddField( field );
|
2022-08-28 23:02:12 +00:00
|
|
|
|
|
|
|
m_customFields.insert( mapping.name );
|
|
|
|
|
|
|
|
if( mapping.visible_in_chooser )
|
|
|
|
m_defaultShownFields.insert( mapping.name );
|
2022-05-29 19:29:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return symbol;
|
2022-08-31 00:58:49 +00:00
|
|
|
}
|