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 > &aColumns)
 
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.
 
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)
 

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::unique_ptr< DB_CACHE_TYPEm_cache
 

Detailed Description

Definition at line 41 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 119 of file database_connection.h.

◆ ROW

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

Definition at line 46 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 82 of file database_connection.cpp.

References Connect(), init(), m_dsn, m_pass, m_timeout, and m_user.

◆ DATABASE_CONNECTION() [2/2]

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

◆ ~DATABASE_CONNECTION()

DATABASE_CONNECTION::~DATABASE_CONNECTION ( )

Definition at line 114 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 > &  aColumns 
)

◆ columnsFor()

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

Definition at line 289 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 ( )

Definition at line 178 of file database_connection.cpp.

References m_conn, and traceDatabase.

Referenced by BOOST_AUTO_TEST_CASE(), and ~DATABASE_CONNECTION().

◆ GetLastError()

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

Definition at line 90 of file database_connection.h.

References m_lastError.

◆ getQuoteChar()

bool DATABASE_CONNECTION::getQuoteChar ( )
private

Definition at line 263 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 121 of file database_connection.cpp.

References m_cache.

Referenced by DATABASE_CONNECTION().

◆ IsConnected()

bool DATABASE_CONNECTION::IsConnected ( ) const

Definition at line 200 of file database_connection.cpp.

References m_conn.

Referenced by 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 643 of file database_connection.cpp.

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

◆ selectAllAndCache()

bool DATABASE_CONNECTION::selectAllAndCache ( const std::string &  aTable,
const std::string &  aKey 
)
private

◆ 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 318 of file database_connection.cpp.

References columnsFor(), fromUTF8(), m_cache, m_columnCache, m_conn, m_lastError, m_quoteChar, m_tables, PROF_TIMER::msecs(), selectAllAndCache(), PROF_TIMER::Stop(), toUTF8(), and traceDatabase.

Referenced by BOOST_AUTO_TEST_CASE().

◆ SetCacheParams()

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

Definition at line 127 of file database_connection.cpp.

References m_cache.

Member Data Documentation

◆ DEFAULT_TIMEOUT

const long DATABASE_CONNECTION::DEFAULT_TIMEOUT = 10
static

Definition at line 44 of file database_connection.h.

◆ m_cache

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

Definition at line 121 of file database_connection.h.

Referenced by init(), SelectAll(), selectAllAndCache(), SelectOne(), and SetCacheParams().

◆ 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 113 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 106 of file database_connection.h.

Referenced by Connect().

◆ m_dsn

std::string DATABASE_CONNECTION::m_dsn
private

Definition at line 103 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 105 of file database_connection.h.

Referenced by Connect(), and DATABASE_CONNECTION().

◆ m_quoteChar

char DATABASE_CONNECTION::m_quoteChar
private

Definition at line 117 of file database_connection.h.

Referenced by columnsFor(), getQuoteChar(), selectAllAndCache(), and SelectOne().

◆ m_tables

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

Definition at line 110 of file database_connection.h.

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

◆ m_timeout

long DATABASE_CONNECTION::m_timeout
private

Definition at line 115 of file database_connection.h.

Referenced by Connect(), and DATABASE_CONNECTION().

◆ m_user

std::string DATABASE_CONNECTION::m_user
private

Definition at line 104 of file database_connection.h.

Referenced by Connect(), and DATABASE_CONNECTION().


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