KiCad PCB EDA Suite
Loading...
Searching...
No Matches
database_backend.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 The KiCad Developers, see AUTHORS.txt for contributors.
5 *
6 * This program is free software: you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License as published by the
8 * Free Software Foundation, either version 3 of the License, or (at your
9 * option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <https://www.gnu.org/licenses/>.
18 */
19
20#ifndef KICAD_DATABASE_BACKEND_H
21#define KICAD_DATABASE_BACKEND_H
22
23#include <functional>
24#include <limits>
25#include <memory>
26#include <optional>
27#include <string>
28#include <vector>
29
30#include <wx/string.h>
31
33#include <trace_helpers.h>
34
35struct DATABASE_SOURCE;
36
37
54
55
58{
59 std::string name;
60 int type;
61 bool supported = true;
62};
63
64
66{
67 std::string name;
68 std::vector<DB_COLUMN> columns;
69
71 bool columnsUnavailable = false;
72};
73
74
76using DB_ROW_SINK = std::function<void( std::vector<DB_VALUE>&& )>;
77
79using DB_RESTART_SINK = std::function<void()>;
80
82using DB_STOP_CHECK = std::function<bool()>;
83
84
86
87
90{
91 std::unique_ptr<DATABASE_BACKEND> backend;
93 std::string error;
94};
95
96
103{
104public:
106 static constexpr size_t ALL_ROWS = std::numeric_limits<size_t>::max();
107
108 virtual ~DATABASE_BACKEND() = default;
109
110 virtual bool Connect() = 0;
111
112 virtual void Disconnect() = 0;
113
114 virtual bool IsConnected() const = 0;
115
117 virtual bool ListTables( std::vector<std::string>& aTables ) = 0;
118
120 virtual std::optional<DB_TABLE_INFO> DescribeTable( const std::string& aTable ) = 0;
121
123 virtual bool ListColumns( const std::string& aTable, std::vector<std::string>& aColumns );
124
135 virtual bool ReadTable( const DB_TABLE_INFO& aTable, const std::vector<std::string>& aColumns, size_t aMaxRows,
136 std::vector<std::string>& aLabels, const DB_ROW_SINK& aSink ) = 0;
137
146 virtual bool ReadAllRows( const DB_TABLE_INFO& aTable, const std::vector<std::string>& aColumns,
147 std::vector<std::string>& aLabels, const DB_ROW_SINK& aSink,
148 const DB_RESTART_SINK& aRestart )
149 {
150 return ReadTable( aTable, aColumns, ALL_ROWS, aLabels, aSink );
151 }
152
161 virtual void BeginLoadCycle( const DB_STOP_CHECK& aStopped ) {}
162
163 virtual void EndLoadCycle() {}
164
170 virtual std::optional<bool> ReadRow( const DB_TABLE_INFO& aTable, const std::vector<std::string>& aColumns,
171 const std::string& aKeyColumn, const std::string& aKey,
172 std::vector<std::string>& aLabels, std::vector<DB_VALUE>& aRow )
173 {
174 return std::nullopt;
175 }
176
177 virtual std::string GetLastError() const = 0;
178
179 virtual DB_FAILURE GetLastFailure() const = 0;
180
188 static DB_BACKEND_RESULT Create( const DATABASE_SOURCE& aSource, const wxString& aBasePath );
189};
190
191#endif // KICAD_DATABASE_BACKEND_H
A read-only source of tables.
virtual std::optional< DB_TABLE_INFO > DescribeTable(const std::string &aTable)=0
virtual DB_FAILURE GetLastFailure() const =0
virtual bool ListTables(std::vector< std::string > &aTables)=0
Lists the tables and views, sorted by name.
virtual bool ListColumns(const std::string &aTable, std::vector< std::string > &aColumns)
Lists the column names of a table as the catalog reports them, empty if it has none,...
virtual void Disconnect()=0
virtual void EndLoadCycle()
virtual bool IsConnected() const =0
virtual bool Connect()=0
virtual ~DATABASE_BACKEND()=default
virtual std::optional< bool > ReadRow(const DB_TABLE_INFO &aTable, const std::vector< std::string > &aColumns, const std::string &aKeyColumn, const std::string &aKey, std::vector< std::string > &aLabels, std::vector< DB_VALUE > &aRow)
Reads the one row whose key column equals aKey, using an indexed lookup if the backend has one.
virtual bool ReadTable(const DB_TABLE_INFO &aTable, const std::vector< std::string > &aColumns, size_t aMaxRows, std::vector< std::string > &aLabels, const DB_ROW_SINK &aSink)=0
Reads rows of a table, handing each to aSink.
virtual void BeginLoadCycle(const DB_STOP_CHECK &aStopped)
Brackets a load cycle, in which the caller reads each table once.
static constexpr size_t ALL_ROWS
Passed as ReadTable()'s row limit to read every row.
static DB_BACKEND_RESULT Create(const DATABASE_SOURCE &aSource, const wxString &aBasePath)
Builds the backend for a source.
virtual bool ReadAllRows(const DB_TABLE_INFO &aTable, const std::vector< std::string > &aColumns, std::vector< std::string > &aLabels, const DB_ROW_SINK &aSink, const DB_RESTART_SINK &aRestart)
Reads every row of a table for a caller that keeps them, handing each to aSink.
virtual std::string GetLastError() const =0
std::function< bool()> DB_STOP_CHECK
True once the caller no longer wants what a read would deliver.
std::function< void(std::vector< DB_VALUE > &&)> DB_ROW_SINK
Receives one row at a time, in the order of the labels the read produced.
DB_FAILURE
Why a connection attempt failed, as far as the backend's report tells.
@ TIMEOUT
The source did not answer within the timeout.
@ INVALID_TYPE
The source type is not one KiCad knows.
@ NO_DSN
The data source name is not configured.
@ NETWORK_WAL
A write-ahead-log database on a network path cannot be read safely.
@ LOGIN
The server rejected the user name or password.
@ PROVIDER_MISSING
The database engine needed to open the file is not installed.
@ UNREACHABLE
Nothing answered at the server address, or it refused the connection.
@ NOT_SUPPORTED_ON_PLATFORM
No backend for the source type exists on this platform.
@ FILE_IN_USE
The database file is locked by another process.
@ FILE_NOT_FOUND
The database file does not exist.
@ NO_DRIVER
The driver named by the data source could not be loaded.
std::function< void()> DB_RESTART_SINK
Told that the rows delivered so far are void, because the read starts the table again from its first ...
The outcome of building a backend; backend is null when failure and error say why.
std::unique_ptr< DATABASE_BACKEND > backend
A column as the backend reports it.
std::string name
bool supported
False for columns the backend can never read, such as attachments.
bool columnsUnavailable
True when the table exists but the backend could not read its column list, so only SELECT * works.
std::vector< DB_COLUMN > columns
std::string name
The table's name as the backend spells it.
wxLogTrace helper definitions.