KiCad PCB EDA Suite
Loading...
Searching...
No Matches
database_connection.cpp
Go to the documentation of this file.
1/*
2 * This program source code file is part of KiCad, a free EDA CAD application.
3 *
4 * Copyright (C) 2022 Jon Evans <[email protected]>
5 * Copyright (C) 2022-2023 KiCad Developers, see AUTHORS.txt for contributors.
6 *
7 * This program is free software: you can redistribute it and/or modify it
8 * under the terms of the GNU General Public License as published by the
9 * Free Software Foundation, either version 3 of the License, or (at your
10 * option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License along
18 * with this program. If not, see <http://www.gnu.org/licenses/>.
19 */
20
21#include <boost/locale.hpp>
22#include <fmt/core.h>
23#include <nanodbc/nanodbc.h>
24
25// Some outdated definitions are used in sql.h
26// We need to define them for "recent" dev tools
27#define INT64 int64_t
28#define UINT64 uint64_t
29
30#ifdef __MINGW32__
31#define BYTE uint8_t
32#define WORD uint16_t
33#define DWORD uint32_t
34#define HWND uint32_t /* dummy define */
35#endif
36
37#ifdef WIN32
38#include <windows.h> // for sql.h
39#endif
40
41#include <sql.h> // SQL_IDENTIFIER_QUOTE_CHAR
42
43#include <wx/log.h>
44
47#include <core/profile.h>
48
49
50const char* const traceDatabase = "KICAD_DATABASE";
51
65nanodbc::string fromUTF8( const std::string& aString )
66{
67 return boost::locale::conv::utf_to_utf<nanodbc::string::value_type>( aString );
68}
69
70
76std::string toUTF8( const nanodbc::string& aString )
77{
78 return boost::locale::conv::utf_to_utf<char>( aString );
79}
80
81
82DATABASE_CONNECTION::DATABASE_CONNECTION( const std::string& aDataSourceName,
83 const std::string& aUsername,
84 const std::string& aPassword, int aTimeoutSeconds,
85 bool aConnectNow ) :
86 m_quoteChar( '"' )
87{
88 m_dsn = aDataSourceName;
89 m_user = aUsername;
90 m_pass = aPassword;
91 m_timeout = aTimeoutSeconds;
92
93 init();
94
95 if( aConnectNow )
96 Connect();
97}
98
99
100DATABASE_CONNECTION::DATABASE_CONNECTION( const std::string& aConnectionString,
101 int aTimeoutSeconds, bool aConnectNow ) :
102 m_quoteChar( '"' )
103{
104 m_connectionString = aConnectionString;
105 m_timeout = aTimeoutSeconds;
106
107 init();
108
109 if( aConnectNow )
110 Connect();
111}
112
113
115{
116 Disconnect();
117 m_conn.reset();
118}
119
120
122{
123 m_cache = std::make_unique<DB_CACHE_TYPE>( 10, 1 );
124}
125
126
127void DATABASE_CONNECTION::SetCacheParams( int aMaxSize, int aMaxAge )
128{
129 if( !m_cache )
130 return;
131
132 if( aMaxSize < 0 )
133 aMaxSize = 0;
134
135 if( aMaxAge < 0 )
136 aMaxAge = 0;
137
138 m_cache->SetMaxSize( static_cast<size_t>( aMaxSize ) );
139 m_cache->SetMaxAge( static_cast<time_t>( aMaxAge ) );
140}
141
142
144{
145 nanodbc::string dsn = fromUTF8( m_dsn );
146 nanodbc::string user = fromUTF8( m_user );
147 nanodbc::string pass = fromUTF8( m_pass );
148 nanodbc::string cs = fromUTF8( m_connectionString );
149
150 try
151 {
152 if( cs.empty() )
153 {
154 wxLogTrace( traceDatabase, wxT( "Creating connection to DSN %s" ), m_dsn );
155 m_conn = std::make_unique<nanodbc::connection>( dsn, user, pass, m_timeout );
156 }
157 else
158 {
159 wxLogTrace( traceDatabase, wxT( "Creating connection with connection string" ) );
160 m_conn = std::make_unique<nanodbc::connection>( cs, m_timeout );
161 }
162 }
163 catch( nanodbc::database_error& e )
164 {
165 m_lastError = e.what();
166 return false;
167 }
168
169 m_tables.clear();
170
171 if( IsConnected() )
172 getQuoteChar();
173
174 return IsConnected();
175}
176
177
179{
180 if( !m_conn )
181 {
182 wxLogTrace( traceDatabase, wxT( "Note: Disconnect() called without valid connection" ) );
183 return false;
184 }
185
186 try
187 {
188 m_conn->disconnect();
189 }
190 catch( boost::locale::conv::conversion_error& exc )
191 {
192 wxLogTrace( traceDatabase, wxT( "Disconnect() error \"%s\" occured." ), exc.what() );
193 return false;
194 }
195
196 return !m_conn->connected();
197}
198
199
201{
202 if( !m_conn )
203 return false;
204
205 return m_conn->connected();
206}
207
208
209bool DATABASE_CONNECTION::CacheTableInfo( const std::string& aTable,
210 const std::set<std::string>& aColumns )
211{
212 if( !m_conn )
213 return false;
214
215 try
216 {
217 nanodbc::catalog catalog( *m_conn );
218 nanodbc::catalog::tables tables = catalog.find_tables( fromUTF8( aTable ) );
219
220 if( !tables.next() )
221 {
222 wxLogTrace( traceDatabase, wxT( "CacheTableInfo: table '%s' not found in catalog" ),
223 aTable );
224 return false;
225 }
226
227 std::string key = toUTF8( tables.table_name() );
228 m_tables[key] = toUTF8( tables.table_type() );
229
230 try
231 {
232 nanodbc::catalog::columns columns =
233 catalog.find_columns( NANODBC_TEXT( "" ), tables.table_name() );
234
235 while( columns.next() )
236 {
237 std::string columnKey = toUTF8( columns.column_name() );
238
239 if( aColumns.count( columnKey ) )
240 m_columnCache[key][columnKey] = columns.data_type();
241 }
242
243 }
244 catch( nanodbc::database_error& e )
245 {
246 m_lastError = e.what();
247 wxLogTrace( traceDatabase, wxT( "Exception while syncing columns for table '%s': %s" ),
248 key, m_lastError );
249 return false;
250 }
251 }
252 catch( nanodbc::database_error& e )
253 {
254 m_lastError = e.what();
255 wxLogTrace( traceDatabase, wxT( "Exception while caching table info: %s" ), m_lastError );
256 return false;
257 }
258
259 return true;
260}
261
262
264{
265 if( !m_conn )
266 return false;
267
268 try
269 {
270 nanodbc::string qc = m_conn->get_info<nanodbc::string>( SQL_IDENTIFIER_QUOTE_CHAR );
271
272 if( qc.empty() )
273 return false;
274
275 m_quoteChar = *toUTF8( qc ).begin();
276
277 wxLogTrace( traceDatabase, wxT( "Quote char retrieved: %c" ), m_quoteChar );
278 }
279 catch( nanodbc::database_error& )
280 {
281 wxLogTrace( traceDatabase, wxT( "Exception while querying quote char: %s" ), m_lastError );
282 return false;
283 }
284
285 return true;
286}
287
288
289std::string DATABASE_CONNECTION::columnsFor( const std::string& aTable )
290{
291 if( !m_columnCache.count( aTable ) )
292 {
293 wxLogTrace( traceDatabase, wxT( "columnsFor: requested table %s missing from cache!" ),
294 aTable );
295 return "*";
296 }
297
298 if( m_columnCache[aTable].empty() )
299 {
300 wxLogTrace( traceDatabase, wxT( "columnsFor: requested table %s has no columns mapped!" ),
301 aTable );
302 return "*";
303 }
304
305 std::string ret;
306
307 for( const auto& [ columnName, columnType ] : m_columnCache[aTable] )
308 ret += fmt::format( "{}{}{}, ", m_quoteChar, columnName, m_quoteChar );
309
310 // strip tailing ', '
311 ret.resize( ret.length() - 2 );
312
313 return ret;
314}
315
316//next step, make SelectOne take from the SelectAll cache if the SelectOne cache is missing.
317//To do this, need to build a map of PK->ROW for the cache result.
318bool DATABASE_CONNECTION::SelectOne( const std::string& aTable,
319 const std::pair<std::string, std::string>& aWhere,
320 DATABASE_CONNECTION::ROW& aResult )
321{
322 if( !m_conn )
323 {
324 wxLogTrace( traceDatabase, wxT( "Called SelectOne without valid connection!" ) );
325 return false;
326 }
327
328 auto tableMapIter = m_tables.find( aTable );
329
330 if( tableMapIter == m_tables.end() )
331 {
332 wxLogTrace( traceDatabase, wxT( "SelectOne: requested table %s not found in cache" ),
333 aTable );
334 return false;
335 }
336
337 const std::string& tableName = tableMapIter->first;
339
340 if( m_cache->Get( tableName, cacheEntry ) )
341 {
342 if( cacheEntry.count( aWhere.second ) )
343 {
344 wxLogTrace( traceDatabase, wxT( "SelectOne: `%s` with parameter `%s` - cache hit" ),
345 tableName, aWhere.second );
346 aResult = cacheEntry.at( aWhere.second );
347 return true;
348 }
349 }
350
351 if( !m_columnCache.count( tableName ) )
352 {
353 wxLogTrace( traceDatabase, wxT( "SelectOne: requested table %s missing from column cache" ),
354 tableName );
355 return false;
356 }
357
358 auto columnCacheIter = m_columnCache.at( tableName ).find( aWhere.first );
359
360 if( columnCacheIter == m_columnCache.at( tableName ).end() )
361 {
362 wxLogTrace( traceDatabase, wxT( "SelectOne: requested column %s not found in cache for %s" ),
363 aWhere.first, tableName );
364 return false;
365 }
366
367 const std::string& columnName = columnCacheIter->first;
368
369 std::string cacheKey = fmt::format( "{}{}{}", tableName, columnName, aWhere.second );
370
371 std::string queryStr = fmt::format( "SELECT {} FROM {}{}{} WHERE {}{}{} = ?",
372 columnsFor( tableName ),
373 m_quoteChar, tableName, m_quoteChar,
374 m_quoteChar, columnName, m_quoteChar );
375
376 nanodbc::statement statement( *m_conn );
377 nanodbc::string query = fromUTF8( queryStr );
378
379 PROF_TIMER timer;
380
381 try
382 {
383 statement.prepare( query );
384 statement.bind( 0, aWhere.second.c_str() );
385 }
386 catch( nanodbc::database_error& e )
387 {
388 m_lastError = e.what();
389 wxLogTrace( traceDatabase, wxT( "Exception while preparing statement for SelectOne: %s" ),
390 m_lastError );
391
392 // Exception may be due to a connection error; nanodbc won't auto-reconnect
393 m_conn->disconnect();
394
395 return false;
396 }
397
398 wxLogTrace( traceDatabase, wxT( "SelectOne: `%s` with parameter `%s`" ), toUTF8( query ),
399 aWhere.second );
400
401 nanodbc::result results;
402
403 try
404 {
405 results = nanodbc::execute( statement );
406 }
407 catch( nanodbc::database_error& e )
408 {
409 m_lastError = e.what();
410 wxLogTrace( traceDatabase, wxT( "Exception while executing statement for SelectOne: %s" ),
411 m_lastError );
412
413 // Exception may be due to a connection error; nanodbc won't auto-reconnect
414 m_conn->disconnect();
415
416 return false;
417 }
418
419 timer.Stop();
420
421
422 if( !results.first() )
423 {
424 wxLogTrace( traceDatabase, wxT( "SelectOne: no results returned from query" ) );
425 return false;
426 }
427
428 wxLogTrace( traceDatabase, wxT( "SelectOne: %ld results returned from query in %0.1f ms" ),
429 results.rows(), timer.msecs() );
430
431 aResult.clear();
432
433 try
434 {
435 for( short i = 0; i < results.columns(); ++i )
436 {
437 std::string column = toUTF8( results.column_name( i ) );
438
439 switch( results.column_datatype( i ) )
440 {
441 case SQL_DOUBLE:
442 case SQL_FLOAT:
443 case SQL_REAL:
444 case SQL_DECIMAL:
445 case SQL_NUMERIC:
446 {
447 try
448 {
449 aResult[column] = fmt::format( "{:G}", results.get<double>( i ) );
450 }
451 catch( nanodbc::null_access_error& )
452 {
453 // Column was empty (null)
454 aResult[column] = std::string();
455 }
456
457 break;
458 }
459
460 default:
461 aResult[column] = toUTF8( results.get<nanodbc::string>( i, NANODBC_TEXT( "" ) ) );
462 }
463 }
464 }
465 catch( nanodbc::database_error& e )
466 {
467 m_lastError = e.what();
468 wxLogTrace( traceDatabase, wxT( "Exception while parsing results from SelectOne: %s" ),
469 m_lastError );
470 return false;
471 }
472
473 return true;
474}
475
476
477bool DATABASE_CONNECTION::SelectAll( const std::string& aTable, const std::string& aKey,
478 std::vector<ROW>& aResults )
479{
480 if( !m_conn )
481 {
482 wxLogTrace( traceDatabase, wxT( "Called SelectAll without valid connection!" ) );
483 return false;
484 }
485
486 auto tableMapIter = m_tables.find( aTable );
487
488 if( tableMapIter == m_tables.end() )
489 {
490 wxLogTrace( traceDatabase, wxT( "SelectAll: requested table %s not found in cache" ),
491 aTable );
492 return false;
493 }
494
496
497 if( m_cache->Get( aTable, cacheEntry ) )
498 {
499 wxLogTrace( traceDatabase, wxT( "SelectAll: `%s` - cache hit" ), aTable );
500
501 aResults.reserve( cacheEntry.size() );
502
503 for( auto &[ key, row ] : cacheEntry )
504 aResults.emplace_back( row );
505
506 return true;
507 }
508
509 nanodbc::statement statement( *m_conn );
510
511 nanodbc::string query = fromUTF8( fmt::format( "SELECT {} FROM {}{}{}", columnsFor( aTable ),
512 m_quoteChar, aTable, m_quoteChar ) );
513
514 wxLogTrace( traceDatabase, wxT( "SelectAll: `%s`" ), toUTF8( query ) );
515
516 PROF_TIMER timer;
517
518 try
519 {
520 statement.prepare( query );
521 }
522 catch( nanodbc::database_error& e )
523 {
524 m_lastError = e.what();
525 wxLogTrace( traceDatabase, wxT( "Exception while preparing query for SelectAll: %s" ),
526 m_lastError );
527
528 // Exception may be due to a connection error; nanodbc won't auto-reconnect
529 m_conn->disconnect();
530
531 return false;
532 }
533
534 nanodbc::result results;
535
536 try
537 {
538 results = nanodbc::execute( statement );
539 }
540 catch( nanodbc::database_error& e )
541 {
542 m_lastError = e.what();
543 wxLogTrace( traceDatabase, wxT( "Exception while executing query for SelectAll: %s" ),
544 m_lastError );
545
546 // Exception may be due to a connection error; nanodbc won't auto-reconnect
547 m_conn->disconnect();
548
549 return false;
550 }
551
552 timer.Stop();
553
554 auto handleException =
555 [&]( std::runtime_error& aException, const std::string& aExtraContext = "" )
556 {
557 m_lastError = aException.what();
558 std::string extra = aExtraContext.empty() ? "" : ": " + aExtraContext;
559 wxLogTrace( traceDatabase,
560 wxT( "Exception while parsing result %d from SelectAll: %s%s" ),
561 aResults.size(), m_lastError, extra );
562 };
563
564 while( results.next() )
565 {
566 short columnCount = 0;
567 ROW result;
568
569 try
570 {
571 columnCount = results.columns();
572 }
573 catch( nanodbc::database_error& e )
574 {
575 handleException( e );
576 return false;
577 }
578
579 for( short j = 0; j < columnCount; ++j )
580 {
581 std::string column;
582 std::string columnExtraDbgInfo;
583 int datatype = SQL_UNKNOWN_TYPE;
584
585 try
586 {
587 column = toUTF8( results.column_name( j ) );
588 datatype = results.column_datatype( j );
589 columnExtraDbgInfo = fmt::format( "column index {}, name '{}', type {}", j, column,
590 datatype );
591 }
592 catch( nanodbc::index_range_error& e )
593 {
594 handleException( e, columnExtraDbgInfo );
595 return false;
596 }
597
598 switch( datatype )
599 {
600 case SQL_DOUBLE:
601 case SQL_FLOAT:
602 case SQL_REAL:
603 case SQL_DECIMAL:
604 case SQL_NUMERIC:
605 {
606 try
607 {
608 result[column] = fmt::format( "{:G}", results.get<double>( j ) );
609 }
610 catch( nanodbc::null_access_error& )
611 {
612 // Column was empty (null)
613 result[column] = std::string();
614 }
615 catch( std::runtime_error& e )
616 {
617 handleException( e, columnExtraDbgInfo );
618 return false;
619 }
620 break;
621 }
622
623 default:
624 {
625 try
626 {
627 result[column] = toUTF8( results.get<nanodbc::string>( j,
628 NANODBC_TEXT( "" ) ) );
629 }
630 catch( std::runtime_error& e )
631 {
632 handleException( e, columnExtraDbgInfo );
633 return false;
634 }
635 }
636 }
637 }
638
639 aResults.emplace_back( std::move( result ) );
640 }
641
642 wxLogTrace( traceDatabase, wxT( "SelectAll from %s completed in %0.1f ms" ), aTable,
643 timer.msecs() );
644
645 for( const ROW& row : aResults )
646 {
647 wxASSERT( row.count( aKey ) );
648 std::string keyStr = std::any_cast<std::string>( row.at( aKey ) );
649 cacheEntry[keyStr] = row;
650 }
651
652 m_cache->Put( aTable, cacheEntry );
653
654 return true;
655}
CacheValueType CACHE_VALUE
bool CacheTableInfo(const std::string &aTable, const std::set< std::string > &aColumns)
std::map< std::string, std::any > ROW
std::map< std::string, std::map< std::string, int > > m_columnCache
Map of table -> map of column name -> data type.
void SetCacheParams(int aMaxSize, int aMaxAge)
bool SelectAll(const std::string &aTable, const std::string &aKey, std::vector< ROW > &aResults)
Retrieves all rows from a database table.
DATABASE_CONNECTION(const std::string &aDataSourceName, const std::string &aUsername, const std::string &aPassword, int aTimeoutSeconds=DEFAULT_TIMEOUT, bool aConnectNow=true)
std::unique_ptr< DB_CACHE_TYPE > m_cache
std::string columnsFor(const std::string &aTable)
std::unique_ptr< nanodbc::connection > m_conn
bool SelectOne(const std::string &aTable, const std::pair< std::string, std::string > &aWhere, ROW &aResult)
Retrieves a single row from a database table.
std::map< std::string, std::string > m_tables
A small class to help profiling.
Definition: profile.h:49
void Stop()
Save the time when this function was called, and set the counter stane to stop.
Definition: profile.h:88
double msecs(bool aSinceLast=false)
Definition: profile.h:149
const char *const traceDatabase
std::string toUTF8(const nanodbc::string &aString)
Converts a string from nanodbc-native to KiCad-native.
nanodbc::string fromUTF8(const std::string &aString)
When Unicode support is enabled in nanodbc, string formats are used matching the appropriate characte...
const char *const traceDatabase
static bool empty(const wxTextEntryBase *aCtrl)