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 <cstdint>
26#include <map>
27#include <memory>
28#include <mutex>
29#include <set>
30#include <string>
31#include <vector>
32
35
36
43{
44public:
45 typedef std::map<std::string, std::any> ROW;
46
51 DATABASE_CONNECTION( std::unique_ptr<DATABASE_BACKEND> aBackend, bool aConnectNow = true );
52
55 {
56 public:
57 LOAD_CYCLE( DATABASE_CONNECTION& aConnection, DB_STOP_CHECK aStopped );
58
60
61 LOAD_CYCLE( const LOAD_CYCLE& ) = delete;
62 LOAD_CYCLE& operator=( const LOAD_CYCLE& ) = delete;
63
64 private:
66 };
67
68 void SetCacheParams( int aMaxSize, int aMaxAge );
69
70 bool Connect();
71
72 bool Disconnect();
73
74 bool IsConnected() const;
75
90 bool CacheTableInfo( const std::string& aTable, const std::set<std::string>& aRequiredColumns,
91 const std::set<std::string>& aOptionalColumns = {} );
92
102 bool SelectOne( const std::string& aTable, const std::pair<std::string, std::string>& aWhere,
103 ROW& aResult );
104
107 {
108 bool connected = false;
110 std::string error;
111 };
112
115 {
117 std::string error;
118
120 std::set<std::string> columns;
121 };
122
126 std::optional<TABLE_FAILURE> GetTableFailure( const std::string& aTable ) const;
127
136 bool SelectAll( const std::string& aTable, const std::string& aKey, std::vector<ROW>& aResults,
137 QUERY_STATUS* aStatus = nullptr );
138
147 bool SelectSample( const std::string& aTable, size_t aMaxRows, std::vector<std::string>& aColumns,
148 std::vector<ROW>& aRows );
149
153 bool GetTables( std::vector<std::string>& aTables );
154
158 bool GetColumns( const std::string& aTable, std::vector<std::string>& aColumns );
159
160 void ClearCache( const std::string& aTable )
161 {
162 if( m_cache )
163 m_cache->Clear( aTable );
164 }
165
166 std::string GetLastError() const { return m_backend->GetLastError(); }
167
169 DB_FAILURE GetLastFailure() const { return m_backend->GetLastFailure(); }
170
171private:
173
176 {
178
180 std::set<std::string> columns;
181 };
182
184 static std::vector<std::string> columnNames( const TABLE_STATE& aTable );
185
187 static std::optional<size_t> findLabel( const std::vector<std::string>& aLabels, const std::string& aKey );
188
190 static ROW makeRow( const std::vector<std::string>& aLabels, std::vector<DB_VALUE>&& aValues );
191
196 bool readAndCache( const std::string& aTable, const std::string& aKey, DB_CACHE_TYPE::CACHE_VALUE& aEntry );
197
198 std::unique_ptr<DATABASE_BACKEND> m_backend;
199
200 std::map<std::string, TABLE_STATE> m_tables;
201
203 std::map<std::string, TABLE_FAILURE> m_tableFailures;
204
205 mutable std::mutex m_queryMutex;
206
207 std::unique_ptr<DB_CACHE_TYPE> m_cache;
208};
209
210#endif //KICAD_DATABASE_CONNECTION_H
LOAD_CYCLE(const LOAD_CYCLE &)=delete
LOAD_CYCLE(DATABASE_CONNECTION &aConnection, DB_STOP_CHECK aStopped)
LOAD_CYCLE & operator=(const LOAD_CYCLE &)=delete
DB_FAILURE GetLastFailure() const
bool SelectAll(const std::string &aTable, const std::string &aKey, std::vector< ROW > &aResults, QUERY_STATUS *aStatus=nullptr)
Retrieves all rows from a database table.
DATABASE_CONNECTION(std::unique_ptr< DATABASE_BACKEND > aBackend, bool aConnectNow=true)
bool GetColumns(const std::string &aTable, std::vector< std::string > &aColumns)
Lists the columns of a table in catalog order.
std::map< std::string, std::any > ROW
std::unique_ptr< DATABASE_BACKEND > m_backend
static ROW makeRow(const std::vector< std::string > &aLabels, std::vector< DB_VALUE > &&aValues)
Converts one row delivered by the backend to text keyed by result-column label.
std::map< std::string, TABLE_STATE > m_tables
void SetCacheParams(int aMaxSize, int aMaxAge)
std::unique_ptr< DB_CACHE_TYPE > m_cache
std::map< std::string, TABLE_FAILURE > m_tableFailures
Why CacheTableInfo could not describe each table, so a later query on it reports that table's own fai...
static std::vector< std::string > columnNames(const TABLE_STATE &aTable)
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, bypassing the row cache.
DATABASE_CACHE< std::map< std::string, ROW > > DB_CACHE_TYPE
bool SelectOne(const std::string &aTable, const std::pair< std::string, std::string > &aWhere, ROW &aResult)
Retrieves a single row from a database table.
static std::optional< size_t > findLabel(const std::vector< std::string > &aLabels, const std::string &aKey)
std::string GetLastError() const
bool readAndCache(const std::string &aTable, const std::string &aKey, DB_CACHE_TYPE::CACHE_VALUE &aEntry)
Reads a whole table into aEntry and offers it to the row cache.
std::optional< TABLE_FAILURE > GetTableFailure(const std::string &aTable) 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::function< bool()> DB_STOP_CHECK
True once the caller no longer wants what a read would deliver.
DB_FAILURE
Why a connection attempt failed, as far as the backend's report tells.
What a failed query left behind, copied before another thread's query can replace it.
Why CacheTableInfo could not describe a table.
std::set< std::string > columns
Lowercase columns the failed lookups asked for; only a lookup asking for all of them clears the failu...
A table the backend described, with the columns queries against it read.
std::set< std::string > columns
The column spellings queries read. Spellings differing only in case are distinct columns.