21#ifndef KICAD_DATABASE_CONNECTION_H
22#define KICAD_DATABASE_CONNECTION_H
47 typedef std::map<std::string, std::any>
ROW;
62 bool aConnectNow =
true );
66 bool aConnectNow =
true );
91 bool CacheTableInfo(
const std::string& aTable,
const std::set<std::string>& aRequiredColumns,
92 const std::set<std::string>& aOptionalColumns = {} );
103 bool SelectOne(
const std::string& aTable,
const std::pair<std::string, std::string>& aWhere,
113 bool SelectAll(
const std::string& aTable,
const std::string& aKey,
114 std::vector<ROW>& aResults );
124 bool SelectSample(
const std::string& aTable,
size_t aMaxRows, std::vector<std::string>& aColumns,
125 std::vector<ROW>& aRows );
130 bool GetTables( std::vector<std::string>& aTables );
135 bool GetColumns(
const std::string& aTable, std::vector<std::string>& aColumns );
170 std::string
quoted(
const std::string& aIdentifier )
const;
175 std::string
columnsFor(
const std::string& aTable );
180 std::unique_ptr<nanodbc::connection>
m_conn;
FAILURE
Why a connection attempt failed, as far as the driver's report tells.
@ TIMEOUT
The server did not answer within the timeout.
@ NO_DSN
The data source name is not configured.
@ LOGIN
The server rejected the user name or password.
@ UNREACHABLE
Nothing answered at the server address, or it refused the connection.
@ NO_DRIVER
The driver named by the data source could not be loaded.
std::string m_connectionString
bool GetColumns(const std::string &aTable, std::vector< std::string > &aColumns)
Lists the columns of a table in catalog order.
DATABASE_CONNECTION(const std::string &aConnectionString, int aTimeoutSeconds=DEFAULT_TIMEOUT, bool aConnectNow=true)
static FAILURE ClassifyFailure(const std::string &aState, const std::string &aMessage)
Classifies a driver error from its SQLSTATE, falling back to its text since drivers disagree on state...
static std::vector< std::string > ListDrivers()
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.
std::string quoted(const std::string &aIdentifier) const
Wraps an identifier in the driver's quote character, doubling any it contains.
void SetCacheParams(int aMaxSize, int aMaxAge)
bool selectAllAndCache(const std::string &aTable, const std::string &aKey)
The caller must already hold m_queryMutex.
bool SelectAll(const std::string &aTable, const std::string &aKey, std::vector< ROW > &aResults)
Retrieves all rows from a database table.
std::string escapePattern(const std::string &aName) const
Escapes catalog search pattern characters so a name matches only itself.
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 GetLastState() const
static const long DEFAULT_TIMEOUT
void ClearCache(const std::string &aTable)
bool GetTables(std::vector< std::string > &aTables)
Lists the tables and views visible through this connection, sorted by name.
bool SelectSample(const std::string &aTable, size_t aMaxRows, std::vector< std::string > &aColumns, std::vector< ROW > &aRows)
Reads the first rows of a table with a forward-only cursor, bypassing the row cache.
std::string columnsFor(const std::string &aTable)
std::string m_patternEscape
DATABASE_CACHE< std::map< std::string, ROW > > DB_CACHE_TYPE
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::string GetLastError() const
static std::vector< DATA_SOURCE > ListDataSourceDetails()
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.
std::map< std::string, std::string > m_tables
const char *const traceDatabase
A data source configured in the ODBC driver manager.