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>
|
2023-03-02 14:04:37 +00:00
|
|
|
* Copyright (C) 2022-2023 KiCad Developers, see AUTHORS.txt for contributors.
|
2022-05-29 19:29:32 +00:00
|
|
|
*
|
|
|
|
* 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 <boost/locale.hpp>
|
|
|
|
#include <fmt/core.h>
|
|
|
|
#include <nanodbc/nanodbc.h>
|
2022-08-26 16:13:24 +00:00
|
|
|
|
|
|
|
// Some outdated definitions are used in sql.h
|
|
|
|
// We need to define them for "recent" dev tools
|
|
|
|
#define INT64 int64_t
|
|
|
|
#define UINT64 uint64_t
|
|
|
|
|
|
|
|
#ifdef __MINGW32__
|
|
|
|
#define BYTE uint8_t
|
|
|
|
#define WORD uint16_t
|
|
|
|
#define DWORD uint32_t
|
|
|
|
#define HWND uint32_t /* dummy define */
|
|
|
|
#endif
|
|
|
|
|
2022-08-26 16:51:14 +00:00
|
|
|
#ifdef WIN32
|
|
|
|
#include <windows.h> // for sql.h
|
|
|
|
#endif
|
|
|
|
|
2022-05-29 19:29:32 +00:00
|
|
|
#include <sql.h> // SQL_IDENTIFIER_QUOTE_CHAR
|
2022-08-26 16:13:24 +00:00
|
|
|
|
2022-05-29 19:29:32 +00:00
|
|
|
#include <wx/log.h>
|
|
|
|
|
|
|
|
#include <database/database_connection.h>
|
2022-08-31 02:18:21 +00:00
|
|
|
#include <database/database_cache.h>
|
2023-09-07 11:22:10 +00:00
|
|
|
#include <core/profile.h>
|
2022-05-29 19:29:32 +00:00
|
|
|
|
|
|
|
|
|
|
|
const char* const traceDatabase = "KICAD_DATABASE";
|
|
|
|
|
|
|
|
/**
|
|
|
|
* When Unicode support is enabled in nanodbc, string formats are used matching the appropriate
|
|
|
|
* character set of the platform. KiCad uses UTF-8 encoded strings internally, but different
|
|
|
|
* platforms use different encodings for SQL strings. Unicode mode must be enabled for compilation
|
|
|
|
* on Windows, since Visual Studio forces the use of Unicode SQL headers if any part of the project
|
|
|
|
* has Unicode enabled.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Converts a string from KiCad-native to nanodbc-native
|
|
|
|
* @param aString is a UTF-8 encoded string
|
|
|
|
* @return a string in nanodbc's platform-specific representation
|
|
|
|
*/
|
|
|
|
nanodbc::string fromUTF8( const std::string& aString )
|
|
|
|
{
|
|
|
|
return boost::locale::conv::utf_to_utf<nanodbc::string::value_type>( aString );
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Converts a string from nanodbc-native to KiCad-native
|
|
|
|
* @param aString is a string encoded in nanodbc's platform-specific way
|
|
|
|
* @return a string with UTF-8 encoding
|
|
|
|
*/
|
|
|
|
std::string toUTF8( const nanodbc::string& aString )
|
|
|
|
{
|
|
|
|
return boost::locale::conv::utf_to_utf<char>( aString );
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
DATABASE_CONNECTION::DATABASE_CONNECTION( const std::string& aDataSourceName,
|
|
|
|
const std::string& aUsername,
|
|
|
|
const std::string& aPassword, int aTimeoutSeconds,
|
|
|
|
bool aConnectNow ) :
|
|
|
|
m_quoteChar( '"' )
|
|
|
|
{
|
|
|
|
m_dsn = aDataSourceName;
|
|
|
|
m_user = aUsername;
|
|
|
|
m_pass = aPassword;
|
|
|
|
m_timeout = aTimeoutSeconds;
|
|
|
|
|
2023-06-22 02:47:41 +00:00
|
|
|
init();
|
2022-08-31 02:18:21 +00:00
|
|
|
|
2022-05-29 19:29:32 +00:00
|
|
|
if( aConnectNow )
|
|
|
|
Connect();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
DATABASE_CONNECTION::DATABASE_CONNECTION( const std::string& aConnectionString,
|
|
|
|
int aTimeoutSeconds, bool aConnectNow ) :
|
|
|
|
m_quoteChar( '"' )
|
|
|
|
{
|
|
|
|
m_connectionString = aConnectionString;
|
|
|
|
m_timeout = aTimeoutSeconds;
|
|
|
|
|
2023-06-22 02:47:41 +00:00
|
|
|
init();
|
2022-08-31 02:18:21 +00:00
|
|
|
|
2022-05-29 19:29:32 +00:00
|
|
|
if( aConnectNow )
|
|
|
|
Connect();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
DATABASE_CONNECTION::~DATABASE_CONNECTION()
|
|
|
|
{
|
|
|
|
Disconnect();
|
|
|
|
m_conn.reset();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2023-06-22 02:47:41 +00:00
|
|
|
void DATABASE_CONNECTION::init()
|
|
|
|
{
|
|
|
|
m_cache = std::make_unique<DB_CACHE_TYPE>( 10, 1 );
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2022-08-31 02:18:21 +00:00
|
|
|
void DATABASE_CONNECTION::SetCacheParams( int aMaxSize, int aMaxAge )
|
|
|
|
{
|
|
|
|
if( !m_cache )
|
|
|
|
return;
|
|
|
|
|
|
|
|
if( aMaxSize < 0 )
|
|
|
|
aMaxSize = 0;
|
|
|
|
|
|
|
|
if( aMaxAge < 0 )
|
|
|
|
aMaxAge = 0;
|
|
|
|
|
|
|
|
m_cache->SetMaxSize( static_cast<size_t>( aMaxSize ) );
|
|
|
|
m_cache->SetMaxAge( static_cast<time_t>( aMaxAge ) );
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2022-05-29 19:29:32 +00:00
|
|
|
bool DATABASE_CONNECTION::Connect()
|
|
|
|
{
|
|
|
|
nanodbc::string dsn = fromUTF8( m_dsn );
|
|
|
|
nanodbc::string user = fromUTF8( m_user );
|
|
|
|
nanodbc::string pass = fromUTF8( m_pass );
|
|
|
|
nanodbc::string cs = fromUTF8( m_connectionString );
|
|
|
|
|
|
|
|
try
|
|
|
|
{
|
|
|
|
if( cs.empty() )
|
|
|
|
{
|
|
|
|
wxLogTrace( traceDatabase, wxT( "Creating connection to DSN %s" ), m_dsn );
|
|
|
|
m_conn = std::make_unique<nanodbc::connection>( dsn, user, pass, m_timeout );
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
wxLogTrace( traceDatabase, wxT( "Creating connection with connection string" ) );
|
|
|
|
m_conn = std::make_unique<nanodbc::connection>( cs, m_timeout );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
catch( nanodbc::database_error& e )
|
|
|
|
{
|
|
|
|
m_lastError = e.what();
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2023-06-22 02:47:41 +00:00
|
|
|
m_tables.clear();
|
|
|
|
|
2022-05-29 19:29:32 +00:00
|
|
|
if( IsConnected() )
|
|
|
|
getQuoteChar();
|
|
|
|
|
|
|
|
return IsConnected();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool DATABASE_CONNECTION::Disconnect()
|
|
|
|
{
|
|
|
|
if( !m_conn )
|
|
|
|
{
|
|
|
|
wxLogTrace( traceDatabase, wxT( "Note: Disconnect() called without valid connection" ) );
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2023-03-02 14:04:37 +00:00
|
|
|
try
|
|
|
|
{
|
|
|
|
m_conn->disconnect();
|
|
|
|
}
|
|
|
|
catch( boost::locale::conv::conversion_error& exc )
|
|
|
|
{
|
|
|
|
wxLogTrace( traceDatabase, wxT( "Disconnect() error \"%s\" occured." ), exc.what() );
|
|
|
|
return false;
|
|
|
|
}
|
2022-05-29 19:29:32 +00:00
|
|
|
|
|
|
|
return !m_conn->connected();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool DATABASE_CONNECTION::IsConnected() const
|
|
|
|
{
|
|
|
|
if( !m_conn )
|
|
|
|
return false;
|
|
|
|
|
|
|
|
return m_conn->connected();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2023-06-22 02:47:41 +00:00
|
|
|
bool DATABASE_CONNECTION::CacheTableInfo( const std::string& aTable,
|
|
|
|
const std::set<std::string>& aColumns )
|
2022-05-29 19:29:32 +00:00
|
|
|
{
|
|
|
|
if( !m_conn )
|
|
|
|
return false;
|
|
|
|
|
|
|
|
try
|
|
|
|
{
|
|
|
|
nanodbc::catalog catalog( *m_conn );
|
2023-06-22 02:47:41 +00:00
|
|
|
nanodbc::catalog::tables tables = catalog.find_tables( fromUTF8( aTable ) );
|
2022-05-29 19:29:32 +00:00
|
|
|
|
2023-08-15 20:45:10 +00:00
|
|
|
if( !tables.next() )
|
|
|
|
{
|
|
|
|
wxLogTrace( traceDatabase, wxT( "CacheTableInfo: table '%s' not found in catalog" ),
|
|
|
|
aTable );
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2023-06-22 02:47:41 +00:00
|
|
|
std::string key = toUTF8( tables.table_name() );
|
|
|
|
m_tables[key] = toUTF8( tables.table_type() );
|
2022-05-29 19:29:32 +00:00
|
|
|
|
|
|
|
try
|
|
|
|
{
|
|
|
|
nanodbc::catalog::columns columns =
|
2023-06-22 02:47:41 +00:00
|
|
|
catalog.find_columns( NANODBC_TEXT( "" ), tables.table_name() );
|
2022-05-29 19:29:32 +00:00
|
|
|
|
|
|
|
while( columns.next() )
|
|
|
|
{
|
|
|
|
std::string columnKey = toUTF8( columns.column_name() );
|
2023-06-22 02:47:41 +00:00
|
|
|
|
|
|
|
if( aColumns.count( columnKey ) )
|
|
|
|
m_columnCache[key][columnKey] = columns.data_type();
|
2022-05-29 19:29:32 +00:00
|
|
|
}
|
2022-08-26 16:13:24 +00:00
|
|
|
|
2022-05-29 19:29:32 +00:00
|
|
|
}
|
|
|
|
catch( nanodbc::database_error& e )
|
|
|
|
{
|
|
|
|
m_lastError = e.what();
|
2023-08-15 20:45:10 +00:00
|
|
|
wxLogTrace( traceDatabase, wxT( "Exception while syncing columns for table '%s': %s" ),
|
2023-06-22 02:47:41 +00:00
|
|
|
key, m_lastError );
|
2022-05-29 19:29:32 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
2023-06-22 02:47:41 +00:00
|
|
|
catch( nanodbc::database_error& e )
|
|
|
|
{
|
|
|
|
m_lastError = e.what();
|
|
|
|
wxLogTrace( traceDatabase, wxT( "Exception while caching table info: %s" ), m_lastError );
|
|
|
|
return false;
|
|
|
|
}
|
2022-05-29 19:29:32 +00:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool DATABASE_CONNECTION::getQuoteChar()
|
|
|
|
{
|
|
|
|
if( !m_conn )
|
|
|
|
return false;
|
|
|
|
|
|
|
|
try
|
|
|
|
{
|
|
|
|
nanodbc::string qc = m_conn->get_info<nanodbc::string>( SQL_IDENTIFIER_QUOTE_CHAR );
|
|
|
|
|
|
|
|
if( qc.empty() )
|
|
|
|
return false;
|
|
|
|
|
|
|
|
m_quoteChar = *toUTF8( qc ).begin();
|
|
|
|
|
|
|
|
wxLogTrace( traceDatabase, wxT( "Quote char retrieved: %c" ), m_quoteChar );
|
|
|
|
}
|
2022-09-18 12:42:42 +00:00
|
|
|
catch( nanodbc::database_error& )
|
2022-05-29 19:29:32 +00:00
|
|
|
{
|
|
|
|
wxLogTrace( traceDatabase, wxT( "Exception while querying quote char: %s" ), m_lastError );
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2023-06-22 02:47:41 +00:00
|
|
|
std::string DATABASE_CONNECTION::columnsFor( const std::string& aTable )
|
|
|
|
{
|
|
|
|
if( !m_columnCache.count( aTable ) )
|
|
|
|
{
|
|
|
|
wxLogTrace( traceDatabase, wxT( "columnsFor: requested table %s missing from cache!" ),
|
|
|
|
aTable );
|
|
|
|
return "*";
|
|
|
|
}
|
|
|
|
|
|
|
|
if( m_columnCache[aTable].empty() )
|
|
|
|
{
|
|
|
|
wxLogTrace( traceDatabase, wxT( "columnsFor: requested table %s has no columns mapped!" ),
|
|
|
|
aTable );
|
|
|
|
return "*";
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string ret;
|
|
|
|
|
|
|
|
for( const auto& [ columnName, columnType ] : m_columnCache[aTable] )
|
|
|
|
ret += fmt::format( "{}{}{}, ", m_quoteChar, columnName, m_quoteChar );
|
|
|
|
|
|
|
|
// strip tailing ', '
|
|
|
|
ret.resize( ret.length() - 2 );
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
//next step, make SelectOne take from the SelectAll cache if the SelectOne cache is missing.
|
|
|
|
//To do this, need to build a map of PK->ROW for the cache result.
|
2022-05-29 19:29:32 +00:00
|
|
|
bool DATABASE_CONNECTION::SelectOne( const std::string& aTable,
|
|
|
|
const std::pair<std::string, std::string>& aWhere,
|
|
|
|
DATABASE_CONNECTION::ROW& aResult )
|
|
|
|
{
|
|
|
|
if( !m_conn )
|
|
|
|
{
|
|
|
|
wxLogTrace( traceDatabase, wxT( "Called SelectOne without valid connection!" ) );
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
auto tableMapIter = m_tables.find( aTable );
|
|
|
|
|
|
|
|
if( tableMapIter == m_tables.end() )
|
|
|
|
{
|
|
|
|
wxLogTrace( traceDatabase, wxT( "SelectOne: requested table %s not found in cache" ),
|
|
|
|
aTable );
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
const std::string& tableName = tableMapIter->first;
|
2023-06-22 02:47:41 +00:00
|
|
|
DB_CACHE_TYPE::CACHE_VALUE cacheEntry;
|
|
|
|
|
|
|
|
if( m_cache->Get( tableName, cacheEntry ) )
|
|
|
|
{
|
|
|
|
if( cacheEntry.count( aWhere.second ) )
|
|
|
|
{
|
|
|
|
wxLogTrace( traceDatabase, wxT( "SelectOne: `%s` with parameter `%s` - cache hit" ),
|
|
|
|
tableName, aWhere.second );
|
|
|
|
aResult = cacheEntry.at( aWhere.second );
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
2024-05-03 21:03:40 +00:00
|
|
|
else
|
|
|
|
{
|
|
|
|
wxLogTrace( traceDatabase, wxT( "SelectOne: table `%s` not in row cache; will SelectAll" ),
|
|
|
|
tableName, aWhere.second );
|
|
|
|
|
|
|
|
selectAllAndCache( tableName, aWhere.first );
|
|
|
|
|
|
|
|
if( m_cache->Get( tableName, cacheEntry ) )
|
|
|
|
{
|
|
|
|
if( cacheEntry.count( aWhere.second ) )
|
|
|
|
{
|
|
|
|
wxLogTrace( traceDatabase, wxT( "SelectOne: `%s` with parameter `%s` - cache hit" ),
|
|
|
|
tableName, aWhere.second );
|
|
|
|
aResult = cacheEntry.at( aWhere.second );
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-05-29 19:29:32 +00:00
|
|
|
|
|
|
|
if( !m_columnCache.count( tableName ) )
|
|
|
|
{
|
|
|
|
wxLogTrace( traceDatabase, wxT( "SelectOne: requested table %s missing from column cache" ),
|
|
|
|
tableName );
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
auto columnCacheIter = m_columnCache.at( tableName ).find( aWhere.first );
|
|
|
|
|
|
|
|
if( columnCacheIter == m_columnCache.at( tableName ).end() )
|
|
|
|
{
|
|
|
|
wxLogTrace( traceDatabase, wxT( "SelectOne: requested column %s not found in cache for %s" ),
|
|
|
|
aWhere.first, tableName );
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
const std::string& columnName = columnCacheIter->first;
|
|
|
|
|
2022-09-02 03:11:09 +00:00
|
|
|
std::string cacheKey = fmt::format( "{}{}{}", tableName, columnName, aWhere.second );
|
|
|
|
|
2023-06-22 02:47:41 +00:00
|
|
|
std::string queryStr = fmt::format( "SELECT {} FROM {}{}{} WHERE {}{}{} = ?",
|
|
|
|
columnsFor( tableName ),
|
2022-08-31 02:18:21 +00:00
|
|
|
m_quoteChar, tableName, m_quoteChar,
|
|
|
|
m_quoteChar, columnName, m_quoteChar );
|
|
|
|
nanodbc::string query = fromUTF8( queryStr );
|
2022-05-29 19:29:32 +00:00
|
|
|
|
2023-06-22 02:47:41 +00:00
|
|
|
PROF_TIMER timer;
|
2024-05-06 20:59:49 +00:00
|
|
|
nanodbc::statement statement;
|
2022-05-29 19:29:32 +00:00
|
|
|
|
|
|
|
try
|
|
|
|
{
|
2024-05-06 20:59:49 +00:00
|
|
|
statement.prepare( *m_conn, query );
|
2022-05-29 19:29:32 +00:00
|
|
|
statement.bind( 0, aWhere.second.c_str() );
|
|
|
|
}
|
|
|
|
catch( nanodbc::database_error& e )
|
|
|
|
{
|
|
|
|
m_lastError = e.what();
|
|
|
|
wxLogTrace( traceDatabase, wxT( "Exception while preparing statement for SelectOne: %s" ),
|
|
|
|
m_lastError );
|
2022-12-22 17:14:16 +00:00
|
|
|
|
|
|
|
// Exception may be due to a connection error; nanodbc won't auto-reconnect
|
|
|
|
m_conn->disconnect();
|
|
|
|
|
2022-05-29 19:29:32 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
wxLogTrace( traceDatabase, wxT( "SelectOne: `%s` with parameter `%s`" ), toUTF8( query ),
|
|
|
|
aWhere.second );
|
|
|
|
|
|
|
|
nanodbc::result results;
|
|
|
|
|
|
|
|
try
|
|
|
|
{
|
|
|
|
results = nanodbc::execute( statement );
|
|
|
|
}
|
|
|
|
catch( nanodbc::database_error& e )
|
|
|
|
{
|
|
|
|
m_lastError = e.what();
|
|
|
|
wxLogTrace( traceDatabase, wxT( "Exception while executing statement for SelectOne: %s" ),
|
|
|
|
m_lastError );
|
2022-12-22 17:14:16 +00:00
|
|
|
|
|
|
|
// Exception may be due to a connection error; nanodbc won't auto-reconnect
|
|
|
|
m_conn->disconnect();
|
|
|
|
|
2022-05-29 19:29:32 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2023-06-22 02:47:41 +00:00
|
|
|
timer.Stop();
|
|
|
|
|
|
|
|
|
2022-05-29 19:29:32 +00:00
|
|
|
if( !results.first() )
|
|
|
|
{
|
|
|
|
wxLogTrace( traceDatabase, wxT( "SelectOne: no results returned from query" ) );
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2023-06-22 02:47:41 +00:00
|
|
|
wxLogTrace( traceDatabase, wxT( "SelectOne: %ld results returned from query in %0.1f ms" ),
|
|
|
|
results.rows(), timer.msecs() );
|
2022-05-29 19:29:32 +00:00
|
|
|
|
|
|
|
aResult.clear();
|
|
|
|
|
|
|
|
try
|
|
|
|
{
|
|
|
|
for( short i = 0; i < results.columns(); ++i )
|
|
|
|
{
|
|
|
|
std::string column = toUTF8( results.column_name( i ) );
|
2023-09-17 02:37:12 +00:00
|
|
|
|
|
|
|
switch( results.column_datatype( i ) )
|
|
|
|
{
|
|
|
|
case SQL_DOUBLE:
|
|
|
|
case SQL_FLOAT:
|
|
|
|
case SQL_REAL:
|
|
|
|
case SQL_DECIMAL:
|
|
|
|
case SQL_NUMERIC:
|
|
|
|
{
|
2023-11-15 17:27:25 +00:00
|
|
|
try
|
|
|
|
{
|
|
|
|
aResult[column] = fmt::format( "{:G}", results.get<double>( i ) );
|
|
|
|
}
|
2024-02-19 20:45:24 +00:00
|
|
|
catch( nanodbc::null_access_error& )
|
2023-11-15 17:27:25 +00:00
|
|
|
{
|
|
|
|
// Column was empty (null)
|
|
|
|
aResult[column] = std::string();
|
|
|
|
}
|
|
|
|
|
2023-09-17 02:37:12 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
default:
|
|
|
|
aResult[column] = toUTF8( results.get<nanodbc::string>( i, NANODBC_TEXT( "" ) ) );
|
|
|
|
}
|
2022-05-29 19:29:32 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
catch( nanodbc::database_error& e )
|
|
|
|
{
|
|
|
|
m_lastError = e.what();
|
|
|
|
wxLogTrace( traceDatabase, wxT( "Exception while parsing results from SelectOne: %s" ),
|
|
|
|
m_lastError );
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2024-05-03 21:03:40 +00:00
|
|
|
bool DATABASE_CONNECTION::selectAllAndCache( const std::string& aTable, const std::string& aKey )
|
2022-05-29 19:29:32 +00:00
|
|
|
{
|
|
|
|
nanodbc::statement statement( *m_conn );
|
|
|
|
|
2023-06-22 02:47:41 +00:00
|
|
|
nanodbc::string query = fromUTF8( fmt::format( "SELECT {} FROM {}{}{}", columnsFor( aTable ),
|
|
|
|
m_quoteChar, aTable, m_quoteChar ) );
|
2022-05-29 19:29:32 +00:00
|
|
|
|
2024-05-03 21:03:40 +00:00
|
|
|
wxLogTrace( traceDatabase, wxT( "selectAllAndCache: `%s`" ), toUTF8( query ) );
|
2022-05-29 19:29:32 +00:00
|
|
|
|
2023-06-22 02:47:41 +00:00
|
|
|
PROF_TIMER timer;
|
|
|
|
|
2022-05-29 19:29:32 +00:00
|
|
|
try
|
|
|
|
{
|
|
|
|
statement.prepare( query );
|
|
|
|
}
|
|
|
|
catch( nanodbc::database_error& e )
|
|
|
|
{
|
|
|
|
m_lastError = e.what();
|
2024-05-03 21:03:40 +00:00
|
|
|
wxLogTrace( traceDatabase,
|
|
|
|
wxT( "Exception while preparing query for selectAllAndCache: %s" ),
|
2022-05-29 19:29:32 +00:00
|
|
|
m_lastError );
|
2022-12-22 17:14:16 +00:00
|
|
|
|
|
|
|
// Exception may be due to a connection error; nanodbc won't auto-reconnect
|
|
|
|
m_conn->disconnect();
|
|
|
|
|
2022-05-29 19:29:32 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
nanodbc::result results;
|
|
|
|
|
|
|
|
try
|
|
|
|
{
|
|
|
|
results = nanodbc::execute( statement );
|
|
|
|
}
|
|
|
|
catch( nanodbc::database_error& e )
|
|
|
|
{
|
|
|
|
m_lastError = e.what();
|
2024-05-03 21:03:40 +00:00
|
|
|
wxLogTrace( traceDatabase,
|
|
|
|
wxT( "Exception while executing query for selectAllAndCache: %s" ),
|
2022-05-29 19:29:32 +00:00
|
|
|
m_lastError );
|
2022-12-22 17:14:16 +00:00
|
|
|
|
|
|
|
// Exception may be due to a connection error; nanodbc won't auto-reconnect
|
|
|
|
m_conn->disconnect();
|
|
|
|
|
2022-05-29 19:29:32 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2023-06-22 02:47:41 +00:00
|
|
|
timer.Stop();
|
|
|
|
|
2024-05-03 21:03:40 +00:00
|
|
|
DB_CACHE_TYPE::CACHE_VALUE cacheEntry;
|
|
|
|
|
2023-11-15 17:27:25 +00:00
|
|
|
auto handleException =
|
|
|
|
[&]( std::runtime_error& aException, const std::string& aExtraContext = "" )
|
2024-05-03 21:03:40 +00:00
|
|
|
{
|
|
|
|
m_lastError = aException.what();
|
|
|
|
std::string extra = aExtraContext.empty() ? "" : ": " + aExtraContext;
|
|
|
|
wxLogTrace( traceDatabase,
|
|
|
|
wxT( "Exception while parsing result %d from selectAllAndCache: %s%s" ),
|
|
|
|
cacheEntry.size(), m_lastError, extra );
|
|
|
|
};
|
2023-11-15 17:27:25 +00:00
|
|
|
|
|
|
|
while( results.next() )
|
2022-05-29 19:29:32 +00:00
|
|
|
{
|
2023-11-15 17:27:25 +00:00
|
|
|
short columnCount = 0;
|
|
|
|
ROW result;
|
|
|
|
|
|
|
|
try
|
|
|
|
{
|
|
|
|
columnCount = results.columns();
|
|
|
|
}
|
|
|
|
catch( nanodbc::database_error& e )
|
|
|
|
{
|
|
|
|
handleException( e );
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
for( short j = 0; j < columnCount; ++j )
|
2022-05-29 19:29:32 +00:00
|
|
|
{
|
2023-11-15 17:27:25 +00:00
|
|
|
std::string column;
|
|
|
|
std::string columnExtraDbgInfo;
|
|
|
|
int datatype = SQL_UNKNOWN_TYPE;
|
2022-05-29 19:29:32 +00:00
|
|
|
|
2023-11-15 17:27:25 +00:00
|
|
|
try
|
|
|
|
{
|
|
|
|
column = toUTF8( results.column_name( j ) );
|
|
|
|
datatype = results.column_datatype( j );
|
|
|
|
columnExtraDbgInfo = fmt::format( "column index {}, name '{}', type {}", j, column,
|
|
|
|
datatype );
|
|
|
|
}
|
|
|
|
catch( nanodbc::index_range_error& e )
|
2022-05-29 19:29:32 +00:00
|
|
|
{
|
2023-11-15 17:27:25 +00:00
|
|
|
handleException( e, columnExtraDbgInfo );
|
|
|
|
return false;
|
|
|
|
}
|
2023-09-17 02:37:12 +00:00
|
|
|
|
2023-11-15 17:27:25 +00:00
|
|
|
switch( datatype )
|
|
|
|
{
|
|
|
|
case SQL_DOUBLE:
|
|
|
|
case SQL_FLOAT:
|
|
|
|
case SQL_REAL:
|
|
|
|
case SQL_DECIMAL:
|
|
|
|
case SQL_NUMERIC:
|
|
|
|
{
|
|
|
|
try
|
2023-09-17 02:37:12 +00:00
|
|
|
{
|
|
|
|
result[column] = fmt::format( "{:G}", results.get<double>( j ) );
|
|
|
|
}
|
2024-02-19 20:45:24 +00:00
|
|
|
catch( nanodbc::null_access_error& )
|
2023-11-15 17:27:25 +00:00
|
|
|
{
|
|
|
|
// Column was empty (null)
|
|
|
|
result[column] = std::string();
|
|
|
|
}
|
|
|
|
catch( std::runtime_error& e )
|
|
|
|
{
|
|
|
|
handleException( e, columnExtraDbgInfo );
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
2023-09-17 02:37:12 +00:00
|
|
|
|
2023-11-15 17:27:25 +00:00
|
|
|
default:
|
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
2023-09-17 02:37:12 +00:00
|
|
|
result[column] = toUTF8( results.get<nanodbc::string>( j,
|
|
|
|
NANODBC_TEXT( "" ) ) );
|
|
|
|
}
|
2023-11-15 17:27:25 +00:00
|
|
|
catch( std::runtime_error& e )
|
|
|
|
{
|
|
|
|
handleException( e, columnExtraDbgInfo );
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
2022-05-29 19:29:32 +00:00
|
|
|
}
|
2022-09-23 23:28:04 +00:00
|
|
|
}
|
2023-11-15 17:27:25 +00:00
|
|
|
|
2024-05-03 21:03:40 +00:00
|
|
|
wxASSERT( result.count( aKey ) );
|
|
|
|
std::string keyStr = std::any_cast<std::string>( result.at( aKey ) );
|
|
|
|
cacheEntry[keyStr] = result;
|
2022-05-29 19:29:32 +00:00
|
|
|
}
|
|
|
|
|
2024-05-03 21:03:40 +00:00
|
|
|
wxLogTrace( traceDatabase, wxT( "selectAllAndCache from %s completed in %0.1f ms" ), aTable,
|
2023-06-22 02:47:41 +00:00
|
|
|
timer.msecs() );
|
|
|
|
|
2024-05-03 21:03:40 +00:00
|
|
|
m_cache->Put( aTable, cacheEntry );
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool DATABASE_CONNECTION::SelectAll( const std::string& aTable, const std::string& aKey,
|
|
|
|
std::vector<ROW>& aResults )
|
|
|
|
{
|
|
|
|
if( !m_conn )
|
2023-06-22 02:47:41 +00:00
|
|
|
{
|
2024-05-03 21:03:40 +00:00
|
|
|
wxLogTrace( traceDatabase, wxT( "Called SelectAll without valid connection!" ) );
|
|
|
|
return false;
|
2023-06-22 02:47:41 +00:00
|
|
|
}
|
|
|
|
|
2024-05-03 21:03:40 +00:00
|
|
|
auto tableMapIter = m_tables.find( aTable );
|
|
|
|
|
|
|
|
if( tableMapIter == m_tables.end() )
|
|
|
|
{
|
|
|
|
wxLogTrace( traceDatabase, wxT( "SelectAll: requested table %s not found in cache" ),
|
|
|
|
aTable );
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
DB_CACHE_TYPE::CACHE_VALUE cacheEntry;
|
|
|
|
|
|
|
|
if( !m_cache->Get( aTable, cacheEntry ) )
|
|
|
|
{
|
|
|
|
if( !selectAllAndCache( aTable, aKey ) )
|
|
|
|
{
|
|
|
|
wxLogTrace( traceDatabase, wxT( "SelectAll: `%s` cache fill failed" ), aTable );
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Now it should be filled
|
|
|
|
m_cache->Get( aTable, cacheEntry );
|
|
|
|
}
|
|
|
|
|
|
|
|
if( !m_cache->Get( aTable, cacheEntry ) )
|
|
|
|
{
|
|
|
|
wxLogTrace( traceDatabase, wxT( "SelectAll: `%s` failed to get results from cache!" ),
|
|
|
|
aTable );
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
wxLogTrace( traceDatabase, wxT( "SelectAll: `%s` - returning cached results" ), aTable );
|
|
|
|
|
|
|
|
aResults.reserve( cacheEntry.size() );
|
|
|
|
|
|
|
|
for( auto &[ key, row ] : cacheEntry )
|
|
|
|
aResults.emplace_back( row );
|
2023-06-22 02:47:41 +00:00
|
|
|
|
2022-05-29 19:29:32 +00:00
|
|
|
return true;
|
|
|
|
}
|