KiCad PCB EDA Suite
Loading...
Searching...
No Matches
database_connection.h
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 The 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
18 * along with this program. If not, see <https://www.gnu.org/licenses/>.
19 */
20
21#ifndef KICAD_DATABASE_CONNECTION_H
22#define KICAD_DATABASE_CONNECTION_H
23
24#include <any>
25#include <map>
26#include <memory>
27#include <mutex>
28#include <set>
29#include <vector>
30
32
33extern const char* const traceDatabase;
34
35
36namespace nanodbc
37{
38 class connection;
39}
40
41
43{
44public:
45 static const long DEFAULT_TIMEOUT = 10;
46
47 typedef std::map<std::string, std::any> ROW;
48
59
60 DATABASE_CONNECTION( const std::string& aDataSourceName, const std::string& aUsername,
61 const std::string& aPassword, int aTimeoutSeconds = DEFAULT_TIMEOUT,
62 bool aConnectNow = true );
63
64 DATABASE_CONNECTION( const std::string& aConnectionString,
65 int aTimeoutSeconds = DEFAULT_TIMEOUT,
66 bool aConnectNow = true );
67
69
70 void SetCacheParams( int aMaxSize, int aMaxAge );
71
72 bool Connect();
73
74 bool Disconnect();
75
76 bool IsConnected() const;
77
91 bool CacheTableInfo( const std::string& aTable, const std::set<std::string>& aRequiredColumns,
92 const std::set<std::string>& aOptionalColumns = {} );
93
103 bool SelectOne( const std::string& aTable, const std::pair<std::string, std::string>& aWhere,
104 ROW& aResult );
105
113 bool SelectAll( const std::string& aTable, const std::string& aKey,
114 std::vector<ROW>& aResults );
115
124 bool SelectSample( const std::string& aTable, size_t aMaxRows, std::vector<std::string>& aColumns,
125 std::vector<ROW>& aRows );
126
130 bool GetTables( std::vector<std::string>& aTables );
131
135 bool GetColumns( const std::string& aTable, std::vector<std::string>& aColumns );
136
139 {
140 std::string name;
141 std::string driver;
142 };
143
145 static std::vector<DATA_SOURCE> ListDataSourceDetails();
146
148 static std::vector<std::string> ListDrivers();
149
150 void ClearCache( const std::string& aTable )
151 {
152 if( m_cache )
153 m_cache->Clear( aTable );
154 }
155
156 std::string GetLastError() const { return m_lastError; }
157
159 std::string GetLastState() const { return m_lastState; }
160
162 static FAILURE ClassifyFailure( const std::string& aState, const std::string& aMessage );
163
164private:
165 void init();
166
167 bool getQuoteChar();
168
170 std::string quoted( const std::string& aIdentifier ) const;
171
173 std::string escapePattern( const std::string& aName ) const;
174
175 std::string columnsFor( const std::string& aTable );
176
178 bool selectAllAndCache( const std::string& aTable, const std::string& aKey );
179
180 std::unique_ptr<nanodbc::connection> m_conn;
181
182 std::string m_dsn;
183 std::string m_user;
184 std::string m_pass;
186
187 std::string m_lastError;
188 std::string m_lastState;
189
190 std::map<std::string, std::string> m_tables;
191
193 std::map<std::string, std::map<std::string, int>> m_columnCache;
194
196
198
199 std::string m_patternEscape;
200
201 mutable std::mutex m_queryMutex;
202
204
205 std::unique_ptr<DB_CACHE_TYPE> m_cache;
206};
207
208#endif //KICAD_DATABASE_CONNECTION_H
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.
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)
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.