KiCad PCB EDA Suite
Loading...
Searching...
No Matches
DATABASE_CONNECTION Class Reference

#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_TYPEm_cache
 

Detailed Description

Definition at line 42 of file database_connection.h.

Member Typedef Documentation

◆ DB_CACHE_TYPE

typedef DATABASE_CACHE<std::map<std::string, ROW> > DATABASE_CONNECTION::DB_CACHE_TYPE
private

Definition at line 143 of file database_connection.h.

◆ ROW

typedef std::map<std::string, std::any> DATABASE_CONNECTION::ROW

Definition at line 47 of file database_connection.h.

Constructor & Destructor Documentation

◆ DATABASE_CONNECTION() [1/2]

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() [2/2]

DATABASE_CONNECTION::DATABASE_CONNECTION ( const std::string & aConnectionString,
int aTimeoutSeconds = DEFAULT_TIMEOUT,
bool aConnectNow = true )

References DEFAULT_TIMEOUT.

◆ ~DATABASE_CONNECTION()

DATABASE_CONNECTION::~DATABASE_CONNECTION ( )

Definition at line 115 of file database_connection.cpp.

References Disconnect(), and m_conn.

Member Function Documentation

◆ CacheTableInfo()

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.

Parameters
aTablethe name of a table in the database
aRequiredColumnscolumns that must be present in queries even if the driver does not report them
aOptionalColumnscolumns that are added only when the driver confirms they exist
Returns
true on success

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().

◆ ClearCache()

void DATABASE_CONNECTION::ClearCache ( const std::string & aTable)
inline

Definition at line 105 of file database_connection.h.

References m_cache.

◆ columnsFor()

std::string DATABASE_CONNECTION::columnsFor ( const std::string & aTable)
private

Definition at line 341 of file database_connection.cpp.

References empty(), m_columnCache, m_quoteChar, and traceDatabase.

Referenced by selectAllAndCache(), and SelectOne().

◆ Connect()

bool DATABASE_CONNECTION::Connect ( )

◆ Disconnect()

bool DATABASE_CONNECTION::Disconnect ( )

◆ GetLastError()

std::string DATABASE_CONNECTION::GetLastError ( ) const
inline

Definition at line 111 of file database_connection.h.

References m_lastError.

◆ getQuoteChar()

bool DATABASE_CONNECTION::getQuoteChar ( )
private

Definition at line 314 of file database_connection.cpp.

References m_conn, m_lastError, m_quoteChar, toUTF8(), and traceDatabase.

Referenced by Connect().

◆ init()

void DATABASE_CONNECTION::init ( )
private

Definition at line 122 of file database_connection.cpp.

References m_cache.

Referenced by DATABASE_CONNECTION().

◆ IsConnected()

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().

◆ SelectAll()

bool DATABASE_CONNECTION::SelectAll ( const std::string & aTable,
const std::string & aKey,
std::vector< ROW > & aResults )

Retrieves all rows from a database table.

Parameters
aTablethe name of a table in the database
aKeyholds the column name of the primary key used for caching results
aResultswill be filled with all rows in the table
Returns
true if the query succeeded and at least one ROW was found, false otherwise

Definition at line 730 of file database_connection.cpp.

References m_cache, m_conn, m_queryMutex, m_tables, selectAllAndCache(), and traceDatabase.

◆ selectAllAndCache()

bool DATABASE_CONNECTION::selectAllAndCache ( const std::string & aTable,
const std::string & aKey )
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().

◆ 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.

Parameters
aTablethe name of a table in the database
aWherecolumn to search, and the value to search for
aResultwill be filled with a row in the database if one was found
Returns
true if aResult was filled; false otherwise

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().

◆ SetCacheParams()

void DATABASE_CONNECTION::SetCacheParams ( int aMaxSize,
int aMaxAge )

Definition at line 128 of file database_connection.cpp.

References m_cache.

Member Data Documentation

◆ DEFAULT_TIMEOUT

◆ m_cache

std::unique_ptr<DB_CACHE_TYPE> DATABASE_CONNECTION::m_cache
private

◆ m_columnCache

std::map<std::string, std::map<std::string, int> > DATABASE_CONNECTION::m_columnCache
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().

◆ m_conn

std::unique_ptr<nanodbc::connection> DATABASE_CONNECTION::m_conn
private

◆ m_connectionString

std::string DATABASE_CONNECTION::m_connectionString
private

Definition at line 128 of file database_connection.h.

Referenced by Connect().

◆ m_dsn

std::string DATABASE_CONNECTION::m_dsn
private

Definition at line 125 of file database_connection.h.

Referenced by Connect(), and DATABASE_CONNECTION().

◆ m_lastError

std::string DATABASE_CONNECTION::m_lastError
private

◆ m_pass

std::string DATABASE_CONNECTION::m_pass
private

Definition at line 127 of file database_connection.h.

Referenced by Connect(), and DATABASE_CONNECTION().

◆ m_queryMutex

std::mutex DATABASE_CONNECTION::m_queryMutex
mutableprivate

Definition at line 141 of file database_connection.h.

Referenced by CacheTableInfo(), SelectAll(), and SelectOne().

◆ m_quoteChar

char DATABASE_CONNECTION::m_quoteChar
private

◆ m_tables

std::map<std::string, std::string> DATABASE_CONNECTION::m_tables
private

Definition at line 132 of file database_connection.h.

Referenced by CacheTableInfo(), Connect(), SelectAll(), and SelectOne().

◆ m_timeout

long DATABASE_CONNECTION::m_timeout
private

Definition at line 137 of file database_connection.h.

Referenced by Connect(), and DATABASE_CONNECTION().

◆ m_user

std::string DATABASE_CONNECTION::m_user
private

Definition at line 126 of file database_connection.h.

Referenced by Connect(), and DATABASE_CONNECTION().


The documentation for this class was generated from the following files: