DotNetDupe
4.0.6
C++17/20 Implementation of the .NET Base Class Library (BCL)
Toggle main menu visibility
Loading...
Searching...
No Matches
SqlConnection.cpp
Go to the documentation of this file.
1
#include "
pch.h
"
2
#include "
System/Data/SqlClient/SqlConnection.h
"
3
#include "
System/Data/SqlClient/SqlCommand.h
"
4
#include "
System/Data/Internal/DatabaseEngine.h
"
5
#include "
System/Data/Internal/SqliteDatabaseBackend.h
"
6
#include "
System/Data/Internal/InMemoryDatabaseBackend.h
"
7
#include "
System/InvalidOperationException.h
"
8
#include <algorithm>
9
#include <string>
10
11
namespace
DotNetDupe
{
12
namespace
System
{
13
namespace
Data
{
14
namespace
SqlClient
{
15
16
struct
SqlConnection::Impl {
17
DotNetDupe::System::String m_sConnectionString;
18
std::string m_dbName;
19
std::string m_engineType;
20
bool
m_bIsOpen =
false
;
21
};
22
23
SqlConnection::SqlConnection
() : m_pImpl(
DotNetDupe
::
System
::
SmartPointer
<Impl>::NewShared()) {
24
}
25
26
SqlConnection::SqlConnection
(
const
DotNetDupe::System::String
& sConnectionString)
27
: m_pImpl(
DotNetDupe
::
System
::
SmartPointer
<Impl>::NewShared()) {
28
SetConnectionString
(sConnectionString);
29
}
30
31
SqlConnection::~SqlConnection
() =
default
;
32
33
DotNetDupe::System::String
SqlConnection::GetConnectionString
()
const
{
34
return
m_pImpl->m_sConnectionString;
35
}
36
37
static
std::string
ExtractConnectionStringValue
(
const
std::string& raw,
const
std::string& key,
const
std::string& defaultVal) {
38
size_t
idx = raw.find(key);
39
if
(idx == std::string::npos)
return
defaultVal;
40
idx += key.length();
41
size_t
endIdx = raw.find(
";"
, idx);
42
return
(endIdx != std::string::npos) ? raw.substr(idx, endIdx - idx) : raw.substr(idx);
43
}
44
45
static
std::string
ExtractDatabaseName
(
const
std::string& raw) {
46
if
(raw.find(
"Database="
) != std::string::npos)
return
ExtractConnectionStringValue
(raw,
"Database="
,
"DefaultDb"
);
47
return
ExtractConnectionStringValue
(raw,
"Data Source="
,
"DefaultDb"
);
48
}
49
50
void
SqlConnection::SetConnectionString
(
const
DotNetDupe::System::String
& sConnStr) {
51
m_pImpl->m_sConnectionString = sConnStr;
52
std::string raw = sConnStr.
GetRawString
();
53
m_pImpl->m_dbName =
ExtractDatabaseName
(raw);
54
m_pImpl->m_engineType =
ExtractConnectionStringValue
(raw,
"Engine="
,
"InMemory"
);
55
}
56
57
DotNetDupe::System::String
SqlConnection::GetDatabaseName
()
const
{
58
return
DotNetDupe::System::String
(m_pImpl->m_dbName.c_str());
59
}
60
61
bool
SqlConnection::IsOpen
()
const
{
62
return
m_pImpl->m_bIsOpen;
63
}
64
65
static
void
RegisterSelectedBackend
(
const
std::string& engineType,
const
std::string& dbName,
const
DotNetDupe::System::String
& sConnStr) {
66
std::string engineUpper = engineType;
67
std::transform(engineUpper.begin(), engineUpper.end(), engineUpper.begin(), ::toupper);
68
if
(engineUpper ==
"SQLITE"
) {
69
#if defined(DOTNETDUPE_USE_SQLITE)
70
auto
sqliteBackend =
DotNetDupe::System::SmartPointer<DotNetDupe::System::Data::Internal::SqliteDatabaseBackend>::NewShared
(sConnStr.
GetRawString
());
71
DotNetDupe::System::Data::Internal::DatabaseEngine::Instance
().
RegisterBackend
(
DotNetDupe::System::String
(dbName.c_str()), sqliteBackend.DynamicCast<
DotNetDupe::System::Data::Internal::IDatabaseBackend
>());
72
#else
73
throw
DotNetDupe::System::InvalidOperationException
(
"SQLite database engine is not compiled in this build."
);
74
#endif
75
}
else
{
76
auto
inMemoryBackend =
DotNetDupe::System::SmartPointer<DotNetDupe::System::Data::Internal::InMemoryDatabaseBackend>::NewShared
();
77
DotNetDupe::System::Data::Internal::DatabaseEngine::Instance
().
RegisterBackend
(
DotNetDupe::System::String
(dbName.c_str()), inMemoryBackend.DynamicCast<
DotNetDupe::System::Data::Internal::IDatabaseBackend
>());
78
}
79
}
80
81
void
SqlConnection::Open
() {
83
m_pImpl->m_bIsOpen =
true
;
84
RegisterSelectedBackend
(m_pImpl->m_engineType, m_pImpl->m_dbName, m_pImpl->m_sConnectionString);
85
}
86
87
void
SqlConnection::Close
() {
89
m_pImpl->m_bIsOpen =
false
;
90
}
91
92
DotNetDupe::System::SmartPointer<DotNetDupe::System::Data::Common::DbCommand>
SqlConnection::CreateCommand
() {
94
return
DotNetDupe::System::SmartPointer<SqlCommand>::NewShared
(
""
,
this
);
95
}
96
97
}
98
}
99
}
100
}
DatabaseEngine.h
InMemoryDatabaseBackend.h
InvalidOperationException.h
Defines the exception thrown when a method call is invalid for the object's current state.
SqlCommand.h
SqlConnection.h
SqliteDatabaseBackend.h
DotNetDupe::System::Data::Internal::DatabaseEngine::RegisterBackend
void RegisterBackend(const DotNetDupe::System::String &dbName, DotNetDupe::System::SmartPointer< IDatabaseBackend > backend)
Registers a backend instance for a specific database name.
Definition
DatabaseEngine.cpp:30
DotNetDupe::System::Data::Internal::DatabaseEngine::Instance
static DatabaseEngine & Instance()
Retrieves the singleton instance of the DatabaseEngine.
Definition
DatabaseEngine.cpp:24
DotNetDupe::System::Data::Internal::IDatabaseBackend
Interface defining the contract for database storage backends in DotNetDupe.
Definition
IDatabaseBackend.h:29
DotNetDupe::System::Data::SqlClient::SqlConnection::Open
void Open() override
Opens a database connection with the property settings specified by the ConnectionString.
Definition
SqlConnection.cpp:81
DotNetDupe::System::Data::SqlClient::SqlConnection::~SqlConnection
~SqlConnection() override
Destructor ensuring connection closure.
DotNetDupe::System::Data::SqlClient::SqlConnection::SqlConnection
SqlConnection()
Initializes a new instance of the SqlConnection class.
Definition
SqlConnection.cpp:23
DotNetDupe::System::Data::SqlClient::SqlConnection::SetConnectionString
void SetConnectionString(const DotNetDupe::System::String &sConnStr) override
Sets the string used to open a SQL database.
Definition
SqlConnection.cpp:50
DotNetDupe::System::Data::SqlClient::SqlConnection::CreateCommand
DotNetDupe::System::SmartPointer< DotNetDupe::System::Data::Common::DbCommand > CreateCommand() override
Creates and returns a SqlCommand object associated with the SqlConnection.
Definition
SqlConnection.cpp:92
DotNetDupe::System::Data::SqlClient::SqlConnection::GetDatabaseName
DotNetDupe::System::String GetDatabaseName() const
Gets the name of the current database.
Definition
SqlConnection.cpp:57
DotNetDupe::System::Data::SqlClient::SqlConnection::IsOpen
bool IsOpen() const
Gets whether the connection is currently open.
Definition
SqlConnection.cpp:61
DotNetDupe::System::Data::SqlClient::SqlConnection::Close
void Close() override
Closes the connection to the database.
Definition
SqlConnection.cpp:87
DotNetDupe::System::Data::SqlClient::SqlConnection::GetConnectionString
DotNetDupe::System::String GetConnectionString() const override
Gets the string used to open a SQL database.
Definition
SqlConnection.cpp:33
DotNetDupe::System::InvalidOperationException
The exception that is thrown when a method call is invalid for the object's current state.
Definition
InvalidOperationException.h:16
DotNetDupe::System::SmartPointer
A unified smart pointer that supports both unique and shared ownership semantics.
Definition
SmartPointer.h:72
DotNetDupe::System::SmartPointer::NewShared
static SmartPointer< T > NewShared()
Creates a Shared SmartPointer, default constructing T.
Definition
SmartPointer.h:277
DotNetDupe::System::String
Represents text as a sequence of UTF-8 code units with culture-invariant operations.
Definition
String.h:74
DotNetDupe::System::String::GetRawString
const char * GetRawString() const
Definition
String.cpp:230
DotNetDupe::System::Data::SqlClient
Definition
SqlCommand.h:8
DotNetDupe::System::Data::SqlClient::ExtractConnectionStringValue
static std::string ExtractConnectionStringValue(const std::string &raw, const std::string &key, const std::string &defaultVal)
Definition
SqlConnection.cpp:37
DotNetDupe::System::Data::SqlClient::RegisterSelectedBackend
static void RegisterSelectedBackend(const std::string &engineType, const std::string &dbName, const DotNetDupe::System::String &sConnStr)
Definition
SqlConnection.cpp:65
DotNetDupe::System::Data::SqlClient::ExtractDatabaseName
static std::string ExtractDatabaseName(const std::string &raw)
Definition
SqlConnection.cpp:45
DotNetDupe::System::Data
Definition
DbCommand.h:11
DotNetDupe::System
Definition
Action.h:11
DotNetDupe
Definition
IServiceCollection.h:7
pch.h
DotNetDupe
SqlConnection.cpp
Generated by
1.18.0