28#include <boost/algorithm/string.hpp>
37#include <wx/filename.h>
54 bool aVisibleOnAdd,
bool aVisibleInChooser,
55 bool aShowName,
bool aInheritProperties ) :
58 name_wx( aName.c_str(), wxConvUTF8 ),
71 name_wx = wxString( aName.c_str(), wxConvUTF8 );
86 std::map<std::string, std::string> pairs;
87 const size_t len = aConnectionString.size();
90 auto skipSpaces = [&]()
92 while( pos < len && std::isspace(
static_cast<unsigned char>( aConnectionString[pos] ) ) )
103 if( aConnectionString[pos] ==
';' )
109 size_t eq = aConnectionString.find_first_of(
"=;", pos );
111 if( eq == std::string::npos || aConnectionString[eq] ==
';' )
114 std::string key = boost::algorithm::trim_right_copy( aConnectionString.substr( pos, eq - pos ) );
115 boost::algorithm::to_lower( key );
125 if( pos < len && aConnectionString[pos] ==
'{' )
131 while( pos < len && !closed )
133 const char c = aConnectionString[pos++];
137 else if( pos < len && aConnectionString[pos] ==
'}' )
138 value += aConnectionString[pos++];
148 if( pos < len && aConnectionString[pos] !=
';' )
153 size_t end = std::min( aConnectionString.find(
';', pos ), len );
155 value = boost::algorithm::trim_right_copy( aConnectionString.substr( pos,
end - pos ) );
157 if( value.find_first_of(
"={}\"'" ) != std::string::npos )
163 pairs.emplace( key, value );
173 return aText.find(
'=' ) != std::string::npos;
180 const std::string basePath( aBaseDir.ToUTF8() );
185 boost::replace_all( expanded.
path,
"${CWD}", basePath );
203 auto find = [&]( std::initializer_list<const char*> aKeys ) -> std::string
205 for(
const char* key : aKeys )
207 auto it = pairs.find( key );
209 if( it != pairs.end() && !it->second.empty() )
217 const std::string driver =
find( {
"driver" } );
218 const std::string target =
find( {
"dbq",
"database",
"data source",
"dsn",
"server" } );
226 return target +
" (" + driver +
")";
235static void patchKey( nlohmann::json& aObj,
const std::string& aKey,
const T& aValue,
const T& aParsed,
238 if( aValue == aParsed && ( aObj.contains( aKey ) || !aRequired ) )
241 if constexpr( std::is_same_v<T, std::string> )
243 if( aValue.empty() && !aRequired )
270 nlohmann::json out = aField.
source.is_object() ? aField.
source : nlohmann::json::object();
288 table.source = aEntry;
298 std::erase(
table.name,
'/' );
300 if( aEntry.contains(
"properties" ) && aEntry[
"properties"].is_object() )
302 const nlohmann::json& propJson = aEntry[
"properties"];
312 if( aEntry.contains(
"fields" ) && aEntry[
"fields"].is_array() )
314 for(
const nlohmann::json& fieldJson : aEntry[
"fields"] )
316 if( fieldJson.empty() || !fieldJson.is_object() )
329 nlohmann::json out = aTable.
source.is_object() ? aTable.
source : nlohmann::json::object();
341 bool hadProps = out.contains(
"properties" );
342 nlohmann::json propJson = hadProps ? out[
"properties"] : nlohmann::json::object();
345 if( props != oldProps && !propJson.is_object() )
346 propJson = nlohmann::json::object();
348 if( propJson.is_object() )
357 if( hadProps || !propJson.empty() )
358 out[
"properties"] = propJson;
361 if( !aTable.
fields.empty() || out.contains(
"fields" ) )
363 nlohmann::json fieldsJson = nlohmann::json::array();
369 bool fieldsEdited = aTable.
fields.size() != parsed.
fields.size();
371 for(
size_t i = 0; !fieldsEdited && i < aTable.
fields.size(); ++i )
374 if( fieldsEdited || !out.contains(
"fields" ) )
375 out[
"fields"] = fieldsJson;
391 [&]() -> nlohmann::json
403 [&](
const nlohmann::json& aType )
407 if( aType ==
"odbc" )
411 else if( aType ==
"sqlite" )
415 else if( aType ==
"access" )
422 m_Source.type_name = aType.is_string() ? aType.get<std::string>() : aType.dump();
436 &
m_Source.connection_string,
"" ) );
443 [&]() -> nlohmann::json
445 nlohmann::json libraries = nlohmann::json::array();
447 for( const DATABASE_LIB_TABLE& table : m_Tables )
448 libraries.push_back( tableToJson( table ) );
452 [&](
const nlohmann::json& aObj )
456 if( !aObj.is_array() )
459 for( const nlohmann::json& entry : aObj )
461 if( entry.empty() || !entry.is_object() )
464 m_Tables.emplace_back( tableFromJson( entry ) );
469 m_params.emplace_back(
new PARAM<int>(
"cache.max_size", &m_Cache.max_size, 256 ) );
471 m_params.emplace_back(
new PARAM<int>(
"cache.max_age", &m_Cache.max_age, 60 ) );
473 m_params.emplace_back(
new PARAM<bool>(
"globally_unique_keys", &m_GloballyUniqueKeys,
false ) );
475 registerMigration( 0, 1,
483 if( !Contains(
"libraries" ) || !At(
"libraries" ).is_array() )
486 for( nlohmann::json&
library : At(
"libraries" ) )
488 if( !
library.contains(
"fields" ) )
491 for(
const nlohmann::json& field :
library[
"fields"] )
493 if( !field.contains(
"name" ) || !field.contains(
"column" ) )
496 std::string
name = field[
"name"].get<std::string>();
497 std::string col = field[
"column"].get<std::string>();
499 if(
name ==
"ki_description" )
500 library[
"properties"][
"description"] = col;
501 else if(
name ==
"ki_fp_filters" )
502 library[
"properties"][
"footprint_filters"] = col;
520 nlohmann::json::json_pointer ptr( parent );
522 if( aInternals.contains( ptr ) && !aInternals[ptr].is_object() )
523 aInternals[ptr] = nlohmann::json::object();
546 if( !original.is_object() || original.empty() )
549 const std::vector<std::pair<std::string, bool>> unchanged = {
557 {
"source.connection_string",
m_Source.connection_string ==
m_loaded.source.connection_string },
564 std::set<std::string> editedParents;
566 for(
const auto& [
path, isUnchanged] : unchanged )
572 editedParents.insert( ptr.parent_pointer().to_string() );
576 if( original.contains( ptr ) )
578 else if(
Internals()->contains( ptr.parent_pointer() ) )
579 ( *
Internals() )[ptr.parent_pointer()].erase( ptr.back() );
585 nlohmann::json::json_pointer ptr( parent );
587 if( editedParents.contains( parent ) )
590 if( original.contains( ptr ) )
609 return m_Source.Expanded( aBaseDir.IsEmpty() ? wxFileName(
GetFilename() ).GetPath() : aBaseDir );
618 return path.GetFullPath();
630 auto settings = std::make_unique<DATABASE_LIB_SETTINGS>( std::string( aPath.ToUTF8() ) );
633 settings->SetReadOnly(
true );
634 bool loaded = settings->LoadFromFile();
638 wxFileName
path( settings->GetLoadPath() );
641 *aLoaded = !
path.FileExists() || ( loaded &&
path.IsFileReadable() );
649 wxString
name = aLibName;
650 name.Trim(
true ).Trim(
false );
654 if(
name.IsEmpty() ||
name.find_first_not_of( wxS(
"._" ) ) == wxString::npos )
655 return wxS(
"database_library" );
663 const std::string unchanged( aPath.ToUTF8() );
665 wxFileName file( aPath );
667 base.AssignDir( aBaseDir );
669 if( aPath.IsEmpty() || aBaseDir.IsEmpty() || !file.IsAbsolute() || !base.IsAbsolute() )
672 file.Normalize( wxPATH_NORM_DOTS );
673 base.Normalize( wxPATH_NORM_DOTS );
675 if( !file.GetVolume().IsSameAs( base.GetVolume(), wxFileName::IsCaseSensitive() ) )
679 const wxArrayString& baseDirs = base.GetDirs();
680 const wxArrayString& fileDirs = file.GetDirs();
682 if( file.GetFullName().IsEmpty() || fileDirs.GetCount() < baseDirs.GetCount() )
685 for(
size_t i = 0; i < baseDirs.GetCount(); ++i )
687 if( !fileDirs[i].IsSameAs( baseDirs[i], wxFileName::IsCaseSensitive() ) )
693 for(
size_t i = baseDirs.GetCount(); i < fileDirs.GetCount(); ++i )
694 relative << fileDirs[i] << wxS(
"/" );
696 relative << file.GetFullName();
698 return "${CWD}/" + std::string( relative.ToUTF8() );
704 if( aInput.IsEmpty() )
708 source.path = aInput.ToUTF8();
710 const wxString expanded = wxString::FromUTF8(
source.Expanded( aBaseDir ).path );
711 wxFileName file( expanded );
713 if( file.IsAbsolute() )
714 return file.GetFullPath();
717 const bool rooted = !expanded.IsEmpty() && wxFileName::GetPathSeparators().Find( expanded[0] ) != wxNOT_FOUND;
719 if( file.HasVolume() || rooted || aBaseDir.IsEmpty() )
722 file.Normalize( wxPATH_NORM_DOTS | wxPATH_NORM_ABSOLUTE, aBaseDir );
724 return file.GetFullPath();
730 return wxS(
"KiCad ODBC:" ) + wxString::FromUTF8( aSource.
dsn );
742 using NO_PROMPT_FN = bool ( * )(
const wxString&,
const wxString&, wxString& );
744 const NO_PROMPT_FN* target = aLookup.target<NO_PROMPT_FN>();
770 aPassword = secret.utf8_string();
788 wxString user = wxString::FromUTF8( aSource.
username );
790 if( aPassword.empty() )
bool m_GloballyUniqueKeys
static DATABASE_LIB_TABLE tableFromJson(const nlohmann::json &aEntry)
Missing or mistyped keys load as defaults so one incomplete entry cannot stop the others.
void ResolvePassword(const DATABASE_SECRET_LOOKUP &aLookup=nullptr)
Fills in the source password from the keyring when the file has none.
DATABASE_LIB_SETTINGS(const std::string &aFilename)
wxString getFileExt() const override
static DATABASE_FIELD_MAPPING fieldFromJson(const nlohmann::json &aEntry)
struct DATABASE_LIB_SETTINGS::LOADED_SCALARS m_loaded
DATABASE_SOURCE ExpandedSource(const wxString &aBaseDir=wxEmptyString) const
bool Store() override
Stores parameters, then restores the raw file value of every scalar the caller left unchanged so that...
void Load() override
Records the scalars as loaded; runs before a schema migration writes the file back.
std::string m_Description
static nlohmann::json tableToJson(const DATABASE_LIB_TABLE &aTable)
wxString GetLoadPath() const
DATABASE_CACHE_SETTINGS m_Cache
static nlohmann::json fieldToJson(const DATABASE_FIELD_MAPPING &aField)
static nlohmann::json::json_pointer PointerFromString(std::string aPath)
Builds a JSON pointer based on a given string.
const nlohmann::json & originalJson() const
wxString m_filename
The filename (not including path) of this settings file (inicode)
virtual void Load()
Updates the parameters of this object based on the current JSON document contents.
std::vector< PARAM_BASE * > m_params
The list of parameters (owned by this object)
JSON_SETTINGS_INTERNALS * Internals()
static ResultType fetchOrDefault(const nlohmann::json &aJson, const std::string &aKey, ResultType aDefault=ResultType())
Helper to retrieve a value from a JSON object (dictionary) as a certain result type.
JSON_SETTINGS(const wxString &aFilename, SETTINGS_LOC aLocation, int aSchemaVersion)
virtual bool Store()
Stores the current parameters into the JSON document represented by this object Note: this doesn't do...
wxString GetFilename() const
Like a normal param, but with custom getter and setter functions.
static std::map< std::string, std::string > parseConnectionString(const std::string &aConnectionString)
Splits a connection string into lower-case keys and values for display.
wxString DefaultDatabaseLibFileName(const wxString &aLibName)
static bool holdsAssignment(const std::string &aText)
A shown value holding '=' has swallowed another key's assignment, possibly a credential.
const int dblibSchemaVersion
bool IsNoPromptSecretLookup(const DATABASE_SECRET_LOOKUP &aLookup)
static constexpr const char * c_scalarParents[]
Objects that hold scalar settings rather than library tables.
static void normalizeScalarParents(JSON_SETTINGS_INTERNALS &aInternals)
Nested params cannot store into a non-object parent, so replace a malformed one before storing.
static void patchKey(nlohmann::json &aObj, const std::string &aKey, const T &aValue, const T &aParsed, bool aRequired)
Writes aValue to aObj[aKey] only if it differs from aParsed, the value the raw key was read as.
std::unique_ptr< DATABASE_LIB_SETTINGS > LoadDatabaseLibSettingsReadOnly(const wxString &aPath, bool *aLoaded)
Loads a database library settings file without allowing the load itself (e.g.
static wxString passwordService(const DATABASE_SOURCE &aSource)
std::string DatabasePathForStorage(const wxString &aPath, const wxString &aBaseDir)
void ResolveDatabasePassword(DATABASE_SOURCE &aSource, const DATABASE_SECRET_LOOKUP &aLookup)
Fills in the source password from the keyring, but only when the library file did not supply one.
static bool hasKeyringLogin(const DATABASE_SOURCE &aSource)
wxString DatabasePathFromInput(const wxString &aInput, const wxString &aBaseDir)
bool StoreDatabasePassword(const DATABASE_SOURCE &aSource, const std::string &aPassword)
Stores the password for the source's DSN login in the system keyring, or removes the entry when the p...
DATABASE_SECRET_LOOKUP DefaultDatabaseSecretLookup()
The lookup used when none is injected, which must never prompt to unlock the keyring.
bool GetDatabasePassword(const DATABASE_SOURCE &aSource, std::string &aPassword, const DATABASE_SECRET_LOOKUP &aLookup)
DSN passwords that are not saved in the (often shared) library file live in the system keyring,...
KICOMMON_API void ResolveDatabasePassword(DATABASE_SOURCE &aSource, const DATABASE_SECRET_LOOKUP &aLookup=nullptr)
Fills in the source password from the keyring, but only when the library file did not supply one.
std::function< bool(const wxString &aService, const wxString &aKey, wxString &aSecret)> DATABASE_SECRET_LOOKUP
Keyring lookup signature, matching KIPLATFORM::SECRETS::GetSecretNoPrompt.
constexpr int DEFAULT_DATABASE_TIMEOUT_SECONDS
The source timeout a library file gets when it sets none.
static const std::string DatabaseLibraryFileExtension
bool ReplaceIllegalFileNameChars(std::string &aName, int aReplaceChar)
Checks aName for illegal file name characters.
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.
void SetName(const std::string &aName)
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.
nlohmann::json source
Raw JSON as loaded; untouched keys are written back verbatim.
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)
nlohmann::json source
Raw JSON as loaded; untouched keys are written back verbatim.
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)
DATABASE_SOURCE Expanded(const wxString &aBaseDir) const
std::string Describe() const
std::string path
Database file of a SQLITE or ACCESS source.
std::string connection_string
DATABASE_SOURCE_TYPE type
std::string footprint_filters
std::string exclude_from_sim
std::string exclude_from_board
std::string exclude_from_bom
Definition of file extensions used in Kicad.