23#include <boost/algorithm/string.hpp>
35 wxASSERT_MSG(
m_backend,
"DATABASE_CONNECTION needs a backend" );
70 m_cache->SetMaxSize(
static_cast<size_t>( aMaxSize ) );
71 m_cache->SetMaxAge(
static_cast<time_t
>( aMaxAge ) );
106 const std::set<std::string>& aOptionalColumns )
113 std::optional<DB_TABLE_INFO>
info =
m_backend->DescribeTable( aTable );
116 std::set<std::string> requiredLower;
117 std::set<std::string> optionalLower;
119 for(
const std::string& col : aRequiredColumns )
120 requiredLower.insert( boost::to_lower_copy( col ) );
122 for(
const std::string& col : aOptionalColumns )
123 optionalLower.insert( boost::to_lower_copy( col ) );
127 wxLogTrace(
traceDatabase, wxT(
"CacheTableInfo: table '%s' not found in catalog" ), aTable );
133 failed.
columns.insert( requiredLower.begin(), requiredLower.end() );
134 failed.
columns.insert( optionalLower.begin(), optionalLower.end() );
142 const bool covered = std::all_of( failed->second.columns.begin(), failed->second.columns.end(),
143 [&](
const std::string& aColumn )
145 return requiredLower.count( aColumn ) || optionalLower.count( aColumn );
148 if( covered && !
info->columnsUnavailable )
156 if(
info->columnsUnavailable )
158 wxLogTrace(
traceDatabase, wxT(
"CacheTableInfo: columns of table '%s' unavailable" ),
info->name );
162 std::set<std::string> columnsReported;
166 const std::string lower = boost::to_lower_copy( column.
name );
168 if( !requiredLower.count( lower ) && !optionalLower.count( lower ) )
171 columnsReported.insert( lower );
175 wxLogTrace(
traceDatabase, wxT(
"CacheTableInfo: column '%s' of table '%s' cannot be read; dropped" ),
185 for(
const std::string& requestedCol : aRequiredColumns )
187 const std::string requestedLower = boost::to_lower_copy( requestedCol );
189 if( !columnsReported.count( requestedLower ) && !requestedCol.empty() )
192 wxT(
"CacheTableInfo: column '%s' not found in catalog for table '%s', adding anyway" ),
193 requestedCol,
info->name );
195 table.columns.insert( requestedCol );
201 for(
const std::string& requestedCol : aOptionalColumns )
203 if( !columnsReported.count( boost::to_lower_copy( requestedCol ) ) && !requestedCol.empty() )
205 wxLogWarning( wxT(
"Database table '%s' has no column '%s'; ignoring the "
206 "misconfigured properties mapping." ),
207 info->name, requestedCol );
217 std::vector<std::string> names;
219 for(
const std::string& spelling : aTable.
columns )
220 names.push_back( spelling );
223 wxLogTrace(
traceDatabase, wxT(
"columnNames: table %s has no columns mapped!" ), aTable.
info.
name );
230 const std::string& aKey )
232 for(
size_t i = 0; i < aLabels.size(); ++i )
234 if( aLabels[i] == aKey )
238 for(
size_t i = 0; i < aLabels.size(); ++i )
240 if( boost::iequals( aLabels[i], aKey ) )
249 std::vector<DB_VALUE>&& aValues )
253 for(
size_t i = 0; i < aLabels.size() && i < aValues.size(); ++i )
255 if( std::string*
text = std::get_if<std::string>( &aValues[i] ) )
256 row[aLabels[i]] = std::move( *
text );
272 std::vector<std::string> labels;
273 std::optional<size_t> keyIndex;
274 bool keyResolved =
false;
278 [&]( std::vector<DB_VALUE>&& aValues )
287 wxLogTrace(
traceDatabase, wxT(
"readAndCache: warning: key %s not found in result set" ),
292 if( !keyIndex || *keyIndex >= aValues.size() )
296 built[std::move( key )] =
makeRow( labels, std::move( aValues ) );
313 wxLogTrace(
traceDatabase, wxT(
"readAndCache from %s completed in %0.1f ms" ), aTable, timer.
msecs() );
317 aEntry = std::move( built );
330 wxLogTrace(
traceDatabase, wxT(
"Called SelectOne without valid connection!" ) );
334 auto tableIter =
m_tables.find( aTable );
338 wxLogTrace(
traceDatabase, wxT(
"SelectOne: requested table %s not found in cache" ), aTable );
342 const std::string& tableName = tableIter->first;
346 bool cached =
m_cache->Get( tableName, cacheEntry );
349 wxLogTrace(
traceDatabase, wxT(
"SelectOne: table `%s` not in row cache; will SelectAll" ), tableName );
351 if( cached ||
readAndCache( tableName, aWhere.first, cacheEntry ) )
353 if(
auto hit = cacheEntry.find( aWhere.second ); hit != cacheEntry.end() )
355 wxLogTrace(
traceDatabase, wxT(
"SelectOne: `%s` with parameter `%s` - cache hit" ), tableName,
357 aResult = hit->second;
362 auto keyColumn =
table.columns.find( aWhere.first );
364 if( keyColumn ==
table.columns.end() )
366 keyColumn = std::find_if(
table.columns.begin(),
table.columns.end(),
367 [&](
const std::string& aColumn )
369 return boost::iequals( aColumn, aWhere.first );
373 if( keyColumn ==
table.columns.end() )
375 wxLogTrace(
traceDatabase, wxT(
"SelectOne: requested column %s not found in cache for %s" ), aWhere.first,
380 std::vector<std::string> labels;
381 std::vector<DB_VALUE> values;
384 aWhere.second, labels, values );
391 aResult =
makeRow( labels, std::move( values ) );
400std::optional<DATABASE_CONNECTION::TABLE_FAILURE>
406 return failed->second;
432 wxLogTrace(
traceDatabase, wxT(
"Called SelectAll without valid connection!" ) );
439 wxLogTrace(
traceDatabase, wxT(
"SelectAll: table %s could not be described" ), aTable );
449 wxLogTrace(
traceDatabase, wxT(
"SelectAll: requested table %s not found in cache" ), aTable );
459 if( !
m_cache->Get( aTable, cacheEntry ) )
463 wxLogTrace(
traceDatabase, wxT(
"SelectAll: `%s` cache fill failed" ), aTable );
469 wxLogTrace(
traceDatabase, wxT(
"SelectAll: `%s` - returning cached results" ), aTable );
472 aResults.reserve( cacheEntry.size() );
474 for(
auto& [key, row] : cacheEntry )
475 aResults.emplace_back( std::move( row ) );
482 std::vector<std::string>& aColumns, std::vector<ROW>& aRows )
493 auto tableIter =
m_tables.find( aTable );
497 [&]( std::vector<DB_VALUE>&& aValues )
499 aRows.emplace_back(
makeRow( aColumns, std::move( aValues ) ) );
502 if( !
m_backend->ReadTable(
info, {}, aMaxRows, aColumns, sink ) )
535 return m_backend->ListColumns( aTable, aColumns );
std::map< std::string, ROW > CACHE_VALUE
DATABASE_CONNECTION & m_connection
LOAD_CYCLE(DATABASE_CONNECTION &aConnection, DB_STOP_CHECK aStopped)
bool SelectAll(const std::string &aTable, const std::string &aKey, std::vector< ROW > &aResults, QUERY_STATUS *aStatus=nullptr)
Retrieves all rows from a database table.
DATABASE_CONNECTION(std::unique_ptr< DATABASE_BACKEND > aBackend, bool aConnectNow=true)
bool GetColumns(const std::string &aTable, std::vector< std::string > &aColumns)
Lists the columns of a table in catalog order.
std::map< std::string, std::any > ROW
std::unique_ptr< DATABASE_BACKEND > m_backend
static ROW makeRow(const std::vector< std::string > &aLabels, std::vector< DB_VALUE > &&aValues)
Converts one row delivered by the backend to text keyed by result-column label.
std::map< std::string, TABLE_STATE > m_tables
void SetCacheParams(int aMaxSize, int aMaxAge)
std::unique_ptr< DB_CACHE_TYPE > m_cache
std::map< std::string, TABLE_FAILURE > m_tableFailures
Why CacheTableInfo could not describe each table, so a later query on it reports that table's own fai...
static std::vector< std::string > columnNames(const TABLE_STATE &aTable)
bool GetTables(std::vector< std::string > &aTables)
Lists the tables and views visible through this connection, sorted by name.
bool SelectSample(const std::string &aTable, size_t aMaxRows, std::vector< std::string > &aColumns, std::vector< ROW > &aRows)
Reads the first rows of a table, bypassing the row cache.
DATABASE_CACHE< std::map< std::string, ROW > > DB_CACHE_TYPE
bool SelectOne(const std::string &aTable, const std::pair< std::string, std::string > &aWhere, ROW &aResult)
Retrieves a single row from a database table.
static std::optional< size_t > findLabel(const std::vector< std::string > &aLabels, const std::string &aKey)
bool readAndCache(const std::string &aTable, const std::string &aKey, DB_CACHE_TYPE::CACHE_VALUE &aEntry)
Reads a whole table into aEntry and offers it to the row cache.
std::optional< TABLE_FAILURE > GetTableFailure(const std::string &aTable) const
bool CacheTableInfo(const std::string &aTable, const std::set< std::string > &aRequiredColumns, const std::set< std::string > &aOptionalColumns={})
Caches schema information for a table so that subsequent queries know which columns exist.
A small class to help profiling.
void Stop()
Save the time when this function was called, and set the counter stane to stop.
double msecs(bool aSinceLast=false)
std::function< bool()> DB_STOP_CHECK
True once the caller no longer wants what a read would deliver.
std::string DatabaseValueToString(const DB_VALUE &aValue)
Convert a database value to the text KiCad stores for it.
const wxChar *const traceDatabase
Flag to enable database library tracing.
What a failed query left behind, copied before another thread's query can replace it.
Why CacheTableInfo could not describe a table.
std::set< std::string > columns
Lowercase columns the failed lookups asked for; only a lookup asking for all of them clears the failu...
A table the backend described, with the columns queries against it read.
std::set< std::string > columns
The column spellings queries read. Spellings differing only in case are distinct columns.
A column as the backend reports it.
bool supported
False for columns the backend can never read, such as attachments.
std::string name
The table's name as the backend spells it.