11#include " connection.h"
22#include " statement.h"
3+ #include " utils/error_manager.h"
34
45#include < cstring>
56#include < string>
@@ -35,8 +36,7 @@ SQLRETURN TConnection::DriverConnect(const std::string& connectionString) {
3536 Database_ = params[" Database" ];
3637
3738 if (Endpoint_.empty () || Database_.empty ()) {
38- AddError (" 08001" , 0 , " Missing Endpoint or Database in connection string" );
39- return SQL_ERROR;
39+ throw TOdbcException (" 08001" , 0 , " Missing Endpoint or Database in connection string" );
4040 }
4141
4242 YdbDriver_ = std::make_unique<NYdb::TDriver>(NYdb::TDriverConfig ()
@@ -64,8 +64,7 @@ SQLRETURN TConnection::Connect(const std::string& serverName,
6464 Database_ = database;
6565
6666 if (Endpoint_.empty () || Database_.empty ()) {
67- AddError (" 08001" , 0 , " Missing Endpoint or Database in DSN" );
68- return SQL_ERROR;
67+ throw TOdbcException (" 08001" , 0 , " Missing Endpoint or Database in DSN" );
6968 }
7069
7170 YdbDriver_ = std::make_unique<NYdb::TDriver>(NYdb::TDriverConfig ()
@@ -85,30 +84,6 @@ SQLRETURN TConnection::Disconnect() {
8584 return SQL_SUCCESS;
8685}
8786
88- SQLRETURN TConnection::GetDiagRec (SQLSMALLINT recNumber, SQLCHAR* sqlState, SQLINTEGER* nativeError,
89- SQLCHAR* messageText, SQLSMALLINT bufferLength, SQLSMALLINT* textLength) {
90- if (recNumber < 1 || recNumber > (SQLSMALLINT)Errors_.size ()) {
91- return SQL_NO_DATA;
92- }
93-
94- const auto & err = Errors_[recNumber-1 ];
95- if (sqlState) {
96- strncpy ((char *)sqlState, err.SqlState .c_str (), 6 );
97- }
98-
99- if (nativeError) {
100- *nativeError = err.NativeError ;
101- }
102-
103- if (messageText && bufferLength > 0 ) {
104- strncpy ((char *)messageText, err.Message .c_str (), bufferLength);
105- if (textLength) {
106- *textLength = (SQLSMALLINT)std::min ((int )err.Message .size (), (int )bufferLength);
107- }
108- }
109- return SQL_SUCCESS;
110- }
111-
11287std::unique_ptr<TStatement> TConnection::CreateStatement () {
11388 return std::make_unique<TStatement>(this );
11489}
@@ -118,22 +93,11 @@ void TConnection::RemoveStatement(TStatement* stmt) {
11893 [stmt](const std::unique_ptr<TStatement>& s) { return s.get () == stmt; }), Statements_.end ());
11994}
12095
121- void TConnection::AddError (const std::string& sqlState, SQLINTEGER nativeError, const std::string& message) {
122- Errors_.push_back ({sqlState, nativeError, message});
123- }
124-
125- void TConnection::ClearErrors () {
126- Errors_.clear ();
127- }
128-
12996SQLRETURN TConnection::SetAutocommit (bool value) {
13097 Autocommit_ = value;
13198 if (Autocommit_ && Tx_) {
13299 auto status = Tx_->Commit ().ExtractValueSync ();
133- if (!status.IsSuccess ()) {
134- AddError (" 08001" , 0 , " Failed to commit transaction" );
135- return SQL_ERROR;
136- }
100+ NStatusHelpers::ThrowOnError (status);
137101 Tx_.reset ();
138102 }
139103 return SQL_SUCCESS;
@@ -153,20 +117,14 @@ void TConnection::SetTx(const NQuery::TTransaction& tx) {
153117
154118SQLRETURN TConnection::CommitTx () {
155119 auto status = Tx_->Commit ().ExtractValueSync ();
156- if (!status.IsSuccess ()) {
157- AddError (" 08001" , 0 , " Failed to commit transaction" );
158- return SQL_ERROR;
159- }
120+ NStatusHelpers::ThrowOnError (status);
160121 Tx_.reset ();
161122 return SQL_SUCCESS;
162123}
163124
164125SQLRETURN TConnection::RollbackTx () {
165126 auto status = Tx_->Rollback ().ExtractValueSync ();
166- if (!status.IsSuccess ()) {
167- AddError (" 08001" , 0 , " Failed to rollback transaction" );
168- return SQL_ERROR;
169- }
127+ NStatusHelpers::ThrowOnError (status);
170128 Tx_.reset ();
171129 return SQL_SUCCESS;
172130}
0 commit comments