KiCad PCB EDA Suite
Loading...
Searching...
No Matches
sqlite_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_SQLITE_BACKEND_H
21#define KICAD_SQLITE_BACKEND_H
22
23#include <atomic>
24#include <chrono>
25#include <functional>
26#include <memory>
27#include <optional>
28#include <string>
29#include <vector>
30
32
33#include "db_worker_thread.h"
34
35struct SQLITE_DB;
36
37
44{
45public:
50 SQLITE_BACKEND( std::string aPath, int aTimeoutSeconds );
51
52 ~SQLITE_BACKEND() override;
53
54 bool Connect() override;
55
56 void Disconnect() override;
57
58 bool IsConnected() const override { return m_connected; }
59
60 bool ListTables( std::vector<std::string>& aTables ) override;
61
62 std::optional<DB_TABLE_INFO> DescribeTable( const std::string& aTable ) override;
63
64
65 bool ReadTable( const DB_TABLE_INFO& aTable, const std::vector<std::string>& aColumns, size_t aMaxRows,
66 std::vector<std::string>& aLabels, const DB_ROW_SINK& aSink ) override;
67
68 std::string GetLastError() const override { return m_lastError; }
69
70 DB_FAILURE GetLastFailure() const override { return m_lastFailure; }
71
79 static std::string FileUri( const std::string& aPath, bool aWindowsPath );
80
82 static bool IsUncPath( const std::string& aPath );
83
85 static bool IsNetworkPath( const std::string& aPath );
86
92 static bool IsRemoteFuseMount( const std::string& aMountInfo, const std::string& aPath );
93
94private:
95 std::chrono::milliseconds callerBound() const;
96
98 template <typename R>
99 std::optional<R> runOnWorker( std::function<R( DB_JOB_CONTEXT& )> aJob, const DB_ROW_SINK* aSink );
100
102 void dropConnection();
103
104 void fail( DB_FAILURE aFailure, std::string aError );
105
107 void failCall( DB_FAILURE aFailure, std::string aMessage );
108
110 bool requireConnection();
111
112 std::string m_path;
114
115 std::unique_ptr<DB_WORKER_THREAD> m_worker;
116 std::shared_ptr<SQLITE_DB> m_db;
117
118 std::atomic<bool> m_connected{ false };
119
120 std::string m_lastError;
122};
123
124#endif // KICAD_SQLITE_BACKEND_H
A read-only source of tables.
What a job running on a DB_WORKER_THREAD uses to hand rows to the caller.
static bool IsUncPath(const std::string &aPath)
std::unique_ptr< DB_WORKER_THREAD > m_worker
void failCall(DB_FAILURE aFailure, std::string aMessage)
Records a failure of a call that ran, and drops the connection if the failure means it cannot be trus...
bool ListTables(std::vector< std::string > &aTables) override
Lists the tables and views, sorted by name.
DB_FAILURE m_lastFailure
void fail(DB_FAILURE aFailure, std::string aError)
SQLITE_BACKEND(std::string aPath, int aTimeoutSeconds)
~SQLITE_BACKEND() override
std::string m_lastError
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) override
Reads rows of a table, handing each to aSink.
static std::string FileUri(const std::string &aPath, bool aWindowsPath)
Builds the read-only SQLite URI for a file.
DB_FAILURE GetLastFailure() const override
std::string GetLastError() const override
void Disconnect() override
bool requireConnection()
Fails with a message and returns false unless a connection is open.
std::string m_path
bool Connect() override
std::chrono::milliseconds callerBound() const
std::atomic< bool > m_connected
std::shared_ptr< SQLITE_DB > m_db
bool IsConnected() const override
void dropConnection()
Forgets the connection and closes it on a worker thread, never on the caller.
static bool IsNetworkPath(const std::string &aPath)
std::optional< R > runOnWorker(std::function< R(DB_JOB_CONTEXT &)> aJob, const DB_ROW_SINK *aSink)
Runs aJob on the worker, recording why it did not finish as the failure.
static bool IsRemoteFuseMount(const std::string &aMountInfo, const std::string &aPath)
Decides from the text of /proc/self/mountinfo whether aPath lies on a FUSE mount that reaches a remot...
std::optional< DB_TABLE_INFO > DescribeTable(const std::string &aTable) override
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.
Owns one connection, which a worker thread opens and uses.