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 (C) 2022 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 along
18 * with this program. If not, see <http://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 <set>
28#include <vector>
29
31
32extern const char* const traceDatabase;
33
34
35namespace nanodbc
36{
37 class connection;
38}
39
40
42{
43public:
44 static const long DEFAULT_TIMEOUT = 10;
45
46 typedef std::map<std::string, std::any> ROW;
47
48 DATABASE_CONNECTION( const std::string& aDataSourceName, const std::string& aUsername,
49 const std::string& aPassword, int aTimeoutSeconds = DEFAULT_TIMEOUT,
50 bool aConnectNow = true );
51
52 DATABASE_CONNECTION( const std::string& aConnectionString,
53 int aTimeoutSeconds = DEFAULT_TIMEOUT,
54 bool aConnectNow = true );
55
57
58 void SetCacheParams( int aMaxSize, int aMaxAge );
59
60 bool Connect();
61
62 bool Disconnect();
63
64 bool IsConnected() const;
65
66 bool CacheTableInfo( const std::string& aTable, const std::set<std::string>& aColumns );
67
77 bool SelectOne( const std::string& aTable, const std::pair<std::string, std::string>& aWhere,
78 ROW& aResult );
79
87 bool SelectAll( const std::string& aTable, const std::string& aKey,
88 std::vector<ROW>& aResults );
89
90 std::string GetLastError() const { return m_lastError; }
91
92private:
93 void init();
94
95 bool getQuoteChar();
96
97 std::string columnsFor( const std::string& aTable );
98
99 bool selectAllAndCache( const std::string& aTable, const std::string& aKey );
100
101 std::unique_ptr<nanodbc::connection> m_conn;
102
103 std::string m_dsn;
104 std::string m_user;
105 std::string m_pass;
107
108 std::string m_lastError;
109
110 std::map<std::string, std::string> m_tables;
111
113 std::map<std::string, std::map<std::string, int>> m_columnCache;
114
116
118
120
121 std::unique_ptr<DB_CACHE_TYPE> m_cache;
122};
123
124#endif //KICAD_DATABASE_CONNECTION_H
bool CacheTableInfo(const std::string &aTable, const std::set< std::string > &aColumns)
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)
bool SelectAll(const std::string &aTable, const std::string &aKey, std::vector< ROW > &aResults)
Retrieves all rows from a database table.
std::unique_ptr< DB_CACHE_TYPE > m_cache
static const long DEFAULT_TIMEOUT
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
std::map< std::string, std::string > m_tables
const char *const traceDatabase