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
49 DATABASE_CONNECTION( const std::string& aDataSourceName, const std::string& aUsername,
50 const std::string& aPassword, int aTimeoutSeconds = DEFAULT_TIMEOUT,
51 bool aConnectNow = true );
52
53 DATABASE_CONNECTION( const std::string& aConnectionString,
54 int aTimeoutSeconds = DEFAULT_TIMEOUT,
55 bool aConnectNow = true );
56
58
59 void SetCacheParams( int aMaxSize, int aMaxAge );
60
61 bool Connect();
62
63 bool Disconnect();
64
65 bool IsConnected() const;
66
80 bool CacheTableInfo( const std::string& aTable, const std::set<std::string>& aRequiredColumns,
81 const std::set<std::string>& aOptionalColumns = {} );
82
92 bool SelectOne( const std::string& aTable, const std::pair<std::string, std::string>& aWhere,
93 ROW& aResult );
94
102 bool SelectAll( const std::string& aTable, const std::string& aKey,
103 std::vector<ROW>& aResults );
104
105 void ClearCache( const std::string& aTable )
106 {
107 if( m_cache )
108 m_cache->Clear( aTable );
109 }
110
111 std::string GetLastError() const { return m_lastError; }
112
113private:
114 void init();
115
116 bool getQuoteChar();
117
118 std::string columnsFor( const std::string& aTable );
119
121 bool selectAllAndCache( const std::string& aTable, const std::string& aKey );
122
123 std::unique_ptr<nanodbc::connection> m_conn;
124
125 std::string m_dsn;
126 std::string m_user;
127 std::string m_pass;
129
130 std::string m_lastError;
131
132 std::map<std::string, std::string> m_tables;
133
135 std::map<std::string, std::map<std::string, int>> m_columnCache;
136
138
140
141 mutable std::mutex m_queryMutex;
142
144
145 std::unique_ptr<DB_CACHE_TYPE> m_cache;
146};
147
148#endif //KICAD_DATABASE_CONNECTION_H
DATABASE_CONNECTION(const std::string &aConnectionString, int aTimeoutSeconds=DEFAULT_TIMEOUT, bool aConnectNow=true)
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.
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.
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
static const long DEFAULT_TIMEOUT
void ClearCache(const std::string &aTable)
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
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