Skip to content

Commit 292d8e9

Browse files
authored
Merge pull request #52 from microsoft/saumya/connectionclassc++
FEAT: adding connection class structure in C++
2 parents c7661d6 + eca18a0 commit 292d8e9

File tree

2 files changed

+105
-0
lines changed

2 files changed

+105
-0
lines changed
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
// Copyright (c) Microsoft Corporation.
2+
// Licensed under the MIT license.
3+
4+
// INFO|TODO - Note that is file is Windows specific right now. Making it arch agnostic will be
5+
// taken up in future
6+
7+
#include "connection.h"
8+
#include <iostream>
9+
10+
//-------------------------------------------------------------------------------------------------
11+
// Implements the Connection class declared in connection.h.
12+
// This class wraps low-level ODBC operations like connect/disconnect,
13+
// transaction control, and autocommit configuration.
14+
//-------------------------------------------------------------------------------------------------
15+
Connection::Connection(const std::wstring& conn_str) : _conn_str(conn_str) {}
16+
17+
Connection::~Connection() {
18+
LOG("Connection destructor called");
19+
close(); // Ensure the connection is closed when the object is destroyed.
20+
}
21+
22+
SQLRETURN Connection::connect() {
23+
LOG("Connecting to MSSQL");
24+
// to be added
25+
}
26+
27+
SQLRETURN Connection::close() {
28+
LOG("Disconnect from MSSQL");
29+
// to be added
30+
}
31+
32+
SQLRETURN Connection::commit() {
33+
LOG("Committing transaction");
34+
// to be added
35+
}
36+
37+
SQLRETURN Connection::rollback() {
38+
LOG("Rolling back transaction");
39+
// to be added
40+
}
41+
42+
SQLRETURN Connection::end_transaction(SQLSMALLINT completion_type) {
43+
// to be added
44+
}
45+
46+
SQLRETURN Connection::set_autocommit(bool enable) {
47+
LOG("Setting autocommit mode");
48+
// to be added
49+
}
50+
51+
bool Connection::get_autocommit() const {
52+
LOG("Getting autocommit mode");
53+
// to be added
54+
}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
// Copyright (c) Microsoft Corporation.
2+
// Licensed under the MIT license.
3+
4+
// INFO|TODO - Note that is file is Windows specific right now. Making it arch agnostic will be
5+
// taken up in future.
6+
7+
#ifndef CONNECTION_H
8+
#define CONNECTION_H
9+
10+
#include "ddbc_bindings.h"
11+
12+
// Represents a single ODBC database connection.
13+
// Manages its own environment and connection handles.
14+
// Note: This class does NOT implement pooling logic directly.
15+
16+
class Connection {
17+
public:
18+
Connection(const std::wstring& conn_str);
19+
~Connection();
20+
21+
// Establish the connection using the stored connection string.
22+
SQLRETURN connect();
23+
24+
// Close the connection and free resources.
25+
SQLRETURN close();
26+
27+
// Commit the current transaction.
28+
SQLRETURN commit();
29+
30+
// Rollback the current transaction.
31+
SQLRETURN rollback();
32+
33+
// End the transaction with the specified completion type.
34+
SQLRETURN end_transaction(SQLSMALLINT completion_type);
35+
36+
// Enable or disable autocommit mode.
37+
SQLRETURN set_autocommit(bool value);
38+
39+
// Check whether autocommit is enabled.
40+
bool get_autocommit() const;
41+
42+
private:
43+
44+
std::wstring _conn_str; // Connection string
45+
SqlHandlePtr _env_handle; // Environment handle
46+
SqlHandlePtr _dbc_handle; // Connection handle
47+
48+
bool _autocommit = false;
49+
std::shared_ptr<Connection> _conn;
50+
};
51+
#endif // CONNECTION_H

0 commit comments

Comments
 (0)