DotNetDupe 4.0.6
C++17/20 Implementation of the .NET Base Class Library (BCL)
Loading...
Searching...
No Matches
SqlConnection.cpp
Go to the documentation of this file.
1#include "pch.h"
8#include <algorithm>
9#include <string>
10
11namespace 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
27 : m_pImpl(DotNetDupe::System::SmartPointer<Impl>::NewShared()) {
28 SetConnectionString(sConnectionString);
29 }
30
32
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
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
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)
72#else
73 throw DotNetDupe::System::InvalidOperationException("SQLite database engine is not compiled in this build.");
74#endif
75 } else {
78 }
79 }
80
83 m_pImpl->m_bIsOpen = true;
84 RegisterSelectedBackend(m_pImpl->m_engineType, m_pImpl->m_dbName, m_pImpl->m_sConnectionString);
85 }
86
89 m_pImpl->m_bIsOpen = false;
90 }
91
96
97 }
98 }
99 }
100}
Defines the exception thrown when a method call is invalid for the object's current state.
void RegisterBackend(const DotNetDupe::System::String &dbName, DotNetDupe::System::SmartPointer< IDatabaseBackend > backend)
Registers a backend instance for a specific database name.
static DatabaseEngine & Instance()
Retrieves the singleton instance of the DatabaseEngine.
Interface defining the contract for database storage backends in DotNetDupe.
void Open() override
Opens a database connection with the property settings specified by the ConnectionString.
~SqlConnection() override
Destructor ensuring connection closure.
SqlConnection()
Initializes a new instance of the SqlConnection class.
void SetConnectionString(const DotNetDupe::System::String &sConnStr) override
Sets the string used to open a SQL database.
DotNetDupe::System::SmartPointer< DotNetDupe::System::Data::Common::DbCommand > CreateCommand() override
Creates and returns a SqlCommand object associated with the SqlConnection.
DotNetDupe::System::String GetDatabaseName() const
Gets the name of the current database.
bool IsOpen() const
Gets whether the connection is currently open.
void Close() override
Closes the connection to the database.
DotNetDupe::System::String GetConnectionString() const override
Gets the string used to open a SQL database.
The exception that is thrown when a method call is invalid for the object's current state.
A unified smart pointer that supports both unique and shared ownership semantics.
static SmartPointer< T > NewShared()
Creates a Shared SmartPointer, default constructing T.
Represents text as a sequence of UTF-8 code units with culture-invariant operations.
Definition String.h:74
const char * GetRawString() const
Definition String.cpp:230
static std::string ExtractConnectionStringValue(const std::string &raw, const std::string &key, const std::string &defaultVal)
static void RegisterSelectedBackend(const std::string &engineType, const std::string &dbName, const DotNetDupe::System::String &sConnStr)
static std::string ExtractDatabaseName(const std::string &raw)