DotNetDupe 4.0.6
C++17/20 Implementation of the .NET Base Class Library (BCL)
Loading...
Searching...
No Matches
SqlCommand.cpp
Go to the documentation of this file.
1#include "pch.h"
6
7namespace 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
21
23 : m_pImpl(DotNetDupe::System::SmartPointer<Impl>::NewShared()) {
24 m_pImpl->m_sCommandText = sText;
25 m_pImpl->m_connection = connection;
27 }
28
29 SqlCommand::~SqlCommand() = default;
30
32 return m_pImpl->m_sCommandText;
33 }
34
36 m_pImpl->m_sCommandText = sText;
37 }
38
42
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
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);
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
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);
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
79 auto reader = ExecuteReader();
80 if (reader->Read()) {
81 return reader->GetString(0);
82 }
83 return "";
84 }
85
86 }
87 }
88 }
89}
Represents a collection of keys and values.
Definition Dictionary.h:66
void Add(const TKey &key, const TValue &value)
Definition Dictionary.h:244
Represents a strongly typed list of objects accessible by index.
Definition List.h:29
Collections::Generic::List< Row > Execute(const DotNetDupe::System::String &dbName, const String &sql, const Collections::Generic::Dictionary< String, String > &parameters, Collections::Generic::List< String > &columnNames, int &rowsAffected)
Executes a SQL command against the named database backend.
static DatabaseEngine & Instance()
Retrieves the singleton instance of the DatabaseEngine.
int ExecuteNonQuery() override
Executes a Transact-SQL statement against the connection and returns the number of rows affected.
DotNetDupe::System::String GetCommandText() const override
Gets the Transact-SQL statement or stored procedure to execute at the data source.
DotNetDupe::System::SmartPointer< DotNetDupe::System::Data::Common::DbParameterCollection > GetParameters() const override
Gets the SqlParameterCollection.
DotNetDupe::System::SmartPointer< DotNetDupe::System::Data::Common::DbDataReader > ExecuteReader() override
Sends the CommandText to the Connection and builds a SqlDataReader.
SqlCommand()
Initializes a new instance of the SqlCommand class.
void SetCommandText(const DotNetDupe::System::String &sText) override
Sets the Transact-SQL statement or stored procedure to execute at the data source.
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...
~SqlCommand() override
Destructor releasing command resources.
Represents a connection to a SQL database.
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
static Collections::Generic::Dictionary< String, String > ExtractDbParameters(const DotNetDupe::System::SmartPointer< DotNetDupe::System::Data::Common::DbParameterCollection > &pParams)
Extract parameter key-value pairs into dictionary.