|
KiCad PCB EDA Suite
|
#include <database_connection.h>
Public Types | |
| typedef std::map< std::string, std::any > | ROW |
Public Member Functions | |
| DATABASE_CONNECTION (const std::string &aDataSourceName, const std::string &aUsername, const std::string &aPassword, int aTimeoutSeconds=DEFAULT_TIMEOUT, bool aConnectNow=true) | |
| DATABASE_CONNECTION (const std::string &aConnectionString, int aTimeoutSeconds=DEFAULT_TIMEOUT, bool aConnectNow=true) | |
| ~DATABASE_CONNECTION () | |
| void | SetCacheParams (int aMaxSize, int aMaxAge) |
| bool | Connect () |
| bool | Disconnect () |
| bool | IsConnected () 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. | |
| bool | SelectOne (const std::string &aTable, const std::pair< std::string, std::string > &aWhere, ROW &aResult) |
| Retrieves a single row from a database table. | |
| bool | SelectAll (const std::string &aTable, const std::string &aKey, std::vector< ROW > &aResults) |
| Retrieves all rows from a database table. | |
| void | ClearCache (const std::string &aTable) |
| std::string | GetLastError () const |
Static Public Attributes | |
| static const long | DEFAULT_TIMEOUT = 10 |
Private Types | |
| typedef DATABASE_CACHE< std::map< std::string, ROW > > | DB_CACHE_TYPE |
Private Member Functions | |
| void | init () |
| bool | getQuoteChar () |
| std::string | columnsFor (const std::string &aTable) |
| bool | selectAllAndCache (const std::string &aTable, const std::string &aKey) |
| The caller must already hold m_queryMutex. | |
Private Attributes | |
| std::unique_ptr< nanodbc::connection > | m_conn |
| std::string | m_dsn |
| std::string | m_user |
| std::string | m_pass |
| std::string | m_connectionString |
| std::string | m_lastError |
| std::map< std::string, std::string > | m_tables |
| std::map< std::string, std::map< std::string, int > > | m_columnCache |
| Map of table -> map of column name -> data type. | |
| long | m_timeout |
| char | m_quoteChar |
| std::mutex | m_queryMutex |
| std::unique_ptr< DB_CACHE_TYPE > | m_cache |
Definition at line 42 of file database_connection.h.
|
private |
Definition at line 143 of file database_connection.h.
| typedef std::map<std::string, std::any> DATABASE_CONNECTION::ROW |
Definition at line 47 of file database_connection.h.
| DATABASE_CONNECTION::DATABASE_CONNECTION | ( | const std::string & | aDataSourceName, |
| const std::string & | aUsername, | ||
| const std::string & | aPassword, | ||
| int | aTimeoutSeconds = DEFAULT_TIMEOUT, | ||
| bool | aConnectNow = true ) |
Definition at line 83 of file database_connection.cpp.
References Connect(), init(), m_dsn, m_pass, m_quoteChar, m_timeout, and m_user.
| DATABASE_CONNECTION::DATABASE_CONNECTION | ( | const std::string & | aConnectionString, |
| int | aTimeoutSeconds = DEFAULT_TIMEOUT, | ||
| bool | aConnectNow = true ) |
References DEFAULT_TIMEOUT.
| DATABASE_CONNECTION::~DATABASE_CONNECTION | ( | ) |
Definition at line 115 of file database_connection.cpp.
References Disconnect(), and m_conn.
| bool DATABASE_CONNECTION::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.
Required columns are trusted even if the driver doesn't report them (e.g. SQLite generated columns). Optional columns are added only if confirmed present; otherwise they are dropped with a warning rather than breaking every query against the table. Matching is case-insensitive.
| aTable | the name of a table in the database |
| aRequiredColumns | columns that must be present in queries even if the driver does not report them |
| aOptionalColumns | columns that are added only when the driver confirms they exist |
Definition at line 210 of file database_connection.cpp.
References fromUTF8(), m_columnCache, m_conn, m_lastError, m_queryMutex, m_tables, toUTF8(), and traceDatabase.
Referenced by BOOST_AUTO_TEST_CASE(), and BOOST_AUTO_TEST_CASE().
|
inline |
Definition at line 105 of file database_connection.h.
References m_cache.
|
private |
Definition at line 341 of file database_connection.cpp.
References empty(), m_columnCache, m_quoteChar, and traceDatabase.
Referenced by selectAllAndCache(), and SelectOne().
| bool DATABASE_CONNECTION::Connect | ( | ) |
Definition at line 144 of file database_connection.cpp.
References fromUTF8(), getQuoteChar(), IsConnected(), m_conn, m_connectionString, m_dsn, m_lastError, m_pass, m_tables, m_timeout, m_user, and traceDatabase.
Referenced by BOOST_AUTO_TEST_CASE(), and DATABASE_CONNECTION().
| bool DATABASE_CONNECTION::Disconnect | ( | ) |
Definition at line 179 of file database_connection.cpp.
References m_conn, and traceDatabase.
Referenced by BOOST_AUTO_TEST_CASE(), selectAllAndCache(), SelectOne(), and ~DATABASE_CONNECTION().
|
inline |
Definition at line 111 of file database_connection.h.
References m_lastError.
|
private |
Definition at line 314 of file database_connection.cpp.
References m_conn, m_lastError, m_quoteChar, toUTF8(), and traceDatabase.
Referenced by Connect().
|
private |
Definition at line 122 of file database_connection.cpp.
References m_cache.
Referenced by DATABASE_CONNECTION().
| bool DATABASE_CONNECTION::IsConnected | ( | ) | const |
Definition at line 201 of file database_connection.cpp.
References m_conn.
Referenced by BOOST_AUTO_TEST_CASE(), BOOST_AUTO_TEST_CASE(), and Connect().
| bool DATABASE_CONNECTION::SelectAll | ( | const std::string & | aTable, |
| const std::string & | aKey, | ||
| std::vector< ROW > & | aResults ) |
Retrieves all rows from a database table.
| aTable | the name of a table in the database |
| aKey | holds the column name of the primary key used for caching results |
| aResults | will be filled with all rows in the table |
Definition at line 730 of file database_connection.cpp.
References m_cache, m_conn, m_queryMutex, m_tables, selectAllAndCache(), and traceDatabase.
|
private |
The caller must already hold m_queryMutex.
Definition at line 565 of file database_connection.cpp.
References columnsFor(), Disconnect(), fromUTF8(), m_cache, m_conn, m_lastError, m_quoteChar, PROF_TIMER::msecs(), result, PROF_TIMER::Stop(), toUTF8(), and traceDatabase.
Referenced by SelectAll(), and SelectOne().
| bool DATABASE_CONNECTION::SelectOne | ( | const std::string & | aTable, |
| const std::pair< std::string, std::string > & | aWhere, | ||
| DATABASE_CONNECTION::ROW & | aResult ) |
Retrieves a single row from a database table.
Table and column names are cached when the connection is created, so schema changes to the database won't be recognized unless the connection is recreated.
| aTable | the name of a table in the database |
| aWhere | column to search, and the value to search for |
| aResult | will be filled with a row in the database if one was found |
Definition at line 370 of file database_connection.cpp.
References columnsFor(), Disconnect(), fromUTF8(), m_cache, m_columnCache, m_conn, m_lastError, m_queryMutex, m_quoteChar, m_tables, PROF_TIMER::msecs(), selectAllAndCache(), PROF_TIMER::Stop(), toUTF8(), and traceDatabase.
Referenced by BOOST_AUTO_TEST_CASE(), and BOOST_AUTO_TEST_CASE().
| void DATABASE_CONNECTION::SetCacheParams | ( | int | aMaxSize, |
| int | aMaxAge ) |
Definition at line 128 of file database_connection.cpp.
References m_cache.
|
static |
Definition at line 45 of file database_connection.h.
Referenced by DATABASE_CONNECTION(), DIALOG_GENERATE_DATABASE_CONNECTION::DIALOG_GENERATE_DATABASE_CONNECTION(), DIALOG_GENERATE_DATABASE_CONNECTION::GetSource(), and DIALOG_GENERATE_DATABASE_CONNECTION::OnTest().
|
private |
Definition at line 145 of file database_connection.h.
Referenced by ClearCache(), init(), SelectAll(), selectAllAndCache(), SelectOne(), and SetCacheParams().
|
private |
Map of table -> map of column name -> data type.
Definition at line 135 of file database_connection.h.
Referenced by CacheTableInfo(), columnsFor(), and SelectOne().
|
private |
Definition at line 123 of file database_connection.h.
Referenced by CacheTableInfo(), Connect(), Disconnect(), getQuoteChar(), IsConnected(), SelectAll(), selectAllAndCache(), SelectOne(), and ~DATABASE_CONNECTION().
|
private |
Definition at line 128 of file database_connection.h.
Referenced by Connect().
|
private |
Definition at line 125 of file database_connection.h.
Referenced by Connect(), and DATABASE_CONNECTION().
|
private |
Definition at line 130 of file database_connection.h.
Referenced by CacheTableInfo(), Connect(), GetLastError(), getQuoteChar(), selectAllAndCache(), and SelectOne().
|
private |
Definition at line 127 of file database_connection.h.
Referenced by Connect(), and DATABASE_CONNECTION().
|
mutableprivate |
Definition at line 141 of file database_connection.h.
Referenced by CacheTableInfo(), SelectAll(), and SelectOne().
|
private |
Definition at line 139 of file database_connection.h.
Referenced by columnsFor(), DATABASE_CONNECTION(), getQuoteChar(), selectAllAndCache(), and SelectOne().
|
private |
Definition at line 132 of file database_connection.h.
Referenced by CacheTableInfo(), Connect(), SelectAll(), and SelectOne().
|
private |
Definition at line 137 of file database_connection.h.
Referenced by Connect(), and DATABASE_CONNECTION().
|
private |
Definition at line 126 of file database_connection.h.
Referenced by Connect(), and DATABASE_CONNECTION().