KiCad PCB EDA Suite
Loading...
Searching...
No Matches
database_backend.cpp
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
21
23#include <wx/intl.h>
24
25#include "odbc_backend.h"
26#include "db_backend_support.h"
27#include "sqlite_backend.h"
28
29#ifdef _WIN32
30#include "ace_backend.h"
31#endif
32
33
34DB_BACKEND_RESULT DATABASE_BACKEND::Create( const DATABASE_SOURCE& aSource, const wxString& aBasePath )
35{
36 const DATABASE_SOURCE source = aSource.Expanded( aBasePath );
37
38 switch( source.type )
39 {
41 return { std::make_unique<ODBC_BACKEND>( source.dsn, source.username, source.password,
42 source.connection_string, source.timeout ),
43 DB_FAILURE::UNKNOWN, std::string() };
44
46 return { std::make_unique<SQLITE_BACKEND>( source.path, source.timeout ), DB_FAILURE::UNKNOWN,
47 std::string() };
48
50#ifdef _WIN32
51 // A probe still running is waited on once, by Connect
54
55 return { std::make_unique<ACE_BACKEND>( source.path, source.password, source.timeout ), DB_FAILURE::UNKNOWN,
56 std::string() };
57#else
59 utf8( wxString::Format( _( "source type '%s' is only available on Windows" ), wxS( "access" ) ) ) };
60#endif
61
63 break;
64 }
65
66 return { nullptr, DB_FAILURE::INVALID_TYPE,
67 utf8( wxString::Format( _( "unknown source type '%s'" ), wxString::FromUTF8( source.type_name ) ) ) };
68}
69
70
71bool DATABASE_BACKEND::ListColumns( const std::string& aTable, std::vector<std::string>& aColumns )
72{
73 aColumns.clear();
74
75 std::optional<DB_TABLE_INFO> info = DescribeTable( aTable );
76
77 // DescribeTable clears the error first, so an absent table leaves none and lists no columns
78 if( !info )
79 return GetLastError().empty();
80
81 for( const DB_COLUMN& column : info->columns )
82 aColumns.push_back( column.name );
83
84 return true;
85}
static PROVIDER_STATE ProviderState()
Tells whether the ACE OLE DB provider is installed and loads.
static std::string ProviderMissingMessage()
The message for a source that needs the provider when ProviderState() is MISSING.
virtual std::optional< DB_TABLE_INFO > DescribeTable(const std::string &aTable)=0
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,...
static DB_BACKEND_RESULT Create(const DATABASE_SOURCE &aSource, const wxString &aBasePath)
Builds the backend for a source.
virtual std::string GetLastError() const =0
@ INVALID_TYPE
The source type is not one KiCad knows.
@ PROVIDER_MISSING
The database engine needed to open the file is not installed.
@ NOT_SUPPORTED_ON_PLATFORM
No backend for the source type exists on this platform.
std::string utf8(const wxString &aText)
#define _(s)
std::string source
DATABASE_SOURCE Expanded(const wxString &aBaseDir) const
The outcome of building a backend; backend is null when failure and error say why.
A column as the backend reports it.
std::string name