DotNetDupe
4.0.6
C++17/20 Implementation of the .NET Base Class Library (BCL)
Toggle main menu visibility
Loading...
Searching...
No Matches
SqlCommand.cpp
Go to the documentation of this file.
1
#include "
pch.h
"
2
#include "
System/Data/SqlClient/SqlCommand.h
"
3
#include "
System/Data/SqlClient/SqlConnection.h
"
4
#include "
System/Data/SqlClient/SqlDataReader.h
"
5
#include "
System/Data/Internal/DatabaseEngine.h
"
6
7
namespace
DotNetDupe
{
8
namespace
System
{
9
namespace
Data
{
10
namespace
SqlClient
{
11
12
struct
SqlCommand::Impl {
13
DotNetDupe::System::String m_sCommandText;
14
DotNetDupe::System::SmartPointer<DotNetDupe::System::Data::Common::DbParameterCollection> m_parameters;
15
SqlConnection* m_connection =
nullptr
;
16
};
17
18
SqlCommand::SqlCommand
() : m_pImpl(
DotNetDupe
::
System
::
SmartPointer
<Impl>::NewShared()) {
19
m_pImpl->m_parameters =
DotNetDupe::System::SmartPointer<DotNetDupe::System::Data::Common::DbParameterCollection>::NewShared
();
20
}
21
22
SqlCommand::SqlCommand
(
const
DotNetDupe::System::String
& sText,
SqlConnection
* connection)
23
: m_pImpl(
DotNetDupe
::
System
::
SmartPointer
<Impl>::NewShared()) {
24
m_pImpl->m_sCommandText = sText;
25
m_pImpl->m_connection = connection;
26
m_pImpl->m_parameters =
DotNetDupe::System::SmartPointer<DotNetDupe::System::Data::Common::DbParameterCollection>::NewShared
();
27
}
28
29
SqlCommand::~SqlCommand
() =
default
;
30
31
DotNetDupe::System::String
SqlCommand::GetCommandText
()
const
{
32
return
m_pImpl->m_sCommandText;
33
}
34
35
void
SqlCommand::SetCommandText
(
const
DotNetDupe::System::String
& sText) {
36
m_pImpl->m_sCommandText = sText;
37
}
38
39
DotNetDupe::System::SmartPointer<DotNetDupe::System::Data::Common::DbParameterCollection>
SqlCommand::GetParameters
()
const
{
40
return
m_pImpl->m_parameters;
41
}
42
44
static
Collections::Generic::Dictionary<String, String>
ExtractDbParameters
(
const
DotNetDupe::System::SmartPointer<DotNetDupe::System::Data::Common::DbParameterCollection>
& pParams) {
45
Collections::Generic::Dictionary<String, String>
dict;
46
for
(
int
i = 0; i < pParams->GetCount(); ++i) {
47
auto
p = pParams->GetAt(i);
48
dict.
Add
(p->GetParameterName(), p->GetValue());
49
}
50
return
dict;
51
}
52
53
DotNetDupe::System::SmartPointer<DotNetDupe::System::Data::Common::DbDataReader>
SqlCommand::ExecuteReader
() {
55
DotNetDupe::System::String
dbName = (m_pImpl->m_connection ==
nullptr
) ?
DotNetDupe::System::String
(
"DefaultDb"
) : m_pImpl->m_connection->GetDatabaseName();
56
auto
params =
ExtractDbParameters
(m_pImpl->m_parameters);
57
Collections::Generic::List<String>
columns;
58
int
rowsAffected = 0;
59
61
auto
resultRows =
DotNetDupe::System::Data::Internal::DatabaseEngine::Instance
().
Execute
(dbName, m_pImpl->m_sCommandText, params, columns, rowsAffected);
62
return
DotNetDupe::System::SmartPointer<SqlDataReader>::NewShared
(std::move(resultRows), std::move(columns));
63
}
64
65
int
SqlCommand::ExecuteNonQuery
() {
67
DotNetDupe::System::String
dbName = (m_pImpl->m_connection ==
nullptr
) ?
DotNetDupe::System::String
(
"DefaultDb"
) : m_pImpl->m_connection->GetDatabaseName();
68
auto
params =
ExtractDbParameters
(m_pImpl->m_parameters);
69
Collections::Generic::List<String>
columns;
70
int
rowsAffected = 0;
71
73
DotNetDupe::System::Data::Internal::DatabaseEngine::Instance
().
Execute
(dbName, m_pImpl->m_sCommandText, params, columns, rowsAffected);
74
return
rowsAffected;
75
}
76
77
DotNetDupe::System::String
SqlCommand::ExecuteScalar
() {
79
auto
reader =
ExecuteReader
();
80
if
(reader->Read()) {
81
return
reader->GetString(0);
82
}
83
return
""
;
84
}
85
86
}
87
}
88
}
89
}
DatabaseEngine.h
SqlCommand.h
SqlConnection.h
SqlDataReader.h
DotNetDupe::System::Collections::Generic::Dictionary
Represents a collection of keys and values.
Definition
Dictionary.h:66
DotNetDupe::System::Collections::Generic::Dictionary::Add
void Add(const TKey &key, const TValue &value)
Definition
Dictionary.h:244
DotNetDupe::System::Collections::Generic::List
Represents a strongly typed list of objects accessible by index.
Definition
List.h:29
DotNetDupe::System::Data::Internal::DatabaseEngine::Execute
Collections::Generic::List< Row > Execute(const DotNetDupe::System::String &dbName, const String &sql, const Collections::Generic::Dictionary< String, String > ¶meters, Collections::Generic::List< String > &columnNames, int &rowsAffected)
Executes a SQL command against the named database backend.
Definition
DatabaseEngine.cpp:62
DotNetDupe::System::Data::Internal::DatabaseEngine::Instance
static DatabaseEngine & Instance()
Retrieves the singleton instance of the DatabaseEngine.
Definition
DatabaseEngine.cpp:24
DotNetDupe::System::Data::SqlClient::SqlCommand::ExecuteNonQuery
int ExecuteNonQuery() override
Executes a Transact-SQL statement against the connection and returns the number of rows affected.
Definition
SqlCommand.cpp:65
DotNetDupe::System::Data::SqlClient::SqlCommand::GetCommandText
DotNetDupe::System::String GetCommandText() const override
Gets the Transact-SQL statement or stored procedure to execute at the data source.
Definition
SqlCommand.cpp:31
DotNetDupe::System::Data::SqlClient::SqlCommand::GetParameters
DotNetDupe::System::SmartPointer< DotNetDupe::System::Data::Common::DbParameterCollection > GetParameters() const override
Gets the SqlParameterCollection.
Definition
SqlCommand.cpp:39
DotNetDupe::System::Data::SqlClient::SqlCommand::ExecuteReader
DotNetDupe::System::SmartPointer< DotNetDupe::System::Data::Common::DbDataReader > ExecuteReader() override
Sends the CommandText to the Connection and builds a SqlDataReader.
Definition
SqlCommand.cpp:53
DotNetDupe::System::Data::SqlClient::SqlCommand::SqlCommand
SqlCommand()
Initializes a new instance of the SqlCommand class.
Definition
SqlCommand.cpp:18
DotNetDupe::System::Data::SqlClient::SqlCommand::SetCommandText
void SetCommandText(const DotNetDupe::System::String &sText) override
Sets the Transact-SQL statement or stored procedure to execute at the data source.
Definition
SqlCommand.cpp:35
DotNetDupe::System::Data::SqlClient::SqlCommand::ExecuteScalar
DotNetDupe::System::String ExecuteScalar() override
Executes the query, and returns the first column of the first row in the result set returned by the q...
Definition
SqlCommand.cpp:77
DotNetDupe::System::Data::SqlClient::SqlCommand::~SqlCommand
~SqlCommand() override
Destructor releasing command resources.
DotNetDupe::System::Data::SqlClient::SqlConnection
Represents a connection to a SQL database.
Definition
SqlConnection.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::Data::SqlClient
Definition
SqlCommand.h:8
DotNetDupe::System::Data::SqlClient::ExtractDbParameters
static Collections::Generic::Dictionary< String, String > ExtractDbParameters(const DotNetDupe::System::SmartPointer< DotNetDupe::System::Data::Common::DbParameterCollection > &pParams)
Extract parameter key-value pairs into dictionary.
Definition
SqlCommand.cpp:44
DotNetDupe::System::Data
Definition
DbCommand.h:11
DotNetDupe::System
Definition
Action.h:11
DotNetDupe
Definition
IServiceCollection.h:7
pch.h
DotNetDupe
SqlCommand.cpp
Generated by
1.18.0